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

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