1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2010, 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.
37 * Data serialization functions below
40 typedef union double64u {
45 static inline void append_val(pds_t *pds, uint64_t val)
47 if (pds->offset < pds->maxsize)
48 pds->data[pds->offset++] = ((uint8_t)(val));
55 void Pds_append_data(pds_t *pds, const uint8_t *data, uint32_t len)
58 Pds_add_numval(pds, len);
59 left = pds->maxsize - pds->offset;
61 memcpy(&pds->data[pds->offset], data, len);
64 memset(&pds->data[pds->offset], 0, left);
66 pds->overshoot += len - left;
71 void Pds_append_data_nosize(pds_t *pds, const uint8_t *data, uint32_t len)
74 left = pds->maxsize - pds->offset;
76 memcpy(&pds->data[pds->offset], data, len);
79 memset(&pds->data[pds->offset], 0, left);
81 pds->overshoot += len - left;
86 uint8_t Pds_next8(pds_t *pds)
88 if (pds->offset < pds->maxsize)
89 return pds->data[pds->offset++];
96 int Pds_skip(pds_t *pds, int offset)
98 if (pds->offset + offset <= pds->maxsize) {
99 pds->offset += offset;
108 static inline uint64_t next(pds_t *pds)
110 if (pds->offset < pds->maxsize)
111 return pds->data[pds->offset++];
118 pds_t *Pds_create(uint8_t *buf, int size)
120 pds_t *pds = malloc(sizeof(pds_t));
122 Log_fatal("Out of memory");
124 pds->offset = pds->overshoot = 0;
130 void Pds_free(pds_t *pds)
135 void Pds_add_double(pds_t *pds, double value)
141 Pds_add_numval(pds, u.u64);
144 double Pds_get_double(pds_t *pds)
147 u.u64 = Pds_get_numval(pds);
150 void Pds_add_numval(pds_t *pds, const uint64_t value)
154 if ((i & 0x8000000000000000LL) && (~i < 0x100000000LL)) {
158 // Shortcase for -1 to -4
159 append_val(pds, 0xFC | i);
162 append_val(pds, 0xF8);
166 // Need top bit clear
168 } else if (i < 0x4000) {
169 // Need top two bits clear
170 append_val(pds, (i >> 8) | 0x80);
171 append_val(pds, i & 0xFF);
172 } else if (i < 0x200000) {
173 // Need top three bits clear
174 append_val(pds, (i >> 16) | 0xC0);
175 append_val(pds, (i >> 8) & 0xFF);
176 append_val(pds, i & 0xFF);
177 } else if (i < 0x10000000) {
178 // Need top four bits clear
179 append_val(pds, (i >> 24) | 0xE0);
180 append_val(pds, (i >> 16) & 0xFF);
181 append_val(pds, (i >> 8) & 0xFF);
182 append_val(pds, i & 0xFF);
183 } else if (i < 0x100000000LL) {
184 // It's a full 32-bit integer.
185 append_val(pds, 0xF0);
186 append_val(pds, (i >> 24) & 0xFF);
187 append_val(pds, (i >> 16) & 0xFF);
188 append_val(pds, (i >> 8) & 0xFF);
189 append_val(pds, i & 0xFF);
191 // It's a 64-bit value.
192 append_val(pds, 0xF4);
193 append_val(pds, (i >> 56) & 0xFF);
194 append_val(pds, (i >> 48) & 0xFF);
195 append_val(pds, (i >> 40) & 0xFF);
196 append_val(pds, (i >> 32) & 0xFF);
197 append_val(pds, (i >> 24) & 0xFF);
198 append_val(pds, (i >> 16) & 0xFF);
199 append_val(pds, (i >> 8) & 0xFF);
200 append_val(pds, i & 0xFF);
204 uint64_t Pds_get_numval(pds_t *pds)
207 uint64_t v = next(pds);
209 if ((v & 0x80) == 0x00) {
211 } else if ((v & 0xC0) == 0x80) {
212 i=(v & 0x3F) << 8 | next(pds);
213 } else if ((v & 0xF0) == 0xF0) {
216 i=next(pds) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
219 i=next(pds) << 56 | next(pds) << 48 | next(pds) << 40 | next(pds) << 32 | next(pds) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
222 i = Pds_get_numval(pds);
234 } else if ((v & 0xF0) == 0xE0) {
235 i=(v & 0x0F) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
236 } else if ((v & 0xE0) == 0xC0) {
237 i=(v & 0x1F) << 16 | next(pds) << 8 | next(pds);
242 void Pds_add_string(pds_t *pds, const char *str)
244 Pds_append_data(pds, (uint8_t *)str, strlen(str));
247 void Pds_get_string(pds_t *pds, char *str, int maxlen)
249 int len = Pds_get_numval(pds);
251 memcpy(str, &pds->data[pds->offset], len);
255 Log_warn("Too long string from network");
260 int Pds_get_data(pds_t *pds, uint8_t *data, int maxlen)
262 int len = Pds_get_numval(pds);
264 memcpy(data, &pds->data[pds->offset], len);
268 Log_warn("Pds_get_data: Too much data from network");