xdr_rec.c 18 KB

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