]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr.h
ff46e001c383b7cfae467dd1e4adb61fbc89a76e
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
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.
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.
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.
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.
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.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
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 $
58 * xdr.h, External Data Representation Serialization Routines.
60 * Copyright (C) 1984, Sun Microsystems, Inc.
65 #include <sys/cdefs.h>
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.
74 * Each data type provides a single procedure which takes two arguments:
77 * xdrproc(xdrs, argresp)
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.
86 * XDR_DECODE may allocate space if the pointer argresp is null. This
87 * data can be freed with the XDR_FREE operation.
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.
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
109 * This is the number of bytes per unit of external data.
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)
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.
121 typedef struct __rpc_xdr
{
122 enum xdr_op x_op
; /* operation; fast additional param */
123 const struct xdr_ops
{
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 *);
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 *);
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 *);
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 */
156 * A xdrproc_t exists for each data type which is to be encoded or decoded.
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.
164 * IMPORTANT NOTE: Apple iOS and OS X
166 * Previous versions of this header file defined the xdrproc_t prototype as
167 * typedef bool_t (*xdrproc_t)(XDR *, ...);
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.
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.
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.
191 typedef bool_t (*xdrproc_t
)(XDR
*, void *, unsigned int);
194 * Operations defined on a XDR handle
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)
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)
215 xdr_getint32(XDR
*xdrs
, int32_t *ip
)
219 if (!xdr_getlong(xdrs
, &l
))
226 xdr_putint32(XDR
*xdrs
, int32_t *ip
)
231 return xdr_putlong(xdrs
, &l
);
235 xdr_getint32(XDR
*xdrs
, int32_t *ip
)
239 if (!xdr_getlong(xdrs
, (long *)&l
))
246 xdr_putint32(XDR
*xdrs
, int32_t *ip
)
251 return xdr_putlong(xdrs
, (long *)&l
);
255 #define XDR_GETINT32(xdrs, int32p) xdr_getint32(xdrs, int32p)
256 #define XDR_PUTINT32(xdrs, int32p) xdr_putint32(xdrs, int32p)
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)
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)
268 #define XDR_GETPOS(xdrs) \
269 (*(xdrs)->x_ops->x_getpostn)(xdrs)
270 #define xdr_getpos(xdrs) \
271 (*(xdrs)->x_ops->x_getpostn)(xdrs)
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)
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)
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)
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)
296 * Solaris strips the '_t' from these types -- not sure why.
297 * But, let's be compatible.
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)
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.
315 #define NULL_xdrproc_t ((xdrproc_t)0)
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
327 * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
329 * <<< macro calls >>>
330 * where ``count'' is the number of bytes of data occupied
331 * by the primitive data types.
333 * N.B. and frozen for all time: each data type here uses 4 bytes
334 * of external representation.
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)))
342 #define IXDR_GET_LONG(buf) (ntohl((u_int32_t)*(buf)++))
343 #define IXDR_PUT_LONG(buf, v) (*(buf)++ = htonl((u_int32_t)v))
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))
349 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
350 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
352 #define IXDR_GET_U_LONG(buf) ((unsigned int)IXDR_GET_LONG(buf))
354 #define IXDR_GET_U_LONG(buf) ((unsigned long)IXDR_GET_LONG(buf))
356 #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
357 #define IXDR_GET_U_SHORT(buf) ((unsigned short)IXDR_GET_LONG(buf))
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))
366 * These are the "generic" xdr routines.
369 extern bool_t
xdr_void(void);
370 extern bool_t
xdr_int(XDR
*, int *);
371 extern bool_t
xdr_u_int(XDR
*, unsigned int *);
373 extern bool_t
xdr_long(XDR
*, int *);
374 extern bool_t
xdr_u_long(XDR
*, unsigned int *);
376 extern bool_t
xdr_long(XDR
*, long *);
377 extern bool_t
xdr_u_long(XDR
*, unsigned long *);
379 extern bool_t
xdr_short(XDR
*, short *);
380 extern bool_t
xdr_u_short(XDR
*, unsigned short *);
381 extern bool_t
xdr_int16_t(XDR
*, int16_t *);
382 extern bool_t
xdr_u_int16_t(XDR
*, u_int16_t
*);
383 extern bool_t
xdr_int32_t(XDR
*, int32_t *);
384 extern bool_t
xdr_u_int32_t(XDR
*, u_int32_t
*);
385 extern bool_t
xdr_int64_t(XDR
*, int64_t *);
386 extern bool_t
xdr_u_int64_t(XDR
*, u_int64_t
*);
387 extern bool_t
xdr_bool(XDR
*, bool_t
*);
388 extern bool_t
xdr_enum(XDR
*, enum_t
*);
389 extern bool_t
xdr_array(XDR
*, char **, unsigned int *, unsigned int, unsigned int, xdrproc_t
);
390 extern bool_t
xdr_bytes(XDR
*, char **, unsigned int *, unsigned int);
391 extern bool_t
xdr_opaque(XDR
*, char *, unsigned int);
392 extern bool_t
xdr_string(XDR
*, char **, unsigned int);
393 extern bool_t
xdr_union(XDR
*, enum_t
*, char *, const struct xdr_discrim
*, xdrproc_t
);
394 extern bool_t
xdr_char(XDR
*, char *);
395 extern bool_t
xdr_u_char(XDR
*, unsigned char *);
396 extern bool_t
xdr_vector(XDR
*, char *, unsigned int, unsigned int, xdrproc_t
);
397 extern bool_t
xdr_float(XDR
*, float *);
398 extern bool_t
xdr_double(XDR
*, double *);
399 extern bool_t
xdr_quadruple(XDR
*, long double *);
400 extern bool_t
xdr_reference(XDR
*, char **, unsigned int, xdrproc_t
);
401 extern bool_t
xdr_pointer(XDR
*, char **, unsigned int, xdrproc_t
);
402 extern bool_t
xdr_wrapstring(XDR
*, char **);
403 extern void xdr_free(xdrproc_t
, void *);
404 extern bool_t
xdr_hyper(XDR
*, quad_t
*);
405 extern bool_t
xdr_u_hyper(XDR
*, u_quad_t
*);
406 extern bool_t
xdr_longlong_t(XDR
*, quad_t
*);
407 extern bool_t
xdr_u_longlong_t(XDR
*, u_quad_t
*);
411 * Common opaque bytes objects used by many rpc protocols;
412 * declared here due to commonality.
414 #define MAX_NETOBJ_SZ 1024
419 typedef struct netobj netobj
;
420 extern bool_t
xdr_netobj(XDR
*, struct netobj
*);
423 * These are the public routines for the various implementations of
427 /* XDR using memory buffers */
428 extern void xdrmem_create(XDR
*, char *, unsigned int, enum xdr_op
);
430 /* XDR using stdio library */
432 extern void xdrstdio_create(XDR
*, FILE *, enum xdr_op
);
435 /* XDR pseudo records for tcp */
436 extern void xdrrec_create(XDR
*, unsigned int, unsigned int, void *,
437 int (*)(void *, void *, int),
438 int (*)(void *, void *, int));
440 /* make end of xdr record */
441 extern bool_t
xdrrec_endofrecord(XDR
*, int);
443 /* move to beginning of next record */
444 extern bool_t
xdrrec_skiprecord(XDR
*);
446 /* true if no more input */
447 extern bool_t
xdrrec_eof(XDR
*);
448 extern unsigned int xdrrec_readbytes(XDR
*, caddr_t
, unsigned int);
451 #endif /* !_RPC_XDR_H */