1 | /* $NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
5 | * unrestricted use provided that this legend is included on all tape |
---|
6 | * media and as a part of the software program in whole or part. Users |
---|
7 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
8 | * to license or distribute it to anyone else except as part of a product or |
---|
9 | * program developed by the user. |
---|
10 | * |
---|
11 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
12 | * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
13 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
14 | * |
---|
15 | * Sun RPC is provided with no support and without any obligation on the |
---|
16 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
17 | * modification or enhancement. |
---|
18 | * |
---|
19 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
20 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
21 | * OR ANY PART THEREOF. |
---|
22 | * |
---|
23 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
24 | * or profits or other special, indirect and consequential damages, even if |
---|
25 | * Sun has been advised of the possibility of such damages. |
---|
26 | * |
---|
27 | * Sun Microsystems, Inc. |
---|
28 | * 2550 Garcia Avenue |
---|
29 | * Mountain View, California 94043 |
---|
30 | * |
---|
31 | * from: @(#)xdr.h 1.19 87/04/22 SMI |
---|
32 | * from: @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC |
---|
33 | * $FreeBSD: src/include/rpc/xdr.h,v 1.23 2003/03/07 13:19:40 nectar Exp $ |
---|
34 | */ |
---|
35 | |
---|
36 | /* |
---|
37 | * xdr.h, External Data Representation Serialization Routines. |
---|
38 | * |
---|
39 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
40 | */ |
---|
41 | |
---|
42 | #ifndef _RPC_XDR_H |
---|
43 | #define _RPC_XDR_H |
---|
44 | |
---|
45 | #include <sys/cdefs.h> |
---|
46 | |
---|
47 | #include <rpc/types.h> |
---|
48 | |
---|
49 | /* |
---|
50 | * XDR provides a conventional way for converting between C data |
---|
51 | * types and an external bit-string representation. Library supplied |
---|
52 | * routines provide for the conversion on built-in C data types. These |
---|
53 | * routines and utility routines defined here are used to help implement |
---|
54 | * a type encode/decode routine for each user-defined type. |
---|
55 | * |
---|
56 | * Each data type provides a single procedure which takes two arguments: |
---|
57 | * |
---|
58 | * bool_t |
---|
59 | * xdrproc(xdrs, argresp) |
---|
60 | * XDR *xdrs; |
---|
61 | * <type> *argresp; |
---|
62 | * |
---|
63 | * xdrs is an instance of a XDR handle, to which or from which the data |
---|
64 | * type is to be converted. argresp is a pointer to the structure to be |
---|
65 | * converted. The XDR handle contains an operation field which indicates |
---|
66 | * which of the operations (ENCODE, DECODE * or FREE) is to be performed. |
---|
67 | * |
---|
68 | * XDR_DECODE may allocate space if the pointer argresp is null. This |
---|
69 | * data can be freed with the XDR_FREE operation. |
---|
70 | * |
---|
71 | * We write only one procedure per data type to make it easy |
---|
72 | * to keep the encode and decode procedures for a data type consistent. |
---|
73 | * In many cases the same code performs all operations on a user defined type, |
---|
74 | * because all the hard work is done in the component type routines. |
---|
75 | * decode as a series of calls on the nested data types. |
---|
76 | */ |
---|
77 | |
---|
78 | /* |
---|
79 | * Xdr operations. XDR_ENCODE causes the type to be encoded into the |
---|
80 | * stream. XDR_DECODE causes the type to be extracted from the stream. |
---|
81 | * XDR_FREE can be used to release the space allocated by an XDR_DECODE |
---|
82 | * request. |
---|
83 | */ |
---|
84 | enum xdr_op { |
---|
85 | XDR_ENCODE=0, |
---|
86 | XDR_DECODE=1, |
---|
87 | XDR_FREE=2 |
---|
88 | }; |
---|
89 | |
---|
90 | /* |
---|
91 | * This is the number of bytes per unit of external data. |
---|
92 | */ |
---|
93 | #define BYTES_PER_XDR_UNIT (4) |
---|
94 | #define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \ |
---|
95 | * BYTES_PER_XDR_UNIT) |
---|
96 | |
---|
97 | /* |
---|
98 | * The XDR handle. |
---|
99 | * Contains operation which is being applied to the stream, |
---|
100 | * an operations vector for the particular implementation (e.g. see xdr_mem.c), |
---|
101 | * and two private fields for the use of the particular implementation. |
---|
102 | */ |
---|
103 | typedef struct __rpc_xdr { |
---|
104 | enum xdr_op x_op; /* operation; fast additional param */ |
---|
105 | const struct xdr_ops { |
---|
106 | /* get a long from underlying stream */ |
---|
107 | bool_t (*x_getlong)(struct __rpc_xdr *, long *); |
---|
108 | /* put a long to " */ |
---|
109 | bool_t (*x_putlong)(struct __rpc_xdr *, const long *); |
---|
110 | /* get some bytes from " */ |
---|
111 | bool_t (*x_getbytes)(struct __rpc_xdr *, char *, u_int); |
---|
112 | /* put some bytes to " */ |
---|
113 | bool_t (*x_putbytes)(struct __rpc_xdr *, const char *, u_int); |
---|
114 | /* returns bytes off from beginning */ |
---|
115 | u_int (*x_getpostn)(struct __rpc_xdr *); |
---|
116 | /* lets you reposition the stream */ |
---|
117 | bool_t (*x_setpostn)(struct __rpc_xdr *, u_int); |
---|
118 | /* buf quick ptr to buffered data */ |
---|
119 | int32_t *(*x_inline)(struct __rpc_xdr *, u_int); |
---|
120 | /* free privates of this xdr_stream */ |
---|
121 | void (*x_destroy)(struct __rpc_xdr *); |
---|
122 | } *x_ops; |
---|
123 | char * x_public; /* users' data */ |
---|
124 | void * x_private; /* pointer to private data */ |
---|
125 | char * x_base; /* private used for position info */ |
---|
126 | u_int x_handy; /* extra private word */ |
---|
127 | } XDR; |
---|
128 | |
---|
129 | /* |
---|
130 | * A xdrproc_t exists for each data type which is to be encoded or decoded. |
---|
131 | * |
---|
132 | * The second argument to the xdrproc_t is a pointer to an opaque pointer. |
---|
133 | * The opaque pointer generally points to a structure of the data type |
---|
134 | * to be decoded. If this pointer is 0, then the type routines should |
---|
135 | * allocate dynamic storage of the appropriate size and return it. |
---|
136 | */ |
---|
137 | typedef bool_t (*xdrproc_t) (XDR *, void *, ...); |
---|
138 | |
---|
139 | /* |
---|
140 | * Operations defined on a XDR handle |
---|
141 | * |
---|
142 | * XDR *xdrs; |
---|
143 | * long *longp; |
---|
144 | * caddr_t addr; |
---|
145 | * u_int len; |
---|
146 | * u_int pos; |
---|
147 | */ |
---|
148 | #define XDR_GETLONG(xdrs, longp) \ |
---|
149 | (*(xdrs)->x_ops->x_getlong)(xdrs, longp) |
---|
150 | #define xdr_getlong(xdrs, longp) \ |
---|
151 | (*(xdrs)->x_ops->x_getlong)(xdrs, longp) |
---|
152 | |
---|
153 | #define XDR_PUTLONG(xdrs, longp) \ |
---|
154 | (*(xdrs)->x_ops->x_putlong)(xdrs, longp) |
---|
155 | #define xdr_putlong(xdrs, longp) \ |
---|
156 | (*(xdrs)->x_ops->x_putlong)(xdrs, longp) |
---|
157 | |
---|
158 | #define XDR_GETBYTES(xdrs, addr, len) \ |
---|
159 | (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) |
---|
160 | #define xdr_getbytes(xdrs, addr, len) \ |
---|
161 | (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) |
---|
162 | |
---|
163 | #define XDR_PUTBYTES(xdrs, addr, len) \ |
---|
164 | (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) |
---|
165 | #define xdr_putbytes(xdrs, addr, len) \ |
---|
166 | (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) |
---|
167 | |
---|
168 | #define XDR_GETPOS(xdrs) \ |
---|
169 | (*(xdrs)->x_ops->x_getpostn)(xdrs) |
---|
170 | #define xdr_getpos(xdrs) \ |
---|
171 | (*(xdrs)->x_ops->x_getpostn)(xdrs) |
---|
172 | |
---|
173 | #define XDR_SETPOS(xdrs, pos) \ |
---|
174 | (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) |
---|
175 | #define xdr_setpos(xdrs, pos) \ |
---|
176 | (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) |
---|
177 | |
---|
178 | #define XDR_INLINE(xdrs, len) \ |
---|
179 | (*(xdrs)->x_ops->x_inline)(xdrs, len) |
---|
180 | #define xdr_inline(xdrs, len) \ |
---|
181 | (*(xdrs)->x_ops->x_inline)(xdrs, len) |
---|
182 | |
---|
183 | #define XDR_DESTROY(xdrs) \ |
---|
184 | if ((xdrs)->x_ops->x_destroy) \ |
---|
185 | (*(xdrs)->x_ops->x_destroy)(xdrs) |
---|
186 | #define xdr_destroy(xdrs) \ |
---|
187 | if ((xdrs)->x_ops->x_destroy) \ |
---|
188 | (*(xdrs)->x_ops->x_destroy)(xdrs) |
---|
189 | |
---|
190 | /* |
---|
191 | * Support struct for discriminated unions. |
---|
192 | * You create an array of xdrdiscrim structures, terminated with |
---|
193 | * an entry with a null procedure pointer. The xdr_union routine gets |
---|
194 | * the discriminant value and then searches the array of structures |
---|
195 | * for a matching value. If a match is found the associated xdr routine |
---|
196 | * is called to handle that part of the union. If there is |
---|
197 | * no match, then a default routine may be called. |
---|
198 | * If there is no match and no default routine it is an error. |
---|
199 | */ |
---|
200 | #define NULL_xdrproc_t ((xdrproc_t)0) |
---|
201 | struct xdr_discrim { |
---|
202 | int value; |
---|
203 | xdrproc_t proc; |
---|
204 | }; |
---|
205 | |
---|
206 | /* |
---|
207 | * In-line routines for fast encode/decode of primitive data types. |
---|
208 | * Caveat emptor: these use single memory cycles to get the |
---|
209 | * data from the underlying buffer, and will fail to operate |
---|
210 | * properly if the data is not aligned. The standard way to use these |
---|
211 | * is to say: |
---|
212 | * if ((buf = XDR_INLINE(xdrs, count)) == NULL) |
---|
213 | * return (FALSE); |
---|
214 | * <<< macro calls >>> |
---|
215 | * where ``count'' is the number of bytes of data occupied |
---|
216 | * by the primitive data types. |
---|
217 | * |
---|
218 | * N.B. and frozen for all time: each data type here uses 4 bytes |
---|
219 | * of external representation. |
---|
220 | */ |
---|
221 | #define IXDR_GET_LONG(buf) ((long)ntohl((u_long)*(buf)++)) |
---|
222 | #define IXDR_PUT_LONG(buf, v) (*(buf)++ = (long)htonl((u_long)v)) |
---|
223 | |
---|
224 | #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf)) |
---|
225 | #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf)) |
---|
226 | #define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf)) |
---|
227 | #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf)) |
---|
228 | #define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf)) |
---|
229 | |
---|
230 | #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) |
---|
231 | #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) |
---|
232 | #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) |
---|
233 | #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) |
---|
234 | #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) |
---|
235 | |
---|
236 | /* |
---|
237 | * These are the "generic" xdr routines. |
---|
238 | */ |
---|
239 | __BEGIN_DECLS |
---|
240 | extern bool_t xdr_void(void); |
---|
241 | extern bool_t xdr_int(XDR *, int *); |
---|
242 | extern bool_t xdr_u_int(XDR *, u_int *); |
---|
243 | extern bool_t xdr_long(XDR *, long *); |
---|
244 | extern bool_t xdr_u_long(XDR *, u_long *); |
---|
245 | extern bool_t xdr_short(XDR *, short *); |
---|
246 | extern bool_t xdr_u_short(XDR *, u_short *); |
---|
247 | extern bool_t xdr_int16_t(XDR *, int16_t *); |
---|
248 | extern bool_t xdr_u_int16_t(XDR *, u_int16_t *); |
---|
249 | extern bool_t xdr_int32_t(XDR *, int32_t *); |
---|
250 | extern bool_t xdr_u_int32_t(XDR *, u_int32_t *); |
---|
251 | extern bool_t xdr_int64_t(XDR *, int64_t *); |
---|
252 | extern bool_t xdr_u_int64_t(XDR *, u_int64_t *); |
---|
253 | extern bool_t xdr_bool(XDR *, bool_t *); |
---|
254 | extern bool_t xdr_enum(XDR *, enum_t *); |
---|
255 | extern bool_t xdr_array(XDR *, char **, u_int *, u_int, u_int, xdrproc_t); |
---|
256 | extern bool_t xdr_bytes(XDR *, char **, u_int *, u_int); |
---|
257 | extern bool_t xdr_opaque(XDR *, caddr_t, u_int); |
---|
258 | extern bool_t xdr_string(XDR *, char **, u_int); |
---|
259 | extern bool_t xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t); |
---|
260 | extern unsigned long xdr_sizeof (xdrproc_t, void *); |
---|
261 | extern bool_t xdr_char(XDR *, char *); |
---|
262 | extern bool_t xdr_u_char(XDR *, u_char *); |
---|
263 | extern bool_t xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t); |
---|
264 | extern bool_t xdr_float(XDR *, float *); |
---|
265 | extern bool_t xdr_double(XDR *, double *); |
---|
266 | extern bool_t xdr_reference(XDR *, caddr_t *, u_int, xdrproc_t); |
---|
267 | extern bool_t xdr_pointer(XDR *, caddr_t *, u_int, xdrproc_t); |
---|
268 | extern bool_t xdr_wrapstring(XDR *, char **); |
---|
269 | extern void xdr_free(xdrproc_t, char *); |
---|
270 | __END_DECLS |
---|
271 | |
---|
272 | /* |
---|
273 | * Common opaque bytes objects used by many rpc protocols; |
---|
274 | * declared here due to commonality. |
---|
275 | */ |
---|
276 | #define MAX_NETOBJ_SZ 1024 |
---|
277 | struct netobj { |
---|
278 | u_int n_len; |
---|
279 | char *n_bytes; |
---|
280 | }; |
---|
281 | typedef struct netobj netobj; |
---|
282 | extern bool_t xdr_netobj(XDR *, struct netobj *); |
---|
283 | |
---|
284 | /* |
---|
285 | * These are the public routines for the various implementations of |
---|
286 | * xdr streams. |
---|
287 | */ |
---|
288 | __BEGIN_DECLS |
---|
289 | /* XDR using memory buffers */ |
---|
290 | extern void xdrmem_create(XDR *, char *, u_int, enum xdr_op); |
---|
291 | |
---|
292 | /* XDR using stdio library */ |
---|
293 | #ifdef _STDIO_H_ |
---|
294 | extern void xdrstdio_create(XDR *, FILE *, enum xdr_op); |
---|
295 | #endif |
---|
296 | |
---|
297 | /* XDR pseudo records for tcp */ |
---|
298 | extern void xdrrec_create(XDR *, u_int, u_int, char *, |
---|
299 | int (*) (caddr_t, caddr_t, int), |
---|
300 | int (*) (caddr_t, caddr_t, int)); |
---|
301 | |
---|
302 | /* make end of xdr record */ |
---|
303 | extern bool_t xdrrec_endofrecord(XDR *, bool_t); |
---|
304 | |
---|
305 | /* move to beginning of next record */ |
---|
306 | extern bool_t xdrrec_skiprecord(XDR *); |
---|
307 | |
---|
308 | /* true if no more input */ |
---|
309 | extern bool_t xdrrec_eof(XDR *); |
---|
310 | __END_DECLS |
---|
311 | |
---|
312 | #endif /* !_RPC_XDR_H */ |
---|