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