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