-
Notifications
You must be signed in to change notification settings - Fork 11
/
serialize.c
349 lines (282 loc) · 8.21 KB
/
serialize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdio.h>
#include "serialize.h"
#include <memory.h>
#include <stdlib.h>
#include <assert.h>
void
init_serialized_buffer(ser_buff_t **b){
(*b) = (ser_buff_t *)calloc(1, sizeof(ser_buff_t));
(*b)->b = calloc(1, SERIALIZE_BUFFER_DEFAULT_SIZE);
(*b)->size = SERIALIZE_BUFFER_DEFAULT_SIZE;
(*b)->next = 0;
(*b)->checkpoint = 0;
return;
}
void
init_serialized_buffer_of_defined_size(ser_buff_t **b, int size){
(*b) = (ser_buff_t *)calloc(1, sizeof(ser_buff_t));
(*b)->b = calloc(1, size);
(*b)->size = size;
(*b)->next = 0;
(*b)->checkpoint = 0;
}
char
is_serialized_buffer_empty(ser_buff_t *b){
if(b->next == 0)
return 1;
return 0;
}
void free_serialize_buffer(ser_buff_t *b){
free(b->b);
free(b);
}
int get_serialize_buffer_current_ptr_offset(ser_buff_t *b){
if(!b) return -1;
return b->next;
}
char* get_serialize_buffer_current_ptr(ser_buff_t *b){
if(!b) return NULL;
return (char *)(b->b) + b->next;
}
int get_serialize_buffer_size(ser_buff_t *b){
return b->next;
}
void
serialize_buffer_skip(ser_buff_t *b, int size){
int available_size = b->size - b->next;
if(available_size >= size){
b->next += size;
return;
}
while(available_size < size){
b->size = b->size * 2;
available_size = b->size - b->next;
}
b->b = realloc(b->b, b->size);
b->next += size;
}
void
restore_checkpoint_serialize_buffer(ser_buff_t *b){
if(!b) assert(0);
b->next = b->checkpoint;
}
void
mark_checkpoint_serialize_buffer(ser_buff_t *b){
if(!b) assert(0);
b->checkpoint = b->next;
}
int
get_serialize_buffer_checkpoint_offset(ser_buff_t *b){
return b->checkpoint;
}
void serialize_int(ser_buff_t *b, int data){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < sizeof(int)){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, &data, sizeof(int));
buff->next += sizeof(int);
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, &data, sizeof(int));
buff->next += sizeof(int);
return;
}
void serialize_int8(ser_buff_t *b, char data){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < sizeof(char)){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, &data, sizeof(char));
buff->next += sizeof(char);
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, &data, sizeof(char));
buff->next += sizeof(char);
return;
}
void serialize_uint8(ser_buff_t *b, char data){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < sizeof(char)){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, &data, sizeof(char));
buff->next += sizeof(char);
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, &data, sizeof(char));
buff->next += sizeof(char);
return;
}
void serialize_int32(ser_buff_t *b, int data){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < sizeof(int)){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, &data, sizeof(int));
buff->next += sizeof(int);
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, &data, sizeof(int));
buff->next += sizeof(int);
return;
}
void serialize_uint32(ser_buff_t *b, unsigned int data){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < sizeof(unsigned int)){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, &data, sizeof(unsigned int));
buff->next += sizeof(unsigned int);
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, &data, sizeof(unsigned int));
buff->next += sizeof(unsigned int);
return;
}
void serialize_float(ser_buff_t *b, float data){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < sizeof(float)){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, &data, sizeof(float));
buff->next += sizeof(float);
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, &data, sizeof(float));
buff->next += sizeof(float);
return;
}
void serialize_double(ser_buff_t *b, double data){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < sizeof(double)){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, &data, sizeof(double));
buff->next += sizeof(double);
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, &data, sizeof(double));
buff->next += sizeof(double);
return;
}
void serialize_string(ser_buff_t *b, char *data, int nbytes){
if (b == NULL) assert(0);
ser_buff_t *buff = (ser_buff_t *)(b);
int available_size = buff->size - buff->next;
char isResize = 0;
while(available_size < nbytes){
buff->size = buff->size * 2;
available_size = buff->size - buff->next;
isResize = 1;
}
if(isResize == 0){
memcpy((char *)buff->b + buff->next, data, nbytes);
buff->next += nbytes;
return;
}
// resize of the buffer
buff->b = realloc(buff->b, buff->size);
memcpy((char *)buff->b + buff->next, data, nbytes);
buff->next += nbytes;
return;
}
void truncate_serialize_buffer(ser_buff_t **b){
ser_buff_t *clone = NULL;
if((*b)->next == (*b)->size){
return;
}
init_serialized_buffer_of_defined_size(&clone, (*b)->next);
memcpy(clone->b, (*b)->b, (*b)->next);
clone->next = clone->size;
free_serialize_buffer(*b);
*b = clone;
}
void print_buffer_details(ser_buff_t *b, const char *fn, int lineno){
#if 1
printf("%s():%d : starting address = %p\n", fn, lineno, b);
printf("size = %d\n", b->size);
printf("next = %d\n", b->next);
#endif
return;
}
/* de serialize fn */
void
de_serialize_string(char *dest, ser_buff_t *b, int size){
if(!b || !b->b) assert(0);
if(!size) assert(0);;
if((b->size - b->next)< size) assert(0);
memcpy(dest, b->b + b->next, size);
b->next += size;
}
void copy_in_serialized_buffer_by_offset(ser_buff_t *b, int size, char *value, int offset){
if((b->size - b->next) < size){
printf("%s(): Error : Insufficient buffer space\n", __FUNCTION__);
return;
}
if(offset > b->size){
printf("%s(): Error : Attemt to write outside buffer boundaries\n", __FUNCTION__);
return;
}
memcpy(b->b + offset, value, size);
}
void reset_serialize_buffer(ser_buff_t *b){
b->next = 0;
b->checkpoint = 0;
}