xdr.c 14 KB

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