1ea906c41SOllivier Robert /* 2ea906c41SOllivier Robert * /src/NTP/ntp4-dev/libntp/binio.c,v 4.5 2005/04/16 17:32:10 kardel RELEASE_20050508_A 3ea906c41SOllivier Robert * 4ea906c41SOllivier Robert * binio.c,v 4.5 2005/04/16 17:32:10 kardel RELEASE_20050508_A 5ea906c41SOllivier Robert * 6ea906c41SOllivier Robert * $Created: Sun Jul 20 12:55:33 1997 $ 7ea906c41SOllivier Robert * 8ea906c41SOllivier Robert * Copyright (c) 1997-2005 by Frank Kardel <kardel <AT> ntp.org> 9ea906c41SOllivier Robert * 10ea906c41SOllivier Robert * Redistribution and use in source and binary forms, with or without 11ea906c41SOllivier Robert * modification, are permitted provided that the following conditions 12ea906c41SOllivier Robert * are met: 13ea906c41SOllivier Robert * 1. Redistributions of source code must retain the above copyright 14ea906c41SOllivier Robert * notice, this list of conditions and the following disclaimer. 15ea906c41SOllivier Robert * 2. Redistributions in binary form must reproduce the above copyright 16ea906c41SOllivier Robert * notice, this list of conditions and the following disclaimer in the 17ea906c41SOllivier Robert * documentation and/or other materials provided with the distribution. 18ea906c41SOllivier Robert * 3. Neither the name of the author nor the names of its contributors 19ea906c41SOllivier Robert * may be used to endorse or promote products derived from this software 20ea906c41SOllivier Robert * without specific prior written permission. 21ea906c41SOllivier Robert * 22ea906c41SOllivier Robert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23ea906c41SOllivier Robert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24ea906c41SOllivier Robert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25ea906c41SOllivier Robert * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26ea906c41SOllivier Robert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27ea906c41SOllivier Robert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28ea906c41SOllivier Robert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29ea906c41SOllivier Robert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30ea906c41SOllivier Robert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31ea906c41SOllivier Robert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32ea906c41SOllivier Robert * SUCH DAMAGE. 33ea906c41SOllivier Robert * 34ea906c41SOllivier Robert */ 35ea906c41SOllivier Robert 36*2b15cb3dSCy Schubert #include <config.h> 37ea906c41SOllivier Robert #include "binio.h" 38ea906c41SOllivier Robert 39ea906c41SOllivier Robert long 40ea906c41SOllivier Robert get_lsb_short( 41ea906c41SOllivier Robert unsigned char **bufpp 42ea906c41SOllivier Robert ) 43ea906c41SOllivier Robert { 44ea906c41SOllivier Robert long retval; 45ea906c41SOllivier Robert 46ea906c41SOllivier Robert retval = *((*bufpp)++); 47ea906c41SOllivier Robert retval |= *((*bufpp)++) << 8; 48ea906c41SOllivier Robert 49ea906c41SOllivier Robert return (retval & 0x8000) ? (~0xFFFF | retval) : retval; 50ea906c41SOllivier Robert } 51ea906c41SOllivier Robert 52ea906c41SOllivier Robert void 53ea906c41SOllivier Robert put_lsb_short( 54ea906c41SOllivier Robert unsigned char **bufpp, 55ea906c41SOllivier Robert long val 56ea906c41SOllivier Robert ) 57ea906c41SOllivier Robert { 58ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char) (val & 0xFF); 59ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char) ((val >> 8) & 0xFF); 60ea906c41SOllivier Robert } 61ea906c41SOllivier Robert 62ea906c41SOllivier Robert long 63ea906c41SOllivier Robert get_lsb_long( 64ea906c41SOllivier Robert unsigned char **bufpp 65ea906c41SOllivier Robert ) 66ea906c41SOllivier Robert { 67ea906c41SOllivier Robert long retval; 68ea906c41SOllivier Robert 69ea906c41SOllivier Robert retval = *((*bufpp)++); 70ea906c41SOllivier Robert retval |= *((*bufpp)++) << 8; 71ea906c41SOllivier Robert retval |= *((*bufpp)++) << 16; 72*2b15cb3dSCy Schubert retval |= (u_long)*((*bufpp)++) << 24; 73ea906c41SOllivier Robert 74ea906c41SOllivier Robert return retval; 75ea906c41SOllivier Robert } 76ea906c41SOllivier Robert 77ea906c41SOllivier Robert void 78ea906c41SOllivier Robert put_lsb_long( 79ea906c41SOllivier Robert unsigned char **bufpp, 80ea906c41SOllivier Robert long val 81ea906c41SOllivier Robert ) 82ea906c41SOllivier Robert { 83ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)(val & 0xFF); 84ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 8) & 0xFF); 85ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 16) & 0xFF); 86ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 24) & 0xFF); 87ea906c41SOllivier Robert } 88ea906c41SOllivier Robert 89ea906c41SOllivier Robert long 90ea906c41SOllivier Robert get_msb_short( 91ea906c41SOllivier Robert unsigned char **bufpp 92ea906c41SOllivier Robert ) 93ea906c41SOllivier Robert { 94ea906c41SOllivier Robert long retval; 95ea906c41SOllivier Robert 96ea906c41SOllivier Robert retval = *((*bufpp)++) << 8; 97ea906c41SOllivier Robert retval |= *((*bufpp)++); 98ea906c41SOllivier Robert 99ea906c41SOllivier Robert return (retval & 0x8000) ? (~0xFFFF | retval) : retval; 100ea906c41SOllivier Robert } 101ea906c41SOllivier Robert 102ea906c41SOllivier Robert void 103ea906c41SOllivier Robert put_msb_short( 104ea906c41SOllivier Robert unsigned char **bufpp, 105ea906c41SOllivier Robert long val 106ea906c41SOllivier Robert ) 107ea906c41SOllivier Robert { 108ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 8) & 0xFF); 109ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)( val & 0xFF); 110ea906c41SOllivier Robert } 111ea906c41SOllivier Robert 112ea906c41SOllivier Robert long 113ea906c41SOllivier Robert get_msb_long( 114ea906c41SOllivier Robert unsigned char **bufpp 115ea906c41SOllivier Robert ) 116ea906c41SOllivier Robert { 117ea906c41SOllivier Robert long retval; 118ea906c41SOllivier Robert 119*2b15cb3dSCy Schubert retval = (u_long)*((*bufpp)++) << 24; 120ea906c41SOllivier Robert retval |= *((*bufpp)++) << 16; 121ea906c41SOllivier Robert retval |= *((*bufpp)++) << 8; 122ea906c41SOllivier Robert retval |= *((*bufpp)++); 123ea906c41SOllivier Robert 124ea906c41SOllivier Robert return retval; 125ea906c41SOllivier Robert } 126ea906c41SOllivier Robert 127ea906c41SOllivier Robert void 128ea906c41SOllivier Robert put_msb_long( 129ea906c41SOllivier Robert unsigned char **bufpp, 130ea906c41SOllivier Robert long val 131ea906c41SOllivier Robert ) 132ea906c41SOllivier Robert { 133ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 24) & 0xFF); 134ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 16) & 0xFF); 135ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 8 ) & 0xFF); 136ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)( val & 0xFF); 137ea906c41SOllivier Robert } 138ea906c41SOllivier Robert 139ea906c41SOllivier Robert /* 140ea906c41SOllivier Robert * binio.c,v 141ea906c41SOllivier Robert * Revision 4.2 1999/02/21 12:17:34 kardel 142ea906c41SOllivier Robert * 4.91f reconcilation 143ea906c41SOllivier Robert * 144ea906c41SOllivier Robert * Revision 4.1 1998/06/28 16:47:50 kardel 145ea906c41SOllivier Robert * added {get,put}_msb_{short,long} functions 146ea906c41SOllivier Robert * 147ea906c41SOllivier Robert * Revision 4.0 1998/04/10 19:46:16 kardel 148ea906c41SOllivier Robert * Start 4.0 release version numbering 149ea906c41SOllivier Robert * 150ea906c41SOllivier Robert * Revision 1.1 1998/04/10 19:27:46 kardel 151ea906c41SOllivier Robert * initial NTP VERSION 4 integration of PARSE with GPS166 binary support 152ea906c41SOllivier Robert * 153ea906c41SOllivier Robert * Revision 1.1 1997/10/06 21:05:46 kardel 154ea906c41SOllivier Robert * new parse structure 155ea906c41SOllivier Robert * 156ea906c41SOllivier Robert */ 157