xdr.c 14 KB

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