Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

xdr.h

Go to the documentation of this file.
00001 /*  $Id$ */
00002 /*      $OpenBSD: xdr.h,v 1.2 1997/09/21 10:46:18 niklas Exp $  */
00003 /*      $NetBSD: xdr.h,v 1.7 1995/04/29 05:28:06 cgd Exp $      */
00004 
00005 /*
00006  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00007  * unrestricted use provided that this legend is included on all tape
00008  * media and as a part of the software program in whole or part.  Users
00009  * may copy or modify Sun RPC without charge, but are not authorized
00010  * to license or distribute it to anyone else except as part of a product or
00011  * program developed by the user.
00012  * 
00013  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00014  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00015  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00016  * 
00017  * Sun RPC is provided with no support and without any obligation on the
00018  * part of Sun Microsystems, Inc. to assist in its use, correction,
00019  * modification or enhancement.
00020  * 
00021  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00022  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00023  * OR ANY PART THEREOF.
00024  * 
00025  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00026  * or profits or other special, indirect and consequential damages, even if
00027  * Sun has been advised of the possibility of such damages.
00028  * 
00029  * Sun Microsystems, Inc.
00030  * 2550 Garcia Avenue
00031  * Mountain View, California  94043
00032  *
00033  *      from: @(#)xdr.h 1.19 87/04/22 SMI
00034  *      @(#)xdr.h       2.2 88/07/29 4.0 RPCSRC
00035  */
00036 
00037 /*
00038  * xdr.h, External Data Representation Serialization Routines.
00039  *
00040  * Copyright (C) 1984, Sun Microsystems, Inc.
00041  */
00042 
00043 #ifndef _RPC_XDR_H
00044 #define _RPC_XDR_H
00045 #ifndef WIN32
00046         #include <sys/cdefs.h>
00047 #else
00048     #include "cdefs.h"
00049 #endif
00050 
00051 /*
00052  * XDR provides a conventional way for converting between C data
00053  * types and an external bit-string representation.  Library supplied
00054  * routines provide for the conversion on built-in C data types.  These
00055  * routines and utility routines defined here are used to help implement
00056  * a type encode/decode routine for each user-defined type.
00057  *
00058  * Each data type provides a single procedure which takes two arguments:
00059  *
00060  *      bool_t
00061  *      xdrproc(xdrs, argresp)
00062  *              XDR *xdrs;
00063  *              <type> *argresp;
00064  *
00065  * xdrs is an instance of a XDR handle, to which or from which the data
00066  * type is to be converted.  argresp is a pointer to the structure to be
00067  * converted.  The XDR handle contains an operation field which indicates
00068  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
00069  *
00070  * XDR_DECODE may allocate space if the pointer argresp is null.  This
00071  * data can be freed with the XDR_FREE operation.
00072  *
00073  * We write only one procedure per data type to make it easy
00074  * to keep the encode and decode procedures for a data type consistent.
00075  * In many cases the same code performs all operations on a user defined type,
00076  * because all the hard work is done in the component type routines.
00077  * decode as a series of calls on the nested data types.
00078  */
00079 
00080 /*
00081  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
00082  * stream.  XDR_DECODE causes the type to be extracted from the stream.
00083  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
00084  * request.
00085  */
00086 enum xdr_op {
00087         XDR_ENCODE=0,
00088         XDR_DECODE=1,
00089         XDR_FREE=2
00090 };
00091 
00092 /*
00093  * This is the number of bytes per unit of external data.
00094  */
00095 #define BYTES_PER_XDR_UNIT      (4)
00096 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
00097                     * BYTES_PER_XDR_UNIT)
00098 
00099 /*
00100  * The XDR handle.
00101  * Contains operation which is being applied to the stream,
00102  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
00103  * and two private fields for the use of the particular impelementation.
00104  */
00105 typedef struct __rpc_xdr {
00106         enum xdr_op     x_op;           /* operation; fast additional param */
00107         struct xdr_ops {
00108                 /* get a long from underlying stream */
00109                 bool_t  (*x_getlong) __P((struct __rpc_xdr *, long *));
00110                 /* put a long to " */
00111                 bool_t  (*x_putlong) __P((struct __rpc_xdr *, long *));
00112                 /* get some bytes from " */
00113                 bool_t  (*x_getbytes) __P((struct __rpc_xdr *, caddr_t, u_int));
00114                 /* put some bytes to " */
00115                 bool_t  (*x_putbytes) __P((struct __rpc_xdr *, caddr_t, u_int));
00116                 /* returns bytes off from beginning */
00117                 u_int   (*x_getpostn) __P((struct __rpc_xdr *));
00118                 /* lets you reposition the stream */
00119                 bool_t  (*x_setpostn) __P((struct __rpc_xdr *, u_int));
00120                 /* buf quick ptr to buffered data */
00121                 int32_t *(*x_inline) __P((struct __rpc_xdr *, u_int));
00122                 /* free privates of this xdr_stream */
00123                 void    (*x_destroy) __P((struct __rpc_xdr *));
00124         } *x_ops;
00125         caddr_t         x_public;       /* users' data */
00126         caddr_t         x_private;      /* pointer to private data */
00127         caddr_t         x_base;         /* private used for position info */
00128         int             x_handy;        /* extra private word */
00129 } XDR;
00130 
00131 /*
00132  * A xdrproc_t exists for each data type which is to be encoded or decoded.
00133  *
00134  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
00135  * The opaque pointer generally points to a structure of the data type
00136  * to be decoded.  If this pointer is 0, then the type routines should
00137  * allocate dynamic storage of the appropriate size and return it.
00138  *
00139  * XXX can't actually prototype it, because some take three args!!!
00140  */
00141 typedef bool_t (*xdrproc_t) __P((/* XDR *, void *, u_int */));
00142 
00143 /*
00144  * Operations defined on a XDR handle
00145  *
00146  * XDR          *xdrs;
00147  * long         *longp;
00148  * caddr_t       addr;
00149  * u_int         len;
00150  * u_int         pos;
00151  */
00152 #define XDR_GETLONG(xdrs, longp)                        \
00153         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
00154 #define xdr_getlong(xdrs, longp)                        \
00155         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
00156 
00157 #define XDR_PUTLONG(xdrs, longp)                        \
00158         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
00159 #define xdr_putlong(xdrs, longp)                        \
00160         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
00161 
00162 #define XDR_GETBYTES(xdrs, addr, len)                   \
00163         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
00164 #define xdr_getbytes(xdrs, addr, len)                   \
00165         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
00166 
00167 #define XDR_PUTBYTES(xdrs, addr, len)                   \
00168         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
00169 #define xdr_putbytes(xdrs, addr, len)                   \
00170         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
00171 
00172 #define XDR_GETPOS(xdrs)                                \
00173         (*(xdrs)->x_ops->x_getpostn)(xdrs)
00174 #define xdr_getpos(xdrs)                                \
00175         (*(xdrs)->x_ops->x_getpostn)(xdrs)
00176 
00177 #define XDR_SETPOS(xdrs, pos)                           \
00178         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
00179 #define xdr_setpos(xdrs, pos)                           \
00180         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
00181 
00182 #define XDR_INLINE(xdrs, len)                           \
00183         (*(xdrs)->x_ops->x_inline)(xdrs, len)
00184 #define xdr_inline(xdrs, len)                           \
00185         (*(xdrs)->x_ops->x_inline)(xdrs, len)
00186 
00187 #define XDR_DESTROY(xdrs)                               \
00188         if ((xdrs)->x_ops->x_destroy)                   \
00189                 (*(xdrs)->x_ops->x_destroy)(xdrs)
00190 #define xdr_destroy(xdrs)                               \
00191         if ((xdrs)->x_ops->x_destroy)                   \
00192                 (*(xdrs)->x_ops->x_destroy)(xdrs)
00193 
00194 /*
00195  * Support struct for discriminated unions.
00196  * You create an array of xdrdiscrim structures, terminated with
00197  * a entry with a null procedure pointer.  The xdr_union routine gets
00198  * the discriminant value and then searches the array of structures
00199  * for a matching value.  If a match is found the associated xdr routine
00200  * is called to handle that part of the union.  If there is
00201  * no match, then a default routine may be called.
00202  * If there is no match and no default routine it is an error.
00203  */
00204 #define NULL_xdrproc_t ((xdrproc_t)0)
00205 struct xdr_discrim {
00206         int     value;
00207         xdrproc_t proc;
00208 };
00209 
00210 /*
00211  * In-line routines for fast encode/decode of primitve data types.
00212  * Caveat emptor: these use single memory cycles to get the
00213  * data from the underlying buffer, and will fail to operate
00214  * properly if the data is not aligned.  The standard way to use these
00215  * is to say:
00216  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
00217  *              return (FALSE);
00218  *      <<< macro calls >>>
00219  * where ``count'' is the number of bytes of data occupied
00220  * by the primitive data types.
00221  *
00222  * N.B. and frozen for all time: each data type here uses 4 bytes
00223  * of external representation.
00224  */
00225 #define IXDR_GET_LONG(buf)              ((long)ntohl((u_long)*(buf)++))
00226 #define IXDR_PUT_LONG(buf, v)           (*(buf)++ = (long)htonl((u_long)v))
00227 
00228 #define IXDR_GET_BOOL(buf)              ((bool_t)IXDR_GET_LONG(buf))
00229 #define IXDR_GET_ENUM(buf, t)           ((t)IXDR_GET_LONG(buf))
00230 #define IXDR_GET_U_LONG(buf)            ((u_long)IXDR_GET_LONG(buf))
00231 #define IXDR_GET_SHORT(buf)             ((short)IXDR_GET_LONG(buf))
00232 #define IXDR_GET_U_SHORT(buf)           ((u_short)IXDR_GET_LONG(buf))
00233 
00234 #define IXDR_PUT_BOOL(buf, v)           IXDR_PUT_LONG((buf), ((long)(v)))
00235 #define IXDR_PUT_ENUM(buf, v)           IXDR_PUT_LONG((buf), ((long)(v)))
00236 #define IXDR_PUT_U_LONG(buf, v)         IXDR_PUT_LONG((buf), ((long)(v)))
00237 #define IXDR_PUT_SHORT(buf, v)          IXDR_PUT_LONG((buf), ((long)(v)))
00238 #define IXDR_PUT_U_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
00239 
00240 /*
00241  * These are the "generic" xdr routines.
00242  */
00243 __BEGIN_DECLS
00244 extern bool_t   xdr_void        __P((void));
00245 extern bool_t   xdr_int         __P((XDR *, int *));
00246 extern bool_t   xdr_u_int       __P((XDR *, u_int *));
00247 extern bool_t   xdr_long        __P((XDR *, long *));
00248 extern bool_t   xdr_u_long      __P((XDR *, u_long *));
00249 extern bool_t   xdr_short       __P((XDR *, short *));
00250 extern bool_t   xdr_u_short     __P((XDR *, u_short *));
00251 extern bool_t   xdr_int16_t     __P((XDR *, int16_t *));
00252 extern bool_t   xdr_u_int16_t   __P((XDR *, u_int16_t *));
00253 extern bool_t   xdr_int32_t     __P((XDR *, int32_t *));
00254 extern bool_t   xdr_u_int32_t   __P((XDR *, u_int32_t *));
00255 extern bool_t   xdr_bool        __P((XDR *, bool_t *));
00256 extern bool_t   xdr_enum        __P((XDR *, enum_t *));
00257 extern bool_t   xdr_array       __P((XDR *, char **, u_int *, u_int, u_int, xdrproc_t));
00258 extern bool_t   xdr_bytes       __P((XDR *, char **, u_int *, u_int));
00259 extern bool_t   xdr_opaque      __P((XDR *, caddr_t, u_int));
00260 extern bool_t   xdr_string      __P((XDR *, char **, u_int));
00261 extern bool_t   xdr_union       __P((XDR *, enum_t *, char *, struct xdr_discrim *, xdrproc_t));
00262 extern bool_t   xdr_char        __P((XDR *, char *));
00263 extern bool_t   xdr_u_char      __P((XDR *, u_char *));
00264 extern bool_t   xdr_vector      __P((XDR *, char *, u_int, u_int, xdrproc_t));
00265 extern bool_t   xdr_float       __P((XDR *, float *));
00266 extern bool_t   xdr_double      __P((XDR *, double *));
00267 extern bool_t   xdr_reference   __P((XDR *, caddr_t *, u_int, xdrproc_t));
00268 extern bool_t   xdr_pointer     __P((XDR *, caddr_t *, u_int, xdrproc_t));
00269 extern bool_t   xdr_wrapstring  __P((XDR *, char **));
00270 extern void     xdr_free        __P((xdrproc_t, char *));
00271 __END_DECLS
00272 
00273 /*
00274  * Common opaque bytes objects used by many rpc protocols;
00275  * declared here due to commonality.
00276  */
00277 #define MAX_NETOBJ_SZ 1024 
00278 struct netobj {
00279         u_int   n_len;
00280         char    *n_bytes;
00281 };
00282 typedef struct netobj netobj;
00283 extern bool_t   xdr_netobj __P((XDR *, struct netobj *));
00284 
00285 /*
00286  * These are the public routines for the various implementations of
00287  * xdr streams.
00288  */
00289 __BEGIN_DECLS
00290 /* XDR using memory buffers */
00291 extern void   xdrmem_create     __P((XDR *, char *, u_int, enum xdr_op));
00292 
00293 #ifdef _STDIO_H_
00294 /* XDR using stdio library */
00295 extern void   xdrstdio_create   __P((XDR *, FILE *, enum xdr_op));
00296 #endif
00297 
00298 /* XDR pseudo records for tcp */
00299 extern void   xdrrec_create     __P((XDR *, u_int, u_int, char *,
00300                                     int (*) __P((caddr_t, caddr_t, int)),
00301                                     int (*) __P((caddr_t, caddr_t, int))));
00302 
00303 /* make end of xdr record */
00304 extern bool_t xdrrec_endofrecord __P((XDR *, int));
00305 
00306 /* move to beginning of next record */
00307 extern bool_t xdrrec_skiprecord __P((XDR *));
00308 
00309 /* true if no more input */
00310 extern bool_t xdrrec_eof        __P((XDR *));
00311 __END_DECLS
00312 
00313 #endif /* !_RPC_XDR_H */

Generated on Sun May 14 14:51:20 2006 by  doxygen 1.4.2