Annotation of libelwix/src/pack.c, revision 1.1.2.3
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.2 misho 4: static inline uint8_t *
5: rpack_next_boundary(uint8_t * __restrict buf, uint8_t * __restrict p, size_t align)
6: {
7: size_t misa = (size_t) (p - buf) % align;
1.1.2.1 misho 8:
1.1.2.2 misho 9: if (!misa)
10: return p;
11:
12: return p + (align - misa);
13: }
14:
15:
16: /*
17: * rpack_create() - Allocate & init raw packet structure
18: *
19: * @buf = buffer
20: * @buflen = length of buffer
21: * return: NULL error or !=NULL raw packet, should be freed by rpack_destroy()
22: */
23: rpack_t *
24: rpack_create(void * __restrict buf, size_t buflen)
25: {
26: rpack_t *rp = NULL;
27:
28: rp = e_malloc(sizeof(rpack_t));
29: if (!rp) {
30: LOGERR;
31: return NULL;
32: }
33:
34: RPACK_INIT(rp, buf, buflen);
35: return rp;
36: }
37:
38: /*
39: * rpack_destroy() - Release & free raw packet structure
40: *
41: * @rp = raw packet
42: * return: none
43: */
44: void
45: rpack_destroy(rpack_t ** __restrict rp)
46: {
47: if (!rp)
48: return;
49:
50: if (*rp) {
51: RPACK_FREE(*rp);
52: e_free(*rp);
53: *rp = NULL;
54: }
55: }
56:
57: /*
58: * rpack_align_and_reserve() - Align & reserve space
59: *
60: * @rp = raw buffer
61: * @siz = need size
62: * return: NULL error or not enough space, !=NULL next position
63: */
64: uint8_t *
65: rpack_align_and_reserve(rpack_t * __restrict rp, size_t siz)
66: {
67: uint8_t *n;
68:
69: if (!RPACK_SANITY(rp))
70: return NULL;
71:
72: n = rpack_next_boundary(rp->r_buf, rp->r_next, siz);
73: /* too little space for siz */
74: if (n - rp->r_buf + siz > rp->r_len)
75: return NULL;
76:
77: return n;
78: }
79:
80:
81: /*
82: * rpack_uint8() - Pack/Unpack 8bit value
83: *
84: * @rp = raw buffer
85: * @n = set value if !=NULL
86: * return: -1 error or get value
87: */
88: uint8_t
89: rpack_uint8(rpack_t * __restrict rp, uint8_t * __restrict n)
90: {
1.1.2.3 ! misho 91: uint8_t u;
! 92:
1.1.2.2 misho 93: if (!RPACK_SANITY(rp))
94: return (uint8_t) -1;
95: /* No space left */
96: if ((size_t) (rp->r_next - rp->r_buf) >= rp->r_len)
97: return (uint8_t) -1;
98:
1.1.2.3 ! misho 99: u = *rp->r_next;
1.1.2.2 misho 100: if (n)
101: *rp->r_next = *n;
1.1.2.3 ! misho 102:
! 103: rp->r_next++;
! 104: return u;
! 105: }
! 106:
! 107: /*
! 108: * rpack_uint16() - Pack/Unpack 16bit value
! 109: *
! 110: * @rp = raw buffer
! 111: * @n = set value if !=NULL
! 112: * return: -1 error or get value
! 113: */
! 114: uint16_t
! 115: rpack_uint16(rpack_t * __restrict rp, uint16_t * __restrict n)
! 116: {
! 117: uint16_t u;
! 118: uint8_t *next;
! 119:
! 120: if (!RPACK_SANITY(rp))
! 121: return (uint16_t) -1;
! 122: /* No space left */
! 123: if (!(next = rpack_align_and_reserve(rp, sizeof(uint16_t))))
! 124: return (uint16_t) -1;
! 125:
! 126: u = EXTRACT_LE_16(next);
! 127: if (n)
! 128: EXTRACT_SET_16(next, *n);
! 129:
! 130: rp->r_next = next + sizeof(uint16_t);
! 131: return u;
1.1.2.2 misho 132: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>