home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilss / sockets / include / rpc / h / xdr < prev   
Encoding:
Text File  |  1995-01-11  |  9.5 KB  |  286 lines

  1. /* -*-C-*-
  2.  *
  3.  * $Header: /ax/networking:include/rpc/xdr.h:networking  1.1  $
  4.  * $Source: /ax/networking:include/rpc/xdr.h: $
  5.  *
  6.  * Copyright (c) 1995 Acorn Computers Ltd., Cambridge, England
  7.  *
  8.  * $Log:    xdr.h,v $
  9.  * Revision 1.1  95/01/11  10:18:38  kwelton
  10.  * Initial revision
  11.  * 
  12.  */
  13.  
  14. /* @(#)xdr.h    2.2 88/07/29 4.0 RPCSRC */
  15. /*
  16.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  17.  * unrestricted use provided that this legend is included on all tape
  18.  * media and as a part of the software program in whole or part.  Users
  19.  * may copy or modify Sun RPC without charge, but are not authorized
  20.  * to license or distribute it to anyone else except as part of a product or
  21.  * program developed by the user.
  22.  * 
  23.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  24.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  25.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  26.  * 
  27.  * Sun RPC is provided with no support and without any obligation on the
  28.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  29.  * modification or enhancement.
  30.  * 
  31.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  32.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  33.  * OR ANY PART THEREOF.
  34.  * 
  35.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  36.  * or profits or other special, indirect and consequential damages, even if
  37.  * Sun has been advised of the possibility of such damages.
  38.  * 
  39.  * Sun Microsystems, Inc.
  40.  * 2550 Garcia Avenue
  41.  * Mountain View, California  94043
  42.  */
  43. /*      @(#)xdr.h 1.19 87/04/22 SMI      */
  44.  
  45. /*
  46.  * xdr.h, External Data Representation Serialization Routines.
  47.  *
  48.  * Copyright (C) 1984, Sun Microsystems, Inc.
  49.  */
  50.  
  51. #ifndef __XDR_HEADER__
  52. #define __XDR_HEADER__
  53.  
  54. /*
  55.  * XDR provides a conventional way for converting between C data
  56.  * types and an external bit-string representation.  Library supplied
  57.  * routines provide for the conversion on built-in C data types.  These
  58.  * routines and utility routines defined here are used to help implement
  59.  * a type encode/decode routine for each user-defined type.
  60.  *
  61.  * Each data type provides a single procedure which takes two arguments:
  62.  *
  63.  *    bool_t
  64.  *    xdrproc(xdrs, argresp)
  65.  *        XDR *xdrs;
  66.  *        <type> *argresp;
  67.  *
  68.  * xdrs is an instance of a XDR handle, to which or from which the data
  69.  * type is to be converted.  argresp is a pointer to the structure to be
  70.  * converted.  The XDR handle contains an operation field which indicates
  71.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  72.  *
  73.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  74.  * data can be freed with the XDR_FREE operation.
  75.  *
  76.  * We write only one procedure per data type to make it easy
  77.  * to keep the encode and decode procedures for a data type consistent.
  78.  * In many cases the same code performs all operations on a user defined type,
  79.  * because all the hard work is done in the component type routines.
  80.  * decode as a series of calls on the nested data types.
  81.  */
  82.  
  83. /*
  84.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  85.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  86.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  87.  * request.
  88.  */
  89. enum xdr_op {
  90.     XDR_ENCODE=0,
  91.     XDR_DECODE=1,
  92.     XDR_FREE=2
  93. };
  94.  
  95. /*
  96.  * This is the number of bytes per unit of external data.
  97.  */
  98. #define BYTES_PER_XDR_UNIT    (4)
  99. #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  100.             * BYTES_PER_XDR_UNIT)
  101.  
  102. /*
  103.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  104.  *
  105.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  106.  * The opaque pointer generally points to a structure of the data type
  107.  * to be decoded.  If this pointer is 0, then the type routines should
  108.  * allocate dynamic storage of the appropriate size and return it.
  109.  * bool_t    (*xdrproc_t)(XDR *, caddr_t *);
  110.  */
  111. typedef    bool_t (*xdrproc_t)();
  112.  
  113. /*
  114.  * The XDR handle.
  115.  * Contains operation which is being applied to the stream,
  116.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  117.  * and two private fields for the use of the particular impelementation.
  118.  */
  119. typedef struct {
  120.     enum xdr_op    x_op;        /* operation; fast additional param */
  121.     struct xdr_ops {
  122.         bool_t    (*x_getlong)();    /* get a long from underlying stream */
  123.         bool_t    (*x_putlong)();    /* put a long to " */
  124.         bool_t    (*x_getbytes)();/* get some bytes from " */
  125.         bool_t    (*x_putbytes)();/* put some bytes to " */
  126.         u_int    (*x_getpostn)();/* returns bytes off from beginning */
  127.         bool_t  (*x_setpostn)();/* lets you reposition the stream */
  128.         long *    (*x_inline)();    /* buf quick ptr to buffered data */
  129.         void    (*x_destroy)();    /* free privates of this xdr_stream */
  130.     } *x_ops;
  131.     caddr_t     x_public;    /* users' data */
  132.     caddr_t        x_private;    /* pointer to private data */
  133.     caddr_t     x_base;        /* private used for position info */
  134.     int        x_handy;    /* extra private word */
  135. } XDR;
  136.  
  137. /*
  138.  * Operations defined on a XDR handle
  139.  *
  140.  * XDR        *xdrs;
  141.  * long        *longp;
  142.  * caddr_t     addr;
  143.  * u_int     len;
  144.  * u_int     pos;
  145.  */
  146. #define XDR_GETLONG(xdrs, longp)            \
  147.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  148. #define xdr_getlong(xdrs, longp)            \
  149.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  150.  
  151. #define XDR_PUTLONG(xdrs, longp)            \
  152.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  153. #define xdr_putlong(xdrs, longp)            \
  154.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  155.  
  156. #define XDR_GETBYTES(xdrs, addr, len)            \
  157.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  158. #define xdr_getbytes(xdrs, addr, len)            \
  159.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  160.  
  161. #define XDR_PUTBYTES(xdrs, addr, len)            \
  162.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  163. #define xdr_putbytes(xdrs, addr, len)            \
  164.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  165.  
  166. #define XDR_GETPOS(xdrs)                \
  167.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  168. #define xdr_getpos(xdrs)                \
  169.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  170.  
  171. #define XDR_SETPOS(xdrs, pos)                \
  172.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  173. #define xdr_setpos(xdrs, pos)                \
  174.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  175.  
  176. #define    XDR_INLINE(xdrs, len)                \
  177.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  178. #define    xdr_inline(xdrs, len)                \
  179.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  180.  
  181. #define    XDR_DESTROY(xdrs)                \
  182.     if ((xdrs)->x_ops->x_destroy)             \
  183.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  184. #define    xdr_destroy(xdrs)                \
  185.     if ((xdrs)->x_ops->x_destroy)             \
  186.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  187.  
  188. /*
  189.  * Support struct for discriminated unions.
  190.  * You create an array of xdrdiscrim structures, terminated with
  191.  * a entry with a null procedure pointer.  The xdr_union routine gets
  192.  * the discriminant value and then searches the array of structures
  193.  * for a matching value.  If a match is found the associated xdr routine
  194.  * is called to handle that part of the union.  If there is
  195.  * no match, then a default routine may be called.
  196.  * If there is no match and no default routine it is an error.
  197.  */
  198. #define NULL_xdrproc_t ((xdrproc_t)0)
  199. struct xdr_discrim {
  200.     int    value;
  201.     xdrproc_t proc;
  202. };
  203.  
  204. /*
  205.  * In-line routines for fast encode/decode of primitve data types.
  206.  * Caveat emptor: these use single memory cycles to get the
  207.  * data from the underlying buffer, and will fail to operate
  208.  * properly if the data is not aligned.  The standard way to use these
  209.  * is to say:
  210.  *    if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  211.  *        return (FALSE);
  212.  *    <<< macro calls >>>
  213.  * where ``count'' is the number of bytes of data occupied
  214.  * by the primitive data types.
  215.  *
  216.  * N.B. and frozen for all time: each data type here uses 4 bytes
  217.  * of external representation.
  218.  */
  219. #define IXDR_GET_LONG(buf)        ((long)ntohl((u_long)*(buf)++))
  220. #define IXDR_PUT_LONG(buf, v)        (*(buf)++ = (long)htonl((u_long)v))
  221.  
  222. #define IXDR_GET_BOOL(buf)        ((bool_t)IXDR_GET_LONG(buf))
  223. #define IXDR_GET_ENUM(buf, t)        ((t)IXDR_GET_LONG(buf))
  224. #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_LONG(buf))
  225. #define IXDR_GET_SHORT(buf)        ((short)IXDR_GET_LONG(buf))
  226. #define IXDR_GET_U_SHORT(buf)        ((u_short)IXDR_GET_LONG(buf))
  227.  
  228. #define IXDR_PUT_BOOL(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  229. #define IXDR_PUT_ENUM(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  230. #define IXDR_PUT_U_LONG(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  231. #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  232. #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_LONG((buf), ((long)(v)))
  233.  
  234. /*
  235.  * These are the "generic" xdr routines.
  236.  */
  237. extern bool_t    xdr_void();
  238. extern bool_t    xdr_int();
  239. extern bool_t    xdr_u_int();
  240. extern bool_t    xdr_long();
  241. extern bool_t    xdr_u_long();
  242. extern bool_t    xdr_short();
  243. extern bool_t    xdr_u_short();
  244. extern bool_t    xdr_bool();
  245. extern bool_t    xdr_enum();
  246. extern bool_t    xdr_array();
  247. extern bool_t    xdr_bytes();
  248. extern bool_t    xdr_opaque();
  249. extern bool_t    xdr_string();
  250. extern bool_t    xdr_union();
  251. extern bool_t    xdr_char();
  252. extern bool_t    xdr_u_char();
  253. extern bool_t    xdr_vector();
  254. extern bool_t    xdr_float();
  255. extern bool_t    xdr_double();
  256. extern bool_t    xdr_reference();
  257. extern bool_t    xdr_pointer();
  258. extern bool_t    xdr_wrapstring();
  259.  
  260. /*
  261.  * Common opaque bytes objects used by many rpc protocols;
  262.  * declared here due to commonality.
  263.  */
  264. #define MAX_NETOBJ_SZ 1024 
  265. struct netobj {
  266.     u_int    n_len;
  267.     char    *n_bytes;
  268. };
  269. typedef struct netobj netobj;
  270. extern bool_t   xdr_netobj();
  271.  
  272. /*
  273.  * These are the public routines for the various implementations of
  274.  * xdr streams.
  275.  */
  276. extern void   xdrmem_create();        /* XDR using memory buffers */
  277. extern void   xdrstdio_create();    /* XDR using stdio library */
  278. extern void   xdrrec_create();        /* XDR pseudo records for tcp */
  279. extern bool_t xdrrec_endofrecord();    /* make end of xdr record */
  280. extern bool_t xdrrec_skiprecord();    /* move to beginning of next record */
  281. extern bool_t xdrrec_eof();        /* true if no more input */
  282.  
  283. #endif !__XDR_HEADER__
  284.  
  285. /* EOF xdr.h */
  286.