Coverage Report

Created: 2025-12-03 10:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-toxcore/toxcore/state.c
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2020 The TokTok team.
3
 * Copyright © 2014 Tox project.
4
 */
5
#include "state.h"
6
7
#include <string.h>
8
9
#include "ccompat.h"
10
#include "logger.h"
11
12
/** state load/save */
13
int state_load(const Logger *_Nonnull log, state_load_cb *_Nonnull state_load_callback, void *_Nonnull outer,
14
               const uint8_t *_Nonnull data, uint32_t length, uint16_t cookie_inner)
15
2.06k
{
16
2.06k
    if (state_load_callback == nullptr || data == nullptr) {
17
0
        LOGGER_ERROR(log, "state_load() called with invalid args.");
18
0
        return -1;
19
0
    }
20
21
2.06k
    const uint32_t size_head = sizeof(uint32_t) * 2;
22
23
32.5k
    while (length >= size_head) {
24
30.8k
        uint32_t length_sub;
25
30.8k
        lendian_bytes_to_host32(&length_sub, data);
26
27
30.8k
        uint32_t cookie_type;
28
30.8k
        lendian_bytes_to_host32(&cookie_type, data + sizeof(uint32_t));
29
30
30.8k
        data += size_head;
31
30.8k
        length -= size_head;
32
33
30.8k
        if (length < length_sub) {
34
            /* file truncated */
35
218
            LOGGER_ERROR(log, "state file too short: %u < %u", length, length_sub);
36
218
            return -1;
37
218
        }
38
39
30.6k
        if (lendian_to_host16(cookie_type >> 16) != cookie_inner) {
40
            /* something is not matching up in a bad way, give up */
41
28
            LOGGER_ERROR(log, "state file garbled: %04x != %04x", cookie_type >> 16, cookie_inner);
42
28
            return -1;
43
28
        }
44
45
30.6k
        const uint16_t type = lendian_to_host16(cookie_type & 0xFFFF);
46
47
30.6k
        switch (state_load_callback(outer, data, length_sub, type)) {
48
30.5k
            case STATE_LOAD_STATUS_CONTINUE: {
49
30.5k
                data += length_sub;
50
30.5k
                length -= length_sub;
51
30.5k
                break;
52
0
            }
53
54
104
            case STATE_LOAD_STATUS_ERROR: {
55
104
                LOGGER_ERROR(log, "Error occcured in state file (type: 0x%02x).", type);
56
104
                return -1;
57
0
            }
58
59
6
            case STATE_LOAD_STATUS_END: {
60
6
                return 0;
61
0
            }
62
30.6k
        }
63
30.6k
    }
64
65
1.71k
    if (length != 0) {
66
396
        LOGGER_ERROR(log, "unparsed data in state file of length %u", length);
67
396
        return -1;
68
396
    }
69
70
1.31k
    return 0;
71
1.71k
}
72
73
uint8_t *_Nonnull state_write_section_header(uint8_t *_Nonnull data, uint16_t cookie_type, uint32_t len, uint32_t section_type)
74
12.7k
{
75
12.7k
    host_to_lendian_bytes32(data, len);
76
12.7k
    data += sizeof(uint32_t);
77
12.7k
    host_to_lendian_bytes32(data, (host_to_lendian16(cookie_type) << 16) | host_to_lendian16(section_type));
78
12.7k
    data += sizeof(uint32_t);
79
12.7k
    return data;
80
12.7k
}
81
82
uint16_t lendian_to_host16(uint16_t lendian)
83
86.8k
{
84
#ifdef WORDS_BIGENDIAN
85
    return (lendian << 8) | (lendian >> 8);
86
#else
87
86.8k
    return lendian;
88
86.8k
#endif /* WORDS_BIGENDIAN */
89
86.8k
}
90
91
uint16_t host_to_lendian16(uint16_t host)
92
25.5k
{
93
25.5k
    return lendian_to_host16(host);
94
25.5k
}
95
96
void host_to_lendian_bytes64(uint8_t *_Nonnull dest, uint64_t num)
97
204
{
98
#ifdef WORDS_BIGENDIAN
99
    num = ((num << 8) & 0xFF00FF00FF00FF00) | ((num >> 8) & 0xFF00FF00FF00FF);
100
    num = ((num << 16) & 0xFFFF0000FFFF0000) | ((num >> 16) & 0xFFFF0000FFFF);
101
    num = (num << 32) | (num >> 32);
102
#endif /* WORDS_BIGENDIAN */
103
204
    memcpy(dest, &num, sizeof(uint64_t));
104
204
}
105
106
void lendian_bytes_to_host64(uint64_t *_Nonnull dest, const uint8_t *_Nonnull lendian)
107
409
{
108
409
    uint64_t d;
109
409
    memcpy(&d, lendian, sizeof(uint64_t));
110
#ifdef WORDS_BIGENDIAN
111
    d = ((d << 8) & 0xFF00FF00FF00FF00) | ((d >> 8) & 0xFF00FF00FF00FF);
112
    d = ((d << 16) & 0xFFFF0000FFFF0000) | ((d >> 16) & 0xFFFF0000FFFF);
113
    d = (d << 32) | (d >> 32);
114
#endif /* WORDS_BIGENDIAN */
115
409
    *dest = d;
116
409
}
117
118
void host_to_lendian_bytes32(uint8_t *_Nonnull dest, uint32_t num)
119
31.9k
{
120
#ifdef WORDS_BIGENDIAN
121
    num = ((num << 8) & 0xFF00FF00) | ((num >> 8) & 0xFF00FF);
122
    num = (num << 16) | (num >> 16);
123
#endif /* WORDS_BIGENDIAN */
124
31.9k
    memcpy(dest, &num, sizeof(uint32_t));
125
31.9k
}
126
127
void lendian_bytes_to_host32(uint32_t *_Nonnull dest, const uint8_t *_Nonnull lendian)
128
75.8k
{
129
75.8k
    uint32_t d;
130
75.8k
    memcpy(&d, lendian, sizeof(uint32_t));
131
#ifdef WORDS_BIGENDIAN
132
    d = ((d << 8) & 0xFF00FF00) | ((d >> 8) & 0xFF00FF);
133
    d = (d << 16) | (d >> 16);
134
#endif /* WORDS_BIGENDIAN */
135
75.8k
    *dest = d;
136
75.8k
}
137
138
void host_to_lendian_bytes16(uint8_t *_Nonnull dest, uint16_t num)
139
3.50k
{
140
#ifdef WORDS_BIGENDIAN
141
    num = (num << 8) | (num >> 8);
142
#endif /* WORDS_BIGENDIAN */
143
3.50k
    memcpy(dest, &num, sizeof(uint16_t));
144
3.50k
}
145
146
void lendian_bytes_to_host16(uint16_t *_Nonnull dest, const uint8_t *_Nonnull lendian)
147
11.8k
{
148
11.8k
    uint16_t d;
149
11.8k
    memcpy(&d, lendian, sizeof(uint16_t));
150
#ifdef WORDS_BIGENDIAN
151
    d = (d << 8) | (d >> 8);
152
#endif /* WORDS_BIGENDIAN */
153
11.8k
    *dest = d;
154
11.8k
}