1181834Sroberto/* 2181834Sroberto * /src/NTP/ntp4-dev/libntp/binio.c,v 4.5 2005/04/16 17:32:10 kardel RELEASE_20050508_A 3181834Sroberto * 4181834Sroberto * binio.c,v 4.5 2005/04/16 17:32:10 kardel RELEASE_20050508_A 5181834Sroberto * 6181834Sroberto * $Created: Sun Jul 20 12:55:33 1997 $ 7181834Sroberto * 8181834Sroberto * Copyright (c) 1997-2005 by Frank Kardel <kardel <AT> ntp.org> 9181834Sroberto * 10181834Sroberto * Redistribution and use in source and binary forms, with or without 11181834Sroberto * modification, are permitted provided that the following conditions 12181834Sroberto * are met: 13181834Sroberto * 1. Redistributions of source code must retain the above copyright 14181834Sroberto * notice, this list of conditions and the following disclaimer. 15181834Sroberto * 2. Redistributions in binary form must reproduce the above copyright 16181834Sroberto * notice, this list of conditions and the following disclaimer in the 17181834Sroberto * documentation and/or other materials provided with the distribution. 18181834Sroberto * 3. Neither the name of the author nor the names of its contributors 19181834Sroberto * may be used to endorse or promote products derived from this software 20181834Sroberto * without specific prior written permission. 21181834Sroberto * 22181834Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23181834Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24181834Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25181834Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26181834Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27181834Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28181834Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29181834Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30181834Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31181834Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32181834Sroberto * SUCH DAMAGE. 33181834Sroberto * 34181834Sroberto */ 35181834Sroberto 36181834Sroberto#include "binio.h" 37181834Sroberto 38181834Srobertolong 39181834Srobertoget_lsb_short( 40181834Sroberto unsigned char **bufpp 41181834Sroberto ) 42181834Sroberto{ 43181834Sroberto long retval; 44181834Sroberto 45181834Sroberto retval = *((*bufpp)++); 46181834Sroberto retval |= *((*bufpp)++) << 8; 47181834Sroberto 48181834Sroberto return (retval & 0x8000) ? (~0xFFFF | retval) : retval; 49181834Sroberto} 50181834Sroberto 51181834Srobertovoid 52181834Srobertoput_lsb_short( 53181834Sroberto unsigned char **bufpp, 54181834Sroberto long val 55181834Sroberto ) 56181834Sroberto{ 57181834Sroberto *((*bufpp)++) = (unsigned char) (val & 0xFF); 58181834Sroberto *((*bufpp)++) = (unsigned char) ((val >> 8) & 0xFF); 59181834Sroberto} 60181834Sroberto 61181834Srobertolong 62181834Srobertoget_lsb_long( 63181834Sroberto unsigned char **bufpp 64181834Sroberto ) 65181834Sroberto{ 66181834Sroberto long retval; 67181834Sroberto 68181834Sroberto retval = *((*bufpp)++); 69181834Sroberto retval |= *((*bufpp)++) << 8; 70181834Sroberto retval |= *((*bufpp)++) << 16; 71181834Sroberto retval |= *((*bufpp)++) << 24; 72181834Sroberto 73181834Sroberto return retval; 74181834Sroberto} 75181834Sroberto 76181834Srobertovoid 77181834Srobertoput_lsb_long( 78181834Sroberto unsigned char **bufpp, 79181834Sroberto long val 80181834Sroberto ) 81181834Sroberto{ 82181834Sroberto *((*bufpp)++) = (unsigned char)(val & 0xFF); 83181834Sroberto *((*bufpp)++) = (unsigned char)((val >> 8) & 0xFF); 84181834Sroberto *((*bufpp)++) = (unsigned char)((val >> 16) & 0xFF); 85181834Sroberto *((*bufpp)++) = (unsigned char)((val >> 24) & 0xFF); 86181834Sroberto} 87181834Sroberto 88181834Srobertolong 89181834Srobertoget_msb_short( 90181834Sroberto unsigned char **bufpp 91181834Sroberto ) 92181834Sroberto{ 93181834Sroberto long retval; 94181834Sroberto 95181834Sroberto retval = *((*bufpp)++) << 8; 96181834Sroberto retval |= *((*bufpp)++); 97181834Sroberto 98181834Sroberto return (retval & 0x8000) ? (~0xFFFF | retval) : retval; 99181834Sroberto} 100181834Sroberto 101181834Srobertovoid 102181834Srobertoput_msb_short( 103181834Sroberto unsigned char **bufpp, 104181834Sroberto long val 105181834Sroberto ) 106181834Sroberto{ 107181834Sroberto *((*bufpp)++) = (unsigned char)((val >> 8) & 0xFF); 108181834Sroberto *((*bufpp)++) = (unsigned char)( val & 0xFF); 109181834Sroberto} 110181834Sroberto 111181834Srobertolong 112181834Srobertoget_msb_long( 113181834Sroberto unsigned char **bufpp 114181834Sroberto ) 115181834Sroberto{ 116181834Sroberto long retval; 117181834Sroberto 118181834Sroberto retval = *((*bufpp)++) << 24; 119181834Sroberto retval |= *((*bufpp)++) << 16; 120181834Sroberto retval |= *((*bufpp)++) << 8; 121181834Sroberto retval |= *((*bufpp)++); 122181834Sroberto 123181834Sroberto return retval; 124181834Sroberto} 125181834Sroberto 126181834Srobertovoid 127181834Srobertoput_msb_long( 128181834Sroberto unsigned char **bufpp, 129181834Sroberto long val 130181834Sroberto ) 131181834Sroberto{ 132181834Sroberto *((*bufpp)++) = (unsigned char)((val >> 24) & 0xFF); 133181834Sroberto *((*bufpp)++) = (unsigned char)((val >> 16) & 0xFF); 134181834Sroberto *((*bufpp)++) = (unsigned char)((val >> 8 ) & 0xFF); 135181834Sroberto *((*bufpp)++) = (unsigned char)( val & 0xFF); 136181834Sroberto} 137181834Sroberto 138181834Sroberto/* 139181834Sroberto * binio.c,v 140181834Sroberto * Revision 4.2 1999/02/21 12:17:34 kardel 141181834Sroberto * 4.91f reconcilation 142181834Sroberto * 143181834Sroberto * Revision 4.1 1998/06/28 16:47:50 kardel 144181834Sroberto * added {get,put}_msb_{short,long} functions 145181834Sroberto * 146181834Sroberto * Revision 4.0 1998/04/10 19:46:16 kardel 147181834Sroberto * Start 4.0 release version numbering 148181834Sroberto * 149181834Sroberto * Revision 1.1 1998/04/10 19:27:46 kardel 150181834Sroberto * initial NTP VERSION 4 integration of PARSE with GPS166 binary support 151181834Sroberto * 152181834Sroberto * Revision 1.1 1997/10/06 21:05:46 kardel 153181834Sroberto * new parse structure 154181834Sroberto * 155181834Sroberto */ 156