xdr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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
  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. /*
  218. * XDR hyper integers
  219. * same as xdr_hyper - open coded to save a proc call!
  220. */
  221. bool_t
  222. xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
  223. {
  224. unsigned long t1;
  225. unsigned long t2;
  226. if (xdrs->x_op == XDR_ENCODE)
  227. {
  228. t1 = (unsigned long) ((*ullp) >> 32);
  229. t2 = (unsigned long) (*ullp);
  230. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  231. }
  232. if (xdrs->x_op == XDR_DECODE)
  233. {
  234. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  235. return FALSE;
  236. *ullp = ((u_quad_t) t1) << 32;
  237. *ullp |= t2;
  238. return TRUE;
  239. }
  240. if (xdrs->x_op == XDR_FREE)
  241. return TRUE;
  242. return FALSE;
  243. }
  244. bool_t
  245. xdr_longlong_t (XDR *xdrs, quad_t *llp)
  246. {
  247. return xdr_hyper (xdrs, llp);
  248. }
  249. bool_t
  250. xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
  251. {
  252. return xdr_u_hyper (xdrs, ullp);
  253. }
  254. /*
  255. * XDR short integers
  256. */
  257. bool_t
  258. xdr_short (XDR *xdrs, short *sp)
  259. {
  260. long l;
  261. switch (xdrs->x_op)
  262. {
  263. case XDR_ENCODE:
  264. l = (long) *sp;
  265. return XDR_PUTLONG (xdrs, &l);
  266. case XDR_DECODE:
  267. if (!XDR_GETLONG (xdrs, &l))
  268. {
  269. return FALSE;
  270. }
  271. *sp = (short) l;
  272. return TRUE;
  273. case XDR_FREE:
  274. return TRUE;
  275. }
  276. return FALSE;
  277. }
  278. /*
  279. * XDR unsigned short integers
  280. */
  281. bool_t
  282. xdr_u_short (XDR *xdrs, u_short *usp)
  283. {
  284. u_long l;
  285. switch (xdrs->x_op)
  286. {
  287. case XDR_ENCODE:
  288. l = (u_long) * usp;
  289. return XDR_PUTLONG (xdrs, &l);
  290. case XDR_DECODE:
  291. if (!XDR_GETLONG (xdrs, &l))
  292. {
  293. return FALSE;
  294. }
  295. *usp = (u_short) l;
  296. return TRUE;
  297. case XDR_FREE:
  298. return TRUE;
  299. }
  300. return FALSE;
  301. }
  302. /*
  303. * XDR a char
  304. */
  305. bool_t
  306. xdr_char (XDR *xdrs, char *cp)
  307. {
  308. int i;
  309. i = (*cp);
  310. if (!xdr_int (xdrs, &i))
  311. {
  312. return FALSE;
  313. }
  314. *cp = i;
  315. return TRUE;
  316. }
  317. /*
  318. * XDR an unsigned char
  319. */
  320. bool_t
  321. xdr_u_char (XDR *xdrs, u_char *cp)
  322. {
  323. u_int u;
  324. u = (*cp);
  325. if (!xdr_u_int (xdrs, &u))
  326. {
  327. return FALSE;
  328. }
  329. *cp = u;
  330. return TRUE;
  331. }
  332. /*
  333. * XDR booleans
  334. */
  335. bool_t
  336. xdr_bool (XDR *xdrs, bool_t *bp)
  337. {
  338. long lb;
  339. switch (xdrs->x_op)
  340. {
  341. case XDR_ENCODE:
  342. lb = *bp ? XDR_TRUE : XDR_FALSE;
  343. return XDR_PUTLONG (xdrs, &lb);
  344. case XDR_DECODE:
  345. if (!XDR_GETLONG (xdrs, &lb))
  346. {
  347. return FALSE;
  348. }
  349. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  350. return TRUE;
  351. case XDR_FREE:
  352. return TRUE;
  353. }
  354. return FALSE;
  355. }
  356. /*
  357. * XDR enumerations
  358. */
  359. bool_t
  360. xdr_enum (XDR *xdrs, enum_t *ep)
  361. {
  362. enum sizecheck
  363. {
  364. SIZEVAL
  365. }; /* used to find the size of an enum */
  366. /*
  367. * enums are treated as ints
  368. */
  369. if (sizeof (enum sizecheck) == 4)
  370. {
  371. #if INT_MAX < LONG_MAX
  372. long l;
  373. switch (xdrs->x_op)
  374. {
  375. case XDR_ENCODE:
  376. l = *ep;
  377. return XDR_PUTLONG (xdrs, &l);
  378. case XDR_DECODE:
  379. if (!XDR_GETLONG (xdrs, &l))
  380. {
  381. return FALSE;
  382. }
  383. *ep = l;
  384. case XDR_FREE:
  385. return TRUE;
  386. }
  387. return FALSE;
  388. #else
  389. return xdr_long (xdrs, (long *) ep);
  390. #endif
  391. }
  392. else if (sizeof (enum sizecheck) == sizeof (short))
  393. {
  394. return xdr_short (xdrs, (short *) ep);
  395. }
  396. else
  397. {
  398. return FALSE;
  399. }
  400. }
  401. /*
  402. * XDR opaque data
  403. * Allows the specification of a fixed size sequence of opaque bytes.
  404. * cp points to the opaque object and cnt gives the byte length.
  405. */
  406. bool_t
  407. xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
  408. {
  409. u_int rndup;
  410. static char crud[BYTES_PER_XDR_UNIT];
  411. /*
  412. * if no data we are done
  413. */
  414. if (cnt == 0)
  415. return TRUE;
  416. /*
  417. * round byte count to full xdr units
  418. */
  419. rndup = cnt % BYTES_PER_XDR_UNIT;
  420. if (rndup > 0)
  421. rndup = BYTES_PER_XDR_UNIT - rndup;
  422. switch (xdrs->x_op)
  423. {
  424. case XDR_DECODE:
  425. if (!XDR_GETBYTES (xdrs, cp, cnt))
  426. {
  427. return FALSE;
  428. }
  429. if (rndup == 0)
  430. return TRUE;
  431. return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
  432. case XDR_ENCODE:
  433. if (!XDR_PUTBYTES (xdrs, cp, cnt))
  434. {
  435. return FALSE;
  436. }
  437. if (rndup == 0)
  438. return TRUE;
  439. return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
  440. case XDR_FREE:
  441. return TRUE;
  442. }
  443. return FALSE;
  444. }
  445. /*
  446. * XDR counted bytes
  447. * *cpp is a pointer to the bytes, *sizep is the count.
  448. * If *cpp is NULL maxsize bytes are allocated
  449. */
  450. bool_t
  451. xdr_bytes (xdrs, cpp, sizep, maxsize)
  452. XDR *xdrs;
  453. char **cpp;
  454. u_int *sizep;
  455. u_int maxsize;
  456. {
  457. char *sp = *cpp; /* sp is the actual string pointer */
  458. u_int nodesize;
  459. /*
  460. * first deal with the length since xdr bytes are counted
  461. */
  462. if (!xdr_u_int (xdrs, sizep))
  463. {
  464. return FALSE;
  465. }
  466. nodesize = *sizep;
  467. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
  468. {
  469. return FALSE;
  470. }
  471. /*
  472. * now deal with the actual bytes
  473. */
  474. switch (xdrs->x_op)
  475. {
  476. case XDR_DECODE:
  477. if (nodesize == 0)
  478. {
  479. return TRUE;
  480. }
  481. if (sp == NULL)
  482. {
  483. *cpp = sp = (char *) mem_alloc (nodesize);
  484. }
  485. if (sp == NULL)
  486. {
  487. #ifdef USE_IN_LIBIO
  488. if (_IO_fwide (stderr, 0) > 0)
  489. (void) __fwprintf (stderr, L"%s", _("xdr_bytes: out of memory\n"));
  490. else
  491. #endif
  492. (void) fputs (_("xdr_bytes: out of memory\n"), stderr);
  493. return FALSE;
  494. }
  495. /* fall into ... */
  496. case XDR_ENCODE:
  497. return xdr_opaque (xdrs, sp, nodesize);
  498. case XDR_FREE:
  499. if (sp != NULL)
  500. {
  501. mem_free (sp, nodesize);
  502. *cpp = NULL;
  503. }
  504. return TRUE;
  505. }
  506. return FALSE;
  507. }
  508. /*
  509. * Implemented here due to commonality of the object.
  510. */
  511. bool_t
  512. xdr_netobj (xdrs, np)
  513. XDR *xdrs;
  514. struct netobj *np;
  515. {
  516. return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
  517. }
  518. /*
  519. * XDR a discriminated union
  520. * Support routine for discriminated unions.
  521. * You create an array of xdrdiscrim structures, terminated with
  522. * an entry with a null procedure pointer. The routine gets
  523. * the discriminant value and then searches the array of xdrdiscrims
  524. * looking for that value. It calls the procedure given in the xdrdiscrim
  525. * to handle the discriminant. If there is no specific routine a default
  526. * routine may be called.
  527. * If there is no specific or default routine an error is returned.
  528. */
  529. bool_t
  530. xdr_union (xdrs, dscmp, unp, choices, dfault)
  531. XDR *xdrs;
  532. enum_t *dscmp; /* enum to decide which arm to work on */
  533. char *unp; /* the union itself */
  534. const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
  535. xdrproc_t dfault; /* default xdr routine */
  536. {
  537. enum_t dscm;
  538. /*
  539. * we deal with the discriminator; it's an enum
  540. */
  541. if (!xdr_enum (xdrs, dscmp))
  542. {
  543. return FALSE;
  544. }
  545. dscm = *dscmp;
  546. /*
  547. * search choices for a value that matches the discriminator.
  548. * if we find one, execute the xdr routine for that value.
  549. */
  550. for (; choices->proc != NULL_xdrproc_t; choices++)
  551. {
  552. if (choices->value == dscm)
  553. return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
  554. }
  555. /*
  556. * no match - execute the default xdr routine if there is one
  557. */
  558. return ((dfault == NULL_xdrproc_t) ? FALSE :
  559. (*dfault) (xdrs, unp, LASTUNSIGNED));
  560. }
  561. /*
  562. * Non-portable xdr primitives.
  563. * Care should be taken when moving these routines to new architectures.
  564. */
  565. /*
  566. * XDR null terminated ASCII strings
  567. * xdr_string deals with "C strings" - arrays of bytes that are
  568. * terminated by a NULL character. The parameter cpp references a
  569. * pointer to storage; If the pointer is null, then the necessary
  570. * storage is allocated. The last parameter is the max allowed length
  571. * of the string as specified by a protocol.
  572. */
  573. bool_t
  574. xdr_string (xdrs, cpp, maxsize)
  575. XDR *xdrs;
  576. char **cpp;
  577. u_int maxsize;
  578. {
  579. char *sp = *cpp; /* sp is the actual string pointer */
  580. u_int size;
  581. u_int nodesize;
  582. /*
  583. * first deal with the length since xdr strings are counted-strings
  584. */
  585. switch (xdrs->x_op)
  586. {
  587. case XDR_FREE:
  588. if (sp == NULL)
  589. {
  590. return TRUE; /* already free */
  591. }
  592. /* fall through... */
  593. case XDR_ENCODE:
  594. if (sp == NULL)
  595. return FALSE;
  596. size = strlen (sp);
  597. break;
  598. case XDR_DECODE:
  599. break;
  600. }
  601. if (!xdr_u_int (xdrs, &size))
  602. {
  603. return FALSE;
  604. }
  605. if (size > maxsize)
  606. {
  607. return FALSE;
  608. }
  609. nodesize = size + 1;
  610. /*
  611. * now deal with the actual bytes
  612. */
  613. switch (xdrs->x_op)
  614. {
  615. case XDR_DECODE:
  616. if (nodesize == 0)
  617. {
  618. return TRUE;
  619. }
  620. if (sp == NULL)
  621. *cpp = sp = (char *) mem_alloc (nodesize);
  622. if (sp == NULL)
  623. {
  624. #ifdef USE_IN_LIBIO
  625. if (_IO_fwide (stderr, 0) > 0)
  626. (void) __fwprintf (stderr, L"%s",
  627. _("xdr_string: out of memory\n"));
  628. else
  629. #endif
  630. (void) fputs (_("xdr_string: out of memory\n"), stderr);
  631. return FALSE;
  632. }
  633. sp[size] = 0;
  634. /* fall into ... */
  635. case XDR_ENCODE:
  636. return xdr_opaque (xdrs, sp, size);
  637. case XDR_FREE:
  638. mem_free (sp, nodesize);
  639. *cpp = NULL;
  640. return TRUE;
  641. }
  642. return FALSE;
  643. }
  644. /*
  645. * Wrapper for xdr_string that can be called directly from
  646. * routines like clnt_call
  647. */
  648. bool_t
  649. xdr_wrapstring (xdrs, cpp)
  650. XDR *xdrs;
  651. char **cpp;
  652. {
  653. if (xdr_string (xdrs, cpp, LASTUNSIGNED))
  654. {
  655. return TRUE;
  656. }
  657. return FALSE;
  658. }