1147072Sbrooks/*	$OpenBSD: convert.c,v 1.5 2004/02/07 11:35:59 henning Exp $	*/
2147072Sbrooks
3147072Sbrooks/*
4147072Sbrooks * Safe copying of option values into and out of the option buffer,
5147072Sbrooks * which can't be assumed to be aligned.
6147072Sbrooks */
7147072Sbrooks
8147072Sbrooks/*
9147072Sbrooks * Copyright (c) 1995, 1996 The Internet Software Consortium.
10147072Sbrooks * All rights reserved.
11147072Sbrooks *
12147072Sbrooks * Redistribution and use in source and binary forms, with or without
13147072Sbrooks * modification, are permitted provided that the following conditions
14147072Sbrooks * are met:
15147072Sbrooks *
16147072Sbrooks * 1. Redistributions of source code must retain the above copyright
17147072Sbrooks *    notice, this list of conditions and the following disclaimer.
18147072Sbrooks * 2. Redistributions in binary form must reproduce the above copyright
19147072Sbrooks *    notice, this list of conditions and the following disclaimer in the
20147072Sbrooks *    documentation and/or other materials provided with the distribution.
21147072Sbrooks * 3. Neither the name of The Internet Software Consortium nor the names
22147072Sbrooks *    of its contributors may be used to endorse or promote products derived
23147072Sbrooks *    from this software without specific prior written permission.
24147072Sbrooks *
25147072Sbrooks * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
26147072Sbrooks * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27147072Sbrooks * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28147072Sbrooks * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29147072Sbrooks * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
30147072Sbrooks * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31147072Sbrooks * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32147072Sbrooks * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
33147072Sbrooks * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34147072Sbrooks * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35147072Sbrooks * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36147072Sbrooks * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37147072Sbrooks * SUCH DAMAGE.
38147072Sbrooks *
39147072Sbrooks * This software has been written for the Internet Software Consortium
40147072Sbrooks * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
41147072Sbrooks * Enterprises.  To learn more about the Internet Software Consortium,
42147072Sbrooks * see ``http://www.vix.com/isc''.  To learn more about Vixie
43147072Sbrooks * Enterprises, see ``http://www.vix.com''.
44147072Sbrooks */
45147072Sbrooks
46149399Sbrooks#include <sys/cdefs.h>
47149399Sbrooks__FBSDID("$FreeBSD$");
48149399Sbrooks
49147072Sbrooks#include "dhcpd.h"
50147072Sbrooks
51147072Sbrooksu_int32_t
52147072SbrooksgetULong(unsigned char *buf)
53147072Sbrooks{
54147072Sbrooks	u_int32_t ibuf;
55147072Sbrooks
56147072Sbrooks	memcpy(&ibuf, buf, sizeof(ibuf));
57147072Sbrooks	return (ntohl(ibuf));
58147072Sbrooks}
59147072Sbrooks
60147072Sbrooksint32_t
61147072SbrooksgetLong(unsigned char *(buf))
62147072Sbrooks{
63147072Sbrooks	int32_t ibuf;
64147072Sbrooks
65147072Sbrooks	memcpy(&ibuf, buf, sizeof(ibuf));
66147072Sbrooks	return (ntohl(ibuf));
67147072Sbrooks}
68147072Sbrooks
69147072Sbrooksu_int16_t
70147072SbrooksgetUShort(unsigned char *buf)
71147072Sbrooks{
72147072Sbrooks	u_int16_t ibuf;
73147072Sbrooks
74147072Sbrooks	memcpy(&ibuf, buf, sizeof(ibuf));
75147072Sbrooks	return (ntohs(ibuf));
76147072Sbrooks}
77147072Sbrooks
78147072Sbrooksint16_t
79147072SbrooksgetShort(unsigned char *buf)
80147072Sbrooks{
81147072Sbrooks	int16_t ibuf;
82147072Sbrooks
83147072Sbrooks	memcpy(&ibuf, buf, sizeof(ibuf));
84147072Sbrooks	return (ntohs(ibuf));
85147072Sbrooks}
86147072Sbrooks
87147072Sbrooksvoid
88147072SbrooksputULong(unsigned char *obuf, u_int32_t val)
89147072Sbrooks{
90147072Sbrooks	u_int32_t tmp = htonl(val);
91147072Sbrooks
92147072Sbrooks	memcpy(obuf, &tmp, sizeof(tmp));
93147072Sbrooks}
94147072Sbrooks
95147072Sbrooksvoid
96147072SbrooksputLong(unsigned char *obuf, int32_t val)
97147072Sbrooks{
98147072Sbrooks	int32_t tmp = htonl(val);
99147072Sbrooks
100147072Sbrooks	memcpy(obuf, &tmp, sizeof(tmp));
101147072Sbrooks}
102147072Sbrooks
103147072Sbrooksvoid
104147072SbrooksputUShort(unsigned char *obuf, unsigned int val)
105147072Sbrooks{
106147072Sbrooks	u_int16_t tmp = htons(val);
107147072Sbrooks
108147072Sbrooks	memcpy(obuf, &tmp, sizeof(tmp));
109147072Sbrooks}
110147072Sbrooks
111147072Sbrooksvoid
112147072SbrooksputShort(unsigned char *obuf, int val)
113147072Sbrooks{
114147072Sbrooks	int16_t tmp = htons(val);
115147072Sbrooks
116147072Sbrooks	memcpy(obuf, &tmp, sizeof(tmp));
117147072Sbrooks}
118