3 Serialisation Support Functions
10 Bacula® - The Network Backup Solution
12 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
14 The main author of Bacula is Kern Sibbald, with contributions from
15 many others, a complete list can be found in the file AUTHORS.
16 This program is Free Software; you can redistribute it and/or
17 modify it under the terms of version two of the GNU General Public
18 License as published by the Free Software Foundation plus additions
19 that are listed in the file LICENSE.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 Bacula® is a registered trademark of John Walker.
32 The licensor of Bacula is the Free Software Foundation Europe
33 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34 Switzerland, email:ftf@fsfeurope.org.
43 NOTE: The following functions should work on any
44 vaguely contemporary platform. Production
45 builds should use optimised macros (void
46 on platforms with network byte order and IEEE
47 floating point format as native.
51 /* serial_int16 -- Serialise a signed 16 bit integer. */
53 void serial_int16(uint8_t * * const ptr, const int16_t v)
55 int16_t vo = htons(v);
57 memcpy(*ptr, &vo, sizeof vo);
61 /* serial_uint16 -- Serialise an unsigned 16 bit integer. */
63 void serial_uint16(uint8_t * * const ptr, const uint16_t v)
65 uint16_t vo = htons(v);
67 memcpy(*ptr, &vo, sizeof vo);
71 /* serial_int32 -- Serialise a signed 32 bit integer. */
73 void serial_int32(uint8_t * * const ptr, const int32_t v)
75 int32_t vo = htonl(v);
77 memcpy(*ptr, &vo, sizeof vo);
81 /* serial_uint32 -- Serialise an unsigned 32 bit integer. */
83 void serial_uint32(uint8_t * * const ptr, const uint32_t v)
85 uint32_t vo = htonl(v);
87 memcpy(*ptr, &vo, sizeof vo);
91 /* serial_int64 -- Serialise a signed 64 bit integer. */
93 void serial_int64(uint8_t * * const ptr, const int64_t v)
96 memcpy(*ptr, &v, sizeof(int64_t));
99 uint8_t rv[sizeof(int64_t)];
100 uint8_t *pv = (uint8_t *) &v;
102 for (i = 0; i < 8; i++) {
105 memcpy(*ptr, &rv, sizeof(int64_t));
107 *ptr += sizeof(int64_t);
111 /* serial_uint64 -- Serialise an unsigned 64 bit integer. */
113 void serial_uint64(uint8_t * * const ptr, const uint64_t v)
115 if (htonl(1) == 1L) {
116 memcpy(*ptr, &v, sizeof(uint64_t));
119 uint8_t rv[sizeof(uint64_t)];
120 uint8_t *pv = (uint8_t *) &v;
122 for (i = 0; i < 8; i++) {
125 memcpy(*ptr, &rv, sizeof(uint64_t));
127 *ptr += sizeof(uint64_t);
131 /* serial_btime -- Serialise an btime_t 64 bit integer. */
133 void serial_btime(uint8_t * * const ptr, const btime_t v)
135 if (htonl(1) == 1L) {
136 memcpy(*ptr, &v, sizeof(btime_t));
139 uint8_t rv[sizeof(btime_t)];
140 uint8_t *pv = (uint8_t *) &v;
142 for (i = 0; i < 8; i++) {
145 memcpy(*ptr, &rv, sizeof(btime_t));
147 *ptr += sizeof(btime_t);
152 /* serial_float64 -- Serialise a 64 bit IEEE floating point number.
153 This code assumes that the host floating point
154 format is IEEE and that floating point quantities
155 are stored in IEEE format either LSB first or MSB
156 first. More creative host formats will require
157 additional transformations here. */
159 void serial_float64(uint8_t * * const ptr, const float64_t v)
161 if (htonl(1) == 1L) {
162 memcpy(*ptr, &v, sizeof(float64_t));
165 uint8_t rv[sizeof(float64_t)];
166 uint8_t *pv = (uint8_t *) &v;
168 for (i = 0; i < 8; i++) {
171 memcpy(*ptr, &rv, sizeof(float64_t));
173 *ptr += sizeof(float64_t);
176 void serial_string(uint8_t * * const ptr, const char * const str)
178 int len = strlen(str) + 1;
180 memcpy(*ptr, str, len);
185 /* unserial_int16 -- Unserialise a signed 16 bit integer. */
187 int16_t unserial_int16(uint8_t * * const ptr)
191 memcpy(&vo, *ptr, sizeof vo);
196 /* unserial_uint16 -- Unserialise an unsigned 16 bit integer. */
198 uint16_t unserial_uint16(uint8_t * * const ptr)
202 memcpy(&vo, *ptr, sizeof vo);
207 /* unserial_int32 -- Unserialise a signed 32 bit integer. */
209 int32_t unserial_int32(uint8_t * * const ptr)
213 memcpy(&vo, *ptr, sizeof vo);
218 /* unserial_uint32 -- Unserialise an unsigned 32 bit integer. */
220 uint32_t unserial_uint32(uint8_t * * const ptr)
224 memcpy(&vo, *ptr, sizeof vo);
229 /* unserial_uint64 -- Unserialise an unsigned 64 bit integer. */
231 uint64_t unserial_uint64(uint8_t * * const ptr)
235 if (htonl(1) == 1L) {
236 memcpy(&v, *ptr, sizeof(uint64_t));
239 uint8_t rv[sizeof(uint64_t)];
240 uint8_t *pv = (uint8_t *) &v;
242 memcpy(&v, *ptr, sizeof(uint64_t));
243 for (i = 0; i < 8; i++) {
246 memcpy(&v, &rv, sizeof(uint64_t));
248 *ptr += sizeof(uint64_t);
252 /* unserial_btime -- Unserialise a btime_t 64 bit integer. */
254 btime_t unserial_btime(uint8_t * * const ptr)
258 if (htonl(1) == 1L) {
259 memcpy(&v, *ptr, sizeof(btime_t));
262 uint8_t rv[sizeof(btime_t)];
263 uint8_t *pv = (uint8_t *) &v;
265 memcpy(&v, *ptr, sizeof(btime_t));
266 for (i = 0; i < 8; i++) {
269 memcpy(&v, &rv, sizeof(btime_t));
271 *ptr += sizeof(btime_t);
277 /* unserial_float64 -- Unserialise a 64 bit IEEE floating point number.
278 This code assumes that the host floating point
279 format is IEEE and that floating point quantities
280 are stored in IEEE format either LSB first or MSB
281 first. More creative host formats will require
282 additional transformations here. */
284 float64_t unserial_float64(uint8_t * * const ptr)
288 if (htonl(1) == 1L) {
289 memcpy(&v, *ptr, sizeof(float64_t));
292 uint8_t rv[sizeof(float64_t)];
293 uint8_t *pv = (uint8_t *) &v;
295 memcpy(&v, *ptr, sizeof(float64_t));
296 for (i = 0; i < 8; i++) {
299 memcpy(&v, &rv, sizeof(float64_t));
301 *ptr += sizeof(float64_t);
305 void unserial_string(uint8_t * * const ptr, char * const str)
307 int len = strlen((char *) *ptr) + 1;
308 memcpy(str, (char *) *ptr, len);