File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / apack.h
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Sun Jun 30 22:10:27 2013 UTC (11 years ago) by misho
Branches: MAIN
CVS tags: elwix2_0, HEAD, ELWIX1_9
version 1.9

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

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