Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:04

0001 /* @(#)xdr.h    2.2 88/07/29 4.0 RPCSRC */
0002 /*
0003  * Copyright (c) 2010, Oracle America, Inc.
0004  *
0005  * All rights reserved.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions are met:
0009  *
0010  *     * Redistributions of source code must retain the above copyright
0011  *       notice, this list of conditions and the following disclaimer.
0012  *
0013  *     * Redistributions in binary form must reproduce the above copyright
0014  *       notice, this list of conditions and the following disclaimer in
0015  *       the documentation and/or other materials provided with the
0016  *       distribution.
0017  *
0018  *     * Neither the name of the "Oracle America, Inc." nor the names of
0019  *       its contributors may be used to endorse or promote products
0020  *       derived from this software without specific prior written permission.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
0024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
0025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0026  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
0028  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 /*      @(#)xdr.h 1.19 87/04/22 SMI      */
0035 
0036 /*
0037  * xdr.h, External Data Representation Serialization Routines.
0038  */
0039 
0040 #ifndef GSSRPC_XDR_H
0041 #define GSSRPC_XDR_H
0042 
0043 #include <stdio.h>      /* for FILE */
0044 
0045 GSSRPC__BEGIN_DECLS
0046 /*
0047  * XDR provides a conventional way for converting between C data
0048  * types and an external bit-string representation.  Library supplied
0049  * routines provide for the conversion on built-in C data types.  These
0050  * routines and utility routines defined here are used to help implement
0051  * a type encode/decode routine for each user-defined type.
0052  *
0053  * Each data type provides a single procedure which takes two arguments:
0054  *
0055  *  bool_t
0056  *  xdrproc(xdrs, argresp)
0057  *      XDR *xdrs;
0058  *      <type> *argresp;
0059  *
0060  * xdrs is an instance of a XDR handle, to which or from which the data
0061  * type is to be converted.  argresp is a pointer to the structure to be
0062  * converted.  The XDR handle contains an operation field which indicates
0063  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
0064  *
0065  * XDR_DECODE may allocate space if the pointer argresp is null.  This
0066  * data can be freed with the XDR_FREE operation.
0067  *
0068  * We write only one procedure per data type to make it easy
0069  * to keep the encode and decode procedures for a data type consistent.
0070  * In many cases the same code performs all operations on a user defined type,
0071  * because all the hard work is done in the component type routines.
0072  * decode as a series of calls on the nested data types.
0073  */
0074 
0075 /*
0076  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
0077  * stream.  XDR_DECODE causes the type to be extracted from the stream.
0078  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
0079  * request.
0080  */
0081 enum xdr_op {
0082     XDR_ENCODE=0,
0083     XDR_DECODE=1,
0084     XDR_FREE=2
0085 };
0086 
0087 /*
0088  * This is the number of bytes per unit of external data.
0089  */
0090 #define BYTES_PER_XDR_UNIT  (4)
0091 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
0092             * BYTES_PER_XDR_UNIT)
0093 
0094 /*
0095  * A xdrproc_t exists for each data type which is to be encoded or decoded.
0096  *
0097  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
0098  * The opaque pointer generally points to a structure of the data type
0099  * to be decoded.  If this pointer is 0, then the type routines should
0100  * allocate dynamic storage of the appropriate size and return it.
0101  * bool_t   (*xdrproc_t)(XDR *, caddr_t *);
0102  *
0103  * XXX can't actually prototype it, because some take three args!!!
0104  */
0105 typedef bool_t (*xdrproc_t)();
0106 
0107 /*
0108  * The XDR handle.
0109  * Contains operation which is being applied to the stream,
0110  * an operations vector for the particular implementation (e.g. see xdr_mem.c),
0111  * and two private fields for the use of the particular impelementation.
0112  */
0113 typedef struct XDR {
0114     enum xdr_op x_op;       /* operation; fast additional param */
0115     struct xdr_ops {
0116         /* get a long from underlying stream */
0117         bool_t  (*x_getlong)(struct XDR *, long *);
0118 
0119         /* put a long to underlying stream */
0120         bool_t  (*x_putlong)(struct XDR *, long *);
0121 
0122         /* get some bytes from underlying stream */
0123         bool_t  (*x_getbytes)(struct XDR *, caddr_t, u_int);
0124 
0125         /* put some bytes to underlying stream */
0126         bool_t  (*x_putbytes)(struct XDR *, caddr_t, u_int);
0127 
0128         /* returns bytes off from beginning */
0129         u_int   (*x_getpostn)(struct XDR *);
0130 
0131         /* lets you reposition the stream */
0132         bool_t  (*x_setpostn)(struct XDR *, u_int);
0133 
0134         /* buf quick ptr to buffered data */
0135         rpc_inline_t *(*x_inline)(struct XDR *, int);
0136 
0137         /* free privates of this xdr_stream */
0138         void    (*x_destroy)(struct XDR *);
0139     } *x_ops;
0140     caddr_t     x_public;   /* users' data */
0141     void *      x_private;  /* pointer to private data */
0142     caddr_t     x_base;     /* private used for position info */
0143     int     x_handy;    /* extra private word */
0144 } XDR;
0145 
0146 /*
0147  * Operations defined on a XDR handle
0148  *
0149  * XDR      *xdrs;
0150  * int32_t  *longp;
0151  * caddr_t   addr;
0152  * u_int     len;
0153  * u_int     pos;
0154  */
0155 #define XDR_GETLONG(xdrs, longp)            \
0156     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
0157 #define xdr_getlong(xdrs, longp)            \
0158     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
0159 
0160 #define XDR_PUTLONG(xdrs, longp)            \
0161     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
0162 #define xdr_putlong(xdrs, longp)            \
0163     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
0164 
0165 #define XDR_GETBYTES(xdrs, addr, len)           \
0166     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
0167 #define xdr_getbytes(xdrs, addr, len)           \
0168     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
0169 
0170 #define XDR_PUTBYTES(xdrs, addr, len)           \
0171     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
0172 #define xdr_putbytes(xdrs, addr, len)           \
0173     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
0174 
0175 #define XDR_GETPOS(xdrs)                \
0176     (*(xdrs)->x_ops->x_getpostn)(xdrs)
0177 #define xdr_getpos(xdrs)                \
0178     (*(xdrs)->x_ops->x_getpostn)(xdrs)
0179 
0180 #define XDR_SETPOS(xdrs, pos)               \
0181     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
0182 #define xdr_setpos(xdrs, pos)               \
0183     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
0184 
0185 #define XDR_INLINE(xdrs, len)               \
0186     (*(xdrs)->x_ops->x_inline)(xdrs, len)
0187 #define xdr_inline(xdrs, len)               \
0188     (*(xdrs)->x_ops->x_inline)(xdrs, len)
0189 
0190 #define XDR_DESTROY(xdrs)               \
0191     if ((xdrs)->x_ops->x_destroy)           \
0192         (*(xdrs)->x_ops->x_destroy)(xdrs)
0193 #define xdr_destroy(xdrs)               \
0194     if ((xdrs)->x_ops->x_destroy)           \
0195         (*(xdrs)->x_ops->x_destroy)(xdrs)
0196 
0197 /*
0198  * Support struct for discriminated unions.
0199  * You create an array of xdrdiscrim structures, terminated with
0200  * a entry with a null procedure pointer.  The xdr_union routine gets
0201  * the discriminant value and then searches the array of structures
0202  * for a matching value.  If a match is found the associated xdr routine
0203  * is called to handle that part of the union.  If there is
0204  * no match, then a default routine may be called.
0205  * If there is no match and no default routine it is an error.
0206  */
0207 #define NULL_xdrproc_t ((xdrproc_t)0)
0208 struct xdr_discrim {
0209     int value;
0210     xdrproc_t proc;
0211 };
0212 
0213 /*
0214  * In-line routines for fast encode/decode of primitive data types.
0215  * Caveat emptor: these use single memory cycles to get the
0216  * data from the underlying buffer, and will fail to operate
0217  * properly if the data is not aligned.  The standard way to use these
0218  * is to say:
0219  *  if ((buf = XDR_INLINE(xdrs, count)) == NULL)
0220  *      return (FALSE);
0221  *  <<< macro calls >>>
0222  * where ``count'' is the number of bytes of data occupied
0223  * by the primitive data types.
0224  *
0225  * N.B. and frozen for all time: each data type here uses 4 bytes
0226  * of external representation.
0227  */
0228 #define IXDR_GET_INT32(buf)     ((int32_t)IXDR_GET_U_INT32(buf))
0229 #define IXDR_PUT_INT32(buf, v)      IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
0230 #define IXDR_GET_U_INT32(buf)       (ntohl((uint32_t)*(buf)++))
0231 #define IXDR_PUT_U_INT32(buf, v)    (*(buf)++ = (int32_t)htonl((v)))
0232 
0233 #define IXDR_GET_LONG(buf)      ((long)IXDR_GET_INT32(buf))
0234 #define IXDR_PUT_LONG(buf, v)       IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
0235 
0236 #define IXDR_GET_BOOL(buf)      ((bool_t)IXDR_GET_LONG(buf))
0237 #define IXDR_GET_ENUM(buf, t)       ((t)IXDR_GET_INT32(buf))
0238 #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_U_INT32(buf))
0239 #define IXDR_GET_SHORT(buf)     ((short)IXDR_GET_INT32(buf))
0240 #define IXDR_GET_U_SHORT(buf)       ((u_short)IXDR_GET_U_INT32(buf))
0241 
0242 #define IXDR_PUT_BOOL(buf, v)       IXDR_PUT_INT32((buf),((int32_t)(v)))
0243 #define IXDR_PUT_ENUM(buf, v)       IXDR_PUT_INT32((buf),((int32_t)(v)))
0244 #define IXDR_PUT_U_LONG(buf, v)     IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
0245 #define IXDR_PUT_SHORT(buf, v)      IXDR_PUT_INT32((buf),((int32_t)(v)))
0246 #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
0247 
0248 /*
0249  * These are the "generic" xdr routines.
0250  */
0251 extern bool_t   xdr_void(XDR *, void *);
0252 extern bool_t   xdr_int(XDR *, int *);
0253 extern bool_t   xdr_u_int(XDR *, u_int *);
0254 extern bool_t   xdr_long(XDR *, long *);
0255 extern bool_t   xdr_u_long(XDR *, u_long *);
0256 extern bool_t   xdr_short(XDR *, short *);
0257 extern bool_t   xdr_u_short(XDR *, u_short *);
0258 extern bool_t   xdr_bool(XDR *, bool_t *);
0259 extern bool_t   xdr_enum(XDR *, enum_t *);
0260 extern bool_t   xdr_array(XDR *, caddr_t *, u_int *,
0261             u_int, u_int, xdrproc_t);
0262 extern bool_t   xdr_bytes(XDR *, char **, u_int *, u_int);
0263 extern bool_t   xdr_opaque(XDR *, caddr_t, u_int);
0264 extern bool_t   xdr_string(XDR *, char **, u_int);
0265 extern bool_t   xdr_union(XDR *, enum_t *, char *, struct xdr_discrim *,
0266             xdrproc_t);
0267 extern bool_t   xdr_char(XDR *, char *);
0268 extern bool_t   xdr_u_char(XDR *, u_char *);
0269 extern bool_t   xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
0270 extern bool_t   xdr_float(XDR *, float *);
0271 extern bool_t   xdr_double(XDR *, double *);
0272 extern bool_t   xdr_reference(XDR *, caddr_t *, u_int, xdrproc_t);
0273 extern bool_t   xdr_pointer(XDR *, char **, u_int, xdrproc_t);
0274 extern bool_t   xdr_wrapstring(XDR *, char **);
0275 
0276 extern unsigned long xdr_sizeof(xdrproc_t, void *);
0277 
0278 #define xdr_rpcprog xdr_u_int32
0279 #define xdr_rpcvers xdr_u_int32
0280 #define xdr_rpcprot xdr_u_int32
0281 #define xdr_rpcproc xdr_u_int32
0282 #define xdr_rpcport xdr_u_int32
0283 
0284 /*
0285  * Common opaque bytes objects used by many rpc protocols;
0286  * declared here due to commonality.
0287  */
0288 #define MAX_NETOBJ_SZ 2048
0289 struct netobj {
0290     u_int   n_len;
0291     char    *n_bytes;
0292 };
0293 typedef struct netobj netobj;
0294 
0295 extern bool_t   xdr_netobj(XDR *, struct netobj *);
0296 
0297 extern bool_t   xdr_int32(XDR *, int32_t *);
0298 extern bool_t   xdr_u_int32(XDR *, uint32_t *);
0299 
0300 /*
0301  * These are the public routines for the various implementations of
0302  * xdr streams.
0303  */
0304 
0305 /* XDR allocating memory buffer */
0306 extern void xdralloc_create(XDR *, enum xdr_op);
0307 
0308 /* destroy xdralloc, save buf */
0309 extern void xdralloc_release(XDR *);
0310 
0311 /* get buffer from xdralloc */
0312 extern caddr_t  xdralloc_getdata(XDR *);
0313 
0314 /* XDR using memory buffers */
0315 extern void xdrmem_create(XDR *, caddr_t, u_int, enum xdr_op);
0316 
0317 /* XDR using stdio library */
0318 extern void xdrstdio_create(XDR *, FILE *, enum xdr_op);
0319 
0320 /* XDR pseudo records for tcp */
0321 extern void xdrrec_create(XDR *xdrs, u_int, u_int, caddr_t,
0322             int (*) (caddr_t, caddr_t, int),
0323             int (*) (caddr_t, caddr_t, int));
0324 
0325 /* make end of xdr record */
0326 extern bool_t   xdrrec_endofrecord(XDR *, bool_t);
0327 
0328 /* move to beginning of next record */
0329 extern bool_t   xdrrec_skiprecord (XDR *xdrs);
0330 
0331 /* true if no more input */
0332 extern bool_t   xdrrec_eof (XDR *xdrs);
0333 
0334 /* free memory buffers for xdr */
0335 extern void xdr_free (xdrproc_t, void *);
0336 GSSRPC__END_DECLS
0337 
0338 #endif /* !defined(GSSRPC_XDR_H) */