xdr_rec.c 18 KB

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