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