Annotation of embedaddon/libpdel/structs/type/structs_type_id.c, revision 1.1

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: 
        !            43: #include <stdio.h>
        !            44: #include <stdlib.h>
        !            45: #include <stdarg.h>
        !            46: #include <string.h>
        !            47: #include <assert.h>
        !            48: #include <errno.h>
        !            49: 
        !            50: #include "structs/structs.h"
        !            51: #include "structs/type/array.h"
        !            52: #include "structs/type/id.h"
        !            53: #include "util/typed_mem.h"
        !            54: 
        !            55: /*********************************************************************
        !            56:                        IDENTIFIER TYPES
        !            57: *********************************************************************/
        !            58: 
        !            59: int
        !            60: structs_id_init(const struct structs_type *type, void *data)
        !            61: {
        !            62:        const struct structs_id *const ids = type->args[0].v;
        !            63: 
        !            64:        switch (type->size) {
        !            65:        case 1:
        !            66:            {
        !            67:                const u_int8_t value = (u_int8_t)ids[0].value;
        !            68: 
        !            69:                memcpy(data, &value, type->size);
        !            70:                break;
        !            71:            }
        !            72:        case 2:
        !            73:            {
        !            74:                const u_int16_t value = (u_int16_t)ids[0].value;
        !            75: 
        !            76:                memcpy(data, &value, type->size);
        !            77:                break;
        !            78:            }
        !            79:        case 4:
        !            80:            {
        !            81:                const u_int32_t value = (u_int32_t)ids[0].value;
        !            82: 
        !            83:                memcpy(data, &value, type->size);
        !            84:                break;
        !            85:            }
        !            86:        default:
        !            87:                assert(0);
        !            88:        }
        !            89:        return (0);
        !            90: }
        !            91: 
        !            92: char *
        !            93: structs_id_ascify(const struct structs_type *type,
        !            94:        const char *mtype, const void *data)
        !            95: {
        !            96:        const struct structs_id *id;
        !            97:        u_int32_t value = 0;
        !            98: 
        !            99:        switch (type->size) {
        !           100:        case 1:
        !           101:                value = *((u_int8_t *)data);
        !           102:                break;
        !           103:        case 2:
        !           104:                value = *((u_int16_t *)data);
        !           105:                break;
        !           106:        case 4:
        !           107:                value = *((u_int32_t *)data);
        !           108:                break;
        !           109:        default:
        !           110:                assert(0);
        !           111:        }
        !           112:        for (id = type->args[0].v; id->id != NULL; id++) {
        !           113:                if (value == id->value)
        !           114:                        return (STRDUP(mtype, id->id));
        !           115:        }
        !           116:        return (STRDUP(mtype, "INVALID"));
        !           117: }
        !           118: 
        !           119: int
        !           120: structs_id_binify(const struct structs_type *type,
        !           121:        const char *ascii, void *data, char *ebuf, size_t emax)
        !           122: {
        !           123:        const struct structs_id *id;
        !           124: 
        !           125:        for (id = type->args[0].v; id->id != NULL; id++) {
        !           126:                int (*const cmp)(const char *, const char *)
        !           127:                    = id->imatch ? strcasecmp : strcmp;
        !           128: 
        !           129:                if ((*cmp)(ascii, id->id) == 0) {
        !           130:                        switch (type->size) {
        !           131:                        case 1:
        !           132:                                *((u_int8_t *)data) = id->value;
        !           133:                                break;
        !           134:                        case 2:
        !           135:                                *((u_int16_t *)data) = id->value;
        !           136:                                break;
        !           137:                        case 4:
        !           138:                                *((u_int32_t *)data) = id->value;
        !           139:                                break;
        !           140:                        default:
        !           141:                                assert(0);
        !           142:                        }
        !           143:                        return (0);
        !           144:                }
        !           145:        }
        !           146:        snprintf(ebuf, emax, "invalid value \"%s\"", ascii);
        !           147:        errno = EINVAL;
        !           148:        return (-1);
        !           149: }
        !           150: 

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