home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / rpc / xdr.h < prev   
Encoding:
C/C++ Source or Header  |  1999-01-04  |  9.9 KB  |  297 lines

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