1/* 2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights 7 * Reserved. This file contains Original Code and/or Modifications of 8 * Original Code as defined in and that are subject to the Apple Public 9 * Source License Version 1.1 (the "License"). You may not use this file 10 * except in compliance with the License. Please obtain a copy of the 11 * License at http://www.apple.com/publicsource and read it before using 12 * this file. 13 * 14 * The Original Code and all software distributed under the License are 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the 19 * License for the specific language governing rights and limitations 20 * under the License. 21 * 22 * @APPLE_LICENSE_HEADER_END@ 23 */ 24/* 25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 26 * unrestricted use provided that this legend is included on all tape 27 * media and as a part of the software program in whole or part. Users 28 * may copy or modify Sun RPC without charge, but are not authorized 29 * to license or distribute it to anyone else except as part of a product or 30 * program developed by the user. 31 * 32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 33 * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 35 * 36 * Sun RPC is provided with no support and without any obligation on the 37 * part of Sun Microsystems, Inc. to assist in its use, correction, 38 * modification or enhancement. 39 * 40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 42 * OR ANY PART THEREOF. 43 * 44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 45 * or profits or other special, indirect and consequential damages, even if 46 * Sun has been advised of the possibility of such damages. 47 * 48 * Sun Microsystems, Inc. 49 * 2550 Garcia Avenue 50 * Mountain View, California 94043 51 * 52 * from: @(#)xdr.h 1.19 87/04/22 SMI 53 * from: @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC 54 * $FreeBSD: src/include/rpc/xdr.h,v 1.23 2003/03/07 13:19:40 nectar Exp $ 55 */ 56 57/* 58 * xdr.h, External Data Representation Serialization Routines. 59 * 60 * Copyright (C) 1984, Sun Microsystems, Inc. 61 */ 62 63#ifndef _RPC_XDR_H 64#define _RPC_XDR_H 65#include <sys/cdefs.h> 66 67/* 68 * XDR provides a conventional way for converting between C data 69 * types and an external bit-string representation. Library supplied 70 * routines provide for the conversion on built-in C data types. These 71 * routines and utility routines defined here are used to help implement 72 * a type encode/decode routine for each user-defined type. 73 * 74 * Each data type provides a single procedure which takes two arguments: 75 * 76 * bool_t 77 * xdrproc(xdrs, argresp) 78 * XDR *xdrs; 79 * <type> *argresp; 80 * 81 * xdrs is an instance of a XDR handle, to which or from which the data 82 * type is to be converted. argresp is a pointer to the structure to be 83 * converted. The XDR handle contains an operation field which indicates 84 * which of the operations (ENCODE, DECODE * or FREE) is to be performed. 85 * 86 * XDR_DECODE may allocate space if the pointer argresp is null. This 87 * data can be freed with the XDR_FREE operation. 88 * 89 * We write only one procedure per data type to make it easy 90 * to keep the encode and decode procedures for a data type consistent. 91 * In many cases the same code performs all operations on a user defined type, 92 * because all the hard work is done in the component type routines. 93 * decode as a series of calls on the nested data types. 94 */ 95 96/* 97 * Xdr operations. XDR_ENCODE causes the type to be encoded into the 98 * stream. XDR_DECODE causes the type to be extracted from the stream. 99 * XDR_FREE can be used to release the space allocated by an XDR_DECODE 100 * request. 101 */ 102enum xdr_op { 103 XDR_ENCODE=0, 104 XDR_DECODE=1, 105 XDR_FREE=2 106}; 107 108/* 109 * This is the number of bytes per unit of external data. 110 */ 111#define BYTES_PER_XDR_UNIT (4) 112#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \ 113 * BYTES_PER_XDR_UNIT) 114 115/* 116 * The XDR handle. 117 * Contains operation which is being applied to the stream, 118 * an operations vector for the particular implementation (e.g. see xdr_mem.c), 119 * and two private fields for the use of the particular implementation. 120 */ 121typedef struct __rpc_xdr { 122 enum xdr_op x_op; /* operation; fast additional param */ 123 const struct xdr_ops { 124#ifdef __LP64__ 125 /* get an int from underlying stream */ 126 bool_t (*x_getlong)(struct __rpc_xdr *, int *); 127 /* put an int to " */ 128 bool_t (*x_putlong)(struct __rpc_xdr *, const int *); 129#else 130 /* get a long from underlying stream */ 131 bool_t (*x_getlong)(struct __rpc_xdr *, long *); 132 /* put a long to " */ 133 bool_t (*x_putlong)(struct __rpc_xdr *, const long *); 134#endif 135 /* get some bytes from " */ 136 bool_t (*x_getbytes)(struct __rpc_xdr *, char *, unsigned int); 137 /* put some bytes to " */ 138 bool_t (*x_putbytes)(struct __rpc_xdr *, const char *, unsigned int); 139 /* returns bytes off from beginning */ 140 unsigned int (*x_getpostn)(struct __rpc_xdr *); 141 /* lets you reposition the stream */ 142 bool_t (*x_setpostn)(struct __rpc_xdr *, unsigned int); 143 /* buf quick ptr to buffered data */ 144 int32_t *(*x_inline)(struct __rpc_xdr *, unsigned int); 145 /* free privates of this xdr_stream */ 146 void (*x_destroy)(struct __rpc_xdr *); 147 bool_t (*x_control)(struct __rpc_xdr *, int, void *); 148 } *x_ops; 149 char * x_public; /* users' data */ 150 void * x_private; /* pointer to private data */ 151 char * x_base; /* private used for position info */ 152 unsigned int x_handy; /* extra private word */ 153} XDR; 154 155/* 156 * A xdrproc_t exists for each data type which is to be encoded or decoded. 157 * 158 * The second argument to the xdrproc_t is a pointer to an opaque pointer. 159 * The opaque pointer generally points to a structure of the data type 160 * to be decoded. If this pointer is 0, then the type routines should 161 * allocate dynamic storage of the appropriate size and return it. 162 * 163 * 164 * IMPORTANT NOTE: Apple iOS and OS X 165 * 166 * Previous versions of this header file defined the xdrproc_t prototype as 167 * typedef bool_t (*xdrproc_t)(XDR *, ...); 168 * 169 * This prototype is incorrect. One may not use a varargs function pointer 170 * in place of pointer to a function with positional parameters. 171 * This mistake has been masked by the fact that clients of this API would 172 * generally cast their xdr functions as (xdrproc_t), and thus silence compiler 173 * warnings. The code worked because the ABI for varargs routines that pass a 174 * small number of arguments has been the same as the ABI for routines with a 175 * few positional parameters. However, if the ABI differs this will cause the 176 * compiled code to fail. 177 * 178 * Historically, some client xdr routines expected three parameters. This 179 * does not seem to be the case in more recent code, but we have decided to 180 * retain this definition in the XDR library in case some legacy code still 181 * expects three parameters. The library will pass zero as the third 182 * parameter. Routines that expect two parameters will work correctly. 183 * 184 * If your client-side xdr routine is of the form: 185 * bool_t xdr_my_datatype(XDR *, void *); 186 * and you pass your function pointer to routines like xdr_pointer or 187 * xdr_reference using "(xdrproc_t)xdr_my_datatype", then your code will 188 * compile and run correctly. If your code invokes an xdrproc_t callback, 189 * it must be modified to pass a third parameter, which may simply be zero. 190 */ 191typedef bool_t (*xdrproc_t)(XDR *, void *, unsigned int); 192 193/* 194 * Operations defined on a XDR handle 195 * 196 * XDR *xdrs; 197 * long *longp; 198 * char * addr; 199 * unsigned int len; 200 * unsigned int pos; 201 */ 202#define XDR_GETLONG(xdrs, longp) \ 203 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 204#define xdr_getlong(xdrs, longp) \ 205 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 206 207#define XDR_PUTLONG(xdrs, longp) \ 208 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 209#define xdr_putlong(xdrs, longp) \ 210 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 211 212 213#ifdef __LP64__ 214static __inline int 215xdr_getint32(XDR *xdrs, int32_t *ip) 216{ 217 int32_t l; 218 219 if (!xdr_getlong(xdrs, &l)) 220 return (FALSE); 221 *ip = l; 222 return (TRUE); 223} 224 225static __inline int 226xdr_putint32(XDR *xdrs, int32_t *ip) 227{ 228 int32_t l; 229 230 l = *ip; 231 return xdr_putlong(xdrs, &l); 232} 233#else 234static __inline int 235xdr_getint32(XDR *xdrs, int32_t *ip) 236{ 237 int32_t l; 238 239 if (!xdr_getlong(xdrs, (long *)&l)) 240 return (FALSE); 241 *ip = l; 242 return (TRUE); 243} 244 245static __inline int 246xdr_putint32(XDR *xdrs, int32_t *ip) 247{ 248 int32_t l; 249 250 l = *ip; 251 return xdr_putlong(xdrs, (long *)&l); 252} 253#endif 254 255#define XDR_GETINT32(xdrs, int32p) xdr_getint32(xdrs, int32p) 256#define XDR_PUTINT32(xdrs, int32p) xdr_putint32(xdrs, int32p) 257 258#define XDR_GETBYTES(xdrs, addr, len) \ 259 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 260#define xdr_getbytes(xdrs, addr, len) \ 261 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 262 263#define XDR_PUTBYTES(xdrs, addr, len) \ 264 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 265#define xdr_putbytes(xdrs, addr, len) \ 266 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 267 268#define XDR_GETPOS(xdrs) \ 269 (*(xdrs)->x_ops->x_getpostn)(xdrs) 270#define xdr_getpos(xdrs) \ 271 (*(xdrs)->x_ops->x_getpostn)(xdrs) 272 273#define XDR_SETPOS(xdrs, pos) \ 274 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 275#define xdr_setpos(xdrs, pos) \ 276 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 277 278#define XDR_INLINE(xdrs, len) \ 279 (*(xdrs)->x_ops->x_inline)(xdrs, len) 280#define xdr_inline(xdrs, len) \ 281 (*(xdrs)->x_ops->x_inline)(xdrs, len) 282 283#define XDR_DESTROY(xdrs) \ 284 if ((xdrs)->x_ops->x_destroy) \ 285 (*(xdrs)->x_ops->x_destroy)(xdrs) 286#define xdr_destroy(xdrs) \ 287 if ((xdrs)->x_ops->x_destroy) \ 288 (*(xdrs)->x_ops->x_destroy)(xdrs) 289 290#define XDR_CONTROL(xdrs, req, op) \ 291 if ((xdrs)->x_ops->x_control) \ 292 (*(xdrs)->x_ops->x_control)(xdrs, req, op) 293#define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op) 294 295/* 296 * Solaris strips the '_t' from these types -- not sure why. 297 * But, let's be compatible. 298 */ 299#define xdr_rpcvers(xdrs, versp) xdr_u_int32(xdrs, versp) 300#define xdr_rpcprog(xdrs, progp) xdr_u_int32(xdrs, progp) 301#define xdr_rpcproc(xdrs, procp) xdr_u_int32(xdrs, procp) 302#define xdr_rpcprot(xdrs, protp) xdr_u_int32(xdrs, protp) 303#define xdr_rpcport(xdrs, portp) xdr_u_int32(xdrs, portp) 304 305/* 306 * Support struct for discriminated unions. 307 * You create an array of xdrdiscrim structures, terminated with 308 * an entry with a null procedure pointer. The xdr_union routine gets 309 * the discriminant value and then searches the array of structures 310 * for a matching value. If a match is found the associated xdr routine 311 * is called to handle that part of the union. If there is 312 * no match, then a default routine may be called. 313 * If there is no match and no default routine it is an error. 314 */ 315#define NULL_xdrproc_t ((xdrproc_t)0) 316struct xdr_discrim { 317 int value; 318 xdrproc_t proc; 319}; 320 321/* 322 * In-line routines for fast encode/decode of primitive data types. 323 * Caveat emptor: these use single memory cycles to get the 324 * data from the underlying buffer, and will fail to operate 325 * properly if the data is not aligned. The standard way to use these 326 * is to say: 327 * if ((buf = XDR_INLINE(xdrs, count)) == NULL) 328 * return (FALSE); 329 * <<< macro calls >>> 330 * where ``count'' is the number of bytes of data occupied 331 * by the primitive data types. 332 * 333 * N.B. and frozen for all time: each data type here uses 4 bytes 334 * of external representation. 335 */ 336#define IXDR_GET_INT32(buf) ((int32_t)ntohl((u_int32_t)*(buf)++)) 337#define IXDR_PUT_INT32(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v)) 338#define IXDR_GET_U_INT32(buf) ((u_int32_t)IXDR_GET_INT32(buf)) 339#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32((buf), ((int32_t)(v))) 340 341#ifdef __LP64__ 342#define IXDR_GET_LONG(buf) (ntohl((u_int32_t)*(buf)++)) 343#define IXDR_PUT_LONG(buf, v) (*(buf)++ = htonl((u_int32_t)v)) 344#else 345#define IXDR_GET_LONG(buf) ((long)ntohl((u_int32_t)*(buf)++)) 346#define IXDR_PUT_LONG(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v)) 347#endif 348 349#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf)) 350#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf)) 351#ifdef __LP64__ 352#define IXDR_GET_U_LONG(buf) ((unsigned int)IXDR_GET_LONG(buf)) 353#else 354#define IXDR_GET_U_LONG(buf) ((unsigned long)IXDR_GET_LONG(buf)) 355#endif 356#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf)) 357#define IXDR_GET_U_SHORT(buf) ((unsigned short)IXDR_GET_LONG(buf)) 358 359#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), (v)) 360#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), (v)) 361#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), (v)) 362#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), (v)) 363#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), (v)) 364 365/* 366 * These are the "generic" xdr routines. 367 */ 368__BEGIN_DECLS 369extern bool_t xdr_void(void); 370extern bool_t xdr_int(XDR *, int *); 371extern bool_t xdr_u_int(XDR *, unsigned int *); 372#ifdef __LP64__ 373extern bool_t xdr_long(XDR *, int *); 374extern bool_t xdr_u_long(XDR *, unsigned int *); 375#else 376extern bool_t xdr_long(XDR *, long *); 377extern bool_t xdr_u_long(XDR *, unsigned long *); 378#endif 379extern bool_t xdr_short(XDR *, short *); 380extern bool_t xdr_u_short(XDR *, unsigned short *); 381extern bool_t xdr_int16_t(XDR *, int16_t *); 382extern bool_t xdr_u_int16_t(XDR *, u_int16_t *); 383extern bool_t xdr_int32_t(XDR *, int32_t *); 384extern bool_t xdr_u_int32_t(XDR *, u_int32_t *); 385extern bool_t xdr_int64_t(XDR *, int64_t *); 386extern bool_t xdr_u_int64_t(XDR *, u_int64_t *); 387extern bool_t xdr_bool(XDR *, bool_t *); 388extern bool_t xdr_enum(XDR *, enum_t *); 389extern bool_t xdr_array(XDR *, char **, unsigned int *, unsigned int, unsigned int, xdrproc_t); 390extern bool_t xdr_bytes(XDR *, char **, unsigned int *, unsigned int); 391extern bool_t xdr_opaque(XDR *, char *, unsigned int); 392extern bool_t xdr_string(XDR *, char **, unsigned int); 393extern bool_t xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t); 394extern bool_t xdr_char(XDR *, char *); 395extern bool_t xdr_u_char(XDR *, unsigned char *); 396extern bool_t xdr_vector(XDR *, char *, unsigned int, unsigned int, xdrproc_t); 397extern bool_t xdr_float(XDR *, float *); 398extern bool_t xdr_double(XDR *, double *); 399extern bool_t xdr_quadruple(XDR *, long double *); 400extern bool_t xdr_reference(XDR *, char **, unsigned int, xdrproc_t); 401extern bool_t xdr_pointer(XDR *, char **, unsigned int, xdrproc_t); 402extern bool_t xdr_wrapstring(XDR *, char **); 403extern void xdr_free(xdrproc_t, void *); 404extern bool_t xdr_hyper(XDR *, quad_t *); 405extern bool_t xdr_u_hyper(XDR *, u_quad_t *); 406extern bool_t xdr_longlong_t(XDR *, quad_t *); 407extern bool_t xdr_u_longlong_t(XDR *, u_quad_t *); 408__END_DECLS 409 410/* 411 * Common opaque bytes objects used by many rpc protocols; 412 * declared here due to commonality. 413 */ 414#define MAX_NETOBJ_SZ 1024 415struct netobj { 416 unsigned int n_len; 417 char *n_bytes; 418}; 419typedef struct netobj netobj; 420extern bool_t xdr_netobj(XDR *, struct netobj *); 421 422/* 423 * These are the public routines for the various implementations of 424 * xdr streams. 425 */ 426__BEGIN_DECLS 427/* XDR using memory buffers */ 428extern void xdrmem_create(XDR *, char *, unsigned int, enum xdr_op); 429 430/* XDR using stdio library */ 431#ifdef _STDIO_H_ 432extern void xdrstdio_create(XDR *, FILE *, enum xdr_op); 433#endif 434 435/* XDR pseudo records for tcp */ 436extern void xdrrec_create(XDR *, unsigned int, unsigned int, void *, 437 int (*)(void *, void *, int), 438 int (*)(void *, void *, int)); 439 440/* make end of xdr record */ 441extern bool_t xdrrec_endofrecord(XDR *, int); 442 443/* move to beginning of next record */ 444extern bool_t xdrrec_skiprecord(XDR *); 445 446/* true if no more input */ 447extern bool_t xdrrec_eof(XDR *); 448extern unsigned int xdrrec_readbytes(XDR *, caddr_t, unsigned int); 449__END_DECLS 450 451#endif /* !_RPC_XDR_H */ 452