xdr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 attribute_hidden
  453. __xdr_bytes (XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
  454. {
  455. char *sp = *cpp; /* sp is the actual string pointer */
  456. u_int nodesize;
  457. /*
  458. * first deal with the length since xdr bytes are counted
  459. */
  460. if (!xdr_u_int (xdrs, sizep))
  461. {
  462. return FALSE;
  463. }
  464. nodesize = *sizep;
  465. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
  466. {
  467. return FALSE;
  468. }
  469. /*
  470. * now deal with the actual bytes
  471. */
  472. switch (xdrs->x_op)
  473. {
  474. case XDR_DECODE:
  475. if (nodesize == 0)
  476. {
  477. return TRUE;
  478. }
  479. if (sp == NULL)
  480. {
  481. *cpp = sp = (char *) mem_alloc (nodesize);
  482. }
  483. if (sp == NULL)
  484. {
  485. #ifdef USE_IN_LIBIO
  486. if (_IO_fwide (stderr, 0) > 0)
  487. (void) __fwprintf (stderr, L"%s", _("xdr_bytes: out of memory\n"));
  488. else
  489. #endif
  490. (void) fputs (_("xdr_bytes: out of memory\n"), stderr);
  491. return FALSE;
  492. }
  493. /* fall into ... */
  494. case XDR_ENCODE:
  495. return xdr_opaque (xdrs, sp, nodesize);
  496. case XDR_FREE:
  497. if (sp != NULL)
  498. {
  499. mem_free (sp, nodesize);
  500. *cpp = NULL;
  501. }
  502. return TRUE;
  503. }
  504. return FALSE;
  505. }
  506. strong_alias(__xdr_bytes,xdr_bytes)
  507. /*
  508. * Implemented here due to commonality of the object.
  509. */
  510. bool_t
  511. xdr_netobj (xdrs, np)
  512. XDR *xdrs;
  513. struct netobj *np;
  514. {
  515. return __xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
  516. }
  517. /*
  518. * XDR a discriminated union
  519. * Support routine for discriminated unions.
  520. * You create an array of xdrdiscrim structures, terminated with
  521. * an entry with a null procedure pointer. The routine gets
  522. * the discriminant value and then searches the array of xdrdiscrims
  523. * looking for that value. It calls the procedure given in the xdrdiscrim
  524. * to handle the discriminant. If there is no specific routine a default
  525. * routine may be called.
  526. * If there is no specific or default routine an error is returned.
  527. */
  528. bool_t attribute_hidden
  529. __xdr_union (XDR *xdrs, enum_t *dscmp, char *unp, const struct xdr_discrim *choices, xdrproc_t dfault)
  530. {
  531. enum_t dscm;
  532. /*
  533. * we deal with the discriminator; it's an enum
  534. */
  535. if (!xdr_enum (xdrs, dscmp))
  536. {
  537. return FALSE;
  538. }
  539. dscm = *dscmp;
  540. /*
  541. * search choices for a value that matches the discriminator.
  542. * if we find one, execute the xdr routine for that value.
  543. */
  544. for (; choices->proc != NULL_xdrproc_t; choices++)
  545. {
  546. if (choices->value == dscm)
  547. return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
  548. }
  549. /*
  550. * no match - execute the default xdr routine if there is one
  551. */
  552. return ((dfault == NULL_xdrproc_t) ? FALSE :
  553. (*dfault) (xdrs, unp, LASTUNSIGNED));
  554. }
  555. strong_alias(__xdr_union,xdr_union)
  556. /*
  557. * Non-portable xdr primitives.
  558. * Care should be taken when moving these routines to new architectures.
  559. */
  560. /*
  561. * XDR null terminated ASCII strings
  562. * xdr_string deals with "C strings" - arrays of bytes that are
  563. * terminated by a NULL character. The parameter cpp references a
  564. * pointer to storage; If the pointer is null, then the necessary
  565. * storage is allocated. The last parameter is the max allowed length
  566. * of the string as specified by a protocol.
  567. */
  568. bool_t
  569. xdr_string (xdrs, cpp, maxsize)
  570. XDR *xdrs;
  571. char **cpp;
  572. u_int maxsize;
  573. {
  574. char *sp = *cpp; /* sp is the actual string pointer */
  575. u_int size;
  576. u_int nodesize;
  577. /*
  578. * first deal with the length since xdr strings are counted-strings
  579. */
  580. switch (xdrs->x_op)
  581. {
  582. case XDR_FREE:
  583. if (sp == NULL)
  584. {
  585. return TRUE; /* already free */
  586. }
  587. /* fall through... */
  588. case XDR_ENCODE:
  589. if (sp == NULL)
  590. return FALSE;
  591. size = __strlen (sp);
  592. break;
  593. case XDR_DECODE:
  594. break;
  595. }
  596. if (!xdr_u_int (xdrs, &size))
  597. {
  598. return FALSE;
  599. }
  600. if (size > maxsize)
  601. {
  602. return FALSE;
  603. }
  604. nodesize = size + 1;
  605. /*
  606. * now deal with the actual bytes
  607. */
  608. switch (xdrs->x_op)
  609. {
  610. case XDR_DECODE:
  611. if (nodesize == 0)
  612. {
  613. return TRUE;
  614. }
  615. if (sp == NULL)
  616. *cpp = sp = (char *) mem_alloc (nodesize);
  617. if (sp == NULL)
  618. {
  619. #ifdef USE_IN_LIBIO
  620. if (_IO_fwide (stderr, 0) > 0)
  621. (void) __fwprintf (stderr, L"%s",
  622. _("xdr_string: out of memory\n"));
  623. else
  624. #endif
  625. (void) fputs (_("xdr_string: out of memory\n"), stderr);
  626. return FALSE;
  627. }
  628. sp[size] = 0;
  629. /* fall into ... */
  630. case XDR_ENCODE:
  631. return xdr_opaque (xdrs, sp, size);
  632. case XDR_FREE:
  633. mem_free (sp, nodesize);
  634. *cpp = NULL;
  635. return TRUE;
  636. }
  637. return FALSE;
  638. }
  639. /*
  640. * Wrapper for xdr_string that can be called directly from
  641. * routines like clnt_call
  642. */
  643. bool_t
  644. xdr_wrapstring (xdrs, cpp)
  645. XDR *xdrs;
  646. char **cpp;
  647. {
  648. if (xdr_string (xdrs, cpp, LASTUNSIGNED))
  649. {
  650. return TRUE;
  651. }
  652. return FALSE;
  653. }