omapip/convert.c

Go to the documentation of this file.
00001 /* convert.c
00002 
00003    Safe copying of option values into and out of the option buffer, which
00004    can't be assumed to be aligned. */
00005 
00006 /*
00007  * Copyright (c) 2004,2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC")
00008  * Copyright (c) 1996-2003 by Internet Software Consortium
00009  *
00010  * Permission to use, copy, modify, and distribute this software for any
00011  * purpose with or without fee is hereby granted, provided that the above
00012  * copyright notice and this permission notice appear in all copies.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00015  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00016  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00017  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00018  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00019  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00020  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00021  *
00022  *   Internet Systems Consortium, Inc.
00023  *   950 Charter Street
00024  *   Redwood City, CA 94063
00025  *   <info@isc.org>
00026  *   https://www.isc.org/
00027  *
00028  */
00029 
00030 #include "dhcpd.h"
00031 
00032 #include <omapip/omapip_p.h>
00033 
00034 u_int32_t getULong (buf)
00035         const unsigned char *buf;
00036 {
00037         u_int32_t ibuf;
00038 
00039         memcpy (&ibuf, buf, sizeof (u_int32_t));
00040         return ntohl (ibuf);
00041 }
00042 
00043 int32_t getLong (buf)
00044         const unsigned char *buf;
00045 {
00046         int32_t ibuf;
00047 
00048         memcpy (&ibuf, buf, sizeof (int32_t));
00049         return ntohl (ibuf);
00050 }
00051 
00052 u_int32_t getUShort (buf)
00053         const unsigned char *buf;
00054 {
00055         unsigned short ibuf;
00056 
00057         memcpy (&ibuf, buf, sizeof (u_int16_t));
00058         return ntohs (ibuf);
00059 }
00060 
00061 int32_t getShort (buf)
00062         const unsigned char *buf;
00063 {
00064         short ibuf;
00065 
00066         memcpy (&ibuf, buf, sizeof (int16_t));
00067         return ntohs (ibuf);
00068 }
00069 
00070 void putULong (obuf, val)
00071         unsigned char *obuf;
00072         u_int32_t val;
00073 {
00074         u_int32_t tmp = htonl (val);
00075         memcpy (obuf, &tmp, sizeof tmp);
00076 }
00077 
00078 void putLong (obuf, val)
00079         unsigned char *obuf;
00080         int32_t val;
00081 {
00082         int32_t tmp = htonl (val);
00083         memcpy (obuf, &tmp, sizeof tmp);
00084 }
00085 
00086 void putUShort (obuf, val)
00087         unsigned char *obuf;
00088         u_int32_t val;
00089 {
00090         u_int16_t tmp = htons (val);
00091         memcpy (obuf, &tmp, sizeof tmp);
00092 }
00093 
00094 void putShort (obuf, val)
00095         unsigned char *obuf;
00096         int32_t val;
00097 {
00098         int16_t tmp = htons (val);
00099         memcpy (obuf, &tmp, sizeof tmp);
00100 }
00101 
00102 void putUChar (obuf, val)
00103         unsigned char *obuf;
00104         u_int32_t val;
00105 {
00106         *obuf = val;
00107 }
00108 
00109 u_int32_t getUChar (obuf)
00110         const unsigned char *obuf;
00111 {
00112         return obuf [0];
00113 }
00114 
00115 int converted_length (buf, base, width)
00116         const unsigned char *buf;
00117         unsigned int base;
00118         unsigned int width;
00119 {
00120         u_int32_t number;
00121         u_int32_t column;
00122         int power = 1;
00123         u_int32_t newcolumn = base;
00124 
00125         if (base > 16)
00126                 return 0;
00127 
00128         if (width == 1)
00129                 number = getUChar (buf);
00130         else if (width == 2)
00131                 number = getUShort (buf);
00132         else if (width == 4)
00133                 number = getULong (buf);
00134         else
00135                 return 0;
00136 
00137         do {
00138                 column = newcolumn;
00139 
00140                 if (number < column)
00141                         return power;
00142                 power++;
00143                 newcolumn = column * base;
00144                 /* If we wrap around, it must be the next power of two up. */
00145         } while (newcolumn > column);
00146 
00147         return power;
00148 }
00149 
00150 int binary_to_ascii (outbuf, inbuf, base, width)
00151         unsigned char *outbuf;
00152         const unsigned char *inbuf;
00153         unsigned int base;
00154         unsigned int width;
00155 {
00156         u_int32_t number;
00157         static char h2a [] = "0123456789abcdef";
00158         int power = converted_length (inbuf, base, width);
00159         int i;
00160 
00161         if (base > 16)
00162                 return 0;
00163 
00164         if (width == 1)
00165                 number = getUChar (inbuf);
00166         else if (width == 2)
00167                 number = getUShort (inbuf);
00168         else if (width == 4)
00169                 number = getULong (inbuf);
00170         else
00171                 return 0;
00172 
00173         for (i = power - 1 ; i >= 0; i--) {
00174                 outbuf [i] = h2a [number % base];
00175                 number /= base;
00176         }
00177 
00178         return power;
00179 }

Generated on 5 Apr 2014 for ISC DHCP by  doxygen 1.6.1