xdr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /* @(#)xdr.c 2.1 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. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
  32. #endif
  33. /*
  34. * xdr.c, Generic XDR routines implementation.
  35. *
  36. * Copyright (C) 1986, Sun Microsystems, Inc.
  37. *
  38. * These are the "generic" xdr routines used to serialize and de-serialize
  39. * most common data items. See xdr.h for more info on the interface to
  40. * xdr.
  41. */
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <rpc/types.h>
  45. #include <rpc/xdr.h>
  46. /*
  47. * constants specific to the xdr "protocol"
  48. */
  49. #define XDR_FALSE ((long) 0)
  50. #define XDR_TRUE ((long) 1)
  51. #define LASTUNSIGNED ((u_int) 0-1)
  52. /*
  53. * for unit alignment
  54. */
  55. static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
  56. /*
  57. * Free a data structure using XDR
  58. * Not a filter, but a convenient utility nonetheless
  59. */
  60. void xdr_free(proc, objp)
  61. xdrproc_t proc;
  62. char *objp;
  63. {
  64. XDR x;
  65. x.x_op = XDR_FREE;
  66. (*proc) (&x, objp);
  67. }
  68. /*
  69. * XDR nothing
  70. */
  71. bool_t xdr_void( /* xdrs, addr */ )
  72. /* XDR *xdrs; */
  73. /* caddr_t addr; */
  74. {
  75. return (TRUE);
  76. }
  77. /*
  78. * XDR integers
  79. */
  80. bool_t xdr_int(xdrs, ip)
  81. XDR *xdrs;
  82. int *ip;
  83. {
  84. #ifdef lint
  85. (void) (xdr_short(xdrs, (short *) ip));
  86. return (xdr_long(xdrs, (long *) ip));
  87. #else
  88. if (sizeof(int) == sizeof(long)) {
  89. return (xdr_long(xdrs, (long *) ip));
  90. } else {
  91. return (xdr_short(xdrs, (short *) ip));
  92. }
  93. #endif
  94. }
  95. /*
  96. * XDR unsigned integers
  97. */
  98. bool_t xdr_u_int(xdrs, up)
  99. XDR *xdrs;
  100. u_int *up;
  101. {
  102. #ifdef lint
  103. (void) (xdr_short(xdrs, (short *) up));
  104. return (xdr_u_long(xdrs, (u_long *) up));
  105. #else
  106. if (sizeof(u_int) == sizeof(u_long)) {
  107. return (xdr_u_long(xdrs, (u_long *) up));
  108. } else {
  109. return (xdr_short(xdrs, (short *) up));
  110. }
  111. #endif
  112. }
  113. /*
  114. * XDR long integers
  115. * same as xdr_u_long - open coded to save a proc call!
  116. */
  117. bool_t xdr_long(xdrs, lp)
  118. register XDR *xdrs;
  119. long *lp;
  120. {
  121. if (xdrs->x_op == XDR_ENCODE)
  122. return (XDR_PUTLONG(xdrs, lp));
  123. if (xdrs->x_op == XDR_DECODE)
  124. return (XDR_GETLONG(xdrs, lp));
  125. if (xdrs->x_op == XDR_FREE)
  126. return (TRUE);
  127. return (FALSE);
  128. }
  129. /*
  130. * XDR unsigned long integers
  131. * same as xdr_long - open coded to save a proc call!
  132. */
  133. bool_t xdr_u_long(xdrs, ulp)
  134. register XDR *xdrs;
  135. u_long *ulp;
  136. {
  137. if (xdrs->x_op == XDR_DECODE)
  138. return (XDR_GETLONG(xdrs, (long *) ulp));
  139. if (xdrs->x_op == XDR_ENCODE)
  140. return (XDR_PUTLONG(xdrs, (long *) ulp));
  141. if (xdrs->x_op == XDR_FREE)
  142. return (TRUE);
  143. return (FALSE);
  144. }
  145. /*
  146. * XDR short integers
  147. */
  148. bool_t xdr_short(xdrs, sp)
  149. register XDR *xdrs;
  150. short *sp;
  151. {
  152. long l;
  153. switch (xdrs->x_op) {
  154. case XDR_ENCODE:
  155. l = (long) *sp;
  156. return (XDR_PUTLONG(xdrs, &l));
  157. case XDR_DECODE:
  158. if (!XDR_GETLONG(xdrs, &l)) {
  159. return (FALSE);
  160. }
  161. *sp = (short) l;
  162. return (TRUE);
  163. case XDR_FREE:
  164. return (TRUE);
  165. }
  166. return (FALSE);
  167. }
  168. /*
  169. * XDR unsigned short integers
  170. */
  171. bool_t xdr_u_short(xdrs, usp)
  172. register XDR *xdrs;
  173. u_short *usp;
  174. {
  175. u_long l;
  176. switch (xdrs->x_op) {
  177. case XDR_ENCODE:
  178. l = (u_long) * usp;
  179. return (XDR_PUTLONG(xdrs, &l));
  180. case XDR_DECODE:
  181. if (!XDR_GETLONG(xdrs, &l)) {
  182. return (FALSE);
  183. }
  184. *usp = (u_short) l;
  185. return (TRUE);
  186. case XDR_FREE:
  187. return (TRUE);
  188. }
  189. return (FALSE);
  190. }
  191. /*
  192. * XDR a char
  193. */
  194. bool_t xdr_char(xdrs, cp)
  195. XDR *xdrs;
  196. char *cp;
  197. {
  198. int i;
  199. i = (*cp);
  200. if (!xdr_int(xdrs, &i)) {
  201. return (FALSE);
  202. }
  203. *cp = i;
  204. return (TRUE);
  205. }
  206. /*
  207. * XDR an unsigned char
  208. */
  209. bool_t xdr_u_char(xdrs, cp)
  210. XDR *xdrs;
  211. char *cp;
  212. {
  213. u_int u;
  214. u = (*cp);
  215. if (!xdr_u_int(xdrs, &u)) {
  216. return (FALSE);
  217. }
  218. *cp = u;
  219. return (TRUE);
  220. }
  221. /*
  222. * XDR booleans
  223. */
  224. bool_t xdr_bool(xdrs, bp)
  225. register XDR *xdrs;
  226. bool_t *bp;
  227. {
  228. long lb;
  229. switch (xdrs->x_op) {
  230. case XDR_ENCODE:
  231. lb = *bp ? XDR_TRUE : XDR_FALSE;
  232. return (XDR_PUTLONG(xdrs, &lb));
  233. case XDR_DECODE:
  234. if (!XDR_GETLONG(xdrs, &lb)) {
  235. return (FALSE);
  236. }
  237. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  238. return (TRUE);
  239. case XDR_FREE:
  240. return (TRUE);
  241. }
  242. return (FALSE);
  243. }
  244. /*
  245. * XDR enumerations
  246. */
  247. bool_t xdr_enum(xdrs, ep)
  248. XDR *xdrs;
  249. enum_t *ep;
  250. {
  251. #ifndef lint
  252. enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
  253. /*
  254. * enums are treated as ints
  255. */
  256. if (sizeof(enum sizecheck) == sizeof(long)) {
  257. return (xdr_long(xdrs, (long *) ep));
  258. } else if (sizeof(enum sizecheck) == sizeof(short)) {
  259. return (xdr_short(xdrs, (short *) ep));
  260. } else {
  261. return (FALSE);
  262. }
  263. #else
  264. (void) (xdr_short(xdrs, (short *) ep));
  265. return (xdr_long(xdrs, (long *) ep));
  266. #endif
  267. }
  268. /*
  269. * XDR opaque data
  270. * Allows the specification of a fixed size sequence of opaque bytes.
  271. * cp points to the opaque object and cnt gives the byte length.
  272. */
  273. bool_t xdr_opaque(xdrs, cp, cnt)
  274. register XDR *xdrs;
  275. caddr_t cp;
  276. register u_int cnt;
  277. {
  278. register u_int rndup;
  279. static crud[BYTES_PER_XDR_UNIT];
  280. /*
  281. * if no data we are done
  282. */
  283. if (cnt == 0)
  284. return (TRUE);
  285. /*
  286. * round byte count to full xdr units
  287. */
  288. rndup = cnt % BYTES_PER_XDR_UNIT;
  289. if (rndup > 0)
  290. rndup = BYTES_PER_XDR_UNIT - rndup;
  291. if (xdrs->x_op == XDR_DECODE) {
  292. if (!XDR_GETBYTES(xdrs, cp, cnt)) {
  293. return (FALSE);
  294. }
  295. if (rndup == 0)
  296. return (TRUE);
  297. return (XDR_GETBYTES(xdrs, crud, rndup));
  298. }
  299. if (xdrs->x_op == XDR_ENCODE) {
  300. if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
  301. return (FALSE);
  302. }
  303. if (rndup == 0)
  304. return (TRUE);
  305. return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
  306. }
  307. if (xdrs->x_op == XDR_FREE) {
  308. return (TRUE);
  309. }
  310. return (FALSE);
  311. }
  312. /*
  313. * XDR counted bytes
  314. * *cpp is a pointer to the bytes, *sizep is the count.
  315. * If *cpp is NULL maxsize bytes are allocated
  316. */
  317. bool_t xdr_bytes(xdrs, cpp, sizep, maxsize)
  318. register XDR *xdrs;
  319. char **cpp;
  320. register u_int *sizep;
  321. u_int maxsize;
  322. {
  323. register char *sp = *cpp; /* sp is the actual string pointer */
  324. register u_int nodesize;
  325. /*
  326. * first deal with the length since xdr bytes are counted
  327. */
  328. if (!xdr_u_int(xdrs, sizep)) {
  329. return (FALSE);
  330. }
  331. nodesize = *sizep;
  332. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
  333. return (FALSE);
  334. }
  335. /*
  336. * now deal with the actual bytes
  337. */
  338. switch (xdrs->x_op) {
  339. case XDR_DECODE:
  340. if (nodesize == 0) {
  341. return (TRUE);
  342. }
  343. if (sp == NULL) {
  344. *cpp = sp = (char *) mem_alloc(nodesize);
  345. }
  346. if (sp == NULL) {
  347. (void) fprintf(stderr, "xdr_bytes: out of memory\n");
  348. return (FALSE);
  349. }
  350. /* fall into ... */
  351. case XDR_ENCODE:
  352. return (xdr_opaque(xdrs, sp, nodesize));
  353. case XDR_FREE:
  354. if (sp != NULL) {
  355. mem_free(sp, nodesize);
  356. *cpp = NULL;
  357. }
  358. return (TRUE);
  359. }
  360. return (FALSE);
  361. }
  362. /*
  363. * Implemented here due to commonality of the object.
  364. */
  365. bool_t xdr_netobj(xdrs, np)
  366. XDR *xdrs;
  367. struct netobj *np;
  368. {
  369. return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
  370. }
  371. /*
  372. * XDR a descriminated union
  373. * Support routine for discriminated unions.
  374. * You create an array of xdrdiscrim structures, terminated with
  375. * an entry with a null procedure pointer. The routine gets
  376. * the discriminant value and then searches the array of xdrdiscrims
  377. * looking for that value. It calls the procedure given in the xdrdiscrim
  378. * to handle the discriminant. If there is no specific routine a default
  379. * routine may be called.
  380. * If there is no specific or default routine an error is returned.
  381. */
  382. bool_t xdr_union(xdrs, dscmp, unp, choices, dfault)
  383. register XDR *xdrs;
  384. enum_t *dscmp; /* enum to decide which arm to work on */
  385. char *unp; /* the union itself */
  386. struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
  387. xdrproc_t dfault; /* default xdr routine */
  388. {
  389. register enum_t dscm;
  390. /*
  391. * we deal with the discriminator; it's an enum
  392. */
  393. if (!xdr_enum(xdrs, dscmp)) {
  394. return (FALSE);
  395. }
  396. dscm = *dscmp;
  397. /*
  398. * search choices for a value that matches the discriminator.
  399. * if we find one, execute the xdr routine for that value.
  400. */
  401. for (; choices->proc != NULL_xdrproc_t; choices++) {
  402. if (choices->value == dscm)
  403. return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
  404. }
  405. /*
  406. * no match - execute the default xdr routine if there is one
  407. */
  408. return ((dfault == NULL_xdrproc_t) ? FALSE :
  409. (*dfault) (xdrs, unp, LASTUNSIGNED));
  410. }
  411. /*
  412. * Non-portable xdr primitives.
  413. * Care should be taken when moving these routines to new architectures.
  414. */
  415. /*
  416. * XDR null terminated ASCII strings
  417. * xdr_string deals with "C strings" - arrays of bytes that are
  418. * terminated by a NULL character. The parameter cpp references a
  419. * pointer to storage; If the pointer is null, then the necessary
  420. * storage is allocated. The last parameter is the max allowed length
  421. * of the string as specified by a protocol.
  422. */
  423. bool_t xdr_string(xdrs, cpp, maxsize)
  424. register XDR *xdrs;
  425. char **cpp;
  426. u_int maxsize;
  427. {
  428. register char *sp = *cpp; /* sp is the actual string pointer */
  429. u_int size;
  430. u_int nodesize;
  431. /*
  432. * first deal with the length since xdr strings are counted-strings
  433. */
  434. switch (xdrs->x_op) {
  435. case XDR_FREE:
  436. if (sp == NULL) {
  437. return (TRUE); /* already free */
  438. }
  439. /* fall through... */
  440. case XDR_ENCODE:
  441. size = strlen(sp);
  442. break;
  443. }
  444. if (!xdr_u_int(xdrs, &size)) {
  445. return (FALSE);
  446. }
  447. if (size > maxsize) {
  448. return (FALSE);
  449. }
  450. nodesize = size + 1;
  451. /*
  452. * now deal with the actual bytes
  453. */
  454. switch (xdrs->x_op) {
  455. case XDR_DECODE:
  456. if (nodesize == 0) {
  457. return (TRUE);
  458. }
  459. if (sp == NULL)
  460. *cpp = sp = (char *) mem_alloc(nodesize);
  461. if (sp == NULL) {
  462. (void) fprintf(stderr, "xdr_string: out of memory\n");
  463. return (FALSE);
  464. }
  465. sp[size] = 0;
  466. /* fall into ... */
  467. case XDR_ENCODE:
  468. return (xdr_opaque(xdrs, sp, size));
  469. case XDR_FREE:
  470. mem_free(sp, nodesize);
  471. *cpp = NULL;
  472. return (TRUE);
  473. }
  474. return (FALSE);
  475. }
  476. /*
  477. * Wrapper for xdr_string that can be called directly from
  478. * routines like clnt_call
  479. */
  480. bool_t xdr_wrapstring(xdrs, cpp)
  481. XDR *xdrs;
  482. char **cpp;
  483. {
  484. if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {
  485. return (TRUE);
  486. }
  487. return (FALSE);
  488. }