PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/sys/rpc/xdr.h

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