1 /* Copyright (C) 2009, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2009, 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 static inline uint64_t next(pds_t *pds)
88 if (pds->offset < pds->maxsize)
89 return pds->data[pds->offset++];
96 pds_t *Pds_create(uint8_t *buf, int size)
98 pds_t *pds = malloc(sizeof(pds_t));
100 Log_fatal("Out of memory");
102 pds->offset = pds->overshoot = 0;
108 void Pds_free(pds_t *pds)
113 void Pds_add_double(pds_t *pds, double value)
119 Pds_add_numval(pds, u.u64);
122 double Pds_get_double(pds_t *pds)
125 u.u64 = Pds_get_numval(pds);
128 void Pds_add_numval(pds_t *pds, const uint64_t value)
132 if ((i & 0x8000000000000000LL) && (~i < 0x100000000LL)) {
136 // Shortcase for -1 to -4
137 append_val(pds, 0xFC | i);
140 append_val(pds, 0xF8);
144 // Need top bit clear
146 } else if (i < 0x4000) {
147 // Need top two bits clear
148 append_val(pds, (i >> 8) | 0x80);
149 append_val(pds, i & 0xFF);
150 } else if (i < 0x200000) {
151 // Need top three bits clear
152 append_val(pds, (i >> 16) | 0xC0);
153 append_val(pds, (i >> 8) & 0xFF);
154 append_val(pds, i & 0xFF);
155 } else if (i < 0x10000000) {
156 // Need top four bits clear
157 append_val(pds, (i >> 24) | 0xE0);
158 append_val(pds, (i >> 16) & 0xFF);
159 append_val(pds, (i >> 8) & 0xFF);
160 append_val(pds, i & 0xFF);
161 } else if (i < 0x100000000LL) {
162 // It's a full 32-bit integer.
163 append_val(pds, 0xF0);
164 append_val(pds, (i >> 24) & 0xFF);
165 append_val(pds, (i >> 16) & 0xFF);
166 append_val(pds, (i >> 8) & 0xFF);
167 append_val(pds, i & 0xFF);
169 // It's a 64-bit value.
170 append_val(pds, 0xF4);
171 append_val(pds, (i >> 56) & 0xFF);
172 append_val(pds, (i >> 48) & 0xFF);
173 append_val(pds, (i >> 40) & 0xFF);
174 append_val(pds, (i >> 32) & 0xFF);
175 append_val(pds, (i >> 24) & 0xFF);
176 append_val(pds, (i >> 16) & 0xFF);
177 append_val(pds, (i >> 8) & 0xFF);
178 append_val(pds, i & 0xFF);
182 uint64_t Pds_get_numval(pds_t *pds)
185 uint64_t v = next(pds);
187 if ((v & 0x80) == 0x00) {
189 } else if ((v & 0xC0) == 0x80) {
190 i=(v & 0x3F) << 8 | next(pds);
191 } else if ((v & 0xF0) == 0xF0) {
194 i=next(pds) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
197 i=next(pds) << 56 | next(pds) << 48 | next(pds) << 40 | next(pds) << 32 | next(pds) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
200 i = Pds_get_numval(pds);
212 } else if ((v & 0xF0) == 0xE0) {
213 i=(v & 0x0F) << 24 | next(pds) << 16 | next(pds) << 8 | next(pds);
214 } else if ((v & 0xE0) == 0xC0) {
215 i=(v & 0x1F) << 16 | next(pds) << 8 | next(pds);
220 void Pds_add_string(pds_t *pds, const char *str)
222 Pds_append_data(pds, (uint8_t *)str, strlen(str));
225 void Pds_get_string(pds_t *pds, char *str, int maxlen)
227 int len = Pds_get_numval(pds);
229 memcpy(str, &pds->data[pds->offset], len);
233 Log_warn("Too long string from network");
238 int Pds_get_data(pds_t *pds, uint8_t *data, int maxlen)
240 int len = Pds_get_numval(pds);
242 memcpy(data, &pds->data[pds->offset], len);
246 Log_warn("Pds_get_data: Too much data from network");