Libinfo-78.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, MERCHANTIBILITY 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 * $Id: xdr.h,v 1.2 1999/10/14 21:56:54 wsanchez 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 * A xdrproc_t exists for each data type which is to be encoded or decoded.
117 *
118 * The second argument to the xdrproc_t is a pointer to an opaque pointer.
119 * The opaque pointer generally points to a structure of the data type
120 * to be decoded. If this pointer is 0, then the type routines should
121 * allocate dynamic storage of the appropriate size and return it.
122 * bool_t (*xdrproc_t)(XDR *, caddr_t *);
123 */
124 typedef bool_t (*xdrproc_t)();
125
126 /*
127 * The XDR handle.
128 * Contains operation which is being applied to the stream,
129 * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
130 * and two private fields for the use of the particular impelementation.
131 */
132 typedef struct {
133 enum xdr_op x_op; /* operation; fast additional param */
134 struct xdr_ops {
135 bool_t (*x_getlong)(); /* get a long from underlying stream */
136 bool_t (*x_putlong)(); /* put a long to " */
137 bool_t (*x_getbytes)();/* get some bytes from " */
138 bool_t (*x_putbytes)();/* put some bytes to " */
139 u_int (*x_getpostn)();/* returns bytes off from beginning */
140 bool_t (*x_setpostn)();/* lets you reposition the stream */
141 long * (*x_inline)(); /* buf quick ptr to buffered data */
142 void (*x_destroy)(); /* free privates of this xdr_stream */
143 } *x_ops;
144 caddr_t x_public; /* users' data */
145 caddr_t x_private; /* pointer to private data */
146 caddr_t x_base; /* private used for position info */
147 int x_handy; /* extra private word */
148 } XDR;
149
150 /*
151 * Operations defined on a XDR handle
152 *
153 * XDR *xdrs;
154 * long *longp;
155 * caddr_t addr;
156 * u_int len;
157 * u_int pos;
158 */
159 #define XDR_GETLONG(xdrs, longp) \
160 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
161 #define xdr_getlong(xdrs, longp) \
162 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
163
164 #define XDR_PUTLONG(xdrs, longp) \
165 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
166 #define xdr_putlong(xdrs, longp) \
167 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
168
169 #define XDR_GETBYTES(xdrs, addr, len) \
170 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
171 #define xdr_getbytes(xdrs, addr, len) \
172 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
173
174 #define XDR_PUTBYTES(xdrs, addr, len) \
175 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
176 #define xdr_putbytes(xdrs, addr, len) \
177 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
178
179 #define XDR_GETPOS(xdrs) \
180 (*(xdrs)->x_ops->x_getpostn)(xdrs)
181 #define xdr_getpos(xdrs) \
182 (*(xdrs)->x_ops->x_getpostn)(xdrs)
183
184 #define XDR_SETPOS(xdrs, pos) \
185 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
186 #define xdr_setpos(xdrs, pos) \
187 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
188
189 #define XDR_INLINE(xdrs, len) \
190 (*(xdrs)->x_ops->x_inline)(xdrs, len)
191 #define xdr_inline(xdrs, len) \
192 (*(xdrs)->x_ops->x_inline)(xdrs, len)
193
194 #define XDR_DESTROY(xdrs) \
195 if (xdrs) \
196 if ((xdrs)->x_ops) \
197 if ((xdrs)->x_ops->x_destroy) \
198 (*(xdrs)->x_ops->x_destroy)(xdrs)
199 #define xdr_destroy(xdrs) \
200 if (xdrs) \
201 if ((xdrs)->x_ops) \
202 if ((xdrs)->x_ops->x_destroy) \
203 (*(xdrs)->x_ops->x_destroy)(xdrs)
204
205 /*
206 * Support struct for discriminated unions.
207 * You create an array of xdrdiscrim structures, terminated with
208 * a entry with a null procedure pointer. The xdr_union routine gets
209 * the discriminant value and then searches the array of structures
210 * for a matching value. If a match is found the associated xdr routine
211 * is called to handle that part of the union. If there is
212 * no match, then a default routine may be called.
213 * If there is no match and no default routine it is an error.
214 */
215 #define NULL_xdrproc_t ((xdrproc_t)0)
216 struct xdr_discrim {
217 int value;
218 xdrproc_t proc;
219 };
220
221 /*
222 * In-line routines for fast encode/decode of primitve data types.
223 * Caveat emptor: these use single memory cycles to get the
224 * data from the underlying buffer, and will fail to operate
225 * properly if the data is not aligned. The standard way to use these
226 * is to say:
227 * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
228 * return (FALSE);
229 * <<< macro calls >>>
230 * where ``count'' is the number of bytes of data occupied
231 * by the primitive data types.
232 *
233 * N.B. and frozen for all time: each data type here uses 4 bytes
234 * of external representation.
235 */
236 #define IXDR_GET_LONG(buf) ((long)ntohl((u_long)*(buf)++))
237 #define IXDR_PUT_LONG(buf, v) (*(buf)++ = (long)htonl((u_long)v))
238
239 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
240 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
241 #define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
242 #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
243 #define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
244
245 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
246 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
247 #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
248 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
249 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
250
251 /*
252 * These are the "generic" xdr routines.
253 */
254 __BEGIN_DECLS
255 extern bool_t xdr_void __P((void));
256 extern bool_t xdr_int __P((XDR *, int *));
257 extern bool_t xdr_u_int __P((XDR *, u_int *));
258 extern bool_t xdr_long __P((XDR *, long *));
259 extern bool_t xdr_u_long __P((XDR *, u_long *));
260 extern bool_t xdr_short __P((XDR *, short *));
261 extern bool_t xdr_u_short __P((XDR *, u_short *));
262 extern bool_t xdr_bool __P((XDR *, bool_t *));
263 extern bool_t xdr_enum __P((XDR *, enum_t *));
264 extern bool_t xdr_array __P((XDR *, char **, u_int *, u_int, u_int, xdrproc_t));
265 extern bool_t xdr_bytes __P((XDR *, char **, u_int *, u_int));
266 extern bool_t xdr_opaque __P((XDR *, caddr_t, u_int));
267 extern bool_t xdr_string __P((XDR *, char **, u_int));
268 extern bool_t xdr_union __P((XDR *, enum_t *, char *, struct xdr_discrim *, xdrproc_t));
269 extern bool_t xdr_char __P((XDR *, char *));
270 extern bool_t xdr_u_char __P((XDR *, u_char *));
271 extern bool_t xdr_vector __P((XDR *, char *, u_int, u_int, xdrproc_t));
272 extern bool_t xdr_float __P((XDR *, float *));
273 extern bool_t xdr_double __P((XDR *, double *));
274 extern bool_t xdr_reference __P((XDR *, caddr_t *, u_int, xdrproc_t));
275 extern bool_t xdr_pointer __P((XDR *, caddr_t *, u_int, xdrproc_t));
276 extern bool_t xdr_wrapstring __P((XDR *, char **));
277 extern void xdr_free __P((xdrproc_t, void *));
278 __END_DECLS
279
280 /*
281 * Common opaque bytes objects used by many rpc protocols;
282 * declared here due to commonality.
283 */
284 #define MAX_NETOBJ_SZ 1024
285 struct netobj {
286 u_int n_len;
287 char *n_bytes;
288 };
289 typedef struct netobj netobj;
290 extern bool_t xdr_netobj();
291
292 /*
293 * These are the public routines for the various implementations of
294 * xdr streams.
295 */
296 __BEGIN_DECLS
297 /* XDR using memory buffers */
298 extern void xdrmem_create __P((XDR *, void *, u_int, enum xdr_op));
299
300 #ifdef _STDIO_H_
301 /* XDR using stdio library */
302 extern void xdrstdio_create __P((XDR *, FILE *, enum xdr_op));
303 #endif
304
305 /* XDR pseudo records for tcp */
306 extern void xdrrec_create __P((XDR *, u_int, u_int, char *, int (*)(), int (*)()));
307
308 /* make end of xdr record */
309 extern bool_t xdrrec_endofrecord __P((XDR *, int));
310
311 /* move to beginning of next record */
312 extern bool_t xdrrec_skiprecord __P((XDR *));
313
314 /* true if no more input */
315 extern bool_t xdrrec_eof __P((XDR *));
316 __END_DECLS
317
318 #endif /* !_RPC_XDR_H */