xdr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 long integers
  84. * The definition of xdr_long() is kept for backward
  85. * compatibility. Instead xdr_int() should be used.
  86. */
  87. bool_t attribute_hidden
  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. strong_alias(__xdr_long,xdr_long)
  101. /*
  102. * XDR short integers
  103. */
  104. bool_t attribute_hidden
  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. strong_alias(__xdr_short,xdr_short)
  126. /*
  127. * XDR integers
  128. */
  129. bool_t attribute_hidden
  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. strong_alias(__xdr_int,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 attribute_hidden
  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. strong_alias(__xdr_u_long,xdr_u_long)
  187. /*
  188. * XDR unsigned integers
  189. */
  190. bool_t attribute_hidden
  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, &l);
  200. case XDR_DECODE:
  201. if (!XDR_GETLONG (xdrs, &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. strong_alias(__xdr_u_int,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 attribute_hidden
  224. __xdr_hyper (XDR *xdrs, quad_t *llp)
  225. {
  226. long t1;
  227. unsigned long int 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, &t2));
  233. }
  234. if (xdrs->x_op == XDR_DECODE)
  235. {
  236. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  237. return FALSE;
  238. *llp = ((quad_t) t1) << 32;
  239. *llp |= t2;
  240. return TRUE;
  241. }
  242. if (xdrs->x_op == XDR_FREE)
  243. return TRUE;
  244. return FALSE;
  245. }
  246. strong_alias(__xdr_hyper,xdr_hyper)
  247. /*
  248. * XDR hyper integers
  249. * same as xdr_hyper - open coded to save a proc call!
  250. */
  251. bool_t attribute_hidden
  252. __xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
  253. {
  254. unsigned long t1;
  255. unsigned long t2;
  256. if (xdrs->x_op == XDR_ENCODE)
  257. {
  258. t1 = (unsigned long) ((*ullp) >> 32);
  259. t2 = (unsigned long) (*ullp);
  260. return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
  261. }
  262. if (xdrs->x_op == XDR_DECODE)
  263. {
  264. if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
  265. return FALSE;
  266. *ullp = ((u_quad_t) t1) << 32;
  267. *ullp |= t2;
  268. return TRUE;
  269. }
  270. if (xdrs->x_op == XDR_FREE)
  271. return TRUE;
  272. return FALSE;
  273. }
  274. strong_alias(__xdr_u_hyper,xdr_u_hyper)
  275. bool_t
  276. xdr_longlong_t (XDR *xdrs, quad_t *llp)
  277. {
  278. return __xdr_hyper (xdrs, llp);
  279. }
  280. bool_t
  281. xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
  282. {
  283. return __xdr_u_hyper (xdrs, ullp);
  284. }
  285. /*
  286. * XDR unsigned short integers
  287. */
  288. bool_t
  289. xdr_u_short (XDR *xdrs, u_short *usp)
  290. {
  291. u_long l;
  292. switch (xdrs->x_op)
  293. {
  294. case XDR_ENCODE:
  295. l = (u_long) * usp;
  296. return XDR_PUTLONG (xdrs, &l);
  297. case XDR_DECODE:
  298. if (!XDR_GETLONG (xdrs, &l))
  299. {
  300. return FALSE;
  301. }
  302. *usp = (u_short) l;
  303. return TRUE;
  304. case XDR_FREE:
  305. return TRUE;
  306. }
  307. return FALSE;
  308. }
  309. /*
  310. * XDR a char
  311. */
  312. bool_t
  313. xdr_char (XDR *xdrs, char *cp)
  314. {
  315. int i;
  316. i = (*cp);
  317. if (!__xdr_int (xdrs, &i))
  318. {
  319. return FALSE;
  320. }
  321. *cp = i;
  322. return TRUE;
  323. }
  324. /*
  325. * XDR an unsigned char
  326. */
  327. bool_t
  328. xdr_u_char (XDR *xdrs, u_char *cp)
  329. {
  330. u_int u;
  331. u = (*cp);
  332. if (!__xdr_u_int (xdrs, &u))
  333. {
  334. return FALSE;
  335. }
  336. *cp = u;
  337. return TRUE;
  338. }
  339. /*
  340. * XDR booleans
  341. */
  342. bool_t attribute_hidden
  343. __xdr_bool (XDR *xdrs, bool_t *bp)
  344. {
  345. long lb;
  346. switch (xdrs->x_op)
  347. {
  348. case XDR_ENCODE:
  349. lb = *bp ? XDR_TRUE : XDR_FALSE;
  350. return XDR_PUTLONG (xdrs, &lb);
  351. case XDR_DECODE:
  352. if (!XDR_GETLONG (xdrs, &lb))
  353. {
  354. return FALSE;
  355. }
  356. *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  357. return TRUE;
  358. case XDR_FREE:
  359. return TRUE;
  360. }
  361. return FALSE;
  362. }
  363. strong_alias(__xdr_bool,xdr_bool)
  364. /*
  365. * XDR enumerations
  366. */
  367. bool_t attribute_hidden
  368. __xdr_enum (XDR *xdrs, enum_t *ep)
  369. {
  370. enum sizecheck
  371. {
  372. SIZEVAL
  373. }; /* used to find the size of an enum */
  374. /*
  375. * enums are treated as ints
  376. */
  377. if (sizeof (enum sizecheck) == 4)
  378. {
  379. #if INT_MAX < LONG_MAX
  380. long l;
  381. switch (xdrs->x_op)
  382. {
  383. case XDR_ENCODE:
  384. l = *ep;
  385. return XDR_PUTLONG (xdrs, &l);
  386. case XDR_DECODE:
  387. if (!XDR_GETLONG (xdrs, &l))
  388. {
  389. return FALSE;
  390. }
  391. *ep = l;
  392. case XDR_FREE:
  393. return TRUE;
  394. }
  395. return FALSE;
  396. #else
  397. return __xdr_long (xdrs, (long *) ep);
  398. #endif
  399. }
  400. else if (sizeof (enum sizecheck) == sizeof (short))
  401. {
  402. return __xdr_short (xdrs, (short *) ep);
  403. }
  404. else
  405. {
  406. return FALSE;
  407. }
  408. }
  409. strong_alias(__xdr_enum,xdr_enum)
  410. /*
  411. * XDR opaque data
  412. * Allows the specification of a fixed size sequence of opaque bytes.
  413. * cp points to the opaque object and cnt gives the byte length.
  414. */
  415. bool_t attribute_hidden
  416. __xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
  417. {
  418. u_int rndup;
  419. static char crud[BYTES_PER_XDR_UNIT];
  420. /*
  421. * if no data we are done
  422. */
  423. if (cnt == 0)
  424. return TRUE;
  425. /*
  426. * round byte count to full xdr units
  427. */
  428. rndup = cnt % BYTES_PER_XDR_UNIT;
  429. if (rndup > 0)
  430. rndup = BYTES_PER_XDR_UNIT - rndup;
  431. switch (xdrs->x_op)
  432. {
  433. case XDR_DECODE:
  434. if (!XDR_GETBYTES (xdrs, cp, cnt))
  435. {
  436. return FALSE;
  437. }
  438. if (rndup == 0)
  439. return TRUE;
  440. return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
  441. case XDR_ENCODE:
  442. if (!XDR_PUTBYTES (xdrs, cp, cnt))
  443. {
  444. return FALSE;
  445. }
  446. if (rndup == 0)
  447. return TRUE;
  448. return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
  449. case XDR_FREE:
  450. return TRUE;
  451. }
  452. return FALSE;
  453. }
  454. strong_alias(__xdr_opaque,xdr_opaque)
  455. /*
  456. * XDR counted bytes
  457. * *cpp is a pointer to the bytes, *sizep is the count.
  458. * If *cpp is NULL maxsize bytes are allocated
  459. */
  460. bool_t attribute_hidden
  461. __xdr_bytes (XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
  462. {
  463. char *sp = *cpp; /* sp is the actual string pointer */
  464. u_int nodesize;
  465. /*
  466. * first deal with the length since xdr bytes are counted
  467. */
  468. if (!__xdr_u_int (xdrs, sizep))
  469. {
  470. return FALSE;
  471. }
  472. nodesize = *sizep;
  473. if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
  474. {
  475. return FALSE;
  476. }
  477. /*
  478. * now deal with the actual bytes
  479. */
  480. switch (xdrs->x_op)
  481. {
  482. case XDR_DECODE:
  483. if (nodesize == 0)
  484. {
  485. return TRUE;
  486. }
  487. if (sp == NULL)
  488. {
  489. *cpp = sp = (char *) mem_alloc (nodesize);
  490. }
  491. if (sp == NULL)
  492. {
  493. #ifdef USE_IN_LIBIO
  494. if (_IO_fwide (stderr, 0) > 0)
  495. (void) __fwprintf (stderr, L"%s", _("xdr_bytes: out of memory\n"));
  496. else
  497. #endif
  498. (void) fputs (_("xdr_bytes: out of memory\n"), stderr);
  499. return FALSE;
  500. }
  501. /* fall into ... */
  502. case XDR_ENCODE:
  503. return __xdr_opaque (xdrs, sp, nodesize);
  504. case XDR_FREE:
  505. if (sp != NULL)
  506. {
  507. mem_free (sp, nodesize);
  508. *cpp = NULL;
  509. }
  510. return TRUE;
  511. }
  512. return FALSE;
  513. }
  514. strong_alias(__xdr_bytes,xdr_bytes)
  515. /*
  516. * Implemented here due to commonality of the object.
  517. */
  518. bool_t
  519. xdr_netobj (xdrs, np)
  520. XDR *xdrs;
  521. 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 attribute_hidden
  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. strong_alias(__xdr_union,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 attribute_hidden
  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. strong_alias(__xdr_string,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 (xdrs, cpp)
  651. XDR *xdrs;
  652. char **cpp;
  653. {
  654. if (__xdr_string (xdrs, cpp, LASTUNSIGNED))
  655. {
  656. return TRUE;
  657. }
  658. return FALSE;
  659. }