Annotation of embedaddon/mpd/src/contrib/libpdel/structs/structs_generic.c, revision 1.1.1.2

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #include <sys/types.h>
                     42: #ifdef __linux__
                     43: #include <endian.h>
                     44: #else
                     45: #include <machine/endian.h>
                     46: #endif
                     47: 
                     48: #include <stdio.h>
                     49: #include <stdlib.h>
                     50: #include <stdarg.h>
                     51: #include <string.h>
                     52: #include <errno.h>
                     53: 
                     54: #include "structs/structs.h"
                     55: #include "structs/type/array.h"
                     56: #include "util/typed_mem.h"
                     57: 
                     58: #ifndef BYTE_ORDER
                     59: #error BYTE_ORDER is undefined
                     60: #endif
                     61: 
                     62: /*********************************************************************
                     63:                        GENERIC FUNCTIONS
                     64: *********************************************************************/
                     65: 
                     66: int
                     67: structs_region_init(const struct structs_type *type, void *data)
                     68: {
                     69:        memset(data, 0, type->size);
                     70:        return (0);
                     71: }
                     72: 
                     73: int
                     74: structs_region_copy(const struct structs_type *type, const void *from, void *to)
                     75: {
                     76:        memcpy(to, from, type->size);
                     77:        return (0);
                     78: }
                     79: 
                     80: int
                     81: structs_region_equal(const struct structs_type *type,
                     82:        const void *v1, const void *v2)
                     83: {
                     84:        return (memcmp(v1, v2, type->size) == 0);
                     85: }
                     86: 
                     87: int
                     88: structs_region_encode(const struct structs_type *type, const char *mtype,
                     89:        struct structs_data *code, const void *data)
                     90: {
                     91:        if ((code->data = MALLOC(mtype, type->size)) == NULL)
                     92:                return (-1);
                     93:        memcpy(code->data, data, type->size);
                     94:        code->length = type->size;
                     95:        return (0);
                     96: }
                     97: 
                     98: int
                     99: structs_region_decode(const struct structs_type *type,
                    100:        const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
                    101: {
                    102:        if (cmax < type->size) {
                    103:                strlcpy(ebuf, "encoded data is truncated", emax);
                    104:                errno = EINVAL;
                    105:                return (-1);
                    106:        }
                    107:        memcpy(data, code, type->size);
                    108:        return (type->size);
                    109: }
                    110: 
                    111: int
                    112: structs_region_encode_netorder(const struct structs_type *type,
                    113:        const char *mtype, struct structs_data *code, const void *data)
                    114: {
                    115:        if (structs_region_encode(type, mtype, code, data) == -1)
                    116:                return (-1);
                    117: #if BYTE_ORDER == LITTLE_ENDIAN
                    118:     {
                    119:        u_char temp;
                    120:        u_int i;
                    121: 
                    122:        for (i = 0; i < code->length / 2; i++) {
                    123:                temp = code->data[i];
                    124:                code->data[i] = code->data[code->length - 1 - i];
                    125:                code->data[code->length - 1 - i] = temp;
                    126:        }
                    127:     }
                    128: #endif
                    129:        return (0);
                    130: }
                    131: 
                    132: int
                    133: structs_region_decode_netorder(const struct structs_type *type,
                    134:        const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
                    135: {
                    136: #if BYTE_ORDER == LITTLE_ENDIAN
                    137:        u_char buf[16];
                    138:        u_char temp;
                    139:        u_int i;
                    140: 
                    141:        if (type->size > sizeof(buf)) {
                    142:                errno = ERANGE;         /* XXX oops fixed buffer is too small */
                    143:                return (-1);
                    144:        }
                    145:        if (cmax > type->size)
                    146:                cmax = type->size;
                    147:        memcpy(buf, code, cmax);
                    148:        for (i = 0; i < type->size / 2; i++) {
                    149:                temp = buf[i];
                    150:                buf[i] = buf[type->size - 1 - i];
                    151:                buf[type->size - 1 - i] = temp;
                    152:        }
                    153:        code = buf;
                    154: #endif
                    155:        return (structs_region_decode(type, code, cmax, data, ebuf, emax));
                    156: }
                    157: 
                    158: char *
                    159: structs_notsupp_ascify(const struct structs_type *type,
                    160:        const char *mtype, const void *data)
                    161: {
1.1.1.2 ! misho     162:        (void)type;
        !           163:        (void)mtype;
        !           164:        (void)data;
        !           165: 
1.1       misho     166:        errno = EOPNOTSUPP;
                    167:        return (NULL);
                    168: }
                    169: 
                    170: int
                    171: structs_notsupp_init(const struct structs_type *type, void *data)
                    172: {
1.1.1.2 ! misho     173:        (void)type;
        !           174:        (void)data;
        !           175: 
1.1       misho     176:        errno = EOPNOTSUPP;
                    177:        return (-1);
                    178: }
                    179: 
                    180: int
                    181: structs_notsupp_copy(const struct structs_type *type,
                    182:        const void *from, void *to)
                    183: {
1.1.1.2 ! misho     184:        (void)type;
        !           185:        (void)from;
        !           186:        (void)to;
        !           187: 
1.1       misho     188:        errno = EOPNOTSUPP;
                    189:        return (-1);
                    190: }
                    191: 
                    192: int
                    193: structs_notsupp_equal(const struct structs_type *type,
                    194:        const void *v1, const void *v2)
                    195: {
1.1.1.2 ! misho     196:        (void)type;
        !           197:        (void)v1;
        !           198:        (void)v2;
        !           199: 
1.1       misho     200:        errno = EOPNOTSUPP;
                    201:        return (-1);
                    202: }
                    203: 
                    204: 
                    205: int
                    206: structs_notsupp_binify(const struct structs_type *type,
                    207:        const char *ascii, void *data, char *ebuf, size_t emax)
                    208: {
1.1.1.2 ! misho     209:        (void)type;
        !           210:        (void)ascii;
        !           211:        (void)data;
        !           212: 
1.1       misho     213:        strlcpy(ebuf,
                    214:            "parsing from ASCII is not supported by this structs type", emax);
                    215:        errno = EOPNOTSUPP;
                    216:        return (-1);
                    217: }
                    218: 
                    219: int
                    220: structs_notsupp_encode(const struct structs_type *type, const char *mtype,
                    221:        struct structs_data *code, const void *data)
                    222: {
1.1.1.2 ! misho     223:        (void)type;
        !           224:        (void)mtype;
        !           225:        (void)code;
        !           226:        (void)data;
        !           227: 
1.1       misho     228:        errno = EOPNOTSUPP;
                    229:        return (-1);
                    230: }
                    231: 
                    232: int
                    233: structs_notsupp_decode(const struct structs_type *type,
                    234:        const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
                    235: {
1.1.1.2 ! misho     236:        (void)type;
        !           237:        (void)code;
        !           238:        (void)cmax;
        !           239:        (void)data;
        !           240: 
1.1       misho     241:        strlcpy(ebuf,
                    242:            "binary decoding is not supported by this structs type", emax);
                    243:        errno = EOPNOTSUPP;
                    244:        return (-1);
                    245: }
                    246: 
                    247: void
                    248: structs_nothing_free(const struct structs_type *type, void *data)
                    249: {
1.1.1.2 ! misho     250:        (void)type;
        !           251:        (void)data;
        !           252: 
1.1       misho     253:        return;
                    254: }
                    255: 
                    256: int
                    257: structs_ascii_copy(const struct structs_type *type, const void *from, void *to)
                    258: {
                    259:        char *ascii;
                    260:        int rtn;
                    261: 
                    262:        if ((ascii = (*type->ascify)(type, TYPED_MEM_TEMP, from)) == NULL)
                    263:                return (-1);
                    264:        rtn = (*type->binify)(type, ascii, to, NULL, 0);
                    265:        FREE(TYPED_MEM_TEMP, ascii);
                    266:        return (rtn);
                    267: }
                    268: 

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