Coverage Report

Created: 2026-06-07 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/usr/include/tirpc/rpc/xdr.h
Line
Count
Source
1
/*  $NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt Exp $  */
2
3
/*
4
 * Copyright (c) 2009, Sun Microsystems, Inc.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are met:
9
 * - Redistributions of source code must retain the above copyright notice,
10
 *   this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright notice,
12
 *   this list of conditions and the following disclaimer in the documentation
13
 *   and/or other materials provided with the distribution.
14
 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15
 *   contributors may be used to endorse or promote products derived
16
 *   from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 *  from: @(#)xdr.h 1.19 87/04/22 SMI
31
 *  from: @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC
32
 * $FreeBSD: src/include/rpc/xdr.h,v 1.23 2003/03/07 13:19:40 nectar Exp $
33
 */
34
35
/*
36
 * xdr.h, External Data Representation Serialization Routines.
37
 *
38
 * Copyright (C) 1984, Sun Microsystems, Inc.
39
 */
40
41
#ifndef _TIRPC_XDR_H
42
#define _TIRPC_XDR_H
43
#include <stdio.h>
44
#include <netinet/in.h>
45
46
#include <rpc/types.h>
47
48
/*
49
 * XDR provides a conventional way for converting between C data
50
 * types and an external bit-string representation.  Library supplied
51
 * routines provide for the conversion on built-in C data types.  These
52
 * routines and utility routines defined here are used to help implement
53
 * a type encode/decode routine for each user-defined type.
54
 *
55
 * Each data type provides a single procedure which takes two arguments:
56
 *
57
 *  bool_t
58
 *  xdrproc(xdrs, argresp)
59
 *    XDR *xdrs;
60
 *    <type> *argresp;
61
 *
62
 * xdrs is an instance of a XDR handle, to which or from which the data
63
 * type is to be converted.  argresp is a pointer to the structure to be
64
 * converted.  The XDR handle contains an operation field which indicates
65
 * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
66
 *
67
 * XDR_DECODE may allocate space if the pointer argresp is null.  This
68
 * data can be freed with the XDR_FREE operation.
69
 *
70
 * We write only one procedure per data type to make it easy
71
 * to keep the encode and decode procedures for a data type consistent.
72
 * In many cases the same code performs all operations on a user defined type,
73
 * because all the hard work is done in the component type routines.
74
 * decode as a series of calls on the nested data types.
75
 */
76
77
/*
78
 * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
79
 * stream.  XDR_DECODE causes the type to be extracted from the stream.
80
 * XDR_FREE can be used to release the space allocated by an XDR_DECODE
81
 * request.
82
 */
83
enum xdr_op {
84
  XDR_ENCODE=0,
85
  XDR_DECODE=1,
86
  XDR_FREE=2
87
};
88
89
/*
90
 * This is the number of bytes per unit of external data.
91
 */
92
#define BYTES_PER_XDR_UNIT  (4)
93
#define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
94
        * BYTES_PER_XDR_UNIT)
95
96
/*
97
 * The XDR handle.
98
 * Contains operation which is being applied to the stream,
99
 * an operations vector for the particular implementation (e.g. see xdr_mem.c),
100
 * and two private fields for the use of the particular implementation.
101
 */
