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