root/branches/cu-security-branch/src/proto/endecode-funcs.h @ 8397

Revision 8397, 31.3 KB (checked in by nlmills, 3 years ago)

initial merge with Orange-Branch. much will be broken

Line 
1/*
2 * (C) 2003-6 Pete Wyckoff, Ohio Supercomputer Center <pw@osc.edu>
3 *
4 * See COPYING in top-level directory.
5 *
6 * Defines for macros related to wire encoding and decoding.  Only included
7 * by include/pvfs2-encode-stubs.h by core encoding users.
8 */
9/* NOTE: if you make any changes to the code contained in this file, please
10 * update the PVFS2_PROTO_VERSION in pvfs2-req-proto.h accordingly
11 */
12#ifndef __SRC_PROTO_ENDECODE_FUNCS_H
13#define __SRC_PROTO_ENDECODE_FUNCS_H
14
15#include "src/io/bmi/bmi-byteswap.h"
16#include <stdint.h>
17#include <assert.h>
18
19/*
20 * NOTE - Every macro defined here needs to have a stub defined in
21 * include/pvfs2-encode-stubs.h
22 */
23
24/*
25 * Generic macros to define encoding near target structure declarations.
26 */
27
28/* basic types */
29#define encode_uint64_t(pptr,x) do { \
30    *(u_int64_t*) *(pptr) = htobmi64(*(x)); \
31    *(pptr) += 8; \
32} while (0)
33#define decode_uint64_t(pptr,x) do { \
34    *(x) = bmitoh64(*(u_int64_t*) *(pptr)); \
35    *(pptr) += 8; \
36} while (0)
37
38#define encode_int64_t(pptr,x) do { \
39    *(int64_t*) *(pptr) = htobmi64(*(x)); \
40    *(pptr) += 8; \
41} while (0)
42#define decode_int64_t(pptr,x) do { \
43    *(x) = bmitoh64(*(int64_t*) *(pptr)); \
44    *(pptr) += 8; \
45} while (0)
46
47#define encode_uint32_t(pptr,x) do { \
48    *(u_int32_t*) *(pptr) = htobmi32(*(x)); \
49    *(pptr) += 4; \
50} while (0)
51#define decode_uint32_t(pptr,x) do { \
52    *(x) = bmitoh32(*(u_int32_t*) *(pptr)); \
53    *(pptr) += 4; \
54} while (0)
55
56#define encode_PVFS_signature(pptr,x) encode_char(pptr,x)
57#define decode_PVFS_signature(pptr,x) decode_char(pptr,x)
58
59#define encode_char(pptr,x) do { \
60    *(char *) *(pptr) = *(x); \
61    *(pptr) += 1; \
62} while (0)
63#define decode_char(pptr,x) do { \
64    *(x) = *(char *) *(pptr); \
65    *(pptr) += 1; \
66} while (0)
67
68#define encode_int32_t(pptr,x) do { \
69    *(int32_t*) *(pptr) = htobmi32(*(x)); \
70    *(pptr) += 4; \
71} while (0)
72#define decode_int32_t(pptr,x) do { \
73    *(x) = bmitoh32(*(int32_t*) *(pptr)); \
74    *(pptr) += 4; \
75} while (0)
76
77/* skip 4 bytes, maybe zeroing them to avoid valgrind getting annoyed */
78#ifdef HAVE_VALGRIND_H
79#define encode_skip4(pptr,x) do { \
80    *(int32_t*) *(pptr) = 0; \
81    *(pptr) += 4; \
82} while (0)
83#else
84#define encode_skip4(pptr,x) do { \
85    *(pptr) += 4; \
86} while (0)
87#endif
88
89#define decode_skip4(pptr,x) do { \
90    *(pptr) += 4; \
91} while (0)
92
93/*
94 * Strings. Decoding just points into existing character data.  This handles
95 * NULL strings too, just encoding the length and a single zero byte.  The
96 * valgrind version zeroes out any padding.
97 */
98#ifdef HAVE_VALGRIND_H
99#define encode_string(pptr,pbuf) do { \
100    u_int32_t len = 0; \
101    if (*pbuf) \
102        len = strlen(*pbuf); \
103    *(u_int32_t *) *(pptr) = htobmi32(len); \
104    if (len) { \
105        memcpy(*(pptr)+4, *pbuf, len+1); \
106        int pad = roundup8(4 + len + 1) - (4 + len + 1); \
107        *(pptr) += roundup8(4 + len + 1); \
108        memset(*(pptr)-pad, 0, pad); \
109    } else { \
110        *(u_int32_t *) (*(pptr)+4) = 0; \
111        *(pptr) += 8; \
112    } \
113} while (0)
114#else
115#define encode_string(pptr,pbuf) do { \
116    u_int32_t len = 0; \
117    if (*pbuf) \
118        len = strlen(*pbuf); \
119    *(u_int32_t *) *(pptr) = htobmi32(len); \
120    if (len) { \
121        memcpy(*(pptr)+4, *pbuf, len+1); \
122        *(pptr) += roundup8(4 + len + 1); \
123    } else { \
124        *(u_int32_t *) (*(pptr)+4) = 0; \
125        *(pptr) += 8; \
126    } \
127} while (0)
128#endif
129
130/* determines how much protocol space a string encoding will consume */
131#define encode_string_size_check(pbuf) (strlen(*pbuf) + 5)
132
133#define decode_string(pptr,pbuf) do { \
134    u_int32_t len = bmitoh32(*(u_int32_t *) *(pptr)); \
135    *pbuf = *(pptr) + 4; \
136    *(pptr) += roundup8(4 + len + 1); \
137} while (0)
138
139/* odd variation, space exists in some structure, must copy-in string */
140#define encode_here_string(pptr,pbuf) encode_string(pptr,pbuf)
141#define decode_here_string(pptr,pbuf) do { \
142    u_int32_t len = bmitoh32(*(u_int32_t *) *(pptr)); \
143    memcpy(pbuf, *(pptr) + 4, len + 1); \
144    *(pptr) += roundup8(4 + len + 1); \
145} while (0)
146
147
148/* keyvals; a lot like strings; decoding points existing character data */
149/* BTW we are skipping the read_sz field - keep that in mind */
150#define encode_PVFS_ds_keyval(pptr,pbuf) do { \
151    u_int32_t len = ((PVFS_ds_keyval *)pbuf)->buffer_sz; \
152    *(u_int32_t *) *(pptr) = htobmi32(len); \
153    memcpy(*(pptr)+4, ((PVFS_ds_keyval *)pbuf)->buffer, len); \
154    *(pptr) += roundup8(4 + len); \
155} while (0)
156#define decode_PVFS_ds_keyval(pptr,pbuf) do { \
157    u_int32_t len = bmitoh32(*(u_int32_t *) *(pptr)); \
158    ((PVFS_ds_keyval *)pbuf)->buffer_sz = len; \
159    ((PVFS_ds_keyval *)pbuf)->buffer = *(pptr) + 4; \
160    *(pptr) += roundup8(4 + len); \
161} while (0)
162
163/*
164 * Type maps are put near the type definitions, except for this special one.
165 *
166 * Please remember when changing a fundamental type, e.g. PVFS_size, to update
167 * also the set of #defines that tell the encoder what its type really is.
168 */
169#define encode_enum encode_int32_t
170#define decode_enum decode_int32_t
171
172/* memory alloc and free, just for decoding */
173#if 0
174/* this is for debugging, if you want to see what is malloc'd */
175static inline void *decode_malloc (int n) {
176        void *p;
177        if (n>0)
178                p = malloc(n);
179        else
180                p = (void *)0;
181        printf("decode malloc %d bytes: %p\n",n,p);
182        return p;
183}
184/* this is for debugging, if you want to see what is free'd */
185static inline void decode_free (void *p) {
186        printf("decode free: %p\n",p);
187        free(p);
188}
189#else
190#define decode_malloc(n) ((n) ? malloc(n) : 0)
191#define decode_free(n) free(n)
192#endif
193
194/*
195 * These wrappers define functions to do the encoding of the types or
196 * structures they describe.  Please remember to update the empty stub versions
197 * of these routines in include/pvfs2-encode-stubs.h, although the compiler
198 * will certainly complain too.
199 *
200 * Note that decode can not take a const since we point into the
201 * undecoded buffer for strings.
202 */
203#define endecode_fields_1_generic(name, sname, t1, x1) \
204static inline void encode_##name(char **pptr, const sname *x) { \
205    encode_##t1(pptr, &x->x1); \
206} \
207static inline void decode_##name(char **pptr, sname *x) { \
208    decode_##t1(pptr, &x->x1); \
209}
210
211#define endecode_fields_1(name, t1, x1) \
212    endecode_fields_1_generic(name, name, t1, x1)
213#define endecode_fields_1_struct(name, t1, x1) \
214    endecode_fields_1_generic(name, struct name, t1, x1)
215
216#define endecode_fields_2_generic(name, sname, t1, x1, t2, x2) \
217static inline void encode_##name(char **pptr, const sname *x) { \
218    encode_##t1(pptr, &x->x1); \
219    encode_##t2(pptr, &x->x2); \
220} \
221static inline void decode_##name(char **pptr, sname *x) { \
222    decode_##t1(pptr, &x->x1); \
223    decode_##t2(pptr, &x->x2); \
224}
225
226#define endecode_fields_2(name, t1, x1, t2, x2) \
227    endecode_fields_2_generic(name, name, t1, x1, t2, x2)
228#define endecode_fields_2_struct(name, t1, x1, t2, x2) \
229    endecode_fields_2_generic(name, struct name, t1, x1, t2, x2)
230
231#define endecode_fields_3_generic(name, sname, t1, x1, t2, x2, t3, x3) \
232static inline void encode_##name(char **pptr, const sname *x) { \
233    encode_##t1(pptr, &x->x1); \
234    encode_##t2(pptr, &x->x2); \
235    encode_##t3(pptr, &x->x3); \
236} \
237static inline void decode_##name(char **pptr, sname *x) { \
238    decode_##t1(pptr, &x->x1); \
239    decode_##t2(pptr, &x->x2); \
240    decode_##t3(pptr, &x->x3); \
241}
242
243#define endecode_fields_3(name, t1, x1, t2, x2, t3, x3) \
244    endecode_fields_3_generic(name, name, t1, x1, t2, x2, t3, x3)
245#define endecode_fields_3_struct(name, t1, x1, t2, x2, t3, x3) \
246    endecode_fields_3_generic(name, struct name, t1, x1, t2, x2, t3, x3)
247
248#define endecode_fields_4_generic(name, sname, t1, x1, t2, x2, t3, x3, t4, x4) \
249static inline void encode_##name(char **pptr, const sname *x) { \
250    encode_##t1(pptr, &x->x1); \
251    encode_##t2(pptr, &x->x2); \
252    encode_##t3(pptr, &x->x3); \
253    encode_##t4(pptr, &x->x4); \
254} \
255static inline void decode_##name(char **pptr, sname *x) { \
256    decode_##t1(pptr, &x->x1); \
257    decode_##t2(pptr, &x->x2); \
258    decode_##t3(pptr, &x->x3); \
259    decode_##t4(pptr, &x->x4); \
260}
261
262#define endecode_fields_4(name, t1, x1, t2, x2, t3, x3, t4, x4) \
263    endecode_fields_4_generic(name, name, t1, x1, t2, x2, t3, x3, t4, x4)
264#define endecode_fields_4_struct(name, t1, x1, t2, x2, t3, x3, t4, x4) \
265    endecode_fields_4_generic(name, struct name, t1, x1, t2, x2, t3, x3, t4, x4)
266
267#define endecode_fields_5_generic(name, sname, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5) \
268static inline void encode_##name(char **pptr, const sname *x) { \
269    encode_##t1(pptr, &x->x1); \
270    encode_##t2(pptr, &x->x2); \
271    encode_##t3(pptr, &x->x3); \
272    encode_##t4(pptr, &x->x4); \
273    encode_##t5(pptr, &x->x5); \
274} \
275static inline void decode_##name(char **pptr, sname *x) { \
276    decode_##t1(pptr, &x->x1); \
277    decode_##t2(pptr, &x->x2); \
278    decode_##t3(pptr, &x->x3); \
279    decode_##t4(pptr, &x->x4); \
280    decode_##t5(pptr, &x->x5); \
281}
282
283#define endecode_fields_5(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5) \
284    endecode_fields_5_generic(name, name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5)
285#define endecode_fields_5_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5) \
286    endecode_fields_5_generic(name, struct name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5)
287
288#define endecode_fields_6_generic(name, sname, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6) \
289static inline void encode_##name(char **pptr, const sname *x) { \
290    encode_##t1(pptr, &x->x1); \
291    encode_##t2(pptr, &x->x2); \
292    encode_##t3(pptr, &x->x3); \
293    encode_##t4(pptr, &x->x4); \
294    encode_##t5(pptr, &x->x5); \
295    encode_##t6(pptr, &x->x6); \
296} \
297static inline void decode_##name(char **pptr, sname *x) { \
298    decode_##t1(pptr, &x->x1); \
299    decode_##t2(pptr, &x->x2); \
300    decode_##t3(pptr, &x->x3); \
301    decode_##t4(pptr, &x->x4); \
302    decode_##t5(pptr, &x->x5); \
303    decode_##t6(pptr, &x->x6); \
304}
305
306#define endecode_fields_6(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6) \
307    endecode_fields_6_generic(name, name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6)
308#define endecode_fields_6_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6) \
309    endecode_fields_6_generic(name, struct name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6)
310
311#define endecode_fields_7(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7) \
312static inline void encode_##name(char **pptr, const name *x) { \
313    encode_##t1(pptr, &x->x1); \
314    encode_##t2(pptr, &x->x2); \
315    encode_##t3(pptr, &x->x3); \
316    encode_##t4(pptr, &x->x4); \
317    encode_##t5(pptr, &x->x5); \
318    encode_##t6(pptr, &x->x6); \
319    encode_##t7(pptr, &x->x7); \
320} \
321static inline void decode_##name(char **pptr, name *x) { \
322    decode_##t1(pptr, &x->x1); \
323    decode_##t2(pptr, &x->x2); \
324    decode_##t3(pptr, &x->x3); \
325    decode_##t4(pptr, &x->x4); \
326    decode_##t5(pptr, &x->x5); \
327    decode_##t6(pptr, &x->x6); \
328    decode_##t7(pptr, &x->x7); \
329}
330
331#define endecode_fields_7_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7) \
332static inline void encode_##name(char **pptr, const struct name *x) { \
333    encode_##t1(pptr, &x->x1); \
334    encode_##t2(pptr, &x->x2); \
335    encode_##t3(pptr, &x->x3); \
336    encode_##t4(pptr, &x->x4); \
337    encode_##t5(pptr, &x->x5); \
338    encode_##t6(pptr, &x->x6); \
339    encode_##t7(pptr, &x->x7); \
340} \
341static inline void decode_##name(char **pptr, struct name *x) { \
342    decode_##t1(pptr, &x->x1); \
343    decode_##t2(pptr, &x->x2); \
344    decode_##t3(pptr, &x->x3); \
345    decode_##t4(pptr, &x->x4); \
346    decode_##t5(pptr, &x->x5); \
347    decode_##t6(pptr, &x->x6); \
348    decode_##t7(pptr, &x->x7); \
349}
350
351#define endecode_fields_8_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8) \
352static inline void encode_##name(char **pptr, const struct name *x) { \
353    encode_##t1(pptr, &x->x1); \
354    encode_##t2(pptr, &x->x2); \
355    encode_##t3(pptr, &x->x3); \
356    encode_##t4(pptr, &x->x4); \
357    encode_##t5(pptr, &x->x5); \
358    encode_##t6(pptr, &x->x6); \
359    encode_##t7(pptr, &x->x7); \
360    encode_##t8(pptr, &x->x8); \
361} \
362static inline void decode_##name(char **pptr, struct name *x) { \
363    decode_##t1(pptr, &x->x1); \
364    decode_##t2(pptr, &x->x2); \
365    decode_##t3(pptr, &x->x3); \
366    decode_##t4(pptr, &x->x4); \
367    decode_##t5(pptr, &x->x5); \
368    decode_##t6(pptr, &x->x6); \
369    decode_##t7(pptr, &x->x7); \
370    decode_##t8(pptr, &x->x8); \
371}
372
373#define endecode_fields_9_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9) \
374static inline void encode_##name(char **pptr, const struct name *x) { \
375    encode_##t1(pptr, &x->x1); \
376    encode_##t2(pptr, &x->x2); \
377    encode_##t3(pptr, &x->x3); \
378    encode_##t4(pptr, &x->x4); \
379    encode_##t5(pptr, &x->x5); \
380    encode_##t6(pptr, &x->x6); \
381    encode_##t7(pptr, &x->x7); \
382    encode_##t8(pptr, &x->x8); \
383    encode_##t9(pptr, &x->x9); \
384} \
385static inline void decode_##name(char **pptr, struct name *x) { \
386    decode_##t1(pptr, &x->x1); \
387    decode_##t2(pptr, &x->x2); \
388    decode_##t3(pptr, &x->x3); \
389    decode_##t4(pptr, &x->x4); \
390    decode_##t5(pptr, &x->x5); \
391    decode_##t6(pptr, &x->x6); \
392    decode_##t7(pptr, &x->x7); \
393    decode_##t8(pptr, &x->x8); \
394    decode_##t9(pptr, &x->x9); \
395}
396
397#define endecode_fields_10_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9,t10,x10) \
398static inline void encode_##name(char **pptr, const struct name *x) { \
399    encode_##t1(pptr, &x->x1); \
400    encode_##t2(pptr, &x->x2); \
401    encode_##t3(pptr, &x->x3); \
402    encode_##t4(pptr, &x->x4); \
403    encode_##t5(pptr, &x->x5); \
404    encode_##t6(pptr, &x->x6); \
405    encode_##t7(pptr, &x->x7); \
406    encode_##t8(pptr, &x->x8); \
407    encode_##t9(pptr, &x->x9); \
408    encode_##t10(pptr, &x->x10); \
409} \
410static inline void decode_##name(char **pptr, struct name *x) { \
411    decode_##t1(pptr, &x->x1); \
412    decode_##t2(pptr, &x->x2); \
413    decode_##t3(pptr, &x->x3); \
414    decode_##t4(pptr, &x->x4); \
415    decode_##t5(pptr, &x->x5); \
416    decode_##t6(pptr, &x->x6); \
417    decode_##t7(pptr, &x->x7); \
418    decode_##t8(pptr, &x->x8); \
419    decode_##t9(pptr, &x->x9); \
420    decode_##t10(pptr, &x->x10); \
421}
422
423#define endecode_fields_11_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9,t10,x10,t11,x11) \
424static inline void encode_##name(char **pptr, const struct name *x) { \
425    encode_##t1(pptr, &x->x1); \
426    encode_##t2(pptr, &x->x2); \
427    encode_##t3(pptr, &x->x3); \
428    encode_##t4(pptr, &x->x4); \
429    encode_##t5(pptr, &x->x5); \
430    encode_##t6(pptr, &x->x6); \
431    encode_##t7(pptr, &x->x7); \
432    encode_##t8(pptr, &x->x8); \
433    encode_##t9(pptr, &x->x9); \
434    encode_##t10(pptr, &x->x10); \
435    encode_##t11(pptr, &x->x11); \
436} \
437static inline void decode_##name(char **pptr, struct name *x) { \
438    decode_##t1(pptr, &x->x1); \
439    decode_##t2(pptr, &x->x2); \
440    decode_##t3(pptr, &x->x3); \
441    decode_##t4(pptr, &x->x4); \
442    decode_##t5(pptr, &x->x5); \
443    decode_##t6(pptr, &x->x6); \
444    decode_##t7(pptr, &x->x7); \
445    decode_##t8(pptr, &x->x8); \
446    decode_##t9(pptr, &x->x9); \
447    decode_##t10(pptr, &x->x10); \
448    decode_##t11(pptr, &x->x11); \
449}
450
451#define endecode_fields_12(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9,t10,x10,t11,x11,t12,x12) \
452static inline void encode_##name(char **pptr, const name *x) { \
453    encode_##t1(pptr, &x->x1); \
454    encode_##t2(pptr, &x->x2); \
455    encode_##t3(pptr, &x->x3); \
456    encode_##t4(pptr, &x->x4); \
457    encode_##t5(pptr, &x->x5); \
458    encode_##t6(pptr, &x->x6); \
459    encode_##t7(pptr, &x->x7); \
460    encode_##t8(pptr, &x->x8); \
461    encode_##t9(pptr, &x->x9); \
462    encode_##t10(pptr, &x->x10); \
463    encode_##t11(pptr, &x->x11); \
464    encode_##t12(pptr, &x->x12); \
465} \
466static inline void decode_##name(char **pptr, name *x) { \
467    decode_##t1(pptr, &x->x1); \
468    decode_##t2(pptr, &x->x2); \
469    decode_##t3(pptr, &x->x3); \
470    decode_##t4(pptr, &x->x4); \
471    decode_##t5(pptr, &x->x5); \
472    decode_##t6(pptr, &x->x6); \
473    decode_##t7(pptr, &x->x7); \
474    decode_##t8(pptr, &x->x8); \
475    decode_##t9(pptr, &x->x9); \
476    decode_##t10(pptr, &x->x10); \
477    decode_##t11(pptr, &x->x11); \
478    decode_##t12(pptr, &x->x12); \
479}
480
481#define endecode_fields_15_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7, \
482    t8,x8,t9,x9,t10,x10,t11,x11,t12,x12,t13,x13,t14,x14,t15,x15) \
483static inline void encode_##name(char **pptr, const struct name *x) { \
484    encode_##t1(pptr, &x->x1); \
485    encode_##t2(pptr, &x->x2); \
486    encode_##t3(pptr, &x->x3); \
487    encode_##t4(pptr, &x->x4); \
488    encode_##t5(pptr, &x->x5); \
489    encode_##t6(pptr, &x->x6); \
490    encode_##t7(pptr, &x->x7); \
491    encode_##t8(pptr, &x->x8); \
492    encode_##t9(pptr, &x->x9); \
493    encode_##t10(pptr, &x->x10); \
494    encode_##t11(pptr, &x->x11); \
495    encode_##t12(pptr, &x->x12); \
496    encode_##t13(pptr, &x->x13); \
497    encode_##t14(pptr, &x->x14); \
498    encode_##t15(pptr, &x->x15); \
499} \
500static inline void decode_##name(char **pptr, struct name *x) { \
501    decode_##t1(pptr, &x->x1); \
502    decode_##t2(pptr, &x->x2); \
503    decode_##t3(pptr, &x->x3); \
504    decode_##t4(pptr, &x->x4); \
505    decode_##t5(pptr, &x->x5); \
506    decode_##t6(pptr, &x->x6); \
507    decode_##t7(pptr, &x->x7); \
508    decode_##t8(pptr, &x->x8); \
509    decode_##t9(pptr, &x->x9); \
510    decode_##t10(pptr, &x->x10); \
511    decode_##t11(pptr, &x->x11); \
512    decode_##t12(pptr, &x->x12); \
513    decode_##t13(pptr, &x->x13); \
514    decode_##t14(pptr, &x->x14); \
515    decode_##t15(pptr, &x->x15); \
516}
517
518#define endecode_fields_16_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7, \
519    t8,x8,t9,x9,t10,x10,t11,x11,t12,x12,t13,x13,t14,x14,t15,x15,t16,x16) \
520static inline void encode_##name(char **pptr, const struct name *x) { \
521    encode_##t1(pptr, &x->x1); \
522    encode_##t2(pptr, &x->x2); \
523    encode_##t3(pptr, &x->x3); \
524    encode_##t4(pptr, &x->x4); \
525    encode_##t5(pptr, &x->x5); \
526    encode_##t6(pptr, &x->x6); \
527    encode_##t7(pptr, &x->x7); \
528    encode_##t8(pptr, &x->x8); \
529    encode_##t9(pptr, &x->x9); \
530    encode_##t10(pptr, &x->x10); \
531    encode_##t11(pptr, &x->x11); \
532    encode_##t12(pptr, &x->x12); \
533    encode_##t13(pptr, &x->x13); \
534    encode_##t14(pptr, &x->x14); \
535    encode_##t15(pptr, &x->x15); \
536    encode_##t16(pptr, &x->x16); \
537} \
538static inline void decode_##name(char **pptr, struct name *x) { \
539    decode_##t1(pptr, &x->x1); \
540    decode_##t2(pptr, &x->x2); \
541    decode_##t3(pptr, &x->x3); \
542    decode_##t4(pptr, &x->x4); \
543    decode_##t5(pptr, &x->x5); \
544    decode_##t6(pptr, &x->x6); \
545    decode_##t7(pptr, &x->x7); \
546    decode_##t8(pptr, &x->x8); \
547    decode_##t9(pptr, &x->x9); \
548    decode_##t10(pptr, &x->x10); \
549    decode_##t11(pptr, &x->x11); \
550    decode_##t12(pptr, &x->x12); \
551    decode_##t13(pptr, &x->x13); \
552    decode_##t14(pptr, &x->x14); \
553    decode_##t15(pptr, &x->x15); \
554    decode_##t16(pptr, &x->x16); \
555}
556
557
558#define endecode_fields_17_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7, \
559    t8,x8,t9,x9,t10,x10,t11,x11,t12,x12,t13,x13,t14,x14,t15,x15,t16,x16,t17,x17) \
560static inline void encode_##name(char **pptr, const struct name *x) { \
561    encode_##t1(pptr, &x->x1); \
562    encode_##t2(pptr, &x->x2); \
563    encode_##t3(pptr, &x->x3); \
564    encode_##t4(pptr, &x->x4); \
565    encode_##t5(pptr, &x->x5); \
566    encode_##t6(pptr, &x->x6); \
567    encode_##t7(pptr, &x->x7); \
568    encode_##t8(pptr, &x->x8); \
569    encode_##t9(pptr, &x->x9); \
570    encode_##t10(pptr, &x->x10); \
571    encode_##t11(pptr, &x->x11); \
572    encode_##t12(pptr, &x->x12); \
573    encode_##t13(pptr, &x->x13); \
574    encode_##t14(pptr, &x->x14); \
575    encode_##t15(pptr, &x->x15); \
576    encode_##t16(pptr, &x->x16); \
577    encode_##t17(pptr, &x->x17); \
578} \
579static inline void decode_##name(char **pptr, struct name *x) { \
580    decode_##t1(pptr, &x->x1); \
581    decode_##t2(pptr, &x->x2); \
582    decode_##t3(pptr, &x->x3); \
583    decode_##t4(pptr, &x->x4); \
584    decode_##t5(pptr, &x->x5); \
585    decode_##t6(pptr, &x->x6); \
586    decode_##t7(pptr, &x->x7); \
587    decode_##t8(pptr, &x->x8); \
588    decode_##t9(pptr, &x->x9); \
589    decode_##t10(pptr, &x->x10); \
590    decode_##t11(pptr, &x->x11); \
591    decode_##t12(pptr, &x->x12); \
592    decode_##t13(pptr, &x->x13); \
593    decode_##t14(pptr, &x->x14); \
594    decode_##t15(pptr, &x->x15); \
595    decode_##t16(pptr, &x->x16); \
596    decode_##t17(pptr, &x->x17); \
597}
598
599/* ones with arrays that are allocated in the decode */
600
601/* one field then one array */
602#define endecode_fields_1a_generic(name, sname, t1, x1, tn1, n1, ta1, a1) \
603static inline void encode_##name(char **pptr, const sname *x) { typeof(tn1) i; \
604    encode_##t1(pptr, &x->x1); \
605    encode_##tn1(pptr, &x->n1); \
606    for (i=0; i<x->n1; i++) \
607        encode_##ta1(pptr, &(x)->a1[i]); \
608} \
609static inline void decode_##name(char **pptr, sname *x) { typeof(tn1) i; \
610    decode_##t1(pptr, &x->x1); \
611    decode_##tn1(pptr, &x->n1); \
612    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
613    for (i=0; i<x->n1; i++) \
614        decode_##ta1(pptr, &(x)->a1[i]); \
615}
616
617/* one field then two arrays */
618#define endecode_fields_1aa_generic(name, sname, t1, x1, tn1, n1, ta1, a1, ta2, a2) \
619static inline void encode_##name(char **pptr, const sname *x) { typeof(tn1) i; \
620    encode_##t1(pptr, &x->x1); \
621    encode_##tn1(pptr, &x->n1); \
622    for (i=0; i<x->n1; i++) \
623        encode_##ta1(pptr, &(x)->a1[i]); \
624    for (i=0; i<x->n1; i++) \
625        encode_##ta2(pptr, &(x)->a2[i]); \
626} \
627static inline void decode_##name(char **pptr, sname *x) { typeof(tn1) i; \
628    decode_##t1(pptr, &x->x1); \
629    decode_##tn1(pptr, &x->n1); \
630    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
631    for (i=0; i<x->n1; i++) \
632        decode_##ta1(pptr, &(x)->a1[i]); \
633    x->a2 = decode_malloc(x->n1 * sizeof(*x->a2)); \
634    for (i=0; i<x->n1; i++) \
635        decode_##ta2(pptr, &(x)->a2[i]); \
636}
637
638
639#define endecode_fields_1a(name, t1, x1, tn1, n1, ta1, a1) \
640    endecode_fields_1a_generic(name, name, t1, x1, tn1, n1, ta1, a1)
641#define endecode_fields_1a_struct(name, t1, x1, tn1, n1, ta1, a1) \
642    endecode_fields_1a_generic(name, struct name, t1, x1, tn1, n1, ta1, a1)
643
644#define endecode_fields_1aa(name, t1, x1, tn1, n1, ta1, a1, ta2, a2) \
645    endecode_fields_1aa_generic(name, name, t1, x1, tn1, n1, ta1, a1, ta2, a2)
646#define endecode_fields_1aa_struct(name, t1, x1, tn1, n1, ta1, a1, ta2, a2) \
647    endecode_fields_1aa_generic(name, struct name, t1, x1, tn1, n1, ta1, a1, ta2, a2)
648
649/* one field, and array, another field, another array - a special case */
650#define endecode_fields_1a_1a_struct(name, t1,x1, tn1, n1, ta1, a1, t2,x2, tn2, n2, ta2, a2) \
651static inline void encode_##name(char **pptr, const struct name *x) { int i; \
652    encode_##t1(pptr, &x->x1); \
653    encode_##tn1(pptr, &x->n1); \
654    for (i=0; i<x->n1; i++) { int n; n = i; \
655        encode_##ta1(pptr, &(x)->a1[n]); } \
656    encode_##t2(pptr, &x->x2); \
657    encode_##tn2(pptr, &x->n2); \
658    for (i=0; i<x->n2; i++) { int n; n = i; \
659        encode_##ta2(pptr, &(x)->a2[n]); } \
660} \
661static inline void decode_##name(char **pptr, struct name *x) { int i; \
662    decode_##t1(pptr, &x->x1); \
663    decode_##tn1(pptr, &x->n1); \
664    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
665    for (i=0; i<x->n1; i++) \
666        decode_##ta1(pptr, &(x)->a1[i]); \
667    decode_##t1(pptr, &x->x1); \
668    decode_##tn2(pptr, &x->n2); \
669    x->a2 = decode_malloc(x->n2 * sizeof(*x->a2)); \
670    for (i=0; i<x->n2; i++) \
671        decode_##ta2(pptr, &(x)->a2[i]); \
672}
673
674/* 2 fields, then an array */
675#define endecode_fields_2a_generic(name, sname, t1, x1, t2, x2, tn1, n1, ta1, a1) \
676static inline void encode_##name(char **pptr, const sname *x) { int i; \
677    encode_##t1(pptr, &x->x1); \
678    encode_##t2(pptr, &x->x2); \
679    encode_##tn1(pptr, &x->n1); \
680    for (i=0; i<x->n1; i++) \
681        encode_##ta1(pptr, &(x)->a1[i]); \
682} \
683static inline void decode_##name(char **pptr, sname *x) { int i; \
684    decode_##t1(pptr, &x->x1); \
685    decode_##t2(pptr, &x->x2); \
686    decode_##tn1(pptr, &x->n1); \
687    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
688    for (i=0; i<x->n1; i++) \
689        decode_##ta1(pptr, &(x)->a1[i]); \
690}
691
692#define endecode_fields_2a(name, t1, x1, t2, x2, tn1, n1, ta1, a1) \
693    endecode_fields_2a_generic(name, name, t1, x1, t2, x2, tn1, n1, ta1, a1)
694#define endecode_fields_2a_struct(name, t1, x1, t2, x2, tn1, n1, ta1, a1) \
695    endecode_fields_2a_generic(name, struct name, t1, x1, t2, x2, tn1, n1, ta1, a1)
696
697/* special case where we have two arrays of the same size after 2 fields */
698#define endecode_fields_2aa_struct(name, t1, x1, t2, x2, tn1, n1, ta1, a1, ta2, a2) \
699static inline void encode_##name(char **pptr, const struct name *x) { int i; \
700    encode_##t1(pptr, &x->x1); \
701    encode_##t2(pptr, &x->x2); \
702    encode_##tn1(pptr, &x->n1); \
703    for (i=0; i<x->n1; i++) \
704        encode_##ta1(pptr, &(x)->a1[i]); \
705    for (i=0; i<x->n1; i++) \
706        encode_##ta2(pptr, &(x)->a2[i]); \
707} \
708static inline void decode_##name(char **pptr, struct name *x) { int i; \
709    decode_##t1(pptr, &x->x1); \
710    decode_##t2(pptr, &x->x2); \
711    decode_##tn1(pptr, &x->n1); \
712    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
713    for (i=0; i<x->n1; i++) \
714        decode_##ta1(pptr, &(x)->a1[i]); \
715    x->a2 = decode_malloc(x->n1 * sizeof(*x->a2)); \
716    for (i=0; i<x->n1; i++) \
717        decode_##ta2(pptr, &(x)->a2[i]); \
718}
719
720/* 3 fields, then an array */
721#define endecode_fields_3a_struct(name, t1, x1, t2, x2, t3, x3, tn1, n1, ta1, a1) \
722static inline void encode_##name(char **pptr, const struct name *x) { int i; \
723    encode_##t1(pptr, &x->x1); \
724    encode_##t2(pptr, &x->x2); \
725    encode_##t3(pptr, &x->x3); \
726    encode_##tn1(pptr, &x->n1); \
727    for (i=0; i<x->n1; i++) \
728        encode_##ta1(pptr, &(x)->a1[i]); \
729} \
730static inline void decode_##name(char **pptr, struct name *x) { int i; \
731    decode_##t1(pptr, &x->x1); \
732    decode_##t2(pptr, &x->x2); \
733    decode_##t3(pptr, &x->x3); \
734    decode_##tn1(pptr, &x->n1); \
735    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
736    for (i=0; i<x->n1; i++) \
737        decode_##ta1(pptr, &(x)->a1[i]); \
738}
739
740/* special case where we have two arrays of the same size after 4 fields */
741#define endecode_fields_4aa_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, tn1, n1, ta1, a1, ta2, a2) \
742static inline void encode_##name(char **pptr, const struct name *x) { int i; \
743    encode_##t1(pptr, &x->x1); \
744    encode_##t2(pptr, &x->x2); \
745    encode_##t3(pptr, &x->x3); \
746    encode_##t4(pptr, &x->x4); \
747    encode_##tn1(pptr, &x->n1); \
748    for (i=0; i<x->n1; i++) \
749        encode_##ta1(pptr, &(x)->a1[i]); \
750    for (i=0; i<x->n1; i++) \
751        encode_##ta2(pptr, &(x)->a2[i]); \
752} \
753static inline void decode_##name(char **pptr, struct name *x) { int i; \
754    decode_##t1(pptr, &x->x1); \
755    decode_##t2(pptr, &x->x2); \
756    decode_##t3(pptr, &x->x3); \
757    decode_##t4(pptr, &x->x4); \
758    decode_##tn1(pptr, &x->n1); \
759    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
760    for (i=0; i<x->n1; i++) \
761        decode_##ta1(pptr, &(x)->a1[i]); \
762    x->a2 = decode_malloc(x->n1 * sizeof(*x->a2)); \
763    for (i=0; i<x->n1; i++) \
764        decode_##ta2(pptr, &(x)->a2[i]); \
765}
766
767
768/* 4 fields, then an array */
769#define endecode_fields_4a_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, tn1,n1,ta1,a1) \
770static inline void encode_##name(char **pptr, const struct name *x) { int i; \
771    encode_##t1(pptr, &x->x1); \
772    encode_##t2(pptr, &x->x2); \
773    encode_##t3(pptr, &x->x3); \
774    encode_##t4(pptr, &x->x4); \
775    encode_##tn1(pptr, &x->n1); \
776    for (i=0; i<x->n1; i++) \
777        encode_##ta1(pptr, &(x)->a1[i]); \
778} \
779static inline void decode_##name(char **pptr, struct name *x) { int i; \
780    decode_##t1(pptr, &x->x1); \
781    decode_##t2(pptr, &x->x2); \
782    decode_##t3(pptr, &x->x3); \
783    decode_##t4(pptr, &x->x4); \
784    decode_##tn1(pptr, &x->n1); \
785    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
786    for (i=0; i<x->n1; i++) \
787        decode_##ta1(pptr, &(x)->a1[i]); \
788}
789
790/* 5 fields, then an array */
791#define endecode_fields_5a_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, tn1,n1,ta1,a1) \
792static inline void encode_##name(char **pptr, const struct name *x) { int i; \
793    encode_##t1(pptr, &x->x1); \
794    encode_##t2(pptr, &x->x2); \
795    encode_##t3(pptr, &x->x3); \
796    encode_##t4(pptr, &x->x4); \
797    encode_##t5(pptr, &x->x5); \
798    encode_##tn1(pptr, &x->n1); \
799    for (i=0; i<x->n1; i++) \
800        encode_##ta1(pptr, &(x)->a1[i]); \
801} \
802static inline void decode_##name(char **pptr, struct name *x) { int i; \
803    decode_##t1(pptr, &x->x1); \
804    decode_##t2(pptr, &x->x2); \
805    decode_##t3(pptr, &x->x3); \
806    decode_##t4(pptr, &x->x4); \
807    decode_##t5(pptr, &x->x5); \
808    decode_##tn1(pptr, &x->n1); \
809    x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
810    for (i=0; i<x->n1; i++) \
811        decode_##ta1(pptr, &(x)->a1[i]); \
812}
813
814#define DEFINE_STATIC_ENDECODE_FUNCS(__name__, __type__) \
815__attribute__((unused)) \
816static void encode_func_##__name__(char **pptr, void *x) \
817{ \
818    encode_##__name__(pptr, (__type__ *)x); \
819}; \
820__attribute__((unused)) \
821static void decode_func_##__name__(char **pptr, void *x) \
822{ \
823    decode_##__name__(pptr, (__type__ *)x); \
824}
825
826#define encode_enum_union_2_struct(name, ename, uname, ut1, un1, en1, ut2, un2, en2)                         \
827static inline void encode_##name(char **pptr, const struct name *x)           \
828{                                                                             \
829    encode_enum(pptr, &x->ename);                                             \
830    switch(x->ename)                                                          \
831    {                                                                         \
832        case en1: encode_##ut1(pptr, &x->uname.un1); break;                   \
833        case en2: encode_##ut2(pptr, &x->uname.un2); break;                   \
834        default: assert(0);                                                   \
835    }                                                                         \
836};                                                                            \
837static inline void decode_##name(char **pptr, struct name *x)                 \
838{                                                                             \
839    decode_enum(pptr, &x->ename);                                             \
840    switch(x->ename)                                                          \
841    {                                                                         \
842        case en1: decode_##ut1(pptr, &x->uname.un1); break;                   \
843        case en2: decode_##ut2(pptr, &x->uname.un2); break;                   \
844        default: assert(0);                                                   \
845    }                                                                         \
846};
847/* 3 fields, then an array, then 2 fields, then an array */
848#define endecode_fields_3a2a_struct(name, t1, x1, t2, x2, t3, x3, tn1, n1, ta1, a1, t4, x4, t5, x5, tn2, n2, ta2, a2) \
849static inline void encode_##name(char **pptr, const struct name *x) { int i; \
850    encode_##t1(pptr, &x->x1); \
851    encode_##t2(pptr, &x->x2); \
852    encode_##t3(pptr, &x->x3); \
853    encode_##tn1(pptr, &x->n1); \
854    if (x->n1 > 0) \
855        for (i=0; i<x->n1; i++) \
856            encode_##ta1(pptr, &(x)->a1[i]); \
857    align8(pptr); \
858    encode_##t4(pptr, &x->x4); \
859    encode_##t5(pptr, &x->x5); \
860    encode_##tn2(pptr, &x->n2); \
861    if (x->n2 > 0) \
862        for (i=0; i<x->n2; i++) \
863            encode_##ta2(pptr, &(x)->a2[i]); \
864    align8(pptr); \
865} \
866static inline void decode_##name(char **pptr, struct name *x) { int i; \
867    decode_##t1(pptr, &x->x1); \
868    decode_##t2(pptr, &x->x2); \
869    decode_##t3(pptr, &x->x3); \
870    decode_##tn1(pptr, &x->n1); \
871    if (x->n1 > 0) \
872    { \
873        x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
874        for (i=0; i<x->n1; i++) \
875            decode_##ta1(pptr, &(x)->a1[i]); \
876    } \
877    else \
878        x->a1 = NULL; \
879    align8(pptr); \
880    decode_##t4(pptr, &x->x4); \
881    decode_##t5(pptr, &x->x5); \
882    decode_##tn2(pptr, &x->n2); \
883    if (x->n2 > 0) \
884    { \
885        x->a2 = decode_malloc(x->n2 * sizeof(*x->a2)); \
886        for (i=0; i<x->n2; i++) \
887            decode_##ta2(pptr, &(x)->a2[i]); \
888    } \
889    else \
890        x->a2 = NULL; \
891    align8(pptr); \
892}
893
894#endif  /* __SRC_PROTO_ENDECODE_FUNCS_H */
895
Note: See TracBrowser for help on using the browser.