xdr_rec.c 17 KB

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