xdr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. #if 0
  31. static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
  32. #endif
  33. /*
  34. * xdr.c, Generic XDR routines implementation.
  35. *
  36. * Copyright (C) 1986, Sun Microsystems, Inc.
  37. *
  38. * These are the "generic" xdr routines used to serialize and de-serialize
  39. * most common data items. See xdr.h for more info on the interface to
  40. * xdr.
  41. */
  42. #define __FORCE_GLIBC
  43. #define _GNU_SOURCE
  44. #include <features.h>
  45. #include <stdio.h>
  46. #include <limits.h>
  47. #include <string.h>
  48. #include <rpc/types.h>
  49. #include <rpc/xdr.h>
  50. #ifdef USE_IN_LIBIO
  51. # include <wchar.h>
  52. #endif
  53. /*
  54. * constants specific to the xdr "protocol"
  55. */
  56. #define XDR_FALSE ((long) 0)
  57. #define XDR_TRUE ((long) 1)
  58. #define LASTUNSIGNED ((u_int) 0-1)
  59. /*
  60. * for unit alignment
  61. */
  62. static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
  63. /*
  64. * Free a data structure using XDR
  65. * Not a filter, but a convenient utility nonetheless
  66. */
  67. void
  68. xdr_free (xdrproc_t proc, char *objp)
  69. {
  70. XDR x;
  71. x.x_op = XDR_FREE;
  72. (*proc) (&x, objp);
  73. }
  74. /*
  75. * XDR nothing
  76. */
  77. bool_t
  78. xdr_void (void)
  79. {
  80. return TRUE;
  81. }
  82. /*
  83. * XDR integers
  84. */
  85. bool_t
  86. xdr_int (XDR *xdrs, int *ip)
  87. {
  88. #if INT_MAX < LONG_MAX
  89. long l;
  90. switch (xdrs->x_op)
  91. {
  92. case XDR_ENCODE:
  93. l = (long) *ip;
  94. return XDR_PUTLONG (xdrs, &l);
  95. case XDR_DECODE:
  96. if (!XDR_GETLONG (xdrs, &l))
  97. {
  98. return FALSE;
  99. }
  100. *ip = (int) l;
  101. case XDR_FREE:
  102. return TRUE;
  103. }
  104. return FALSE;
  105. #elif INT_MAX == LONG_MAX
  106. return xdr_long (xdrs, (long *) ip);
  107. #elif INT_MAX == SHRT_MAX
  108. return xdr_short (xdrs, (short *) ip);
  109. #else
  110. #error unexpected integer sizes in_xdr_int()
  111. #endif
  112. }
  113. /*
  114. * XDR unsigned integers
  115. */
  116. bool_t
  117. xdr_u_int (XDR *xdrs, u_int *up)
  118. {
  119. #if UINT_MAX < ULONG_MAX
  120. u_long l;
  121. switch (xdrs->x_op)
  122. {
  123. case XDR_ENCODE:
  124. l = (u_long) * up;
  125. return XDR_PUTLONG (xdrs, &l);
  126. case XDR_DECODE:
  127. if (!XDR_GETLONG (xdrs, &l))
  128. {
  129. return FALSE;
  130. }
  131. *up = (u_int) l;
  132. case XDR_FREE:
  133. return TRUE;
  134. }
  135. return FALSE;
  136. #elif UINT_MAX == ULONG_MAX
  137. return xdr_u_long (xdrs, (u_long *) up);
  138. #elif UINT_MAX == USHRT_MAX
  139. return xdr_short (xdrs, (short *) up);
  140. #else
  141. #error unexpected integer sizes in_xdr_u_int()
  142. #endif
  143. }
  144. /*
  145. * XDR long integers
  146. * The definition of xdr_long() is kept for backward
  147. * compatibility. Instead xdr_int() should be used.
  148. */
  149. bool_t
  150. xdr_long (XDR *xdrs, long *lp)
  151. {
  152. if (xdrs->x_op == XDR_ENCODE
  153. && (sizeof (int32_t) == sizeof (long)
  154. || (int32_t) *lp == *lp))
  155. return XDR_PUTLONG (xdrs, lp);
  156. if (xdrs->x_op == XDR_DECODE)
  157. return XDR_GETLONG (xdrs, lp);
  158. if (xdrs->x_op == XDR_FREE)
  159. return TRUE;
  160. return FALSE;
  161. }
  162. /*
  163. * XDR unsigned long integers
  164. * The definition of xdr_u_long() is kept for backward
  165. * compatibility. Instead xdr_u_int() should be used.
  166. */
  167. bool_t
  168. xdr_u_long (XDR *xdrs, u_long *ulp)
  169. {
  170. switch (xdrs->x_op)
  171. {
  172. case XDR_DECODE:
  173. {
  174. long int tmp;
  175. if (XDR_GETLONG (xdrs, &tmp) == FALSE)
  176. return FALSE;
  177. *ulp = (uint32_t) tmp;
  178. return TRUE;
  179. }
  180. case XDR_ENCODE:
  181. if (sizeof (uint32_t) != sizeof (u_long)
  182. && (uint32_t) *ulp != *ulp)
  183. return FALSE;
  184. return XDR_PUTLONG (xdrs, (long *) ulp);
  185. case XDR_FREE:
  186. return TRUE;
  187. }
  188. return FALSE;
  189. }
  190. /*
  191. * XDR hyper integers
  192. * same as xdr_u_hyper - open coded to save a proc call!
  193. */
  194. bool_t attribute_hidden
  195. __xdr_hyper (XDR *xdrs, quad_t *llp)
  196. {
  197. long t1;
  198. unsigned long int t2;
  199. if (xdrs->x_op == XDR_ENCODE)
  200. {
  201. t1 = (long) ((*llp) >> 32);
  202. t2 = (long) (*llp);
  203. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  204. }
  205. if (xdrs->x_op == XDR_DECODE)
  206. {
  207. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  208. return FALSE;
  209. *llp = ((quad_t) t1) << 32;
  210. *llp |= t2;
  211. return TRUE;
  212. }
  213. if (xdrs->x_op == XDR_FREE)
  214. return TRUE;
  215. return FALSE;
  216. }
  217. strong_alias(__xdr_hyper,xdr_hyper)
  218. /*
  219. * XDR hyper integers
  220. * same as xdr_hyper - open coded to save a proc call!
  221. */
  222. bool_t attribute_hidden
  223. __xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
  224. {
  225. unsigned long t1;
  226. unsigned long t2;
  227. if (xdrs->x_op == XDR_ENCODE)
  228. {
  229. t1 = (unsigned long) ((*ullp) >> 32);
  230. t2 = (unsigned long) (*ullp);
  231. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  232. }
  233. if (xdrs->x_op == XDR_DECODE)
  234. {
  235. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  236. return FALSE;
  237. *ullp = ((u_quad_t) t1) << 32;
  238. *ullp |= t2;
  239. return TRUE;
  240. }
  241. if (xdrs->x_op == XDR_FREE)
  242. return TRUE;
  243. return FALSE;
  244. }
  245. strong_alias(__xdr_u_hyper,xdr_u_hyper)
  246. bool_t
  247. xdr_longlong_t (XDR *xdrs, quad_t *llp)
  248. {
  249. return __xdr_hyper (xdrs, llp);
  250. }
  251. bool_t
  252. xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
  253. {
  254. return __xdr_u_hyper (xdrs, ullp);
  255. }
  256. /*
  257. * XDR short integers
  258. */
  259. bool_t
  260. xdr_short (XDR *xdrs, short *sp)
  261. {
  262. long l;
  263. switch (xdrs->x_op)
  264. {
  265. case XDR_ENCODE:
  266. l = (long) *sp;
  267. return XDR_PUTLONG (xdrs, &l);
  268. case XDR_DECODE:
  269. if (!XDR_GETLONG (xdrs, &l))
  270. {
  271. return FALSE;
  272. }
  273. *sp = (short) l;
  274. return TRUE;
  275. case XDR_FREE:
  276. return TRUE;
  277. }
  278. return FALSE;
  279. }
  280. /*
  281. * XDR unsigned short integers
  282. */
  283. bool_t
  284. xdr_u_short (XDR *xdrs, u_short *usp)
  285. {
  286. u_long l;
  287. switch (xdrs->x_op)
  288. {
  289. case XDR_ENCODE:
  290. l = (u_long) * usp;
  291. return XDR_PUTLONG (xdrs, &l);
  292. case XDR_DECODE:
  293. if (!XDR_GETLONG (xdrs, &l))
  294. {
  295. return FALSE;
  296. }
  297. *usp = (u_short) l;
  298. return TRUE;
  299. case XDR_FREE:
  300. return TRUE;
  301. }
  302. return FALSE;
  303. }
  304. /*
  305. * XDR a char
  306. */
  307. bool_t
  308. xdr_char (XDR *xdrs, char *cp)
  309. {
  310. int i;
  311. i = (*cp);
  312. if (!xdr_int (xdrs, &i))
  313. {
  314. return FALSE;
  315. }
  316. *cp = i;
  317. return TRUE;
  318. }
  319. /*
  320. * XDR an unsigned char
  321. */
  322. bool_t
  323. xdr_u_char (XDR *xdrs, u_char *cp)
  324. {
  325. u_int u;
  326. u = (*cp);
  327. if (!xdr_u_int (xdrs, &u))
  328. {
  329. return FALSE;
  330. }
  331. *cp = u;
  332. return TRUE;
  333. }
  334. /*
  335. * XDR booleans
  336. */
  337. bool_t
  338. xdr_bool (XDR *xdrs, bool_t *bp)
  339. {
  340. long lb;
  341. switch (xdrs->x_op)
  342. {
  343. case XDR_ENCODE:
  344. lb = *bp ? XDR_TRUE : XDR_FALSE;
  345. return XDR_PUTLONG (xdrs, &lb);
  346. case XDR_DECODE:
  347. if (!XDR_GETLONG (xdrs, &lb))
  348. {
  349. return FALSE;
  350. }
  351. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  352. return TRUE;
  353. case XDR_FREE:
  354. return TRUE;
  355. }
  356. return FALSE;
  357. }
  358. /*
  359. * XDR enumerations
  360. */
  361. bool_t
  362. xdr_enum (XDR *xdrs, enum_t *ep)
  363. {
  364. enum sizecheck
  365. {
  366. SIZEVAL
  367. }; /* used to find the size of an enum */
  368. /*
  369. * enums are treated as ints
  370. */
  371. if (sizeof (enum sizecheck) == 4)
  372. {
  373. #if INT_MAX < LONG_MAX
  374. long l;
  375. switch (xdrs->x_op)
  376. {
  377. case XDR_ENCODE:
  378. l = *ep;
  379. return XDR_PUTLONG (xdrs, &l);
  380. case XDR_DECODE:
  381. if (!XDR_GETLONG (xdrs, &l))
  382. {
  383. return FALSE;
  384. }
  385. *ep = l;
  386. case XDR_FREE:
  387. return TRUE;
  388. }
  389. return FALSE;
  390. #else
  391. return xdr_long (xdrs, (long *) ep);
  392. #endif
  393. }
  394. else if (sizeof (enum sizecheck) == sizeof (short))
  395. {
  396. return xdr_short (xdrs, (short *) ep);
  397. }
  398. else
  399. {
  400. return FALSE;
  401. }
  402. }
  403. /*
  404. * XDR opaque data
  405. * Allows the specification of a fixed size sequence of opaque bytes.
  406. * cp points to the opaque object and cnt gives the byte length.
  407. */
  408. bool_t
  409. xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
  410. {
  411. u_int rndup;
  412. static char crud[BYTES_PER_XDR_UNIT];
  413. /*
  414. * if no data we are done
  415. */
  416. if (cnt == 0)
  417. return TRUE;
  418. /*
  419. * round byte count to full xdr units
  420. */
  421. rndup = cnt % BYTES_PER_XDR_UNIT;
  422. if (rndup > 0)
  423. rndup = BYTES_PER_XDR_UNIT - rndup;
  424. switch (xdrs->x_op)
  425. {
  426. case XDR_DECODE:
  427. if (!XDR_GETBYTES (xdrs, cp, cnt))
  428. {
  429. return FALSE;
  430. }
  431. if (rndup == 0)
  432. return TRUE;
  433. return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
  434. case XDR_ENCODE:
  435. if (!XDR_PUTBYTES (xdrs, cp, cnt))
  436. {
  437. return FALSE;
  438. }
  439. if (rndup == 0)
  440. return TRUE;
  441. return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
  442. case XDR_FREE:
  443. return TRUE;
  444. }
  445. return FALSE;
  446. }
  447. /*
  448. * XDR counted bytes
  449. * *cpp is a pointer to the bytes, *sizep is the count.
  450. * If *cpp is NULL maxsize bytes are allocated
  451. */
  452. bool_t
  453. xdr_bytes (xdrs, cpp, sizep, maxsize)
  454. XDR *xdrs;
  455. char **cpp;
  456. u_int *sizep;
  457. u_int maxsize;
  458. {
  459. char *sp = *cpp; /* sp is the actual string pointer */
  460. u_int nodesize;
  461. /*
  462. * first deal with the length since xdr bytes are counted
  463. */
  464. if (!xdr_u_int (xdrs, sizep))
  465. {
  466. return FALSE;
  467. }
  468. nodesize = *sizep;
  469. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
  470. {
  471. return FALSE;
  472. }
  473. /*
  474. * now deal with the actual bytes
  475. */
  476. switch (xdrs->x_op)
  477. {
  478. case XDR_DECODE:
  479. if (nodesize == 0)
  480. {
  481. return TRUE;
  482. }
  483. if (sp == NULL)
  484. {
  485. *cpp = sp = (char *) mem_alloc (nodesize);
  486. }
  487. if (sp == NULL)
  488. {
  489. #ifdef USE_IN_LIBIO
  490. if (_IO_fwide (stderr, 0) > 0)
  491. (void) __fwprintf (stderr, L"%s", _("xdr_bytes: out of memory\n"));
  492. else
  493. #endif
  494. (void) fputs (_("xdr_bytes: out of memory\n"), stderr);
  495. return FALSE;
  496. }
  497. /* fall into ... */
  498. case XDR_ENCODE:
  499. return xdr_opaque (xdrs, sp, nodesize);
  500. case XDR_FREE:
  501. if (sp != NULL)
  502. {
  503. mem_free (sp, nodesize);
  504. *cpp = NULL;
  505. }
  506. return TRUE;
  507. }
  508. return FALSE;
  509. }
  510. /*
  511. * Implemented here due to commonality of the object.
  512. */
  513. bool_t
  514. xdr_netobj (xdrs, np)
  515. XDR *xdrs;
  516. struct netobj *np;
  517. {
  518. return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
  519. }
  520. /*
  521. * XDR a discriminated union
  522. * Support routine for discriminated unions.
  523. * You create an array of xdrdiscrim structures, terminated with
  524. * an entry with a null procedure pointer. The routine gets
  525. * the discriminant value and then searches the array of xdrdiscrims
  526. * looking for that value. It calls the procedure given in the xdrdiscrim
  527. * to handle the discriminant. If there is no specific routine a default
  528. * routine may be called.
  529. * If there is no specific or default routine an error is returned.
  530. */
  531. bool_t
  532. xdr_union (xdrs, dscmp, unp, choices, dfault)
  533. XDR *xdrs;
  534. enum_t *dscmp; /* enum to decide which arm to work on */
  535. char *unp; /* the union itself */
  536. const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
  537. xdrproc_t dfault; /* default xdr routine */
  538. {
  539. enum_t dscm;
  540. /*
  541. * we deal with the discriminator; it's an enum
  542. */
  543. if (!xdr_enum (xdrs, dscmp))
  544. {
  545. return FALSE;
  546. }
  547. dscm = *dscmp;
  548. /*
  549. * search choices for a value that matches the discriminator.
  550. * if we find one, execute the xdr routine for that value.
  551. */
  552. for (; choices->proc != NULL_xdrproc_t; choices++)
  553. {
  554. if (choices->value == dscm)
  555. return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
  556. }
  557. /*
  558. * no match - execute the default xdr routine if there is one
  559. */
  560. return ((dfault == NULL_xdrproc_t) ? FALSE :
  561. (*dfault) (xdrs, unp, LASTUNSIGNED));
  562. }
  563. /*
  564. * Non-portable xdr primitives.
  565. * Care should be taken when moving these routines to new architectures.
  566. */
  567. /*
  568. * XDR null terminated ASCII strings
  569. * xdr_string deals with "C strings" - arrays of bytes that are
  570. * terminated by a NULL character. The parameter cpp references a
  571. * pointer to storage; If the pointer is null, then the necessary
  572. * storage is allocated. The last parameter is the max allowed length
  573. * of the string as specified by a protocol.
  574. */
  575. bool_t
  576. xdr_string (xdrs, cpp, maxsize)
  577. XDR *xdrs;
  578. char **cpp;
  579. u_int maxsize;
  580. {
  581. char *sp = *cpp; /* sp is the actual string pointer */
  582. u_int size;
  583. u_int nodesize;
  584. /*
  585. * first deal with the length since xdr strings are counted-strings
  586. */
  587. switch (xdrs->x_op)
  588. {
  589. case XDR_FREE:
  590. if (sp == NULL)
  591. {
  592. return TRUE; /* already free */
  593. }
  594. /* fall through... */
  595. case XDR_ENCODE:
  596. if (sp == NULL)
  597. return FALSE;
  598. size = __strlen (sp);
  599. break;
  600. case XDR_DECODE:
  601. break;
  602. }
  603. if (!xdr_u_int (xdrs, &size))
  604. {
  605. return FALSE;
  606. }
  607. if (size > maxsize)
  608. {
  609. return FALSE;
  610. }
  611. nodesize = size + 1;
  612. /*
  613. * now deal with the actual bytes
  614. */
  615. switch (xdrs->x_op)
  616. {
  617. case XDR_DECODE:
  618. if (nodesize == 0)
  619. {
  620. return TRUE;
  621. }
  622. if (sp == NULL)
  623. *cpp = sp = (char *) mem_alloc (nodesize);
  624. if (sp == NULL)
  625. {
  626. #ifdef USE_IN_LIBIO
  627. if (_IO_fwide (stderr, 0) > 0)
  628. (void) __fwprintf (stderr, L"%s",
  629. _("xdr_string: out of memory\n"));
  630. else
  631. #endif
  632. (void) fputs (_("xdr_string: out of memory\n"), stderr);
  633. return FALSE;
  634. }
  635. sp[size] = 0;
  636. /* fall into ... */
  637. case XDR_ENCODE:
  638. return xdr_opaque (xdrs, sp, size);
  639. case XDR_FREE:
  640. mem_free (sp, nodesize);
  641. *cpp = NULL;
  642. return TRUE;
  643. }
  644. return FALSE;
  645. }
  646. /*
  647. * Wrapper for xdr_string that can be called directly from
  648. * routines like clnt_call
  649. */
  650. bool_t
  651. xdr_wrapstring (xdrs, cpp)
  652. XDR *xdrs;
  653. char **cpp;
  654. {
  655. if (xdr_string (xdrs, cpp, LASTUNSIGNED))
  656. {
  657. return TRUE;
  658. }
  659. return FALSE;
  660. }