Path: blob/main/crypto/krb5/src/include/gssrpc/xdr.h
34907 views
/* @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33/* @(#)xdr.h 1.19 87/04/22 SMI */3435/*36* xdr.h, External Data Representation Serialization Routines.37*/3839#ifndef GSSRPC_XDR_H40#define GSSRPC_XDR_H4142#include <stdio.h> /* for FILE */4344GSSRPC__BEGIN_DECLS45/*46* XDR provides a conventional way for converting between C data47* types and an external bit-string representation. Library supplied48* routines provide for the conversion on built-in C data types. These49* routines and utility routines defined here are used to help implement50* a type encode/decode routine for each user-defined type.51*52* Each data type provides a single procedure which takes two arguments:53*54* bool_t55* xdrproc(xdrs, argresp)56* XDR *xdrs;57* <type> *argresp;58*59* xdrs is an instance of a XDR handle, to which or from which the data60* type is to be converted. argresp is a pointer to the structure to be61* converted. The XDR handle contains an operation field which indicates62* which of the operations (ENCODE, DECODE * or FREE) is to be performed.63*64* XDR_DECODE may allocate space if the pointer argresp is null. This65* data can be freed with the XDR_FREE operation.66*67* We write only one procedure per data type to make it easy68* to keep the encode and decode procedures for a data type consistent.69* In many cases the same code performs all operations on a user defined type,70* because all the hard work is done in the component type routines.71* decode as a series of calls on the nested data types.72*/7374/*75* Xdr operations. XDR_ENCODE causes the type to be encoded into the76* stream. XDR_DECODE causes the type to be extracted from the stream.77* XDR_FREE can be used to release the space allocated by an XDR_DECODE78* request.79*/80enum xdr_op {81XDR_ENCODE=0,82XDR_DECODE=1,83XDR_FREE=284};8586/*87* This is the number of bytes per unit of external data.88*/89#define BYTES_PER_XDR_UNIT (4)90#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \91* BYTES_PER_XDR_UNIT)9293/*94* A xdrproc_t exists for each data type which is to be encoded or decoded.95*96* The second argument to the xdrproc_t is a pointer to an opaque pointer.97* The opaque pointer generally points to a structure of the data type98* to be decoded. If this pointer is 0, then the type routines should99* allocate dynamic storage of the appropriate size and return it.100* bool_t (*xdrproc_t)(XDR *, caddr_t *);101*102* XXX can't actually prototype it, because some take three args!!!103*/104105/*106* The XDR handle.107* Contains operation which is being applied to the stream,108* an operations vector for the particular implementation (e.g. see xdr_mem.c),109* and two private fields for the use of the particular impelementation.110*/111typedef struct XDR {112enum xdr_op x_op; /* operation; fast additional param */113struct xdr_ops {114/* get a long from underlying stream */115bool_t (*x_getlong)(struct XDR *, long *);116117/* put a long to underlying stream */118bool_t (*x_putlong)(struct XDR *, long *);119120/* get some bytes from underlying stream */121bool_t (*x_getbytes)(struct XDR *, caddr_t, u_int);122123/* put some bytes to underlying stream */124bool_t (*x_putbytes)(struct XDR *, caddr_t, u_int);125126/* returns bytes off from beginning */127u_int (*x_getpostn)(struct XDR *);128129/* lets you reposition the stream */130bool_t (*x_setpostn)(struct XDR *, u_int);131132/* buf quick ptr to buffered data */133rpc_inline_t *(*x_inline)(struct XDR *, int);134135/* free privates of this xdr_stream */136void (*x_destroy)(struct XDR *);137} *x_ops;138caddr_t x_public; /* users' data */139void * x_private; /* pointer to private data */140caddr_t x_base; /* private used for position info */141int x_handy; /* extra private word */142} XDR;143144typedef bool_t (*xdrproc_t)(XDR *, void *);145146/*147* Operations defined on a XDR handle148*149* XDR *xdrs;150* int32_t *longp;151* caddr_t addr;152* u_int len;153* u_int pos;154*/155#define XDR_GETLONG(xdrs, longp) \156(*(xdrs)->x_ops->x_getlong)(xdrs, longp)157#define xdr_getlong(xdrs, longp) \158(*(xdrs)->x_ops->x_getlong)(xdrs, longp)159160#define XDR_PUTLONG(xdrs, longp) \161(*(xdrs)->x_ops->x_putlong)(xdrs, longp)162#define xdr_putlong(xdrs, longp) \163(*(xdrs)->x_ops->x_putlong)(xdrs, longp)164165#define XDR_GETBYTES(xdrs, addr, len) \166(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)167#define xdr_getbytes(xdrs, addr, len) \168(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)169170#define XDR_PUTBYTES(xdrs, addr, len) \171(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)172#define xdr_putbytes(xdrs, addr, len) \173(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)174175#define XDR_GETPOS(xdrs) \176(*(xdrs)->x_ops->x_getpostn)(xdrs)177#define xdr_getpos(xdrs) \178(*(xdrs)->x_ops->x_getpostn)(xdrs)179180#define XDR_SETPOS(xdrs, pos) \181(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)182#define xdr_setpos(xdrs, pos) \183(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)184185#define XDR_INLINE(xdrs, len) \186(*(xdrs)->x_ops->x_inline)(xdrs, len)187#define xdr_inline(xdrs, len) \188(*(xdrs)->x_ops->x_inline)(xdrs, len)189190#define XDR_DESTROY(xdrs) \191if ((xdrs)->x_ops->x_destroy) \192(*(xdrs)->x_ops->x_destroy)(xdrs)193#define xdr_destroy(xdrs) \194if ((xdrs)->x_ops->x_destroy) \195(*(xdrs)->x_ops->x_destroy)(xdrs)196197/*198* Support struct for discriminated unions.199* You create an array of xdrdiscrim structures, terminated with200* a entry with a null procedure pointer. The xdr_union routine gets201* the discriminant value and then searches the array of structures202* for a matching value. If a match is found the associated xdr routine203* is called to handle that part of the union. If there is204* no match, then a default routine may be called.205* If there is no match and no default routine it is an error.206*/207#define NULL_xdrproc_t ((xdrproc_t)0)208struct xdr_discrim {209int value;210xdrproc_t proc;211};212213/*214* In-line routines for fast encode/decode of primitive data types.215* Caveat emptor: these use single memory cycles to get the216* data from the underlying buffer, and will fail to operate217* properly if the data is not aligned. The standard way to use these218* is to say:219* if ((buf = XDR_INLINE(xdrs, count)) == NULL)220* return (FALSE);221* <<< macro calls >>>222* where ``count'' is the number of bytes of data occupied223* by the primitive data types.224*225* N.B. and frozen for all time: each data type here uses 4 bytes226* of external representation.227*/228#define IXDR_GET_INT32(buf) ((int32_t)IXDR_GET_U_INT32(buf))229#define IXDR_PUT_INT32(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))230#define IXDR_GET_U_INT32(buf) (ntohl((uint32_t)*(buf)++))231#define IXDR_PUT_U_INT32(buf, v) (*(buf)++ = (int32_t)htonl((v)))232233#define IXDR_GET_LONG(buf) ((long)IXDR_GET_INT32(buf))234#define IXDR_PUT_LONG(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))235236#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))237#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_INT32(buf))238#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_U_INT32(buf))239#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_INT32(buf))240#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_U_INT32(buf))241242#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_INT32((buf),((int32_t)(v)))243#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_INT32((buf),((int32_t)(v)))244#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))245#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_INT32((buf),((int32_t)(v)))246#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))247248/*249* These are the "generic" xdr routines.250*/251extern bool_t xdr_void(XDR *, void *);252extern bool_t xdr_int(XDR *, int *);253extern bool_t xdr_u_int(XDR *, u_int *);254extern bool_t xdr_long(XDR *, long *);255extern bool_t xdr_u_long(XDR *, u_long *);256extern bool_t xdr_short(XDR *, short *);257extern bool_t xdr_u_short(XDR *, u_short *);258extern bool_t xdr_bool(XDR *, bool_t *);259extern bool_t xdr_enum(XDR *, enum_t *);260extern bool_t xdr_array(XDR *, caddr_t *, u_int *,261u_int, u_int, xdrproc_t);262extern bool_t xdr_bytes(XDR *, char **, u_int *, u_int);263extern bool_t xdr_opaque(XDR *, caddr_t, u_int);264extern bool_t xdr_string(XDR *, char **, u_int);265extern bool_t xdr_union(XDR *, enum_t *, char *, struct xdr_discrim *,266xdrproc_t);267extern bool_t xdr_char(XDR *, char *);268extern bool_t xdr_u_char(XDR *, u_char *);269extern bool_t xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);270extern bool_t xdr_float(XDR *, float *);271extern bool_t xdr_double(XDR *, double *);272extern bool_t xdr_reference(XDR *, caddr_t *, u_int, xdrproc_t);273extern bool_t xdr_pointer(XDR *, char **, u_int, xdrproc_t);274extern bool_t xdr_wrapstring(XDR *, char **);275276extern unsigned long xdr_sizeof(xdrproc_t, void *);277278#define xdr_rpcprog xdr_u_int32279#define xdr_rpcvers xdr_u_int32280#define xdr_rpcprot xdr_u_int32281#define xdr_rpcproc xdr_u_int32282#define xdr_rpcport xdr_u_int32283284/*285* Common opaque bytes objects used by many rpc protocols;286* declared here due to commonality.287*/288#define MAX_NETOBJ_SZ 2048289struct netobj {290u_int n_len;291char *n_bytes;292};293typedef struct netobj netobj;294295extern bool_t xdr_netobj(XDR *, struct netobj *);296297extern bool_t xdr_int32(XDR *, int32_t *);298extern bool_t xdr_u_int32(XDR *, uint32_t *);299300/*301* These are the public routines for the various implementations of302* xdr streams.303*/304305/* XDR allocating memory buffer */306extern void xdralloc_create(XDR *, enum xdr_op);307308/* destroy xdralloc, save buf */309extern void xdralloc_release(XDR *);310311/* get buffer from xdralloc */312extern caddr_t xdralloc_getdata(XDR *);313314/* XDR using memory buffers */315extern void xdrmem_create(XDR *, caddr_t, u_int, enum xdr_op);316317/* XDR using stdio library */318extern void xdrstdio_create(XDR *, FILE *, enum xdr_op);319320/* XDR pseudo records for tcp */321extern void xdrrec_create(XDR *xdrs, u_int, u_int, caddr_t,322int (*) (caddr_t, caddr_t, int),323int (*) (caddr_t, caddr_t, int));324325/* make end of xdr record */326extern bool_t xdrrec_endofrecord(XDR *, bool_t);327328/* move to beginning of next record */329extern bool_t xdrrec_skiprecord (XDR *xdrs);330331/* true if no more input */332extern bool_t xdrrec_eof (XDR *xdrs);333334/* free memory buffers for xdr */335extern void xdr_free (xdrproc_t, void *);336GSSRPC__END_DECLS337338#endif /* !defined(GSSRPC_XDR_H) */339340341