xdr.c 14 KB

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