Annotation of libelwix/src/pack.c, revision 1.1.2.2
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: {
! 91: if (!RPACK_SANITY(rp))
! 92: return (uint8_t) -1;
! 93: /* No space left */
! 94: if ((size_t) (rp->r_next - rp->r_buf) >= rp->r_len)
! 95: return (uint8_t) -1;
! 96:
! 97: if (n)
! 98: *rp->r_next = *n;
! 99: return *rp->r_next++;
! 100: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>