Subversion Repositories SvarDOS

Rev

Rev 207 | Details | Compare with Previous | Last modification | View Log | RSS feed

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