Annotation of embedaddon/dhcp/omapip/convert.c, revision 1.1.1.1

1.1       misho       1: /* convert.c
                      2: 
                      3:    Safe copying of option values into and out of the option buffer, which
                      4:    can't be assumed to be aligned. */
                      5: 
                      6: /*
                      7:  * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
                      8:  * Copyright (c) 1996-2003 by Internet Software Consortium
                      9:  *
                     10:  * Permission to use, copy, modify, and distribute this software for any
                     11:  * purpose with or without fee is hereby granted, provided that the above
                     12:  * copyright notice and this permission notice appear in all copies.
                     13:  *
                     14:  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
                     15:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     16:  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
                     17:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     18:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     19:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
                     20:  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     21:  *
                     22:  *   Internet Systems Consortium, Inc.
                     23:  *   950 Charter Street
                     24:  *   Redwood City, CA 94063
                     25:  *   <info@isc.org>
                     26:  *   https://www.isc.org/
                     27:  *
                     28:  * This software has been written for Internet Systems Consortium
                     29:  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
                     30:  * To learn more about Internet Systems Consortium, see
                     31:  * ``https://www.isc.org/''.  To learn more about Vixie Enterprises,
                     32:  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
                     33:  * ``http://www.nominum.com''.
                     34:  */
                     35: 
                     36: #include "dhcpd.h"
                     37: 
                     38: #include <omapip/omapip_p.h>
                     39: 
                     40: u_int32_t getULong (buf)
                     41:        const unsigned char *buf;
                     42: {
                     43:        u_int32_t ibuf;
                     44: 
                     45:        memcpy (&ibuf, buf, sizeof (u_int32_t));
                     46:        return ntohl (ibuf);
                     47: }
                     48: 
                     49: int32_t getLong (buf)
                     50:        const unsigned char *buf;
                     51: {
                     52:        int32_t ibuf;
                     53: 
                     54:        memcpy (&ibuf, buf, sizeof (int32_t));
                     55:        return ntohl (ibuf);
                     56: }
                     57: 
                     58: u_int32_t getUShort (buf)
                     59:        const unsigned char *buf;
                     60: {
                     61:        unsigned short ibuf;
                     62: 
                     63:        memcpy (&ibuf, buf, sizeof (u_int16_t));
                     64:        return ntohs (ibuf);
                     65: }
                     66: 
                     67: int32_t getShort (buf)
                     68:        const unsigned char *buf;
                     69: {
                     70:        short ibuf;
                     71: 
                     72:        memcpy (&ibuf, buf, sizeof (int16_t));
                     73:        return ntohs (ibuf);
                     74: }
                     75: 
                     76: void putULong (obuf, val)
                     77:        unsigned char *obuf;
                     78:        u_int32_t val;
                     79: {
                     80:        u_int32_t tmp = htonl (val);
                     81:        memcpy (obuf, &tmp, sizeof tmp);
                     82: }
                     83: 
                     84: void putLong (obuf, val)
                     85:        unsigned char *obuf;
                     86:        int32_t val;
                     87: {
                     88:        int32_t tmp = htonl (val);
                     89:        memcpy (obuf, &tmp, sizeof tmp);
                     90: }
                     91: 
                     92: void putUShort (obuf, val)
                     93:        unsigned char *obuf;
                     94:        u_int32_t val;
                     95: {
                     96:        u_int16_t tmp = htons (val);
                     97:        memcpy (obuf, &tmp, sizeof tmp);
                     98: }
                     99: 
                    100: void putShort (obuf, val)
                    101:        unsigned char *obuf;
                    102:        int32_t val;
                    103: {
                    104:        int16_t tmp = htons (val);
                    105:        memcpy (obuf, &tmp, sizeof tmp);
                    106: }
                    107: 
                    108: void putUChar (obuf, val)
                    109:        unsigned char *obuf;
                    110:        u_int32_t val;
                    111: {
                    112:        *obuf = val;
                    113: }
                    114: 
                    115: u_int32_t getUChar (obuf)
                    116:        const unsigned char *obuf;
                    117: {
                    118:        return obuf [0];
                    119: }
                    120: 
                    121: int converted_length (buf, base, width)
                    122:        const unsigned char *buf;
                    123:        unsigned int base;
                    124:        unsigned int width;
                    125: {
                    126:        u_int32_t number;
                    127:        u_int32_t column;
                    128:        int power = 1;
                    129:        u_int32_t newcolumn = base;
                    130: 
                    131:        if (base > 16)
                    132:                return 0;
                    133: 
                    134:        if (width == 1)
                    135:                number = getUChar (buf);
                    136:        else if (width == 2)
                    137:                number = getUShort (buf);
                    138:        else if (width == 4)
                    139:                number = getULong (buf);
                    140:        else
                    141:                return 0;
                    142: 
                    143:        do {
                    144:                column = newcolumn;
                    145: 
                    146:                if (number < column)
                    147:                        return power;
                    148:                power++;
                    149:                newcolumn = column * base;
                    150:                /* If we wrap around, it must be the next power of two up. */
                    151:        } while (newcolumn > column);
                    152: 
                    153:        return power;
                    154: }
                    155: 
                    156: int binary_to_ascii (outbuf, inbuf, base, width)
                    157:        unsigned char *outbuf;
                    158:        const unsigned char *inbuf;
                    159:        unsigned int base;
                    160:        unsigned int width;
                    161: {
                    162:        u_int32_t number;
                    163:        static char h2a [] = "0123456789abcdef";
                    164:        int power = converted_length (inbuf, base, width);
                    165:        int i;
                    166: 
                    167:        if (base > 16)
                    168:                return 0;
                    169: 
                    170:        if (width == 1)
                    171:                number = getUChar (inbuf);
                    172:        else if (width == 2)
                    173:                number = getUShort (inbuf);
                    174:        else if (width == 4)
                    175:                number = getULong (inbuf);
                    176:        else
                    177:                return 0;
                    178: 
                    179:        for (i = power - 1 ; i >= 0; i--) {
                    180:                outbuf [i] = h2a [number % base];
                    181:                number /= base;
                    182:        }
                    183: 
                    184:        return power;
                    185: }

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