]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/serial.c
Convert to pure GPL v2 license.
[bacula/bacula] / bacula / src / lib / serial.c
1 /*
2
3                    Serialisation Support Functions
4                           John Walker
5
6
7      Version $Id$
8 */
9 /*
10    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
13
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 and included
19    in the file LICENSE.
20
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.
25
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
29    02110-1301, USA.
30
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.
35 */
36
37
38 #include "bacula.h"
39 #include "serial.h"
40
41 /*
42
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.
48
49 */
50
51 /*  serial_int16  --  Serialise a signed 16 bit integer.  */
52
53 void serial_int16(uint8_t * * const ptr, const int16_t v)
54 {
55     int16_t vo = htons(v);
56
57     memcpy(*ptr, &vo, sizeof vo);
58     *ptr += sizeof vo;
59 }
60
61 /*  serial_uint16  --  Serialise an unsigned 16 bit integer.  */
62
63 void serial_uint16(uint8_t * * const ptr, const uint16_t v)
64 {
65     uint16_t vo = htons(v);
66
67     memcpy(*ptr, &vo, sizeof vo);
68     *ptr += sizeof vo;
69 }
70
71 /*  serial_int32  --  Serialise a signed 32 bit integer.  */
72
73 void serial_int32(uint8_t * * const ptr, const int32_t v)
74 {
75     int32_t vo = htonl(v);
76
77     memcpy(*ptr, &vo, sizeof vo);
78     *ptr += sizeof vo;
79 }
80
81 /*  serial_uint32  --  Serialise an unsigned 32 bit integer.  */
82
83 void serial_uint32(uint8_t * * const ptr, const uint32_t v)
84 {
85     uint32_t vo = htonl(v);
86
87     memcpy(*ptr, &vo, sizeof vo);
88     *ptr += sizeof vo;
89 }
90
91 /*  serial_int64  --  Serialise a signed 64 bit integer.  */
92
93 void serial_int64(uint8_t * * const ptr, const int64_t v)
94 {
95     if (htonl(1) == 1L) {
96         memcpy(*ptr, &v, sizeof(int64_t));
97     } else {
98         int i;
99         uint8_t rv[sizeof(int64_t)];
100         uint8_t *pv = (uint8_t *) &v;
101
102         for (i = 0; i < 8; i++) {
103             rv[i] = pv[7 - i];
104         }
105         memcpy(*ptr, &rv, sizeof(int64_t));
106     }
107     *ptr += sizeof(int64_t);
108 }
109
110
111 /*  serial_uint64  --  Serialise an unsigned 64 bit integer.  */
112
113 void serial_uint64(uint8_t * * const ptr, const uint64_t v)
114 {
115     if (htonl(1) == 1L) {
116         memcpy(*ptr, &v, sizeof(uint64_t));
117     } else {
118         int i;
119         uint8_t rv[sizeof(uint64_t)];
120         uint8_t *pv = (uint8_t *) &v;
121
122         for (i = 0; i < 8; i++) {
123             rv[i] = pv[7 - i];
124         }
125         memcpy(*ptr, &rv, sizeof(uint64_t));
126     }
127     *ptr += sizeof(uint64_t);
128 }
129
130
131 /*  serial_btime  --  Serialise an btime_t 64 bit integer.  */
132
133 void serial_btime(uint8_t * * const ptr, const btime_t v)
134 {
135     if (htonl(1) == 1L) {
136         memcpy(*ptr, &v, sizeof(btime_t));
137     } else {
138         int i;
139         uint8_t rv[sizeof(btime_t)];
140         uint8_t *pv = (uint8_t *) &v;
141
142         for (i = 0; i < 8; i++) {
143             rv[i] = pv[7 - i];
144         }
145         memcpy(*ptr, &rv, sizeof(btime_t));
146     }
147     *ptr += sizeof(btime_t);
148 }
149
150
151
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.  */
158
159 void serial_float64(uint8_t * * const ptr, const float64_t v)
160 {
161     if (htonl(1) == 1L) {
162         memcpy(*ptr, &v, sizeof(float64_t));
163     } else {
164         int i;
165         uint8_t rv[sizeof(float64_t)];
166         uint8_t *pv = (uint8_t *) &v;
167
168         for (i = 0; i < 8; i++) {
169             rv[i] = pv[7 - i];
170         }
171         memcpy(*ptr, &rv, sizeof(float64_t));
172     }
173     *ptr += sizeof(float64_t);
174 }
175
176 void serial_string(uint8_t * * const ptr, const char * const str)
177 {
178    int len = strlen(str) + 1;
179
180    memcpy(*ptr, str, len);
181    *ptr += len;
182 }
183
184
185 /*  unserial_int16  --  Unserialise a signed 16 bit integer.  */
186
187 int16_t unserial_int16(uint8_t * * const ptr)
188 {
189     int16_t vo;
190
191     memcpy(&vo, *ptr, sizeof vo);
192     *ptr += sizeof vo;
193     return ntohs(vo);
194 }
195
196 /*  unserial_uint16  --  Unserialise an unsigned 16 bit integer.  */
197
198 uint16_t unserial_uint16(uint8_t * * const ptr)
199 {
200     uint16_t vo;
201
202     memcpy(&vo, *ptr, sizeof vo);
203     *ptr += sizeof vo;
204     return ntohs(vo);
205 }
206
207 /*  unserial_int32  --  Unserialise a signed 32 bit integer.  */
208
209 int32_t unserial_int32(uint8_t * * const ptr)
210 {
211     int32_t vo;
212
213     memcpy(&vo, *ptr, sizeof vo);
214     *ptr += sizeof vo;
215     return ntohl(vo);
216 }
217
218 /*  unserial_uint32  --  Unserialise an unsigned 32 bit integer.  */
219
220 uint32_t unserial_uint32(uint8_t * * const ptr)
221 {
222     uint32_t vo;
223
224     memcpy(&vo, *ptr, sizeof vo);
225     *ptr += sizeof vo;
226     return ntohl(vo);
227 }
228
229 /*  unserial_uint64  --  Unserialise an unsigned 64 bit integer.  */
230
231 uint64_t unserial_uint64(uint8_t * * const ptr)
232 {
233     uint64_t v;
234
235     if (htonl(1) == 1L) {
236         memcpy(&v, *ptr, sizeof(uint64_t));
237     } else {
238         int i;
239         uint8_t rv[sizeof(uint64_t)];
240         uint8_t *pv = (uint8_t *) &v;
241
242         memcpy(&v, *ptr, sizeof(uint64_t));
243         for (i = 0; i < 8; i++) {
244             rv[i] = pv[7 - i];
245         }
246         memcpy(&v, &rv, sizeof(uint64_t));
247     }
248     *ptr += sizeof(uint64_t);
249     return v;
250 }
251
252 /*  unserial_btime  --  Unserialise a btime_t 64 bit integer.  */
253
254 btime_t unserial_btime(uint8_t * * const ptr)
255 {
256     btime_t v;
257
258     if (htonl(1) == 1L) {
259         memcpy(&v, *ptr, sizeof(btime_t));
260     } else {
261         int i;
262         uint8_t rv[sizeof(btime_t)];
263         uint8_t *pv = (uint8_t *) &v;
264
265         memcpy(&v, *ptr, sizeof(btime_t));
266         for (i = 0; i < 8; i++) {
267             rv[i] = pv[7 - i];
268         }
269         memcpy(&v, &rv, sizeof(btime_t));
270     }
271     *ptr += sizeof(btime_t);
272     return v;
273 }
274
275
276
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.  */
283
284 float64_t unserial_float64(uint8_t * * const ptr)
285 {
286     float64_t v;
287
288     if (htonl(1) == 1L) {
289         memcpy(&v, *ptr, sizeof(float64_t));
290     } else {
291         int i;
292         uint8_t rv[sizeof(float64_t)];
293         uint8_t *pv = (uint8_t *) &v;
294
295         memcpy(&v, *ptr, sizeof(float64_t));
296         for (i = 0; i < 8; i++) {
297             rv[i] = pv[7 - i];
298         }
299         memcpy(&v, &rv, sizeof(float64_t));
300     }
301     *ptr += sizeof(float64_t);
302     return v;
303 }
304
305 void unserial_string(uint8_t * * const ptr, char * const str)
306 {
307    int len = strlen((char *) *ptr) + 1;
308    memcpy(str, (char *) *ptr, len);
309    *ptr += len;
310 }