xdr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * xdr.c, Generic XDR routines implementation.
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. * * Neither the name of the "Oracle America, Inc." nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * These are the "generic" xdr routines used to serialize and de-serialize
  34. * most common data items. See xdr.h for more info on the interface to
  35. * xdr.
  36. */
  37. #include <stdio.h>
  38. #include <limits.h>
  39. #include <string.h>
  40. #include <libintl.h>
  41. #include <wchar.h>
  42. #include <stdint.h>
  43. #include <rpc/types.h>
  44. #include <rpc/xdr.h>
  45. #include <shlib-compat.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 const 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 (xdrproc_t proc, char *objp)
  62. {
  63. XDR x;
  64. x.x_op = XDR_FREE;
  65. (*proc) (&x, objp);
  66. }
  67. #ifdef EXPORT_RPC_SYMBOLS
  68. libc_hidden_def (xdr_free)
  69. #else
  70. libc_hidden_nolink_sunrpc (xdr_free, GLIBC_2_0)
  71. #endif
  72. /*
  73. * XDR nothing
  74. */
  75. bool_t
  76. xdr_void (void)
  77. {
  78. return TRUE;
  79. }
  80. #ifdef EXPORT_RPC_SYMBOLS
  81. libc_hidden_def (xdr_void)
  82. #else
  83. libc_hidden_nolink_sunrpc (xdr_void, GLIBC_2_0)
  84. #endif
  85. /*
  86. * XDR integers
  87. */
  88. bool_t
  89. xdr_int (XDR *xdrs, int *ip)
  90. {
  91. #if INT_MAX < LONG_MAX
  92. long l;
  93. switch (xdrs->x_op)
  94. {
  95. case XDR_ENCODE:
  96. l = (long) *ip;
  97. return XDR_PUTLONG (xdrs, &l);
  98. case XDR_DECODE:
  99. if (!XDR_GETLONG (xdrs, &l))
  100. {
  101. return FALSE;
  102. }
  103. *ip = (int) l;
  104. case XDR_FREE:
  105. return TRUE;
  106. }
  107. return FALSE;
  108. #elif INT_MAX == LONG_MAX
  109. return xdr_long (xdrs, (long *) ip);
  110. #elif INT_MAX == SHRT_MAX
  111. return xdr_short (xdrs, (short *) ip);
  112. #else
  113. #error unexpected integer sizes in_xdr_int()
  114. #endif
  115. }
  116. #ifdef EXPORT_RPC_SYMBOLS
  117. libc_hidden_def (xdr_int)
  118. #else
  119. libc_hidden_nolink_sunrpc (xdr_int, GLIBC_2_0)
  120. #endif
  121. /*
  122. * XDR unsigned integers
  123. */
  124. bool_t
  125. xdr_u_int (XDR *xdrs, u_int *up)
  126. {
  127. #if UINT_MAX < ULONG_MAX
  128. long l;
  129. switch (xdrs->x_op)
  130. {
  131. case XDR_ENCODE:
  132. l = (u_long) * up;
  133. return XDR_PUTLONG (xdrs, &l);
  134. case XDR_DECODE:
  135. if (!XDR_GETLONG (xdrs, &l))
  136. {
  137. return FALSE;
  138. }
  139. *up = (u_int) (u_long) l;
  140. case XDR_FREE:
  141. return TRUE;
  142. }
  143. return FALSE;
  144. #elif UINT_MAX == ULONG_MAX
  145. return xdr_u_long (xdrs, (u_long *) up);
  146. #elif UINT_MAX == USHRT_MAX
  147. return xdr_short (xdrs, (short *) up);
  148. #else
  149. #error unexpected integer sizes in_xdr_u_int()
  150. #endif
  151. }
  152. #ifdef EXPORT_RPC_SYMBOLS
  153. libc_hidden_def (xdr_u_int)
  154. #else
  155. libc_hidden_nolink_sunrpc (xdr_u_int, GLIBC_2_0)
  156. #endif
  157. /*
  158. * XDR long integers
  159. * The definition of xdr_long() is kept for backward
  160. * compatibility. Instead xdr_int() should be used.
  161. */
  162. bool_t
  163. xdr_long (XDR *xdrs, long *lp)
  164. {
  165. if (xdrs->x_op == XDR_ENCODE
  166. && (sizeof (int32_t) == sizeof (long)
  167. || (int32_t) *lp == *lp))
  168. return XDR_PUTLONG (xdrs, lp);
  169. if (xdrs->x_op == XDR_DECODE)
  170. return XDR_GETLONG (xdrs, lp);
  171. if (xdrs->x_op == XDR_FREE)
  172. return TRUE;
  173. return FALSE;
  174. }
  175. #ifdef EXPORT_RPC_SYMBOLS
  176. libc_hidden_def (xdr_long)
  177. #else
  178. libc_hidden_nolink_sunrpc (xdr_long, GLIBC_2_0)
  179. #endif
  180. /*
  181. * XDR unsigned long integers
  182. * The definition of xdr_u_long() is kept for backward
  183. * compatibility. Instead xdr_u_int() should be used.
  184. */
  185. bool_t
  186. xdr_u_long (XDR *xdrs, u_long *ulp)
  187. {
  188. switch (xdrs->x_op)
  189. {
  190. case XDR_DECODE:
  191. {
  192. long int tmp;
  193. if (XDR_GETLONG (xdrs, &tmp) == FALSE)
  194. return FALSE;
  195. *ulp = (uint32_t) tmp;
  196. return TRUE;
  197. }
  198. case XDR_ENCODE:
  199. if (sizeof (uint32_t) != sizeof (u_long)
  200. && (uint32_t) *ulp != *ulp)
  201. return FALSE;
  202. return XDR_PUTLONG (xdrs, (long *) ulp);
  203. case XDR_FREE:
  204. return TRUE;
  205. }
  206. return FALSE;
  207. }
  208. #ifdef EXPORT_RPC_SYMBOLS
  209. libc_hidden_def (xdr_u_long)
  210. #else
  211. libc_hidden_nolink_sunrpc (xdr_u_long, GLIBC_2_0)
  212. #endif
  213. /*
  214. * XDR hyper integers
  215. * same as xdr_u_hyper - open coded to save a proc call!
  216. */
  217. bool_t
  218. xdr_hyper (XDR *xdrs, quad_t *llp)
  219. {
  220. long int t1, t2;
  221. if (xdrs->x_op == XDR_ENCODE)
  222. {
  223. t1 = (long) ((*llp) >> 32);
  224. t2 = (long) (*llp);
  225. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  226. }
  227. if (xdrs->x_op == XDR_DECODE)
  228. {
  229. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  230. return FALSE;
  231. *llp = ((quad_t) t1) << 32;
  232. *llp |= (uint32_t) t2;
  233. return TRUE;
  234. }
  235. if (xdrs->x_op == XDR_FREE)
  236. return TRUE;
  237. return FALSE;
  238. }
  239. #ifdef EXPORT_RPC_SYMBOLS
  240. libc_hidden_def (xdr_hyper)
  241. #else
  242. libc_hidden_nolink_sunrpc (xdr_hyper, GLIBC_2_1_1)
  243. #endif
  244. /*
  245. * XDR hyper integers
  246. * same as xdr_hyper - open coded to save a proc call!
  247. */
  248. bool_t
  249. xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
  250. {
  251. long int t1, t2;
  252. if (xdrs->x_op == XDR_ENCODE)
  253. {
  254. t1 = (unsigned long) ((*ullp) >> 32);
  255. t2 = (unsigned long) (*ullp);
  256. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  257. }
  258. if (xdrs->x_op == XDR_DECODE)
  259. {
  260. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  261. return FALSE;
  262. *ullp = ((u_quad_t) t1) << 32;
  263. *ullp |= (uint32_t) t2;
  264. return TRUE;
  265. }
  266. if (xdrs->x_op == XDR_FREE)
  267. return TRUE;
  268. return FALSE;
  269. }
  270. #ifdef EXPORT_RPC_SYMBOLS
  271. libc_hidden_def (xdr_u_hyper)
  272. #else
  273. libc_hidden_nolink_sunrpc (xdr_u_hyper, GLIBC_2_1_1)
  274. #endif
  275. bool_t
  276. xdr_longlong_t (XDR *xdrs, quad_t *llp)
  277. {
  278. return xdr_hyper (xdrs, llp);
  279. }
  280. #ifdef EXPORT_RPC_SYMBOLS
  281. libc_hidden_def (xdr_longlong_t)
  282. #else
  283. libc_hidden_nolink_sunrpc (xdr_longlong_t, GLIBC_2_1_1)
  284. #endif
  285. bool_t
  286. xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
  287. {
  288. return xdr_u_hyper (xdrs, ullp);
  289. }
  290. #ifdef EXPORT_RPC_SYMBOLS
  291. libc_hidden_def (xdr_u_longlong_t)
  292. #else
  293. libc_hidden_nolink_sunrpc (xdr_u_longlong_t, GLIBC_2_1_1)
  294. #endif
  295. /*
  296. * XDR short integers
  297. */
  298. bool_t
  299. xdr_short (XDR *xdrs, short *sp)
  300. {
  301. long l;
  302. switch (xdrs->x_op)
  303. {
  304. case XDR_ENCODE:
  305. l = (long) *sp;
  306. return XDR_PUTLONG (xdrs, &l);
  307. case XDR_DECODE:
  308. if (!XDR_GETLONG (xdrs, &l))
  309. {
  310. return FALSE;
  311. }
  312. *sp = (short) l;
  313. return TRUE;
  314. case XDR_FREE:
  315. return TRUE;
  316. }
  317. return FALSE;
  318. }
  319. #ifdef EXPORT_RPC_SYMBOLS
  320. libc_hidden_def (xdr_short)
  321. #else
  322. libc_hidden_nolink_sunrpc (xdr_short, GLIBC_2_0)
  323. #endif
  324. /*
  325. * XDR unsigned short integers
  326. */
  327. bool_t
  328. xdr_u_short (XDR *xdrs, u_short *usp)
  329. {
  330. long l;
  331. switch (xdrs->x_op)
  332. {
  333. case XDR_ENCODE:
  334. l = (u_long) * usp;
  335. return XDR_PUTLONG (xdrs, &l);
  336. case XDR_DECODE:
  337. if (!XDR_GETLONG (xdrs, &l))
  338. {
  339. return FALSE;
  340. }
  341. *usp = (u_short) (u_long) l;
  342. return TRUE;
  343. case XDR_FREE:
  344. return TRUE;
  345. }
  346. return FALSE;
  347. }
  348. #ifdef EXPORT_RPC_SYMBOLS
  349. libc_hidden_def (xdr_u_short)
  350. #else
  351. libc_hidden_nolink_sunrpc (xdr_u_short, GLIBC_2_0)
  352. #endif
  353. /*
  354. * XDR a char
  355. */
  356. bool_t
  357. xdr_char (XDR *xdrs, char *cp)
  358. {
  359. int i;
  360. i = (*cp);
  361. if (!xdr_int (xdrs, &i))
  362. {
  363. return FALSE;
  364. }
  365. *cp = i;
  366. return TRUE;
  367. }
  368. #ifdef EXPORT_RPC_SYMBOLS
  369. libc_hidden_def (xdr_char)
  370. #else
  371. libc_hidden_nolink_sunrpc (xdr_char, GLIBC_2_0)
  372. #endif
  373. /*
  374. * XDR an unsigned char
  375. */
  376. bool_t
  377. xdr_u_char (XDR *xdrs, u_char *cp)
  378. {
  379. u_int u;
  380. u = (*cp);
  381. if (!xdr_u_int (xdrs, &u))
  382. {
  383. return FALSE;
  384. }
  385. *cp = u;
  386. return TRUE;
  387. }
  388. #ifdef EXPORT_RPC_SYMBOLS
  389. libc_hidden_def (xdr_u_char)
  390. #else
  391. libc_hidden_nolink_sunrpc (xdr_u_char, GLIBC_2_0)
  392. #endif
  393. /*
  394. * XDR booleans
  395. */
  396. bool_t
  397. xdr_bool (XDR *xdrs, bool_t *bp)
  398. {
  399. long lb;
  400. switch (xdrs->x_op)
  401. {
  402. case XDR_ENCODE:
  403. lb = *bp ? XDR_TRUE : XDR_FALSE;
  404. return XDR_PUTLONG (xdrs, &lb);
  405. case XDR_DECODE:
  406. if (!XDR_GETLONG (xdrs, &lb))
  407. {
  408. return FALSE;
  409. }
  410. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  411. return TRUE;
  412. case XDR_FREE:
  413. return TRUE;
  414. }
  415. return FALSE;
  416. }
  417. #ifdef EXPORT_RPC_SYMBOLS
  418. libc_hidden_def (xdr_bool)
  419. #else
  420. libc_hidden_nolink_sunrpc (xdr_bool, GLIBC_2_0)
  421. #endif
  422. /*
  423. * XDR enumerations
  424. */
  425. bool_t
  426. xdr_enum (XDR *xdrs, enum_t *ep)
  427. {
  428. enum sizecheck
  429. {
  430. SIZEVAL
  431. }; /* used to find the size of an enum */
  432. /*
  433. * enums are treated as ints
  434. */
  435. if (sizeof (enum sizecheck) == 4)
  436. {
  437. #if INT_MAX < LONG_MAX
  438. long l;
  439. switch (xdrs->x_op)
  440. {
  441. case XDR_ENCODE:
  442. l = *ep;
  443. return XDR_PUTLONG (xdrs, &l);
  444. case XDR_DECODE:
  445. if (!XDR_GETLONG (xdrs, &l))
  446. {
  447. return FALSE;
  448. }
  449. *ep = l;
  450. case XDR_FREE:
  451. return TRUE;
  452. }
  453. return FALSE;
  454. #else
  455. return xdr_long (xdrs, (long *) ep);
  456. #endif
  457. }
  458. else if (sizeof (enum sizecheck) == sizeof (short))
  459. {
  460. return xdr_short (xdrs, (short *) ep);
  461. }
  462. else
  463. {
  464. return FALSE;
  465. }
  466. }
  467. #ifdef EXPORT_RPC_SYMBOLS
  468. libc_hidden_def (xdr_enum)
  469. #else
  470. libc_hidden_nolink_sunrpc (xdr_enum, GLIBC_2_0)
  471. #endif
  472. /*
  473. * XDR opaque data
  474. * Allows the specification of a fixed size sequence of opaque bytes.
  475. * cp points to the opaque object and cnt gives the byte length.
  476. */
  477. bool_t
  478. xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
  479. {
  480. u_int rndup;
  481. static char crud[BYTES_PER_XDR_UNIT];
  482. /*
  483. * if no data we are done
  484. */
  485. if (cnt == 0)
  486. return TRUE;
  487. /*
  488. * round byte count to full xdr units
  489. */
  490. rndup = cnt % BYTES_PER_XDR_UNIT;
  491. if (rndup > 0)
  492. rndup = BYTES_PER_XDR_UNIT - rndup;
  493. switch (xdrs->x_op)
  494. {
  495. case XDR_DECODE:
  496. if (!XDR_GETBYTES (xdrs, cp, cnt))
  497. {
  498. return FALSE;
  499. }
  500. if (rndup == 0)
  501. return TRUE;
  502. return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
  503. case XDR_ENCODE:
  504. if (!XDR_PUTBYTES (xdrs, cp, cnt))
  505. {
  506. return FALSE;
  507. }
  508. if (rndup == 0)
  509. return TRUE;
  510. return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
  511. case XDR_FREE:
  512. return TRUE;
  513. }
  514. return FALSE;
  515. }
  516. #ifdef EXPORT_RPC_SYMBOLS
  517. libc_hidden_def (xdr_opaque)
  518. #else
  519. libc_hidden_nolink_sunrpc (xdr_opaque, GLIBC_2_0)
  520. #endif
  521. /*
  522. * XDR counted bytes
  523. * *cpp is a pointer to the bytes, *sizep is the count.
  524. * If *cpp is NULL maxsize bytes are allocated
  525. */
  526. bool_t
  527. xdr_bytes (XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
  528. {
  529. char *sp = *cpp; /* sp is the actual string pointer */
  530. u_int nodesize;
  531. /*
  532. * first deal with the length since xdr bytes are counted
  533. */
  534. if (!xdr_u_int (xdrs, sizep))
  535. {
  536. return FALSE;
  537. }
  538. nodesize = *sizep;
  539. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
  540. {
  541. return FALSE;
  542. }
  543. /*
  544. * now deal with the actual bytes
  545. */
  546. switch (xdrs->x_op)
  547. {
  548. case XDR_DECODE:
  549. if (nodesize == 0)
  550. {
  551. return TRUE;
  552. }
  553. if (sp == NULL)
  554. {
  555. *cpp = sp = (char *) mem_alloc (nodesize);
  556. }
  557. if (sp == NULL)
  558. {
  559. (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
  560. return FALSE;
  561. }
  562. /* fall into ... */
  563. case XDR_ENCODE:
  564. return xdr_opaque (xdrs, sp, nodesize);
  565. case XDR_FREE:
  566. if (sp != NULL)
  567. {
  568. mem_free (sp, nodesize);
  569. *cpp = NULL;
  570. }
  571. return TRUE;
  572. }
  573. return FALSE;
  574. }
  575. #ifdef EXPORT_RPC_SYMBOLS
  576. libc_hidden_def (xdr_bytes)
  577. #else
  578. libc_hidden_nolink_sunrpc (xdr_bytes, GLIBC_2_0)
  579. #endif
  580. /*
  581. * Implemented here due to commonality of the object.
  582. */
  583. bool_t
  584. xdr_netobj (XDR *xdrs, struct netobj *np)
  585. {
  586. return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
  587. }
  588. #ifdef EXPORT_RPC_SYMBOLS
  589. libc_hidden_def (xdr_netobj)
  590. #else
  591. libc_hidden_nolink_sunrpc (xdr_netobj, GLIBC_2_0)
  592. #endif
  593. /*
  594. * XDR a discriminated union
  595. * Support routine for discriminated unions.
  596. * You create an array of xdrdiscrim structures, terminated with
  597. * an entry with a null procedure pointer. The routine gets
  598. * the discriminant value and then searches the array of xdrdiscrims
  599. * looking for that value. It calls the procedure given in the xdrdiscrim
  600. * to handle the discriminant. If there is no specific routine a default
  601. * routine may be called.
  602. * If there is no specific or default routine an error is returned.
  603. */
  604. bool_t
  605. xdr_union (XDR *xdrs,
  606. /* enum to decide which arm to work on */
  607. enum_t *dscmp,
  608. /* the union itself */
  609. char *unp,
  610. /* [value, xdr proc] for each arm */
  611. const struct xdr_discrim *choices,
  612. /* default xdr routine */
  613. xdrproc_t dfault)
  614. {
  615. enum_t dscm;
  616. /*
  617. * we deal with the discriminator; it's an enum
  618. */
  619. if (!xdr_enum (xdrs, dscmp))
  620. {
  621. return FALSE;
  622. }
  623. dscm = *dscmp;
  624. /*
  625. * search choices for a value that matches the discriminator.
  626. * if we find one, execute the xdr routine for that value.
  627. */
  628. for (; choices->proc != NULL_xdrproc_t; choices++)
  629. {
  630. if (choices->value == dscm)
  631. return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
  632. }
  633. /*
  634. * no match - execute the default xdr routine if there is one
  635. */
  636. return ((dfault == NULL_xdrproc_t) ? FALSE :
  637. (*dfault) (xdrs, unp, LASTUNSIGNED));
  638. }
  639. libc_hidden_nolink_sunrpc (xdr_union, GLIBC_2_0)
  640. /*
  641. * Non-portable xdr primitives.
  642. * Care should be taken when moving these routines to new architectures.
  643. */
  644. /*
  645. * XDR null terminated ASCII strings
  646. * xdr_string deals with "C strings" - arrays of bytes that are
  647. * terminated by a NULL character. The parameter cpp references a
  648. * pointer to storage; If the pointer is null, then the necessary
  649. * storage is allocated. The last parameter is the max allowed length
  650. * of the string as specified by a protocol.
  651. */
  652. bool_t
  653. xdr_string (XDR *xdrs, char **cpp, u_int maxsize)
  654. {
  655. char *sp = *cpp; /* sp is the actual string pointer */
  656. /* Initialize to silence the compiler. It is not really needed because SIZE
  657. never actually gets used without being initialized. */
  658. u_int size = 0;
  659. u_int nodesize;
  660. /*
  661. * first deal with the length since xdr strings are counted-strings
  662. */
  663. switch (xdrs->x_op)
  664. {
  665. case XDR_FREE:
  666. if (sp == NULL)
  667. {
  668. return TRUE; /* already free */
  669. }
  670. /* fall through... */
  671. case XDR_ENCODE:
  672. if (sp == NULL)
  673. return FALSE;
  674. size = strlen (sp);
  675. break;
  676. case XDR_DECODE:
  677. break;
  678. }
  679. if (!xdr_u_int (xdrs, &size))
  680. {
  681. return FALSE;
  682. }
  683. if (size > maxsize)
  684. {
  685. return FALSE;
  686. }
  687. nodesize = size + 1;
  688. if (nodesize == 0)
  689. {
  690. /* This means an overflow. It a bug in the caller which
  691. provided a too large maxsize but nevertheless catch it
  692. here. */
  693. return FALSE;
  694. }
  695. /*
  696. * now deal with the actual bytes
  697. */
  698. switch (xdrs->x_op)
  699. {
  700. case XDR_DECODE:
  701. if (sp == NULL)
  702. *cpp = sp = (char *) mem_alloc (nodesize);
  703. if (sp == NULL)
  704. {
  705. (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
  706. return FALSE;
  707. }
  708. sp[size] = 0;
  709. /* fall into ... */
  710. case XDR_ENCODE:
  711. return xdr_opaque (xdrs, sp, size);
  712. case XDR_FREE:
  713. mem_free (sp, nodesize);
  714. *cpp = NULL;
  715. return TRUE;
  716. }
  717. return FALSE;
  718. }
  719. #ifdef EXPORT_RPC_SYMBOLS
  720. libc_hidden_def (xdr_string)
  721. #else
  722. libc_hidden_nolink_sunrpc (xdr_string, GLIBC_2_0)
  723. #endif
  724. /*
  725. * Wrapper for xdr_string that can be called directly from
  726. * routines like clnt_call
  727. */
  728. bool_t
  729. xdr_wrapstring (XDR *xdrs, char **cpp)
  730. {
  731. if (xdr_string (xdrs, cpp, LASTUNSIGNED))
  732. {
  733. return TRUE;
  734. }
  735. return FALSE;
  736. }
  737. #ifdef EXPORT_RPC_SYMBOLS
  738. libc_hidden_def (xdr_wrapstring)
  739. #else
  740. libc_hidden_nolink_sunrpc (xdr_wrapstring, GLIBC_2_0)
  741. #endif