xref: /freebsd/lib/libc/xdr/xdr_rec.c (revision 40525d3debff0cc5e5d3175efd1492d5ef3efaa1)
18360efbdSAlfred Perlstein /*	$NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $	*/
28360efbdSAlfred Perlstein 
3eae561b3SGarrett Wollman /*
4eae561b3SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5eae561b3SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
6eae561b3SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
7eae561b3SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
8eae561b3SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
9eae561b3SGarrett Wollman  * program developed by the user.
10eae561b3SGarrett Wollman  *
11eae561b3SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12eae561b3SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13eae561b3SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14eae561b3SGarrett Wollman  *
15eae561b3SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
16eae561b3SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
17eae561b3SGarrett Wollman  * modification or enhancement.
18eae561b3SGarrett Wollman  *
19eae561b3SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20eae561b3SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21eae561b3SGarrett Wollman  * OR ANY PART THEREOF.
22eae561b3SGarrett Wollman  *
23eae561b3SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24eae561b3SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
25eae561b3SGarrett Wollman  * Sun has been advised of the possibility of such damages.
26eae561b3SGarrett Wollman  *
27eae561b3SGarrett Wollman  * Sun Microsystems, Inc.
28eae561b3SGarrett Wollman  * 2550 Garcia Avenue
29eae561b3SGarrett Wollman  * Mountain View, California  94043
30eae561b3SGarrett Wollman  */
318360efbdSAlfred Perlstein 
328360efbdSAlfred Perlstein #include <sys/cdefs.h>
33eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
34333fc21eSDavid E. O'Brien static char *sccsid = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
35333fc21eSDavid E. O'Brien static char *sccsid = "@(#)xdr_rec.c	2.2 88/08/01 4.0 RPCSRC";
36eae561b3SGarrett Wollman #endif
37333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
38333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39eae561b3SGarrett Wollman 
40eae561b3SGarrett Wollman /*
41eae561b3SGarrett Wollman  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
42eae561b3SGarrett Wollman  * layer above tcp (for rpc's use).
43eae561b3SGarrett Wollman  *
44eae561b3SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
45eae561b3SGarrett Wollman  *
46eae561b3SGarrett Wollman  * These routines interface XDRSTREAMS to a tcp/ip connection.
47eae561b3SGarrett Wollman  * There is a record marking layer between the xdr stream
48eae561b3SGarrett Wollman  * and the tcp transport level.  A record is composed on one or more
49eae561b3SGarrett Wollman  * record fragments.  A record fragment is a thirty-two bit header followed
50eae561b3SGarrett Wollman  * by n bytes of data, where n is contained in the header.  The header
51eae561b3SGarrett Wollman  * is represented as a htonl(u_long).  Thegh order bit encodes
52eae561b3SGarrett Wollman  * whether or not the fragment is the last fragment of the record
53eae561b3SGarrett Wollman  * (1 => fragment is last, 0 => more fragments to follow.
54eae561b3SGarrett Wollman  * The other 31 bits encode the byte length of the fragment.
55eae561b3SGarrett Wollman  */
56eae561b3SGarrett Wollman 
578360efbdSAlfred Perlstein #include "namespace.h"
588360efbdSAlfred Perlstein #include <sys/types.h>
598360efbdSAlfred Perlstein 
608360efbdSAlfred Perlstein #include <netinet/in.h>
618360efbdSAlfred Perlstein 
628360efbdSAlfred Perlstein #include <err.h>
63eae561b3SGarrett Wollman #include <stdio.h>
64eae561b3SGarrett Wollman #include <stdlib.h>
6529285d6cSPoul-Henning Kamp #include <string.h>
668360efbdSAlfred Perlstein 
67eae561b3SGarrett Wollman #include <rpc/types.h>
68eae561b3SGarrett Wollman #include <rpc/xdr.h>
6908497c02SMartin Blapp #include <rpc/auth.h>
7008497c02SMartin Blapp #include <rpc/svc.h>
7108497c02SMartin Blapp #include <rpc/clnt.h>
7208497c02SMartin Blapp #include <sys/stddef.h>
738360efbdSAlfred Perlstein #include "un-namespace.h"
74eae561b3SGarrett Wollman 
75c05ac53bSDavid E. O'Brien static bool_t	xdrrec_getlong(XDR *, long *);
76c05ac53bSDavid E. O'Brien static bool_t	xdrrec_putlong(XDR *, const long *);
77c05ac53bSDavid E. O'Brien static bool_t	xdrrec_getbytes(XDR *, char *, u_int);
78eae561b3SGarrett Wollman 
79c05ac53bSDavid E. O'Brien static bool_t	xdrrec_putbytes(XDR *, const char *, u_int);
80c05ac53bSDavid E. O'Brien static u_int	xdrrec_getpos(XDR *);
81c05ac53bSDavid E. O'Brien static bool_t	xdrrec_setpos(XDR *, u_int);
82c05ac53bSDavid E. O'Brien static int32_t *xdrrec_inline(XDR *, u_int);
83c05ac53bSDavid E. O'Brien static void	xdrrec_destroy(XDR *);
84eae561b3SGarrett Wollman 
858360efbdSAlfred Perlstein static const struct  xdr_ops xdrrec_ops = {
86eae561b3SGarrett Wollman 	xdrrec_getlong,
87eae561b3SGarrett Wollman 	xdrrec_putlong,
88eae561b3SGarrett Wollman 	xdrrec_getbytes,
89eae561b3SGarrett Wollman 	xdrrec_putbytes,
90eae561b3SGarrett Wollman 	xdrrec_getpos,
91eae561b3SGarrett Wollman 	xdrrec_setpos,
92eae561b3SGarrett Wollman 	xdrrec_inline,
93eae561b3SGarrett Wollman 	xdrrec_destroy
94eae561b3SGarrett Wollman };
95eae561b3SGarrett Wollman 
96eae561b3SGarrett Wollman /*
97eae561b3SGarrett Wollman  * A record is composed of one or more record fragments.
9808497c02SMartin Blapp  * A record fragment is a four-byte header followed by zero to
99eae561b3SGarrett Wollman  * 2**32-1 bytes.  The header is treated as a long unsigned and is
100eae561b3SGarrett Wollman  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
101eae561b3SGarrett Wollman  * are a byte count of the fragment.  The highest order bit is a boolean:
102eae561b3SGarrett Wollman  * 1 => this fragment is the last fragment of the record,
103eae561b3SGarrett Wollman  * 0 => this fragment is followed by more fragment(s).
104eae561b3SGarrett Wollman  *
105eae561b3SGarrett Wollman  * The fragment/record machinery is not general;  it is constructed to
106eae561b3SGarrett Wollman  * meet the needs of xdr and rpc based on tcp.
107eae561b3SGarrett Wollman  */
108eae561b3SGarrett Wollman 
1091ad08a09SPeter Wemm #define LAST_FRAG ((u_int32_t)(1 << 31))
110eae561b3SGarrett Wollman 
111eae561b3SGarrett Wollman typedef struct rec_strm {
1128360efbdSAlfred Perlstein 	char *tcp_handle;
113eae561b3SGarrett Wollman 	/*
114eae561b3SGarrett Wollman 	 * out-goung bits
115eae561b3SGarrett Wollman 	 */
116f249dbccSDag-Erling Smørgrav 	int (*writeit)(void *, void *, int);
1178360efbdSAlfred Perlstein 	char *out_base;	/* output buffer (points to frag header) */
1188360efbdSAlfred Perlstein 	char *out_finger;	/* next output position */
1198360efbdSAlfred Perlstein 	char *out_boundry;	/* data cannot up to this address */
1208360efbdSAlfred Perlstein 	u_int32_t *frag_header;	/* beginning of curren fragment */
121eae561b3SGarrett Wollman 	bool_t frag_sent;	/* true if buffer sent in middle of record */
122eae561b3SGarrett Wollman 	/*
123eae561b3SGarrett Wollman 	 * in-coming bits
124eae561b3SGarrett Wollman 	 */
125f249dbccSDag-Erling Smørgrav 	int (*readit)(void *, void *, int);
126eae561b3SGarrett Wollman 	u_long in_size;	/* fixed size of the input buffer */
1278360efbdSAlfred Perlstein 	char *in_base;
1288360efbdSAlfred Perlstein 	char *in_finger;	/* location of next byte to be had */
1298360efbdSAlfred Perlstein 	char *in_boundry;	/* can read up to this location */
130eae561b3SGarrett Wollman 	long fbtbc;		/* fragment bytes to be consumed */
131eae561b3SGarrett Wollman 	bool_t last_frag;
132eae561b3SGarrett Wollman 	u_int sendsize;
133eae561b3SGarrett Wollman 	u_int recvsize;
13408497c02SMartin Blapp 
13508497c02SMartin Blapp 	bool_t nonblock;
13608497c02SMartin Blapp 	bool_t in_haveheader;
13708497c02SMartin Blapp 	u_int32_t in_header;
13808497c02SMartin Blapp 	char *in_hdrp;
13908497c02SMartin Blapp 	int in_hdrlen;
14008497c02SMartin Blapp 	int in_reclen;
14108497c02SMartin Blapp 	int in_received;
14208497c02SMartin Blapp 	int in_maxrec;
143eae561b3SGarrett Wollman } RECSTREAM;
144eae561b3SGarrett Wollman 
145c05ac53bSDavid E. O'Brien static u_int	fix_buf_size(u_int);
146c05ac53bSDavid E. O'Brien static bool_t	flush_out(RECSTREAM *, bool_t);
147c05ac53bSDavid E. O'Brien static bool_t	fill_input_buf(RECSTREAM *);
148c05ac53bSDavid E. O'Brien static bool_t	get_input_bytes(RECSTREAM *, char *, int);
149c05ac53bSDavid E. O'Brien static bool_t	set_input_fragment(RECSTREAM *);
150c05ac53bSDavid E. O'Brien static bool_t	skip_input_bytes(RECSTREAM *, long);
15108497c02SMartin Blapp static bool_t	realloc_stream(RECSTREAM *, int);
1528360efbdSAlfred Perlstein 
153eae561b3SGarrett Wollman 
154eae561b3SGarrett Wollman /*
155eae561b3SGarrett Wollman  * Create an xdr handle for xdrrec
156eae561b3SGarrett Wollman  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
157eae561b3SGarrett Wollman  * send and recv buffer sizes (0 => use default).
158eae561b3SGarrett Wollman  * tcp_handle is an opaque handle that is passed as the first parameter to
159eae561b3SGarrett Wollman  * the procedures readit and writeit.  Readit and writeit are read and
160eae561b3SGarrett Wollman  * write respectively.   They are like the system
161eae561b3SGarrett Wollman  * calls expect that they take an opaque handle rather than an fd.
162eae561b3SGarrett Wollman  */
163eae561b3SGarrett Wollman void
164eae561b3SGarrett Wollman xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
1658360efbdSAlfred Perlstein 	XDR *xdrs;
1668360efbdSAlfred Perlstein 	u_int sendsize;
1678360efbdSAlfred Perlstein 	u_int recvsize;
168f249dbccSDag-Erling Smørgrav 	void *tcp_handle;
1698360efbdSAlfred Perlstein 	/* like read, but pass it a tcp_handle, not sock */
170f249dbccSDag-Erling Smørgrav 	int (*readit)(void *, void *, int);
1718360efbdSAlfred Perlstein 	/* like write, but pass it a tcp_handle, not sock */
172f249dbccSDag-Erling Smørgrav 	int (*writeit)(void *, void *, int);
173eae561b3SGarrett Wollman {
1748360efbdSAlfred Perlstein 	RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
175eae561b3SGarrett Wollman 
176eae561b3SGarrett Wollman 	if (rstrm == NULL) {
1778360efbdSAlfred Perlstein 		warnx("xdrrec_create: out of memory");
178eae561b3SGarrett Wollman 		/*
179eae561b3SGarrett Wollman 		 *  This is bad.  Should rework xdrrec_create to
180eae561b3SGarrett Wollman 		 *  return a handle, and in this case return NULL
181eae561b3SGarrett Wollman 		 */
182eae561b3SGarrett Wollman 		return;
183eae561b3SGarrett Wollman 	}
184eae561b3SGarrett Wollman 	rstrm->sendsize = sendsize = fix_buf_size(sendsize);
18508497c02SMartin Blapp 	rstrm->out_base = mem_alloc(rstrm->sendsize);
18608497c02SMartin Blapp 	if (rstrm->out_base == NULL) {
1878360efbdSAlfred Perlstein 		warnx("xdrrec_create: out of memory");
18808497c02SMartin Blapp 		mem_free(rstrm, sizeof(RECSTREAM));
189eae561b3SGarrett Wollman 		return;
190eae561b3SGarrett Wollman 	}
19108497c02SMartin Blapp 	rstrm->recvsize = recvsize = fix_buf_size(recvsize);
19208497c02SMartin Blapp 	rstrm->in_base = mem_alloc(recvsize);
19308497c02SMartin Blapp 	if (rstrm->in_base == NULL) {
19408497c02SMartin Blapp 		warnx("xdrrec_create: out of memory");
19508497c02SMartin Blapp 		mem_free(rstrm->out_base, sendsize);
19608497c02SMartin Blapp 		mem_free(rstrm, sizeof(RECSTREAM));
19708497c02SMartin Blapp 		return;
19808497c02SMartin Blapp 	}
199eae561b3SGarrett Wollman 	/*
200eae561b3SGarrett Wollman 	 * now the rest ...
201eae561b3SGarrett Wollman 	 */
202eae561b3SGarrett Wollman 	xdrs->x_ops = &xdrrec_ops;
2038360efbdSAlfred Perlstein 	xdrs->x_private = rstrm;
204eae561b3SGarrett Wollman 	rstrm->tcp_handle = tcp_handle;
205eae561b3SGarrett Wollman 	rstrm->readit = readit;
206eae561b3SGarrett Wollman 	rstrm->writeit = writeit;
207eae561b3SGarrett Wollman 	rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
2088360efbdSAlfred Perlstein 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
2091ad08a09SPeter Wemm 	rstrm->out_finger += sizeof(u_int32_t);
210eae561b3SGarrett Wollman 	rstrm->out_boundry += sendsize;
211eae561b3SGarrett Wollman 	rstrm->frag_sent = FALSE;
212eae561b3SGarrett Wollman 	rstrm->in_size = recvsize;
213eae561b3SGarrett Wollman 	rstrm->in_boundry = rstrm->in_base;
214eae561b3SGarrett Wollman 	rstrm->in_finger = (rstrm->in_boundry += recvsize);
215eae561b3SGarrett Wollman 	rstrm->fbtbc = 0;
216eae561b3SGarrett Wollman 	rstrm->last_frag = TRUE;
21708497c02SMartin Blapp 	rstrm->in_haveheader = FALSE;
21808497c02SMartin Blapp 	rstrm->in_hdrlen = 0;
21908497c02SMartin Blapp 	rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
22008497c02SMartin Blapp 	rstrm->nonblock = FALSE;
22108497c02SMartin Blapp 	rstrm->in_reclen = 0;
22208497c02SMartin Blapp 	rstrm->in_received = 0;
223eae561b3SGarrett Wollman }
224eae561b3SGarrett Wollman 
225eae561b3SGarrett Wollman 
226eae561b3SGarrett Wollman /*
227eae561b3SGarrett Wollman  * The reoutines defined below are the xdr ops which will go into the
228eae561b3SGarrett Wollman  * xdr handle filled in by xdrrec_create.
229eae561b3SGarrett Wollman  */
230eae561b3SGarrett Wollman 
231eae561b3SGarrett Wollman static bool_t
232eae561b3SGarrett Wollman xdrrec_getlong(xdrs, lp)
233eae561b3SGarrett Wollman 	XDR *xdrs;
234eae561b3SGarrett Wollman 	long *lp;
235eae561b3SGarrett Wollman {
2368360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
2378360efbdSAlfred Perlstein 	int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
2381ad08a09SPeter Wemm 	int32_t mylong;
239eae561b3SGarrett Wollman 
240eae561b3SGarrett Wollman 	/* first try the inline, fast case */
2411ad08a09SPeter Wemm 	if ((rstrm->fbtbc >= sizeof(int32_t)) &&
2421ad08a09SPeter Wemm 		(((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
2431ad08a09SPeter Wemm 		*lp = (long)ntohl((u_int32_t)(*buflp));
2441ad08a09SPeter Wemm 		rstrm->fbtbc -= sizeof(int32_t);
2451ad08a09SPeter Wemm 		rstrm->in_finger += sizeof(int32_t);
246eae561b3SGarrett Wollman 	} else {
2478360efbdSAlfred Perlstein 		if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
2488360efbdSAlfred Perlstein 		    sizeof(int32_t)))
249eae561b3SGarrett Wollman 			return (FALSE);
2501ad08a09SPeter Wemm 		*lp = (long)ntohl((u_int32_t)mylong);
251eae561b3SGarrett Wollman 	}
252eae561b3SGarrett Wollman 	return (TRUE);
253eae561b3SGarrett Wollman }
254eae561b3SGarrett Wollman 
255eae561b3SGarrett Wollman static bool_t
256eae561b3SGarrett Wollman xdrrec_putlong(xdrs, lp)
257eae561b3SGarrett Wollman 	XDR *xdrs;
2588360efbdSAlfred Perlstein 	const long *lp;
259eae561b3SGarrett Wollman {
2608360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
2618360efbdSAlfred Perlstein 	int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
262eae561b3SGarrett Wollman 
2631ad08a09SPeter Wemm 	if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
264eae561b3SGarrett Wollman 		/*
265eae561b3SGarrett Wollman 		 * this case should almost never happen so the code is
266eae561b3SGarrett Wollman 		 * inefficient
267eae561b3SGarrett Wollman 		 */
2681ad08a09SPeter Wemm 		rstrm->out_finger -= sizeof(int32_t);
269eae561b3SGarrett Wollman 		rstrm->frag_sent = TRUE;
270eae561b3SGarrett Wollman 		if (! flush_out(rstrm, FALSE))
271eae561b3SGarrett Wollman 			return (FALSE);
2728360efbdSAlfred Perlstein 		dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
2731ad08a09SPeter Wemm 		rstrm->out_finger += sizeof(int32_t);
274eae561b3SGarrett Wollman 	}
2751ad08a09SPeter Wemm 	*dest_lp = (int32_t)htonl((u_int32_t)(*lp));
276eae561b3SGarrett Wollman 	return (TRUE);
277eae561b3SGarrett Wollman }
278eae561b3SGarrett Wollman 
279eae561b3SGarrett Wollman static bool_t  /* must manage buffers, fragments, and records */
280eae561b3SGarrett Wollman xdrrec_getbytes(xdrs, addr, len)
281eae561b3SGarrett Wollman 	XDR *xdrs;
2828360efbdSAlfred Perlstein 	char *addr;
2838360efbdSAlfred Perlstein 	u_int len;
284eae561b3SGarrett Wollman {
2858360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
2868360efbdSAlfred Perlstein 	int current;
287eae561b3SGarrett Wollman 
288eae561b3SGarrett Wollman 	while (len > 0) {
2898360efbdSAlfred Perlstein 		current = (int)rstrm->fbtbc;
290eae561b3SGarrett Wollman 		if (current == 0) {
291eae561b3SGarrett Wollman 			if (rstrm->last_frag)
292eae561b3SGarrett Wollman 				return (FALSE);
293eae561b3SGarrett Wollman 			if (! set_input_fragment(rstrm))
294eae561b3SGarrett Wollman 				return (FALSE);
295eae561b3SGarrett Wollman 			continue;
296eae561b3SGarrett Wollman 		}
297eae561b3SGarrett Wollman 		current = (len < current) ? len : current;
298eae561b3SGarrett Wollman 		if (! get_input_bytes(rstrm, addr, current))
299eae561b3SGarrett Wollman 			return (FALSE);
300eae561b3SGarrett Wollman 		addr += current;
301eae561b3SGarrett Wollman 		rstrm->fbtbc -= current;
302eae561b3SGarrett Wollman 		len -= current;
303eae561b3SGarrett Wollman 	}
304eae561b3SGarrett Wollman 	return (TRUE);
305eae561b3SGarrett Wollman }
306eae561b3SGarrett Wollman 
307eae561b3SGarrett Wollman static bool_t
308eae561b3SGarrett Wollman xdrrec_putbytes(xdrs, addr, len)
309eae561b3SGarrett Wollman 	XDR *xdrs;
3108360efbdSAlfred Perlstein 	const char *addr;
3118360efbdSAlfred Perlstein 	u_int len;
312eae561b3SGarrett Wollman {
3138360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
3148360efbdSAlfred Perlstein 	size_t current;
315eae561b3SGarrett Wollman 
316eae561b3SGarrett Wollman 	while (len > 0) {
3178360efbdSAlfred Perlstein 		current = (size_t)((u_long)rstrm->out_boundry -
3188360efbdSAlfred Perlstein 		    (u_long)rstrm->out_finger);
319eae561b3SGarrett Wollman 		current = (len < current) ? len : current;
3208360efbdSAlfred Perlstein 		memmove(rstrm->out_finger, addr, current);
321eae561b3SGarrett Wollman 		rstrm->out_finger += current;
322eae561b3SGarrett Wollman 		addr += current;
323eae561b3SGarrett Wollman 		len -= current;
324eae561b3SGarrett Wollman 		if (rstrm->out_finger == rstrm->out_boundry) {
325eae561b3SGarrett Wollman 			rstrm->frag_sent = TRUE;
326eae561b3SGarrett Wollman 			if (! flush_out(rstrm, FALSE))
327eae561b3SGarrett Wollman 				return (FALSE);
328eae561b3SGarrett Wollman 		}
329eae561b3SGarrett Wollman 	}
330eae561b3SGarrett Wollman 	return (TRUE);
331eae561b3SGarrett Wollman }
332eae561b3SGarrett Wollman 
333eae561b3SGarrett Wollman static u_int
334eae561b3SGarrett Wollman xdrrec_getpos(xdrs)
3358360efbdSAlfred Perlstein 	XDR *xdrs;
336eae561b3SGarrett Wollman {
3378360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
3388360efbdSAlfred Perlstein 	off_t pos;
339eae561b3SGarrett Wollman 
3408360efbdSAlfred Perlstein 	pos = lseek((int)(u_long)rstrm->tcp_handle, (off_t)0, 1);
341eae561b3SGarrett Wollman 	if (pos != -1)
342eae561b3SGarrett Wollman 		switch (xdrs->x_op) {
343eae561b3SGarrett Wollman 
344eae561b3SGarrett Wollman 		case XDR_ENCODE:
345eae561b3SGarrett Wollman 			pos += rstrm->out_finger - rstrm->out_base;
346eae561b3SGarrett Wollman 			break;
347eae561b3SGarrett Wollman 
348eae561b3SGarrett Wollman 		case XDR_DECODE:
349eae561b3SGarrett Wollman 			pos -= rstrm->in_boundry - rstrm->in_finger;
350eae561b3SGarrett Wollman 			break;
351eae561b3SGarrett Wollman 
352eae561b3SGarrett Wollman 		default:
3538360efbdSAlfred Perlstein 			pos = (off_t) -1;
354eae561b3SGarrett Wollman 			break;
355eae561b3SGarrett Wollman 		}
356eae561b3SGarrett Wollman 	return ((u_int) pos);
357eae561b3SGarrett Wollman }
358eae561b3SGarrett Wollman 
359eae561b3SGarrett Wollman static bool_t
360eae561b3SGarrett Wollman xdrrec_setpos(xdrs, pos)
3618360efbdSAlfred Perlstein 	XDR *xdrs;
362eae561b3SGarrett Wollman 	u_int pos;
363eae561b3SGarrett Wollman {
3648360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
365eae561b3SGarrett Wollman 	u_int currpos = xdrrec_getpos(xdrs);
366eae561b3SGarrett Wollman 	int delta = currpos - pos;
3678360efbdSAlfred Perlstein 	char *newpos;
368eae561b3SGarrett Wollman 
369eae561b3SGarrett Wollman 	if ((int)currpos != -1)
370eae561b3SGarrett Wollman 		switch (xdrs->x_op) {
371eae561b3SGarrett Wollman 
372eae561b3SGarrett Wollman 		case XDR_ENCODE:
373eae561b3SGarrett Wollman 			newpos = rstrm->out_finger - delta;
3748360efbdSAlfred Perlstein 			if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
375eae561b3SGarrett Wollman 				(newpos < rstrm->out_boundry)) {
376eae561b3SGarrett Wollman 				rstrm->out_finger = newpos;
377eae561b3SGarrett Wollman 				return (TRUE);
378eae561b3SGarrett Wollman 			}
379eae561b3SGarrett Wollman 			break;
380eae561b3SGarrett Wollman 
381eae561b3SGarrett Wollman 		case XDR_DECODE:
382eae561b3SGarrett Wollman 			newpos = rstrm->in_finger - delta;
383eae561b3SGarrett Wollman 			if ((delta < (int)(rstrm->fbtbc)) &&
384eae561b3SGarrett Wollman 				(newpos <= rstrm->in_boundry) &&
385eae561b3SGarrett Wollman 				(newpos >= rstrm->in_base)) {
386eae561b3SGarrett Wollman 				rstrm->in_finger = newpos;
387eae561b3SGarrett Wollman 				rstrm->fbtbc -= delta;
388eae561b3SGarrett Wollman 				return (TRUE);
389eae561b3SGarrett Wollman 			}
390eae561b3SGarrett Wollman 			break;
3918360efbdSAlfred Perlstein 
3928360efbdSAlfred Perlstein 		case XDR_FREE:
3938360efbdSAlfred Perlstein 			break;
394eae561b3SGarrett Wollman 		}
395eae561b3SGarrett Wollman 	return (FALSE);
396eae561b3SGarrett Wollman }
397eae561b3SGarrett Wollman 
3981ad08a09SPeter Wemm static int32_t *
399eae561b3SGarrett Wollman xdrrec_inline(xdrs, len)
4008360efbdSAlfred Perlstein 	XDR *xdrs;
4018360efbdSAlfred Perlstein 	u_int len;
402eae561b3SGarrett Wollman {
4038360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
4041ad08a09SPeter Wemm 	int32_t *buf = NULL;
405eae561b3SGarrett Wollman 
406eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
407eae561b3SGarrett Wollman 
408eae561b3SGarrett Wollman 	case XDR_ENCODE:
409eae561b3SGarrett Wollman 		if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
4108360efbdSAlfred Perlstein 			buf = (int32_t *)(void *)rstrm->out_finger;
411eae561b3SGarrett Wollman 			rstrm->out_finger += len;
412eae561b3SGarrett Wollman 		}
413eae561b3SGarrett Wollman 		break;
414eae561b3SGarrett Wollman 
415eae561b3SGarrett Wollman 	case XDR_DECODE:
416eae561b3SGarrett Wollman 		if ((len <= rstrm->fbtbc) &&
417eae561b3SGarrett Wollman 			((rstrm->in_finger + len) <= rstrm->in_boundry)) {
4188360efbdSAlfred Perlstein 			buf = (int32_t *)(void *)rstrm->in_finger;
419eae561b3SGarrett Wollman 			rstrm->fbtbc -= len;
420eae561b3SGarrett Wollman 			rstrm->in_finger += len;
421eae561b3SGarrett Wollman 		}
422eae561b3SGarrett Wollman 		break;
4238360efbdSAlfred Perlstein 
4248360efbdSAlfred Perlstein 	case XDR_FREE:
4258360efbdSAlfred Perlstein 		break;
426eae561b3SGarrett Wollman 	}
427eae561b3SGarrett Wollman 	return (buf);
428eae561b3SGarrett Wollman }
429eae561b3SGarrett Wollman 
430eae561b3SGarrett Wollman static void
431eae561b3SGarrett Wollman xdrrec_destroy(xdrs)
4328360efbdSAlfred Perlstein 	XDR *xdrs;
433eae561b3SGarrett Wollman {
4348360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
435eae561b3SGarrett Wollman 
43608497c02SMartin Blapp 	mem_free(rstrm->out_base, rstrm->sendsize);
43708497c02SMartin Blapp 	mem_free(rstrm->in_base, rstrm->recvsize);
4388360efbdSAlfred Perlstein 	mem_free(rstrm, sizeof(RECSTREAM));
439eae561b3SGarrett Wollman }
440eae561b3SGarrett Wollman 
441eae561b3SGarrett Wollman 
442eae561b3SGarrett Wollman /*
443eae561b3SGarrett Wollman  * Exported routines to manage xdr records
444eae561b3SGarrett Wollman  */
445eae561b3SGarrett Wollman 
446eae561b3SGarrett Wollman /*
447eae561b3SGarrett Wollman  * Before reading (deserializing from the stream, one should always call
448eae561b3SGarrett Wollman  * this procedure to guarantee proper record alignment.
449eae561b3SGarrett Wollman  */
450eae561b3SGarrett Wollman bool_t
451eae561b3SGarrett Wollman xdrrec_skiprecord(xdrs)
452eae561b3SGarrett Wollman 	XDR *xdrs;
453eae561b3SGarrett Wollman {
4548360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
45508497c02SMartin Blapp 	enum xprt_stat xstat;
45608497c02SMartin Blapp 
45708497c02SMartin Blapp 	if (rstrm->nonblock) {
45808497c02SMartin Blapp 		if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
45908497c02SMartin Blapp 			rstrm->fbtbc = 0;
46008497c02SMartin Blapp 			return TRUE;
46108497c02SMartin Blapp 		}
46208497c02SMartin Blapp 		if (rstrm->in_finger == rstrm->in_boundry &&
46308497c02SMartin Blapp 		    xstat == XPRT_MOREREQS) {
46408497c02SMartin Blapp 			rstrm->fbtbc = 0;
46508497c02SMartin Blapp 			return TRUE;
46608497c02SMartin Blapp 		}
46708497c02SMartin Blapp 		return FALSE;
46808497c02SMartin Blapp 	}
469eae561b3SGarrett Wollman 
470eae561b3SGarrett Wollman 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
471eae561b3SGarrett Wollman 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
472eae561b3SGarrett Wollman 			return (FALSE);
473eae561b3SGarrett Wollman 		rstrm->fbtbc = 0;
474eae561b3SGarrett Wollman 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
475eae561b3SGarrett Wollman 			return (FALSE);
476eae561b3SGarrett Wollman 	}
477eae561b3SGarrett Wollman 	rstrm->last_frag = FALSE;
478eae561b3SGarrett Wollman 	return (TRUE);
479eae561b3SGarrett Wollman }
480eae561b3SGarrett Wollman 
481eae561b3SGarrett Wollman /*
4828360efbdSAlfred Perlstein  * Look ahead function.
483eae561b3SGarrett Wollman  * Returns TRUE iff there is no more input in the buffer
484eae561b3SGarrett Wollman  * after consuming the rest of the current record.
485eae561b3SGarrett Wollman  */
486eae561b3SGarrett Wollman bool_t
487eae561b3SGarrett Wollman xdrrec_eof(xdrs)
488eae561b3SGarrett Wollman 	XDR *xdrs;
489eae561b3SGarrett Wollman {
4908360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
49108497c02SMartin Blapp 	enum xprt_stat xstat;
49208497c02SMartin Blapp 
49308497c02SMartin Blapp 	if (rstrm->nonblock) {
49408497c02SMartin Blapp 		if (__xdrrec_getrec(xdrs, &xstat, FALSE))
49508497c02SMartin Blapp 			return FALSE;
49608497c02SMartin Blapp 		if (!rstrm->in_haveheader && xstat == XPRT_IDLE)
49708497c02SMartin Blapp 			return TRUE;
49808497c02SMartin Blapp 		return FALSE;
49908497c02SMartin Blapp 	}
500eae561b3SGarrett Wollman 
501eae561b3SGarrett Wollman 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
502eae561b3SGarrett Wollman 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
503eae561b3SGarrett Wollman 			return (TRUE);
504eae561b3SGarrett Wollman 		rstrm->fbtbc = 0;
505eae561b3SGarrett Wollman 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
506eae561b3SGarrett Wollman 			return (TRUE);
507eae561b3SGarrett Wollman 	}
508eae561b3SGarrett Wollman 	if (rstrm->in_finger == rstrm->in_boundry)
509eae561b3SGarrett Wollman 		return (TRUE);
510eae561b3SGarrett Wollman 	return (FALSE);
511eae561b3SGarrett Wollman }
512eae561b3SGarrett Wollman 
513eae561b3SGarrett Wollman /*
514eae561b3SGarrett Wollman  * The client must tell the package when an end-of-record has occurred.
515eae561b3SGarrett Wollman  * The second paraemters tells whether the record should be flushed to the
516eae561b3SGarrett Wollman  * (output) tcp stream.  (This let's the package support batched or
517eae561b3SGarrett Wollman  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
518eae561b3SGarrett Wollman  */
519eae561b3SGarrett Wollman bool_t
520eae561b3SGarrett Wollman xdrrec_endofrecord(xdrs, sendnow)
521eae561b3SGarrett Wollman 	XDR *xdrs;
522eae561b3SGarrett Wollman 	bool_t sendnow;
523eae561b3SGarrett Wollman {
5248360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
5258360efbdSAlfred Perlstein 	u_long len;  /* fragment length */
526eae561b3SGarrett Wollman 
527eae561b3SGarrett Wollman 	if (sendnow || rstrm->frag_sent ||
5281ad08a09SPeter Wemm 		((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
529eae561b3SGarrett Wollman 		(u_long)rstrm->out_boundry)) {
530eae561b3SGarrett Wollman 		rstrm->frag_sent = FALSE;
531eae561b3SGarrett Wollman 		return (flush_out(rstrm, TRUE));
532eae561b3SGarrett Wollman 	}
533eae561b3SGarrett Wollman 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
5341ad08a09SPeter Wemm 	   sizeof(u_int32_t);
5358360efbdSAlfred Perlstein 	*(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
5368360efbdSAlfred Perlstein 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
5371ad08a09SPeter Wemm 	rstrm->out_finger += sizeof(u_int32_t);
538eae561b3SGarrett Wollman 	return (TRUE);
539eae561b3SGarrett Wollman }
540eae561b3SGarrett Wollman 
54108497c02SMartin Blapp /*
54208497c02SMartin Blapp  * Fill the stream buffer with a record for a non-blocking connection.
54308497c02SMartin Blapp  * Return true if a record is available in the buffer, false if not.
54408497c02SMartin Blapp  */
54508497c02SMartin Blapp bool_t
54608497c02SMartin Blapp __xdrrec_getrec(xdrs, statp, expectdata)
54708497c02SMartin Blapp 	XDR *xdrs;
54808497c02SMartin Blapp 	enum xprt_stat *statp;
54908497c02SMartin Blapp 	bool_t expectdata;
55008497c02SMartin Blapp {
55108497c02SMartin Blapp 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
55208497c02SMartin Blapp 	ssize_t n;
55308497c02SMartin Blapp 	int fraglen;
55408497c02SMartin Blapp 
55508497c02SMartin Blapp 	if (!rstrm->in_haveheader) {
55608497c02SMartin Blapp 		n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
55708497c02SMartin Blapp 		    (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
55808497c02SMartin Blapp 		if (n == 0) {
55908497c02SMartin Blapp 			*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
56008497c02SMartin Blapp 			return FALSE;
56108497c02SMartin Blapp 		}
56208497c02SMartin Blapp 		if (n < 0) {
56308497c02SMartin Blapp 			*statp = XPRT_DIED;
56408497c02SMartin Blapp 			return FALSE;
56508497c02SMartin Blapp 		}
56608497c02SMartin Blapp 		rstrm->in_hdrp += n;
56708497c02SMartin Blapp 		rstrm->in_hdrlen += n;
56808497c02SMartin Blapp 		if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
56908497c02SMartin Blapp 			*statp = XPRT_MOREREQS;
57008497c02SMartin Blapp 			return FALSE;
57108497c02SMartin Blapp 		}
57208497c02SMartin Blapp 		rstrm->in_header = ntohl(rstrm->in_header);
57308497c02SMartin Blapp 		fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
57408497c02SMartin Blapp 		if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
57508497c02SMartin Blapp 		    (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
57608497c02SMartin Blapp 			*statp = XPRT_DIED;
57708497c02SMartin Blapp 			return FALSE;
57808497c02SMartin Blapp 		}
57908497c02SMartin Blapp 		rstrm->in_reclen += fraglen;
58008497c02SMartin Blapp 		if (rstrm->in_reclen > rstrm->recvsize)
58108497c02SMartin Blapp 			realloc_stream(rstrm, rstrm->in_reclen);
58208497c02SMartin Blapp 		if (rstrm->in_header & LAST_FRAG) {
58308497c02SMartin Blapp 			rstrm->in_header &= ~LAST_FRAG;
58408497c02SMartin Blapp 			rstrm->last_frag = TRUE;
58508497c02SMartin Blapp 		}
58608497c02SMartin Blapp 	}
58708497c02SMartin Blapp 
58808497c02SMartin Blapp 	n =  rstrm->readit(rstrm->tcp_handle,
58908497c02SMartin Blapp 	    rstrm->in_base + rstrm->in_received,
59008497c02SMartin Blapp 	    (rstrm->in_reclen - rstrm->in_received));
59108497c02SMartin Blapp 
59208497c02SMartin Blapp 	if (n < 0) {
59308497c02SMartin Blapp 		*statp = XPRT_DIED;
59408497c02SMartin Blapp 		return FALSE;
59508497c02SMartin Blapp 	}
59608497c02SMartin Blapp 
59708497c02SMartin Blapp 	if (n == 0) {
59808497c02SMartin Blapp 		*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
59908497c02SMartin Blapp 		return FALSE;
60008497c02SMartin Blapp 	}
60108497c02SMartin Blapp 
60208497c02SMartin Blapp 	rstrm->in_received += n;
60308497c02SMartin Blapp 
60408497c02SMartin Blapp 	if (rstrm->in_received == rstrm->in_reclen) {
60508497c02SMartin Blapp 		rstrm->in_haveheader = FALSE;
60608497c02SMartin Blapp 		rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
60708497c02SMartin Blapp 		rstrm->in_hdrlen = 0;
60808497c02SMartin Blapp 		if (rstrm->last_frag) {
60908497c02SMartin Blapp 			rstrm->fbtbc = rstrm->in_reclen;
61008497c02SMartin Blapp 			rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
61108497c02SMartin Blapp 			rstrm->in_finger = rstrm->in_base;
61240525d3dSMartin Blapp 			rstrm->in_reclen = rstrm->in_received = 0;
61308497c02SMartin Blapp 			*statp = XPRT_MOREREQS;
61408497c02SMartin Blapp 			return TRUE;
61508497c02SMartin Blapp 		}
61608497c02SMartin Blapp 	}
61708497c02SMartin Blapp 
61808497c02SMartin Blapp 	*statp = XPRT_MOREREQS;
61908497c02SMartin Blapp 	return FALSE;
62008497c02SMartin Blapp }
62108497c02SMartin Blapp 
62208497c02SMartin Blapp bool_t
62308497c02SMartin Blapp __xdrrec_setnonblock(xdrs, maxrec)
62408497c02SMartin Blapp 	XDR *xdrs;
62508497c02SMartin Blapp 	int maxrec;
62608497c02SMartin Blapp {
62708497c02SMartin Blapp 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
62808497c02SMartin Blapp 
62908497c02SMartin Blapp 	rstrm->nonblock = TRUE;
63008497c02SMartin Blapp 	if (maxrec == 0)
63108497c02SMartin Blapp 		maxrec = rstrm->recvsize;
63208497c02SMartin Blapp 	rstrm->in_maxrec = maxrec;
63308497c02SMartin Blapp 	return TRUE;
63408497c02SMartin Blapp }
635eae561b3SGarrett Wollman 
636eae561b3SGarrett Wollman /*
637eae561b3SGarrett Wollman  * Internal useful routines
638eae561b3SGarrett Wollman  */
639eae561b3SGarrett Wollman static bool_t
640eae561b3SGarrett Wollman flush_out(rstrm, eor)
6418360efbdSAlfred Perlstein 	RECSTREAM *rstrm;
642eae561b3SGarrett Wollman 	bool_t eor;
643eae561b3SGarrett Wollman {
6448360efbdSAlfred Perlstein 	u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
6458360efbdSAlfred Perlstein 	u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
6468360efbdSAlfred Perlstein 		(u_long)(rstrm->frag_header) - sizeof(u_int32_t));
647eae561b3SGarrett Wollman 
648eae561b3SGarrett Wollman 	*(rstrm->frag_header) = htonl(len | eormask);
6498360efbdSAlfred Perlstein 	len = (u_int32_t)((u_long)(rstrm->out_finger) -
6508360efbdSAlfred Perlstein 	    (u_long)(rstrm->out_base));
651eae561b3SGarrett Wollman 	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
652eae561b3SGarrett Wollman 		!= (int)len)
653eae561b3SGarrett Wollman 		return (FALSE);
6548360efbdSAlfred Perlstein 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
6558360efbdSAlfred Perlstein 	rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
656eae561b3SGarrett Wollman 	return (TRUE);
657eae561b3SGarrett Wollman }
658eae561b3SGarrett Wollman 
659eae561b3SGarrett Wollman static bool_t  /* knows nothing about records!  Only about input buffers */
660eae561b3SGarrett Wollman fill_input_buf(rstrm)
6618360efbdSAlfred Perlstein 	RECSTREAM *rstrm;
662eae561b3SGarrett Wollman {
6638360efbdSAlfred Perlstein 	char *where;
6648360efbdSAlfred Perlstein 	u_int32_t i;
6658360efbdSAlfred Perlstein 	int len;
666eae561b3SGarrett Wollman 
66708497c02SMartin Blapp 	if (rstrm->nonblock)
66808497c02SMartin Blapp 		return FALSE;
66908497c02SMartin Blapp 
670eae561b3SGarrett Wollman 	where = rstrm->in_base;
6718360efbdSAlfred Perlstein 	i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
672eae561b3SGarrett Wollman 	where += i;
6738360efbdSAlfred Perlstein 	len = (u_int32_t)(rstrm->in_size - i);
674eae561b3SGarrett Wollman 	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
675eae561b3SGarrett Wollman 		return (FALSE);
676eae561b3SGarrett Wollman 	rstrm->in_finger = where;
677eae561b3SGarrett Wollman 	where += len;
678eae561b3SGarrett Wollman 	rstrm->in_boundry = where;
679eae561b3SGarrett Wollman 	return (TRUE);
680eae561b3SGarrett Wollman }
681eae561b3SGarrett Wollman 
682eae561b3SGarrett Wollman static bool_t  /* knows nothing about records!  Only about input buffers */
683eae561b3SGarrett Wollman get_input_bytes(rstrm, addr, len)
6848360efbdSAlfred Perlstein 	RECSTREAM *rstrm;
6858360efbdSAlfred Perlstein 	char *addr;
6868360efbdSAlfred Perlstein 	int len;
687eae561b3SGarrett Wollman {
6888360efbdSAlfred Perlstein 	size_t current;
689eae561b3SGarrett Wollman 
69040525d3dSMartin Blapp 	if (rstrm->nonblock) {
69140525d3dSMartin Blapp 		if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
69240525d3dSMartin Blapp 			return FALSE;
69340525d3dSMartin Blapp 		memcpy(addr, rstrm->in_finger, (size_t)len);
69440525d3dSMartin Blapp 		rstrm->in_finger += len;
69540525d3dSMartin Blapp 		return TRUE;
69640525d3dSMartin Blapp 	}
69740525d3dSMartin Blapp 
698eae561b3SGarrett Wollman 	while (len > 0) {
6998360efbdSAlfred Perlstein 		current = (size_t)((long)rstrm->in_boundry -
7008360efbdSAlfred Perlstein 		    (long)rstrm->in_finger);
701eae561b3SGarrett Wollman 		if (current == 0) {
702eae561b3SGarrett Wollman 			if (! fill_input_buf(rstrm))
703eae561b3SGarrett Wollman 				return (FALSE);
704eae561b3SGarrett Wollman 			continue;
705eae561b3SGarrett Wollman 		}
706eae561b3SGarrett Wollman 		current = (len < current) ? len : current;
7078360efbdSAlfred Perlstein 		memmove(addr, rstrm->in_finger, current);
708eae561b3SGarrett Wollman 		rstrm->in_finger += current;
709eae561b3SGarrett Wollman 		addr += current;
710eae561b3SGarrett Wollman 		len -= current;
711eae561b3SGarrett Wollman 	}
712eae561b3SGarrett Wollman 	return (TRUE);
713eae561b3SGarrett Wollman }
714eae561b3SGarrett Wollman 
715eae561b3SGarrett Wollman static bool_t  /* next two bytes of the input stream are treated as a header */
716eae561b3SGarrett Wollman set_input_fragment(rstrm)
7178360efbdSAlfred Perlstein 	RECSTREAM *rstrm;
718eae561b3SGarrett Wollman {
7191ad08a09SPeter Wemm 	u_int32_t header;
720eae561b3SGarrett Wollman 
7218360efbdSAlfred Perlstein 	if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
722eae561b3SGarrett Wollman 		return (FALSE);
7238360efbdSAlfred Perlstein 	header = ntohl(header);
724eae561b3SGarrett Wollman 	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
725a9352e90SBill Paul 	/*
726a9352e90SBill Paul 	 * Sanity check. Try not to accept wildly incorrect
7271ce4aec2SBill Paul 	 * record sizes. Unfortunately, the only record size
7281ce4aec2SBill Paul 	 * we can positively identify as being 'wildly incorrect'
7291ce4aec2SBill Paul 	 * is zero. Ridiculously large record sizes may look wrong,
7301ce4aec2SBill Paul 	 * but we don't have any way to be certain that they aren't
7311ce4aec2SBill Paul 	 * what the client actually intended to send us.
732a9352e90SBill Paul 	 */
733c50a9e8fSBill Paul 	if (header == 0)
734a9352e90SBill Paul 		return(FALSE);
735eae561b3SGarrett Wollman 	rstrm->fbtbc = header & (~LAST_FRAG);
736eae561b3SGarrett Wollman 	return (TRUE);
737eae561b3SGarrett Wollman }
738eae561b3SGarrett Wollman 
739eae561b3SGarrett Wollman static bool_t  /* consumes input bytes; knows nothing about records! */
740eae561b3SGarrett Wollman skip_input_bytes(rstrm, cnt)
7418360efbdSAlfred Perlstein 	RECSTREAM *rstrm;
742eae561b3SGarrett Wollman 	long cnt;
743eae561b3SGarrett Wollman {
7448360efbdSAlfred Perlstein 	u_int32_t current;
745eae561b3SGarrett Wollman 
746eae561b3SGarrett Wollman 	while (cnt > 0) {
7478360efbdSAlfred Perlstein 		current = (size_t)((long)rstrm->in_boundry -
7488360efbdSAlfred Perlstein 		    (long)rstrm->in_finger);
749eae561b3SGarrett Wollman 		if (current == 0) {
750eae561b3SGarrett Wollman 			if (! fill_input_buf(rstrm))
751eae561b3SGarrett Wollman 				return (FALSE);
752eae561b3SGarrett Wollman 			continue;
753eae561b3SGarrett Wollman 		}
7548360efbdSAlfred Perlstein 		current = (u_int32_t)((cnt < current) ? cnt : current);
755eae561b3SGarrett Wollman 		rstrm->in_finger += current;
756eae561b3SGarrett Wollman 		cnt -= current;
757eae561b3SGarrett Wollman 	}
758eae561b3SGarrett Wollman 	return (TRUE);
759eae561b3SGarrett Wollman }
760eae561b3SGarrett Wollman 
761eae561b3SGarrett Wollman static u_int
762eae561b3SGarrett Wollman fix_buf_size(s)
7638360efbdSAlfred Perlstein 	u_int s;
764eae561b3SGarrett Wollman {
765eae561b3SGarrett Wollman 
766eae561b3SGarrett Wollman 	if (s < 100)
767eae561b3SGarrett Wollman 		s = 4000;
768eae561b3SGarrett Wollman 	return (RNDUP(s));
769eae561b3SGarrett Wollman }
77008497c02SMartin Blapp 
77108497c02SMartin Blapp /*
77208497c02SMartin Blapp  * Reallocate the input buffer for a non-block stream.
77308497c02SMartin Blapp  */
77408497c02SMartin Blapp static bool_t
77508497c02SMartin Blapp realloc_stream(rstrm, size)
77608497c02SMartin Blapp 	RECSTREAM *rstrm;
77708497c02SMartin Blapp 	int size;
77808497c02SMartin Blapp {
77908497c02SMartin Blapp 	ptrdiff_t diff;
78008497c02SMartin Blapp 	char *buf;
78108497c02SMartin Blapp 
78208497c02SMartin Blapp 	if (size > rstrm->recvsize) {
78308497c02SMartin Blapp 		buf = realloc(rstrm->in_base, (size_t)size);
78408497c02SMartin Blapp 		if (buf == NULL)
78508497c02SMartin Blapp 			return FALSE;
78608497c02SMartin Blapp 		diff = buf - rstrm->in_base;
78708497c02SMartin Blapp 		rstrm->in_finger += diff;
78808497c02SMartin Blapp 		rstrm->in_base = buf;
78908497c02SMartin Blapp 		rstrm->in_boundry = buf + size;
79008497c02SMartin Blapp 		rstrm->recvsize = size;
79108497c02SMartin Blapp 		rstrm->in_size = size;
79208497c02SMartin Blapp 	}
79308497c02SMartin Blapp 
79408497c02SMartin Blapp 	return TRUE;
79508497c02SMartin Blapp }
796