Imported Upstream version 0.2.5
[platform/upstream/libtirpc.git] / src / xdr_rec.c
1
2 /*
3  * Copyright (c) 2009, Sun Microsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  * - Neither the name of Sun Microsystems, Inc. nor the names of its
14  *   contributors may be used to endorse or promote products derived
15  *   from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 #include <sys/cdefs.h>
32
33 /*
34  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
35  * layer above tcp (for rpc's use).
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  *
39  * These routines interface XDRSTREAMS to a tcp/ip connection.
40  * There is a record marking layer between the xdr stream
41  * and the tcp transport level.  A record is composed on one or more
42  * record fragments.  A record fragment is a thirty-two bit header followed
43  * by n bytes of data, where n is contained in the header.  The header
44  * is represented as a htonl(u_long).  Thegh order bit encodes
45  * whether or not the fragment is the last fragment of the record
46  * (1 => fragment is last, 0 => more fragments to follow. 
47  * The other 31 bits encode the byte length of the fragment.
48  */
49
50 #include <sys/types.h>
51
52 #include <netinet/in.h>
53
54 #include <err.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include <rpc/types.h>
60 #include <rpc/xdr.h>
61 #include <rpc/auth.h>
62 #include <rpc/svc_auth.h>
63 #include <rpc/svc.h>
64 #include <rpc/clnt.h>
65 #include <stddef.h>
66 #include "rpc_com.h"
67 static bool_t   xdrrec_getlong(XDR *, long *);
68 static bool_t   xdrrec_putlong(XDR *, const long *);
69 static bool_t   xdrrec_getbytes(XDR *, char *, u_int);
70
71 static bool_t   xdrrec_putbytes(XDR *, const char *, u_int);
72 static u_int    xdrrec_getpos(XDR *);
73 static bool_t   xdrrec_setpos(XDR *, u_int);
74 static int32_t *xdrrec_inline(XDR *, u_int);
75 static void     xdrrec_destroy(XDR *);
76
77 static const struct  xdr_ops xdrrec_ops = {
78         xdrrec_getlong,
79         xdrrec_putlong,
80         xdrrec_getbytes,
81         xdrrec_putbytes,
82         xdrrec_getpos,
83         xdrrec_setpos,
84         xdrrec_inline,
85         xdrrec_destroy
86 };
87
88 /*
89  * A record is composed of one or more record fragments.
90  * A record fragment is a four-byte header followed by zero to
91  * 2**32-1 bytes.  The header is treated as a long unsigned and is
92  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
93  * are a byte count of the fragment.  The highest order bit is a boolean:
94  * 1 => this fragment is the last fragment of the record,
95  * 0 => this fragment is followed by more fragment(s).
96  *
97  * The fragment/record machinery is not general;  it is constructed to
98  * meet the needs of xdr and rpc based on tcp.
99  */
100
101 #define LAST_FRAG ((u_int32_t)(1 << 31))
102
103 typedef struct rec_strm {
104         char *tcp_handle;
105         /*
106          * out-goung bits
107          */
108         int (*writeit)(void *, void *, int);
109         char *out_base; /* output buffer (points to frag header) */
110         char *out_finger;       /* next output position */
111         char *out_boundry;      /* data cannot up to this address */
112         u_int32_t *frag_header; /* beginning of curren fragment */
113         bool_t frag_sent;       /* true if buffer sent in middle of record */
114         /*
115          * in-coming bits
116          */
117         int (*readit)(void *, void *, int);
118         u_long in_size; /* fixed size of the input buffer */
119         char *in_base;
120         char *in_finger;        /* location of next byte to be had */
121         char *in_boundry;       /* can read up to this location */
122         long fbtbc;             /* fragment bytes to be consumed */
123         bool_t last_frag;
124         u_int sendsize;
125         u_int recvsize;
126
127         bool_t nonblock;
128         bool_t in_haveheader;
129         u_int32_t in_header;
130         char *in_hdrp;
131         int in_hdrlen;
132         int in_reclen;
133         int in_received;
134         int in_maxrec;
135 } RECSTREAM;
136
137 static u_int    fix_buf_size(u_int);
138 static bool_t   flush_out(RECSTREAM *, bool_t);
139 static bool_t   fill_input_buf(RECSTREAM *);
140 static bool_t   get_input_bytes(RECSTREAM *, char *, int);
141 static bool_t   set_input_fragment(RECSTREAM *);
142 static bool_t   skip_input_bytes(RECSTREAM *, long);
143 static bool_t   realloc_stream(RECSTREAM *, int);
144
145
146 /*
147  * Create an xdr handle for xdrrec
148  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
149  * send and recv buffer sizes (0 => use default).
150  * tcp_handle is an opaque handle that is passed as the first parameter to
151  * the procedures readit and writeit.  Readit and writeit are read and
152  * write respectively.   They are like the system
153  * calls expect that they take an opaque handle rather than an fd.
154  */
155 void
156 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
157         XDR *xdrs;
158         u_int sendsize;
159         u_int recvsize;
160         void *tcp_handle;
161         /* like read, but pass it a tcp_handle, not sock */
162         int (*readit)(void *, void *, int);
163         /* like write, but pass it a tcp_handle, not sock */
164         int (*writeit)(void *, void *, int);
165 {
166         RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
167
168         if (rstrm == NULL) {
169                 warnx("xdrrec_create: out of memory");
170                 /* 
171                  *  This is bad.  Should rework xdrrec_create to 
172                  *  return a handle, and in this case return NULL
173                  */
174                 return;
175         }
176         rstrm->sendsize = sendsize = fix_buf_size(sendsize);
177         rstrm->out_base = mem_alloc(rstrm->sendsize);
178         if (rstrm->out_base == NULL) {
179                 warnx("xdrrec_create: out of memory");
180                 mem_free(rstrm, sizeof(RECSTREAM));
181                 return;
182         }
183         rstrm->recvsize = recvsize = fix_buf_size(recvsize);
184         rstrm->in_base = mem_alloc(recvsize);
185         if (rstrm->in_base == NULL) {
186                 warnx("xdrrec_create: out of memory");
187                 mem_free(rstrm->out_base, sendsize);
188                 mem_free(rstrm, sizeof(RECSTREAM));
189                 return;
190         }
191         /*
192          * now the rest ...
193          */
194         xdrs->x_ops = &xdrrec_ops;
195         xdrs->x_private = rstrm;
196         rstrm->tcp_handle = tcp_handle;
197         rstrm->readit = readit;
198         rstrm->writeit = writeit;
199         rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
200         rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
201         rstrm->out_finger += sizeof(u_int32_t);
202         rstrm->out_boundry += sendsize;
203         rstrm->frag_sent = FALSE;
204         rstrm->in_size = recvsize;
205         rstrm->in_boundry = rstrm->in_base;
206         rstrm->in_finger = (rstrm->in_boundry += recvsize);
207         rstrm->fbtbc = 0;
208         rstrm->last_frag = TRUE;
209         rstrm->in_haveheader = FALSE;
210         rstrm->in_hdrlen = 0;
211         rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
212         rstrm->nonblock = FALSE;
213         rstrm->in_reclen = 0;
214         rstrm->in_received = 0;
215 }
216
217
218 /*
219  * The reoutines defined below are the xdr ops which will go into the
220  * xdr handle filled in by xdrrec_create.
221  */
222
223 static bool_t
224 xdrrec_getlong(xdrs, lp)
225         XDR *xdrs;
226         long *lp;
227 {
228         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
229         int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
230         int32_t mylong;
231
232         /* first try the inline, fast case */
233         if ((rstrm->fbtbc >= sizeof(int32_t)) &&
234                 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
235                 *lp = (long)ntohl((u_int32_t)(*buflp));
236                 rstrm->fbtbc -= sizeof(int32_t);
237                 rstrm->in_finger += sizeof(int32_t);
238         } else {
239                 if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
240                     sizeof(int32_t)))
241                         return (FALSE);
242                 *lp = (long)ntohl((u_int32_t)mylong);
243         }
244         return (TRUE);
245 }
246
247 static bool_t
248 xdrrec_putlong(xdrs, lp)
249         XDR *xdrs;
250         const long *lp;
251 {
252         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
253         int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
254
255         if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
256                 /*
257                  * this case should almost never happen so the code is
258                  * inefficient
259                  */
260                 rstrm->out_finger -= sizeof(int32_t);
261                 rstrm->frag_sent = TRUE;
262                 if (! flush_out(rstrm, FALSE))
263                         return (FALSE);
264                 dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
265                 rstrm->out_finger += sizeof(int32_t);
266         }
267         *dest_lp = (int32_t)htonl((u_int32_t)(*lp));
268         return (TRUE);
269 }
270
271 static bool_t  /* must manage buffers, fragments, and records */
272 xdrrec_getbytes(xdrs, addr, len)
273         XDR *xdrs;
274         char *addr;
275         u_int len;
276 {
277         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
278         int current;
279
280         while (len > 0) {
281                 current = (int)rstrm->fbtbc;
282                 if (current == 0) {
283                         if (rstrm->last_frag)
284                                 return (FALSE);
285                         if (! set_input_fragment(rstrm))
286                                 return (FALSE);
287                         continue;
288                 }
289                 current = (len < current) ? len : current;
290                 if (! get_input_bytes(rstrm, addr, current))
291                         return (FALSE);
292                 addr += current; 
293                 rstrm->fbtbc -= current;
294                 len -= current;
295         }
296         return (TRUE);
297 }
298
299 static bool_t
300 xdrrec_putbytes(xdrs, addr, len)
301         XDR *xdrs;
302         const char *addr;
303         u_int len;
304 {
305         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
306         size_t current;
307
308         while (len > 0) {
309                 current = (size_t)((u_long)rstrm->out_boundry -
310                     (u_long)rstrm->out_finger);
311                 current = (len < current) ? len : current;
312                 memmove(rstrm->out_finger, addr, current);
313                 rstrm->out_finger += current;
314                 addr += current;
315                 len -= current;
316                 if (rstrm->out_finger == rstrm->out_boundry) {
317                         rstrm->frag_sent = TRUE;
318                         if (! flush_out(rstrm, FALSE))
319                                 return (FALSE);
320                 }
321         }
322         return (TRUE);
323 }
324
325 static u_int
326 xdrrec_getpos(xdrs)
327         XDR *xdrs;
328 {
329         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
330         off_t pos;
331
332         switch (xdrs->x_op) {
333
334         case XDR_ENCODE:
335                 pos = rstrm->out_finger - rstrm->out_base
336                         - BYTES_PER_XDR_UNIT;
337                 break;
338
339         case XDR_DECODE:
340                 pos = rstrm->in_boundry - rstrm->in_finger
341                         - BYTES_PER_XDR_UNIT;
342                 break;
343
344         default:
345                 pos = (off_t) -1;
346                 break;
347         }
348         return ((u_int) pos);
349 }
350
351 static bool_t
352 xdrrec_setpos(xdrs, pos)
353         XDR *xdrs;
354         u_int pos;
355 {
356         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
357         u_int currpos = xdrrec_getpos(xdrs);
358         int delta = currpos - pos;
359         char *newpos;
360
361         if ((int)currpos != -1)
362                 switch (xdrs->x_op) {
363
364                 case XDR_ENCODE:
365                         newpos = rstrm->out_finger - delta;
366                         if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
367                                 (newpos < rstrm->out_boundry)) {
368                                 rstrm->out_finger = newpos;
369                                 return (TRUE);
370                         }
371                         break;
372
373                 case XDR_DECODE:
374                         newpos = rstrm->in_finger - delta;
375                         if ((delta < (int)(rstrm->fbtbc)) &&
376                                 (newpos <= rstrm->in_boundry) &&
377                                 (newpos >= rstrm->in_base)) {
378                                 rstrm->in_finger = newpos;
379                                 rstrm->fbtbc -= delta;
380                                 return (TRUE);
381                         }
382                         break;
383
384                 case XDR_FREE:
385                         break;
386                 }
387         return (FALSE);
388 }
389
390 static int32_t *
391 xdrrec_inline(xdrs, len)
392         XDR *xdrs;
393         u_int len;
394 {
395         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
396         int32_t *buf = NULL;
397
398         switch (xdrs->x_op) {
399
400         case XDR_ENCODE:
401                 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
402                         buf = (int32_t *)(void *)rstrm->out_finger;
403                         rstrm->out_finger += len;
404                 }
405                 break;
406
407         case XDR_DECODE:
408                 if ((len <= rstrm->fbtbc) &&
409                         ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
410                         buf = (int32_t *)(void *)rstrm->in_finger;
411                         rstrm->fbtbc -= len;
412                         rstrm->in_finger += len;
413                 }
414                 break;
415
416         case XDR_FREE:
417                 break;
418         }
419         return (buf);
420 }
421
422 static void
423 xdrrec_destroy(xdrs)
424         XDR *xdrs;
425 {
426         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
427
428         mem_free(rstrm->out_base, rstrm->sendsize);
429         mem_free(rstrm->in_base, rstrm->recvsize);
430         mem_free(rstrm, sizeof(RECSTREAM));
431 }
432
433
434 /*
435  * Exported routines to manage xdr records
436  */
437
438 /*
439  * Before reading (deserializing from the stream, one should always call
440  * this procedure to guarantee proper record alignment.
441  */
442 bool_t
443 xdrrec_skiprecord(xdrs)
444         XDR *xdrs;
445 {
446         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
447         enum xprt_stat xstat;
448
449         if (rstrm->nonblock) {
450                 if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
451                         rstrm->fbtbc = 0;
452                         return TRUE;
453                 }
454                 if (rstrm->in_finger == rstrm->in_boundry &&
455                     xstat == XPRT_MOREREQS) {
456                         rstrm->fbtbc = 0;
457                         return TRUE;
458                 }
459                 return FALSE;
460         }
461
462         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
463                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
464                         return (FALSE);
465                 rstrm->fbtbc = 0;
466                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
467                         return (FALSE);
468         }
469         rstrm->last_frag = FALSE;
470         return (TRUE);
471 }
472
473 /*
474  * Look ahead function.
475  * Returns TRUE iff there is no more input in the buffer
476  * after consuming the rest of the current record.
477  */
478 bool_t
479 xdrrec_eof(xdrs)
480         XDR *xdrs;
481 {
482         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
483
484         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
485                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
486                         return (TRUE);
487                 rstrm->fbtbc = 0;
488                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
489                         return (TRUE);
490         }
491         if (rstrm->in_finger == rstrm->in_boundry)
492                 return (TRUE);
493         return (FALSE);
494 }
495
496 /*
497  * The client must tell the package when an end-of-record has occurred.
498  * The second paraemters tells whether the record should be flushed to the
499  * (output) tcp stream.  (This let's the package support batched or
500  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
501  */
502 bool_t
503 xdrrec_endofrecord(xdrs, sendnow)
504         XDR *xdrs;
505         bool_t sendnow;
506 {
507         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
508         u_long len;  /* fragment length */
509
510         if (sendnow || rstrm->frag_sent ||
511                 ((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
512                 (u_long)rstrm->out_boundry)) {
513                 rstrm->frag_sent = FALSE;
514                 return (flush_out(rstrm, TRUE));
515         }
516         len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
517            sizeof(u_int32_t);
518         *(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
519         rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
520         rstrm->out_finger += sizeof(u_int32_t);
521         return (TRUE);
522 }
523
524 /*
525  * Fill the stream buffer with a record for a non-blocking connection.
526  * Return true if a record is available in the buffer, false if not.
527  */
528 bool_t
529 __xdrrec_getrec(xdrs, statp, expectdata)
530         XDR *xdrs;
531         enum xprt_stat *statp;
532         bool_t expectdata;
533 {
534         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
535         ssize_t n;
536         int fraglen;
537
538         if (!rstrm->in_haveheader) {
539                 n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
540                     (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
541                 if (n == 0) {
542                         *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
543                         return FALSE;
544                 }
545                 if (n < 0) {
546                         *statp = XPRT_DIED;
547                         return FALSE;
548                 }
549                 rstrm->in_hdrp += n;
550                 rstrm->in_hdrlen += n;
551                 if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
552                         *statp = XPRT_MOREREQS;
553                         return FALSE;
554                 }
555                 rstrm->in_header = ntohl(rstrm->in_header);
556                 fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
557                 if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
558                     (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
559                         *statp = XPRT_DIED;
560                         return FALSE;
561                 }
562                 rstrm->in_reclen += fraglen;
563                 if (rstrm->in_reclen > rstrm->recvsize)
564                         realloc_stream(rstrm, rstrm->in_reclen);
565                 if (rstrm->in_header & LAST_FRAG) {
566                         rstrm->in_header &= ~LAST_FRAG;
567                         rstrm->last_frag = TRUE;
568                 }
569         }
570
571         n =  rstrm->readit(rstrm->tcp_handle,
572             rstrm->in_base + rstrm->in_received,
573             (rstrm->in_reclen - rstrm->in_received));
574
575         if (n < 0) {
576                 *statp = XPRT_DIED;
577                 return FALSE;
578         }
579
580         if (n == 0) {
581                 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
582                 return FALSE;
583         }
584
585         rstrm->in_received += n;
586
587         if (rstrm->in_received == rstrm->in_reclen) {
588                 rstrm->in_haveheader = FALSE;
589                 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
590                 rstrm->in_hdrlen = 0;
591                 if (rstrm->last_frag) {
592                         rstrm->fbtbc = rstrm->in_reclen;
593                         rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
594                         rstrm->in_finger = rstrm->in_base;
595                         rstrm->in_reclen = rstrm->in_received = 0;
596                         *statp = XPRT_MOREREQS;
597                         return TRUE;
598                 }
599         }
600
601         *statp = XPRT_MOREREQS;
602         return FALSE;
603 }
604
605 bool_t
606 __xdrrec_setnonblock(xdrs, maxrec)
607         XDR *xdrs;
608         int maxrec;
609 {
610         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
611
612         rstrm->nonblock = TRUE;
613         if (maxrec == 0)
614                 maxrec = rstrm->recvsize;
615         rstrm->in_maxrec = maxrec;
616         return TRUE;
617 }
618
619 /*
620  * Internal useful routines
621  */
622 static bool_t
623 flush_out(rstrm, eor)
624         RECSTREAM *rstrm;
625         bool_t eor;
626 {
627         u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
628         u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) - 
629                 (u_long)(rstrm->frag_header) - sizeof(u_int32_t));
630
631         *(rstrm->frag_header) = htonl(len | eormask);
632         len = (u_int32_t)((u_long)(rstrm->out_finger) - 
633             (u_long)(rstrm->out_base));
634         if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
635                 != (int)len)
636                 return (FALSE);
637         rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
638         rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
639         return (TRUE);
640 }
641
642 static bool_t  /* knows nothing about records!  Only about input buffers */
643 fill_input_buf(rstrm)
644         RECSTREAM *rstrm;
645 {
646         char *where;
647         u_int32_t i;
648         int len;
649
650         if (rstrm->nonblock)
651                 return FALSE;
652
653         where = rstrm->in_base;
654         i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
655         where += i;
656         len = (u_int32_t)(rstrm->in_size - i);
657         if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
658                 return (FALSE);
659         rstrm->in_finger = where;
660         where += len;
661         rstrm->in_boundry = where;
662         return (TRUE);
663 }
664
665 static bool_t  /* knows nothing about records!  Only about input buffers */
666 get_input_bytes(rstrm, addr, len)
667         RECSTREAM *rstrm;
668         char *addr;
669         int len;
670 {
671         size_t current;
672
673         if (rstrm->nonblock) {
674                 if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
675                         return FALSE;
676                 memcpy(addr, rstrm->in_finger, (size_t)len);
677                 rstrm->in_finger += len;
678                 return TRUE;
679         }
680
681         while (len > 0) {
682                 current = (size_t)((long)rstrm->in_boundry -
683                     (long)rstrm->in_finger);
684                 if (current == 0) {
685                         if (! fill_input_buf(rstrm))
686                                 return (FALSE);
687                         continue;
688                 }
689                 current = (len < current) ? len : current;
690                 memmove(addr, rstrm->in_finger, current);
691                 rstrm->in_finger += current;
692                 addr += current;
693                 len -= current;
694         }
695         return (TRUE);
696 }
697
698 static bool_t  /* next two bytes of the input stream are treated as a header */
699 set_input_fragment(rstrm)
700         RECSTREAM *rstrm;
701 {
702         u_int32_t header;
703
704         if (rstrm->nonblock)
705                 return FALSE;
706         if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
707                 return (FALSE);
708         header = ntohl(header);
709         rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
710         /*
711          * Sanity check. Try not to accept wildly incorrect
712          * record sizes. Unfortunately, the only record size
713          * we can positively identify as being 'wildly incorrect'
714          * is zero. Ridiculously large record sizes may look wrong,
715          * but we don't have any way to be certain that they aren't
716          * what the client actually intended to send us.
717          */
718         if (header == 0)
719                 return(FALSE);
720         rstrm->fbtbc = header & (~LAST_FRAG);
721         return (TRUE);
722 }
723
724 static bool_t  /* consumes input bytes; knows nothing about records! */
725 skip_input_bytes(rstrm, cnt)
726         RECSTREAM *rstrm;
727         long cnt;
728 {
729         u_int32_t current;
730
731         while (cnt > 0) {
732                 current = (size_t)((long)rstrm->in_boundry - 
733                     (long)rstrm->in_finger);
734                 if (current == 0) {
735                         if (! fill_input_buf(rstrm))
736                                 return (FALSE);
737                         continue;
738                 }
739                 current = (u_int32_t)((cnt < current) ? cnt : current);
740                 rstrm->in_finger += current;
741                 cnt -= current;
742         }
743         return (TRUE);
744 }
745
746 static u_int
747 fix_buf_size(s)
748         u_int s;
749 {
750
751         if (s < 100)
752                 s = 4000;
753         return (RNDUP(s));
754 }
755
756 /*
757  * Reallocate the input buffer for a non-block stream.
758  */
759 static bool_t
760 realloc_stream(rstrm, size)
761         RECSTREAM *rstrm;
762         int size;
763 {
764         ptrdiff_t diff;
765         char *buf;
766
767         if (size > rstrm->recvsize) {
768                 buf = realloc(rstrm->in_base, (size_t)size);
769                 if (buf == NULL)
770                         return FALSE;
771                 diff = buf - rstrm->in_base;
772                 rstrm->in_finger += diff;
773                 rstrm->in_base = buf;
774                 rstrm->in_boundry = buf + size;
775                 rstrm->recvsize = size;
776                 rstrm->in_size = size;
777         }
778
779         return TRUE;
780 }