xdr_rec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 attribute_hidden
  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. strong_alias(__xdrrec_create,xdrrec_create)
  194. /*
  195. * The routines defined below are the xdr ops which will go into the
  196. * xdr handle filled in by xdrrec_create.
  197. */
  198. static bool_t
  199. xdrrec_getlong (XDR *xdrs, long *lp)
  200. {
  201. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  202. int32_t *buflp = (int32_t *) rstrm->in_finger;
  203. int32_t mylong;
  204. /* first try the inline, fast case */
  205. if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
  206. rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
  207. {
  208. *lp = (int32_t) ntohl (*buflp);
  209. rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
  210. rstrm->in_finger += BYTES_PER_XDR_UNIT;
  211. }
  212. else
  213. {
  214. if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
  215. BYTES_PER_XDR_UNIT))
  216. return FALSE;
  217. *lp = (int32_t) ntohl (mylong);
  218. }
  219. return TRUE;
  220. }
  221. static bool_t
  222. xdrrec_putlong (XDR *xdrs, const long *lp)
  223. {
  224. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  225. int32_t *dest_lp = (int32_t *) rstrm->out_finger;
  226. if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
  227. {
  228. /*
  229. * this case should almost never happen so the code is
  230. * inefficient
  231. */
  232. rstrm->out_finger -= BYTES_PER_XDR_UNIT;
  233. rstrm->frag_sent = TRUE;
  234. if (!flush_out (rstrm, FALSE))
  235. return FALSE;
  236. dest_lp = (int32_t *) rstrm->out_finger;
  237. rstrm->out_finger += BYTES_PER_XDR_UNIT;
  238. }
  239. *dest_lp = htonl (*lp);
  240. return TRUE;
  241. }
  242. static bool_t /* must manage buffers, fragments, and records */
  243. xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
  244. {
  245. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  246. u_int current;
  247. while (len > 0)
  248. {
  249. current = rstrm->fbtbc;
  250. if (current == 0)
  251. {
  252. if (rstrm->last_frag)
  253. return FALSE;
  254. if (!set_input_fragment (rstrm))
  255. return FALSE;
  256. continue;
  257. }
  258. current = (len < current) ? len : current;
  259. if (!get_input_bytes (rstrm, addr, current))
  260. return FALSE;
  261. addr += current;
  262. rstrm->fbtbc -= current;
  263. len -= current;
  264. }
  265. return TRUE;
  266. }
  267. static bool_t
  268. xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
  269. {
  270. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  271. u_int current;
  272. while (len > 0)
  273. {
  274. current = rstrm->out_boundry - rstrm->out_finger;
  275. current = (len < current) ? len : current;
  276. memcpy (rstrm->out_finger, addr, current);
  277. rstrm->out_finger += current;
  278. addr += current;
  279. len -= current;
  280. if (rstrm->out_finger == rstrm->out_boundry && len > 0)
  281. {
  282. rstrm->frag_sent = TRUE;
  283. if (!flush_out (rstrm, FALSE))
  284. return FALSE;
  285. }
  286. }
  287. return TRUE;
  288. }
  289. static u_int
  290. xdrrec_getpos (const XDR *xdrs)
  291. {
  292. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  293. long pos;
  294. pos = lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
  295. if (pos != -1)
  296. switch (xdrs->x_op)
  297. {
  298. case XDR_ENCODE:
  299. pos += rstrm->out_finger - rstrm->out_base;
  300. break;
  301. case XDR_DECODE:
  302. pos -= rstrm->in_boundry - rstrm->in_finger;
  303. break;
  304. default:
  305. pos = (u_int) - 1;
  306. break;
  307. }
  308. return (u_int) pos;
  309. }
  310. static bool_t
  311. xdrrec_setpos (XDR *xdrs, u_int pos)
  312. {
  313. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  314. u_int currpos = xdrrec_getpos (xdrs);
  315. int delta = currpos - pos;
  316. caddr_t newpos;
  317. if ((int) currpos != -1)
  318. switch (xdrs->x_op)
  319. {
  320. case XDR_ENCODE:
  321. newpos = rstrm->out_finger - delta;
  322. if (newpos > (caddr_t) rstrm->frag_header &&
  323. newpos < rstrm->out_boundry)
  324. {
  325. rstrm->out_finger = newpos;
  326. return TRUE;
  327. }
  328. break;
  329. case XDR_DECODE:
  330. newpos = rstrm->in_finger - delta;
  331. if ((delta < (int) (rstrm->fbtbc)) &&
  332. (newpos <= rstrm->in_boundry) &&
  333. (newpos >= rstrm->in_base))
  334. {
  335. rstrm->in_finger = newpos;
  336. rstrm->fbtbc -= delta;
  337. return TRUE;
  338. }
  339. break;
  340. default:
  341. break;
  342. }
  343. return FALSE;
  344. }
  345. static int32_t *
  346. xdrrec_inline (XDR *xdrs, int len)
  347. {
  348. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  349. int32_t *buf = NULL;
  350. switch (xdrs->x_op)
  351. {
  352. case XDR_ENCODE:
  353. if ((rstrm->out_finger + len) <= rstrm->out_boundry)
  354. {
  355. buf = (int32_t *) rstrm->out_finger;
  356. rstrm->out_finger += len;
  357. }
  358. break;
  359. case XDR_DECODE:
  360. if ((len <= rstrm->fbtbc) &&
  361. ((rstrm->in_finger + len) <= rstrm->in_boundry))
  362. {
  363. buf = (int32_t *) rstrm->in_finger;
  364. rstrm->fbtbc -= len;
  365. rstrm->in_finger += len;
  366. }
  367. break;
  368. default:
  369. break;
  370. }
  371. return buf;
  372. }
  373. static void
  374. xdrrec_destroy (XDR *xdrs)
  375. {
  376. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  377. mem_free (rstrm->the_buffer,
  378. rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
  379. mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
  380. }
  381. static bool_t
  382. xdrrec_getint32 (XDR *xdrs, int32_t *ip)
  383. {
  384. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  385. int32_t *bufip = (int32_t *) rstrm->in_finger;
  386. int32_t mylong;
  387. /* first try the inline, fast case */
  388. if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
  389. rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
  390. {
  391. *ip = ntohl (*bufip);
  392. rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
  393. rstrm->in_finger += BYTES_PER_XDR_UNIT;
  394. }
  395. else
  396. {
  397. if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
  398. BYTES_PER_XDR_UNIT))
  399. return FALSE;
  400. *ip = ntohl (mylong);
  401. }
  402. return TRUE;
  403. }
  404. static bool_t
  405. xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
  406. {
  407. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  408. int32_t *dest_ip = (int32_t *) rstrm->out_finger;
  409. if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
  410. {
  411. /*
  412. * this case should almost never happen so the code is
  413. * inefficient
  414. */
  415. rstrm->out_finger -= BYTES_PER_XDR_UNIT;
  416. rstrm->frag_sent = TRUE;
  417. if (!flush_out (rstrm, FALSE))
  418. return FALSE;
  419. dest_ip = (int32_t *) rstrm->out_finger;
  420. rstrm->out_finger += BYTES_PER_XDR_UNIT;
  421. }
  422. *dest_ip = htonl (*ip);
  423. return TRUE;
  424. }
  425. /*
  426. * Exported routines to manage xdr records
  427. */
  428. /*
  429. * Before reading (deserializing from the stream, one should always call
  430. * this procedure to guarantee proper record alignment.
  431. */
  432. bool_t
  433. xdrrec_skiprecord (XDR *xdrs)
  434. {
  435. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  436. while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
  437. {
  438. if (!skip_input_bytes (rstrm, rstrm->fbtbc))
  439. return FALSE;
  440. rstrm->fbtbc = 0;
  441. if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
  442. return FALSE;
  443. }
  444. rstrm->last_frag = FALSE;
  445. return TRUE;
  446. }
  447. /*
  448. * Lookahead function.
  449. * Returns TRUE iff there is no more input in the buffer
  450. * after consuming the rest of the current record.
  451. */
  452. bool_t
  453. xdrrec_eof (XDR *xdrs)
  454. {
  455. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  456. while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
  457. {
  458. if (!skip_input_bytes (rstrm, rstrm->fbtbc))
  459. return TRUE;
  460. rstrm->fbtbc = 0;
  461. if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
  462. return TRUE;
  463. }
  464. if (rstrm->in_finger == rstrm->in_boundry)
  465. return TRUE;
  466. return FALSE;
  467. }
  468. /*
  469. * The client must tell the package when an end-of-record has occurred.
  470. * The second parameter tells whether the record should be flushed to the
  471. * (output) tcp stream. (This lets the package support batched or
  472. * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
  473. */
  474. bool_t
  475. xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
  476. {
  477. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  478. u_long len; /* fragment length */
  479. if (sendnow || rstrm->frag_sent
  480. || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
  481. {
  482. rstrm->frag_sent = FALSE;
  483. return flush_out (rstrm, TRUE);
  484. }
  485. len = (rstrm->out_finger - (char *) rstrm->frag_header
  486. - BYTES_PER_XDR_UNIT);
  487. *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
  488. rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
  489. rstrm->out_finger += BYTES_PER_XDR_UNIT;
  490. return TRUE;
  491. }
  492. /*
  493. * Internal useful routines
  494. */
  495. static bool_t
  496. internal_function
  497. flush_out (RECSTREAM *rstrm, bool_t eor)
  498. {
  499. u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
  500. u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
  501. - BYTES_PER_XDR_UNIT);
  502. *rstrm->frag_header = htonl (len | eormask);
  503. len = rstrm->out_finger - rstrm->out_base;
  504. if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
  505. != (int) len)
  506. return FALSE;
  507. rstrm->frag_header = (u_int32_t *) rstrm->out_base;
  508. rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
  509. return TRUE;
  510. }
  511. static bool_t /* knows nothing about records! Only about input buffers */
  512. fill_input_buf (RECSTREAM *rstrm)
  513. {
  514. caddr_t where;
  515. size_t i;
  516. int len;
  517. where = rstrm->in_base;
  518. i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
  519. where += i;
  520. len = rstrm->in_size - i;
  521. if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
  522. return FALSE;
  523. rstrm->in_finger = where;
  524. where += len;
  525. rstrm->in_boundry = where;
  526. return TRUE;
  527. }
  528. static bool_t /* knows nothing about records! Only about input buffers */
  529. internal_function
  530. get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
  531. {
  532. int current;
  533. while (len > 0)
  534. {
  535. current = rstrm->in_boundry - rstrm->in_finger;
  536. if (current == 0)
  537. {
  538. if (!fill_input_buf (rstrm))
  539. return FALSE;
  540. continue;
  541. }
  542. current = (len < current) ? len : current;
  543. memcpy (addr, rstrm->in_finger, current);
  544. rstrm->in_finger += current;
  545. addr += current;
  546. len -= current;
  547. }
  548. return TRUE;
  549. }
  550. static bool_t /* next two bytes of the input stream are treated as a header */
  551. internal_function
  552. set_input_fragment (RECSTREAM *rstrm)
  553. {
  554. uint32_t header;
  555. if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
  556. return FALSE;
  557. header = ntohl (header);
  558. rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
  559. /*
  560. * Sanity check. Try not to accept wildly incorrect fragment
  561. * sizes. Unfortunately, only a size of zero can be identified as
  562. * 'wildely incorrect', and this only, if it is not the last
  563. * fragment of a message. Ridiculously large fragment sizes may look
  564. * wrong, but we don't have any way to be certain that they aren't
  565. * what the client actually intended to send us. Many existing RPC
  566. * implementations may sent a fragment of size zero as the last
  567. * fragment of a message.
  568. */
  569. if (header == 0)
  570. return FALSE;
  571. rstrm->fbtbc = header & ~LAST_FRAG;
  572. return TRUE;
  573. }
  574. static bool_t /* consumes input bytes; knows nothing about records! */
  575. internal_function
  576. skip_input_bytes (RECSTREAM *rstrm, long cnt)
  577. {
  578. int current;
  579. while (cnt > 0)
  580. {
  581. current = rstrm->in_boundry - rstrm->in_finger;
  582. if (current == 0)
  583. {
  584. if (!fill_input_buf (rstrm))
  585. return FALSE;
  586. continue;
  587. }
  588. current = (cnt < current) ? cnt : current;
  589. rstrm->in_finger += current;
  590. cnt -= current;
  591. }
  592. return TRUE;
  593. }
  594. static u_int
  595. internal_function
  596. fix_buf_size (u_int s)
  597. {
  598. if (s < 100)
  599. s = 4000;
  600. return RNDUP (s);
  601. }