File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / apack.h
Revision 1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Sun Jun 30 21:46:58 2013 UTC (11 years ago) by misho
Branches: elwix1_9
add new macro

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>