xdr.c

Go to the documentation of this file.
00001 /*                           X D R . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup hton */
00023 /*@{*/
00024 /** @file xdr.c
00025  *  Routines to implement an external data representation (XDR)
00026  *  compatible with the usual InterNet standards, e.g.:
00027  *  big-endian, twos-compliment fixed point, and IEEE floating point.
00028  *
00029  *  Routines to insert/extract short/long's into char arrays,
00030  *  independend of machine byte order and word-alignment.
00031  *  Uses encoding compatible with routines found in libpkg,
00032  *  and BSD system routines ntohl(), ntons(), ntohl(), ntohs().
00033  *
00034  *
00035  *  @author
00036  *      Michael John Muuss
00037  *
00038  *  @par Source -
00039  *      The U. S. Army Research Laboratory
00040  *@n    Aberdeen Proving Ground, Maryland  21005-5068  USA
00041  *
00042  */
00043 
00044 
00045 #ifndef lint
00046 static const char libbu_xdr_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/xdr.c,v 14.12 2006/08/31 23:16:39 lbutler Exp $ (ARL)";
00047 #endif
00048 
00049 #include "common.h"
00050 
00051 #include <stdio.h>
00052 #include <ctype.h>
00053 #include <math.h>
00054 #ifdef HAVE_STRING_H
00055 #  include <string.h>
00056 #else
00057 #  include <strings.h>
00058 #endif
00059 
00060 #include "machine.h"
00061 #include "bu.h"
00062 
00063 
00064 
00065 /**
00066  *                      B U _ G S H O R T
00067  */
00068 unsigned short
00069 bu_gshort(const unsigned char *msgp)
00070 {
00071     register const unsigned char *p = msgp;
00072 #ifdef vax
00073     /*
00074      * vax compiler doesn't put shorts in registers
00075      */
00076     register unsigned long u;
00077 #else
00078     register unsigned short u;
00079 #endif
00080 
00081     u = *p++ << 8;
00082     return ((unsigned short)(u | *p));
00083 }
00084 
00085 /**
00086  *                      B U _ G L O N G
00087  */
00088 unsigned long
00089 bu_glong(const unsigned char *msgp)
00090 {
00091     register const unsigned char *p = msgp;
00092     register unsigned long u;
00093 
00094     u = *p++; u <<= 8;
00095     u |= *p++; u <<= 8;
00096     u |= *p++; u <<= 8;
00097     return (u | *p);
00098 }
00099 
00100 /**
00101  *                      B U _ P S H O R T
00102  */
00103 unsigned char *
00104 bu_pshort(register unsigned char *msgp, register int s)
00105 {
00106 
00107     msgp[1] = s;
00108     msgp[0] = s >> 8;
00109     return(msgp+2);
00110 }
00111 
00112 /**
00113  *                      B U _ P L O N G
00114  */
00115 unsigned char *
00116 bu_plong(register unsigned char *msgp, register long unsigned int l)
00117 {
00118 
00119     msgp[3] = l;
00120     msgp[2] = (l >>= 8);
00121     msgp[1] = (l >>= 8);
00122     msgp[0] = l >> 8;
00123     return(msgp+4);
00124 }
00125 
00126 #if 0
00127 /* XXX How do we get "struct timeval" declared for all of bu.h? */
00128 /**
00129  *                      B U _ G T I M E V A L
00130  *
00131  *  Get a timeval structure from an external representation
00132  *  which "on the wire" is a 64-bit seconds, followed by a 32-bit usec.
00133  */
00134 typedef unsigned char ext_timeval_t[8+4];       /* storage for on-wire format */
00135 
00136 void
00137 bu_gtimeval( tvp, msgp )
00138      struct timeval *tvp;
00139      const unsigned char *msgp;
00140 {
00141     tvp->tv_sec = (((time_t)BU_GLONG( msgp+0 )) << 32) |
00142         BU_GLONG( msgp+4 );
00143     tvp->tv_usec = BU_GLONG( msgp+8 );
00144 }
00145 
00146 unsigned char *
00147 bu_ptimeval( msgp, tvp )
00148      const struct timeval *tvp;
00149      unsigned char *msgp;
00150 {
00151     long upper = (long)(tvp->tv_sec >> 32);
00152     long lower = (long)(tvp->tv_sec & 0xFFFFFFFFL);
00153 
00154     (void)bu_plong( msgp+0, upper );
00155     (void)bu_plong( msgp+4, lower );
00156     return bu_plong( msgp+8, tvp->tv_usec );
00157 }
00158 #endif
00159 
00160 /*@}*/
00161 /*
00162  * Local Variables:
00163  * mode: C
00164  * tab-width: 8
00165  * c-basic-offset: 4
00166  * indent-tabs-mode: t
00167  * End:
00168  * ex: shiftwidth=4 tabstop=8
00169  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6