]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/serial.c
Fix: clock diff, Dan's patch, Nic's patch, segfault
[bacula/bacula] / bacula / src / lib / serial.c
1 /*
2
3                    Serialisation Support Functions
4                           John Walker
5
6   
7      Version $Id$
8 */
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
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 GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29
30 #include "bacula.h"
31 #include "serial.h"
32
33 /*
34
35         NOTE:  The following functions should work on any
36                vaguely contemporary platform.  Production
37                builds should use optimised macros (void
38                on platforms with network byte order and IEEE
39                floating point format as native.
40
41 */
42
43 /*  serial_int16  --  Serialise a signed 16 bit integer.  */
44
45 void serial_int16(uint8_t * * const ptr, const int16_t v)
46 {
47     int16_t vo = htons(v);
48
49     memcpy(*ptr, &vo, sizeof vo);
50     *ptr += sizeof vo;
51 }
52
53 /*  serial_uint16  --  Serialise an unsigned 16 bit integer.  */
54
55 void serial_uint16(uint8_t * * const ptr, const uint16_t v)
56 {
57     uint16_t vo = htons(v);
58
59     memcpy(*ptr, &vo, sizeof vo);
60     *ptr += sizeof vo;
61 }
62
63 /*  serial_int32  --  Serialise a signed 32 bit integer.  */
64
65 void serial_int32(uint8_t * * const ptr, const int32_t v)
66 {
67     int32_t vo = htonl(v);
68
69     memcpy(*ptr, &vo, sizeof vo);
70     *ptr += sizeof vo;
71 }
72
73 /*  serial_uint32  --  Serialise an unsigned 32 bit integer.  */
74
75 void serial_uint32(uint8_t * * const ptr, const uint32_t v)
76 {
77     uint32_t vo = htonl(v);
78
79     memcpy(*ptr, &vo, sizeof vo);
80     *ptr += sizeof vo;
81 }
82
83 /*  serial_int64  --  Serialise a signed 64 bit integer.  */
84
85 void serial_int64(uint8_t * * const ptr, const int64_t v)
86 {
87     if (htonl(1) == 1L) {
88         memcpy(*ptr, &v, sizeof(int64_t));
89     } else {
90         int i;
91         uint8_t rv[sizeof(int64_t)];
92         uint8_t *pv = (uint8_t *) &v;
93
94         for (i = 0; i < 8; i++) {
95             rv[i] = pv[7 - i];
96         }
97         memcpy(*ptr, &rv, sizeof(int64_t));
98     }
99     *ptr += sizeof(int64_t);
100 }
101
102
103 /*  serial_uint64  --  Serialise an unsigned 64 bit integer.  */
104
105 void serial_uint64(uint8_t * * const ptr, const uint64_t v)
106 {
107     if (htonl(1) == 1L) {
108         memcpy(*ptr, &v, sizeof(uint64_t));
109     } else {
110         int i;
111         uint8_t rv[sizeof(uint64_t)];
112         uint8_t *pv = (uint8_t *) &v;
113
114         for (i = 0; i < 8; i++) {
115             rv[i] = pv[7 - i];
116         }
117         memcpy(*ptr, &rv, sizeof(uint64_t));
118     }
119     *ptr += sizeof(uint64_t);
120 }
121
122
123 /*  serial_btime  --  Serialise an btime_t 64 bit integer.  */
124
125 void serial_btime(uint8_t * * const ptr, const btime_t v)
126 {
127     if (htonl(1) == 1L) {
128         memcpy(*ptr, &v, sizeof(btime_t));
129     } else {
130         int i;
131         uint8_t rv[sizeof(btime_t)];
132         uint8_t *pv = (uint8_t *) &v;
133
134         for (i = 0; i < 8; i++) {
135             rv[i] = pv[7 - i];
136         }
137         memcpy(*ptr, &rv, sizeof(btime_t));
138     }
139     *ptr += sizeof(btime_t);
140 }
141
142
143
144 /*  serial_float64  --  Serialise a 64 bit IEEE floating point number.
145                         This code assumes that the host floating point
146                         format is IEEE and that floating point quantities
147                         are stored in IEEE format either LSB first or MSB
148                         first.  More creative host formats will require
149                         additional transformations here.  */
150
151 void serial_float64(uint8_t * * const ptr, const float64_t v)
152 {
153     if (htonl(1) == 1L) {
154         memcpy(*ptr, &v, sizeof(float64_t));
155     } else {
156         int i;
157         uint8_t rv[sizeof(float64_t)];
158         uint8_t *pv = (uint8_t *) &v;
159
160         for (i = 0; i < 8; i++) {
161             rv[i] = pv[7 - i];
162         }
163         memcpy(*ptr, &rv, sizeof(float64_t));
164     }
165     *ptr += sizeof(float64_t);
166 }
167
168 int serial_string(uint8_t * const ptr, char * const str)
169 {
170    int len = strlen((const char *) str) + 1;
171    memcpy(ptr, str, len);
172    return len;
173 }
174
175
176 /*  unserial_int16  --  Unserialise a signed 16 bit integer.  */
177
178 int16_t unserial_int16(uint8_t * * const ptr)
179 {
180     int16_t vo;
181
182     memcpy(&vo, *ptr, sizeof vo);
183     *ptr += sizeof vo;
184     return ntohs(vo);
185 }
186
187 /*  unserial_uint16  --  Unserialise an unsigned 16 bit integer.  */
188
189 uint16_t unserial_uint16(uint8_t * * const ptr)
190 {
191     uint16_t vo;
192
193     memcpy(&vo, *ptr, sizeof vo);
194     *ptr += sizeof vo;
195     return ntohs(vo);
196 }
197
198 /*  unserial_int32  --  Unserialise a signed 32 bit integer.  */
199
200 int32_t unserial_int32(uint8_t * * const ptr)
201 {
202     int32_t vo;
203
204     memcpy(&vo, *ptr, sizeof vo);
205     *ptr += sizeof vo;
206     return ntohl(vo);
207 }
208
209 /*  unserial_uint32  --  Unserialise an unsigned 32 bit integer.  */
210
211 uint32_t unserial_uint32(uint8_t * * const ptr)
212 {
213     uint32_t vo;
214
215     memcpy(&vo, *ptr, sizeof vo);
216     *ptr += sizeof vo;
217     return ntohl(vo);
218 }
219
220 /*  unserial_uint64  --  Unserialise an unsigned 64 bit integer.  */
221
222 uint64_t unserial_uint64(uint8_t * * const ptr)
223 {
224     uint64_t v;
225
226     if (htonl(1) == 1L) {
227         memcpy(&v, *ptr, sizeof(uint64_t));
228     } else {
229         int i;
230         uint8_t rv[sizeof(uint64_t)];
231         uint8_t *pv = (uint8_t *) &v;
232
233         memcpy(&v, *ptr, sizeof(uint64_t));
234         for (i = 0; i < 8; i++) {
235             rv[i] = pv[7 - i];
236         }
237         memcpy(&v, &rv, sizeof(uint64_t));
238     }
239     *ptr += sizeof(uint64_t);
240     return v;
241 }
242
243 /*  unserial_btime  --  Unserialise a btime_t 64 bit integer.  */
244
245 btime_t unserial_btime(uint8_t * * const ptr)
246 {
247     btime_t v;
248
249     if (htonl(1) == 1L) {
250         memcpy(&v, *ptr, sizeof(btime_t));
251     } else {
252         int i;
253         uint8_t rv[sizeof(btime_t)];
254         uint8_t *pv = (uint8_t *) &v;
255
256         memcpy(&v, *ptr, sizeof(btime_t));
257         for (i = 0; i < 8; i++) {
258             rv[i] = pv[7 - i];
259         }
260         memcpy(&v, &rv, sizeof(btime_t));
261     }
262     *ptr += sizeof(btime_t);
263     return v;
264 }
265
266
267
268 /*  unserial_float64  --  Unserialise a 64 bit IEEE floating point number.
269                          This code assumes that the host floating point
270                          format is IEEE and that floating point quantities
271                          are stored in IEEE format either LSB first or MSB
272                          first.  More creative host formats will require
273                          additional transformations here.  */
274
275 float64_t unserial_float64(uint8_t * * const ptr)
276 {
277     float64_t v;
278
279     if (htonl(1) == 1L) {
280         memcpy(&v, *ptr, sizeof(float64_t));
281     } else {
282         int i;
283         uint8_t rv[sizeof(float64_t)];
284         uint8_t *pv = (uint8_t *) &v;
285
286         memcpy(&v, *ptr, sizeof(float64_t));
287         for (i = 0; i < 8; i++) {
288             rv[i] = pv[7 - i];
289         }
290         memcpy(&v, &rv, sizeof(float64_t));
291     }
292     *ptr += sizeof(float64_t);
293     return v;
294 }
295
296 int unserial_string(uint8_t  * const ptr, char * const str)
297 {
298    int len = strlen((char *)ptr) + 1;
299    memcpy(str, ptr, len);
300    return len;
301 }