1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 */ 9 /* 10 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 11 * unrestricted use provided that this legend is included on all tape 12 * media and as a part of the software program in whole or part. Users 13 * may copy or modify Sun RPC without charge, but are not authorized 14 * to license or distribute it to anyone else except as part of a product or 15 * program developed by the user. 16 * 17 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 18 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 20 * 21 * Sun RPC is provided with no support and without any obligation on the 22 * part of Sun Microsystems, Inc. to assist in its use, correction, 23 * modification or enhancement. 24 * 25 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 26 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 27 * OR ANY PART THEREOF. 28 * 29 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 30 * or profits or other special, indirect and consequential damages, even if 31 * Sun has been advised of the possibility of such damages. 32 * 33 * Sun Microsystems, Inc. 34 * 2550 Garcia Avenue 35 * Mountain View, California 94043 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 #include <rpc/types.h> 48 49 /* We need FILE. */ 50 #include <stdio.h> 51 52 /* 53 * XDR provides a conventional way for converting between C data 54 * types and an external bit-string representation. Library supplied 55 * routines provide for the conversion on built-in C data types. These 56 * routines and utility routines defined here are used to help implement 57 * a type encode/decode routine for each user-defined type. 58 * 59 * Each data type provides a single procedure which takes two arguments: 60 * 61 * bool_t 62 * xdrproc(xdrs, argresp) 63 * XDR *xdrs; 64 * <type> *argresp; 65 * 66 * xdrs is an instance of a XDR handle, to which or from which the data 67 * type is to be converted. argresp is a pointer to the structure to be 68 * converted. The XDR handle contains an operation field which indicates 69 * which of the operations (ENCODE, DECODE * or FREE) is to be performed. 70 * 71 * XDR_DECODE may allocate space if the pointer argresp is null. This 72 * data can be freed with the XDR_FREE operation. 73 * 74 * We write only one procedure per data type to make it easy 75 * to keep the encode and decode procedures for a data type consistent. 76 * In many cases the same code performs all operations on a user defined type, 77 * because all the hard work is done in the component type routines. 78 * decode as a series of calls on the nested data types. 79 */ 80 81 /* 82 * Xdr operations. XDR_ENCODE causes the type to be encoded into the 83 * stream. XDR_DECODE causes the type to be extracted from the stream. 84 * XDR_FREE can be used to release the space allocated by an XDR_DECODE 85 * request. 86 */ 87 enum xdr_op { 88 XDR_ENCODE = 0, 89 XDR_DECODE = 1, 90 XDR_FREE = 2 91 }; 92 93 /* 94 * This is the number of bytes per unit of external data. 95 */ 96 #define BYTES_PER_XDR_UNIT (4) 97 /* 98 * This only works if the above is a power of 2. But it's defined to be 99 * 4 by the appropriate RFCs. So it will work. And it's normally quicker 100 * than the old routine. 101 */ 102 #define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1)) 103 104 /* 105 * The XDR handle. 106 * Contains operation which is being applied to the stream, 107 * an operations vector for the particular implementation (e.g. see xdr_mem.c), 108 * and two private fields for the use of the particular implementation. 109 */ 110 typedef struct XDR XDR; 111 struct XDR 112 { 113 enum xdr_op x_op; /* operation; fast additional param */ 114 struct xdr_ops 115 { 116 bool_t (*x_getlong) (XDR *__xdrs, long *__lp); 117 /* get a long from underlying stream */ 118 bool_t (*x_putlong) (XDR *__xdrs, const long *__lp); 119 /* put a long to " */ 120 bool_t (*x_getbytes) (XDR *__xdrs, char* __addr, unsigned int __len); 121 /* get some bytes from " */ 122 bool_t (*x_putbytes) (XDR *__xdrs, const char *__addr, unsigned int __len); 123 /* put some bytes to " */ 124 unsigned int (*x_getpostn) (const XDR *__xdrs); 125 /* returns bytes off from beginning */ 126 bool_t (*x_setpostn) (XDR *__xdrs, unsigned int __pos); 127 /* lets you reposition the stream */ 128 int32_t *(*x_inline) (XDR *__xdrs, unsigned int __len); 129 /* buf quick ptr to buffered data */ 130 void (*x_destroy) (XDR *__xdrs); 131 /* free privates of this xdr_stream */ 132 bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip); 133 /* get a int from underlying stream */ 134 bool_t (*x_putint32) (XDR *__xdrs, const int32_t *__ip); 135 /* put a int to " */ 136 } 137 *x_ops; 138 char* x_public; /* users' data */ 139 char* x_private; /* pointer to private data */ 140 char* x_base; /* private used for position info */ 141 unsigned int x_handy; /* extra private word */ 142 }; 143 144 /* 145 * A xdrproc_t exists for each data type which is to be encoded or decoded. 146 * 147 * The second argument to the xdrproc_t is a pointer to an opaque pointer. 148 * The opaque pointer generally points to a structure of the data type 149 * to be decoded. If this pointer is 0, then the type routines should 150 * allocate dynamic storage of the appropriate size and return it. 151 * bool_t (*xdrproc_t)(XDR *, char* *); 152 */ 153 typedef bool_t (*xdrproc_t) (XDR *, void *,...); 154 155 156 /* 157 * Operations defined on a XDR handle 158 * 159 * XDR *xdrs; 160 * int32_t *int32p; 161 * long *longp; 162 * char* addr; 163 * unsigned int len; 164 * unsigned int pos; 165 */ 166 #define XDR_GETINT32(xdrs, int32p) \ 167 (*(xdrs)->x_ops->x_getint32)(xdrs, int32p) 168 #define xdr_getint32(xdrs, int32p) \ 169 (*(xdrs)->x_ops->x_getint32)(xdrs, int32p) 170 171 #define XDR_PUTINT32(xdrs, int32p) \ 172 (*(xdrs)->x_ops->x_putint32)(xdrs, int32p) 173 #define xdr_putint32(xdrs, int32p) \ 174 (*(xdrs)->x_ops->x_putint32)(xdrs, int32p) 175 176 #define XDR_GETLONG(xdrs, longp) \ 177 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 178 #define xdr_getlong(xdrs, longp) \ 179 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 180 181 #define XDR_PUTLONG(xdrs, longp) \ 182 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 183 #define xdr_putlong(xdrs, longp) \ 184 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 185 186 #define XDR_GETBYTES(xdrs, addr, len) \ 187 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 188 #define xdr_getbytes(xdrs, addr, len) \ 189 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 190 191 #define XDR_PUTBYTES(xdrs, addr, len) \ 192 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 193 #define xdr_putbytes(xdrs, addr, len) \ 194 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 195 196 #define XDR_GETPOS(xdrs) \ 197 (*(xdrs)->x_ops->x_getpostn)(xdrs) 198 #define xdr_getpos(xdrs) \ 199 (*(xdrs)->x_ops->x_getpostn)(xdrs) 200 201 #define XDR_SETPOS(xdrs, pos) \ 202 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 203 #define xdr_setpos(xdrs, pos) \ 204 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 205 206 #define XDR_INLINE(xdrs, len) \ 207 (*(xdrs)->x_ops->x_inline)(xdrs, len) 208 #define xdr_inline(xdrs, len) \ 209 (*(xdrs)->x_ops->x_inline)(xdrs, len) 210 211 #define XDR_DESTROY(xdrs) \ 212 do { \ 213 if ((xdrs)->x_ops->x_destroy) \ 214 (*(xdrs)->x_ops->x_destroy)(xdrs); \ 215 } while (0) 216 #define xdr_destroy(xdrs) \ 217 do { \ 218 if ((xdrs)->x_ops->x_destroy) \ 219 (*(xdrs)->x_ops->x_destroy)(xdrs); \ 220 } while (0) 221 222 /* 223 * Support struct for discriminated unions. 224 * You create an array of xdrdiscrim structures, terminated with 225 * a entry with a null procedure pointer. The xdr_union routine gets 226 * the discriminant value and then searches the array of structures 227 * for a matching value. If a match is found the associated xdr routine 228 * is called to handle that part of the union. If there is 229 * no match, then a default routine may be called. 230 * If there is no match and no default routine it is an error. 231 */ 232 #define NULL_xdrproc_t ((xdrproc_t)0) 233 struct xdr_discrim 234 { 235 int value; 236 xdrproc_t proc; 237 }; 238 239 /* 240 * Inline routines for fast encode/decode of primitive data types. 241 * Caveat emptor: these use single memory cycles to get the 242 * data from the underlying buffer, and will fail to operate 243 * properly if the data is not aligned. The standard way to use these 244 * is to say: 245 * if ((buf = XDR_INLINE(xdrs, count)) == NULL) 246 * return (FALSE); 247 * <<< macro calls >>> 248 * where ``count'' is the number of bytes of data occupied 249 * by the primitive data types. 250 * 251 * N.B. and frozen for all time: each data type here uses 4 bytes 252 * of external representation. 253 */ 254 255 #define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++)) 256 #define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v))) 257 #define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf)) 258 #define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v)) 259 260 /* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms 261 * and shouldn't be used any longer. Code which use this defines or longs 262 * in the RPC code will not work on 64bit Solaris platforms ! 263 */ 264 #define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf)) 265 #define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v))) 266 #define IXDR_GET_U_LONG(buf) ((unsigned long)IXDR_GET_LONG(buf)) 267 #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v)) 268 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_SHORT(buf) ((short)IXDR_GET_LONG(buf)) 273 #define IXDR_GET_U_SHORT(buf) ((unsigned short)IXDR_GET_LONG(buf)) 274 275 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v)) 276 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v)) 277 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v)) 278 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v)) 279 280 /* 281 * These are the "generic" xdr routines. 282 * None of these can have const applied because it's not possible to 283 * know whether the call is a read or a write to the passed parameter 284 * also, the XDR structure is always updated by some of these calls. 285 */ 286 extern bool_t xdr_void (void); 287 extern bool_t xdr_short (XDR *__xdrs, short *__sp); 288 extern bool_t xdr_u_short (XDR *__xdrs, unsigned short *__usp); 289 extern bool_t xdr_int (XDR *__xdrs, int *__ip); 290 extern bool_t xdr_u_int (XDR *__xdrs, unsigned int *__up); 291 extern bool_t xdr_long (XDR *__xdrs, long *__lp); 292 extern bool_t xdr_u_long (XDR *__xdrs, unsigned long *__ulp); 293 extern bool_t xdr_hyper (XDR *__xdrs, int64_t *__llp); 294 extern bool_t xdr_u_hyper (XDR *__xdrs, uint64_t *__ullp); 295 extern bool_t xdr_longlong_t (XDR *__xdrs, int64_t *__llp); 296 extern bool_t xdr_u_longlong_t (XDR *__xdrs, uint64_t *__ullp); 297 extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip); 298 extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up); 299 extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip); 300 extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up); 301 extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip); 302 extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up); 303 extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip); 304 extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up); 305 extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp); 306 extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep); 307 extern bool_t xdr_array (XDR * _xdrs, char* *__addrp, unsigned int *__sizep, 308 unsigned int __maxsize, unsigned int __elsize, xdrproc_t __elproc); 309 extern bool_t xdr_bytes (XDR *xdrs, char **cpp, unsigned int *sizep, 310 unsigned int maxsize); 311 extern bool_t xdr_opaque (XDR *__xdrs, char* __cp, unsigned int __cnt); 312 extern bool_t xdr_string (XDR *xdrs, char **cpp, unsigned int maxsize); 313 extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp, 314 const struct xdr_discrim *__choices, 315 xdrproc_t dfault); 316 extern bool_t xdr_char (XDR *__xdrs, char *__cp); 317 extern bool_t xdr_u_char (XDR *__xdrs, unsigned char *__cp); 318 extern bool_t xdr_vector (XDR *__xdrs, char *__basep, unsigned int __nelem, 319 unsigned int __elemsize, xdrproc_t __xdr_elem); 320 extern bool_t xdr_float (XDR *__xdrs, float *__fp); 321 extern bool_t xdr_double (XDR *__xdrs, double *__dp); 322 extern bool_t xdr_reference (XDR *__xdrs, char* *__xpp, unsigned int __size, 323 xdrproc_t __proc); 324 extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp, 325 unsigned int __obj_size, xdrproc_t __xdr_obj); 326 extern bool_t xdr_wrapstring (XDR *__xdrs, char **cpp); 327 extern unsigned long xdr_sizeof (xdrproc_t, void *); 328 329 /* 330 * Common opaque bytes objects used by many rpc protocols; 331 * declared here due to commonality. 332 */ 333 #define MAX_NETOBJ_SZ 1024 334 struct netobj 335 { 336 unsigned int n_len; 337 char *n_bytes; 338 }; 339 typedef struct netobj netobj; 340 extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np); 341 342 /* 343 * These are the public routines for the various implementations of 344 * xdr streams. 345 */ 346 347 /* XDR using memory buffers */ 348 extern void xdrmem_create (XDR *__xdrs, const char* __addr, 349 unsigned int __size, enum xdr_op __xop); 350 351 /* XDR pseudo records for tcp */ 352 extern void xdrrec_create (XDR *__xdrs, unsigned int __sendsize, 353 unsigned int __recvsize, char* __tcp_handle, 354 int (*__readit) (char *, char *, int), 355 int (*__writeit) (char *, char *, int)); 356 357 /* make end of xdr record */ 358 extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow); 359 360 /* move to beginning of next record */ 361 extern bool_t xdrrec_skiprecord (XDR *__xdrs); 362 363 /* true if no more input */ 364 extern bool_t xdrrec_eof (XDR *__xdrs); 365 366 /* free memory buffers for xdr */ 367 extern void xdr_free (xdrproc_t __proc, char *__objp); 368 369 #endif /* rpc/xdr.h */ 370