1
0

xdr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
  10. /*
  11. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  12. * unrestricted use provided that this legend is included on all tape
  13. * media and as a part of the software program in whole or part. Users
  14. * may copy or modify Sun RPC without charge, but are not authorized
  15. * to license or distribute it to anyone else except as part of a product or
  16. * program developed by the user.
  17. *
  18. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  19. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  21. *
  22. * Sun RPC is provided with no support and without any obligation on the
  23. * part of Sun Microsystems, Inc. to assist in its use, correction,
  24. * modification or enhancement.
  25. *
  26. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  27. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  28. * OR ANY PART THEREOF.
  29. *
  30. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  31. * or profits or other special, indirect and consequential damages, even if
  32. * Sun has been advised of the possibility of such damages.
  33. *
  34. * Sun Microsystems, Inc.
  35. * 2550 Garcia Avenue
  36. * Mountain View, California 94043
  37. */
  38. #if !defined(lint) && defined(SCCSIDS)
  39. static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
  40. #endif
  41. /*
  42. * xdr.c, Generic XDR routines implementation.
  43. *
  44. * Copyright (C) 1986, Sun Microsystems, Inc.
  45. *
  46. * These are the "generic" xdr routines used to serialize and de-serialize
  47. * most common data items. See xdr.h for more info on the interface to
  48. * xdr.
  49. */
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <rpc/types.h>
  53. #include <rpc/xdr.h>
  54. #include <string.h>
  55. /*
  56. * constants specific to the xdr "protocol"
  57. */
  58. #define XDR_FALSE ((long) 0)
  59. #define XDR_TRUE ((long) 1)
  60. #define LASTUNSIGNED ((unsigned int) 0-1)
  61. /*
  62. * for unit alignment
  63. */
  64. static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
  65. /*
  66. * Free a data structure using XDR
  67. * Not a filter, but a convenient utility nonetheless
  68. */
  69. void xdr_free(xdrproc_t proc, char* objp)
  70. {
  71. XDR x;
  72. x.x_op = XDR_FREE;
  73. (*proc) (&x, objp);
  74. }
  75. /*
  76. * XDR nothing
  77. */
  78. bool_t xdr_void( /* xdrs, addr */ )
  79. /* XDR *xdrs; */
  80. /* char* addr; */
  81. {
  82. return (TRUE);
  83. }
  84. /*
  85. * XDR integers
  86. */
  87. bool_t xdr_int(XDR* xdrs, int* ip)
  88. {
  89. if (sizeof(int) == sizeof(long)) {
  90. return (xdr_long(xdrs, (long *) ip));
  91. } else if (sizeof(int) < sizeof(long)) {
  92. long l;
  93. switch (xdrs->x_op) {
  94. case XDR_ENCODE:
  95. l = (long) *ip;
  96. return XDR_PUTLONG(xdrs, &l);
  97. case XDR_DECODE:
  98. if (!XDR_GETLONG(xdrs, &l))
  99. return FALSE;
  100. *ip = (int) l;
  101. case XDR_FREE:
  102. return TRUE;
  103. }
  104. return FALSE;
  105. } else {
  106. return (xdr_short(xdrs, (short *) ip));
  107. }
  108. }
  109. /*
  110. * XDR unsigned integers
  111. */
  112. bool_t xdr_u_int(XDR* xdrs, unsigned int* up)
  113. {
  114. if (sizeof(unsigned int) == sizeof(unsigned long)) {
  115. return (xdr_u_long(xdrs, (unsigned long *) up));
  116. } else if (sizeof(unsigned int) < sizeof(unsigned long)) {
  117. unsigned long l;
  118. switch (xdrs->x_op) {
  119. case XDR_ENCODE:
  120. l = (unsigned long) *up;
  121. return XDR_PUTLONG(xdrs, (long*)&l);
  122. case XDR_DECODE:
  123. if (!XDR_GETLONG(xdrs, (long*)&l))
  124. return FALSE;
  125. *up = (unsigned int) l;
  126. case XDR_FREE:
  127. return TRUE;
  128. }
  129. return FALSE;
  130. } else {
  131. return (xdr_short(xdrs, (short *) up));
  132. }
  133. }
  134. /*
  135. * XDR long integers
  136. * same as xdr_u_long - open coded to save a proc call!
  137. */
  138. bool_t xdr_long(XDR* xdrs, long* lp)
  139. {
  140. if (xdrs->x_op == XDR_ENCODE
  141. && (sizeof(int32_t) == sizeof(long)
  142. || (int32_t) *lp == *lp))
  143. return (XDR_PUTLONG(xdrs, lp));
  144. if (xdrs->x_op == XDR_DECODE)
  145. return (XDR_GETLONG(xdrs, lp));
  146. if (xdrs->x_op == XDR_FREE)
  147. return (TRUE);
  148. return (FALSE);
  149. }
  150. /*
  151. * XDR unsigned long integers
  152. * same as xdr_long - open coded to save a proc call!
  153. */
  154. bool_t xdr_u_long(XDR* xdrs, unsigned long* ulp)
  155. {
  156. if (xdrs->x_op == XDR_DECODE) {
  157. long l;
  158. if (XDR_GETLONG(xdrs, &l) == FALSE)
  159. return FALSE;
  160. *ulp = (uint32_t) l;
  161. return TRUE;
  162. }
  163. if (xdrs->x_op == XDR_ENCODE) {
  164. if (sizeof(uint32_t) != sizeof(unsigned long)
  165. && (uint32_t) *ulp != *ulp)
  166. return FALSE;
  167. return (XDR_PUTLONG(xdrs, (long *) ulp));
  168. }
  169. if (xdrs->x_op == XDR_FREE)
  170. return (TRUE);
  171. return (FALSE);
  172. }
  173. /*
  174. * XDR long long integers
  175. */
  176. bool_t xdr_longlong_t (XDR * xdrs, long long* llp)
  177. {
  178. int32_t t1, t2;
  179. switch (xdrs->x_op)
  180. {
  181. case XDR_ENCODE:
  182. t1 = (int32_t) ((*llp) >> 32);
  183. t2 = (int32_t) (*llp);
  184. return (XDR_PUTLONG (xdrs, &t1) && XDR_PUTLONG (xdrs, &t2));
  185. case XDR_DECODE:
  186. if (!XDR_GETLONG (xdrs, &t1) || !XDR_GETLONG (xdrs, &t2))
  187. return FALSE;
  188. *llp = ((int64_t) t1) << 32;
  189. *llp |= (uint32_t) t2;
  190. return TRUE;
  191. case XDR_FREE:
  192. return TRUE;
  193. }
  194. return FALSE;
  195. }
  196. /*
  197. * XDR unsigned long long integers
  198. */
  199. bool_t xdr_u_longlong_t (XDR * xdrs, unsigned long long* ullp)
  200. {
  201. uint32_t t1, t2;
  202. switch (xdrs->x_op)
  203. {
  204. case XDR_ENCODE:
  205. t1 = (uint32_t) ((*ullp) >> 32);
  206. t2 = (uint32_t) (*ullp);
  207. return (XDR_PUTLONG (xdrs, (int32_t *)&t1) &&
  208. XDR_PUTLONG (xdrs, (int32_t *)&t2));
  209. case XDR_DECODE:
  210. if (!XDR_GETLONG (xdrs, (int32_t *)&t1) ||
  211. !XDR_GETLONG (xdrs, (int32_t *)&t2))
  212. return FALSE;
  213. *ullp = ((uint64_t) t1) << 32;
  214. *ullp |= t2;
  215. return TRUE;
  216. case XDR_FREE:
  217. return TRUE;
  218. }
  219. return FALSE;
  220. }
  221. /*
  222. * XDR short integers
  223. */
  224. bool_t xdr_short(XDR* xdrs, short* sp)
  225. {
  226. long l;
  227. switch (xdrs->x_op) {
  228. case XDR_ENCODE:
  229. l = (long) *sp;
  230. return (XDR_PUTLONG(xdrs, &l));
  231. case XDR_DECODE:
  232. if (!XDR_GETLONG(xdrs, &l)) {
  233. return (FALSE);
  234. }
  235. *sp = (short) l;
  236. return (TRUE);
  237. case XDR_FREE:
  238. return (TRUE);
  239. }
  240. return (FALSE);
  241. }
  242. /*
  243. * XDR unsigned short integers
  244. */
  245. bool_t xdr_u_short(XDR* xdrs, unsigned short* usp)
  246. {
  247. unsigned long l;
  248. switch (xdrs->x_op) {
  249. case XDR_ENCODE:
  250. l = (unsigned long) * usp;
  251. return (XDR_PUTLONG(xdrs, (long*)&l));
  252. case XDR_DECODE:
  253. if (!XDR_GETLONG(xdrs, (long*)&l)) {
  254. return (FALSE);
  255. }
  256. *usp = (unsigned short) l;
  257. return (TRUE);
  258. case XDR_FREE:
  259. return (TRUE);
  260. }
  261. return (FALSE);
  262. }
  263. /*
  264. * XDR a char
  265. */
  266. bool_t xdr_char(XDR* xdrs, char* cp)
  267. {
  268. int i;
  269. i = (*cp);
  270. if (!xdr_int(xdrs, &i)) {
  271. return (FALSE);
  272. }
  273. *cp = i;
  274. return (TRUE);
  275. }
  276. /*
  277. * XDR an unsigned char
  278. */
  279. bool_t xdr_u_char(XDR* xdrs, unsigned char* cp)
  280. {
  281. unsigned int u;
  282. u = (*cp);
  283. if (!xdr_u_int(xdrs, &u)) {
  284. return (FALSE);
  285. }
  286. *cp = u;
  287. return (TRUE);
  288. }
  289. /*
  290. * XDR booleans
  291. */
  292. bool_t xdr_bool(XDR *xdrs, bool_t *bp)
  293. {
  294. long lb;
  295. switch (xdrs->x_op) {
  296. case XDR_ENCODE:
  297. lb = *bp ? XDR_TRUE : XDR_FALSE;
  298. return (XDR_PUTLONG(xdrs, &lb));
  299. case XDR_DECODE:
  300. if (!XDR_GETLONG(xdrs, &lb)) {
  301. return (FALSE);
  302. }
  303. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  304. return (TRUE);
  305. case XDR_FREE:
  306. return (TRUE);
  307. }
  308. return (FALSE);
  309. }
  310. /*
  311. * XDR enumerations
  312. */
  313. bool_t xdr_enum(XDR *xdrs, enum_t *ep)
  314. {
  315. /*
  316. * enums are treated as ints
  317. */
  318. return (xdr_long(xdrs, (long *) ep));
  319. }
  320. /*
  321. * XDR opaque data
  322. * Allows the specification of a fixed size sequence of opaque bytes.
  323. * cp points to the opaque object and cnt gives the byte length.
  324. */
  325. bool_t xdr_opaque(XDR *xdrs, char* cp, unsigned int cnt)
  326. {
  327. register unsigned int rndup;
  328. static char crud[BYTES_PER_XDR_UNIT];
  329. /*
  330. * if no data we are done
  331. */
  332. if (cnt == 0)
  333. return (TRUE);
  334. /*
  335. * round byte count to full xdr units
  336. */
  337. rndup = cnt % BYTES_PER_XDR_UNIT;
  338. if (rndup > 0)
  339. rndup = BYTES_PER_XDR_UNIT - rndup;
  340. if (xdrs->x_op == XDR_DECODE) {
  341. if (!XDR_GETBYTES(xdrs, cp, cnt)) {
  342. return (FALSE);
  343. }
  344. if (rndup == 0)
  345. return (TRUE);
  346. return (XDR_GETBYTES(xdrs, crud, rndup));
  347. }
  348. if (xdrs->x_op == XDR_ENCODE) {
  349. if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
  350. return (FALSE);
  351. }
  352. if (rndup == 0)
  353. return (TRUE);
  354. return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
  355. }
  356. if (xdrs->x_op == XDR_FREE) {
  357. return (TRUE);
  358. }
  359. return (FALSE);
  360. }
  361. /*
  362. * XDR counted bytes
  363. * *cpp is a pointer to the bytes, *sizep is the count.
  364. * If *cpp is NULL maxsize bytes are allocated
  365. */
  366. bool_t xdr_bytes(XDR *xdrs, char** cpp, unsigned int *sizep, unsigned int maxsize)
  367. {
  368. register char *sp = *cpp; /* sp is the actual string pointer */
  369. register unsigned int nodesize;
  370. /*
  371. * first deal with the length since xdr bytes are counted
  372. */
  373. if (!xdr_u_int(xdrs, sizep)) {
  374. return (FALSE);
  375. }
  376. nodesize = *sizep;
  377. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
  378. return (FALSE);
  379. }
  380. /*
  381. * now deal with the actual bytes
  382. */
  383. switch (xdrs->x_op) {
  384. case XDR_DECODE:
  385. if (nodesize == 0) {
  386. return (TRUE);
  387. }
  388. if (sp == NULL) {
  389. *cpp = sp = (char *) rt_malloc(nodesize);
  390. }
  391. if (sp == NULL) {
  392. rt_kprintf("xdr_bytes: out of memory\n");
  393. return (FALSE);
  394. }
  395. /* fall into ... */
  396. case XDR_ENCODE:
  397. return (xdr_opaque(xdrs, sp, nodesize));
  398. case XDR_FREE:
  399. if (sp != NULL) {
  400. rt_free(sp);
  401. *cpp = NULL;
  402. }
  403. return (TRUE);
  404. }
  405. return (FALSE);
  406. }
  407. /*
  408. * Implemented here due to commonality of the object.
  409. */
  410. bool_t xdr_netobj(XDR *xdrs, struct netobj *np)
  411. {
  412. return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
  413. }
  414. /*
  415. * XDR a descriminated union
  416. * Support routine for discriminated unions.
  417. * You create an array of xdrdiscrim structures, terminated with
  418. * an entry with a null procedure pointer. The routine gets
  419. * the discriminant value and then searches the array of xdrdiscrims
  420. * looking for that value. It calls the procedure given in the xdrdiscrim
  421. * to handle the discriminant. If there is no specific routine a default
  422. * routine may be called.
  423. * If there is no specific or default routine an error is returned.
  424. */
  425. bool_t xdr_union(XDR* xdrs, enum_t* dscmp, char* unp, const struct xdr_discrim* choices, xdrproc_t dfault)
  426. {
  427. register enum_t dscm;
  428. /*
  429. * we deal with the discriminator; it's an enum
  430. */
  431. if (!xdr_enum(xdrs, dscmp)) {
  432. return (FALSE);
  433. }
  434. dscm = *dscmp;
  435. /*
  436. * search choices for a value that matches the discriminator.
  437. * if we find one, execute the xdr routine for that value.
  438. */
  439. for (; choices->proc != NULL_xdrproc_t; choices++) {
  440. if (choices->value == dscm)
  441. return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
  442. }
  443. /*
  444. * no match - execute the default xdr routine if there is one
  445. */
  446. return ((dfault == NULL_xdrproc_t) ? FALSE :
  447. (*dfault) (xdrs, unp, LASTUNSIGNED));
  448. }
  449. /*
  450. * Non-portable xdr primitives.
  451. * Care should be taken when moving these routines to new architectures.
  452. */
  453. /*
  454. * XDR null terminated ASCII strings
  455. * xdr_string deals with "C strings" - arrays of bytes that are
  456. * terminated by a NULL character. The parameter cpp references a
  457. * pointer to storage; If the pointer is null, then the necessary
  458. * storage is allocated. The last parameter is the max allowed length
  459. * of the string as specified by a protocol.
  460. */
  461. bool_t xdr_string(XDR *xdrs, char **cpp, unsigned int maxsize)
  462. {
  463. register char *sp = *cpp; /* sp is the actual string pointer */
  464. unsigned int size;
  465. unsigned int nodesize;
  466. /*
  467. * first deal with the length since xdr strings are counted-strings
  468. */
  469. switch (xdrs->x_op) {
  470. case XDR_FREE:
  471. if (sp == NULL) {
  472. return (TRUE); /* already free */
  473. }
  474. /* fall through... */
  475. case XDR_ENCODE:
  476. size = strlen(sp);
  477. break;
  478. }
  479. if (!xdr_u_int(xdrs, &size)) {
  480. return (FALSE);
  481. }
  482. if (size > maxsize) {
  483. return (FALSE);
  484. }
  485. nodesize = size + 1;
  486. /*
  487. * now deal with the actual bytes
  488. */
  489. switch (xdrs->x_op) {
  490. case XDR_DECODE:
  491. if (nodesize == 0) {
  492. return (TRUE);
  493. }
  494. if (sp == NULL)
  495. *cpp = sp = (char *) rt_malloc(nodesize);
  496. if (sp == NULL) {
  497. rt_kprintf("xdr_string: out of memory\n");
  498. return (FALSE);
  499. }
  500. sp[size] = 0;
  501. /* fall into ... */
  502. case XDR_ENCODE:
  503. return (xdr_opaque(xdrs, sp, size));
  504. case XDR_FREE:
  505. rt_free(sp);
  506. *cpp = NULL;
  507. return (TRUE);
  508. }
  509. return (FALSE);
  510. }
  511. /*
  512. * Wrapper for xdr_string that can be called directly from
  513. * routines like clnt_call
  514. */
  515. bool_t xdr_wrapstring(XDR *xdrs, char **cpp)
  516. {
  517. if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {
  518. return (TRUE);
  519. }
  520. return (FALSE);
  521. }
  522. /*
  523. * XDR an array of arbitrary elements
  524. * *addrp is a pointer to the array, *sizep is the number of elements.
  525. * If addrp is NULL (*sizep * elsize) bytes are allocated.
  526. * elsize is the size (in bytes) of each element, and elproc is the
  527. * xdr procedure to call to handle each element of the array.
  528. */
  529. bool_t xdr_array(XDR *xdrs, char **addrp, unsigned int *sizep, unsigned int maxsize, unsigned int elsize, xdrproc_t elproc)
  530. {
  531. register unsigned int i;
  532. register char* target = *addrp;
  533. register unsigned int c; /* the actual element count */
  534. register bool_t stat = TRUE;
  535. register unsigned int nodesize;
  536. /* like strings, arrays are really counted arrays */
  537. if (!xdr_u_int(xdrs, sizep)) {
  538. return (FALSE);
  539. }
  540. c = *sizep;
  541. if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) {
  542. return (FALSE);
  543. }
  544. /* duh, look for integer overflow (fefe) */
  545. {
  546. unsigned int i;
  547. nodesize = 0;
  548. for (i=c; i; --i) {
  549. unsigned int tmp=nodesize+elsize;
  550. if (tmp<nodesize) /* overflow */
  551. return FALSE;
  552. nodesize=tmp;
  553. }
  554. }
  555. /*
  556. * if we are deserializing, we may need to allocate an array.
  557. * We also save time by checking for a null array if we are freeing.
  558. */
  559. if (target == NULL)
  560. switch (xdrs->x_op) {
  561. case XDR_DECODE:
  562. if (c == 0)
  563. return (TRUE);
  564. *addrp = target = rt_malloc(nodesize);
  565. if (target == NULL) {
  566. rt_kprintf("xdr_array: out of memory\n");
  567. return (FALSE);
  568. }
  569. memset(target, 0, nodesize);
  570. break;
  571. case XDR_FREE:
  572. return (TRUE);
  573. }
  574. /*
  575. * now we xdr each element of array
  576. */
  577. for (i = 0; (i < c) && stat; i++) {
  578. stat = (*elproc) (xdrs, target, LASTUNSIGNED);
  579. target += elsize;
  580. }
  581. /*
  582. * the array may need freeing
  583. */
  584. if (xdrs->x_op == XDR_FREE) {
  585. rt_free(*addrp);
  586. *addrp = NULL;
  587. }
  588. return (stat);
  589. }
  590. /*
  591. * xdr_vector():
  592. *
  593. * XDR a fixed length array. Unlike variable-length arrays,
  594. * the storage of fixed length arrays is static and unfreeable.
  595. * > basep: base of the array
  596. * > size: size of the array
  597. * > elemsize: size of each element
  598. * > xdr_elem: routine to XDR each element
  599. */
  600. bool_t xdr_vector(XDR *xdrs, char *basep, unsigned int nelem, unsigned int elemsize, xdrproc_t xdr_elem)
  601. {
  602. register unsigned int i;
  603. register char *elptr;
  604. elptr = basep;
  605. for (i = 0; i < nelem; i++) {
  606. if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED)) {
  607. return (FALSE);
  608. }
  609. elptr += elemsize;
  610. }
  611. return (TRUE);
  612. }
  613. /*
  614. * XDR an indirect pointer
  615. * xdr_reference is for recursively translating a structure that is
  616. * referenced by a pointer inside the structure that is currently being
  617. * translated. pp references a pointer to storage. If *pp is null
  618. * the necessary storage is allocated.
  619. * size is the sizeof the referneced structure.
  620. * proc is the routine to handle the referenced structure.
  621. */
  622. bool_t xdr_reference(XDR *xdrs, char **pp, unsigned int size, xdrproc_t proc)
  623. {
  624. register char* loc = *pp;
  625. register bool_t stat;
  626. if (loc == NULL)
  627. switch (xdrs->x_op) {
  628. case XDR_FREE:
  629. return (TRUE);
  630. case XDR_DECODE:
  631. *pp = loc = (char*) rt_malloc(size);
  632. if (loc == NULL) {
  633. rt_kprintf("xdr_reference: out of memory\n");
  634. return (FALSE);
  635. }
  636. memset(loc, 0, (int) size);
  637. break;
  638. }
  639. stat = (*proc) (xdrs, loc, LASTUNSIGNED);
  640. if (xdrs->x_op == XDR_FREE) {
  641. rt_free(loc);
  642. *pp = NULL;
  643. }
  644. return (stat);
  645. }
  646. /*
  647. * xdr_pointer():
  648. *
  649. * XDR a pointer to a possibly recursive data structure. This
  650. * differs with xdr_reference in that it can serialize/deserialiaze
  651. * trees correctly.
  652. *
  653. * What's sent is actually a union:
  654. *
  655. * union object_pointer switch (boolean b) {
  656. * case TRUE: object_data data;
  657. * case FALSE: void nothing;
  658. * }
  659. *
  660. * > objpp: Pointer to the pointer to the object.
  661. * > obj_size: size of the object.
  662. * > xdr_obj: routine to XDR an object.
  663. *
  664. */
  665. bool_t xdr_pointer(XDR *xdrs, char **objpp, unsigned int obj_size, xdrproc_t xdr_obj)
  666. {
  667. bool_t more_data;
  668. more_data = (*objpp != NULL);
  669. if (!xdr_bool(xdrs, &more_data)) {
  670. return (FALSE);
  671. }
  672. if (!more_data) {
  673. *objpp = NULL;
  674. return (TRUE);
  675. }
  676. return (xdr_reference(xdrs, objpp, obj_size, xdr_obj));
  677. }