Annotation of libelwix/inc/elwix/apack.h, revision 1.5

1.4       misho       1: /*************************************************************************
                      2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.5     ! misho       6: * $Id: apack.h,v 1.4.2.1 2014/01/29 14:15:58 misho Exp $
1.4       misho       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: 
1.5     ! misho      15: Copyright 2004 - 2014
1.4       misho      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: */
1.2       misho      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: 
1.3       misho      56: #define RPACK_INITIALIZER      { .r_len = 0, .r_buf = NULL, .r_next = NULL }
1.2       misho      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_LEN(x)           (assert((x)), (x)->r_len)
                     65: #define RPACK_REWIND(x)                (assert((x)), (x)->r_next = (x)->r_buf)
                     66: 
                     67: #define RPACK_SET_16(x, n)     do { assert((x)); \
                     68:                                        *((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
                     69:                                        *((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
                     70:                                } while (0)
                     71: #define RPACK_SET_24(x, n)     do { assert((x)); \
                     72:                                        *((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
                     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_32(x, n)     do { assert((x)); \
                     77:                                        *((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
                     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_64(x, n)     do { assert((x)); \
                     83:                                        *((uint8_t *) (x) + 7) = *((const uint8_t *) (n) + 7); \
                     84:                                        *((uint8_t *) (x) + 6) = *((const uint8_t *) (n) + 6); \
                     85:                                        *((uint8_t *) (x) + 5) = *((const uint8_t *) (n) + 5); \
                     86:                                        *((uint8_t *) (x) + 4) = *((const uint8_t *) (n) + 4); \
                     87:                                        *((uint8_t *) (x) + 3) = *((const uint8_t *) (n) + 3); \
                     88:                                        *((uint8_t *) (x) + 2) = *((const uint8_t *) (n) + 2); \
                     89:                                        *((uint8_t *) (x) + 1) = *((const uint8_t *) (n) + 1); \
                     90:                                        *((uint8_t *) (x) + 0) = *((const uint8_t *) (n) + 0); \
                     91:                                } while (0)
                     92: 
                     93: #define EXTRACT_LE_8(x)                (assert((x)), *(x))
                     94: #define EXTRACT_LE_16(x)       (assert((x)), (u_int16_t) ( \
                     95:                                        (uint16_t) *((const uint8_t *) (x) + 1) << 8 | \
                     96:                                        (uint16_t) *((const uint8_t *) (x) + 0) \
                     97:                                ))
                     98: #define EXTRACT_LE_24(x)       (assert((x)), (u_int32_t) ( \
                     99:                                        (uint32_t) *((const uint8_t *) (x) + 2) << 16 | \
                    100:                                        (uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
                    101:                                        (uint32_t) *((const uint8_t *) (x) + 0) \
                    102:                                ))
                    103: #define EXTRACT_LE_32(x)       (assert((x)), (u_int32_t) ( \
                    104:                                        (uint32_t) *((const uint8_t *) (x) + 3) << 24 | \
                    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_64(x)       (assert((x)), (u_int64_t) ( \
                    110:                                        (uint64_t) *((const uint8_t *) (x) + 7) << 56 | \
                    111:                                        (uint64_t) *((const uint8_t *) (x) + 6) << 48 | \
                    112:                                        (uint64_t) *((const uint8_t *) (x) + 5) << 40 | \
                    113:                                        (uint64_t) *((const uint8_t *) (x) + 4) << 32 | \
                    114:                                        (uint64_t) *((const uint8_t *) (x) + 3) << 24 | \
                    115:                                        (uint64_t) *((const uint8_t *) (x) + 2) << 16 | \
                    116:                                        (uint64_t) *((const uint8_t *) (x) + 1) << 8 | \
                    117:                                        (uint64_t) *((const uint8_t *) (x) + 0) \
                    118:                                ))
                    119: 
                    120: #define EXTRACT_BE_8(x)                (assert((x)), *(x))
                    121: #define EXTRACT_BE_16(x)       (assert((x)), (u_int16_t) ( \
                    122:                                        (uint16_t) *((const uint8_t *) (x) + 0) << 8 | \
                    123:                                        (uint16_t) *((const uint8_t *) (x) + 1) \
                    124:                                ))
                    125: #define EXTRACT_BE_24(x)       (assert((x)), (u_int32_t) ( \
                    126:                                        (uint32_t) *((const uint8_t *) (x) + 0) << 16 | \
                    127:                                        (uint32_t) *((const uint8_t *) (x) + 1) << 8 | \
                    128:                                        (uint32_t) *((const uint8_t *) (x) + 2) \
                    129:                                ))
                    130: #define EXTRACT_BE_32(x)       (assert((x)), (u_int32_t) ( \
                    131:                                        (uint32_t) *((const uint8_t *) (x) + 0) << 24 | \
                    132:                                        (uint32_t) *((const uint8_t *) (x) + 1) << 16 | \
                    133:                                        (uint32_t) *((const uint8_t *) (x) + 2) << 8 | \
                    134:                                        (uint32_t) *((const uint8_t *) (x) + 3) \
                    135:                                ))
                    136: #define EXTRACT_BE_64(x)       (assert((x)), (u_int64_t) ( \
                    137:                                        (uint64_t) *((const uint8_t *) (x) + 0) << 56 | \
                    138:                                        (uint64_t) *((const uint8_t *) (x) + 1) << 48 | \
                    139:                                        (uint64_t) *((const uint8_t *) (x) + 2) << 40 | \
                    140:                                        (uint64_t) *((const uint8_t *) (x) + 3) << 32 | \
                    141:                                        (uint64_t) *((const uint8_t *) (x) + 4) << 24 | \
                    142:                                        (uint64_t) *((const uint8_t *) (x) + 5) << 16 | \
                    143:                                        (uint64_t) *((const uint8_t *) (x) + 6) << 8 | \
                    144:                                        (uint64_t) *((const uint8_t *) (x) + 7) \
                    145:                                ))
                    146: 
                    147: 
                    148: /*
                    149:  * rpack_align_and_reserve() - Align & reserve space
                    150:  *
                    151:  * @rp = raw buffer
                    152:  * @siz = need size
                    153:  * return: NULL error or not enough space, !=NULL next position
                    154:  */
                    155: uint8_t *rpack_align_and_reserve(rpack_t * __restrict rp, size_t siz);
                    156: 
                    157: /*
                    158:  * rpack_create() - Allocate & init raw packet structure
                    159:  *
                    160:  * @buf = buffer
                    161:  * @buflen = length of buffer
                    162:  * return: NULL error or !=NULL raw packet, should be freed by rpack_destroy()
                    163:  */
                    164: rpack_t *rpack_create(void * __restrict buf, size_t buflen);
                    165: /*
                    166:  * rpack_destroy() - Release & free raw packet structure
                    167:  *
                    168:  * @rp = raw packet
                    169:  * return: none
                    170:  */
                    171: void rpack_destroy(rpack_t ** __restrict rp);
                    172: 
                    173: /*
                    174:  * rpack_uint8() - Pack/Unpack 8bit value
                    175:  *
                    176:  * @rp = raw buffer
                    177:  * @n = set value if !=NULL
                    178:  * return: -1 error or get value
                    179:  */
                    180: uint8_t rpack_uint8(rpack_t * __restrict rp, uint8_t * __restrict n);
                    181: /*
                    182:  * rpack_uint16() - Pack/Unpack 16bit value
                    183:  *
                    184:  * @rp = raw buffer
                    185:  * @n = set value if !=NULL
                    186:  * return: -1 error or get value
                    187:  */
                    188: uint16_t rpack_uint16(rpack_t * __restrict rp, uint16_t * __restrict n);
                    189: /*
                    190:  * rpack_uint24() - Pack/Unpack 24bit value
                    191:  *
                    192:  * @rp = raw buffer
                    193:  * @n = set value if !=NULL
                    194:  * return: -1 error or get value
                    195:  */
                    196: uint32_t rpack_uint24(rpack_t * __restrict rp, uint32_t * __restrict n);
                    197: /*
                    198:  * rpack_uint32() - Pack/Unpack 32bit value
                    199:  *
                    200:  * @rp = raw buffer
                    201:  * @n = set value if !=NULL
                    202:  * return: -1 error or get value
                    203:  */
                    204: uint32_t rpack_uint32(rpack_t * __restrict rp, uint32_t * __restrict n);
                    205: /*
                    206:  * rpack_uint64() - Pack/Unpack 64bit value
                    207:  *
                    208:  * @rp = raw buffer
                    209:  * @n = set value if !=NULL
                    210:  * return: -1 error or get value
                    211:  */
                    212: uint64_t rpack_uint64(rpack_t * __restrict rp, uint64_t * __restrict n);
                    213: 
                    214: 
                    215: #endif

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