xdr_rec.c 18 KB

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