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