xdr_rec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3. * unrestricted use provided that this legend is included on all tape
  4. * media and as a part of the software program in whole or part. Users
  5. * may copy or modify Sun RPC without charge, but are not authorized
  6. * to license or distribute it to anyone else except as part of a product or
  7. * program developed by the user.
  8. *
  9. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12. *
  13. * Sun RPC is provided with no support and without any obligation on the
  14. * part of Sun Microsystems, Inc. to assist in its use, correction,
  15. * modification or enhancement.
  16. *
  17. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19. * OR ANY PART THEREOF.
  20. *
  21. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22. * or profits or other special, indirect and consequential damages, even if
  23. * Sun has been advised of the possibility of such damages.
  24. *
  25. * Sun Microsystems, Inc.
  26. * 2550 Garcia Avenue
  27. * Mountain View, California 94043
  28. */
  29. /*
  30. * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
  31. * layer above tcp (for rpc's use).
  32. *
  33. * Copyright (C) 1984, Sun Microsystems, Inc.
  34. *
  35. * These routines interface XDRSTREAMS to a tcp/ip connection.
  36. * There is a record marking layer between the xdr stream
  37. * and the tcp transport level. A record is composed on one or more
  38. * record fragments. A record fragment is a thirty-two bit header followed
  39. * by n bytes of data, where n is contained in the header. The header
  40. * is represented as a htonl(u_long). The high order bit encodes
  41. * whether or not the fragment is the last fragment of the record
  42. * (1 => fragment is last, 0 => more fragments to follow.
  43. * The other 31 bits encode the byte length of the fragment.
  44. */
  45. #define __FORCE_GLIBC
  46. #define _GNU_SOURCE
  47. #include <features.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <unistd.h>
  51. #include <rpc/rpc.h>
  52. #ifdef USE_IN_LIBIO
  53. # include <wchar.h>
  54. # include <libio/iolibio.h>
  55. # define fputs(s, f) _IO_fputs (s, f)
  56. #endif
  57. static bool_t xdrrec_getlong (XDR *, long *);
  58. static bool_t xdrrec_putlong (XDR *, const long *);
  59. static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
  60. static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
  61. static u_int xdrrec_getpos (const XDR *);
  62. static bool_t xdrrec_setpos (XDR *, u_int);
  63. static int32_t *xdrrec_inline (XDR *, int);
  64. static void xdrrec_destroy (XDR *);
  65. static bool_t xdrrec_getint32 (XDR *, int32_t *);
  66. static bool_t xdrrec_putint32 (XDR *, const int32_t *);
  67. static const struct xdr_ops xdrrec_ops = {
  68. xdrrec_getlong,
  69. xdrrec_putlong,
  70. xdrrec_getbytes,
  71. xdrrec_putbytes,
  72. xdrrec_getpos,
  73. xdrrec_setpos,
  74. xdrrec_inline,
  75. xdrrec_destroy,
  76. xdrrec_getint32,
  77. xdrrec_putint32
  78. };
  79. /*
  80. * A record is composed of one or more record fragments.
  81. * A record fragment is a two-byte header followed by zero to
  82. * 2**32-1 bytes. The header is treated as a long unsigned and is
  83. * encode/decoded to the network via htonl/ntohl. The low order 31 bits
  84. * are a byte count of the fragment. The highest order bit is a boolean:
  85. * 1 => this fragment is the last fragment of the record,
  86. * 0 => this fragment is followed by more fragment(s).
  87. *
  88. * The fragment/record machinery is not general; it is constructed to
  89. * meet the needs of xdr and rpc based on tcp.
  90. */
  91. #define LAST_FRAG (1UL << 31)
  92. typedef struct rec_strm
  93. {
  94. caddr_t tcp_handle;
  95. caddr_t the_buffer;
  96. /*
  97. * out-going bits
  98. */
  99. int (*writeit) (char *, char *, int);
  100. caddr_t out_base; /* output buffer (points to frag header) */
  101. caddr_t out_finger; /* next output position */
  102. caddr_t out_boundry; /* data cannot up to this address */
  103. u_int32_t *frag_header; /* beginning of curren fragment */
  104. bool_t frag_sent; /* true if buffer sent in middle of record */
  105. /*
  106. * in-coming bits
  107. */
  108. int (*readit) (char *, char *, int);
  109. u_long in_size; /* fixed size of the input buffer */
  110. caddr_t in_base;
  111. caddr_t in_finger; /* location of next byte to be had */
  112. caddr_t in_boundry; /* can read up to this location */
  113. long fbtbc; /* fragment bytes to be consumed */
  114. bool_t last_frag;
  115. u_int sendsize;
  116. u_int recvsize;
  117. }
  118. RECSTREAM;
  119. static u_int fix_buf_size (u_int) internal_function;
  120. static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
  121. static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
  122. static bool_t set_input_fragment (RECSTREAM *) internal_function;
  123. static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
  124. /*
  125. * Create an xdr handle for xdrrec
  126. * xdrrec_create fills in xdrs. Sendsize and recvsize are
  127. * send and recv buffer sizes (0 => use default).
  128. * tcp_handle is an opaque handle that is passed as the first parameter to
  129. * the procedures readit and writeit. Readit and writeit are read and
  130. * write respectively. They are like the system
  131. * calls expect that they take an opaque handle rather than an fd.
  132. */
  133. void
  134. xdrrec_create (XDR *xdrs, u_int sendsize,
  135. u_int recvsize, caddr_t tcp_handle,
  136. int (*readit) (char *, char *, int),
  137. int (*writeit) (char *, char *, int))
  138. {
  139. RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
  140. caddr_t tmp;
  141. char *buf;
  142. sendsize = fix_buf_size (sendsize);
  143. recvsize = fix_buf_size (recvsize);
  144. buf = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
  145. if (rstrm == NULL || buf == NULL)
  146. {
  147. #ifdef USE_IN_LIBIO
  148. if (_IO_fwide (stderr, 0) > 0)
  149. (void) __fwprintf (stderr, L"%s", _("xdrrec_create: out of memory\n"));
  150. else
  151. #endif
  152. (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
  153. mem_free (rstrm, sizeof (RECSTREAM));
  154. mem_free (buf, sendsize + recvsize + BYTES_PER_XDR_UNIT);
  155. /*
  156. * This is bad. Should rework xdrrec_create to
  157. * return a handle, and in this case return NULL
  158. */
  159. return;
  160. }
  161. /*
  162. * adjust sizes and allocate buffer quad byte aligned
  163. */
  164. rstrm->sendsize = sendsize;
  165. rstrm->recvsize = recvsize;
  166. rstrm->the_buffer = buf;
  167. tmp = rstrm->the_buffer;
  168. if ((size_t)tmp % BYTES_PER_XDR_UNIT)
  169. tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
  170. rstrm->out_base = tmp;
  171. rstrm->in_base = tmp + sendsize;
  172. /*
  173. * now the rest ...
  174. */
  175. /* We have to add the const since the `struct xdr_ops' in `struct XDR'
  176. is not `const'. */
  177. xdrs->x_ops = (struct xdr_ops *) &xdrrec_ops;
  178. xdrs->x_private = (caddr_t) rstrm;
  179. rstrm->tcp_handle = tcp_handle;
  180. rstrm->readit = readit;
  181. rstrm->writeit = writeit;
  182. rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
  183. rstrm->frag_header = (u_int32_t *) rstrm->out_base;
  184. rstrm->out_finger += 4;
  185. rstrm->out_boundry += sendsize;
  186. rstrm->frag_sent = FALSE;
  187. rstrm->in_size = recvsize;
  188. rstrm->in_boundry = rstrm->in_base;
  189. rstrm->in_finger = (rstrm->in_boundry += recvsize);
  190. rstrm->fbtbc = 0;
  191. rstrm->last_frag = TRUE;
  192. }
  193. /*
  194. * The routines defined below are the xdr ops which will go into the
  195. * xdr handle filled in by xdrrec_create.
  196. */
  197. static bool_t
  198. xdrrec_getlong (XDR *xdrs, long *lp)
  199. {
  200. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  201. int32_t *buflp = (int32_t *) rstrm->in_finger;
  202. int32_t mylong;
  203. /* first try the inline, fast case */
  204. if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
  205. rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
  206. {
  207. *lp = (int32_t) ntohl (*buflp);
  208. rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
  209. rstrm->in_finger += BYTES_PER_XDR_UNIT;
  210. }
  211. else
  212. {
  213. if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
  214. BYTES_PER_XDR_UNIT))
  215. return FALSE;
  216. *lp = (int32_t) ntohl (mylong);
  217. }
  218. return TRUE;
  219. }
  220. static bool_t
  221. xdrrec_putlong (XDR *xdrs, const long *lp)
  222. {
  223. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  224. int32_t *dest_lp = (int32_t *) rstrm->out_finger;
  225. if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
  226. {
  227. /*
  228. * this case should almost never happen so the code is
  229. * inefficient
  230. */
  231. rstrm->out_finger -= BYTES_PER_XDR_UNIT;
  232. rstrm->frag_sent = TRUE;
  233. if (!flush_out (rstrm, FALSE))
  234. return FALSE;
  235. dest_lp = (int32_t *) rstrm->out_finger;
  236. rstrm->out_finger += BYTES_PER_XDR_UNIT;
  237. }
  238. *dest_lp = htonl (*lp);
  239. return TRUE;
  240. }
  241. static bool_t /* must manage buffers, fragments, and records */
  242. xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
  243. {
  244. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  245. u_int current;
  246. while (len > 0)
  247. {
  248. current = rstrm->fbtbc;
  249. if (current == 0)
  250. {
  251. if (rstrm->last_frag)
  252. return FALSE;
  253. if (!set_input_fragment (rstrm))
  254. return FALSE;
  255. continue;
  256. }
  257. current = (len < current) ? len : current;
  258. if (!get_input_bytes (rstrm, addr, current))
  259. return FALSE;
  260. addr += current;
  261. rstrm->fbtbc -= current;
  262. len -= current;
  263. }
  264. return TRUE;
  265. }
  266. static bool_t
  267. xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
  268. {
  269. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  270. u_int current;
  271. while (len > 0)
  272. {
  273. current = rstrm->out_boundry - rstrm->out_finger;
  274. current = (len < current) ? len : current;
  275. memcpy (rstrm->out_finger, addr, current);
  276. rstrm->out_finger += current;
  277. addr += current;
  278. len -= current;
  279. if (rstrm->out_finger == rstrm->out_boundry && len > 0)
  280. {
  281. rstrm->frag_sent = TRUE;
  282. if (!flush_out (rstrm, FALSE))
  283. return FALSE;
  284. }
  285. }
  286. return TRUE;
  287. }
  288. static u_int
  289. xdrrec_getpos (const XDR *xdrs)
  290. {
  291. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  292. long pos;
  293. pos = lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
  294. if (pos != -1)
  295. switch (xdrs->x_op)
  296. {
  297. case XDR_ENCODE:
  298. pos += rstrm->out_finger - rstrm->out_base;
  299. break;
  300. case XDR_DECODE:
  301. pos -= rstrm->in_boundry - rstrm->in_finger;
  302. break;
  303. default:
  304. pos = (u_int) - 1;
  305. break;
  306. }
  307. return (u_int) pos;
  308. }
  309. static bool_t
  310. xdrrec_setpos (XDR *xdrs, u_int pos)
  311. {
  312. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  313. u_int currpos = xdrrec_getpos (xdrs);
  314. int delta = currpos - pos;
  315. caddr_t newpos;
  316. if ((int) currpos != -1)
  317. switch (xdrs->x_op)
  318. {
  319. case XDR_ENCODE:
  320. newpos = rstrm->out_finger - delta;
  321. if (newpos > (caddr_t) rstrm->frag_header &&
  322. newpos < rstrm->out_boundry)
  323. {
  324. rstrm->out_finger = newpos;
  325. return TRUE;
  326. }
  327. break;
  328. case XDR_DECODE:
  329. newpos = rstrm->in_finger - delta;
  330. if ((delta < (int) (rstrm->fbtbc)) &&
  331. (newpos <= rstrm->in_boundry) &&
  332. (newpos >= rstrm->in_base))
  333. {
  334. rstrm->in_finger = newpos;
  335. rstrm->fbtbc -= delta;
  336. return TRUE;
  337. }
  338. break;
  339. default:
  340. break;
  341. }
  342. return FALSE;
  343. }
  344. static int32_t *
  345. xdrrec_inline (XDR *xdrs, int len)
  346. {
  347. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  348. int32_t *buf = NULL;
  349. switch (xdrs->x_op)
  350. {
  351. case XDR_ENCODE:
  352. if ((rstrm->out_finger + len) <= rstrm->out_boundry)
  353. {
  354. buf = (int32_t *) rstrm->out_finger;
  355. rstrm->out_finger += len;
  356. }
  357. break;
  358. case XDR_DECODE:
  359. if ((len <= rstrm->fbtbc) &&
  360. ((rstrm->in_finger + len) <= rstrm->in_boundry))
  361. {
  362. buf = (int32_t *) rstrm->in_finger;
  363. rstrm->fbtbc -= len;
  364. rstrm->in_finger += len;
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. return buf;
  371. }
  372. static void
  373. xdrrec_destroy (XDR *xdrs)
  374. {
  375. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  376. mem_free (rstrm->the_buffer,
  377. rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
  378. mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
  379. }
  380. static bool_t
  381. xdrrec_getint32 (XDR *xdrs, int32_t *ip)
  382. {
  383. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  384. int32_t *bufip = (int32_t *) rstrm->in_finger;
  385. int32_t mylong;
  386. /* first try the inline, fast case */
  387. if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
  388. rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
  389. {
  390. *ip = ntohl (*bufip);
  391. rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
  392. rstrm->in_finger += BYTES_PER_XDR_UNIT;
  393. }
  394. else
  395. {
  396. if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
  397. BYTES_PER_XDR_UNIT))
  398. return FALSE;
  399. *ip = ntohl (mylong);
  400. }
  401. return TRUE;
  402. }
  403. static bool_t
  404. xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
  405. {
  406. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  407. int32_t *dest_ip = (int32_t *) rstrm->out_finger;
  408. if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
  409. {
  410. /*
  411. * this case should almost never happen so the code is
  412. * inefficient
  413. */
  414. rstrm->out_finger -= BYTES_PER_XDR_UNIT;
  415. rstrm->frag_sent = TRUE;
  416. if (!flush_out (rstrm, FALSE))
  417. return FALSE;
  418. dest_ip = (int32_t *) rstrm->out_finger;
  419. rstrm->out_finger += BYTES_PER_XDR_UNIT;
  420. }
  421. *dest_ip = htonl (*ip);
  422. return TRUE;
  423. }
  424. /*
  425. * Exported routines to manage xdr records
  426. */
  427. /*
  428. * Before reading (deserializing from the stream, one should always call
  429. * this procedure to guarantee proper record alignment.
  430. */
  431. bool_t
  432. xdrrec_skiprecord (XDR *xdrs)
  433. {
  434. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  435. while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
  436. {
  437. if (!skip_input_bytes (rstrm, rstrm->fbtbc))
  438. return FALSE;
  439. rstrm->fbtbc = 0;
  440. if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
  441. return FALSE;
  442. }
  443. rstrm->last_frag = FALSE;
  444. return TRUE;
  445. }
  446. /*
  447. * Lookahead function.
  448. * Returns TRUE iff there is no more input in the buffer
  449. * after consuming the rest of the current record.
  450. */
  451. bool_t
  452. xdrrec_eof (XDR *xdrs)
  453. {
  454. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  455. while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
  456. {
  457. if (!skip_input_bytes (rstrm, rstrm->fbtbc))
  458. return TRUE;
  459. rstrm->fbtbc = 0;
  460. if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
  461. return TRUE;
  462. }
  463. if (rstrm->in_finger == rstrm->in_boundry)
  464. return TRUE;
  465. return FALSE;
  466. }
  467. /*
  468. * The client must tell the package when an end-of-record has occurred.
  469. * The second parameter tells whether the record should be flushed to the
  470. * (output) tcp stream. (This lets the package support batched or
  471. * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
  472. */
  473. bool_t
  474. xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
  475. {
  476. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  477. u_long len; /* fragment length */
  478. if (sendnow || rstrm->frag_sent
  479. || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
  480. {
  481. rstrm->frag_sent = FALSE;
  482. return flush_out (rstrm, TRUE);
  483. }
  484. len = (rstrm->out_finger - (char *) rstrm->frag_header
  485. - BYTES_PER_XDR_UNIT);
  486. *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
  487. rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
  488. rstrm->out_finger += BYTES_PER_XDR_UNIT;
  489. return TRUE;
  490. }
  491. /*
  492. * Internal useful routines
  493. */
  494. static bool_t
  495. internal_function
  496. flush_out (RECSTREAM *rstrm, bool_t eor)
  497. {
  498. u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
  499. u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
  500. - BYTES_PER_XDR_UNIT);
  501. *rstrm->frag_header = htonl (len | eormask);
  502. len = rstrm->out_finger - rstrm->out_base;
  503. if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
  504. != (int) len)
  505. return FALSE;
  506. rstrm->frag_header = (u_int32_t *) rstrm->out_base;
  507. rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
  508. return TRUE;
  509. }
  510. static bool_t /* knows nothing about records! Only about input buffers */
  511. fill_input_buf (RECSTREAM *rstrm)
  512. {
  513. caddr_t where;
  514. size_t i;
  515. int len;
  516. where = rstrm->in_base;
  517. i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
  518. where += i;
  519. len = rstrm->in_size - i;
  520. if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
  521. return FALSE;
  522. rstrm->in_finger = where;
  523. where += len;
  524. rstrm->in_boundry = where;
  525. return TRUE;
  526. }
  527. static bool_t /* knows nothing about records! Only about input buffers */
  528. internal_function
  529. get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
  530. {
  531. int current;
  532. while (len > 0)
  533. {
  534. current = rstrm->in_boundry - rstrm->in_finger;
  535. if (current == 0)
  536. {
  537. if (!fill_input_buf (rstrm))
  538. return FALSE;
  539. continue;
  540. }
  541. current = (len < current) ? len : current;
  542. memcpy (addr, rstrm->in_finger, current);
  543. rstrm->in_finger += current;
  544. addr += current;
  545. len -= current;
  546. }
  547. return TRUE;
  548. }
  549. static bool_t /* next two bytes of the input stream are treated as a header */
  550. internal_function
  551. set_input_fragment (RECSTREAM *rstrm)
  552. {
  553. uint32_t header;
  554. if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
  555. return FALSE;
  556. header = ntohl (header);
  557. rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
  558. /*
  559. * Sanity check. Try not to accept wildly incorrect fragment
  560. * sizes. Unfortunately, only a size of zero can be identified as
  561. * 'wildely incorrect', and this only, if it is not the last
  562. * fragment of a message. Ridiculously large fragment sizes may look
  563. * wrong, but we don't have any way to be certain that they aren't
  564. * what the client actually intended to send us. Many existing RPC
  565. * implementations may sent a fragment of size zero as the last
  566. * fragment of a message.
  567. */
  568. if (header == 0)
  569. return FALSE;
  570. rstrm->fbtbc = header & ~LAST_FRAG;
  571. return TRUE;
  572. }
  573. static bool_t /* consumes input bytes; knows nothing about records! */
  574. internal_function
  575. skip_input_bytes (RECSTREAM *rstrm, long cnt)
  576. {
  577. int current;
  578. while (cnt > 0)
  579. {
  580. current = rstrm->in_boundry - rstrm->in_finger;
  581. if (current == 0)
  582. {
  583. if (!fill_input_buf (rstrm))
  584. return FALSE;
  585. continue;
  586. }
  587. current = (cnt < current) ? cnt : current;
  588. rstrm->in_finger += current;
  589. cnt -= current;
  590. }
  591. return TRUE;
  592. }
  593. static u_int
  594. internal_function
  595. fix_buf_size (u_int s)
  596. {
  597. if (s < 100)
  598. s = 4000;
  599. return RNDUP (s);
  600. }