3 Serialisation Support Functions
7 Bacula® - The Network Backup Solution
9 Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
11 The main author of Bacula is Kern Sibbald, with contributions from
12 many others, a complete list can be found in the file AUTHORS.
13 This program is Free Software; you can redistribute it and/or
14 modify it under the terms of version three of the GNU Affero General Public
15 License as published by the Free Software Foundation and included
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU Affero General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 Bacula® is a registered trademark of Kern Sibbald.
29 The licensor of Bacula is the Free Software Foundation Europe
30 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
31 Switzerland, email:ftf@fsfeurope.org.
40 NOTE: The following functions should work on any
41 vaguely contemporary platform. Production
42 builds should use optimised macros (void
43 on platforms with network byte order and IEEE
44 floating point format as native.
48 /* serial_int16 -- Serialise a signed 16 bit integer. */
50 void serial_int16(uint8_t * * const ptr, const int16_t v)
52 int16_t vo = htons(v);
54 memcpy(*ptr, &vo, sizeof vo);
58 /* serial_uint16 -- Serialise an unsigned 16 bit integer. */
60 void serial_uint16(uint8_t * * const ptr, const uint16_t v)
62 uint16_t vo = htons(v);
64 memcpy(*ptr, &vo, sizeof vo);
68 /* serial_int32 -- Serialise a signed 32 bit integer. */
70 void serial_int32(uint8_t * * const ptr, const int32_t v)
72 int32_t vo = htonl(v);
74 memcpy(*ptr, &vo, sizeof vo);
78 /* serial_uint32 -- Serialise an unsigned 32 bit integer. */
80 void serial_uint32(uint8_t * * const ptr, const uint32_t v)
82 uint32_t vo = htonl(v);
84 memcpy(*ptr, &vo, sizeof vo);
88 /* serial_int64 -- Serialise a signed 64 bit integer. */
90 void serial_int64(uint8_t * * const ptr, const int64_t v)
93 memcpy(*ptr, &v, sizeof(int64_t));
96 uint8_t rv[sizeof(int64_t)];
97 uint8_t *pv = (uint8_t *) &v;
99 for (i = 0; i < 8; i++) {
102 memcpy(*ptr, &rv, sizeof(int64_t));
104 *ptr += sizeof(int64_t);
108 /* serial_uint64 -- Serialise an unsigned 64 bit integer. */
110 void serial_uint64(uint8_t * * const ptr, const uint64_t v)
113 memcpy(*ptr, &v, sizeof(uint64_t));
116 uint8_t rv[sizeof(uint64_t)];
117 uint8_t *pv = (uint8_t *) &v;
119 for (i = 0; i < 8; i++) {
122 memcpy(*ptr, &rv, sizeof(uint64_t));
124 *ptr += sizeof(uint64_t);
128 /* serial_btime -- Serialise an btime_t 64 bit integer. */
130 void serial_btime(uint8_t * * const ptr, const btime_t v)
133 memcpy(*ptr, &v, sizeof(btime_t));
136 uint8_t rv[sizeof(btime_t)];
137 uint8_t *pv = (uint8_t *) &v;
139 for (i = 0; i < 8; i++) {
142 memcpy(*ptr, &rv, sizeof(btime_t));
144 *ptr += sizeof(btime_t);
149 /* serial_float64 -- Serialise a 64 bit IEEE floating point number.
150 This code assumes that the host floating point
151 format is IEEE and that floating point quantities
152 are stored in IEEE format either LSB first or MSB
153 first. More creative host formats will require
154 additional transformations here. */
156 void serial_float64(uint8_t * * const ptr, const float64_t v)
159 memcpy(*ptr, &v, sizeof(float64_t));
162 uint8_t rv[sizeof(float64_t)];
163 uint8_t *pv = (uint8_t *) &v;
165 for (i = 0; i < 8; i++) {
168 memcpy(*ptr, &rv, sizeof(float64_t));
170 *ptr += sizeof(float64_t);
173 void serial_string(uint8_t * * const ptr, const char * const str)
176 char *dest = (char *)*ptr;
177 char *src = (char *)str;
178 for (i=0; src[i] != 0; i++) {
181 dest[i++] = 0; /* terminate output string */
182 *ptr += i; /* update pointer */
183 // Dmsg2(000, "ser src=%s dest=%s\n", src, dest);
187 /* unserial_int16 -- Unserialise a signed 16 bit integer. */
189 int16_t unserial_int16(uint8_t * * const ptr)
193 memcpy(&vo, *ptr, sizeof vo);
198 /* unserial_uint16 -- Unserialise an unsigned 16 bit integer. */
200 uint16_t unserial_uint16(uint8_t * * const ptr)
204 memcpy(&vo, *ptr, sizeof vo);
209 /* unserial_int32 -- Unserialise a signed 32 bit integer. */
211 int32_t unserial_int32(uint8_t * * const ptr)
215 memcpy(&vo, *ptr, sizeof vo);
220 /* unserial_uint32 -- Unserialise an unsigned 32 bit integer. */
222 uint32_t unserial_uint32(uint8_t * * const ptr)
226 memcpy(&vo, *ptr, sizeof vo);
231 /* unserial_uint64 -- Unserialise an unsigned 64 bit integer. */
233 uint64_t unserial_uint64(uint8_t * * const ptr)
238 memcpy(&v, *ptr, sizeof(uint64_t));
241 uint8_t rv[sizeof(uint64_t)];
242 uint8_t *pv = (uint8_t *) &v;
244 memcpy(&v, *ptr, sizeof(uint64_t));
245 for (i = 0; i < 8; i++) {
248 memcpy(&v, &rv, sizeof(uint64_t));
250 *ptr += sizeof(uint64_t);
254 /* unserial_btime -- Unserialise a btime_t 64 bit integer. */
256 btime_t unserial_btime(uint8_t * * const ptr)
261 memcpy(&v, *ptr, sizeof(btime_t));
264 uint8_t rv[sizeof(btime_t)];
265 uint8_t *pv = (uint8_t *) &v;
267 memcpy(&v, *ptr, sizeof(btime_t));
268 for (i = 0; i < 8; i++) {
271 memcpy(&v, &rv, sizeof(btime_t));
273 *ptr += sizeof(btime_t);
279 /* unserial_float64 -- Unserialise a 64 bit IEEE floating point number.
280 This code assumes that the host floating point
281 format is IEEE and that floating point quantities
282 are stored in IEEE format either LSB first or MSB
283 first. More creative host formats will require
284 additional transformations here. */
286 float64_t unserial_float64(uint8_t * * const ptr)
291 memcpy(&v, *ptr, sizeof(float64_t));
294 uint8_t rv[sizeof(float64_t)];
295 uint8_t *pv = (uint8_t *) &v;
297 memcpy(&v, *ptr, sizeof(float64_t));
298 for (i = 0; i < 8; i++) {
301 memcpy(&v, &rv, sizeof(float64_t));
303 *ptr += sizeof(float64_t);
307 void unserial_string(uint8_t * * const ptr, char * const str, int max)
310 char *src = (char*)(*ptr);
312 for (i=0; i<max && src[i] != 0; i++) {
315 dest[i++] = 0; /* terminate output string */
316 *ptr += i; /* update pointer */
317 // Dmsg2(000, "unser src=%s dest=%s\n", src, dest);