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 36ea906c41SOllivier Robert #include "binio.h" 37ea906c41SOllivier Robert 38ea906c41SOllivier Robert long 39ea906c41SOllivier Robert get_lsb_short( 40ea906c41SOllivier Robert unsigned char **bufpp 41ea906c41SOllivier Robert ) 42ea906c41SOllivier Robert { 43ea906c41SOllivier Robert long retval; 44ea906c41SOllivier Robert 45ea906c41SOllivier Robert retval = *((*bufpp)++); 46ea906c41SOllivier Robert retval |= *((*bufpp)++) << 8; 47ea906c41SOllivier Robert 48ea906c41SOllivier Robert return (retval & 0x8000) ? (~0xFFFF | retval) : retval; 49ea906c41SOllivier Robert } 50ea906c41SOllivier Robert 51ea906c41SOllivier Robert void 52ea906c41SOllivier Robert put_lsb_short( 53ea906c41SOllivier Robert unsigned char **bufpp, 54ea906c41SOllivier Robert long val 55ea906c41SOllivier Robert ) 56ea906c41SOllivier Robert { 57ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char) (val & 0xFF); 58ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char) ((val >> 8) & 0xFF); 59ea906c41SOllivier Robert } 60ea906c41SOllivier Robert 61ea906c41SOllivier Robert long 62ea906c41SOllivier Robert get_lsb_long( 63ea906c41SOllivier Robert unsigned char **bufpp 64ea906c41SOllivier Robert ) 65ea906c41SOllivier Robert { 66ea906c41SOllivier Robert long retval; 67ea906c41SOllivier Robert 68ea906c41SOllivier Robert retval = *((*bufpp)++); 69ea906c41SOllivier Robert retval |= *((*bufpp)++) << 8; 70ea906c41SOllivier Robert retval |= *((*bufpp)++) << 16; 71ea906c41SOllivier Robert retval |= *((*bufpp)++) << 24; 72ea906c41SOllivier Robert 73ea906c41SOllivier Robert return retval; 74ea906c41SOllivier Robert } 75ea906c41SOllivier Robert 76ea906c41SOllivier Robert void 77ea906c41SOllivier Robert put_lsb_long( 78ea906c41SOllivier Robert unsigned char **bufpp, 79ea906c41SOllivier Robert long val 80ea906c41SOllivier Robert ) 81ea906c41SOllivier Robert { 82ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)(val & 0xFF); 83ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 8) & 0xFF); 84ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 16) & 0xFF); 85ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 24) & 0xFF); 86ea906c41SOllivier Robert } 87ea906c41SOllivier Robert 88ea906c41SOllivier Robert long 89ea906c41SOllivier Robert get_msb_short( 90ea906c41SOllivier Robert unsigned char **bufpp 91ea906c41SOllivier Robert ) 92ea906c41SOllivier Robert { 93ea906c41SOllivier Robert long retval; 94ea906c41SOllivier Robert 95ea906c41SOllivier Robert retval = *((*bufpp)++) << 8; 96ea906c41SOllivier Robert retval |= *((*bufpp)++); 97ea906c41SOllivier Robert 98ea906c41SOllivier Robert return (retval & 0x8000) ? (~0xFFFF | retval) : retval; 99ea906c41SOllivier Robert } 100ea906c41SOllivier Robert 101ea906c41SOllivier Robert void 102ea906c41SOllivier Robert put_msb_short( 103ea906c41SOllivier Robert unsigned char **bufpp, 104ea906c41SOllivier Robert long val 105ea906c41SOllivier Robert ) 106ea906c41SOllivier Robert { 107ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 8) & 0xFF); 108ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)( val & 0xFF); 109ea906c41SOllivier Robert } 110ea906c41SOllivier Robert 111ea906c41SOllivier Robert long 112ea906c41SOllivier Robert get_msb_long( 113ea906c41SOllivier Robert unsigned char **bufpp 114ea906c41SOllivier Robert ) 115ea906c41SOllivier Robert { 116ea906c41SOllivier Robert long retval; 117ea906c41SOllivier Robert 118ea906c41SOllivier Robert retval = *((*bufpp)++) << 24; 119ea906c41SOllivier Robert retval |= *((*bufpp)++) << 16; 120ea906c41SOllivier Robert retval |= *((*bufpp)++) << 8; 121ea906c41SOllivier Robert retval |= *((*bufpp)++); 122ea906c41SOllivier Robert 123ea906c41SOllivier Robert return retval; 124ea906c41SOllivier Robert } 125ea906c41SOllivier Robert 126ea906c41SOllivier Robert void 127ea906c41SOllivier Robert put_msb_long( 128ea906c41SOllivier Robert unsigned char **bufpp, 129ea906c41SOllivier Robert long val 130ea906c41SOllivier Robert ) 131ea906c41SOllivier Robert { 132ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 24) & 0xFF); 133ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 16) & 0xFF); 134ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)((val >> 8 ) & 0xFF); 135ea906c41SOllivier Robert *((*bufpp)++) = (unsigned char)( val & 0xFF); 136ea906c41SOllivier Robert } 137ea906c41SOllivier Robert 138ea906c41SOllivier Robert /* 139ea906c41SOllivier Robert * binio.c,v 140ea906c41SOllivier Robert * Revision 4.2 1999/02/21 12:17:34 kardel 141ea906c41SOllivier Robert * 4.91f reconcilation 142ea906c41SOllivier Robert * 143ea906c41SOllivier Robert * Revision 4.1 1998/06/28 16:47:50 kardel 144ea906c41SOllivier Robert * added {get,put}_msb_{short,long} functions 145ea906c41SOllivier Robert * 146ea906c41SOllivier Robert * Revision 4.0 1998/04/10 19:46:16 kardel 147ea906c41SOllivier Robert * Start 4.0 release version numbering 148ea906c41SOllivier Robert * 149ea906c41SOllivier Robert * Revision 1.1 1998/04/10 19:27:46 kardel 150ea906c41SOllivier Robert * initial NTP VERSION 4 integration of PARSE with GPS166 binary support 151ea906c41SOllivier Robert * 152ea906c41SOllivier Robert * Revision 1.1 1997/10/06 21:05:46 kardel 153ea906c41SOllivier Robert * new parse structure 154ea906c41SOllivier Robert * 155ea906c41SOllivier Robert */ 156