147c08596SBrooks Davis /* $OpenBSD: convert.c,v 1.5 2004/02/07 11:35:59 henning Exp $ */ 247c08596SBrooks Davis 347c08596SBrooks Davis /* 447c08596SBrooks Davis * Safe copying of option values into and out of the option buffer, 547c08596SBrooks Davis * which can't be assumed to be aligned. 647c08596SBrooks Davis */ 747c08596SBrooks Davis 8*8a16b7a1SPedro F. Giffuni /*- 9*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 10*8a16b7a1SPedro F. Giffuni * 1147c08596SBrooks Davis * Copyright (c) 1995, 1996 The Internet Software Consortium. 1247c08596SBrooks Davis * All rights reserved. 1347c08596SBrooks Davis * 1447c08596SBrooks Davis * Redistribution and use in source and binary forms, with or without 1547c08596SBrooks Davis * modification, are permitted provided that the following conditions 1647c08596SBrooks Davis * are met: 1747c08596SBrooks Davis * 1847c08596SBrooks Davis * 1. Redistributions of source code must retain the above copyright 1947c08596SBrooks Davis * notice, this list of conditions and the following disclaimer. 2047c08596SBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright 2147c08596SBrooks Davis * notice, this list of conditions and the following disclaimer in the 2247c08596SBrooks Davis * documentation and/or other materials provided with the distribution. 2347c08596SBrooks Davis * 3. Neither the name of The Internet Software Consortium nor the names 2447c08596SBrooks Davis * of its contributors may be used to endorse or promote products derived 2547c08596SBrooks Davis * from this software without specific prior written permission. 2647c08596SBrooks Davis * 2747c08596SBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 2847c08596SBrooks Davis * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 2947c08596SBrooks Davis * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 3047c08596SBrooks Davis * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 3147c08596SBrooks Davis * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 3247c08596SBrooks Davis * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3347c08596SBrooks Davis * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3447c08596SBrooks Davis * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 3547c08596SBrooks Davis * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 3647c08596SBrooks Davis * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 3747c08596SBrooks Davis * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 3847c08596SBrooks Davis * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3947c08596SBrooks Davis * SUCH DAMAGE. 4047c08596SBrooks Davis * 4147c08596SBrooks Davis * This software has been written for the Internet Software Consortium 4247c08596SBrooks Davis * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 4347c08596SBrooks Davis * Enterprises. To learn more about the Internet Software Consortium, 4447c08596SBrooks Davis * see ``http://www.vix.com/isc''. To learn more about Vixie 4547c08596SBrooks Davis * Enterprises, see ``http://www.vix.com''. 4647c08596SBrooks Davis */ 4747c08596SBrooks Davis 488794fdbbSBrooks Davis #include <sys/cdefs.h> 498794fdbbSBrooks Davis __FBSDID("$FreeBSD$"); 508794fdbbSBrooks Davis 5147c08596SBrooks Davis #include "dhcpd.h" 5247c08596SBrooks Davis 5347c08596SBrooks Davis u_int32_t 5447c08596SBrooks Davis getULong(unsigned char *buf) 5547c08596SBrooks Davis { 5647c08596SBrooks Davis u_int32_t ibuf; 5747c08596SBrooks Davis 5847c08596SBrooks Davis memcpy(&ibuf, buf, sizeof(ibuf)); 5947c08596SBrooks Davis return (ntohl(ibuf)); 6047c08596SBrooks Davis } 6147c08596SBrooks Davis 6247c08596SBrooks Davis int32_t 6347c08596SBrooks Davis getLong(unsigned char *(buf)) 6447c08596SBrooks Davis { 6547c08596SBrooks Davis int32_t ibuf; 6647c08596SBrooks Davis 6747c08596SBrooks Davis memcpy(&ibuf, buf, sizeof(ibuf)); 6847c08596SBrooks Davis return (ntohl(ibuf)); 6947c08596SBrooks Davis } 7047c08596SBrooks Davis 7147c08596SBrooks Davis u_int16_t 7247c08596SBrooks Davis getUShort(unsigned char *buf) 7347c08596SBrooks Davis { 7447c08596SBrooks Davis u_int16_t ibuf; 7547c08596SBrooks Davis 7647c08596SBrooks Davis memcpy(&ibuf, buf, sizeof(ibuf)); 7747c08596SBrooks Davis return (ntohs(ibuf)); 7847c08596SBrooks Davis } 7947c08596SBrooks Davis 8047c08596SBrooks Davis int16_t 8147c08596SBrooks Davis getShort(unsigned char *buf) 8247c08596SBrooks Davis { 8347c08596SBrooks Davis int16_t ibuf; 8447c08596SBrooks Davis 8547c08596SBrooks Davis memcpy(&ibuf, buf, sizeof(ibuf)); 8647c08596SBrooks Davis return (ntohs(ibuf)); 8747c08596SBrooks Davis } 8847c08596SBrooks Davis 8947c08596SBrooks Davis void 9047c08596SBrooks Davis putULong(unsigned char *obuf, u_int32_t val) 9147c08596SBrooks Davis { 9247c08596SBrooks Davis u_int32_t tmp = htonl(val); 9347c08596SBrooks Davis 9447c08596SBrooks Davis memcpy(obuf, &tmp, sizeof(tmp)); 9547c08596SBrooks Davis } 9647c08596SBrooks Davis 9747c08596SBrooks Davis void 9847c08596SBrooks Davis putLong(unsigned char *obuf, int32_t val) 9947c08596SBrooks Davis { 10047c08596SBrooks Davis int32_t tmp = htonl(val); 10147c08596SBrooks Davis 10247c08596SBrooks Davis memcpy(obuf, &tmp, sizeof(tmp)); 10347c08596SBrooks Davis } 10447c08596SBrooks Davis 10547c08596SBrooks Davis void 10647c08596SBrooks Davis putUShort(unsigned char *obuf, unsigned int val) 10747c08596SBrooks Davis { 10847c08596SBrooks Davis u_int16_t tmp = htons(val); 10947c08596SBrooks Davis 11047c08596SBrooks Davis memcpy(obuf, &tmp, sizeof(tmp)); 11147c08596SBrooks Davis } 11247c08596SBrooks Davis 11347c08596SBrooks Davis void 11447c08596SBrooks Davis putShort(unsigned char *obuf, int val) 11547c08596SBrooks Davis { 11647c08596SBrooks Davis int16_t tmp = htons(val); 11747c08596SBrooks Davis 11847c08596SBrooks Davis memcpy(obuf, &tmp, sizeof(tmp)); 11947c08596SBrooks Davis } 120