File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / apack.h
Revision 1.10: download - view: text, annotated - select for diffs - revision graph
Thu Jun 25 17:53:49 2015 UTC (8 years, 11 months ago) by misho
Branches: MAIN
CVS tags: elwix5_5, elwix5_4, elwix5_3, elwix5_2, elwix5_1, elwix5_0, elwix4_9, elwix4_8, elwix4_7, elwix4_6, elwix4_5, elwix4_4, elwix4_3, elwix4_26, elwix4_25, elwix4_24, elwix4_23, elwix4_22, elwix4_21, elwix4_20, elwix4_2, elwix4_19, elwix4_18, elwix4_17, elwix4_16, elwix4_15, elwix4_14, elwix4_13, elwix4_12, elwix4_11, elwix4_10, elwix4_1, elwix3_9, elwix3_8, HEAD, ELWIX5_4, ELWIX5_3, ELWIX5_2, ELWIX5_1, ELWIX5_0, ELWIX4_9, ELWIX4_8, ELWIX4_7, ELWIX4_6, ELWIX4_5, ELWIX4_4, ELWIX4_3, ELWIX4_26, ELWIX4_25, ELWIX4_24, ELWIX4_23, ELWIX4_22, ELWIX4_21, ELWIX4_20, ELWIX4_2, ELWIX4_19, ELWIX4_18, ELWIX4_17, ELWIX4_16, ELWIX4_15, ELWIX4_14, ELWIX4_13, ELWIX4_12, ELWIX4_11, ELWIX4_10, ELWIX4_1, ELWIX4_0, ELWIX3_8, ELWIX3_7
version 3.7

    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.10 2015/06/25 17:53:49 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 - 2015
   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_NEXT(x)		(assert((x)), (x)->r_next)
   66: #define RPACK_LEN(x)		(assert((x)), (x)->r_len)
   67: #define RPACK_REWIND(x)		(assert((x)), (x)->r_next = (x)->r_buf)
   68: #define RPACK_OFF(x)		(assert((x)), (x)->r_next - (x)->r_buf)
   69: #define RPACK_REMAIN(x)		(assert((x)), (x)->r_len - ((x)->r_next - (x)->r_buf))
   70: #define RPACK_ISEND(x)		(assert((x)), (size_t) ((x)->r_next - (x)->r_buf) >= (x)->r_len)
   71: 
   72: #define RPACK_SET_16(x, n)	do { assert((x)); \
   73: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   74: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   75: 				} while (0)
   76: #define RPACK_SET_24(x, n)	do { assert((x)); \
   77: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   78: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   79: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   80: 				} while (0)
   81: #define RPACK_SET_32(x, n)	do { assert((x)); \
   82: 					*((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
   83: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   84: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   85: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   86: 				} while (0)
   87: #define RPACK_SET_64(x, n)	do { assert((x)); \
   88: 					*((uint8_t *) (x) + 7) = *((const uint8_t *) (n) + 7); \
   89: 					*((uint8_t *) (x) + 6) = *((const uint8_t *) (n) + 6); \
   90: 					*((uint8_t *) (x) + 5) = *((const uint8_t *) (n) + 5); \
   91: 					*((uint8_t *) (x) + 4) = *((const uint8_t *) (n) + 4); \
   92: 					*((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
   93: 					*((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
   94: 					*((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
   95: 					*((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
   96: 				} while (0)
   97: 
   98: #define EXTRACT_LE_8(x)		(assert((x)), *(x))
   99: #define EXTRACT_LE_16(x)	(assert((x)), (u_int16_t) ( \
  100: 					(uint16_t) *((const uint8_t *) (x) + 1) << 8 | \
  101: 					(uint16_t) *((const uint8_t *) (x) + 0) \
  102: 				))
  103: #define EXTRACT_LE_24(x)	(assert((x)), (u_int32_t) ( \
  104: 					(uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
  105: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
  106: 					(uint32_t) *((const uint8_t *) (x) + 0) \
  107: 				))
  108: #define EXTRACT_LE_32(x)	(assert((x)), (u_int32_t) ( \
  109: 					(uint32_t) *((const uint8_t *) (x) + 3) << 24 | \
  110: 					(uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
  111: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
  112: 					(uint32_t) *((const uint8_t *) (x) + 0) \
  113: 				))
  114: #define EXTRACT_LE_64(x)	(assert((x)), (u_int64_t) ( \
  115: 					(uint64_t) *((const uint8_t *) (x) + 7) << 56 | \
  116: 					(uint64_t) *((const uint8_t *) (x) + 6) << 48 | \
  117: 					(uint64_t) *((const uint8_t *) (x) + 5) << 40 | \
  118: 					(uint64_t) *((const uint8_t *) (x) + 4) << 32 | \
  119: 					(uint64_t) *((const uint8_t *) (x) + 3) << 24 | \
  120: 					(uint64_t) *((const uint8_t *) (x) + 2) << 16 | \
  121: 					(uint64_t) *((const uint8_t *) (x) + 1) << 8 | \
  122: 					(uint64_t) *((const uint8_t *) (x) + 0) \
  123: 				))
  124: 
  125: #define EXTRACT_BE_8(x)		(assert((x)), *(x))
  126: #define EXTRACT_BE_16(x)	(assert((x)), (u_int16_t) ( \
  127: 					(uint16_t) *((const uint8_t *) (x) + 0) << 8 | \
  128: 					(uint16_t) *((const uint8_t *) (x) + 1) \
  129: 				))
  130: #define EXTRACT_BE_24(x)	(assert((x)), (u_int32_t) ( \
  131: 					(uint32_t) *((const uint8_t *) (x) + 0) << 16 | \
  132: 					(uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
  133: 					(uint32_t) *((const uint8_t *) (x) + 2) \
  134: 				))
  135: #define EXTRACT_BE_32(x)	(assert((x)), (u_int32_t) ( \
  136: 					(uint32_t) *((const uint8_t *) (x) + 0) << 24 | \
  137: 					(uint32_t) *((const uint8_t *) (x) + 1) << 16 | \
  138: 					(uint32_t) *((const uint8_t *) (x) + 2) << 8 | \
  139: 					(uint32_t) *((const uint8_t *) (x) + 3) \
  140: 				))
  141: #define EXTRACT_BE_64(x)	(assert((x)), (u_int64_t) ( \
  142: 					(uint64_t) *((const uint8_t *) (x) + 0) << 56 | \
  143: 					(uint64_t) *((const uint8_t *) (x) + 1) << 48 | \
  144: 					(uint64_t) *((const uint8_t *) (x) + 2) << 40 | \
  145: 					(uint64_t) *((const uint8_t *) (x) + 3) << 32 | \
  146: 					(uint64_t) *((const uint8_t *) (x) + 4) << 24 | \
  147: 					(uint64_t) *((const uint8_t *) (x) + 5) << 16 | \
  148: 					(uint64_t) *((const uint8_t *) (x) + 6) << 8 | \
  149: 					(uint64_t) *((const uint8_t *) (x) + 7) \
  150: 				))
  151: 
  152: 
  153: /*
  154:  * rpack_align_and_reserve() - Align & reserve space
  155:  *
  156:  * @rp = raw buffer
  157:  * @siz = need size
  158:  * return: NULL error or not enough space, !=NULL next position
  159:  */
  160: uint8_t *rpack_align_and_reserve(rpack_t * __restrict rp, size_t siz);
  161: /*
  162:  * rpack_next() - Get and set current position
  163:  *
  164:  * @rp = raw packet
  165:  * @after_len = move aligned current position after length
  166:  * return: NULL error or current position
  167:  */
  168: uint8_t *rpack_next(rpack_t * __restrict rp, size_t after_len);
  169: /*
  170:  * rpack_rnext() - Get and set raw current position
  171:  *
  172:  * @rp = raw packet
  173:  * @after_len = !=0 move current position after length
  174:  * return: NULL error or raw current position
  175:  */
  176: uint8_t *rpack_rnext(rpack_t * __restrict rp, size_t after_len);
  177: 
  178: /*
  179:  * rpack_create() - Allocate & init raw packet structure
  180:  *
  181:  * @buf = buffer
  182:  * @buflen = length of buffer
  183:  * return: NULL error or !=NULL raw packet, should be freed by rpack_destroy()
  184:  */
  185: rpack_t *rpack_create(void * __restrict buf, size_t buflen);
  186: /*
  187:  * rpack_destroy() - Release & free raw packet structure
  188:  *
  189:  * @rp = raw packet
  190:  * return: none
  191:  */
  192: void rpack_destroy(rpack_t ** __restrict rp);
  193: 
  194: /*
  195:  * rpack_attach() - Attach dynamic allocating buffer at packet
  196:  *
  197:  * @rp = raw packet;
  198:  * @len = allocate bytes
  199:  * return: -1 error or 0 ok, should be detached with rpack_detach() 
  200:  * 				before call rpack_destroy() after use!
  201:  */
  202: int rpack_attach(rpack_t * __restrict rp, size_t len);
  203: /*
  204:  * rpack_resize() - Resize dynamic allocated buffer at packet
  205:  *
  206:  * @rp = raw packet
  207:  * @newlen = resize buffer to bytes
  208:  * return: -1 error or 0 ok, should be detached with rpack_detach() 
  209:  * 				before call rpack_destroy() after use!
  210:  */
  211: int rpack_resize(rpack_t * __restrict rp, size_t newlen);
  212: /*
  213:  * rpack_detach() - Detach and free dynamic allocated buffer from packet
  214:  *
  215:  * @rp = raw packet
  216:  * return: none
  217:  */
  218: void rpack_detach(rpack_t * __restrict rp);
  219: 
  220: /*
  221:  * rpack_uint8() - Pack/Unpack 8bit value
  222:  *
  223:  * @rp = raw buffer
  224:  * @n = set value if !=NULL
  225:  * return: -1 error or get value
  226:  */
  227: uint8_t rpack_uint8(rpack_t * __restrict rp, uint8_t * __restrict n);
  228: /*
  229:  * rpack_uint16() - Pack/Unpack 16bit value
  230:  *
  231:  * @rp = raw buffer
  232:  * @n = set value if !=NULL
  233:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  234:  * return: -1 error or get value
  235:  */
  236: uint16_t rpack_uint16(rpack_t * __restrict rp, uint16_t * __restrict n, int be);
  237: /*
  238:  * rpack_ruint16() - Pack/Unpack raw 16bit value
  239:  *
  240:  * @rp = raw buffer
  241:  * @n = set value if !=NULL
  242:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  243:  * return: -1 error or get value
  244:  */
  245: uint16_t rpack_ruint16(rpack_t * __restrict rp, uint16_t * __restrict n, int be);
  246: /*
  247:  * rpack_uint24() - Pack/Unpack 24bit value
  248:  *
  249:  * @rp = raw buffer
  250:  * @n = set value if !=NULL
  251:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  252:  * return: -1 error or get value
  253:  */
  254: uint32_t rpack_uint24(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  255: /*
  256:  * rpack_ruint24() - Pack/Unpack raw 24bit value
  257:  *
  258:  * @rp = raw buffer
  259:  * @n = set value if !=NULL
  260:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  261:  * return: -1 error or get value
  262:  */
  263: uint32_t rpack_ruint24(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  264: /*
  265:  * rpack_uint32() - Pack/Unpack 32bit value
  266:  *
  267:  * @rp = raw buffer
  268:  * @n = set value if !=NULL
  269:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  270:  * return: -1 error or get value
  271:  */
  272: uint32_t rpack_uint32(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  273: /*
  274:  * rpack_ruint32() - Pack/Unpack raw 32bit value
  275:  *
  276:  * @rp = raw buffer
  277:  * @n = set value if !=NULL
  278:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  279:  * return: -1 error or get value
  280:  */
  281: uint32_t rpack_ruint32(rpack_t * __restrict rp, uint32_t * __restrict n, int be);
  282: /*
  283:  * rpack_uint64() - Pack/Unpack 64bit value
  284:  *
  285:  * @rp = raw buffer
  286:  * @n = set value if !=NULL
  287:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  288:  * return: -1 error or get value
  289:  */
  290: uint64_t rpack_uint64(rpack_t * __restrict rp, uint64_t * __restrict n, int be);
  291: /*
  292:  * rpack_ruint64() - Pack/Unpack raw 64bit value
  293:  *
  294:  * @rp = raw buffer
  295:  * @n = set value if !=NULL
  296:  * @be = byte order [-1 little endian, 1 big endian and 0 host order]
  297:  * return: -1 error or get value
  298:  */
  299: uint64_t rpack_ruint64(rpack_t * __restrict rp, uint64_t * __restrict n, int be);
  300: /*
  301:  * rpack_data() - Pack/Unpack align data
  302:  *
  303:  * @rp = raw buffer
  304:  * @dat = data
  305:  * @datlen = data length
  306:  * return: NULL error or != NULL get data, must be e_free() after use!
  307:  */
  308: void *rpack_data(rpack_t * __restrict rp, void * __restrict dat, size_t datlen);
  309: /*
  310:  * rpack_rdata() - Pack/Unpack raw data
  311:  *
  312:  * @rp = raw buffer
  313:  * @dat = data
  314:  * @datlen = data length
  315:  * return: NULL error or != NULL get data, must be e_free() after use!
  316:  */
  317: void *rpack_rdata(rpack_t * __restrict rp, void * __restrict dat, size_t datlen);
  318: 
  319: 
  320: #endif

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