Annotation of libelwix/src/pack.c, revision 1.2.16.1

1.2.16.1! misho       1: /*************************************************************************
        !             2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
        !             3: *  by Michael Pounov <misho@elwix.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: str.c,v 1.3 2013/05/30 09:07:33 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: */
1.2       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: static inline uint8_t *
                     50: rpack_next_boundary(uint8_t * __restrict buf, uint8_t * __restrict p, size_t align)
                     51: {
                     52:        size_t misa = (size_t) (p - buf) % align;
                     53: 
                     54:        if (!misa)
                     55:                return p;
                     56: 
                     57:        return p + (align - misa);
                     58: }
                     59: 
                     60: 
                     61: /*
                     62:  * rpack_create() - Allocate & init raw packet structure
                     63:  *
                     64:  * @buf = buffer
                     65:  * @buflen = length of buffer
                     66:  * return: NULL error or !=NULL raw packet, should be freed by rpack_destroy()
                     67:  */
                     68: rpack_t *
                     69: rpack_create(void * __restrict buf, size_t buflen)
                     70: {
                     71:        rpack_t *rp = NULL;
                     72: 
                     73:        rp = e_malloc(sizeof(rpack_t));
                     74:        if (!rp) {
                     75:                LOGERR;
                     76:                return NULL;
                     77:        }
                     78: 
                     79:        RPACK_INIT(rp, buf, buflen);
                     80:        return rp;
                     81: }
                     82: 
                     83: /*
                     84:  * rpack_destroy() - Release & free raw packet structure
                     85:  *
                     86:  * @rp = raw packet
                     87:  * return: none
                     88:  */
                     89: void
                     90: rpack_destroy(rpack_t ** __restrict rp)
                     91: {
                     92:        if (!rp)
                     93:                return;
                     94: 
                     95:        if (*rp) {
                     96:                RPACK_FREE(*rp);
                     97:                e_free(*rp);
                     98:                *rp = NULL;
                     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 *
                    110: rpack_align_and_reserve(rpack_t * __restrict rp, size_t siz)
                    111: {
                    112:        uint8_t *n;
                    113: 
                    114:        if (!RPACK_SANITY(rp))
                    115:                return NULL;
                    116: 
                    117:        n = rpack_next_boundary(rp->r_buf, rp->r_next, siz);
                    118:        /* too little space for siz */
                    119:        if (n - rp->r_buf + siz > rp->r_len)
                    120:                return NULL;
                    121: 
                    122:        return n;
                    123: }
                    124: 
                    125: 
                    126: /*
                    127:  * rpack_uint8() - Pack/Unpack 8bit value
                    128:  *
                    129:  * @rp = raw buffer
                    130:  * @n = set value if !=NULL
                    131:  * return: -1 error or get value
                    132:  */
                    133: uint8_t
                    134: rpack_uint8(rpack_t * __restrict rp, uint8_t * __restrict n)
                    135: {
                    136:        uint8_t u;
                    137: 
                    138:        if (!RPACK_SANITY(rp))
                    139:                return (uint8_t) -1;
                    140:        /* No space left */
                    141:        if ((size_t) (rp->r_next - rp->r_buf) >= rp->r_len)
                    142:                return (uint8_t) -1;
                    143: 
                    144:        u = *rp->r_next;
                    145:        if (n)
                    146:                *rp->r_next = *n;
                    147: 
                    148:        rp->r_next++;
                    149:        return u;
                    150: }
                    151: 
                    152: /*
                    153:  * rpack_uint16() - Pack/Unpack 16bit value
                    154:  *
                    155:  * @rp = raw buffer
                    156:  * @n = set value if !=NULL
                    157:  * return: -1 error or get value
                    158:  */
                    159: uint16_t
                    160: rpack_uint16(rpack_t * __restrict rp, uint16_t * __restrict n)
                    161: {
                    162:        uint16_t u;
                    163:        uint8_t *next;
                    164: 
                    165:        if (!RPACK_SANITY(rp))
                    166:                return (uint16_t) -1;
                    167:        /* No space left */
                    168:        if (!(next = rpack_align_and_reserve(rp, sizeof(uint16_t))))
                    169:                return (uint16_t) -1;
                    170: 
                    171:        u = EXTRACT_LE_16(next);
                    172:        if (n)
                    173:                RPACK_SET_16(next, n);
                    174: 
                    175:        rp->r_next = next + sizeof(uint16_t);
                    176:        return u;
                    177: }
                    178: 
                    179: /*
                    180:  * rpack_uint24() - Pack/Unpack 24bit value
                    181:  *
                    182:  * @rp = raw buffer
                    183:  * @n = set value if !=NULL
                    184:  * return: -1 error or get value
                    185:  */
                    186: uint32_t
                    187: rpack_uint24(rpack_t * __restrict rp, uint32_t * __restrict n)
                    188: {
                    189:        uint32_t u;
                    190:        uint8_t *next;
                    191: 
                    192:        if (!RPACK_SANITY(rp))
                    193:                return (uint32_t) -1;
                    194:        /* No space left */
                    195:        if (!(next = rpack_align_and_reserve(rp, sizeof(uint32_t))))
                    196:                return (uint32_t) -1;
                    197: 
                    198:        u = EXTRACT_LE_24(next);
                    199:        if (n)
                    200:                RPACK_SET_24(next, n);
                    201: 
                    202:        rp->r_next = next + sizeof(uint32_t);
                    203:        return u;
                    204: }
                    205: 
                    206: /*
                    207:  * rpack_uint32() - Pack/Unpack 32bit value
                    208:  *
                    209:  * @rp = raw buffer
                    210:  * @n = set value if !=NULL
                    211:  * return: -1 error or get value
                    212:  */
                    213: uint32_t
                    214: rpack_uint32(rpack_t * __restrict rp, uint32_t * __restrict n)
                    215: {
                    216:        uint32_t u;
                    217:        uint8_t *next;
                    218: 
                    219:        if (!RPACK_SANITY(rp))
                    220:                return (uint32_t) -1;
                    221:        /* No space left */
                    222:        if (!(next = rpack_align_and_reserve(rp, sizeof(uint32_t))))
                    223:                return (uint32_t) -1;
                    224: 
                    225:        u = EXTRACT_LE_32(next);
                    226:        if (n)
                    227:                RPACK_SET_32(next, n);
                    228: 
                    229:        rp->r_next = next + sizeof(uint32_t);
                    230:        return u;
                    231: }
                    232: 
                    233: /*
                    234:  * rpack_uint64() - Pack/Unpack 64bit value
                    235:  *
                    236:  * @rp = raw buffer
                    237:  * @n = set value if !=NULL
                    238:  * return: -1 error or get value
                    239:  */
                    240: uint64_t
                    241: rpack_uint64(rpack_t * __restrict rp, uint64_t * __restrict n)
                    242: {
                    243:        uint64_t u;
                    244:        uint8_t *next;
                    245: 
                    246:        if (!RPACK_SANITY(rp))
                    247:                return (uint64_t) -1;
                    248:        /* No space left */
                    249:        if (!(next = rpack_align_and_reserve(rp, sizeof(uint64_t))))
                    250:                return (uint64_t) -1;
                    251: 
                    252:        u = EXTRACT_LE_64(next);
                    253:        if (n)
                    254:                RPACK_SET_64(next, n);
                    255: 
                    256:        rp->r_next = next + sizeof(uint64_t);
                    257:        return u;
                    258: }

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