OpenAFS
OpenAFS distributed network file system
/cygdrive/c/src/openafs/openafs.git/repo/src/rx/xdr.h
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 
00030 /* This file has the contents of Sun's orginal types.h file added. */
00031 
00032 /*      @(#)types.h 1.1 86/02/03 SMI      */
00033 
00034 /*
00035  * Rpc additions to <sys/types.h>
00036  */
00037 
00038 #ifndef __XDR_INCLUDE__
00039 #define __XDR_INCLUDE__
00040 #include <afs/param.h>
00041 #ifdef AFS_NT40_ENV
00042 #ifndef _MFC_VER
00043 #include <winsock2.h>
00044 #endif /* _MFC_VER */
00045 #endif
00046 #ifndef bool_t
00047 #define bool_t  int
00048 #define enum_t  int
00049 #endif
00050 #ifndef FALSE
00051 #define FALSE   0
00052 #endif /* !FALSE */
00053 #ifndef TRUE
00054 #define TRUE    1
00055 #endif /* !TRUE */
00056 #define __dontcare__    -1
00057 
00058 #if defined(AFS_NBSD50_ENV)
00059 # if !defined(DEF_CADDR_T)
00060 typedef char * caddr_t;
00061 #define DEF_CADDR_T
00062 # endif
00063 #endif
00064 
00065 #ifndef mem_alloc
00066 #define mem_alloc(bsize)        malloc(bsize)
00067 #endif
00068 
00069 #ifndef mem_free
00070 #define mem_free(ptr, bsize)    free(ptr)
00071 #endif
00072 
00073 #if !defined(AFS_NT40_ENV)
00074 #define xdr_alloc afs_xdr_alloc
00075 #define xdr_free afs_xdr_free
00076 #define xdr_void afs_xdr_void
00077 #define xdr_int afs_xdr_int
00078 #define xdr_u_int afs_xdr_u_int
00079 #define xdr_short afs_xdr_short
00080 #define xdr_u_short afs_xdr_u_short
00081 #define xdr_long afs_xdr_long
00082 #define xdr_u_long afs_xdr_u_long
00083 #define xdr_char afs_xdr_char
00084 #define xdr_u_char afs_xdr_u_char
00085 #define xdr_bool afs_xdr_bool
00086 #define xdr_enum afs_xdr_enum
00087 #define xdr_array afs_xdr_array
00088 #define xdr_arrayN afs_xdr_arrayN
00089 #define xdr_bytes afs_xdr_bytes
00090 #define xdr_opaque afs_xdr_opaque
00091 #define xdr_string afs_xdr_string
00092 #define xdr_union afs_xdr_union
00093 #define xdr_float afs_xdr_float
00094 #define xdr_double afs_xdr_double
00095 #define xdr_reference afs_xdr_reference
00096 #define xdr_wrapstring afs_xdr_wrapstring
00097 #define xdr_vector afs_xdr_vector
00098 #define xdr_int64 afs_xdr_int64
00099 #define xdr_uint64 afs_xdr_uint64
00100 #define xdr_pointer afs_xdr_pointer
00101 #define xdrmem_create afs_xdrmem_create
00102 #endif
00103 
00104 #ifdef  KERNEL
00105 #define osi_alloc               afs_osi_Alloc
00106 #define osi_free                afs_osi_Free
00107 
00108 /* keep here for now, 64 bit issues */
00109 #ifndef AFS_OBSD44_ENV
00110 extern void *afs_osi_Alloc(size_t x);
00111 extern void afs_osi_Free(void *x, size_t asize);
00112 #endif
00113 
00114 #endif
00115 #ifndef major                   /* ouch! */
00116 #include <sys/types.h>
00117 #endif
00118 
00119 /*      @(#)xdr.h 1.1 86/02/03 SMI      */
00120 
00121 /*
00122  * xdr.h, External Data Representation Serialization Routines.
00123  *
00124  * Copyright (C) 1984, Sun Microsystems, Inc.
00125  */
00126 
00127 /*
00128  * XDR provides a conventional way for converting between C data
00129  * types and an external bit-string representation.  Library supplied
00130  * routines provide for the conversion on built-in C data types.  These
00131  * routines and utility routines defined here are used to help implement
00132  * a type encode/decode routine for each user-defined type.
00133  *
00134  * Each data type provides a single procedure which takes two arguments:
00135  *
00136  *      bool_t
00137  *      xdrproc(xdrs, argresp)
00138  *              XDR *xdrs;
00139  *              <type> *argresp;
00140  *
00141  * xdrs is an instance of a XDR handle, to which or from which the data
00142  * type is to be converted.  argresp is a pointer to the structure to be
00143  * converted.  The XDR handle contains an operation field which indicates
00144  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
00145  *
00146  * XDR_DECODE may allocate space if the pointer argresp is null.  This
00147  * data can be freed with the XDR_FREE operation.
00148  *
00149  * We write only one procedure per data type to make it easy
00150  * to keep the encode and decode procedures for a data type consistent.
00151  * In many cases the same code performs all operations on a user defined type,
00152  * because all the hard work is done in the component type routines.
00153  * decode as a series of calls on the nested data types.
00154  */
00155 
00156 /*
00157  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
00158  * stream.  XDR_DECODE causes the type to be extracted from the stream.
00159  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
00160  * request.
00161  */
00162 enum xdr_op {
00163     XDR_ENCODE = 0,
00164     XDR_DECODE = 1,
00165     XDR_FREE = 2
00166 };
00167 
00168 /*
00169  * This is the number of bytes per unit of external data.
00170  */
00171 #define BYTES_PER_XDR_UNIT      (4)
00172 
00173 /*
00174  * A xdrproc_t exists for each data type which is to be encoded or decoded.
00175  *
00176  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
00177  * The opaque pointer generally points to a structure of the data type
00178  * to be decoded.  If this pointer is 0, then the type routines should
00179  * allocate dynamic storage of the appropriate size and return it.
00180  * bool_t       (*xdrproc_t)(XDR *, caddr_t *);
00181  */
00182 
00183 /* We need a different prototype for i386 Linux kernel code, because it
00184  * uses a register (rather than stack) based calling convention. The
00185  * normal va_args prototype results in the arguments being placed on the
00186  * stack, where they aren't accessible to the 'real' function.
00187  */
00188 #if defined(AFS_I386_LINUX26_ENV) && defined(KERNEL) && !defined(UKERNEL)
00189 typedef bool_t(*xdrproc_t) (void *, caddr_t, u_int);
00190 #else
00191 typedef bool_t(*xdrproc_t) (void *, ...);
00192 #endif
00193 
00194 /*
00195  * The XDR handle.
00196  * Contains operation which is being applied to the stream,
00197  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
00198  * and two private fields for the use of the particular impelementation.
00199  */
00200 
00201 typedef struct __afs_xdr {
00202     enum xdr_op x_op;           /* operation; fast additional param */
00203     struct xdr_ops {
00204         bool_t(*x_getint32) (struct __afs_xdr *xdrs, afs_int32 * lp);   /* get an afs_int32 from underlying stream */
00205         bool_t(*x_putint32) (struct __afs_xdr *xdrs, afs_int32 * lp);   /* put an afs_int32 to " */
00206         bool_t(*x_getbytes) (struct __afs_xdr *xdrs, caddr_t addr, u_int len);  /* get some bytes from " */
00207         bool_t(*x_putbytes) (struct __afs_xdr *xdrs, caddr_t addr, u_int len);  /* put some bytes to " */
00208         u_int(*x_getpostn) (struct __afs_xdr *xdrs);    /* returns bytes off from beginning */
00209         bool_t(*x_setpostn) (struct __afs_xdr *xdrs, u_int pos);        /* lets you reposition the stream */
00210         afs_int32 *(*x_inline) (struct __afs_xdr *xdrs, u_int len);     /* buf quick ptr to buffered data */
00211         void (*x_destroy) (struct __afs_xdr *xdrs);     /* free privates of this xdr_stream */
00212     } *x_ops;
00213     caddr_t x_public;           /* users' data */
00214     caddr_t x_private;          /* pointer to private data */
00215     caddr_t x_base;             /* private used for position info */
00216     int x_handy;                /* extra private word */
00217 } XDR;
00218 
00219 /*
00220  * Operations defined on a XDR handle
00221  *
00222  * XDR          *xdrs;
00223  * afs_int32            *int32p;
00224  * caddr_t       addr;
00225  * u_int         len;
00226  * u_int         pos;
00227  */
00228 #define XDR_GETINT32(xdrs, int32p)                      \
00229         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
00230 #define xdr_getint32(xdrs, int32p)                      \
00231         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
00232 
00233 #define XDR_PUTINT32(xdrs, int32p)                      \
00234         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
00235 #define xdr_putint32(xdrs, int32p)                      \
00236         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
00237 
00238 #define XDR_GETBYTES(xdrs, addr, len)                   \
00239         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
00240 #define xdr_getbytes(xdrs, addr, len)                   \
00241         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
00242 
00243 #define XDR_PUTBYTES(xdrs, addr, len)                   \
00244         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
00245 #define xdr_putbytes(xdrs, addr, len)                   \
00246         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
00247 
00248 #define XDR_GETPOS(xdrs)                                \
00249         (*(xdrs)->x_ops->x_getpostn)(xdrs)
00250 #define xdr_getpos(xdrs)                                \
00251         (*(xdrs)->x_ops->x_getpostn)(xdrs)
00252 
00253 #define XDR_SETPOS(xdrs, pos)                           \
00254         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
00255 #define xdr_setpos(xdrs, pos)                           \
00256         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
00257 
00258 #define XDR_INLINE(xdrs, len)                           \
00259         (*(xdrs)->x_ops->x_inline)(xdrs, len)
00260 #define xdr_inline(xdrs, len)                           \
00261         (*(xdrs)->x_ops->x_inline)(xdrs, len)
00262 
00263 #define XDR_DESTROY(xdrs)                               \
00264         if ((xdrs)->x_ops->x_destroy)                   \
00265                 (*(xdrs)->x_ops->x_destroy)(xdrs)
00266 #define xdr_destroy(xdrs)                               \
00267         if ((xdrs)->x_ops->x_destroy)                   \
00268                 (*(xdrs)->x_ops->x_destroy)(xdrs)
00269 
00270 /*
00271  * Support struct for discriminated unions.
00272  * You create an array of xdrdiscrim structures, terminated with
00273  * a entry with a null procedure pointer.  The xdr_union routine gets
00274  * the discriminant value and then searches the array of structures
00275  * for a matching value.  If a match is found the associated xdr routine
00276  * is called to handle that part of the union.  If there is
00277  * no match, then a default routine may be called.
00278  * If there is no match and no default routine it is an error.
00279  */
00280 #define NULL_xdrproc_t ((xdrproc_t)0)
00281 struct xdr_discrim {
00282     int value;
00283     xdrproc_t proc;
00284 };
00285 
00286 /*
00287  * In-line routines for fast encode/decode of primitve data types.
00288  * Caveat emptor: these use single memory cycles to get the
00289  * data from the underlying buffer, and will fail to operate
00290  * properly if the data is not aligned.  The standard way to use these
00291  * is to say:
00292  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
00293  *              return (FALSE);
00294  *      <<< macro calls >>>
00295  * where ``count'' is the number of bytes of data occupied
00296  * by the primitive data types.
00297  *
00298  * N.B. and frozen for all time: each data type here uses 4 bytes
00299  * of external representation.
00300  */
00301 #define IXDR_GET_INT32(buf)             ntohl(*buf++)
00302 #define IXDR_PUT_INT32(buf, v)          (*buf++ = htonl(v))
00303 
00304 #define IXDR_GET_BOOL(buf)              ((bool_t)IXDR_GET_INT32(buf))
00305 #define IXDR_GET_ENUM(buf, t)           ((t)IXDR_GET_INT32(buf))
00306 #define IXDR_GET_U_INT32(buf)           ((afs_uint32)IXDR_GET_INT32(buf))
00307 #define IXDR_GET_SHORT(buf)             ((short)IXDR_GET_INT32(buf))
00308 #define IXDR_GET_U_SHORT(buf)           ((u_short)IXDR_GET_INT32(buf))
00309 
00310 #define IXDR_PUT_BOOL(buf, v)           IXDR_PUT_INT32((buf), ((afs_int32)(v)))
00311 #define IXDR_PUT_ENUM(buf, v)           IXDR_PUT_INT32((buf), ((afs_int32)(v)))
00312 #define IXDR_PUT_U_INT32(buf, v)        IXDR_PUT_INT32((buf), ((afs_int32)(v)))
00313 #define IXDR_PUT_SHORT(buf, v)          IXDR_PUT_INT32((buf), ((afs_int32)(v)))
00314 #define IXDR_PUT_U_SHORT(buf, v)        IXDR_PUT_INT32((buf), ((afs_int32)(v)))
00315 
00316 #include "xdr_prototypes.h"
00317 #endif /* __XDR_INCLUDE__ */
 All Data Structures Files Functions Variables