xdr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC */
  2. /*
  3. * Copyright (c) 2010, Oracle America, Inc.
  4. *
  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. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * * Neither the name of the "Oracle America, Inc." nor the names of
  19. * its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  24. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  25. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  28. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /* @(#)xdr.h 1.19 87/04/22 SMI */
  35. /*
  36. * xdr.h, External Data Representation Serialization Routines.
  37. */
  38. #ifndef GSSRPC_XDR_H
  39. #define GSSRPC_XDR_H
  40. #include <stdio.h> /* for FILE */
  41. GSSRPC__BEGIN_DECLS
  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. * A xdrproc_t exists for each data type which is to be encoded or decoded.
  89. *
  90. * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  91. * The opaque pointer generally points to a structure of the data type
  92. * to be decoded. If this pointer is 0, then the type routines should
  93. * allocate dynamic storage of the appropriate size and return it.
  94. * bool_t (*xdrproc_t)(XDR *, caddr_t *);
  95. *
  96. * XXX can't actually prototype it, because some take three args!!!
  97. */
  98. typedef bool_t (*xdrproc_t)();
  99. /*
  100. * The XDR handle.
  101. * Contains operation which is being applied to the stream,
  102. * an operations vector for the particular implementation (e.g. see xdr_mem.c),
  103. * and two private fields for the use of the particular impelementation.
  104. */
  105. typedef struct XDR {
  106. enum xdr_op x_op; /* operation; fast additional param */
  107. struct xdr_ops {
  108. /* get a long from underlying stream */
  109. bool_t (*x_getlong)(struct XDR *, long *);
  110. /* put a long to underlying stream */
  111. bool_t (*x_putlong)(struct XDR *, long *);
  112. /* get some bytes from underlying stream */
  113. bool_t (*x_getbytes)(struct XDR *, caddr_t, u_int);
  114. /* put some bytes to underlying stream */
  115. bool_t (*x_putbytes)(struct XDR *, caddr_t, u_int);
  116. /* returns bytes off from beginning */
  117. u_int (*x_getpostn)(struct XDR *);
  118. /* lets you reposition the stream */
  119. bool_t (*x_setpostn)(struct XDR *, u_int);
  120. /* buf quick ptr to buffered data */
  121. rpc_inline_t *(*x_inline)(struct XDR *, int);
  122. /* free privates of this xdr_stream */
  123. void (*x_destroy)(struct XDR *);
  124. } *x_ops;
  125. caddr_t x_public; /* users' data */
  126. void * x_private; /* pointer to private data */
  127. caddr_t x_base; /* private used for position info */
  128. int x_handy; /* extra private word */
  129. } XDR;
  130. /*
  131. * Operations defined on a XDR handle
  132. *
  133. * XDR *xdrs;
  134. * int32_t *longp;
  135. * caddr_t addr;
  136. * u_int len;
  137. * u_int pos;
  138. */
  139. #define XDR_GETLONG(xdrs, longp) \
  140. (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  141. #define xdr_getlong(xdrs, longp) \
  142. (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  143. #define XDR_PUTLONG(xdrs, longp) \
  144. (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  145. #define xdr_putlong(xdrs, longp) \
  146. (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  147. #define XDR_GETBYTES(xdrs, addr, len) \
  148. (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  149. #define xdr_getbytes(xdrs, addr, len) \
  150. (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  151. #define XDR_PUTBYTES(xdrs, addr, len) \
  152. (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  153. #define xdr_putbytes(xdrs, addr, len) \
  154. (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  155. #define XDR_GETPOS(xdrs) \
  156. (*(xdrs)->x_ops->x_getpostn)(xdrs)
  157. #define xdr_getpos(xdrs) \
  158. (*(xdrs)->x_ops->x_getpostn)(xdrs)
  159. #define XDR_SETPOS(xdrs, pos) \
  160. (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  161. #define xdr_setpos(xdrs, pos) \
  162. (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  163. #define XDR_INLINE(xdrs, len) \
  164. (*(xdrs)->x_ops->x_inline)(xdrs, len)
  165. #define xdr_inline(xdrs, len) \
  166. (*(xdrs)->x_ops->x_inline)(xdrs, len)
  167. #define XDR_DESTROY(xdrs) \
  168. if ((xdrs)->x_ops->x_destroy) \
  169. (*(xdrs)->x_ops->x_destroy)(xdrs)
  170. #define xdr_destroy(xdrs) \
  171. if ((xdrs)->x_ops->x_destroy) \
  172. (*(xdrs)->x_ops->x_destroy)(xdrs)
  173. /*
  174. * Support struct for discriminated unions.
  175. * You create an array of xdrdiscrim structures, terminated with
  176. * a entry with a null procedure pointer. The xdr_union routine gets
  177. * the discriminant value and then searches the array of structures
  178. * for a matching value. If a match is found the associated xdr routine
  179. * is called to handle that part of the union. If there is
  180. * no match, then a default routine may be called.
  181. * If there is no match and no default routine it is an error.
  182. */
  183. #define NULL_xdrproc_t ((xdrproc_t)0)
  184. struct xdr_discrim {
  185. int value;
  186. xdrproc_t proc;
  187. };
  188. /*
  189. * In-line routines for fast encode/decode of primitive data types.
  190. * Caveat emptor: these use single memory cycles to get the
  191. * data from the underlying buffer, and will fail to operate
  192. * properly if the data is not aligned. The standard way to use these
  193. * is to say:
  194. * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  195. * return (FALSE);
  196. * <<< macro calls >>>
  197. * where ``count'' is the number of bytes of data occupied
  198. * by the primitive data types.
  199. *
  200. * N.B. and frozen for all time: each data type here uses 4 bytes
  201. * of external representation.
  202. */
  203. #define IXDR_GET_INT32(buf) ((int32_t)IXDR_GET_U_INT32(buf))
  204. #define IXDR_PUT_INT32(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
  205. #define IXDR_GET_U_INT32(buf) (ntohl((uint32_t)*(buf)++))
  206. #define IXDR_PUT_U_INT32(buf, v) (*(buf)++ = (int32_t)htonl((v)))
  207. #define IXDR_GET_LONG(buf) ((long)IXDR_GET_INT32(buf))
  208. #define IXDR_PUT_LONG(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
  209. #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
  210. #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_INT32(buf))
  211. #define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_U_INT32(buf))
  212. #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_INT32(buf))
  213. #define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_U_INT32(buf))
  214. #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_INT32((buf),((int32_t)(v)))
  215. #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_INT32((buf),((int32_t)(v)))
  216. #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
  217. #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_INT32((buf),((int32_t)(v)))
  218. #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
  219. /*
  220. * These are the "generic" xdr routines.
  221. */
  222. extern bool_t xdr_void(XDR *, void *);
  223. extern bool_t xdr_int(XDR *, int *);
  224. extern bool_t xdr_u_int(XDR *, u_int *);
  225. extern bool_t xdr_long(XDR *, long *);
  226. extern bool_t xdr_u_long(XDR *, u_long *);
  227. extern bool_t xdr_short(XDR *, short *);
  228. extern bool_t xdr_u_short(XDR *, u_short *);
  229. extern bool_t xdr_bool(XDR *, bool_t *);
  230. extern bool_t xdr_enum(XDR *, enum_t *);
  231. extern bool_t xdr_array(XDR *, caddr_t *, u_int *,
  232. u_int, u_int, xdrproc_t);
  233. extern bool_t xdr_bytes(XDR *, char **, u_int *, u_int);
  234. extern bool_t xdr_opaque(XDR *, caddr_t, u_int);
  235. extern bool_t xdr_string(XDR *, char **, u_int);
  236. extern bool_t xdr_union(XDR *, enum_t *, char *, struct xdr_discrim *,
  237. xdrproc_t);
  238. extern bool_t xdr_char(XDR *, char *);
  239. extern bool_t xdr_u_char(XDR *, u_char *);
  240. extern bool_t xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
  241. extern bool_t xdr_float(XDR *, float *);
  242. extern bool_t xdr_double(XDR *, double *);
  243. extern bool_t xdr_reference(XDR *, caddr_t *, u_int, xdrproc_t);
  244. extern bool_t xdr_pointer(XDR *, char **, u_int, xdrproc_t);
  245. extern bool_t xdr_wrapstring(XDR *, char **);
  246. extern unsigned long xdr_sizeof(xdrproc_t, void *);
  247. #define xdr_rpcprog xdr_u_int32
  248. #define xdr_rpcvers xdr_u_int32
  249. #define xdr_rpcprot xdr_u_int32
  250. #define xdr_rpcproc xdr_u_int32
  251. #define xdr_rpcport xdr_u_int32
  252. /*
  253. * Common opaque bytes objects used by many rpc protocols;
  254. * declared here due to commonality.
  255. */
  256. #define MAX_NETOBJ_SZ 2048
  257. struct netobj {
  258. u_int n_len;
  259. char *n_bytes;
  260. };
  261. typedef struct netobj netobj;
  262. extern bool_t xdr_netobj(XDR *, struct netobj *);
  263. extern bool_t xdr_int32(XDR *, int32_t *);
  264. extern bool_t xdr_u_int32(XDR *, uint32_t *);
  265. /*
  266. * These are the public routines for the various implementations of
  267. * xdr streams.
  268. */
  269. /* XDR allocating memory buffer */
  270. extern void xdralloc_create(XDR *, enum xdr_op);
  271. /* destroy xdralloc, save buf */
  272. extern void xdralloc_release(XDR *);
  273. /* get buffer from xdralloc */
  274. extern caddr_t xdralloc_getdata(XDR *);
  275. /* XDR using memory buffers */
  276. extern void xdrmem_create(XDR *, caddr_t, u_int, enum xdr_op);
  277. /* XDR using stdio library */
  278. extern void xdrstdio_create(XDR *, FILE *, enum xdr_op);
  279. /* XDR pseudo records for tcp */
  280. extern void xdrrec_create(XDR *xdrs, u_int, u_int, caddr_t,
  281. int (*) (caddr_t, caddr_t, int),
  282. int (*) (caddr_t, caddr_t, int));
  283. /* make end of xdr record */
  284. extern bool_t xdrrec_endofrecord(XDR *, bool_t);
  285. /* move to beginning of next record */
  286. extern bool_t xdrrec_skiprecord (XDR *xdrs);
  287. /* true if no more input */
  288. extern bool_t xdrrec_eof (XDR *xdrs);
  289. /* free memory buffers for xdr */
  290. extern void xdr_free (xdrproc_t, void *);
  291. GSSRPC__END_DECLS
  292. #endif /* !defined(GSSRPC_XDR_H) */