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