xdr.c 14 KB

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