xdr.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC */
  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, MERCHANTIBILITY 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. /* @(#)xdr.h 1.19 87/04/22 SMI */
  31. /*
  32. * xdr.h, External Data Representation Serialization Routines.
  33. *
  34. * Copyright (C) 1984, Sun Microsystems, Inc.
  35. */
  36. #ifndef __XDR_HEADER__
  37. #define __XDR_HEADER__
  38. /*
  39. * XDR provides a conventional way for converting between C data
  40. * types and an external bit-string representation. Library supplied
  41. * routines provide for the conversion on built-in C data types. These
  42. * routines and utility routines defined here are used to help implement
  43. * a type encode/decode routine for each user-defined type.
  44. *
  45. * Each data type provides a single procedure which takes two arguments:
  46. *
  47. * bool_t
  48. * xdrproc(xdrs, argresp)
  49. * XDR *xdrs;
  50. * <type> *argresp;
  51. *
  52. * xdrs is an instance of a XDR handle, to which or from which the data
  53. * type is to be converted. argresp is a pointer to the structure to be
  54. * converted. The XDR handle contains an operation field which indicates
  55. * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  56. *
  57. * XDR_DECODE may allocate space if the pointer argresp is null. This
  58. * data can be freed with the XDR_FREE operation.
  59. *
  60. * We write only one procedure per data type to make it easy
  61. * to keep the encode and decode procedures for a data type consistent.
  62. * In many cases the same code performs all operations on a user defined type,
  63. * because all the hard work is done in the component type routines.
  64. * decode as a series of calls on the nested data types.
  65. */
  66. /*
  67. * Xdr operations. XDR_ENCODE causes the type to be encoded into the
  68. * stream. XDR_DECODE causes the type to be extracted from the stream.
  69. * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  70. * request.
  71. */
  72. enum xdr_op {
  73. XDR_ENCODE=0,
  74. XDR_DECODE=1,
  75. XDR_FREE=2
  76. };
  77. /*
  78. * This is the number of bytes per unit of external data.
  79. */
  80. #define BYTES_PER_XDR_UNIT (4)
  81. #define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  82. * BYTES_PER_XDR_UNIT)
  83. /*
  84. * A xdrproc_t exists for each data type which is to be encoded or decoded.
  85. *
  86. * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  87. * The opaque pointer generally points to a structure of the data type
  88. * to be decoded. If this pointer is 0, then the type routines should
  89. * allocate dynamic storage of the appropriate size and return it.
  90. * bool_t (*xdrproc_t)(XDR *, caddr_t *);
  91. */
  92. typedef bool_t (*xdrproc_t)();
  93. /*
  94. * The XDR handle.
  95. * Contains operation which is being applied to the stream,
  96. * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  97. * and two private fields for the use of the particular impelementation.
  98. */
  99. typedef struct {
  100. enum xdr_op x_op; /* operation; fast additional param */
  101. struct xdr_ops {
  102. bool_t (*x_getlong)(); /* get a long from underlying stream */
  103. bool_t (*x_putlong)(); /* put a long to " */
  104. bool_t (*x_getbytes)();/* get some bytes from " */
  105. bool_t (*x_putbytes)();/* put some bytes to " */
  106. u_int (*x_getpostn)();/* returns bytes off from beginning */
  107. bool_t (*x_setpostn)();/* lets you reposition the stream */
  108. long * (*x_inline)(); /* buf quick ptr to buffered data */
  109. void (*x_destroy)(); /* free privates of this xdr_stream */
  110. } *x_ops;
  111. caddr_t x_public; /* users' data */
  112. caddr_t x_private; /* pointer to private data */
  113. caddr_t x_base; /* private used for position info */
  114. int x_handy; /* extra private word */
  115. } XDR;
  116. /*
  117. * Operations defined on a XDR handle
  118. *
  119. * XDR *xdrs;
  120. * long *longp;
  121. * caddr_t addr;
  122. * u_int len;
  123. * u_int pos;
  124. */
  125. #define XDR_GETLONG(xdrs, longp) \
  126. (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  127. #define xdr_getlong(xdrs, longp) \
  128. (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  129. #define XDR_PUTLONG(xdrs, longp) \
  130. (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  131. #define xdr_putlong(xdrs, longp) \
  132. (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  133. #define XDR_GETBYTES(xdrs, addr, len) \
  134. (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  135. #define xdr_getbytes(xdrs, addr, len) \
  136. (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  137. #define XDR_PUTBYTES(xdrs, addr, len) \
  138. (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  139. #define xdr_putbytes(xdrs, addr, len) \
  140. (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  141. #define XDR_GETPOS(xdrs) \
  142. (*(xdrs)->x_ops->x_getpostn)(xdrs)
  143. #define xdr_getpos(xdrs) \
  144. (*(xdrs)->x_ops->x_getpostn)(xdrs)
  145. #define XDR_SETPOS(xdrs, pos) \
  146. (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  147. #define xdr_setpos(xdrs, pos) \
  148. (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  149. #define XDR_INLINE(xdrs, len) \
  150. (*(xdrs)->x_ops->x_inline)(xdrs, len)
  151. #define xdr_inline(xdrs, len) \
  152. (*(xdrs)->x_ops->x_inline)(xdrs, len)
  153. #define XDR_DESTROY(xdrs) \
  154. if ((xdrs)->x_ops->x_destroy) \
  155. (*(xdrs)->x_ops->x_destroy)(xdrs)
  156. #define xdr_destroy(xdrs) \
  157. if ((xdrs)->x_ops->x_destroy) \
  158. (*(xdrs)->x_ops->x_destroy)(xdrs)
  159. /*
  160. * Support struct for discriminated unions.
  161. * You create an array of xdrdiscrim structures, terminated with
  162. * a entry with a null procedure pointer. The xdr_union routine gets
  163. * the discriminant value and then searches the array of structures
  164. * for a matching value. If a match is found the associated xdr routine
  165. * is called to handle that part of the union. If there is
  166. * no match, then a default routine may be called.
  167. * If there is no match and no default routine it is an error.
  168. */
  169. #define NULL_xdrproc_t ((xdrproc_t)0)
  170. struct xdr_discrim {
  171. int value;
  172. xdrproc_t proc;
  173. };
  174. /*
  175. * In-line routines for fast encode/decode of primitve data types.
  176. * Caveat emptor: these use single memory cycles to get the
  177. * data from the underlying buffer, and will fail to operate
  178. * properly if the data is not aligned. The standard way to use these
  179. * is to say:
  180. * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  181. * return (FALSE);
  182. * <<< macro calls >>>
  183. * where ``count'' is the number of bytes of data occupied
  184. * by the primitive data types.
  185. *
  186. * N.B. and frozen for all time: each data type here uses 4 bytes
  187. * of external representation.
  188. */
  189. #define IXDR_GET_LONG(buf) ((long)ntohl((u_long)*(buf)++))
  190. #define IXDR_PUT_LONG(buf, v) (*(buf)++ = (long)htonl((u_long)v))
  191. #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
  192. #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
  193. #define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
  194. #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
  195. #define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
  196. #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
  197. #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
  198. #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
  199. #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
  200. #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
  201. /*
  202. * These are the "generic" xdr routines.
  203. */
  204. extern bool_t xdr_void();
  205. extern bool_t xdr_int();
  206. extern bool_t xdr_u_int();
  207. extern bool_t xdr_long();
  208. extern bool_t xdr_u_long();
  209. extern bool_t xdr_short();
  210. extern bool_t xdr_u_short();
  211. extern bool_t xdr_bool();
  212. extern bool_t xdr_enum();
  213. extern bool_t xdr_array();
  214. extern bool_t xdr_bytes();
  215. extern bool_t xdr_opaque();
  216. extern bool_t xdr_string();
  217. extern bool_t xdr_union();
  218. extern bool_t xdr_char();
  219. extern bool_t xdr_u_char();
  220. extern bool_t xdr_vector();
  221. extern bool_t xdr_float();
  222. extern bool_t xdr_double();
  223. extern bool_t xdr_reference();
  224. extern bool_t xdr_pointer();
  225. extern bool_t xdr_wrapstring();
  226. /*
  227. * Common opaque bytes objects used by many rpc protocols;
  228. * declared here due to commonality.
  229. */
  230. #define MAX_NETOBJ_SZ 1024
  231. struct netobj {
  232. u_int n_len;
  233. char *n_bytes;
  234. };
  235. typedef struct netobj netobj;
  236. extern bool_t xdr_netobj();
  237. /*
  238. * These are the public routines for the various implementations of
  239. * xdr streams.
  240. */
  241. extern void xdrmem_create(); /* XDR using memory buffers */
  242. extern void xdrstdio_create(); /* XDR using stdio library */
  243. extern void xdrrec_create(); /* XDR pseudo records for tcp */
  244. extern bool_t xdrrec_endofrecord(); /* make end of xdr record */
  245. extern bool_t xdrrec_skiprecord(); /* move to beginning of next record */
  246. extern bool_t xdrrec_eof(); /* true if no more input */
  247. #endif !__XDR_HEADER__