xdr_rec.c 18 KB

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