102
typedef struct __rpc_xdr {
103
  enum xdr_op x_op;   /* operation; fast additional param */
104
  const struct xdr_ops {
105
    /* get a long from underlying stream */
106
    bool_t  (*x_getlong)(struct __rpc_xdr *, long *);
107
    /* put a long to " */
108
    bool_t  (*x_putlong)(struct __rpc_xdr *, const long *);
109
    /* get some bytes from " */
110
    bool_t  (*x_getbytes)(struct __rpc_xdr *, char *, u_int);
111
    /* put some bytes to " */
112
    bool_t  (*x_putbytes)(struct __rpc_xdr *, const char *, u_int);
113
    /* returns bytes off from beginning */
114
    u_int (*x_getpostn)(struct __rpc_xdr *);
115
    /* lets you reposition the stream */
116
    bool_t  (*x_setpostn)(struct __rpc_xdr *, u_int);
117
    /* buf quick ptr to buffered data */
118
    int32_t *(*x_inline)(struct __rpc_xdr *, u_int);
119
    /* free privates of this xdr_stream */
120
    void  (*x_destroy)(struct __rpc_xdr *);
121
    bool_t  (*x_control)(struct __rpc_xdr *, int, void *);
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
#ifdef _KERNEL
138
typedef bool_t (*xdrproc_t)(XDR *, void *, u_int);
139
#else
140
/*
141
 * XXX can't actually prototype it, because some take three args!!!
142
 */
143
typedef bool_t (*xdrproc_t)(XDR *, ...);
144
#endif
145
146
/*
147
 * Operations defined on a XDR handle
148
 *
149
 * XDR    *xdrs;
150
 * long   *longp;
151
 * char *  addr;
152
 * u_int   len;
153
 * u_int   pos;
154
 */
155
#define XDR_GETLONG(xdrs, longp)      \
156
  (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
157
#define xdr_getlong(xdrs, longp)      \
158
  (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
159
160
#define XDR_PUTLONG(xdrs, longp)      \
161
  (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
162
#define xdr_putlong(xdrs, longp)      \
163
  (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
164
165
static __inline int
166
xdr_getint32(XDR *xdrs, int32_t *ip)
167
0
{
168
0
  long l;
169
0
170
0
  if (!xdr_getlong(xdrs, &l))
171
0
    return (FALSE);
172
0
  *ip = (int32_t)l;
173
0
  return (TRUE);
174
0
}
175
176
static __inline int
177
xdr_putint32(XDR *xdrs, int32_t *ip)
178
0
{
179
0
  long l;
180
0
181
0
  l = (long)*ip;
182
0
  return xdr_putlong(xdrs, &l);
183
0
}
184
185
#define XDR_GETINT32(xdrs, int32p)  xdr_getint32(xdrs, int32p)
186
#define XDR_PUTINT32(xdrs, int32p)  xdr_putint32(xdrs, int32p)
187
188
#define XDR_GETBYTES(xdrs, addr, len)     \
189
  (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
190
#define xdr_getbytes(xdrs, addr, len)     \
191
  (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
192
193
#define XDR_PUTBYTES(xdrs, addr, len)     \
194
  (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
195
#define xdr_putbytes(xdrs, addr, len)     \
196
  (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
197
198
#define XDR_GETPOS(xdrs)        \
199
  (*(xdrs)->x_ops->x_getpostn)(xdrs)
200
#define xdr_getpos(xdrs)        \
201
  (*(xdrs)->x_ops->x_getpostn)(xdrs)
202
203
#define XDR_SETPOS(xdrs, pos)       \
204
  (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
205
#define xdr_setpos(xdrs, pos)       \
206
  (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
207
208
#define XDR_INLINE(xdrs, len)       \
209
  (*(xdrs)->x_ops->x_inline)(xdrs, len)
210
#define xdr_inline(xdrs, len)       \
211
  (*(xdrs)->x_ops->x_inline)(xdrs, len)
212
213
#define XDR_DESTROY(xdrs)       \
214
  if ((xdrs)->x_ops->x_destroy)       \
215
    (*(xdrs)->x_ops->x_destroy)(xdrs)
216
#define xdr_destroy(xdrs)       \
217
  if ((xdrs)->x_ops->x_destroy)       \
218
    (*(xdrs)->x_ops->x_destroy)(xdrs)
219
220
#define XDR_CONTROL(xdrs, req, op)      \
221
  if ((xdrs)->x_ops->x_control)     \
222
    (*(xdrs)->x_ops->x_control)(xdrs, req, op)
223
#define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op)
224
225
#define xdr_rpcvers(xdrs, versp) xdr_u_int32_t(xdrs, versp)
226
#define xdr_rpcprog(xdrs, progp) xdr_u_int32_t(xdrs, progp)
227
#define xdr_rpcproc(xdrs, procp) xdr_u_int32_t(xdrs, procp)
228
#define xdr_rpcprot(xdrs, protp) xdr_u_int32_t(xdrs, protp)
229
#define xdr_rpcport(xdrs, portp) xdr_u_int32_t(xdrs, portp)
230
231
/*
232
 * Support struct for discriminated unions.
233
 * You create an array of xdrdiscrim structures, terminated with
234
 * an entry with a null procedure pointer.  The xdr_union routine gets
235
 * the discriminant value and then searches the array of structures
236
 * for a matching value.  If a match is found the associated xdr routine
237
 * is called to handle that part of the union.  If there is
238
 * no match, then a default routine may be called.
239
 * If there is no match and no default routine it is an error.
240
 */
241
#define NULL_xdrproc_t ((xdrproc_t)0)
242
struct xdr_discrim {
243
  int value;
244
  xdrproc_t proc;
245
};
246
247
/*
248
 * In-line routines for fast encode/decode of primitive data types.
249
 * Caveat emptor: these use single memory cycles to get the
250
 * data from the underlying buffer, and will fail to operate
251
 * properly if the data is not aligned.  The standard way to use these
252
 * is to say:
253
 *  if ((buf = XDR_INLINE(xdrs, count)) == NULL)
254
 *    return (FALSE);
255
 *  <<< macro calls >>>
256
 * where ``count'' is the number of bytes of data occupied
257
 * by the primitive data types.
258
 *
259
 * N.B. and frozen for all time: each data type here uses 4 bytes
260
 * of external representation.
261
 */
262
#define IXDR_GET_INT32(buf)   ((int32_t)ntohl((u_int32_t)*(buf)++))
263
#define IXDR_PUT_INT32(buf, v)    (*(buf)++ =(int32_t)htonl((u_int32_t)v))
264
#define IXDR_GET_U_INT32(buf)   ((u_int32_t)IXDR_GET_INT32(buf))
265
#define IXDR_PUT_U_INT32(buf, v)  IXDR_PUT_INT32((buf), ((int32_t)(v)))
266
267
#define IXDR_GET_LONG(buf)    ((long)ntohl((u_int32_t)*(buf)++))
268
#define IXDR_PUT_LONG(buf, v)   (*(buf)++ =(int32_t)htonl((u_int32_t)v))
269
270
#define IXDR_GET_BOOL(buf)    ((bool_t)IXDR_GET_LONG(buf))
271
#define IXDR_GET_ENUM(buf, t)   ((t)IXDR_GET_LONG(buf))
272
#define IXDR_GET_U_LONG(buf)    ((u_long)IXDR_GET_LONG(buf))
273
#define IXDR_GET_SHORT(buf)   ((short)IXDR_GET_LONG(buf))
274
#define IXDR_GET_U_SHORT(buf)   ((u_short)IXDR_GET_LONG(buf))
275
276
#define IXDR_PUT_BOOL(buf, v)   IXDR_PUT_LONG((buf), (v))
277
#define IXDR_PUT_ENUM(buf, v)   IXDR_PUT_LONG((buf), (v))
278
#define IXDR_PUT_U_LONG(buf, v)   IXDR_PUT_LONG((buf), (v))
279
#define IXDR_PUT_SHORT(buf, v)    IXDR_PUT_LONG((buf), (v))
280
#define IXDR_PUT_U_SHORT(buf, v)  IXDR_PUT_LONG((buf), (v))
281
282
/*
283
 * These are the "generic" xdr routines.
284
 */
285
#ifdef __cplusplus
286
extern "C" {
287
#endif
288
extern bool_t xdr_void(void);
289
extern bool_t xdr_int(XDR *, int *);
290
extern bool_t xdr_u_int(XDR *, u_int *);
291
extern bool_t xdr_long(XDR *, long *);
292
extern bool_t xdr_u_long(XDR *, u_long *);
293
extern bool_t xdr_short(XDR *, short *);
294
extern bool_t xdr_u_short(XDR *, u_short *);
295
extern bool_t xdr_int8_t(XDR *, int8_t *);
296
extern bool_t xdr_u_int8_t(XDR *, uint8_t *);
297
extern bool_t xdr_uint8_t(XDR *, uint8_t *);
298
extern bool_t xdr_int16_t(XDR *, int16_t *);
299
extern bool_t xdr_u_int16_t(XDR *, u_int16_t *);
300
extern bool_t xdr_uint16_t(XDR *, uint16_t *);
301
extern bool_t xdr_int32_t(XDR *, int32_t *);
302
extern bool_t xdr_u_int32_t(XDR *, u_int32_t *);
303
extern bool_t xdr_uint32_t(XDR *, uint32_t *);
304
extern bool_t xdr_int64_t(XDR *, int64_t *);
305
extern bool_t xdr_u_int64_t(XDR *, u_int64_t *);
306
extern bool_t xdr_uint64_t(XDR *, uint64_t *);
307
extern bool_t xdr_quad_t(XDR *, int64_t *);
308
extern bool_t xdr_u_quad_t(XDR *, u_int64_t *);
309
extern bool_t xdr_bool(XDR *, bool_t *);
310
extern bool_t xdr_enum(XDR *, enum_t *);
311
extern bool_t xdr_array(XDR *, char **, u_int *, u_int, u_int, xdrproc_t);
312
extern bool_t xdr_bytes(XDR *, char **, u_int *, u_int);
313
extern bool_t xdr_opaque(XDR *, char *, u_int);
314
extern bool_t xdr_string(XDR *, char **, u_int);
315
extern bool_t xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t);
316
extern bool_t xdr_char(XDR *, char *);
317
extern bool_t xdr_u_char(XDR *, u_char *);
318
extern bool_t xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
319
extern bool_t xdr_float(XDR *, float *);
320
extern bool_t xdr_double(XDR *, double *);
321
extern bool_t xdr_quadruple(XDR *, long double *);
322
extern bool_t xdr_reference(XDR *, char **, u_int, xdrproc_t);
323
extern bool_t xdr_pointer(XDR *, char **, u_int, xdrproc_t);
324
extern bool_t xdr_wrapstring(XDR *, char **);
325
extern void xdr_free(xdrproc_t, void *);
326
extern bool_t xdr_hyper(XDR *, quad_t *);
327
extern bool_t xdr_u_hyper(XDR *, u_quad_t *);
328
extern bool_t xdr_longlong_t(XDR *, quad_t *);
329
extern bool_t xdr_u_longlong_t(XDR *, u_quad_t *);
330
extern u_long xdr_sizeof(xdrproc_t, void *);
331
#ifdef __cplusplus
332
}
333
#endif
334
335
/*
336
 * Common opaque bytes objects used by many rpc protocols;
337
 * declared here due to commonality.
338
 */
339
#define MAX_NETOBJ_SZ 1024
340
struct netobj {
341
  u_int n_len;
342
  char  *n_bytes;
343
};
344
typedef struct netobj netobj;
345
extern bool_t   xdr_netobj(XDR *, struct netobj *);
346
347
/*
348
 * These are the public routines for the various implementations of
349
 * xdr streams.
350
 */
351
#ifdef __cplusplus
352
extern "C" {
353
#endif
354
/* XDR using memory buffers */
355
extern void   xdrmem_create(XDR *, char *, u_int, enum xdr_op);
356
357
/* XDR using stdio library */
358
extern void   xdrstdio_create(XDR *, FILE *, enum xdr_op);
359
360
/* XDR pseudo records for tcp */
361
extern void   xdrrec_create(XDR *, u_int, u_int, void *,
362
          int (*)(void *, void *, int),
363
          int (*)(void *, void *, int));
364
365
/* make end of xdr record */
366
extern bool_t xdrrec_endofrecord(XDR *, int);
367
368
/* move to beginning of next record */
369
extern bool_t xdrrec_skiprecord(XDR *);
370
371
/* true if no more input */
372
extern bool_t xdrrec_eof(XDR *);
373
extern u_int xdrrec_readbytes(XDR *, caddr_t, u_int);
374
#ifdef __cplusplus
375
}
376
#endif
377
378
#endif /* !_TIRPC_XDR_H */