Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/state.c
Line
Count
Source (jump to first uncovered line)
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 *log, state_load_cb *state_load_callback, void *outer,
14
               const uint8_t *data, uint32_t length, uint16_t cookie_inner)
15
1.67k
{
16
1.67k
    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
22
1.67k
    const uint32_t size_head = sizeof(uint32_t) * 2;
23
24
20.8k
    while (length >= size_head) {
25
19.4k
        uint32_t length_sub;
26
19.4k
        lendian_bytes_to_host32(&length_sub, data);
27
28
19.4k
        uint32_t cookie_type;
29
19.4k
        lendian_bytes_to_host32(&cookie_type, data + sizeof(uint32_t));
30
31
19.4k
        data += size_head;
32
19.4k
        length -= size_head;
33
34
19.4k
        if (length < length_sub) {
35
            /* file truncated */
36
155
            LOGGER_ERROR(log, "state file too short: %u < %u", length, length_sub);
37
155
            return -1;
38
155
        }
39
40
19.2k
        if (lendian_to_host16(cookie_type >> 16) != cookie_inner) {
41
            /* something is not matching up in a bad way, give up */
42
28
            LOGGER_ERROR(log, "state file garbled: %04x != %04x", cookie_type >> 16, cookie_inner);
43
28
            return -1;
44
28
        }
45
46
19.2k
        const uint16_t type = lendian_to_host16(cookie_type & 0xFFFF);
47
48
19.2k
        switch (state_load_callback(outer, data, length_sub, type)) {
49
19.1k
            case STATE_LOAD_STATUS_CONTINUE: {
50
19.1k
                data += length_sub;
51
19.1k
                length -= length_sub;
52
19.1k
                break;
53
0
            }
54
55
93
            case STATE_LOAD_STATUS_ERROR: {
56
93
                LOGGER_ERROR(log, "Error occcured in state file (type: 0x%02x).", type);
57
93
                return -1;
58
0
            }
59
60
32
            case STATE_LOAD_STATUS_END: {
61
32
                return 0;
62
0
            }
63
19.2k
        }
64
19.2k
    }
65
66
1.36k
    if (length != 0) {
67
312
        LOGGER_ERROR(log, "unparsed data in state file of length %u", length);
68
312
        return -1;
69
312
    }
70
71
1.05k
    return 0;
72
1.36k
}
73
74
uint8_t *state_write_section_header(uint8_t *data, uint16_t cookie_type, uint32_t len, uint32_t section_type)
75
11.8k
{
76
11.8k
    host_to_lendian_bytes32(data, len);
77
11.8k
    data += sizeof(uint32_t);
78
11.8k
    host_to_lendian_bytes32(data, (host_to_lendian16(cookie_type) << 16) | host_to_lendian16(section_type));
79
11.8k
    data += sizeof(uint32_t);
80
11.8k
    return data;
81
11.8k
}
82
83
uint16_t lendian_to_host16(uint16_t lendian)
84
62.1k
{
85
#ifdef WORDS_BIGENDIAN
86
    return (lendian << 8) | (lendian >> 8);
87
#else
88
62.1k
    return lendian;
89
62.1k
#endif /* WORDS_BIGENDIAN */
90
62.1k
}
91
92
uint16_t host_to_lendian16(uint16_t host)
93
23.6k
{
94
23.6k
    return lendian_to_host16(host);
95
23.6k
}
96
97
void host_to_lendian_bytes64(uint8_t *dest, uint64_t num)
98
313
{
99
#ifdef WORDS_BIGENDIAN
100
    num = ((num << 8) & 0xFF00FF00FF00FF00) | ((num >> 8) & 0xFF00FF00FF00FF);
101
    num = ((num << 16) & 0xFFFF0000FFFF0000) | ((num >> 16) & 0xFFFF0000FFFF);
102
    num = (num << 32) | (num >> 32);
103
#endif /* WORDS_BIGENDIAN */
104
313
    memcpy(dest, &num, sizeof(uint64_t));
105
313
}
106
107
void lendian_bytes_to_host64(uint64_t *dest, const uint8_t *lendian)
108
460
{
109
460
    uint64_t d;
110
460
    memcpy(&d, lendian, sizeof(uint64_t));
111
#ifdef WORDS_BIGENDIAN
112
    d = ((d << 8) & 0xFF00FF00FF00FF00) | ((d >> 8) & 0xFF00FF00FF00FF);
113
    d = ((d << 16) & 0xFFFF0000FFFF0000) | ((d >> 16) & 0xFFFF0000FFFF);
114
    d = (d << 32) | (d >> 32);
115
#endif /* WORDS_BIGENDIAN */
116
460
    *dest = d;
117
460
}
118
119
void host_to_lendian_bytes32(uint8_t *dest, uint32_t num)
120
28.3k
{
121
#ifdef WORDS_BIGENDIAN
122
    num = ((num << 8) & 0xFF00FF00) | ((num >> 8) & 0xFF00FF);
123
    num = (num << 16) | (num >> 16);
124
#endif /* WORDS_BIGENDIAN */
125
28.3k
    memcpy(dest, &num, sizeof(uint32_t));
126
28.3k
}
127
128
void lendian_bytes_to_host32(uint32_t *dest, const uint8_t *lendian)
129
44.4k
{
130
44.4k
    uint32_t d;
131
44.4k
    memcpy(&d, lendian, sizeof(uint32_t));
132
#ifdef WORDS_BIGENDIAN
133
    d = ((d << 8) & 0xFF00FF00) | ((d >> 8) & 0xFF00FF);
134
    d = (d << 16) | (d >> 16);
135
#endif /* WORDS_BIGENDIAN */
136
44.4k
    *dest = d;
137
44.4k
}
138
139
void host_to_lendian_bytes16(uint8_t *dest, uint16_t num)
140
2.13k
{
141
#ifdef WORDS_BIGENDIAN
142
    num = (num << 8) | (num >> 8);
143
#endif /* WORDS_BIGENDIAN */
144
2.13k
    memcpy(dest, &num, sizeof(uint16_t));
145
2.13k
}
146
147
void lendian_bytes_to_host16(uint16_t *dest, const uint8_t *lendian)
148
3.81k
{
149
3.81k
    uint16_t d;
150
3.81k
    memcpy(&d, lendian, sizeof(uint16_t));
151
#ifdef WORDS_BIGENDIAN
152
    d = (d << 8) | (d >> 8);
153
#endif /* WORDS_BIGENDIAN */
154
3.81k
    *dest = d;
155
3.81k
}