1 /* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 - Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 - Neither the name of the Developers nor the names of its contributors may
16 be used to endorse or promote products derived from this software without
17 specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Data serialization functions below
41 typedef union double64u {
46 static inline void append_val(pds_t *pds, uint64_t val)
48 if (pds->offset < pds->maxsize)
49 pds->data[pds->offset++] = ((uint8_t)(val));
56 void Pds_append_data(pds_t *pds, const uint8_t *data, uint32_t len)
59 Pds_add_numval(pds, len);
60 left = pds->maxsize - pds->offset;
62 memcpy(&pds->data[pds->offset], data, len);
65 memset(&pds->data[pds->offset], 0, left);
67 pds->overshoot += len - left;
72 void Pds_append_data_nosize(pds_t *pds, const uint8_t *data, uint32_t len)
75 left = pds->maxsize - pds->offset;
77 memcpy(&pds->data[pds->offset], data, len);
80 memset(&pds->data[pds->offset], 0, left);
82 pds->overshoot += len - left;
87 uint8_t Pds_next8(pds_t *pds)
89 if (pds->offset < pds->maxsize)
90 return pds->data[pds->offset++];
97 int Pds_skip(pds_t *pds, int offset)
99 if (pds->offset + offset <= pds->maxsize) {
100 pds->offset += offset;
109 static inline uint64_t next(pds_t *pds)
111 if (pds->offset < pds->maxsize)
112 return pds->data[pds->offset++];
119 pds_t *Pds_create(uint8_t *buf, int size)
121 pds_t *pds = Memory_safeMalloc(1, sizeof(pds_t));
123 pds->offset = pds->overshoot = 0;
129 void Pds_free(pds_t *pds)
134 void Pds_add_double(pds_t *pds, double value)
140 Pds_add_numval(pds, u.u64);
143 double Pds_get_double(pds_t *pds)
146 u.u64 = Pds_get_numval(pds);
149 void Pds_add_numval(pds_t *pds, const uint64_t value)
153 if ((i & 0x8000000000000000LL) && (~i < 0x100000000LL)) {
157 // Shortcase for -1 to -4
158 append_val(pds, 0xFC | i);
161 append_val(pds, 0xF8);
165 // Need top bit clear
167 } else if (i < 0x4000) {
168 // Need top two bits clear
169 append_val(pds, (i >> 8) | 0x80);
170 append_val(pds, i & 0xFF);
171 } else if (i < 0x200000) {
172 // Need top three bits clear
173 append_val(pds, (i >> 16) | 0xC0);
174 append_val(pds, (i >> 8) & 0xFF);
175 append_val(pds, i & 0xFF);
176 } else if (i < 0x10000000) {
177 // Need top four bits clear
178 append_val(pds, (i >> 24) | 0xE0);
179 append_val(pds, (i >> 16) & 0xFF);
180 append_val(pds, (i >> 8) & 0xFF);
181 append_val(pds, i & 0xFF);
182 } else if (i < 0x100000000LL) {
183 // It's a full 32-bit integer.
184 append_val(pds, 0xF0);
185 append_val(pds, (i >> 24) & 0xFF);
186 append_val(pds, (i >> 16) & 0xFF);
187 append_val(pds, (i >> 8) & 0xFF);
188 append_val(pds, i & 0xFF);
190 // It's a 64-bit value.
191 append_val(pds, 0xF4);
192 append_val(pds, (i >> 56) & 0xFF);
193 append_val(pds, (i >> 48) & 0xFF);
194 append_val(pds, (i >> 40) & 0xFF);
195 append_val(pds, (i >> 32) & 0xFF);
196 append_val(pds, (i >> 24) & 0xFF);
197 append_val(pds, (i >> 16) & 0xFF);
198 append_val(pds, (i >> 8) & 0xFF);
199 append_val(pds, i & 0xFF);
203 uint64_t Pds_get_numval(pds_t *pds)
206 uint64_t v = next(pds);
208 if ((v & 0x80) == 0x00) {
210 } else if ((v & 0xC0) == 0x80) {
211 i=(v & 0x3F) << 8 | next(pds);
212 } else if ((v & 0xF0) == 0xF0) {
215 i=next(pds) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
218 i=next(pds) << 56 | next(pds) << 48 | next(pds) << 40 | next(pds) << 32 | next(pds) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
221 i = Pds_get_numval(pds);
233 } else if ((v & 0xF0) == 0xE0) {
234 i=(v & 0x0F) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
235 } else if ((v & 0xE0) == 0xC0) {
236 i=(v & 0x1F) << 16 | next(pds) << 8 | next(pds);
241 void Pds_add_string(pds_t *pds, const char *str)
243 Pds_append_data(pds, (uint8_t *)str, strlen(str));
246 void Pds_get_string(pds_t *pds, char *str, int maxlen)
248 int len = Pds_get_numval(pds);
250 memcpy(str, &pds->data[pds->offset], len);
254 Log_warn("Too long string from network");
259 int Pds_get_data(pds_t *pds, uint8_t *data, int maxlen)
261 int len = Pds_get_numval(pds);
263 memcpy(data, &pds->data[pds->offset], len);
267 Log_warn("Pds_get_data: Too much data from network");