1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2018 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Written by John Walker, MM
24  */
25 /**
26  * @file
27  *  Serialisation support functions from serial.c
28  */
29 
30 
31 extern void serial_int16(uint8_t** const ptr, const int16_t v);
32 extern void serial_uint16(uint8_t** const ptr, const uint16_t v);
33 extern void serial_int32(uint8_t** const ptr, const int32_t v);
34 extern void serial_uint32(uint8_t** const ptr, const uint32_t v);
35 extern void serial_int64(uint8_t** ptr, int64_t v);
36 extern void serial_uint64(uint8_t** const ptr, const uint64_t v);
37 extern void SerialBtime(uint8_t** const ptr, const btime_t v);
38 extern void serial_float64(uint8_t** const ptr, const float64_t v);
39 extern void SerialString(uint8_t** const ptr, const char* const str);
40 
41 extern int16_t unserial_int16(uint8_t** const ptr);
42 extern uint16_t unserial_uint16(uint8_t** const ptr);
43 extern int32_t unserial_int32(uint8_t** const ptr);
44 extern uint32_t unserial_uint32(uint8_t** const ptr);
45 extern int64_t unserial_int64(uint8_t** const ptr);
46 extern uint64_t unserial_uint64(uint8_t** const ptr);
47 extern btime_t UnserialBtime(uint8_t** const ptr);
48 extern float64_t unserial_float64(uint8_t** const ptr);
49 extern void UnserialString(uint8_t** const ptr, char* const str, int max);
50 
51 /**
52 
53                          Serialisation Macros
54 
55     These macros use a uint8_t pointer, ser_ptr, which must be
56     defined by the code which uses them.
57 
58 */
59 
60 #ifndef BAREOS_LIB_SERIAL_H_
61 #  define BAREOS_LIB_SERIAL_H_ 1
62 
63 /*  ser_declare  --  Declare ser_ptr locally within a function.  */
64 #  define ser_declare uint8_t* ser_ptr
65 #  define unser_declare uint8_t* ser_ptr
66 
67 /*  SerBegin(x, s)  --  Begin serialisation into a buffer x of size s.  */
68 #  define SerBegin(x, s) ser_ptr = ((uint8_t*)(x))
69 #  define UnserBegin(x, s) ser_ptr = ((uint8_t*)(x))
70 
71 /*  SerLength  --  Determine length in bytes of serialised into a
72                     buffer x.  */
73 #  define SerLength(x) ((uint32_t)(ser_ptr - (uint8_t*)(x)))
74 #  define UnserLength(x) ((uint32_t)(ser_ptr - (uint8_t*)(x)))
75 
76 /*  SerEnd(x, s)  --  End serialisation into a buffer x of size s.  */
77 #  define SerEnd(x, s) ASSERT(SerLength(x) <= (s))
78 #  define UnserEnd(x, s) ASSERT(SerLength(x) <= (s))
79 
80 /*  ser_check(x, s)  --  Verify length of serialised data in buffer x is
81                          expected length s.  */
82 #  define ser_check(x, s) ASSERT(SerLength(x) == (s))
83 
84 /*                          Serialisation                   */
85 
86 /*  8 bit signed integer  */
87 #  define ser_int8(x) *ser_ptr++ = (x)
88 /*  8 bit unsigned integer  */
89 #  define ser_uint8(x) *ser_ptr++ = (x)
90 
91 /*  16 bit signed integer  */
92 #  define ser_int16(x) serial_int16(&ser_ptr, x)
93 /*  16 bit unsigned integer  */
94 #  define ser_uint16(x) serial_uint16(&ser_ptr, x)
95 
96 /*  32 bit signed integer  */
97 #  define ser_int32(x) serial_int32(&ser_ptr, x)
98 /*  32 bit unsigned integer  */
99 #  define ser_uint32(x) serial_uint32(&ser_ptr, x)
100 
101 /*  64 bit signed integer  */
102 #  define ser_int64(x) serial_int64(&ser_ptr, x)
103 /*  64 bit unsigned integer  */
104 #  define ser_uint64(x) serial_uint64(&ser_ptr, x)
105 
106 /* btime -- 64 bit unsigned integer */
107 #  define SerBtime(x) SerialBtime(&ser_ptr, x)
108 
109 
110 /*  64 bit IEEE floating point number  */
111 #  define ser_float64(x) serial_float64(&ser_ptr, x)
112 
113 /*  128 bit signed integer  */
114 #  define ser_int128(x) \
115     memcpy(ser_ptr, x, sizeof(int128_t)), ser_ptr += sizeof(int128_t)
116 
117 /*  Binary byte stream len bytes not requiring serialisation  */
118 #  define SerBytes(x, len) memcpy(ser_ptr, (x), (len)), ser_ptr += (len)
119 
120 /*  Binary byte stream not requiring serialisation (length obtained by sizeof)
121  */
122 #  define ser_buffer(x) SerBytes((x), (sizeof(x)))
123 
124 /* Binary string not requiring serialization */
125 #  define SerString(x) SerialString(&ser_ptr, (x))
126 
127 /*                         Unserialisation                  */
128 
129 /*  8 bit signed integer  */
130 #  define unser_int8(x) (x) = *ser_ptr++
131 /*  8 bit unsigned integer  */
132 #  define unser_uint8(x) (x) = *ser_ptr++
133 
134 /*  16 bit signed integer  */
135 #  define unser_int16(x) (x) = unserial_int16(&ser_ptr)
136 /*  16 bit unsigned integer  */
137 #  define unser_uint16(x) (x) = unserial_uint16(&ser_ptr)
138 
139 /*  32 bit signed integer  */
140 #  define unser_int32(x) (x) = unserial_int32(&ser_ptr)
141 /*  32 bit unsigned integer  */
142 #  define unser_uint32(x) (x) = unserial_uint32(&ser_ptr)
143 
144 /*  64 bit signed integer  */
145 #  define unser_int64(x) (x) = unserial_int64(&ser_ptr)
146 /*  64 bit unsigned integer  */
147 #  define unser_uint64(x) (x) = unserial_uint64(&ser_ptr)
148 
149 /* btime -- 64 bit unsigned integer */
150 #  define UnserBtime(x) (x) = UnserialBtime(&ser_ptr)
151 
152 /*  64 bit IEEE floating point number  */
153 #  define unser_float64(x) (x) = unserial_float64(&ser_ptr)
154 
155 /*  128 bit signed integer  */
156 #  define unser_int128(x) \
157     memcpy(ser_ptr, x, sizeof(int128_t)), ser_ptr += sizeof(int128_t)
158 
159 /*  Binary byte stream len bytes not requiring serialisation  */
160 #  define UnserBytes(x, len) memcpy((x), ser_ptr, (len)), ser_ptr += (len)
161 
162 /*  Binary byte stream not requiring serialisation (length obtained by sizeof)
163  */
164 #  define unser_buffer(x) UnserBytes((x), (sizeof(x)))
165 
166 /* Binary string not requiring serialization (length obtained from max) */
167 #  define unser_nstring(x, max) UnserialString(&ser_ptr, (x), (int)(max))
168 
169 /*  Binary string not requiring serialisation (length obtained by sizeof)  */
170 #  define UnserString(x) UnserialString(&ser_ptr, (x), sizeof(x))
171 
172 #endif /* BAREOS_LIB_SERIAL_H_ */
173