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. libc_hidden_proto(xdr_long)
  91. bool_t
  92. xdr_long (XDR *xdrs, long *lp)
  93. {
  94. if (xdrs->x_op == XDR_ENCODE
  95. && (sizeof (int32_t) == sizeof (long)
  96. || (int32_t) *lp == *lp))
  97. return XDR_PUTLONG (xdrs, lp);
  98. if (xdrs->x_op == XDR_DECODE)
  99. return XDR_GETLONG (xdrs, lp);
  100. if (xdrs->x_op == XDR_FREE)
  101. return TRUE;
  102. return FALSE;
  103. }
  104. libc_hidden_def(xdr_long)
  105. /*
  106. * XDR short integers
  107. */
  108. libc_hidden_proto(xdr_short)
  109. bool_t
  110. xdr_short (XDR *xdrs, short *sp)
  111. {
  112. long l;
  113. switch (xdrs->x_op)
  114. {
  115. case XDR_ENCODE:
  116. l = (long) *sp;
  117. return XDR_PUTLONG (xdrs, &l);
  118. case XDR_DECODE:
  119. if (!XDR_GETLONG (xdrs, &l))
  120. {
  121. return FALSE;
  122. }
  123. *sp = (short) l;
  124. return TRUE;
  125. case XDR_FREE:
  126. return TRUE;
  127. }
  128. return FALSE;
  129. }
  130. libc_hidden_def(xdr_short)
  131. /*
  132. * XDR integers
  133. */
  134. libc_hidden_proto(xdr_int)
  135. bool_t
  136. xdr_int (XDR *xdrs, int *ip)
  137. {
  138. #if INT_MAX < LONG_MAX
  139. long l;
  140. switch (xdrs->x_op)
  141. {
  142. case XDR_ENCODE:
  143. l = (long) *ip;
  144. return XDR_PUTLONG (xdrs, &l);
  145. case XDR_DECODE:
  146. if (!XDR_GETLONG (xdrs, &l))
  147. {
  148. return FALSE;
  149. }
  150. *ip = (int) l;
  151. case XDR_FREE:
  152. return TRUE;
  153. }
  154. return FALSE;
  155. #elif INT_MAX == LONG_MAX
  156. return xdr_long (xdrs, (long *) ip);
  157. #elif INT_MAX == SHRT_MAX
  158. return xdr_short (xdrs, (short *) ip);
  159. #else
  160. #error unexpected integer sizes in xdr_int()
  161. #endif
  162. }
  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. libc_hidden_proto(xdr_u_long)
  170. bool_t
  171. xdr_u_long (XDR *xdrs, u_long *ulp)
  172. {
  173. switch (xdrs->x_op)
  174. {
  175. case XDR_DECODE:
  176. {
  177. long int tmp;
  178. if (XDR_GETLONG (xdrs, &tmp) == FALSE)
  179. return FALSE;
  180. *ulp = (uint32_t) tmp;
  181. return TRUE;
  182. }
  183. case XDR_ENCODE:
  184. if (sizeof (uint32_t) != sizeof (u_long)
  185. && (uint32_t) *ulp != *ulp)
  186. return FALSE;
  187. return XDR_PUTLONG (xdrs, (long *) ulp);
  188. case XDR_FREE:
  189. return TRUE;
  190. }
  191. return FALSE;
  192. }
  193. libc_hidden_def(xdr_u_long)
  194. /*
  195. * XDR unsigned integers
  196. */
  197. libc_hidden_proto(xdr_u_int)
  198. bool_t
  199. xdr_u_int (XDR *xdrs, u_int *up)
  200. {
  201. #if UINT_MAX < ULONG_MAX
  202. u_long l;
  203. switch (xdrs->x_op)
  204. {
  205. case XDR_ENCODE:
  206. l = (u_long) * up;
  207. return XDR_PUTLONG (xdrs, &l);
  208. case XDR_DECODE:
  209. if (!XDR_GETLONG (xdrs, &l))
  210. {
  211. return FALSE;
  212. }
  213. *up = (u_int) l;
  214. case XDR_FREE:
  215. return TRUE;
  216. }
  217. return FALSE;
  218. #elif UINT_MAX == ULONG_MAX
  219. return xdr_u_long (xdrs, (u_long *) up);
  220. #elif UINT_MAX == USHRT_MAX
  221. return xdr_short (xdrs, (short *) up);
  222. #else
  223. #error unexpected integer sizes in xdr_u_int()
  224. #endif
  225. }
  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. libc_hidden_proto(xdr_hyper)
  232. bool_t
  233. xdr_hyper (XDR *xdrs, quad_t *llp)
  234. {
  235. long t1;
  236. unsigned long int t2;
  237. if (xdrs->x_op == XDR_ENCODE)
  238. {
  239. t1 = (long) ((*llp) >> 32);
  240. t2 = (long) (*llp);
  241. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  242. }
  243. if (xdrs->x_op == XDR_DECODE)
  244. {
  245. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  246. return FALSE;
  247. *llp = ((quad_t) t1) << 32;
  248. *llp |= t2;
  249. return TRUE;
  250. }
  251. if (xdrs->x_op == XDR_FREE)
  252. return TRUE;
  253. return FALSE;
  254. }
  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. libc_hidden_proto(xdr_u_hyper)
  261. bool_t
  262. xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
  263. {
  264. unsigned long t1;
  265. unsigned long t2;
  266. if (xdrs->x_op == XDR_ENCODE)
  267. {
  268. t1 = (unsigned long) ((*ullp) >> 32);
  269. t2 = (unsigned long) (*ullp);
  270. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  271. }
  272. if (xdrs->x_op == XDR_DECODE)
  273. {
  274. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  275. return FALSE;
  276. *ullp = ((u_quad_t) t1) << 32;
  277. *ullp |= t2;
  278. return TRUE;
  279. }
  280. if (xdrs->x_op == XDR_FREE)
  281. return TRUE;
  282. return FALSE;
  283. }
  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. libc_hidden_proto(xdr_bool)
  353. bool_t
  354. xdr_bool (XDR *xdrs, bool_t *bp)
  355. {
  356. long lb;
  357. switch (xdrs->x_op)
  358. {
  359. case XDR_ENCODE:
  360. lb = *bp ? XDR_TRUE : XDR_FALSE;
  361. return XDR_PUTLONG (xdrs, &lb);
  362. case XDR_DECODE:
  363. if (!XDR_GETLONG (xdrs, &lb))
  364. {
  365. return FALSE;
  366. }
  367. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  368. return TRUE;
  369. case XDR_FREE:
  370. return TRUE;
  371. }
  372. return FALSE;
  373. }
  374. libc_hidden_def(xdr_bool)
  375. /*
  376. * XDR enumerations
  377. */
  378. libc_hidden_proto(xdr_enum)
  379. bool_t
  380. xdr_enum (XDR *xdrs, enum_t *ep)
  381. {
  382. enum sizecheck
  383. {
  384. SIZEVAL
  385. }; /* used to find the size of an enum */
  386. /*
  387. * enums are treated as ints
  388. */
  389. if (sizeof (enum sizecheck) == 4)
  390. {
  391. #if INT_MAX < LONG_MAX
  392. long l;
  393. switch (xdrs->x_op)
  394. {
  395. case XDR_ENCODE:
  396. l = *ep;
  397. return XDR_PUTLONG (xdrs, &l);
  398. case XDR_DECODE:
  399. if (!XDR_GETLONG (xdrs, &l))
  400. {
  401. return FALSE;
  402. }
  403. *ep = l;
  404. case XDR_FREE:
  405. return TRUE;
  406. }
  407. return FALSE;
  408. #else
  409. return xdr_long (xdrs, (long *) ep);
  410. #endif
  411. }
  412. else if (sizeof (enum sizecheck) == sizeof (short))
  413. {
  414. return xdr_short (xdrs, (short *) ep);
  415. }
  416. else
  417. {
  418. return FALSE;
  419. }
  420. }
  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. libc_hidden_proto(xdr_opaque)
  428. bool_t
  429. xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
  430. {
  431. u_int rndup;
  432. static char crud[BYTES_PER_XDR_UNIT];
  433. /*
  434. * if no data we are done
  435. */
  436. if (cnt == 0)
  437. return TRUE;
  438. /*
  439. * round byte count to full xdr units
  440. */
  441. rndup = cnt % BYTES_PER_XDR_UNIT;
  442. if (rndup > 0)
  443. rndup = BYTES_PER_XDR_UNIT - rndup;
  444. switch (xdrs->x_op)
  445. {
  446. case XDR_DECODE:
  447. if (!XDR_GETBYTES (xdrs, cp, cnt))
  448. {
  449. return FALSE;
  450. }
  451. if (rndup == 0)
  452. return TRUE;
  453. return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
  454. case XDR_ENCODE:
  455. if (!XDR_PUTBYTES (xdrs, cp, cnt))
  456. {
  457. return FALSE;
  458. }
  459. if (rndup == 0)
  460. return TRUE;
  461. return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
  462. case XDR_FREE:
  463. return TRUE;
  464. }
  465. return FALSE;
  466. }
  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. libc_hidden_proto(xdr_bytes)
  474. bool_t
  475. xdr_bytes (XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
  476. {
  477. char *sp = *cpp; /* sp is the actual string pointer */
  478. u_int nodesize;
  479. /*
  480. * first deal with the length since xdr bytes are counted
  481. */
  482. if (!xdr_u_int (xdrs, sizep))
  483. {
  484. return FALSE;
  485. }
  486. nodesize = *sizep;
  487. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
  488. {
  489. return FALSE;
  490. }
  491. /*
  492. * now deal with the actual bytes
  493. */
  494. switch (xdrs->x_op)
  495. {
  496. case XDR_DECODE:
  497. if (nodesize == 0)
  498. {
  499. return TRUE;
  500. }
  501. if (sp == NULL)
  502. {
  503. *cpp = sp = (char *) mem_alloc (nodesize);
  504. }
  505. if (sp == NULL)
  506. {
  507. #ifdef USE_IN_LIBIO
  508. if (_IO_fwide (stderr, 0) > 0)
  509. (void) fwprintf (stderr, L"%s", _("xdr_bytes: out of memory\n"));
  510. else
  511. #endif
  512. (void) fputs (_("xdr_bytes: out of memory\n"), stderr);
  513. return FALSE;
  514. }
  515. /* fall into ... */
  516. case XDR_ENCODE:
  517. return xdr_opaque (xdrs, sp, nodesize);
  518. case XDR_FREE:
  519. if (sp != NULL)
  520. {
  521. mem_free (sp, nodesize);
  522. *cpp = NULL;
  523. }
  524. return TRUE;
  525. }
  526. return FALSE;
  527. }
  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. libc_hidden_proto(xdr_union)
  551. bool_t
  552. xdr_union (XDR *xdrs, enum_t *dscmp, char *unp, const struct xdr_discrim *choices, xdrproc_t dfault)
  553. {
  554. enum_t dscm;
  555. /*
  556. * we deal with the discriminator; it's an enum
  557. */
  558. if (!xdr_enum (xdrs, dscmp))
  559. {
  560. return FALSE;
  561. }
  562. dscm = *dscmp;
  563. /*
  564. * search choices for a value that matches the discriminator.
  565. * if we find one, execute the xdr routine for that value.
  566. */
  567. for (; choices->proc != NULL_xdrproc_t; choices++)
  568. {
  569. if (choices->value == dscm)
  570. return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
  571. }
  572. /*
  573. * no match - execute the default xdr routine if there is one
  574. */
  575. return ((dfault == NULL_xdrproc_t) ? FALSE :
  576. (*dfault) (xdrs, unp, LASTUNSIGNED));
  577. }
  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. libc_hidden_proto(xdr_string)
  592. bool_t
  593. xdr_string (XDR *xdrs, char **cpp, u_int maxsize)
  594. {
  595. char *sp = *cpp; /* sp is the actual string pointer */
  596. u_int size;
  597. u_int nodesize;
  598. /*
  599. * first deal with the length since xdr strings are counted-strings
  600. */
  601. switch (xdrs->x_op)
  602. {
  603. case XDR_FREE:
  604. if (sp == NULL)
  605. {
  606. return TRUE; /* already free */
  607. }
  608. /* fall through... */
  609. case XDR_ENCODE:
  610. if (sp == NULL)
  611. return FALSE;
  612. size = strlen (sp);
  613. break;
  614. case XDR_DECODE:
  615. break;
  616. }
  617. if (!xdr_u_int (xdrs, &size))
  618. {
  619. return FALSE;
  620. }
  621. if (size > maxsize)
  622. {
  623. return FALSE;
  624. }
  625. nodesize = size + 1;
  626. /*
  627. * now deal with the actual bytes
  628. */
  629. switch (xdrs->x_op)
  630. {
  631. case XDR_DECODE:
  632. if (nodesize == 0)
  633. {
  634. return TRUE;
  635. }
  636. if (sp == NULL)
  637. *cpp = sp = (char *) mem_alloc (nodesize);
  638. if (sp == NULL)
  639. {
  640. #ifdef USE_IN_LIBIO
  641. if (_IO_fwide (stderr, 0) > 0)
  642. (void) fwprintf (stderr, L"%s",
  643. _("xdr_string: out of memory\n"));
  644. else
  645. #endif
  646. (void) fputs (_("xdr_string: out of memory\n"), stderr);
  647. return FALSE;
  648. }
  649. sp[size] = 0;
  650. /* fall into ... */
  651. case XDR_ENCODE:
  652. return xdr_opaque (xdrs, sp, size);
  653. case XDR_FREE:
  654. mem_free (sp, nodesize);
  655. *cpp = NULL;
  656. return TRUE;
  657. }
  658. return FALSE;
  659. }
  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. }