xdr.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:15k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* xdr.h, External Data Representation Serialization Routines. */
  2. /* Copyright 1984 - 1999 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  6.  * unrestricted use provided that this legend is included on all tape
  7.  * media and as a part of the software program in whole or part.  Users
  8.  * may copy or modify Sun RPC without charge, but are not authorized
  9.  * to license or distribute it to anyone else except as part of a product or
  10.  * program developed by the user.
  11.  *
  12.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  13.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  14.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  15.  *
  16.  * Sun RPC is provided with no support and without any obligation on the
  17.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  18.  * modification or enhancement.
  19.  *
  20.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  21.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  22.  * OR ANY PART THEREOF.
  23.  *
  24.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  25.  * or profits or other special, indirect and consequential damages, even if
  26.  * Sun has been advised of the possibility of such damages.
  27.  *
  28.  * Sun Microsystems, Inc.
  29.  * 2550 Garcia Avenue
  30.  * Mountain View, California  94043
  31.  */
  32. /*      @(#)xdr.h 1.1 86/02/03 SMI      */
  33. /*
  34.  * xdr.h, External Data Representation Serialization Routines.
  35.  *
  36.  * Copyright (C) 1984, Sun Microsystems, Inc.
  37.  */
  38. /*
  39. modification history
  40. --------------------
  41. 01q,27jul99,elg  Add XDR_PUTWORDS and XDR_PUTLONGS.
  42. 01p,01mar99,spm  added a C++ compatible XDR structure (SPR #24837)
  43. 01o,29jan96,mem  replaced test of CPU_FAMILY with test of _BYTE_ORDER
  44. 01n,09jun93,hdn  added support for I80X86
  45. 01n,17oct94,ism  fixed prototype of xdr_u_char() (SPR#2675)
  46. 01m,22sep92,rrr  added support for c++
  47. 01l,07sep92,smb  added include of rpctypes.h
  48. 01k,26may92,rrr  the tree shuffle
  49. 01j,26may92,ajm  fixed #define of IXDR_GET_LONG for all compilers (see 01g)
  50.   updated copyright
  51. 01i,04oct91,rrr  passed through the ansification filter
  52.   -fixed broken prototype
  53.   -fixed #else and #endif
  54.   -changed copyright notice
  55. 01h,18sep91,wmd  use defined(HOST_MIPS) || defined(HOST_DEC) instead of
  56.  CPU=R3000 for the conditional.
  57. 01g,05aug91,ajm  changed #define of IXDR_GET_LONG to be K+R compliant
  58.                  for mips compiler (++ can't be applied to an expresion)
  59. 01f,05aug91,del  IXDR_GET_LONG is ixdr_get_long function for I960
  60.  problem w/htonl macro expansion.
  61. 01e,24oct90,shl  commented out redundant function declarations.
  62. 01d,05oct90,shl  added ANSI function prototypes.
  63.                  added copyright notice.
  64. 01c,19apr90,hjb  modified IXDR_GET_LONG, IXDR_PUT_LONG, IXDR_PUT_BOOL,
  65.  IXDR_PUT_ENUM, IXDR_PUT_U_LONG, IXDR_PUT_SHORT,
  66.  IXDR_PUT_U_SHORT.
  67. 01b,26oct89,hjb  upgraded to release 4.0
  68. */
  69. #ifndef __INCxdrh
  70. #define __INCxdrh
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. #include "rpctypes.h"
  75. /*
  76.  * XDR provides a conventional way for converting between C data
  77.  * types and an external bit-string representation.  Library supplied
  78.  * routines provide for the conversion on built-in C data types.  These
  79.  * routines and utility routines defined here are used to help implement
  80.  * a type encode/decode routine for each user-defined type.
  81.  *
  82.  * Each data type provides a single procedure which takes two arguments:
  83.  *
  84.  * bool_t
  85.  * xdrproc(xdrs, argresp)
  86.  * XDR *xdrs;
  87.  * <type> *argresp;
  88.  *
  89.  * xdrs is an instance of a XDR handle, to which or from which the data
  90.  * type is to be converted.  argresp is a pointer to the structure to be
  91.  * converted.  The XDR handle contains an operation field which indicates
  92.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  93.  *
  94.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  95.  * data can be freed with the XDR_FREE operation.
  96.  *
  97.  * We write only one procedure per data type to make it easy
  98.  * to keep the encode and decode procedures for a data type consistent.
  99.  * In many cases the same code performs all operations on a user defined type,
  100.  * because all the hard work is done in the component type routines.
  101.  * decode as a series of calls on the nested data types.
  102.  */
  103. /*
  104.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  105.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  106.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  107.  * request.
  108.  */
  109. enum xdr_op {
  110. XDR_ENCODE=0,
  111. XDR_DECODE=1,
  112. XDR_FREE=2
  113. };
  114. /*
  115.  * This is the number of bytes per unit of external data.
  116.  */
  117. #define BYTES_PER_XDR_UNIT (4)
  118. #define RNDUP(x) 
  119. ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) * 
  120. BYTES_PER_XDR_UNIT) /* 4.0 */
  121. /*
  122.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  123.  *
  124.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  125.  * The opaque pointer generally points to a structure of the data type
  126.  * to be decoded.  If this pointer is 0, then the type routines should
  127.  * allocate dynamic storage of the appropriate size and return it.
  128.  * bool_t (*xdrproc_t)(XDR *, caddr_t *);
  129.  */
  130. typedef bool_t (*xdrproc_t)();
  131. /*
  132.  * The XDR handle.
  133.  * Contains operation which is being applied to the stream,
  134.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  135.  * and two private fields for the use of the particular impelementation.
  136.  */
  137. #ifdef __cplusplus
  138. typedef struct {
  139. enum xdr_op x_op; /* operation; fast additional param */
  140. struct xdr_ops {
  141.                     /* get a long from underlying stream */
  142. bool_t (*x_getlong)(...);
  143.                     /* put a long to underlying stream */
  144. bool_t (*x_putlong) (...);
  145.                     /* get some bytes from underlying stream */
  146. bool_t (*x_getbytes) (...);
  147.                     /* put some bytes to underlying stream */
  148. bool_t (*x_putbytes) (...);
  149.                     /* put some words to underlying stream */
  150. bool_t (*x_putwords) (...);
  151.                     /* put some longs to underlying stream */
  152. bool_t (*x_putlongs) (...);
  153.                     /* returns bytes from beginning of stream*/
  154. u_int (*x_getpostn) (...);
  155.                     /* reposition the stream */
  156. bool_t  (*x_setpostn) (...);
  157.                     /* buf quick ptr to buffered data */
  158. long * (*x_inline) (...);
  159.                     /* free private elements of this stream */
  160.                 void (*x_destroy) (...);
  161. } *x_ops;
  162. caddr_t  x_public; /* users' data */
  163. caddr_t x_private; /* pointer to private data */
  164. caddr_t  x_base; /* private used for position info */
  165. int x_handy; /* extra private word */
  166. } XDR;
  167. #else
  168. typedef struct {
  169. enum xdr_op x_op; /* operation; fast additional param */
  170. struct xdr_ops {
  171. bool_t (*x_getlong)(); /* get a long from underlying stream */
  172. bool_t (*x_putlong)(); /* put a long to " */
  173. bool_t (*x_getbytes)();/* get some bytes from " */
  174. bool_t (*x_putbytes)();/* put some bytes to " */
  175. bool_t (*x_putwords)();/* put some words to " */
  176. bool_t (*x_putlongs)();/* put some longs to " */
  177. u_int (*x_getpostn)();/* returns bytes off from beginning */
  178. bool_t  (*x_setpostn)();/* lets you reposition the stream */
  179. long * (*x_inline)(); /* buf quick ptr to buffered data */
  180. void (*x_destroy)(); /* free privates of this xdr_stream */
  181. } *x_ops;
  182. caddr_t  x_public; /* users' data */
  183. caddr_t x_private; /* pointer to private data */
  184. caddr_t  x_base; /* private used for position info */
  185. int x_handy; /* extra private word */
  186. } XDR;
  187. #endif /* __cplusplus */
  188. /*
  189.  * Operations defined on a XDR handle
  190.  *
  191.  * XDR *xdrs;
  192.  * long *longp;
  193.  * caddr_t  addr;
  194.  * u_int  len;
  195.  * u_int  pos;
  196.  */
  197. #define XDR_GETLONG(xdrs, longp)
  198. (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  199. #define xdr_getlong(xdrs, longp)
  200. (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  201. #define XDR_PUTLONG(xdrs, longp)
  202. (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  203. #define xdr_putlong(xdrs, longp)
  204. (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  205. #define XDR_GETBYTES(xdrs, addr, len)
  206. (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  207. #define xdr_getbytes(xdrs, addr, len)
  208. (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  209. #define XDR_PUTBYTES(xdrs, addr, len)
  210. (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  211. #define xdr_putbytes(xdrs, addr, len)
  212. (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  213. #define XDR_PUTWORDS(xdrs, addr, len)
  214. (*(xdrs)->x_ops->x_putwords)(xdrs, addr, len)
  215. #define xdr_putwords(xdrs, addr, len)
  216. (*(xdrs)->x_ops->x_putwords)(xdrs, addr, len)
  217. #define XDR_PUTLONGS(xdrs, addr, len)
  218. (*(xdrs)->x_ops->x_putlongs)(xdrs, addr, len)
  219. #define xdr_putlongs(xdrs, addr, len)
  220. (*(xdrs)->x_ops->x_putlongs)(xdrs, addr, len)
  221. #define XDR_GETPOS(xdrs)
  222. (*(xdrs)->x_ops->x_getpostn)(xdrs)
  223. #define xdr_getpos(xdrs)
  224. (*(xdrs)->x_ops->x_getpostn)(xdrs)
  225. #define XDR_SETPOS(xdrs, pos)
  226. (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  227. #define xdr_setpos(xdrs, pos)
  228. (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  229. #define XDR_INLINE(xdrs, len)
  230. (*(xdrs)->x_ops->x_inline)(xdrs, len)
  231. #define xdr_inline(xdrs, len)
  232. (*(xdrs)->x_ops->x_inline)(xdrs, len)
  233. #define XDR_DESTROY(xdrs)
  234. if ((xdrs)->x_ops->x_destroy) 
  235. (*(xdrs)->x_ops->x_destroy)(xdrs)
  236. #define xdr_destroy(xdrs)
  237. if ((xdrs)->x_ops->x_destroy) 
  238. (*(xdrs)->x_ops->x_destroy)(xdrs)
  239. /*
  240.  * Support struct for discriminated unions.
  241.  * You create an array of xdrdiscrim structures, terminated with
  242.  * a entry with a null procedure pointer.  The xdr_union routine gets
  243.  * the discriminant value and then searches the array of structures
  244.  * for a matching value.  If a match is found the associated xdr routine
  245.  * is called to handle that part of the union.  If there is
  246.  * no match, then a default routine may be called.
  247.  * If there is no match and no default routine it is an error.
  248.  */
  249. #define NULL_xdrproc_t ((xdrproc_t)0)
  250. struct xdr_discrim {
  251. int value;
  252. xdrproc_t proc;
  253. };
  254. /*
  255.  * In-line routines for fast encode/decode of primitve data types.
  256.  * Caveat emptor: these use single memory cycles to get the
  257.  * data from the underlying buffer, and will fail to operate
  258.  * properly if the data is not aligned.  The standard way to use these
  259.  * is to say:
  260.  * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  261.  * return (FALSE);
  262.  * <<< macro calls >>>
  263.  * where ``count'' is the number of bytes of data occupied
  264.  * by the primitive data types.
  265.  *
  266.  * N.B. and frozen for all time: each data type here uses 4 bytes
  267.  * of external representation.
  268.  */
  269. #ifndef _BYTE_ORDER
  270. #error _BYTE_ORDER must be defined.
  271. #endif
  272. #ifndef _BIG_ENDIAN
  273. #error _BIG_ENDIAN must be defined.
  274. #endif
  275. #if  (_BYTE_ORDER == _BIG_ENDIAN)
  276. #define IXDR_GET_LONG(buf)              ntohl((u_long) *(buf)++)
  277. #else
  278. #define IXDR_GET_LONG(buf)              ixdr_get_long (&buf)
  279. #endif /* (_BYTE_ORDER == _BIG_ENDIAN) */
  280. #define IXDR_PUT_LONG(buf, v) (*buf++ = htonl((u_long) (v)))
  281. #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
  282. #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
  283. #define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
  284. #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
  285. #define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
  286. #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), (v))
  287. #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), (v))
  288. #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), (v))
  289. #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
  290. #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
  291. /*
  292.  * Common opaque bytes objects used by many rpc protocols
  293.  * declared here due to commonality.
  294.  */
  295. #define MAX_NETOBJ_SZ 1024
  296. struct netobj
  297.     {
  298.     u_int n_len;
  299.     char *n_bytes;
  300.     };
  301. typedef struct netobj netobj;
  302. /*
  303.  * These are the public routines for the various implementations of
  304.  * xdr streams.
  305.  */
  306. /* extern void   xdrmem_create(); */ /* XDR using memory buffers */
  307. /* extern void   xdrstdio_create(); */ /* XDR using stdio library */
  308. /* extern void   xdrrec_create(); */ /* XDR pseudo records for tcp */
  309. /* extern bool_t xdrrec_endofrecord(); */ /* make end of xdr record */
  310. /* extern bool_t xdrrec_skiprecord(); */   /* move to begining of next record */
  311. /* extern bool_t xdrrec_eof(); */ /* true iff no more input */
  312. /* function declarations */
  313. #if defined(__STDC__) || defined(__cplusplus)
  314. extern   void        xdr_free (xdrproc_t proc, char *objp);
  315. extern   bool_t       xdr_void (void);
  316. extern   bool_t       xdr_int (XDR *xdrs, int *ip);
  317. extern   bool_t       xdr_u_int (XDR *xdrs, u_int *up);
  318. extern   bool_t       xdr_long (XDR *xdrs, long *lp);
  319. extern   bool_t       xdr_u_long (XDR *xdrs, u_long *ulp);
  320. extern   bool_t       xdr_short (XDR *xdrs, short *sp);
  321. extern   bool_t       xdr_u_short (XDR *xdrs, u_short *usp);
  322. extern   bool_t       xdr_char (XDR *xdrs, char *cp);
  323. extern   bool_t       xdr_u_char (XDR *xdrs, u_char *ucp);
  324. extern   bool_t       xdr_bool (XDR *xdrs, bool_t *bp);
  325. extern   bool_t       xdr_enum (XDR *xdrs, enum_t *ep);
  326. extern   bool_t       xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt);
  327. extern   bool_t       xdr_bytes (XDR *xdrs, char **cpp, u_int *sizep,
  328.   u_int maxsize);
  329. extern   bool_t       xdr_netobj (XDR *xdrs, struct netobj *np);
  330. extern   bool_t       xdr_union (XDR *xdrs, enum_t *dscmp, caddr_t unp,
  331.  struct xdr_discrim *choices, xdrproc_t dfault);
  332. extern   bool_t       xdr_string (XDR *xdrs, char **cpp, u_int maxsize);
  333. extern   bool_t       xdr_wrapstring (XDR *xdrs, char **cpp);
  334. extern   bool_t       xdr_array (XDR *xdrs, caddr_t *addrp, u_int *sizep,
  335.  u_int maxsize, u_int elsize, xdrproc_t elproc);
  336. extern   bool_t       xdr_vector (XDR *xdrs, char *basep, u_int nelem,
  337.    u_int elemsize, xdrproc_t xdr_elem);
  338. extern   bool_t       xdr_float (XDR *xdrs, float *fp);
  339. extern   bool_t       xdr_double (XDR *xdrs, double *dp);
  340. extern   void        xdrmem_create (XDR *xdrs, caddr_t addr, u_int size,
  341.       enum xdr_op op);
  342. extern   void        xdrrec_create (XDR *xdrs, u_int sendsize, u_int recvsize,
  343.    caddr_t tcp_handle, int (*readit)(), int (*writeit)());
  344. extern   bool_t       xdrrec_skiprecord (XDR *xdrs);
  345. extern   bool_t       xdrrec_eof (XDR *xdrs);
  346. extern   bool_t       xdrrec_endofrecord (XDR *xdrs, bool_t sendnow);
  347. extern   bool_t       xdr_reference (XDR *xdrs, caddr_t *pp, u_int size,
  348.       xdrproc_t proc);
  349. extern   bool_t       xdr_pointer (XDR *xdrs, char **objpp, u_int obj_size,
  350.     xdrproc_t xdr_obj);
  351. #else
  352. extern   void        xdr_free ();
  353. extern   bool_t       xdr_void ();
  354. extern   bool_t       xdr_int ();
  355. extern   bool_t       xdr_u_int ();
  356. extern   bool_t       xdr_long ();
  357. extern   bool_t       xdr_u_long ();
  358. extern   bool_t       xdr_short ();
  359. extern   bool_t       xdr_u_short ();
  360. extern   bool_t       xdr_char ();
  361. extern   bool_t       xdr_u_char ();
  362. extern   bool_t       xdr_bool ();
  363. extern   bool_t       xdr_enum ();
  364. extern   bool_t       xdr_opaque ();
  365. extern   bool_t       xdr_bytes ();
  366. extern   bool_t       xdr_netobj ();
  367. extern   bool_t       xdr_union ();
  368. extern   bool_t       xdr_string ();
  369. extern   bool_t       xdr_wrapstring ();
  370. extern   bool_t       xdr_array ();
  371. extern   bool_t       xdr_vector ();
  372. extern   bool_t       xdr_float ();
  373. extern   bool_t       xdr_double ();
  374. extern   void        xdrmem_create ();
  375. extern   void        xdrrec_create ();
  376. extern   bool_t       xdrrec_skiprecord ();
  377. extern   bool_t       xdrrec_eof ();
  378. extern   bool_t       xdrrec_endofrecord ();
  379. extern   bool_t       xdr_reference ();
  380. extern   bool_t       xdr_pointer ();
  381. #endif /* __STDC__ */
  382. #ifdef __cplusplus
  383. }
  384. #endif
  385. #endif /* __INCxdrh */