home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Headers / bsd / rpc / xdr.h < prev   
C/C++ Source or Header  |  1995-01-23  |  8KB  |  243 lines

  1. /*    @(#)xdr.h    1.1 88/03/04 4.0NFSSRC SMI    */
  2.  
  3. /* 
  4.  * Copyright (c) 1988 by Sun Microsystems, Inc.
  5.  *      @(#)xdr.h 1.22 88/02/08 SMI 
  6.  */
  7.  
  8.  
  9. /*
  10.  * xdr.h, External Data Representation Serialization Routines.
  11.  */
  12.  
  13. #ifndef __XDR_HEADER__
  14. #define __XDR_HEADER__
  15.  
  16. /*
  17.  * XDR provides a conventional way for converting between C data
  18.  * types and an external bit-string representation.  Library supplied
  19.  * routines provide for the conversion on built-in C data types.  These
  20.  * routines and utility routines defined here are used to help implement
  21.  * a type encode/decode routine for each user-defined type.
  22.  *
  23.  * Each data type provides a single procedure which takes two arguments:
  24.  *
  25.  *    bool_t
  26.  *    xdrproc(xdrs, argresp)
  27.  *        XDR *xdrs;
  28.  *        <type> *argresp;
  29.  *
  30.  * xdrs is an instance of a XDR handle, to which or from which the data
  31.  * type is to be converted.  argresp is a pointer to the structure to be
  32.  * converted.  The XDR handle contains an operation field which indicates
  33.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  34.  *
  35.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  36.  * data can be freed with the XDR_FREE operation.
  37.  *
  38.  * We write only one procedure per data type to make it easy
  39.  * to keep the encode and decode procedures for a data type consistent.
  40.  * In many cases the same code performs all operations on a user defined type,
  41.  * because all the hard work is done in the component type routines.
  42.  * decode as a series of calls on the nested data types.
  43.  */
  44.  
  45. /*
  46.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  47.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  48.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  49.  * request.
  50.  */
  51. enum xdr_op {
  52.     XDR_ENCODE=0,
  53.     XDR_DECODE=1,
  54.     XDR_FREE=2
  55. };
  56.  
  57. /*
  58.  * This is the number of bytes per unit of external data.
  59.  */
  60. #define BYTES_PER_XDR_UNIT    (4)
  61. #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  62.             * BYTES_PER_XDR_UNIT)
  63.  
  64. /*
  65.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  66.  *
  67.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  68.  * The opaque pointer generally points to a structure of the data type
  69.  * to be decoded.  If this pointer is 0, then the type routines should
  70.  * allocate dynamic storage of the appropriate size and return it.
  71.  * bool_t    (*xdrproc_t)(XDR *, caddr_t *);
  72.  */
  73. typedef    bool_t (*xdrproc_t)();
  74.  
  75. /*
  76.  * The XDR handle.
  77.  * Contains operation which is being applied to the stream,
  78.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  79.  * and two private fields for the use of the particular impelementation.
  80.  */
  81. typedef struct {
  82.     enum xdr_op    x_op;        /* operation; fast additional param */
  83.     struct xdr_ops {
  84.         bool_t    (*x_getlong)();    /* get a long from underlying stream */
  85.         bool_t    (*x_putlong)();    /* put a long to " */
  86.         bool_t    (*x_getbytes)();/* get some bytes from " */
  87.         bool_t    (*x_putbytes)();/* put some bytes to " */
  88.         u_int    (*x_getpostn)();/* returns bytes off from beginning */
  89.         bool_t  (*x_setpostn)();/* lets you reposition the stream */
  90.         long *    (*x_inline)();    /* buf quick ptr to buffered data */
  91.         void    (*x_destroy)();    /* free privates of this xdr_stream */
  92.     } *x_ops;
  93.     caddr_t     x_public;    /* users' data */
  94.     caddr_t        x_private;    /* pointer to private data */
  95.     caddr_t     x_base;        /* private used for position info */
  96.     int        x_handy;    /* extra private word */
  97. } XDR;
  98.  
  99. /*
  100.  * Operations defined on a XDR handle
  101.  *
  102.  * XDR        *xdrs;
  103.  * long        *longp;
  104.  * caddr_t     addr;
  105.  * u_int     len;
  106.  * u_int     pos;
  107.  */
  108. #define XDR_GETLONG(xdrs, longp)            \
  109.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  110. #define xdr_getlong(xdrs, longp)            \
  111.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  112.  
  113. #define XDR_PUTLONG(xdrs, longp)            \
  114.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  115. #define xdr_putlong(xdrs, longp)            \
  116.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  117.  
  118. #define XDR_GETBYTES(xdrs, addr, len)            \
  119.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  120. #define xdr_getbytes(xdrs, addr, len)            \
  121.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  122.  
  123. #define XDR_PUTBYTES(xdrs, addr, len)            \
  124.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  125. #define xdr_putbytes(xdrs, addr, len)            \
  126.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  127.  
  128. #define XDR_GETPOS(xdrs)                \
  129.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  130. #define xdr_getpos(xdrs)                \
  131.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  132.  
  133. #define XDR_SETPOS(xdrs, pos)                \
  134.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  135. #define xdr_setpos(xdrs, pos)                \
  136.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  137.  
  138. #define    XDR_INLINE(xdrs, len)                \
  139.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  140. #define    xdr_inline(xdrs, len)                \
  141.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  142.  
  143. #define    XDR_DESTROY(xdrs)                \
  144.     (*(xdrs)->x_ops->x_destroy)(xdrs)
  145. #define    xdr_destroy(xdrs) XDR_DESTROY(xdrs)
  146.  
  147. /*
  148.  * Support struct for discriminated unions.
  149.  * You create an array of xdrdiscrim structures, terminated with
  150.  * a entry with a null procedure pointer.  The xdr_union routine gets
  151.  * the discriminant value and then searches the array of structures
  152.  * for a matching value.  If a match is found the associated xdr routine
  153.  * is called to handle that part of the union.  If there is
  154.  * no match, then a default routine may be called.
  155.  * If there is no match and no default routine it is an error.
  156.  */
  157. #define NULL_xdrproc_t ((xdrproc_t)0)
  158. struct xdr_discrim {
  159.     int    value;
  160.     xdrproc_t proc;
  161. };
  162.  
  163. /*
  164.  * In-line routines for fast encode/decode of primitve data types.
  165.  * Caveat emptor: these use single memory cycles to get the
  166.  * data from the underlying buffer, and will fail to operate
  167.  * properly if the data is not aligned.  The standard way to use these
  168.  * is to say:
  169.  *    if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  170.  *        return (FALSE);
  171.  *    <<< macro calls >>>
  172.  * where ``count'' is the number of bytes of data occupied
  173.  * by the primitive data types.
  174.  *
  175.  * N.B. and frozen for all time: each data type here uses 4 bytes
  176.  * of external representation.
  177.  */
  178. #define IXDR_GET_LONG(buf)        ((long)ntohl((u_long)*(buf)++))
  179. #define IXDR_PUT_LONG(buf, v)        (*(buf)++ = (long)htonl((u_long)v))
  180.  
  181. #define IXDR_GET_BOOL(buf)        ((bool_t)IXDR_GET_LONG(buf))
  182. #define IXDR_GET_ENUM(buf, t)        ((t)IXDR_GET_LONG(buf))
  183. #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_LONG(buf))
  184. #define IXDR_GET_SHORT(buf)        ((short)IXDR_GET_LONG(buf))
  185. #define IXDR_GET_U_SHORT(buf)        ((u_short)IXDR_GET_LONG(buf))
  186.  
  187. #define IXDR_PUT_BOOL(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  188. #define IXDR_PUT_ENUM(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  189. #define IXDR_PUT_U_LONG(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  190. #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  191. #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_LONG((buf), ((long)(v)))
  192.  
  193. /*
  194.  * These are the "generic" xdr routines.
  195.  */
  196. extern bool_t    xdr_void();
  197. extern bool_t    xdr_int();
  198. extern bool_t    xdr_u_int();
  199. extern bool_t    xdr_long();
  200. extern bool_t    xdr_u_long();
  201. extern bool_t    xdr_short();
  202. extern bool_t    xdr_u_short();
  203. extern bool_t    xdr_bool();
  204. extern bool_t    xdr_enum();
  205. extern bool_t    xdr_array();
  206. extern bool_t    xdr_bytes();
  207. extern bool_t    xdr_opaque();
  208. extern bool_t    xdr_string();
  209. extern bool_t    xdr_union();
  210. extern bool_t    xdr_char();
  211. extern bool_t    xdr_u_char();
  212. extern bool_t    xdr_vector();
  213. extern bool_t    xdr_float();
  214. extern bool_t    xdr_double();
  215. extern bool_t    xdr_reference();
  216. extern bool_t    xdr_pointer();
  217. extern bool_t    xdr_wrapstring();
  218.  
  219. /*
  220.  * Common opaque bytes objects used by many rpc protocols;
  221.  * declared here due to commonality.
  222.  */
  223. #define MAX_NETOBJ_SZ 1024 
  224. struct netobj {
  225.     u_int    n_len;
  226.     char    *n_bytes;
  227. };
  228. typedef struct netobj netobj;
  229. extern bool_t   xdr_netobj();
  230.  
  231. /*
  232.  * These are the public routines for the various implementations of
  233.  * xdr streams.
  234.  */
  235. extern void   xdrmem_create();        /* XDR using memory buffers */
  236. extern void   xdrstdio_create();    /* XDR using stdio library */
  237. extern void   xdrrec_create();        /* XDR pseudo records for tcp */
  238. extern bool_t xdrrec_endofrecord();    /* make end of xdr record */
  239. extern bool_t xdrrec_skiprecord();    /* move to beginning of next record */
  240. extern bool_t xdrrec_eof();        /* true if no more input */
  241.  
  242. #endif !__XDR_HEADER__
  243.