Libinfo-324.1.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 #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 #ifdef _KERNEL
164 typedef bool_t (*xdrproc_t)(XDR *, void *, unsigned int);
165 #else
166 /*
167 * XXX can't actually prototype it, because some take three args!!!
168 */
169 typedef bool_t (*xdrproc_t)(XDR *, ...);
170 #endif
171
172 /*
173 * Operations defined on a XDR handle
174 *
175 * XDR *xdrs;
176 * long *longp;
177 * char * addr;
178 * unsigned int len;
179 * unsigned int pos;
180 */
181 #define XDR_GETLONG(xdrs, longp) \
182 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
183 #define xdr_getlong(xdrs, longp) \
184 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
185
186 #define XDR_PUTLONG(xdrs, longp) \
187 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
188 #define xdr_putlong(xdrs, longp) \
189 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
190
191
192 #ifdef __LP64__
193 static __inline int
194 xdr_getint32(XDR *xdrs, int32_t *ip)
195 {
196 int32_t l;
197
198 if (!xdr_getlong(xdrs, &l))
199 return (FALSE);
200 *ip = l;
201 return (TRUE);
202 }
203
204 static __inline int
205 xdr_putint32(XDR *xdrs, int32_t *ip)
206 {
207 int32_t l;
208
209 l = *ip;
210 return xdr_putlong(xdrs, &l);
211 }
212 #else
213 static __inline int
214 xdr_getint32(XDR *xdrs, int32_t *ip)
215 {
216 int32_t l;
217
218 if (!xdr_getlong(xdrs, (long *)&l))
219 return (FALSE);
220 *ip = l;
221 return (TRUE);
222 }
223
224 static __inline int
225 xdr_putint32(XDR *xdrs, int32_t *ip)
226 {
227 int32_t l;
228
229 l = *ip;
230 return xdr_putlong(xdrs, (long *)&l);
231 }
232 #endif
233
234 #define XDR_GETINT32(xdrs, int32p) xdr_getint32(xdrs, int32p)
235 #define XDR_PUTINT32(xdrs, int32p) xdr_putint32(xdrs, int32p)
236
237 #define XDR_GETBYTES(xdrs, addr, len) \
238 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
239 #define xdr_getbytes(xdrs, addr, len) \
240 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
241
242 #define XDR_PUTBYTES(xdrs, addr, len) \
243 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
244 #define xdr_putbytes(xdrs, addr, len) \
245 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
246
247 #define XDR_GETPOS(xdrs) \
248 (*(xdrs)->x_ops->x_getpostn)(xdrs)
249 #define xdr_getpos(xdrs) \
250 (*(xdrs)->x_ops->x_getpostn)(xdrs)
251
252 #define XDR_SETPOS(xdrs, pos) \
253 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
254 #define xdr_setpos(xdrs, pos) \
255 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
256
257 #define XDR_INLINE(xdrs, len) \
258 (*(xdrs)->x_ops->x_inline)(xdrs, len)
259 #define xdr_inline(xdrs, len) \
260 (*(xdrs)->x_ops->x_inline)(xdrs, len)
261
262 #define XDR_DESTROY(xdrs) \
263 if ((xdrs)->x_ops->x_destroy) \
264 (*(xdrs)->x_ops->x_destroy)(xdrs)
265 #define xdr_destroy(xdrs) \
266 if ((xdrs)->x_ops->x_destroy) \
267 (*(xdrs)->x_ops->x_destroy)(xdrs)
268
269 #define XDR_CONTROL(xdrs, req, op) \
270 if ((xdrs)->x_ops->x_control) \
271 (*(xdrs)->x_ops->x_control)(xdrs, req, op)
272 #define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op)
273
274 /*
275 * Solaris strips the '_t' from these types -- not sure why.
276 * But, let's be compatible.
277 */
278 #define xdr_rpcvers(xdrs, versp) xdr_u_int32(xdrs, versp)
279 #define xdr_rpcprog(xdrs, progp) xdr_u_int32(xdrs, progp)
280 #define xdr_rpcproc(xdrs, procp) xdr_u_int32(xdrs, procp)
281 #define xdr_rpcprot(xdrs, protp) xdr_u_int32(xdrs, protp)
282 #define xdr_rpcport(xdrs, portp) xdr_u_int32(xdrs, portp)
283
284 /*
285 * Support struct for discriminated unions.
286 * You create an array of xdrdiscrim structures, terminated with
287 * an entry with a null procedure pointer. The xdr_union routine gets
288 * the discriminant value and then searches the array of structures
289 * for a matching value. If a match is found the associated xdr routine
290 * is called to handle that part of the union. If there is
291 * no match, then a default routine may be called.
292 * If there is no match and no default routine it is an error.
293 */
294 #define NULL_xdrproc_t ((xdrproc_t)0)
295 struct xdr_discrim {
296 int value;
297 xdrproc_t proc;
298 };
299
300 /*
301 * In-line routines for fast encode/decode of primitive data types.
302 * Caveat emptor: these use single memory cycles to get the
303 * data from the underlying buffer, and will fail to operate
304 * properly if the data is not aligned. The standard way to use these
305 * is to say:
306 * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
307 * return (FALSE);
308 * <<< macro calls >>>
309 * where ``count'' is the number of bytes of data occupied
310 * by the primitive data types.
311 *
312 * N.B. and frozen for all time: each data type here uses 4 bytes
313 * of external representation.
314 */
315 #define IXDR_GET_INT32(buf) ((int32_t)ntohl((u_int32_t)*(buf)++))
316 #define IXDR_PUT_INT32(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v))
317 #define IXDR_GET_U_INT32(buf) ((u_int32_t)IXDR_GET_INT32(buf))
318 #define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32((buf), ((int32_t)(v)))
319
320 #ifdef __LP64__
321 #define IXDR_GET_LONG(buf) (ntohl((u_int32_t)*(buf)++))
322 #define IXDR_PUT_LONG(buf, v) (*(buf)++ = htonl((u_int32_t)v))
323 #else
324 #define IXDR_GET_LONG(buf) ((long)ntohl((u_int32_t)*(buf)++))
325 #define IXDR_PUT_LONG(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v))
326 #endif
327
328 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
329 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
330 #ifdef __LP64__
331 #define IXDR_GET_U_LONG(buf) ((unsigned int)IXDR_GET_LONG(buf))
332 #else
333 #define IXDR_GET_U_LONG(buf) ((unsigned long)IXDR_GET_LONG(buf))
334 #endif
335 #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
336 #define IXDR_GET_U_SHORT(buf) ((unsigned short)IXDR_GET_LONG(buf))
337
338 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), (v))
339 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), (v))
340 #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), (v))
341 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
342 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
343
344 /*
345 * These are the "generic" xdr routines.
346 */
347 __BEGIN_DECLS
348 extern bool_t xdr_void(void);
349 extern bool_t xdr_int(XDR *, int *);
350 extern bool_t xdr_u_int(XDR *, unsigned int *);
351 #ifdef __LP64__
352 extern bool_t xdr_long(XDR *, int *);
353 extern bool_t xdr_u_long(XDR *, unsigned int *);
354 #else
355 extern bool_t xdr_long(XDR *, long *);
356 extern bool_t xdr_u_long(XDR *, unsigned long *);
357 #endif
358 extern bool_t xdr_short(XDR *, short *);
359 extern bool_t xdr_u_short(XDR *, unsigned short *);
360 extern bool_t xdr_int16_t(XDR *, int16_t *);
361 extern bool_t xdr_u_int16_t(XDR *, u_int16_t *);
362 extern bool_t xdr_int32_t(XDR *, int32_t *);
363 extern bool_t xdr_u_int32_t(XDR *, u_int32_t *);
364 extern bool_t xdr_int64_t(XDR *, int64_t *);
365 extern bool_t xdr_u_int64_t(XDR *, u_int64_t *);
366 extern bool_t xdr_bool(XDR *, bool_t *);
367 extern bool_t xdr_enum(XDR *, enum_t *);
368 extern bool_t xdr_array(XDR *, char **, unsigned int *, unsigned int, unsigned int, xdrproc_t);
369 extern bool_t xdr_bytes(XDR *, char **, unsigned int *, unsigned int);
370 extern bool_t xdr_opaque(XDR *, char *, unsigned int);
371 extern bool_t xdr_string(XDR *, char **, unsigned int);
372 extern bool_t xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t);
373 extern bool_t xdr_char(XDR *, char *);
374 extern bool_t xdr_u_char(XDR *, unsigned char *);
375 extern bool_t xdr_vector(XDR *, char *, unsigned int, unsigned int, xdrproc_t);
376 extern bool_t xdr_float(XDR *, float *);
377 extern bool_t xdr_double(XDR *, double *);
378 extern bool_t xdr_quadruple(XDR *, long double *);
379 extern bool_t xdr_reference(XDR *, char **, unsigned int, xdrproc_t);
380 extern bool_t xdr_pointer(XDR *, char **, unsigned int, xdrproc_t);
381 extern bool_t xdr_wrapstring(XDR *, char **);
382 extern void xdr_free(xdrproc_t, void *);
383 extern bool_t xdr_hyper(XDR *, quad_t *);
384 extern bool_t xdr_u_hyper(XDR *, u_quad_t *);
385 extern bool_t xdr_longlong_t(XDR *, quad_t *);
386 extern bool_t xdr_u_longlong_t(XDR *, u_quad_t *);
387 __END_DECLS
388
389 /*
390 * Common opaque bytes objects used by many rpc protocols;
391 * declared here due to commonality.
392 */
393 #define MAX_NETOBJ_SZ 1024
394 struct netobj {
395 unsigned int n_len;
396 char *n_bytes;
397 };
398 typedef struct netobj netobj;
399 extern bool_t xdr_netobj(XDR *, struct netobj *);
400
401 /*
402 * These are the public routines for the various implementations of
403 * xdr streams.
404 */
405 __BEGIN_DECLS
406 /* XDR using memory buffers */
407 extern void xdrmem_create(XDR *, char *, unsigned int, enum xdr_op);
408
409 /* XDR using stdio library */
410 #ifdef _STDIO_H_
411 extern void xdrstdio_create(XDR *, FILE *, enum xdr_op);
412 #endif
413
414 /* XDR pseudo records for tcp */
415 extern void xdrrec_create(XDR *, unsigned int, unsigned int, void *,
416 int (*)(void *, void *, int),
417 int (*)(void *, void *, int));
418
419 /* make end of xdr record */
420 extern bool_t xdrrec_endofrecord(XDR *, int);
421
422 /* move to beginning of next record */
423 extern bool_t xdrrec_skiprecord(XDR *);
424
425 /* true if no more input */
426 extern bool_t xdrrec_eof(XDR *);
427 extern unsigned int xdrrec_readbytes(XDR *, caddr_t, unsigned int);
428 __END_DECLS
429
430 #endif /* !_RPC_XDR_H */