Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:02

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