xdr_rec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 attribute_hidden
  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. strong_alias(__xdrrec_skiprecord,xdrrec_skiprecord)
  448. /*
  449. * Lookahead function.
  450. * Returns TRUE iff there is no more input in the buffer
  451. * after consuming the rest of the current record.
  452. */
  453. bool_t attribute_hidden
  454. __xdrrec_eof (XDR *xdrs)
  455. {
  456. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  457. while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
  458. {
  459. if (!skip_input_bytes (rstrm, rstrm->fbtbc))
  460. return TRUE;
  461. rstrm->fbtbc = 0;
  462. if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
  463. return TRUE;
  464. }
  465. if (rstrm->in_finger == rstrm->in_boundry)
  466. return TRUE;
  467. return FALSE;
  468. }
  469. strong_alias(__xdrrec_eof,xdrrec_eof)
  470. /*
  471. * The client must tell the package when an end-of-record has occurred.
  472. * The second parameter tells whether the record should be flushed to the
  473. * (output) tcp stream. (This lets the package support batched or
  474. * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
  475. */
  476. bool_t attribute_hidden
  477. __xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
  478. {
  479. RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  480. u_long len; /* fragment length */
  481. if (sendnow || rstrm->frag_sent
  482. || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
  483. {
  484. rstrm->frag_sent = FALSE;
  485. return flush_out (rstrm, TRUE);
  486. }
  487. len = (rstrm->out_finger - (char *) rstrm->frag_header
  488. - BYTES_PER_XDR_UNIT);
  489. *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
  490. rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
  491. rstrm->out_finger += BYTES_PER_XDR_UNIT;
  492. return TRUE;
  493. }
  494. strong_alias(__xdrrec_endofrecord,xdrrec_endofrecord)
  495. /*
  496. * Internal useful routines
  497. */
  498. static bool_t
  499. internal_function
  500. flush_out (RECSTREAM *rstrm, bool_t eor)
  501. {
  502. u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
  503. u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
  504. - BYTES_PER_XDR_UNIT);
  505. *rstrm->frag_header = htonl (len | eormask);
  506. len = rstrm->out_finger - rstrm->out_base;
  507. if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
  508. != (int) len)
  509. return FALSE;
  510. rstrm->frag_header = (u_int32_t *) rstrm->out_base;
  511. rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
  512. return TRUE;
  513. }
  514. static bool_t /* knows nothing about records! Only about input buffers */
  515. fill_input_buf (RECSTREAM *rstrm)
  516. {
  517. caddr_t where;
  518. size_t i;
  519. int len;
  520. where = rstrm->in_base;
  521. i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
  522. where += i;
  523. len = rstrm->in_size - i;
  524. if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
  525. return FALSE;
  526. rstrm->in_finger = where;
  527. where += len;
  528. rstrm->in_boundry = where;
  529. return TRUE;
  530. }
  531. static bool_t /* knows nothing about records! Only about input buffers */
  532. internal_function
  533. get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
  534. {
  535. int current;
  536. while (len > 0)
  537. {
  538. current = rstrm->in_boundry - rstrm->in_finger;
  539. if (current == 0)
  540. {
  541. if (!fill_input_buf (rstrm))
  542. return FALSE;
  543. continue;
  544. }
  545. current = (len < current) ? len : current;
  546. __memcpy (addr, rstrm->in_finger, current);
  547. rstrm->in_finger += current;
  548. addr += current;
  549. len -= current;
  550. }
  551. return TRUE;
  552. }
  553. static bool_t /* next two bytes of the input stream are treated as a header */
  554. internal_function
  555. set_input_fragment (RECSTREAM *rstrm)
  556. {
  557. uint32_t header;
  558. if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
  559. return FALSE;
  560. header = ntohl (header);
  561. rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
  562. /*
  563. * Sanity check. Try not to accept wildly incorrect fragment
  564. * sizes. Unfortunately, only a size of zero can be identified as
  565. * 'wildely incorrect', and this only, if it is not the last
  566. * fragment of a message. Ridiculously large fragment sizes may look
  567. * wrong, but we don't have any way to be certain that they aren't
  568. * what the client actually intended to send us. Many existing RPC
  569. * implementations may sent a fragment of size zero as the last
  570. * fragment of a message.
  571. */
  572. if (header == 0)
  573. return FALSE;
  574. rstrm->fbtbc = header & ~LAST_FRAG;
  575. return TRUE;
  576. }
  577. static bool_t /* consumes input bytes; knows nothing about records! */
  578. internal_function
  579. skip_input_bytes (RECSTREAM *rstrm, long cnt)
  580. {
  581. int current;
  582. while (cnt > 0)
  583. {
  584. current = rstrm->in_boundry - rstrm->in_finger;
  585. if (current == 0)
  586. {
  587. if (!fill_input_buf (rstrm))
  588. return FALSE;
  589. continue;
  590. }
  591. current = (cnt < current) ? cnt : current;
  592. rstrm->in_finger += current;
  593. cnt -= current;
  594. }
  595. return TRUE;
  596. }
  597. static u_int
  598. internal_function
  599. fix_buf_size (u_int s)
  600. {
  601. if (s < 100)
  602. s = 4000;
  603. return RNDUP (s);
  604. }