1995-11-29 07:40:04 +00:00
|
|
|
/* convert.c
|
|
|
|
|
|
|
|
Safe copying of option values into and out of the option buffer, which
|
|
|
|
can't be assumed to be aligned. */
|
|
|
|
|
|
|
|
/*
|
2017-07-12 09:23:23 -04:00
|
|
|
* Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
|
2005-03-17 20:15:29 +00:00
|
|
|
* Copyright (c) 1996-2003 by Internet Software Consortium
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
2017-07-12 09:23:23 -04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* Internet Systems Consortium, Inc.
|
2022-01-19 20:13:19 +01:00
|
|
|
* PO Box 360
|
|
|
|
* Newmarket, NH 03857 USA
|
2005-03-17 20:15:29 +00:00
|
|
|
* <info@isc.org>
|
2009-07-23 18:52:21 +00:00
|
|
|
* https://www.isc.org/
|
2000-03-17 04:00:32 +00:00
|
|
|
*
|
1995-11-29 07:40:04 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-19 18:47:15 +00:00
|
|
|
#include "dhcpd.h"
|
|
|
|
|
2000-08-01 22:34:36 +00:00
|
|
|
#include <omapip/omapip_p.h>
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-20 00:00:48 +00:00
|
|
|
u_int32_t getULong (buf)
|
1999-10-07 06:36:35 +00:00
|
|
|
const unsigned char *buf;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
2005-03-17 20:15:29 +00:00
|
|
|
u_int32_t ibuf;
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-20 00:00:48 +00:00
|
|
|
memcpy (&ibuf, buf, sizeof (u_int32_t));
|
1995-11-29 07:40:04 +00:00
|
|
|
return ntohl (ibuf);
|
|
|
|
}
|
|
|
|
|
1996-05-20 00:00:48 +00:00
|
|
|
int32_t getLong (buf)
|
1999-10-07 06:36:35 +00:00
|
|
|
const unsigned char *buf;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
2005-03-17 20:15:29 +00:00
|
|
|
int32_t ibuf;
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-20 00:00:48 +00:00
|
|
|
memcpy (&ibuf, buf, sizeof (int32_t));
|
1995-11-29 07:40:04 +00:00
|
|
|
return ntohl (ibuf);
|
|
|
|
}
|
|
|
|
|
1999-04-05 15:33:52 +00:00
|
|
|
u_int32_t getUShort (buf)
|
1999-10-07 06:36:35 +00:00
|
|
|
const unsigned char *buf;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
|
|
|
unsigned short ibuf;
|
|
|
|
|
1996-05-20 00:00:48 +00:00
|
|
|
memcpy (&ibuf, buf, sizeof (u_int16_t));
|
1995-11-29 07:40:04 +00:00
|
|
|
return ntohs (ibuf);
|
|
|
|
}
|
|
|
|
|
1999-04-05 15:33:52 +00:00
|
|
|
int32_t getShort (buf)
|
1999-10-07 06:36:35 +00:00
|
|
|
const unsigned char *buf;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
|
|
|
short ibuf;
|
|
|
|
|
1996-05-20 00:00:48 +00:00
|
|
|
memcpy (&ibuf, buf, sizeof (int16_t));
|
1995-11-29 07:40:04 +00:00
|
|
|
return ntohs (ibuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void putULong (obuf, val)
|
|
|
|
unsigned char *obuf;
|
1996-05-20 00:00:48 +00:00
|
|
|
u_int32_t val;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
1996-05-20 00:00:48 +00:00
|
|
|
u_int32_t tmp = htonl (val);
|
1995-11-29 07:40:04 +00:00
|
|
|
memcpy (obuf, &tmp, sizeof tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void putLong (obuf, val)
|
|
|
|
unsigned char *obuf;
|
1996-05-20 00:00:48 +00:00
|
|
|
int32_t val;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
1996-05-20 00:00:48 +00:00
|
|
|
int32_t tmp = htonl (val);
|
1995-11-29 07:40:04 +00:00
|
|
|
memcpy (obuf, &tmp, sizeof tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void putUShort (obuf, val)
|
|
|
|
unsigned char *obuf;
|
1998-03-16 06:16:54 +00:00
|
|
|
u_int32_t val;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
1997-05-09 07:58:33 +00:00
|
|
|
u_int16_t tmp = htons (val);
|
1995-11-29 07:40:04 +00:00
|
|
|
memcpy (obuf, &tmp, sizeof tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void putShort (obuf, val)
|
|
|
|
unsigned char *obuf;
|
1998-03-16 06:16:54 +00:00
|
|
|
int32_t val;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
1997-05-09 07:58:33 +00:00
|
|
|
int16_t tmp = htons (val);
|
1995-11-29 07:40:04 +00:00
|
|
|
memcpy (obuf, &tmp, sizeof tmp);
|
|
|
|
}
|
|
|
|
|
1999-04-05 15:33:52 +00:00
|
|
|
void putUChar (obuf, val)
|
|
|
|
unsigned char *obuf;
|
|
|
|
u_int32_t val;
|
|
|
|
{
|
|
|
|
*obuf = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t getUChar (obuf)
|
1999-10-07 06:36:35 +00:00
|
|
|
const unsigned char *obuf;
|
1999-04-05 15:33:52 +00:00
|
|
|
{
|
|
|
|
return obuf [0];
|
|
|
|
}
|
1999-07-02 20:58:48 +00:00
|
|
|
|
|
|
|
int converted_length (buf, base, width)
|
1999-10-07 06:36:35 +00:00
|
|
|
const unsigned char *buf;
|
1999-07-02 20:58:48 +00:00
|
|
|
unsigned int base;
|
|
|
|
unsigned int width;
|
|
|
|
{
|
|
|
|
u_int32_t number;
|
1999-07-31 17:54:06 +00:00
|
|
|
u_int32_t column;
|
1999-07-02 20:58:48 +00:00
|
|
|
int power = 1;
|
1999-07-31 17:54:06 +00:00
|
|
|
u_int32_t newcolumn = base;
|
1999-07-02 20:58:48 +00:00
|
|
|
|
|
|
|
if (base > 16)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (width == 1)
|
|
|
|
number = getUChar (buf);
|
|
|
|
else if (width == 2)
|
|
|
|
number = getUShort (buf);
|
|
|
|
else if (width == 4)
|
|
|
|
number = getULong (buf);
|
2005-03-17 20:15:29 +00:00
|
|
|
else
|
|
|
|
return 0;
|
1999-07-02 20:58:48 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
column = newcolumn;
|
|
|
|
|
|
|
|
if (number < column)
|
|
|
|
return power;
|
|
|
|
power++;
|
|
|
|
newcolumn = column * base;
|
|
|
|
/* If we wrap around, it must be the next power of two up. */
|
1999-07-31 17:54:06 +00:00
|
|
|
} while (newcolumn > column);
|
1999-07-02 20:58:48 +00:00
|
|
|
|
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
|
|
|
int binary_to_ascii (outbuf, inbuf, base, width)
|
|
|
|
unsigned char *outbuf;
|
1999-10-07 06:36:35 +00:00
|
|
|
const unsigned char *inbuf;
|
1999-07-02 20:58:48 +00:00
|
|
|
unsigned int base;
|
|
|
|
unsigned int width;
|
|
|
|
{
|
|
|
|
u_int32_t number;
|
|
|
|
static char h2a [] = "0123456789abcdef";
|
1999-07-31 17:54:06 +00:00
|
|
|
int power = converted_length (inbuf, base, width);
|
2007-07-13 06:43:43 +00:00
|
|
|
int i;
|
1999-07-02 20:58:48 +00:00
|
|
|
|
|
|
|
if (base > 16)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (width == 1)
|
|
|
|
number = getUChar (inbuf);
|
|
|
|
else if (width == 2)
|
|
|
|
number = getUShort (inbuf);
|
|
|
|
else if (width == 4)
|
|
|
|
number = getULong (inbuf);
|
2005-03-17 20:15:29 +00:00
|
|
|
else
|
|
|
|
return 0;
|
1999-07-02 20:58:48 +00:00
|
|
|
|
1999-07-31 17:54:06 +00:00
|
|
|
for (i = power - 1 ; i >= 0; i--) {
|
1999-07-02 20:58:48 +00:00
|
|
|
outbuf [i] = h2a [number % base];
|
|
|
|
number /= base;
|
|
|
|
}
|
1999-07-06 16:51:19 +00:00
|
|
|
|
1999-07-02 20:58:48 +00:00
|
|
|
return power;
|
|
|
|
}
|