3 Serialisation Support Functions
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 NOTE: The following functions should work on any
36 vaguely contemporary platform. Production
37 builds should use optimised macros (void
38 on platforms with network byte order and IEEE
39 floating point format as native.
43 /* serial_int16 -- Serialise a signed 16 bit integer. */
45 void serial_int16(uint8_t * * const ptr, const int16_t v)
47 int16_t vo = htons(v);
49 memcpy(*ptr, &vo, sizeof vo);
53 /* serial_uint16 -- Serialise an unsigned 16 bit integer. */
55 void serial_uint16(uint8_t * * const ptr, const uint16_t v)
57 uint16_t vo = htons(v);
59 memcpy(*ptr, &vo, sizeof vo);
63 /* serial_int32 -- Serialise a signed 32 bit integer. */
65 void serial_int32(uint8_t * * const ptr, const int32_t v)
67 int32_t vo = htonl(v);
69 memcpy(*ptr, &vo, sizeof vo);
73 /* serial_uint32 -- Serialise an unsigned 32 bit integer. */
75 void serial_uint32(uint8_t * * const ptr, const uint32_t v)
77 uint32_t vo = htonl(v);
79 memcpy(*ptr, &vo, sizeof vo);
83 /* serial_int64 -- Serialise a signed 64 bit integer. */
85 void serial_int64(uint8_t * * const ptr, const int64_t v)
88 memcpy(*ptr, &v, sizeof(int64_t));
91 uint8_t rv[sizeof(int64_t)];
92 uint8_t *pv = (uint8_t *) &v;
94 for (i = 0; i < 8; i++) {
97 memcpy(*ptr, &rv, sizeof(int64_t));
99 *ptr += sizeof(int64_t);
103 /* serial_uint64 -- Serialise an unsigned 64 bit integer. */
105 void serial_uint64(uint8_t * * const ptr, const uint64_t v)
107 if (htonl(1) == 1L) {
108 memcpy(*ptr, &v, sizeof(uint64_t));
111 uint8_t rv[sizeof(uint64_t)];
112 uint8_t *pv = (uint8_t *) &v;
114 for (i = 0; i < 8; i++) {
117 memcpy(*ptr, &rv, sizeof(uint64_t));
119 *ptr += sizeof(uint64_t);
123 /* serial_btime -- Serialise an btime_t 64 bit integer. */
125 void serial_btime(uint8_t * * const ptr, const btime_t v)
127 if (htonl(1) == 1L) {
128 memcpy(*ptr, &v, sizeof(btime_t));
131 uint8_t rv[sizeof(btime_t)];
132 uint8_t *pv = (uint8_t *) &v;
134 for (i = 0; i < 8; i++) {
137 memcpy(*ptr, &rv, sizeof(btime_t));
139 *ptr += sizeof(btime_t);
144 /* serial_float64 -- Serialise a 64 bit IEEE floating point number.
145 This code assumes that the host floating point
146 format is IEEE and that floating point quantities
147 are stored in IEEE format either LSB first or MSB
148 first. More creative host formats will require
149 additional transformations here. */
151 void serial_float64(uint8_t * * const ptr, const float64_t v)
153 if (htonl(1) == 1L) {
154 memcpy(*ptr, &v, sizeof(float64_t));
157 uint8_t rv[sizeof(float64_t)];
158 uint8_t *pv = (uint8_t *) &v;
160 for (i = 0; i < 8; i++) {
163 memcpy(*ptr, &rv, sizeof(float64_t));
165 *ptr += sizeof(float64_t);
168 void serial_string(uint8_t * * const ptr, const char * const str)
170 int len = strlen(str) + 1;
172 memcpy(*ptr, str, len);
177 /* unserial_int16 -- Unserialise a signed 16 bit integer. */
179 int16_t unserial_int16(uint8_t * * const ptr)
183 memcpy(&vo, *ptr, sizeof vo);
188 /* unserial_uint16 -- Unserialise an unsigned 16 bit integer. */
190 uint16_t unserial_uint16(uint8_t * * const ptr)
194 memcpy(&vo, *ptr, sizeof vo);
199 /* unserial_int32 -- Unserialise a signed 32 bit integer. */
201 int32_t unserial_int32(uint8_t * * const ptr)
205 memcpy(&vo, *ptr, sizeof vo);
210 /* unserial_uint32 -- Unserialise an unsigned 32 bit integer. */
212 uint32_t unserial_uint32(uint8_t * * const ptr)
216 memcpy(&vo, *ptr, sizeof vo);
221 /* unserial_uint64 -- Unserialise an unsigned 64 bit integer. */
223 uint64_t unserial_uint64(uint8_t * * const ptr)
227 if (htonl(1) == 1L) {
228 memcpy(&v, *ptr, sizeof(uint64_t));
231 uint8_t rv[sizeof(uint64_t)];
232 uint8_t *pv = (uint8_t *) &v;
234 memcpy(&v, *ptr, sizeof(uint64_t));
235 for (i = 0; i < 8; i++) {
238 memcpy(&v, &rv, sizeof(uint64_t));
240 *ptr += sizeof(uint64_t);
244 /* unserial_btime -- Unserialise a btime_t 64 bit integer. */
246 btime_t unserial_btime(uint8_t * * const ptr)
250 if (htonl(1) == 1L) {
251 memcpy(&v, *ptr, sizeof(btime_t));
254 uint8_t rv[sizeof(btime_t)];
255 uint8_t *pv = (uint8_t *) &v;
257 memcpy(&v, *ptr, sizeof(btime_t));
258 for (i = 0; i < 8; i++) {
261 memcpy(&v, &rv, sizeof(btime_t));
263 *ptr += sizeof(btime_t);
269 /* unserial_float64 -- Unserialise a 64 bit IEEE floating point number.
270 This code assumes that the host floating point
271 format is IEEE and that floating point quantities
272 are stored in IEEE format either LSB first or MSB
273 first. More creative host formats will require
274 additional transformations here. */
276 float64_t unserial_float64(uint8_t * * const ptr)
280 if (htonl(1) == 1L) {
281 memcpy(&v, *ptr, sizeof(float64_t));
284 uint8_t rv[sizeof(float64_t)];
285 uint8_t *pv = (uint8_t *) &v;
287 memcpy(&v, *ptr, sizeof(float64_t));
288 for (i = 0; i < 8; i++) {
291 memcpy(&v, &rv, sizeof(float64_t));
293 *ptr += sizeof(float64_t);
297 void unserial_string(uint8_t * * const ptr, char * const str)
299 int len = strlen((char *) *ptr) + 1;
300 memcpy(str, (char *) *ptr, len);