xdr.c 11 KB

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