home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / header45.zip / stack16 / rpc / xdr.h < prev   
C/C++ Source or Header  |  1999-05-11  |  15KB  |  347 lines

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