3 Serialisation Support Functions
10 Copyright (C) 2000-2006 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 version 2 as amended with additional clauses defined in the
15 file LICENSE in the main source directory.
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
20 the file LICENSE for additional details.
30 NOTE: The following functions should work on any
31 vaguely contemporary platform. Production
32 builds should use optimised macros (void
33 on platforms with network byte order and IEEE
34 floating point format as native.
38 /* serial_int16 -- Serialise a signed 16 bit integer. */
40 void serial_int16(uint8_t * * const ptr, const int16_t v)
42 int16_t vo = htons(v);
44 memcpy(*ptr, &vo, sizeof vo);
48 /* serial_uint16 -- Serialise an unsigned 16 bit integer. */
50 void serial_uint16(uint8_t * * const ptr, const uint16_t v)
52 uint16_t vo = htons(v);
54 memcpy(*ptr, &vo, sizeof vo);
58 /* serial_int32 -- Serialise a signed 32 bit integer. */
60 void serial_int32(uint8_t * * const ptr, const int32_t v)
62 int32_t vo = htonl(v);
64 memcpy(*ptr, &vo, sizeof vo);
68 /* serial_uint32 -- Serialise an unsigned 32 bit integer. */
70 void serial_uint32(uint8_t * * const ptr, const uint32_t v)
72 uint32_t vo = htonl(v);
74 memcpy(*ptr, &vo, sizeof vo);
78 /* serial_int64 -- Serialise a signed 64 bit integer. */
80 void serial_int64(uint8_t * * const ptr, const int64_t v)
83 memcpy(*ptr, &v, sizeof(int64_t));
86 uint8_t rv[sizeof(int64_t)];
87 uint8_t *pv = (uint8_t *) &v;
89 for (i = 0; i < 8; i++) {
92 memcpy(*ptr, &rv, sizeof(int64_t));
94 *ptr += sizeof(int64_t);
98 /* serial_uint64 -- Serialise an unsigned 64 bit integer. */
100 void serial_uint64(uint8_t * * const ptr, const uint64_t v)
102 if (htonl(1) == 1L) {
103 memcpy(*ptr, &v, sizeof(uint64_t));
106 uint8_t rv[sizeof(uint64_t)];
107 uint8_t *pv = (uint8_t *) &v;
109 for (i = 0; i < 8; i++) {
112 memcpy(*ptr, &rv, sizeof(uint64_t));
114 *ptr += sizeof(uint64_t);
118 /* serial_btime -- Serialise an btime_t 64 bit integer. */
120 void serial_btime(uint8_t * * const ptr, const btime_t v)
122 if (htonl(1) == 1L) {
123 memcpy(*ptr, &v, sizeof(btime_t));
126 uint8_t rv[sizeof(btime_t)];
127 uint8_t *pv = (uint8_t *) &v;
129 for (i = 0; i < 8; i++) {
132 memcpy(*ptr, &rv, sizeof(btime_t));
134 *ptr += sizeof(btime_t);
139 /* serial_float64 -- Serialise a 64 bit IEEE floating point number.
140 This code assumes that the host floating point
141 format is IEEE and that floating point quantities
142 are stored in IEEE format either LSB first or MSB
143 first. More creative host formats will require
144 additional transformations here. */
146 void serial_float64(uint8_t * * const ptr, const float64_t v)
148 if (htonl(1) == 1L) {
149 memcpy(*ptr, &v, sizeof(float64_t));
152 uint8_t rv[sizeof(float64_t)];
153 uint8_t *pv = (uint8_t *) &v;
155 for (i = 0; i < 8; i++) {
158 memcpy(*ptr, &rv, sizeof(float64_t));
160 *ptr += sizeof(float64_t);
163 void serial_string(uint8_t * * const ptr, const char * const str)
165 int len = strlen(str) + 1;
167 memcpy(*ptr, str, len);
172 /* unserial_int16 -- Unserialise a signed 16 bit integer. */
174 int16_t unserial_int16(uint8_t * * const ptr)
178 memcpy(&vo, *ptr, sizeof vo);
183 /* unserial_uint16 -- Unserialise an unsigned 16 bit integer. */
185 uint16_t unserial_uint16(uint8_t * * const ptr)
189 memcpy(&vo, *ptr, sizeof vo);
194 /* unserial_int32 -- Unserialise a signed 32 bit integer. */
196 int32_t unserial_int32(uint8_t * * const ptr)
200 memcpy(&vo, *ptr, sizeof vo);
205 /* unserial_uint32 -- Unserialise an unsigned 32 bit integer. */
207 uint32_t unserial_uint32(uint8_t * * const ptr)
211 memcpy(&vo, *ptr, sizeof vo);
216 /* unserial_uint64 -- Unserialise an unsigned 64 bit integer. */
218 uint64_t unserial_uint64(uint8_t * * const ptr)
222 if (htonl(1) == 1L) {
223 memcpy(&v, *ptr, sizeof(uint64_t));
226 uint8_t rv[sizeof(uint64_t)];
227 uint8_t *pv = (uint8_t *) &v;
229 memcpy(&v, *ptr, sizeof(uint64_t));
230 for (i = 0; i < 8; i++) {
233 memcpy(&v, &rv, sizeof(uint64_t));
235 *ptr += sizeof(uint64_t);
239 /* unserial_btime -- Unserialise a btime_t 64 bit integer. */
241 btime_t unserial_btime(uint8_t * * const ptr)
245 if (htonl(1) == 1L) {
246 memcpy(&v, *ptr, sizeof(btime_t));
249 uint8_t rv[sizeof(btime_t)];
250 uint8_t *pv = (uint8_t *) &v;
252 memcpy(&v, *ptr, sizeof(btime_t));
253 for (i = 0; i < 8; i++) {
256 memcpy(&v, &rv, sizeof(btime_t));
258 *ptr += sizeof(btime_t);
264 /* unserial_float64 -- Unserialise a 64 bit IEEE floating point number.
265 This code assumes that the host floating point
266 format is IEEE and that floating point quantities
267 are stored in IEEE format either LSB first or MSB
268 first. More creative host formats will require
269 additional transformations here. */
271 float64_t unserial_float64(uint8_t * * const ptr)
275 if (htonl(1) == 1L) {
276 memcpy(&v, *ptr, sizeof(float64_t));
279 uint8_t rv[sizeof(float64_t)];
280 uint8_t *pv = (uint8_t *) &v;
282 memcpy(&v, *ptr, sizeof(float64_t));
283 for (i = 0; i < 8; i++) {
286 memcpy(&v, &rv, sizeof(float64_t));
288 *ptr += sizeof(float64_t);
292 void unserial_string(uint8_t * * const ptr, char * const str)
294 int len = strlen((char *) *ptr) + 1;
295 memcpy(str, (char *) *ptr, len);