xdr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. #define __FORCE_GLIBC__
  31. #include <features.h>
  32. /*
  33. * xdr.c, Generic XDR routines implementation.
  34. *
  35. * Copyright (C) 1986, Sun Microsystems, Inc.
  36. *
  37. * These are the "generic" xdr routines used to serialize and de-serialize
  38. * most common data items. See xdr.h for more info on the interface to
  39. * xdr.
  40. */
  41. #include <stdio.h>
  42. #include <string.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 (XDR *xdrs, u_char *cp)
  210. {
  211. u_int u;
  212. u = (*cp);
  213. if (!xdr_u_int(xdrs, &u)) {
  214. return (FALSE);
  215. }
  216. *cp = u;
  217. return (TRUE);
  218. }
  219. /*
  220. * XDR booleans
  221. */
  222. bool_t xdr_bool(xdrs, bp)
  223. register XDR *xdrs;
  224. bool_t *bp;
  225. {
  226. long lb;
  227. switch (xdrs->x_op) {
  228. case XDR_ENCODE:
  229. lb = *bp ? XDR_TRUE : XDR_FALSE;
  230. return (XDR_PUTLONG(xdrs, &lb));
  231. case XDR_DECODE:
  232. if (!XDR_GETLONG(xdrs, &lb)) {
  233. return (FALSE);
  234. }
  235. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  236. return (TRUE);
  237. case XDR_FREE:
  238. return (TRUE);
  239. }
  240. return (FALSE);
  241. }
  242. /*
  243. * XDR enumerations
  244. */
  245. bool_t xdr_enum(xdrs, ep)
  246. XDR *xdrs;
  247. enum_t *ep;
  248. {
  249. #ifndef lint
  250. enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
  251. /*
  252. * enums are treated as ints
  253. */
  254. if (sizeof(enum sizecheck) == sizeof(long)) {
  255. return (xdr_long(xdrs, (long *) ep));
  256. } else if (sizeof(enum sizecheck) == sizeof(short)) {
  257. return (xdr_short(xdrs, (short *) ep));
  258. } else {
  259. return (FALSE);
  260. }
  261. #else
  262. (void) (xdr_short(xdrs, (short *) ep));
  263. return (xdr_long(xdrs, (long *) ep));
  264. #endif
  265. }
  266. /*
  267. * XDR opaque data
  268. * Allows the specification of a fixed size sequence of opaque bytes.
  269. * cp points to the opaque object and cnt gives the byte length.
  270. */
  271. bool_t xdr_opaque(xdrs, cp, cnt)
  272. register XDR *xdrs;
  273. caddr_t cp;
  274. register u_int cnt;
  275. {
  276. register u_int rndup;
  277. static u_long crud[BYTES_PER_XDR_UNIT];
  278. /*
  279. * if no data we are done
  280. */
  281. if (cnt == 0)
  282. return (TRUE);
  283. /*
  284. * round byte count to full xdr units
  285. */
  286. rndup = cnt % BYTES_PER_XDR_UNIT;
  287. if (rndup > 0)
  288. rndup = BYTES_PER_XDR_UNIT - rndup;
  289. if (xdrs->x_op == XDR_DECODE) {
  290. if (!XDR_GETBYTES(xdrs, cp, cnt)) {
  291. return (FALSE);
  292. }
  293. if (rndup == 0)
  294. return (TRUE);
  295. return (XDR_GETBYTES(xdrs, (caddr_t) crud, rndup));
  296. }
  297. if (xdrs->x_op == XDR_ENCODE) {
  298. if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
  299. return (FALSE);
  300. }
  301. if (rndup == 0)
  302. return (TRUE);
  303. return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
  304. }
  305. if (xdrs->x_op == XDR_FREE) {
  306. return (TRUE);
  307. }
  308. return (FALSE);
  309. }
  310. /*
  311. * XDR counted bytes
  312. * *cpp is a pointer to the bytes, *sizep is the count.
  313. * If *cpp is NULL maxsize bytes are allocated
  314. */
  315. bool_t xdr_bytes(xdrs, cpp, sizep, maxsize)
  316. register XDR *xdrs;
  317. char **cpp;
  318. register u_int *sizep;
  319. u_int maxsize;
  320. {
  321. register char *sp = *cpp; /* sp is the actual string pointer */
  322. register u_int nodesize;
  323. /*
  324. * first deal with the length since xdr bytes are counted
  325. */
  326. if (!xdr_u_int(xdrs, sizep)) {
  327. return (FALSE);
  328. }
  329. nodesize = *sizep;
  330. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
  331. return (FALSE);
  332. }
  333. /*
  334. * now deal with the actual bytes
  335. */
  336. switch (xdrs->x_op) {
  337. case XDR_DECODE:
  338. if (nodesize == 0) {
  339. return (TRUE);
  340. }
  341. if (sp == NULL) {
  342. *cpp = sp = (char *) mem_alloc(nodesize);
  343. }
  344. if (sp == NULL) {
  345. (void) fprintf(stderr, "xdr_bytes: out of memory\n");
  346. return (FALSE);
  347. }
  348. /* fall into ... */
  349. case XDR_ENCODE:
  350. return (xdr_opaque(xdrs, sp, nodesize));
  351. case XDR_FREE:
  352. if (sp != NULL) {
  353. mem_free(sp, nodesize);
  354. *cpp = NULL;
  355. }
  356. return (TRUE);
  357. }
  358. return (FALSE);
  359. }
  360. /*
  361. * Implemented here due to commonality of the object.
  362. */
  363. bool_t xdr_netobj(xdrs, np)
  364. XDR *xdrs;
  365. struct netobj *np;
  366. {
  367. return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
  368. }
  369. /*
  370. * XDR a descriminated union
  371. * Support routine for discriminated unions.
  372. * You create an array of xdrdiscrim structures, terminated with
  373. * an entry with a null procedure pointer. The routine gets
  374. * the discriminant value and then searches the array of xdrdiscrims
  375. * looking for that value. It calls the procedure given in the xdrdiscrim
  376. * to handle the discriminant. If there is no specific routine a default
  377. * routine may be called.
  378. * If there is no specific or default routine an error is returned.
  379. */
  380. bool_t xdr_union(xdrs, dscmp, unp, choices, dfault)
  381. register XDR *xdrs;
  382. enum_t *dscmp; /* enum to decide which arm to work on */
  383. char *unp; /* the union itself */
  384. const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
  385. xdrproc_t dfault; /* default xdr routine */
  386. {
  387. register enum_t dscm;
  388. /*
  389. * we deal with the discriminator; it's an enum
  390. */
  391. if (!xdr_enum(xdrs, dscmp)) {
  392. return (FALSE);
  393. }
  394. dscm = *dscmp;
  395. /*
  396. * search choices for a value that matches the discriminator.
  397. * if we find one, execute the xdr routine for that value.
  398. */
  399. for (; choices->proc != NULL_xdrproc_t; choices++) {
  400. if (choices->value == dscm)
  401. return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
  402. }
  403. /*
  404. * no match - execute the default xdr routine if there is one
  405. */
  406. return ((dfault == NULL_xdrproc_t) ? FALSE :
  407. (*dfault) (xdrs, unp, LASTUNSIGNED));
  408. }
  409. /*
  410. * Non-portable xdr primitives.
  411. * Care should be taken when moving these routines to new architectures.
  412. */
  413. /*
  414. * XDR null terminated ASCII strings
  415. * xdr_string deals with "C strings" - arrays of bytes that are
  416. * terminated by a NULL character. The parameter cpp references a
  417. * pointer to storage; If the pointer is null, then the necessary
  418. * storage is allocated. The last parameter is the max allowed length
  419. * of the string as specified by a protocol.
  420. */
  421. bool_t xdr_string(xdrs, cpp, maxsize)
  422. register XDR *xdrs;
  423. char **cpp;
  424. u_int maxsize;
  425. {
  426. register char *sp = *cpp; /* sp is the actual string pointer */
  427. u_int size;
  428. u_int nodesize;
  429. /*
  430. * first deal with the length since xdr strings are counted-strings
  431. */
  432. switch (xdrs->x_op) {
  433. case XDR_FREE:
  434. if (sp == NULL) {
  435. return (TRUE); /* already free */
  436. }
  437. /* fall through... */
  438. case XDR_ENCODE:
  439. size = strlen(sp);
  440. break;
  441. default: /* silence the warnings */
  442. }
  443. if (!xdr_u_int(xdrs, &size)) {
  444. return (FALSE);
  445. }
  446. if (size > maxsize) {
  447. return (FALSE);
  448. }
  449. nodesize = size + 1;
  450. /*
  451. * now deal with the actual bytes
  452. */
  453. switch (xdrs->x_op) {
  454. case XDR_DECODE:
  455. if (nodesize == 0) {
  456. return (TRUE);
  457. }
  458. if (sp == NULL)
  459. *cpp = sp = (char *) mem_alloc(nodesize);
  460. if (sp == NULL) {
  461. (void) fprintf(stderr, "xdr_string: out of memory\n");
  462. return (FALSE);
  463. }
  464. sp[size] = 0;
  465. /* fall into ... */
  466. case XDR_ENCODE:
  467. return (xdr_opaque(xdrs, sp, size));
  468. case XDR_FREE:
  469. mem_free(sp, nodesize);
  470. *cpp = NULL;
  471. return (TRUE);
  472. }
  473. return (FALSE);
  474. }
  475. /*
  476. * Wrapper for xdr_string that can be called directly from
  477. * routines like clnt_call
  478. */
  479. bool_t xdr_wrapstring(xdrs, cpp)
  480. XDR *xdrs;
  481. char **cpp;
  482. {
  483. if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {
  484. return (TRUE);
  485. }
  486. return (FALSE);
  487. }