xdr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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, int64_t* 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, uint64_t* 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. enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
  316. /*
  317. * enums are treated as ints
  318. */
  319. /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
  320. return (xdr_long(xdrs, (long *)(void *)ep));
  321. } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
  322. return (xdr_int(xdrs, (int *)(void *)ep));
  323. } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
  324. return (xdr_short(xdrs, (short *)(void *)ep));
  325. } else {
  326. return (FALSE);
  327. }
  328. }
  329. /*
  330. * XDR opaque data
  331. * Allows the specification of a fixed size sequence of opaque bytes.
  332. * cp points to the opaque object and cnt gives the byte length.
  333. */
  334. bool_t xdr_opaque(XDR *xdrs, char* cp, unsigned int cnt)
  335. {
  336. register unsigned int rndup;
  337. static char crud[BYTES_PER_XDR_UNIT];
  338. /*
  339. * if no data we are done
  340. */
  341. if (cnt == 0)
  342. return (TRUE);
  343. /*
  344. * round byte count to full xdr units
  345. */
  346. rndup = cnt % BYTES_PER_XDR_UNIT;
  347. if (rndup > 0)
  348. rndup = BYTES_PER_XDR_UNIT - rndup;
  349. if (xdrs->x_op == XDR_DECODE) {
  350. if (!XDR_GETBYTES(xdrs, cp, cnt)) {
  351. return (FALSE);
  352. }
  353. if (rndup == 0)
  354. return (TRUE);
  355. return (XDR_GETBYTES(xdrs, crud, rndup));
  356. }
  357. if (xdrs->x_op == XDR_ENCODE) {
  358. if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
  359. return (FALSE);
  360. }
  361. if (rndup == 0)
  362. return (TRUE);
  363. return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
  364. }
  365. if (xdrs->x_op == XDR_FREE) {
  366. return (TRUE);
  367. }
  368. return (FALSE);
  369. }
  370. /*
  371. * XDR counted bytes
  372. * *cpp is a pointer to the bytes, *sizep is the count.
  373. * If *cpp is NULL maxsize bytes are allocated
  374. */
  375. bool_t xdr_bytes(XDR *xdrs, char** cpp, unsigned int *sizep, unsigned int maxsize)
  376. {
  377. register char *sp = *cpp; /* sp is the actual string pointer */
  378. register unsigned int nodesize;
  379. /*
  380. * first deal with the length since xdr bytes are counted
  381. */
  382. if (!xdr_u_int(xdrs, sizep)) {
  383. return (FALSE);
  384. }
  385. nodesize = *sizep;
  386. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
  387. return (FALSE);
  388. }
  389. /*
  390. * now deal with the actual bytes
  391. */
  392. switch (xdrs->x_op) {
  393. case XDR_DECODE:
  394. if (nodesize == 0) {
  395. return (TRUE);
  396. }
  397. if (sp == NULL) {
  398. *cpp = sp = (char *) rt_malloc(nodesize);
  399. }
  400. if (sp == NULL) {
  401. rt_kprintf("xdr_bytes: out of memory\n");
  402. return (FALSE);
  403. }
  404. /* fall into ... */
  405. case XDR_ENCODE:
  406. return (xdr_opaque(xdrs, sp, nodesize));
  407. case XDR_FREE:
  408. if (sp != NULL) {
  409. rt_free(sp);
  410. *cpp = NULL;
  411. }
  412. return (TRUE);
  413. }
  414. return (FALSE);
  415. }
  416. /*
  417. * Implemented here due to commonality of the object.
  418. */
  419. bool_t xdr_netobj(XDR *xdrs, struct netobj *np)
  420. {
  421. return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
  422. }
  423. /*
  424. * XDR a descriminated union
  425. * Support routine for discriminated unions.
  426. * You create an array of xdrdiscrim structures, terminated with
  427. * an entry with a null procedure pointer. The routine gets
  428. * the discriminant value and then searches the array of xdrdiscrims
  429. * looking for that value. It calls the procedure given in the xdrdiscrim
  430. * to handle the discriminant. If there is no specific routine a default
  431. * routine may be called.
  432. * If there is no specific or default routine an error is returned.
  433. */
  434. bool_t xdr_union(XDR* xdrs, enum_t* dscmp, char* unp, const struct xdr_discrim* choices, xdrproc_t dfault)
  435. {
  436. register enum_t dscm;
  437. /*
  438. * we deal with the discriminator; it's an enum
  439. */
  440. if (!xdr_enum(xdrs, dscmp)) {
  441. return (FALSE);
  442. }
  443. dscm = *dscmp;
  444. /*
  445. * search choices for a value that matches the discriminator.
  446. * if we find one, execute the xdr routine for that value.
  447. */
  448. for (; choices->proc != NULL_xdrproc_t; choices++) {
  449. if (choices->value == dscm)
  450. return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
  451. }
  452. /*
  453. * no match - execute the default xdr routine if there is one
  454. */
  455. return ((dfault == NULL_xdrproc_t) ? FALSE :
  456. (*dfault) (xdrs, unp, LASTUNSIGNED));
  457. }
  458. /*
  459. * Non-portable xdr primitives.
  460. * Care should be taken when moving these routines to new architectures.
  461. */
  462. /*
  463. * XDR null terminated ASCII strings
  464. * xdr_string deals with "C strings" - arrays of bytes that are
  465. * terminated by a NULL character. The parameter cpp references a
  466. * pointer to storage; If the pointer is null, then the necessary
  467. * storage is allocated. The last parameter is the max allowed length
  468. * of the string as specified by a protocol.
  469. */
  470. bool_t xdr_string(XDR *xdrs, char **cpp, unsigned int maxsize)
  471. {
  472. register char *sp = *cpp; /* sp is the actual string pointer */
  473. unsigned int size;
  474. unsigned int nodesize;
  475. /*
  476. * first deal with the length since xdr strings are counted-strings
  477. */
  478. switch (xdrs->x_op) {
  479. case XDR_FREE:
  480. if (sp == NULL) {
  481. return (TRUE); /* already free */
  482. }
  483. /* fall through... */
  484. case XDR_ENCODE:
  485. size = strlen(sp);
  486. break;
  487. }
  488. if (!xdr_u_int(xdrs, &size)) {
  489. return (FALSE);
  490. }
  491. if (size > maxsize) {
  492. return (FALSE);
  493. }
  494. nodesize = size + 1;
  495. /*
  496. * now deal with the actual bytes
  497. */
  498. switch (xdrs->x_op) {
  499. case XDR_DECODE:
  500. if (nodesize == 0) {
  501. return (TRUE);
  502. }
  503. if (sp == NULL)
  504. *cpp = sp = (char *) rt_malloc(nodesize);
  505. if (sp == NULL) {
  506. rt_kprintf("xdr_string: out of memory\n");
  507. return (FALSE);
  508. }
  509. sp[size] = 0;
  510. /* fall into ... */
  511. case XDR_ENCODE:
  512. return (xdr_opaque(xdrs, sp, size));
  513. case XDR_FREE:
  514. rt_free(sp);
  515. *cpp = NULL;
  516. return (TRUE);
  517. }
  518. return (FALSE);
  519. }
  520. /*
  521. * Wrapper for xdr_string that can be called directly from
  522. * routines like clnt_call
  523. */
  524. bool_t xdr_wrapstring(XDR *xdrs, char **cpp)
  525. {
  526. if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {
  527. return (TRUE);
  528. }
  529. return (FALSE);
  530. }
  531. /*
  532. * XDR an array of arbitrary elements
  533. * *addrp is a pointer to the array, *sizep is the number of elements.
  534. * If addrp is NULL (*sizep * elsize) bytes are allocated.
  535. * elsize is the size (in bytes) of each element, and elproc is the
  536. * xdr procedure to call to handle each element of the array.
  537. */
  538. bool_t xdr_array(XDR *xdrs, char **addrp, unsigned int *sizep, unsigned int maxsize, unsigned int elsize, xdrproc_t elproc)
  539. {
  540. register unsigned int i;
  541. register char* target = *addrp;
  542. register unsigned int c; /* the actual element count */
  543. register bool_t stat = TRUE;
  544. register unsigned int nodesize;
  545. /* like strings, arrays are really counted arrays */
  546. if (!xdr_u_int(xdrs, sizep)) {
  547. return (FALSE);
  548. }
  549. c = *sizep;
  550. if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) {
  551. return (FALSE);
  552. }
  553. /* duh, look for integer overflow (fefe) */
  554. {
  555. unsigned int i;
  556. nodesize = 0;
  557. for (i=c; i; --i) {
  558. unsigned int tmp=nodesize+elsize;
  559. if (tmp<nodesize) /* overflow */
  560. return FALSE;
  561. nodesize=tmp;
  562. }
  563. }
  564. /*
  565. * if we are deserializing, we may need to allocate an array.
  566. * We also save time by checking for a null array if we are freeing.
  567. */
  568. if (target == NULL)
  569. switch (xdrs->x_op) {
  570. case XDR_DECODE:
  571. if (c == 0)
  572. return (TRUE);
  573. *addrp = target = rt_malloc(nodesize);
  574. if (target == NULL) {
  575. rt_kprintf("xdr_array: out of memory\n");
  576. return (FALSE);
  577. }
  578. memset(target, 0, nodesize);
  579. break;
  580. case XDR_FREE:
  581. return (TRUE);
  582. }
  583. /*
  584. * now we xdr each element of array
  585. */
  586. for (i = 0; (i < c) && stat; i++) {
  587. stat = (*elproc) (xdrs, target, LASTUNSIGNED);
  588. target += elsize;
  589. }
  590. /*
  591. * the array may need freeing
  592. */
  593. if (xdrs->x_op == XDR_FREE) {
  594. rt_free(*addrp);
  595. *addrp = NULL;
  596. }
  597. return (stat);
  598. }
  599. /*
  600. * xdr_vector():
  601. *
  602. * XDR a fixed length array. Unlike variable-length arrays,
  603. * the storage of fixed length arrays is static and unfreeable.
  604. * > basep: base of the array
  605. * > size: size of the array
  606. * > elemsize: size of each element
  607. * > xdr_elem: routine to XDR each element
  608. */
  609. bool_t xdr_vector(XDR *xdrs, char *basep, unsigned int nelem, unsigned int elemsize, xdrproc_t xdr_elem)
  610. {
  611. register unsigned int i;
  612. register char *elptr;
  613. elptr = basep;
  614. for (i = 0; i < nelem; i++) {
  615. if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED)) {
  616. return (FALSE);
  617. }
  618. elptr += elemsize;
  619. }
  620. return (TRUE);
  621. }
  622. /*
  623. * XDR an indirect pointer
  624. * xdr_reference is for recursively translating a structure that is
  625. * referenced by a pointer inside the structure that is currently being
  626. * translated. pp references a pointer to storage. If *pp is null
  627. * the necessary storage is allocated.
  628. * size is the sizeof the referneced structure.
  629. * proc is the routine to handle the referenced structure.
  630. */
  631. bool_t xdr_reference(XDR *xdrs, char **pp, unsigned int size, xdrproc_t proc)
  632. {
  633. register char* loc = *pp;
  634. register bool_t stat;
  635. if (loc == NULL)
  636. switch (xdrs->x_op) {
  637. case XDR_FREE:
  638. return (TRUE);
  639. case XDR_DECODE:
  640. *pp = loc = (char*) rt_malloc(size);
  641. if (loc == NULL) {
  642. rt_kprintf("xdr_reference: out of memory\n");
  643. return (FALSE);
  644. }
  645. memset(loc, 0, (int) size);
  646. break;
  647. }
  648. stat = (*proc) (xdrs, loc, LASTUNSIGNED);
  649. if (xdrs->x_op == XDR_FREE) {
  650. rt_free(loc);
  651. *pp = NULL;
  652. }
  653. return (stat);
  654. }
  655. /*
  656. * xdr_pointer():
  657. *
  658. * XDR a pointer to a possibly recursive data structure. This
  659. * differs with xdr_reference in that it can serialize/deserialiaze
  660. * trees correctly.
  661. *
  662. * What's sent is actually a union:
  663. *
  664. * union object_pointer switch (boolean b) {
  665. * case TRUE: object_data data;
  666. * case FALSE: void nothing;
  667. * }
  668. *
  669. * > objpp: Pointer to the pointer to the object.
  670. * > obj_size: size of the object.
  671. * > xdr_obj: routine to XDR an object.
  672. *
  673. */
  674. bool_t xdr_pointer(XDR *xdrs, char **objpp, unsigned int obj_size, xdrproc_t xdr_obj)
  675. {
  676. bool_t more_data;
  677. more_data = (*objpp != NULL);
  678. if (!xdr_bool(xdrs, &more_data)) {
  679. return (FALSE);
  680. }
  681. if (!more_data) {
  682. *objpp = NULL;
  683. return (TRUE);
  684. }
  685. return (xdr_reference(xdrs, objpp, obj_size, xdr_obj));
  686. }