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