1: #ifndef __APACK_H
2: #define __APACK_H
3:
4:
5: typedef struct tagRawPacket {
6: size_t r_len;
7: uint8_t *r_buf;
8: uint8_t *r_next;
9: } rpack_t;
10:
11: #define RPACK_FREE(x) (assert((x)), memset((x), 0, sizeof(rpack_t)))
12: #define RPACK_INIT(x, b, l) do { assert((x)); RPACK_FREE(x); \
13: (x)->r_buf = (b); \
14: (x)->r_len = (l); \
15: (x)->r_next = (x)->r_buf; \
16: } while (0)
17: #define RPACK_SANITY(x) ((x) && (x)->r_buf && (x)->r_next && (x)->r_next >= (x)->r_buf)
18: #define RPACK_LEN(x) (assert((x)), (x)->r_len)
19: #define RPACK_REWIND(x) (assert((x)), (x)->r_next = (x)->r_buf)
20:
21: #define EXTRACT_LE_8(x) (assert((x)), *(x))
22: #define EXTRACT_LE_16(x) (assert((x)), (u_int16_t) ( \
23: (uint16_t) *((const uint8_t *) (x) + 1) << 8 | \
24: (uint16_t) *((const uint8_t *) (x) + 0) \
25: ))
26: #define EXTRACT_LE_24(x) (assert((x)), (u_int32_t) ( \
27: (uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
28: (uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
29: (uint32_t) *((const uint8_t *) (x) + 0) \
30: ))
31: #define EXTRACT_LE_32(x) (assert((x)), (u_int32_t) ( \
32: (uint32_t) *((const uint8_t *) (x) + 3) << 24 | \
33: (uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
34: (uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
35: (uint32_t) *((const uint8_t *) (x) + 0) \
36: ))
37: #define EXTRACT_LE_64(x) (assert((x)), (u_int64_t) ( \
38: (uint64_t) *((const uint8_t *) (x) + 7) << 56 | \
39: (uint64_t) *((const uint8_t *) (x) + 6) << 48 | \
40: (uint64_t) *((const uint8_t *) (x) + 5) << 40 | \
41: (uint64_t) *((const uint8_t *) (x) + 4) << 32 | \
42: (uint64_t) *((const uint8_t *) (x) + 3) << 24 | \
43: (uint64_t) *((const uint8_t *) (x) + 2) << 16 | \
44: (uint64_t) *((const uint8_t *) (x) + 1) << 8 | \
45: (uint64_t) *((const uint8_t *) (x) + 0) \
46: ))
47:
48: #define EXTRACT_BE_8(x) (assert((x)), *(x))
49: #define EXTRACT_BE_16(x) (assert((x)), (u_int16_t) ( \
50: (uint16_t) *((const uint8_t *) (x) + 0) << 8 | \
51: (uint16_t) *((const uint8_t *) (x) + 1) \
52: ))
53: #define EXTRACT_BE_24(x) (assert((x)), (u_int32_t) ( \
54: (uint32_t) *((const uint8_t *) (x) + 0) << 16 | \
55: (uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
56: (uint32_t) *((const uint8_t *) (x) + 2) \
57: ))
58: #define EXTRACT_BE_32(x) (assert((x)), (u_int32_t) ( \
59: (uint32_t) *((const uint8_t *) (x) + 0) << 24 | \
60: (uint32_t) *((const uint8_t *) (x) + 1) << 16 | \
61: (uint32_t) *((const uint8_t *) (x) + 2) << 8 | \
62: (uint32_t) *((const uint8_t *) (x) + 3) \
63: ))
64: #define EXTRACT_BE_64(x) (assert((x)), (u_int64_t) ( \
65: (uint64_t) *((const uint8_t *) (x) + 0) << 56 | \
66: (uint64_t) *((const uint8_t *) (x) + 1) << 48 | \
67: (uint64_t) *((const uint8_t *) (x) + 2) << 40 | \
68: (uint64_t) *((const uint8_t *) (x) + 3) << 32 | \
69: (uint64_t) *((const uint8_t *) (x) + 4) << 24 | \
70: (uint64_t) *((const uint8_t *) (x) + 5) << 16 | \
71: (uint64_t) *((const uint8_t *) (x) + 6) << 8 | \
72: (uint64_t) *((const uint8_t *) (x) + 7) \
73: ))
74:
75:
76: /*
77: * rpack_align_and_reserve() - Align & reserve space
78: *
79: * @rp = raw buffer
80: * @siz = need size
81: * return: NULL error or not enough space, !=NULL next position
82: */
83: uint8_t *rpack_align_and_reserve(rpack_t * __restrict rp, size_t siz);
84:
85: /*
86: * rpack_create() - Allocate & init raw packet structure
87: *
88: * @buf = buffer
89: * @buflen = length of buffer
90: * return: NULL error or !=NULL raw packet, should be freed by rpack_destroy()
91: */
92: rpack_t *rpack_create(void * __restrict buf, size_t buflen);
93: /*
94: * rpack_destroy() - Release & free raw packet structure
95: *
96: * @rp = raw packet
97: * return: none
98: */
99: void rpack_destroy(rpack_t ** __restrict rp);
100:
101: /*
102: * rpack_uint8() - Pack/Unpack 8bit value
103: *
104: * @rp = raw buffer
105: * @n = set value if !=NULL
106: * return: -1 error or get value
107: */
108: uint8_t rpack_uint8(rpack_t * __restrict rp, uint8_t * __restrict n);
109: /*
110: * rpack_uint16() - Pack/Unpack 16bit value
111: *
112: * @rp = raw buffer
113: * @n = set value if !=NULL
114: * return: -1 error or get value
115: */
116: uint16_t rpack_uint16(rpack_t * __restrict rp, uint16_t * __restrict n);
117:
118:
119: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>