File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / apack.h
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Tue Jul 9 00:18:19 2013 UTC (10 years, 11 months ago) by misho
Branches: MAIN
CVS tags: elwix2_6, elwix2_5, elwix2_4, elwix2_3, elwix2_2, elwix2_1, HEAD, ELWIX2_5, ELWIX2_4, ELWIX2_3, ELWIX2_2, ELWIX2_1, ELWIX2_0
version 2.0

    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_INITIALIZER	{ .r_len = 0, .r_buf = NULL, .r_next = NULL }
   12: #define RPACK_FREE(x)		(assert((x)), memset((x), 0, sizeof(rpack_t)))
   13: #define RPACK_INIT(x, b, l)	do { assert((x)); RPACK_FREE(x); \
   14: 					(x)->r_buf = (b); \
   15:        					(x)->r_len = (l); \
   16: 					(x)->r_next = (x)->r_buf; \
   17: 				} while (0)
   18: #define RPACK_SANITY(x)		((x) && (x)->r_buf && (x)->r_next && (x)->r_next >= (x)->r_buf)
   19: #define RPACK_LEN(x)		(assert((x)), (x)->r_len)
   20: #define RPACK_REWIND(x)		(assert((x)), (x)->r_next = (x)->r_buf)
   21: 
   22: #define RPACK_SET_16(x, n)	do { assert((x)); \
   23: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   24: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   25: 				} while (0)
   26: #define RPACK_SET_24(x, n)	do { assert((x)); \
   27: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   28: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   29: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   30: 				} while (0)
   31: #define RPACK_SET_32(x, n)	do { assert((x)); \
   32: 					*((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
   33: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   34: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   35: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   36: 				} while (0)
   37: #define RPACK_SET_64(x, n)	do { assert((x)); \
   38: 					*((uint8_t *) (x) + 7) = *((const uint8_t *) (n) + 7); \
   39: 					*((uint8_t *) (x) + 6) = *((const uint8_t *) (n) + 6); \
   40: 					*((uint8_t *) (x) + 5) = *((const uint8_t *) (n) + 5); \
   41: 					*((uint8_t *) (x) + 4) = *((const uint8_t *) (n) + 4); \
   42: 					*((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
   43: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   44: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   45: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   46: 				} while (0)
   47: 
   48: #define EXTRACT_LE_8(x)		(assert((x)), *(x))
   49: #define EXTRACT_LE_16(x)	(assert((x)), (u_int16_t) ( \
   50: 					(uint16_t) *((const uint8_t *) (x) + 1) << 8 | \
   51: 					(uint16_t) *((const uint8_t *) (x) + 0) \
   52: 				))
   53: #define EXTRACT_LE_24(x)	(assert((x)), (u_int32_t) ( \
   54: 					(uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
   55: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
   56: 					(uint32_t) *((const uint8_t *) (x) + 0) \
   57: 				))
   58: #define EXTRACT_LE_32(x)	(assert((x)), (u_int32_t) ( \
   59: 					(uint32_t) *((const uint8_t *) (x) + 3) << 24 | \
   60: 					(uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
   61: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
   62: 					(uint32_t) *((const uint8_t *) (x) + 0) \
   63: 				))
   64: #define EXTRACT_LE_64(x)	(assert((x)), (u_int64_t) ( \
   65: 					(uint64_t) *((const uint8_t *) (x) + 7) << 56 | \
   66: 					(uint64_t) *((const uint8_t *) (x) + 6) << 48 | \
   67: 					(uint64_t) *((const uint8_t *) (x) + 5) << 40 | \
   68: 					(uint64_t) *((const uint8_t *) (x) + 4) << 32 | \
   69: 					(uint64_t) *((const uint8_t *) (x) + 3) << 24 | \
   70: 					(uint64_t) *((const uint8_t *) (x) + 2) << 16 | \
   71: 					(uint64_t) *((const uint8_t *) (x) + 1) << 8 | \
   72: 					(uint64_t) *((const uint8_t *) (x) + 0) \
   73: 				))
   74: 
   75: #define EXTRACT_BE_8(x)		(assert((x)), *(x))
   76: #define EXTRACT_BE_16(x)	(assert((x)), (u_int16_t) ( \
   77: 					(uint16_t) *((const uint8_t *) (x) + 0) << 8 | \
   78: 					(uint16_t) *((const uint8_t *) (x) + 1) \
   79: 				))
   80: #define EXTRACT_BE_24(x)	(assert((x)), (u_int32_t) ( \
   81: 					(uint32_t) *((const uint8_t *) (x) + 0) << 16 | \
   82: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
   83: 					(uint32_t) *((const uint8_t *) (x) + 2) \
   84: 				))
   85: #define EXTRACT_BE_32(x)	(assert((x)), (u_int32_t) ( \
   86: 					(uint32_t) *((const uint8_t *) (x) + 0) << 24 | \
   87: 					(uint32_t) *((const uint8_t *) (x) + 1) << 16 | \
   88: 					(uint32_t) *((const uint8_t *) (x) + 2) << 8 | \
   89: 					(uint32_t) *((const uint8_t *) (x) + 3) \
   90: 				))
   91: #define EXTRACT_BE_64(x)	(assert((x)), (u_int64_t) ( \
   92: 					(uint64_t) *((const uint8_t *) (x) + 0) << 56 | \
   93: 					(uint64_t) *((const uint8_t *) (x) + 1) << 48 | \
   94: 					(uint64_t) *((const uint8_t *) (x) + 2) << 40 | \
   95: 					(uint64_t) *((const uint8_t *) (x) + 3) << 32 | \
   96: 					(uint64_t) *((const uint8_t *) (x) + 4) << 24 | \
   97: 					(uint64_t) *((const uint8_t *) (x) + 5) << 16 | \
   98: 					(uint64_t) *((const uint8_t *) (x) + 6) << 8 | \
   99: 					(uint64_t) *((const uint8_t *) (x) + 7) \
  100: 				))
  101: 
  102: 
  103: /*
  104:  * rpack_align_and_reserve() - Align & reserve space
  105:  *
  106:  * @rp = raw buffer
  107:  * @siz = need size
  108:  * return: NULL error or not enough space, !=NULL next position
  109:  */
  110: uint8_t *rpack_align_and_reserve(rpack_t * __restrict rp, size_t siz);
  111: 
  112: /*
  113:  * rpack_create() - Allocate & init raw packet structure
  114:  *
  115:  * @buf = buffer
  116:  * @buflen = length of buffer
  117:  * return: NULL error or !=NULL raw packet, should be freed by rpack_destroy()
  118:  */
  119: rpack_t *rpack_create(void * __restrict buf, size_t buflen);
  120: /*
  121:  * rpack_destroy() - Release & free raw packet structure
  122:  *
  123:  * @rp = raw packet
  124:  * return: none
  125:  */
  126: void rpack_destroy(rpack_t ** __restrict rp);
  127: 
  128: /*
  129:  * rpack_uint8() - Pack/Unpack 8bit value
  130:  *
  131:  * @rp = raw buffer
  132:  * @n = set value if !=NULL
  133:  * return: -1 error or get value
  134:  */
  135: uint8_t rpack_uint8(rpack_t * __restrict rp, uint8_t * __restrict n);
  136: /*
  137:  * rpack_uint16() - Pack/Unpack 16bit value
  138:  *
  139:  * @rp = raw buffer
  140:  * @n = set value if !=NULL
  141:  * return: -1 error or get value
  142:  */
  143: uint16_t rpack_uint16(rpack_t * __restrict rp, uint16_t * __restrict n);
  144: /*
  145:  * rpack_uint24() - Pack/Unpack 24bit value
  146:  *
  147:  * @rp = raw buffer
  148:  * @n = set value if !=NULL
  149:  * return: -1 error or get value
  150:  */
  151: uint32_t rpack_uint24(rpack_t * __restrict rp, uint32_t * __restrict n);
  152: /*
  153:  * rpack_uint32() - Pack/Unpack 32bit value
  154:  *
  155:  * @rp = raw buffer
  156:  * @n = set value if !=NULL
  157:  * return: -1 error or get value
  158:  */
  159: uint32_t rpack_uint32(rpack_t * __restrict rp, uint32_t * __restrict n);
  160: /*
  161:  * rpack_uint64() - Pack/Unpack 64bit value
  162:  *
  163:  * @rp = raw buffer
  164:  * @n = set value if !=NULL
  165:  * return: -1 error or get value
  166:  */
  167: uint64_t rpack_uint64(rpack_t * __restrict rp, uint64_t * __restrict n);
  168: 
  169: 
  170: #endif

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