File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / apack.h
Revision 1.8.2.3: download - view: text, annotated - select for diffs - revision graph
Wed Feb 12 09:29:15 2014 UTC (10 years, 5 months ago) by misho
Branches: elwix3_3
Diff to: branchpoint 1.8: preferred, unified
add macro

    1: /*************************************************************************
    2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: apack.h,v 1.8.2.3 2014/02/12 09:29:15 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2014
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #ifndef __APACK_H
   47: #define __APACK_H
   48: 
   49: 
   50: typedef struct tagRawPacket {
   51: 	size_t	r_len;
   52: 	uint8_t	*r_buf;
   53: 	uint8_t	*r_next;
   54: } rpack_t;
   55: 
   56: #define RPACK_INITIALIZER	{ .r_len = 0, .r_buf = NULL, .r_next = NULL }
   57: #define RPACK_FREE(x)		(assert((x)), memset((x), 0, sizeof(rpack_t)))
   58: #define RPACK_INIT(x, b, l)	do { assert((x)); RPACK_FREE(x); \
   59: 					(x)->r_buf = (b); \
   60:        					(x)->r_len = (l); \
   61: 					(x)->r_next = (x)->r_buf; \
   62: 				} while (0)
   63: #define RPACK_SANITY(x)		((x) && (x)->r_buf && (x)->r_next && (x)->r_next >= (x)->r_buf)
   64: #define RPACK_BUF(x)		(assert((x)), (x)->r_buf)
   65: #define RPACK_LEN(x)		(assert((x)), (x)->r_len)
   66: #define RPACK_REWIND(x)		(assert((x)), (x)->r_next = (x)->r_buf)
   67: #define RPACK_OFF(x)		(assert((x)), (x)->r_next - (x)->r_buf)
   68: 
   69: #define RPACK_SET_16(x, n)	do { assert((x)); \
   70: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   71: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   72: 				} while (0)
   73: #define RPACK_SET_24(x, n)	do { assert((x)); \
   74: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   75: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   76: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   77: 				} while (0)
   78: #define RPACK_SET_32(x, n)	do { assert((x)); \
   79: 					*((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
   80: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   81: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   82: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   83: 				} while (0)
   84: #define RPACK_SET_64(x, n)	do { assert((x)); \
   85: 					*((uint8_t *) (x) + 7) = *((const uint8_t *) (n) + 7); \
   86: 					*((uint8_t *) (x) + 6) = *((const uint8_t *) (n) + 6); \
   87: 					*((uint8_t *) (x) + 5) = *((const uint8_t *) (n) + 5); \
   88: 					*((uint8_t *) (x) + 4) = *((const uint8_t *) (n) + 4); \
   89: 					*((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
   90: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   91: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   92: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   93: 				} while (0)
   94: 
   95: #define EXTRACT_LE_8(x)		(assert((x)), *(x))
   96: #define EXTRACT_LE_16(x)	(assert((x)), (u_int16_t) ( \
   97: 					(uint16_t) *((const uint8_t *) (x) + 1) << 8 | \
   98: 					(uint16_t) *((const uint8_t *) (x) + 0) \
   99: 				))
  100: #define EXTRACT_LE_24(x)	(assert((x)), (u_int32_t) ( \
  101: 					(uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
  102: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
  103: 					(uint32_t) *((const uint8_t *) (x) + 0) \
  104: 				))
  105: #define EXTRACT_LE_32(x)	(assert((x)), (u_int32_t) ( \
  106: 					(uint32_t) *((const uint8_t *) (x) + 3) << 24 | \
  107: 					(uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
  108: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
  109: 					(uint32_t) *((const uint8_t *) (x) + 0) \
  110: 				))
  111: #define EXTRACT_LE_64(x)	(assert((x)), (u_int64_t) ( \
  112: 					(uint64_t) *((const uint8_t *) (x) + 7) << 56 | \
  113: 					(uint64_t) *((const uint8_t *) (x) + 6) << 48 | \
  114: 					(uint64_t) *((const uint8_t *) (x) + 5) << 40 | \
  115: 					(uint64_t) *((const uint8_t *) (x) + 4) << 32 | \
  116: 					(uint64_t) *((const uint8_t *) (x) + 3) << 24 | \
  117: 					(uint64_t) *((const uint8_t *) (x) + 2) << 16 | \
  118: 					(uint64_t) *((const uint8_t *) (x) + 1) << 8 | \
  119: 					(uint64_t) *((const uint8_t *) (x) + 0) \
  120: 				))
  121: 
  122: #define EXTRACT_BE_8(x)		(assert((x)), *(x))
  123: #define EXTRACT_BE_16(x)	(assert((x)), (u_int16_t) ( \
  124: 					(uint16_t) *((const uint8_t *) (x) + 0) << 8 | \
  125: 					(uint16_t) *((const uint8_t *) (x) + 1) \
  126: 				))
  127: #define EXTRACT_BE_24(x)	(assert((x)), (u_int32_t) ( \
  128: 					(uint32_t) *((const uint8_t *) (x) + 0) << 16 | \
  129: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
  130: 					(uint32_t) *((const uint8_t *) (x) + 2) \
  131: 				))
  132: #define EXTRACT_BE_32(x)	(assert((x)), (u_int32_t) ( \
  133: 					(uint32_t) *((const uint8_t *) (x) + 0) << 24 | \
  134: 					(uint32_t) *((const uint8_t *) (x) + 1) << 16 | \
  135: 					(uint32_t) *((const uint8_t *) (x) + 2) << 8 | \
  136: 					(uint32_t) *((const uint8_t *) (x) + 3) \
  137: 				))
  138: #define EXTRACT_BE_64(x)	(assert((x)), (u_int64_t) ( \
  139: 					(uint64_t) *((const uint8_t *) (x) + 0) << 56 | \
  140: 					(uint64_t) *((const uint8_t *) (x) + 1) << 48 | \
  141: 					(uint64_t) *((const uint8_t *) (x) + 2) << 40 | \
  142: 					(uint64_t) *((const uint8_t *) (x) + 3) << 32 | \
  143: 					(uint64_t) *((const uint8_t *) (x) + 4) << 24 | \
  144: 					(uint64_t) *((const uint8_t *) (x) + 5) << 16 | \
  145: 					(uint64_t) *((const uint8_t *) (x) + 6) << 8 | \
  146: 					(uint64_t) *((const uint8_t *) (x) + 7) \
  147: 				))
  148: 
  149: 
  150: /*
  151:  * rpack_align_and_reserve() - Align & reserve space
  152:  *
  153:  * @rp = raw buffer
  154:  * @siz = need size
  155:  * return: NULL error or not enough space, !=NULL next position
  156:  */
  157: uint8_t *rpack_align_and_reserve(rpack_t * __restrict rp, size_t siz);
  158: 
  159: /*
  160:  * rpack_create() - Allocate & init raw packet structure
  161:  *
  162:  * @buf = buffer
  163:  * @buflen = length of buffer
  164:  * return: NULL error or !=NULL raw packet, should be freed by rpack_destroy()
  165:  */
  166: rpack_t *rpack_create(void * __restrict buf, size_t buflen);
  167: /*
  168:  * rpack_destroy() - Release & free raw packet structure
  169:  *
  170:  * @rp = raw packet
  171:  * return: none
  172:  */
  173: void rpack_destroy(rpack_t ** __restrict rp);
  174: 
  175: /*
  176:  * rpack_attach() - Attach dynamic allocating buffer at packet
  177:  *
  178:  * @rp = raw packet;
  179:  * @len = allocate bytes
  180:  * return: -1 error or 0 ok, should be detached with rpack_detach() 
  181:  * 				before call rpack_destroy() after use!
  182:  */
  183: int rpack_attach(rpack_t * __restrict rp, size_t len);
  184: /*
  185:  * rpack_resize() - Resize dynamic allocated buffer at packet
  186:  *
  187:  * @rp = raw packet
  188:  * @newlen = resize buffer to bytes
  189:  * return: -1 error or 0 ok, should be detached with rpack_detach() 
  190:  * 				before call rpack_destroy() after use!
  191:  */
  192: int rpack_resize(rpack_t * __restrict rp, size_t newlen);
  193: /*
  194:  * rpack_detach() - Detach and free dynamic allocated buffer from packet
  195:  *
  196:  * @rp = raw packet
  197:  * return: none
  198:  */
  199: void rpack_detach(rpack_t * __restrict rp);
  200: 
  201: /*
  202:  * rpack_uint8() - Pack/Unpack 8bit value
  203:  *
  204:  * @rp = raw buffer
  205:  * @n = set value if !=NULL
  206:  * return: -1 error or get value
  207:  */
  208: uint8_t rpack_uint8(rpack_t * __restrict rp, uint8_t * __restrict n);
  209: /*
  210:  * rpack_uint16() - Pack/Unpack 16bit value
  211:  *
  212:  * @rp = raw buffer
  213:  * @n = set value if !=NULL
  214:  * @be = extract in big-endian
  215:  * return: -1 error or get value
  216:  */
  217: uint16_t rpack_uint16(rpack_t * __restrict rp, uint16_t * __restrict n, int be);
  218: /*
  219:  * rpack_ruint16() - Pack/Unpack raw 16bit value
  220:  *
  221:  * @rp = raw buffer
  222:  * @n = set value if !=NULL
  223:  * @be = extract in big-endian
  224:  * return: -1 error or get value
  225:  */
  226: uint16_t rpack_ruint16(rpack_t * __restrict rp, uint16_t * __restrict n, int be);
  227: /*
  228:  * rpack_uint24() - Pack/Unpack 24bit value
  229:  *
  230:  * @rp = raw buffer
  231:  * @n = set value if !=NULL
  232:  * @be = extract in big-endian
  233:  * return: -1 error or get value
  234:  */
  235: uint32_t rpack_uint24(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  236: /*
  237:  * rpack_ruint24() - Pack/Unpack raw 24bit value
  238:  *
  239:  * @rp = raw buffer
  240:  * @n = set value if !=NULL
  241:  * @be = extract in big-endian
  242:  * return: -1 error or get value
  243:  */
  244: uint32_t rpack_ruint24(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  245: /*
  246:  * rpack_uint32() - Pack/Unpack 32bit value
  247:  *
  248:  * @rp = raw buffer
  249:  * @n = set value if !=NULL
  250:  * @be = extract in big-endian
  251:  * return: -1 error or get value
  252:  */
  253: uint32_t rpack_uint32(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  254: /*
  255:  * rpack_ruint32() - Pack/Unpack raw 32bit value
  256:  *
  257:  * @rp = raw buffer
  258:  * @n = set value if !=NULL
  259:  * @be = extract in big-endian
  260:  * return: -1 error or get value
  261:  */
  262: uint32_t rpack_ruint32(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  263: /*
  264:  * rpack_uint64() - Pack/Unpack 64bit value
  265:  *
  266:  * @rp = raw buffer
  267:  * @n = set value if !=NULL
  268:  * @be = extract in big-endian
  269:  * return: -1 error or get value
  270:  */
  271: uint64_t rpack_uint64(rpack_t * __restrict rp, uint64_t * __restrict n, int be);
  272: /*
  273:  * rpack_ruint64() - Pack/Unpack raw 64bit value
  274:  *
  275:  * @rp = raw buffer
  276:  * @n = set value if !=NULL
  277:  * @be = extract in big-endian
  278:  * return: -1 error or get value
  279:  */
  280: uint64_t rpack_ruint64(rpack_t * __restrict rp, uint64_t * __restrict n, int be);
  281: /*
  282:  * rpack_data() - Pack/Unpack align data
  283:  *
  284:  * @rp = raw buffer
  285:  * @dat = data
  286:  * @datlen = data length
  287:  * return: NULL error or != NULL get data, must be e_free() after use!
  288:  */
  289: void *rpack_data(rpack_t * __restrict rp, void * __restrict dat, size_t datlen);
  290: /*
  291:  * rpack_rdata() - Pack/Unpack raw data
  292:  *
  293:  * @rp = raw buffer
  294:  * @dat = data
  295:  * @datlen = data length
  296:  * return: NULL error or != NULL get data, must be e_free() after use!
  297:  */
  298: void *rpack_rdata(rpack_t * __restrict rp, void * __restrict dat, size_t datlen);
  299: 
  300: 
  301: #endif

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