Libinfo-221.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr.h
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 */
102 enum 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 */
121 typedef struct __rpc_xdr {
122 enum xdr_op x_op; /* operation; fast additional param */
123 const struct xdr_ops {
124 /* get a long from underlying stream */
125 bool_t (*x_getlong)(struct __rpc_xdr *, long *);
126 /* put a long to " */
127 bool_t (*x_putlong)(struct __rpc_xdr *, const long *);
128 /* get some bytes from " */
129 bool_t (*x_getbytes)(struct __rpc_xdr *, char *, unsigned int);
130 /* put some bytes to " */
131 bool_t (*x_putbytes)(struct __rpc_xdr *, const char *, unsigned int);
132 /* returns bytes off from beginning */
133 unsigned int (*x_getpostn)(struct __rpc_xdr *);
134 /* lets you reposition the stream */
135 bool_t (*x_setpostn)(struct __rpc_xdr *, unsigned int);
136 /* buf quick ptr to buffered data */
137 int32_t *(*x_inline)(struct __rpc_xdr *, unsigned int);
138 /* free privates of this xdr_stream */
139 void (*x_destroy)(struct __rpc_xdr *);
140 bool_t (*x_control)(struct __rpc_xdr *, int, void *);
141 } *x_ops;
142 char * x_public; /* users' data */
143 void * x_private; /* pointer to private data */
144 char * x_base; /* private used for position info */
145 unsigned int x_handy; /* extra private word */
146 } XDR;
147
148 /*
149 * A xdrproc_t exists for each data type which is to be encoded or decoded.
150 *
151 * The second argument to the xdrproc_t is a pointer to an opaque pointer.
152 * The opaque pointer generally points to a structure of the data type
153 * to be decoded. If this pointer is 0, then the type routines should
154 * allocate dynamic storage of the appropriate size and return it.
155 */
156 #ifdef _KERNEL
157 typedef bool_t (*xdrproc_t)(XDR *, void *, unsigned int);
158 #else
159 /*
160 * XXX can't actually prototype it, because some take three args!!!
161 */
162 typedef bool_t (*xdrproc_t)(XDR *, ...);
163 #endif
164
165 /*
166 * Operations defined on a XDR handle
167 *
168 * XDR *xdrs;
169 * long *longp;
170 * char * addr;
171 * unsigned int len;
172 * unsigned int pos;
173 */
174 #define XDR_GETLONG(xdrs, longp) \
175 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
176 #define xdr_getlong(xdrs, longp) \
177 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
178
179 #define XDR_PUTLONG(xdrs, longp) \
180 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
181 #define xdr_putlong(xdrs, longp) \
182 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
183
184 static __inline int
185 xdr_getint32(XDR *xdrs, int32_t *ip)
186 {
187 long l;
188
189 if (!xdr_getlong(xdrs, &l))
190 return (FALSE);
191 *ip = (int32_t)l;
192 return (TRUE);
193 }
194
195 static __inline int
196 xdr_putint32(XDR *xdrs, int32_t *ip)
197 {
198 long l;
199
200 l = (long)*ip;
201 return xdr_putlong(xdrs, &l);
202 }
203
204 #define XDR_GETINT32(xdrs, int32p) xdr_getint32(xdrs, int32p)
205 #define XDR_PUTINT32(xdrs, int32p) xdr_putint32(xdrs, int32p)
206
207 #define XDR_GETBYTES(xdrs, addr, len) \
208 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
209 #define xdr_getbytes(xdrs, addr, len) \
210 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
211
212 #define XDR_PUTBYTES(xdrs, addr, len) \
213 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
214 #define xdr_putbytes(xdrs, addr, len) \
215 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
216
217 #define XDR_GETPOS(xdrs) \
218 (*(xdrs)->x_ops->x_getpostn)(xdrs)
219 #define xdr_getpos(xdrs) \
220 (*(xdrs)->x_ops->x_getpostn)(xdrs)
221
222 #define XDR_SETPOS(xdrs, pos) \
223 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
224 #define xdr_setpos(xdrs, pos) \
225 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
226
227 #define XDR_INLINE(xdrs, len) \
228 (*(xdrs)->x_ops->x_inline)(xdrs, len)
229 #define xdr_inline(xdrs, len) \
230 (*(xdrs)->x_ops->x_inline)(xdrs, len)
231
232 #define XDR_DESTROY(xdrs) \
233 if ((xdrs)->x_ops->x_destroy) \
234 (*(xdrs)->x_ops->x_destroy)(xdrs)
235 #define xdr_destroy(xdrs) \
236 if ((xdrs)->x_ops->x_destroy) \
237 (*(xdrs)->x_ops->x_destroy)(xdrs)
238
239 #define XDR_CONTROL(xdrs, req, op) \
240 if ((xdrs)->x_ops->x_control) \
241 (*(xdrs)->x_ops->x_control)(xdrs, req, op)
242 #define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op)
243
244 /*
245 * Solaris strips the '_t' from these types -- not sure why.
246 * But, let's be compatible.
247 */
248 #define xdr_rpcvers(xdrs, versp) xdr_u_int32(xdrs, versp)
249 #define xdr_rpcprog(xdrs, progp) xdr_u_int32(xdrs, progp)
250 #define xdr_rpcproc(xdrs, procp) xdr_u_int32(xdrs, procp)
251 #define xdr_rpcprot(xdrs, protp) xdr_u_int32(xdrs, protp)
252 #define xdr_rpcport(xdrs, portp) xdr_u_int32(xdrs, portp)
253
254 /*
255 * Support struct for discriminated unions.
256 * You create an array of xdrdiscrim structures, terminated with
257 * an entry with a null procedure pointer. The xdr_union routine gets
258 * the discriminant value and then searches the array of structures
259 * for a matching value. If a match is found the associated xdr routine
260 * is called to handle that part of the union. If there is
261 * no match, then a default routine may be called.
262 * If there is no match and no default routine it is an error.
263 */
264 #define NULL_xdrproc_t ((xdrproc_t)0)
265 struct xdr_discrim {
266 int value;
267 xdrproc_t proc;
268 };
269
270 /*
271 * In-line routines for fast encode/decode of primitive data types.
272 * Caveat emptor: these use single memory cycles to get the
273 * data from the underlying buffer, and will fail to operate
274 * properly if the data is not aligned. The standard way to use these
275 * is to say:
276 * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
277 * return (FALSE);
278 * <<< macro calls >>>
279 * where ``count'' is the number of bytes of data occupied
280 * by the primitive data types.
281 *
282 * N.B. and frozen for all time: each data type here uses 4 bytes
283 * of external representation.
284 */
285 #define IXDR_GET_INT32(buf) ((int32_t)ntohl((u_int32_t)*(buf)++))
286 #define IXDR_PUT_INT32(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v))
287 #define IXDR_GET_U_INT32(buf) ((u_int32_t)IXDR_GET_INT32(buf))
288 #define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32((buf), ((int32_t)(v)))
289
290 #define IXDR_GET_LONG(buf) ((long)ntohl((u_int32_t)*(buf)++))
291 #define IXDR_PUT_LONG(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v))
292
293 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
294 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
295 #define IXDR_GET_U_LONG(buf) ((unsigned long)IXDR_GET_LONG(buf))
296 #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
297 #define IXDR_GET_U_SHORT(buf) ((unsigned short)IXDR_GET_LONG(buf))
298
299 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), (v))
300 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), (v))
301 #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), (v))
302 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
303 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
304
305 /*
306 * These are the "generic" xdr routines.
307 */
308 __BEGIN_DECLS
309 extern bool_t xdr_void(void);
310 extern bool_t xdr_int(XDR *, int *);
311 extern bool_t xdr_u_int(XDR *, unsigned int *);
312 extern bool_t xdr_long(XDR *, long *);
313 extern bool_t xdr_u_long(XDR *, unsigned long *);
314 extern bool_t xdr_short(XDR *, short *);
315 extern bool_t xdr_u_short(XDR *, unsigned short *);
316 extern bool_t xdr_int16_t(XDR *, int16_t *);
317 extern bool_t xdr_u_int16_t(XDR *, u_int16_t *);
318 extern bool_t xdr_int32_t(XDR *, int32_t *);
319 extern bool_t xdr_u_int32_t(XDR *, u_int32_t *);
320 extern bool_t xdr_int64_t(XDR *, int64_t *);
321 extern bool_t xdr_u_int64_t(XDR *, u_int64_t *);
322 extern bool_t xdr_bool(XDR *, bool_t *);
323 extern bool_t xdr_enum(XDR *, enum_t *);
324 extern bool_t xdr_array(XDR *, char **, unsigned int *, unsigned int, unsigned int, xdrproc_t);
325 extern bool_t xdr_bytes(XDR *, char **, unsigned int *, unsigned int);
326 extern bool_t xdr_opaque(XDR *, char *, unsigned int);
327 extern bool_t xdr_string(XDR *, char **, unsigned int);
328 extern bool_t xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t);
329 extern bool_t xdr_char(XDR *, char *);
330 extern bool_t xdr_u_char(XDR *, unsigned char *);
331 extern bool_t xdr_vector(XDR *, char *, unsigned int, unsigned int, xdrproc_t);
332 extern bool_t xdr_float(XDR *, float *);
333 extern bool_t xdr_double(XDR *, double *);
334 extern bool_t xdr_quadruple(XDR *, long double *);
335 extern bool_t xdr_reference(XDR *, char **, unsigned int, xdrproc_t);
336 extern bool_t xdr_pointer(XDR *, char **, unsigned int, xdrproc_t);
337 extern bool_t xdr_wrapstring(XDR *, char **);
338 extern void xdr_free(xdrproc_t, void *);
339 extern bool_t xdr_hyper(XDR *, quad_t *);
340 extern bool_t xdr_u_hyper(XDR *, u_quad_t *);
341 extern bool_t xdr_longlong_t(XDR *, quad_t *);
342 extern bool_t xdr_u_longlong_t(XDR *, u_quad_t *);
343 __END_DECLS
344
345 /*
346 * Common opaque bytes objects used by many rpc protocols;
347 * declared here due to commonality.
348 */
349 #define MAX_NETOBJ_SZ 1024
350 struct netobj {
351 unsigned int n_len;
352 char *n_bytes;
353 };
354 typedef struct netobj netobj;
355 extern bool_t xdr_netobj(XDR *, struct netobj *);
356
357 /*
358 * These are the public routines for the various implementations of
359 * xdr streams.
360 */
361 __BEGIN_DECLS
362 /* XDR using memory buffers */
363 extern void xdrmem_create(XDR *, char *, unsigned int, enum xdr_op);
364
365 /* XDR using stdio library */
366 #ifdef _STDIO_H_
367 extern void xdrstdio_create(XDR *, FILE *, enum xdr_op);
368 #endif
369
370 /* XDR pseudo records for tcp */
371 extern void xdrrec_create(XDR *, unsigned int, unsigned int, void *,
372 int (*)(void *, void *, int),
373 int (*)(void *, void *, int));
374
375 /* make end of xdr record */
376 extern bool_t xdrrec_endofrecord(XDR *, int);
377
378 /* move to beginning of next record */
379 extern bool_t xdrrec_skiprecord(XDR *);
380
381 /* true if no more input */
382 extern bool_t xdrrec_eof(XDR *);
383 extern unsigned int xdrrec_readbytes(XDR *, caddr_t, unsigned int);
384 __END_DECLS
385
386 #endif /* !_RPC_XDR_H */