Home | History | Annotate | Line # | Download | only in rpc
xdr_rec.c revision 1.14
      1  1.14   mycroft /*	$NetBSD: xdr_rec.c,v 1.14 1998/07/26 12:47:39 mycroft Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5   1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6   1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7   1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8   1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9   1.1       cgd  * program developed by the user.
     10   1.1       cgd  *
     11   1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12   1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13   1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14   1.1       cgd  *
     15   1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16   1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17   1.1       cgd  * modification or enhancement.
     18   1.1       cgd  *
     19   1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20   1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21   1.1       cgd  * OR ANY PART THEREOF.
     22   1.1       cgd  *
     23   1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24   1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25   1.1       cgd  * Sun has been advised of the possibility of such damages.
     26   1.1       cgd  *
     27   1.1       cgd  * Sun Microsystems, Inc.
     28   1.1       cgd  * 2550 Garcia Avenue
     29   1.1       cgd  * Mountain View, California  94043
     30   1.1       cgd  */
     31   1.7  christos 
     32   1.7  christos #include <sys/cdefs.h>
     33   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34   1.7  christos #if 0
     35   1.7  christos static char *sccsid = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
     36   1.7  christos static char *sccsid = "@(#)xdr_rec.c	2.2 88/08/01 4.0 RPCSRC";
     37   1.7  christos #else
     38  1.14   mycroft __RCSID("$NetBSD: xdr_rec.c,v 1.14 1998/07/26 12:47:39 mycroft Exp $");
     39   1.7  christos #endif
     40   1.1       cgd #endif
     41   1.1       cgd 
     42   1.1       cgd /*
     43   1.1       cgd  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
     44   1.1       cgd  * layer above tcp (for rpc's use).
     45   1.1       cgd  *
     46   1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     47   1.1       cgd  *
     48   1.1       cgd  * These routines interface XDRSTREAMS to a tcp/ip connection.
     49   1.1       cgd  * There is a record marking layer between the xdr stream
     50   1.1       cgd  * and the tcp transport level.  A record is composed on one or more
     51   1.1       cgd  * record fragments.  A record fragment is a thirty-two bit header followed
     52   1.1       cgd  * by n bytes of data, where n is contained in the header.  The header
     53  1.11     lukem  * is represented as a htonl(u_long).  Thegh order bit encodes
     54   1.1       cgd  * whether or not the fragment is the last fragment of the record
     55   1.1       cgd  * (1 => fragment is last, 0 => more fragments to follow.
     56   1.1       cgd  * The other 31 bits encode the byte length of the fragment.
     57   1.1       cgd  */
     58   1.1       cgd 
     59   1.8       jtc #include "namespace.h"
     60  1.12     lukem 
     61  1.12     lukem #include <sys/types.h>
     62  1.12     lukem 
     63  1.12     lukem #include <netinet/in.h>
     64  1.12     lukem 
     65  1.12     lukem #include <err.h>
     66   1.1       cgd #include <stdio.h>
     67   1.1       cgd #include <stdlib.h>
     68   1.6       cgd #include <string.h>
     69  1.12     lukem 
     70   1.1       cgd #include <rpc/types.h>
     71   1.1       cgd #include <rpc/xdr.h>
     72   1.8       jtc 
     73   1.8       jtc #ifdef __weak_alias
     74   1.8       jtc __weak_alias(xdrrec_create,_xdrrec_create);
     75   1.8       jtc __weak_alias(xdrrec_endofrecord,_xdrrec_endofrecord);
     76   1.8       jtc __weak_alias(xdrrec_eof,_xdrrec_eof);
     77   1.8       jtc __weak_alias(xdrrec_skiprecord,_xdrrec_skiprecord);
     78   1.8       jtc #endif
     79   1.1       cgd 
     80  1.11     lukem static bool_t	xdrrec_getlong __P((XDR *, long *));
     81  1.14   mycroft static bool_t	xdrrec_putlong __P((XDR *, const long *));
     82  1.14   mycroft static bool_t	xdrrec_getbytes __P((XDR *, char *, u_int));
     83  1.11     lukem 
     84  1.14   mycroft static bool_t	xdrrec_putbytes __P((XDR *, const char *, u_int));
     85  1.11     lukem static u_int	xdrrec_getpos __P((XDR *));
     86  1.11     lukem static bool_t	xdrrec_setpos __P((XDR *, u_int));
     87  1.11     lukem static int32_t *xdrrec_inline __P((XDR *, u_int));
     88   1.6       cgd static void	xdrrec_destroy __P((XDR *));
     89   1.1       cgd 
     90  1.13   mycroft static const struct  xdr_ops xdrrec_ops = {
     91   1.1       cgd 	xdrrec_getlong,
     92   1.1       cgd 	xdrrec_putlong,
     93   1.1       cgd 	xdrrec_getbytes,
     94   1.1       cgd 	xdrrec_putbytes,
     95   1.1       cgd 	xdrrec_getpos,
     96   1.1       cgd 	xdrrec_setpos,
     97   1.1       cgd 	xdrrec_inline,
     98   1.1       cgd 	xdrrec_destroy
     99   1.1       cgd };
    100   1.1       cgd 
    101   1.1       cgd /*
    102   1.1       cgd  * A record is composed of one or more record fragments.
    103   1.1       cgd  * A record fragment is a two-byte header followed by zero to
    104   1.1       cgd  * 2**32-1 bytes.  The header is treated as a long unsigned and is
    105   1.1       cgd  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
    106   1.1       cgd  * are a byte count of the fragment.  The highest order bit is a boolean:
    107   1.1       cgd  * 1 => this fragment is the last fragment of the record,
    108   1.1       cgd  * 0 => this fragment is followed by more fragment(s).
    109   1.1       cgd  *
    110   1.1       cgd  * The fragment/record machinery is not general;  it is constructed to
    111   1.1       cgd  * meet the needs of xdr and rpc based on tcp.
    112   1.1       cgd  */
    113   1.1       cgd 
    114   1.4       cgd #define LAST_FRAG ((u_int32_t)(1 << 31))
    115   1.1       cgd 
    116   1.1       cgd typedef struct rec_strm {
    117  1.14   mycroft 	char *tcp_handle;
    118  1.14   mycroft 	char *the_buffer;
    119   1.1       cgd 	/*
    120   1.1       cgd 	 * out-goung bits
    121   1.1       cgd 	 */
    122  1.14   mycroft 	int (*writeit) __P((char *, char *, int));
    123  1.14   mycroft 	char *out_base;	/* output buffer (points to frag header) */
    124  1.14   mycroft 	char *out_finger;	/* next output position */
    125  1.14   mycroft 	char *out_boundry;	/* data cannot up to this address */
    126  1.11     lukem 	u_int32_t *frag_header;	/* beginning of curren fragment */
    127   1.1       cgd 	bool_t frag_sent;	/* true if buffer sent in middle of record */
    128   1.1       cgd 	/*
    129   1.1       cgd 	 * in-coming bits
    130   1.1       cgd 	 */
    131  1.14   mycroft 	int (*readit) __P((char *, char *, int));
    132  1.11     lukem 	u_long in_size;	/* fixed size of the input buffer */
    133  1.14   mycroft 	char *in_base;
    134  1.14   mycroft 	char *in_finger;	/* location of next byte to be had */
    135  1.14   mycroft 	char *in_boundry;	/* can read up to this location */
    136  1.11     lukem 	long fbtbc;		/* fragment bytes to be consumed */
    137   1.1       cgd 	bool_t last_frag;
    138  1.11     lukem 	u_int sendsize;
    139  1.11     lukem 	u_int recvsize;
    140   1.1       cgd } RECSTREAM;
    141   1.1       cgd 
    142  1.11     lukem static u_int	fix_buf_size __P((u_int));
    143   1.6       cgd static bool_t	flush_out __P((RECSTREAM *, bool_t));
    144   1.7  christos static bool_t	fill_input_buf __P((RECSTREAM *));
    145  1.14   mycroft static bool_t	get_input_bytes __P((RECSTREAM *, char *, int));
    146   1.6       cgd static bool_t	set_input_fragment __P((RECSTREAM *));
    147  1.11     lukem static bool_t	skip_input_bytes __P((RECSTREAM *, long));
    148   1.6       cgd 
    149   1.1       cgd 
    150   1.1       cgd /*
    151   1.1       cgd  * Create an xdr handle for xdrrec
    152   1.1       cgd  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
    153   1.1       cgd  * send and recv buffer sizes (0 => use default).
    154   1.1       cgd  * tcp_handle is an opaque handle that is passed as the first parameter to
    155   1.1       cgd  * the procedures readit and writeit.  Readit and writeit are read and
    156   1.1       cgd  * write respectively.   They are like the system
    157   1.1       cgd  * calls expect that they take an opaque handle rather than an fd.
    158   1.1       cgd  */
    159   1.1       cgd void
    160   1.1       cgd xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
    161  1.12     lukem 	XDR *xdrs;
    162  1.12     lukem 	u_int sendsize;
    163  1.12     lukem 	u_int recvsize;
    164  1.14   mycroft 	char *tcp_handle;
    165  1.11     lukem 	/* like read, but pass it a tcp_handle, not sock */
    166  1.14   mycroft 	int (*readit) __P((char *, char *, int));
    167  1.11     lukem 	/* like write, but pass it a tcp_handle, not sock */
    168  1.14   mycroft 	int (*writeit) __P((char *, char *, int));
    169   1.1       cgd {
    170  1.12     lukem 	RECSTREAM *rstrm =
    171   1.1       cgd 		(RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
    172   1.1       cgd 
    173   1.1       cgd 	if (rstrm == NULL) {
    174  1.12     lukem 		warnx("xdrrec_create: out of memory");
    175   1.1       cgd 		/*
    176   1.1       cgd 		 *  This is bad.  Should rework xdrrec_create to
    177   1.1       cgd 		 *  return a handle, and in this case return NULL
    178   1.1       cgd 		 */
    179   1.1       cgd 		return;
    180   1.1       cgd 	}
    181   1.1       cgd 	/*
    182   1.1       cgd 	 * adjust sizes and allocate buffer quad byte aligned
    183   1.1       cgd 	 */
    184   1.1       cgd 	rstrm->sendsize = sendsize = fix_buf_size(sendsize);
    185   1.1       cgd 	rstrm->recvsize = recvsize = fix_buf_size(recvsize);
    186  1.11     lukem 	rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
    187   1.1       cgd 	if (rstrm->the_buffer == NULL) {
    188  1.12     lukem 		warnx("xdrrec_create: out of memory");
    189   1.1       cgd 		return;
    190   1.1       cgd 	}
    191   1.1       cgd 	for (rstrm->out_base = rstrm->the_buffer;
    192  1.11     lukem 		(u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
    193   1.1       cgd 		rstrm->out_base++);
    194   1.1       cgd 	rstrm->in_base = rstrm->out_base + sendsize;
    195   1.1       cgd 	/*
    196   1.1       cgd 	 * now the rest ...
    197   1.1       cgd 	 */
    198   1.1       cgd 	xdrs->x_ops = &xdrrec_ops;
    199  1.14   mycroft 	xdrs->x_private = (char *)rstrm;
    200   1.1       cgd 	rstrm->tcp_handle = tcp_handle;
    201   1.1       cgd 	rstrm->readit = readit;
    202   1.1       cgd 	rstrm->writeit = writeit;
    203   1.1       cgd 	rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
    204   1.4       cgd 	rstrm->frag_header = (u_int32_t *)rstrm->out_base;
    205   1.4       cgd 	rstrm->out_finger += sizeof(u_int32_t);
    206   1.1       cgd 	rstrm->out_boundry += sendsize;
    207   1.1       cgd 	rstrm->frag_sent = FALSE;
    208   1.1       cgd 	rstrm->in_size = recvsize;
    209   1.1       cgd 	rstrm->in_boundry = rstrm->in_base;
    210   1.1       cgd 	rstrm->in_finger = (rstrm->in_boundry += recvsize);
    211   1.1       cgd 	rstrm->fbtbc = 0;
    212   1.1       cgd 	rstrm->last_frag = TRUE;
    213   1.1       cgd }
    214   1.1       cgd 
    215   1.1       cgd 
    216   1.1       cgd /*
    217  1.11     lukem  * The reoutines defined below are the xdr ops which will go into the
    218   1.1       cgd  * xdr handle filled in by xdrrec_create.
    219   1.1       cgd  */
    220   1.1       cgd 
    221   1.1       cgd static bool_t
    222   1.1       cgd xdrrec_getlong(xdrs, lp)
    223   1.1       cgd 	XDR *xdrs;
    224  1.11     lukem 	long *lp;
    225   1.1       cgd {
    226  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    227  1.12     lukem 	int32_t *buflp = (int32_t *)(rstrm->in_finger);
    228   1.4       cgd 	int32_t mylong;
    229   1.1       cgd 
    230   1.1       cgd 	/* first try the inline, fast case */
    231  1.11     lukem 	if ((rstrm->fbtbc >= sizeof(int32_t)) &&
    232  1.11     lukem 		(((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
    233  1.11     lukem 		*lp = (long)ntohl((u_int32_t)(*buflp));
    234   1.4       cgd 		rstrm->fbtbc -= sizeof(int32_t);
    235   1.4       cgd 		rstrm->in_finger += sizeof(int32_t);
    236   1.1       cgd 	} else {
    237  1.14   mycroft 		if (! xdrrec_getbytes(xdrs, (char *)&mylong, sizeof(int32_t)))
    238   1.1       cgd 			return (FALSE);
    239  1.11     lukem 		*lp = (long)ntohl((u_int32_t)mylong);
    240   1.1       cgd 	}
    241   1.1       cgd 	return (TRUE);
    242   1.1       cgd }
    243   1.1       cgd 
    244   1.1       cgd static bool_t
    245   1.1       cgd xdrrec_putlong(xdrs, lp)
    246   1.1       cgd 	XDR *xdrs;
    247  1.14   mycroft 	const long *lp;
    248   1.1       cgd {
    249  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    250  1.12     lukem 	int32_t *dest_lp = ((int32_t *)(rstrm->out_finger));
    251   1.1       cgd 
    252   1.4       cgd 	if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
    253   1.1       cgd 		/*
    254   1.1       cgd 		 * this case should almost never happen so the code is
    255   1.1       cgd 		 * inefficient
    256   1.1       cgd 		 */
    257   1.4       cgd 		rstrm->out_finger -= sizeof(int32_t);
    258   1.1       cgd 		rstrm->frag_sent = TRUE;
    259   1.1       cgd 		if (! flush_out(rstrm, FALSE))
    260   1.1       cgd 			return (FALSE);
    261   1.4       cgd 		dest_lp = ((int32_t *)(rstrm->out_finger));
    262   1.4       cgd 		rstrm->out_finger += sizeof(int32_t);
    263   1.1       cgd 	}
    264   1.4       cgd 	*dest_lp = (int32_t)htonl((u_int32_t)(*lp));
    265   1.1       cgd 	return (TRUE);
    266   1.1       cgd }
    267   1.1       cgd 
    268   1.1       cgd static bool_t  /* must manage buffers, fragments, and records */
    269   1.1       cgd xdrrec_getbytes(xdrs, addr, len)
    270   1.1       cgd 	XDR *xdrs;
    271  1.14   mycroft 	char *addr;
    272  1.12     lukem 	u_int len;
    273   1.1       cgd {
    274  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    275  1.12     lukem 	int current;
    276   1.1       cgd 
    277   1.1       cgd 	while (len > 0) {
    278   1.1       cgd 		current = rstrm->fbtbc;
    279   1.1       cgd 		if (current == 0) {
    280   1.1       cgd 			if (rstrm->last_frag)
    281   1.1       cgd 				return (FALSE);
    282   1.1       cgd 			if (! set_input_fragment(rstrm))
    283   1.1       cgd 				return (FALSE);
    284   1.1       cgd 			continue;
    285   1.1       cgd 		}
    286   1.1       cgd 		current = (len < current) ? len : current;
    287   1.1       cgd 		if (! get_input_bytes(rstrm, addr, current))
    288   1.1       cgd 			return (FALSE);
    289   1.1       cgd 		addr += current;
    290   1.1       cgd 		rstrm->fbtbc -= current;
    291   1.1       cgd 		len -= current;
    292   1.1       cgd 	}
    293   1.1       cgd 	return (TRUE);
    294   1.1       cgd }
    295   1.1       cgd 
    296   1.1       cgd static bool_t
    297   1.1       cgd xdrrec_putbytes(xdrs, addr, len)
    298   1.1       cgd 	XDR *xdrs;
    299  1.14   mycroft 	const char *addr;
    300  1.12     lukem 	u_int len;
    301   1.1       cgd {
    302  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    303  1.12     lukem 	long current;
    304   1.1       cgd 
    305   1.1       cgd 	while (len > 0) {
    306  1.11     lukem 		current = (u_long)rstrm->out_boundry -
    307  1.11     lukem 		    (u_long)rstrm->out_finger;
    308   1.1       cgd 		current = (len < current) ? len : current;
    309  1.12     lukem 		memmove(rstrm->out_finger, addr, current);
    310   1.1       cgd 		rstrm->out_finger += current;
    311   1.1       cgd 		addr += current;
    312   1.1       cgd 		len -= current;
    313   1.1       cgd 		if (rstrm->out_finger == rstrm->out_boundry) {
    314   1.1       cgd 			rstrm->frag_sent = TRUE;
    315   1.1       cgd 			if (! flush_out(rstrm, FALSE))
    316   1.1       cgd 				return (FALSE);
    317   1.1       cgd 		}
    318   1.1       cgd 	}
    319   1.1       cgd 	return (TRUE);
    320   1.1       cgd }
    321   1.1       cgd 
    322  1.11     lukem static u_int
    323   1.1       cgd xdrrec_getpos(xdrs)
    324  1.12     lukem 	XDR *xdrs;
    325   1.1       cgd {
    326  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    327  1.12     lukem 	long pos;
    328   1.1       cgd 
    329  1.11     lukem 	pos = lseek((off_t)(long)rstrm->tcp_handle, 0, 1);
    330   1.1       cgd 	if (pos != -1)
    331   1.1       cgd 		switch (xdrs->x_op) {
    332   1.1       cgd 
    333   1.1       cgd 		case XDR_ENCODE:
    334   1.1       cgd 			pos += rstrm->out_finger - rstrm->out_base;
    335   1.1       cgd 			break;
    336   1.1       cgd 
    337   1.1       cgd 		case XDR_DECODE:
    338   1.1       cgd 			pos -= rstrm->in_boundry - rstrm->in_finger;
    339   1.1       cgd 			break;
    340   1.1       cgd 
    341   1.1       cgd 		default:
    342  1.11     lukem 			pos = (u_int) -1;
    343   1.1       cgd 			break;
    344   1.1       cgd 		}
    345  1.11     lukem 	return ((u_int) pos);
    346   1.1       cgd }
    347   1.1       cgd 
    348   1.1       cgd static bool_t
    349   1.1       cgd xdrrec_setpos(xdrs, pos)
    350  1.12     lukem 	XDR *xdrs;
    351  1.11     lukem 	u_int pos;
    352   1.1       cgd {
    353  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    354  1.11     lukem 	u_int currpos = xdrrec_getpos(xdrs);
    355   1.1       cgd 	int delta = currpos - pos;
    356  1.14   mycroft 	char *newpos;
    357   1.1       cgd 
    358   1.1       cgd 	if ((int)currpos != -1)
    359   1.1       cgd 		switch (xdrs->x_op) {
    360   1.1       cgd 
    361   1.1       cgd 		case XDR_ENCODE:
    362   1.1       cgd 			newpos = rstrm->out_finger - delta;
    363  1.14   mycroft 			if ((newpos > (char *)(rstrm->frag_header)) &&
    364   1.1       cgd 				(newpos < rstrm->out_boundry)) {
    365   1.1       cgd 				rstrm->out_finger = newpos;
    366   1.1       cgd 				return (TRUE);
    367   1.1       cgd 			}
    368   1.1       cgd 			break;
    369   1.1       cgd 
    370   1.1       cgd 		case XDR_DECODE:
    371   1.1       cgd 			newpos = rstrm->in_finger - delta;
    372   1.1       cgd 			if ((delta < (int)(rstrm->fbtbc)) &&
    373   1.1       cgd 				(newpos <= rstrm->in_boundry) &&
    374   1.1       cgd 				(newpos >= rstrm->in_base)) {
    375   1.1       cgd 				rstrm->in_finger = newpos;
    376   1.1       cgd 				rstrm->fbtbc -= delta;
    377   1.1       cgd 				return (TRUE);
    378   1.1       cgd 			}
    379   1.1       cgd 			break;
    380   1.7  christos 
    381   1.7  christos 		case XDR_FREE:
    382   1.7  christos 			break;
    383   1.1       cgd 		}
    384   1.1       cgd 	return (FALSE);
    385   1.1       cgd }
    386   1.1       cgd 
    387   1.4       cgd static int32_t *
    388   1.1       cgd xdrrec_inline(xdrs, len)
    389  1.12     lukem 	XDR *xdrs;
    390  1.11     lukem 	u_int len;
    391   1.1       cgd {
    392  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    393   1.4       cgd 	int32_t *buf = NULL;
    394   1.1       cgd 
    395   1.1       cgd 	switch (xdrs->x_op) {
    396   1.1       cgd 
    397   1.1       cgd 	case XDR_ENCODE:
    398   1.1       cgd 		if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
    399   1.4       cgd 			buf = (int32_t *) rstrm->out_finger;
    400   1.1       cgd 			rstrm->out_finger += len;
    401   1.1       cgd 		}
    402   1.1       cgd 		break;
    403   1.1       cgd 
    404   1.1       cgd 	case XDR_DECODE:
    405   1.1       cgd 		if ((len <= rstrm->fbtbc) &&
    406   1.1       cgd 			((rstrm->in_finger + len) <= rstrm->in_boundry)) {
    407   1.4       cgd 			buf = (int32_t *) rstrm->in_finger;
    408   1.1       cgd 			rstrm->fbtbc -= len;
    409   1.1       cgd 			rstrm->in_finger += len;
    410   1.1       cgd 		}
    411   1.7  christos 		break;
    412   1.7  christos 
    413   1.7  christos 	case XDR_FREE:
    414   1.1       cgd 		break;
    415   1.1       cgd 	}
    416   1.1       cgd 	return (buf);
    417   1.1       cgd }
    418   1.1       cgd 
    419   1.1       cgd static void
    420   1.1       cgd xdrrec_destroy(xdrs)
    421  1.12     lukem 	XDR *xdrs;
    422   1.1       cgd {
    423  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    424   1.1       cgd 
    425   1.1       cgd 	mem_free(rstrm->the_buffer,
    426   1.1       cgd 		rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
    427  1.14   mycroft 	mem_free((char *)rstrm, sizeof(RECSTREAM));
    428   1.1       cgd }
    429   1.1       cgd 
    430   1.1       cgd 
    431   1.1       cgd /*
    432   1.1       cgd  * Exported routines to manage xdr records
    433   1.1       cgd  */
    434   1.1       cgd 
    435   1.1       cgd /*
    436   1.1       cgd  * Before reading (deserializing from the stream, one should always call
    437   1.1       cgd  * this procedure to guarantee proper record alignment.
    438   1.1       cgd  */
    439   1.1       cgd bool_t
    440   1.1       cgd xdrrec_skiprecord(xdrs)
    441   1.1       cgd 	XDR *xdrs;
    442   1.1       cgd {
    443  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    444   1.1       cgd 
    445   1.1       cgd 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
    446   1.1       cgd 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
    447   1.1       cgd 			return (FALSE);
    448   1.1       cgd 		rstrm->fbtbc = 0;
    449   1.1       cgd 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
    450   1.1       cgd 			return (FALSE);
    451   1.1       cgd 	}
    452   1.1       cgd 	rstrm->last_frag = FALSE;
    453   1.1       cgd 	return (TRUE);
    454   1.1       cgd }
    455   1.1       cgd 
    456   1.1       cgd /*
    457   1.1       cgd  * Look ahead fuction.
    458   1.1       cgd  * Returns TRUE iff there is no more input in the buffer
    459   1.1       cgd  * after consuming the rest of the current record.
    460   1.1       cgd  */
    461   1.1       cgd bool_t
    462   1.1       cgd xdrrec_eof(xdrs)
    463   1.1       cgd 	XDR *xdrs;
    464   1.1       cgd {
    465  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    466   1.1       cgd 
    467   1.1       cgd 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
    468   1.1       cgd 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
    469   1.1       cgd 			return (TRUE);
    470   1.1       cgd 		rstrm->fbtbc = 0;
    471   1.1       cgd 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
    472   1.1       cgd 			return (TRUE);
    473   1.1       cgd 	}
    474   1.1       cgd 	if (rstrm->in_finger == rstrm->in_boundry)
    475   1.1       cgd 		return (TRUE);
    476   1.1       cgd 	return (FALSE);
    477   1.1       cgd }
    478   1.1       cgd 
    479   1.1       cgd /*
    480   1.1       cgd  * The client must tell the package when an end-of-record has occurred.
    481   1.1       cgd  * The second paraemters tells whether the record should be flushed to the
    482   1.1       cgd  * (output) tcp stream.  (This let's the package support batched or
    483   1.1       cgd  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
    484   1.1       cgd  */
    485   1.1       cgd bool_t
    486   1.1       cgd xdrrec_endofrecord(xdrs, sendnow)
    487   1.1       cgd 	XDR *xdrs;
    488   1.1       cgd 	bool_t sendnow;
    489   1.1       cgd {
    490  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    491  1.12     lukem 	u_long len;  /* fragment length */
    492   1.1       cgd 
    493   1.1       cgd 	if (sendnow || rstrm->frag_sent ||
    494  1.11     lukem 		((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
    495  1.11     lukem 		(u_long)rstrm->out_boundry)) {
    496   1.1       cgd 		rstrm->frag_sent = FALSE;
    497   1.1       cgd 		return (flush_out(rstrm, TRUE));
    498   1.1       cgd 	}
    499  1.11     lukem 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
    500   1.4       cgd 	   sizeof(u_int32_t);
    501  1.11     lukem 	*(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
    502   1.4       cgd 	rstrm->frag_header = (u_int32_t *)rstrm->out_finger;
    503   1.4       cgd 	rstrm->out_finger += sizeof(u_int32_t);
    504   1.1       cgd 	return (TRUE);
    505   1.1       cgd }
    506   1.1       cgd 
    507   1.1       cgd 
    508   1.1       cgd /*
    509   1.1       cgd  * Internal useful routines
    510   1.1       cgd  */
    511   1.1       cgd static bool_t
    512   1.1       cgd flush_out(rstrm, eor)
    513  1.12     lukem 	RECSTREAM *rstrm;
    514   1.1       cgd 	bool_t eor;
    515   1.1       cgd {
    516  1.12     lukem 	u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
    517  1.12     lukem 	u_int32_t len = (u_long)(rstrm->out_finger) -
    518   1.4       cgd 		(u_long)(rstrm->frag_header) - sizeof(u_int32_t);
    519   1.1       cgd 
    520   1.1       cgd 	*(rstrm->frag_header) = htonl(len | eormask);
    521   1.1       cgd 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
    522   1.1       cgd 	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
    523   1.1       cgd 		!= (int)len)
    524   1.1       cgd 		return (FALSE);
    525   1.4       cgd 	rstrm->frag_header = (u_int32_t *)rstrm->out_base;
    526  1.14   mycroft 	rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
    527   1.1       cgd 	return (TRUE);
    528   1.1       cgd }
    529   1.1       cgd 
    530   1.1       cgd static bool_t  /* knows nothing about records!  Only about input buffers */
    531   1.1       cgd fill_input_buf(rstrm)
    532  1.12     lukem 	RECSTREAM *rstrm;
    533   1.1       cgd {
    534  1.14   mycroft 	char *where;
    535  1.11     lukem 	u_long i;
    536  1.12     lukem 	long len;
    537   1.1       cgd 
    538   1.1       cgd 	where = rstrm->in_base;
    539  1.11     lukem 	i = (u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
    540   1.1       cgd 	where += i;
    541   1.1       cgd 	len = rstrm->in_size - i;
    542   1.1       cgd 	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
    543   1.1       cgd 		return (FALSE);
    544   1.1       cgd 	rstrm->in_finger = where;
    545   1.1       cgd 	where += len;
    546   1.1       cgd 	rstrm->in_boundry = where;
    547   1.1       cgd 	return (TRUE);
    548   1.1       cgd }
    549   1.1       cgd 
    550   1.1       cgd static bool_t  /* knows nothing about records!  Only about input buffers */
    551   1.1       cgd get_input_bytes(rstrm, addr, len)
    552  1.12     lukem 	RECSTREAM *rstrm;
    553  1.14   mycroft 	char *addr;
    554  1.12     lukem 	int len;
    555   1.1       cgd {
    556  1.12     lukem 	long current;
    557   1.1       cgd 
    558   1.1       cgd 	while (len > 0) {
    559   1.4       cgd 		current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
    560   1.1       cgd 		if (current == 0) {
    561   1.1       cgd 			if (! fill_input_buf(rstrm))
    562   1.1       cgd 				return (FALSE);
    563   1.1       cgd 			continue;
    564   1.1       cgd 		}
    565   1.1       cgd 		current = (len < current) ? len : current;
    566  1.12     lukem 		memmove(addr, rstrm->in_finger, current);
    567   1.1       cgd 		rstrm->in_finger += current;
    568   1.1       cgd 		addr += current;
    569   1.1       cgd 		len -= current;
    570   1.1       cgd 	}
    571   1.1       cgd 	return (TRUE);
    572   1.1       cgd }
    573   1.1       cgd 
    574   1.1       cgd static bool_t  /* next two bytes of the input stream are treated as a header */
    575   1.1       cgd set_input_fragment(rstrm)
    576  1.12     lukem 	RECSTREAM *rstrm;
    577   1.1       cgd {
    578   1.4       cgd 	u_int32_t header;
    579   1.1       cgd 
    580  1.14   mycroft 	if (! get_input_bytes(rstrm, (char *)&header, sizeof(header)))
    581   1.1       cgd 		return (FALSE);
    582   1.1       cgd 	header = (long)ntohl(header);
    583   1.1       cgd 	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
    584   1.1       cgd 	rstrm->fbtbc = header & (~LAST_FRAG);
    585   1.1       cgd 	return (TRUE);
    586   1.1       cgd }
    587   1.1       cgd 
    588   1.1       cgd static bool_t  /* consumes input bytes; knows nothing about records! */
    589   1.1       cgd skip_input_bytes(rstrm, cnt)
    590  1.12     lukem 	RECSTREAM *rstrm;
    591  1.11     lukem 	long cnt;
    592   1.1       cgd {
    593  1.12     lukem 	long current;
    594   1.1       cgd 
    595   1.1       cgd 	while (cnt > 0) {
    596  1.11     lukem 		current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
    597   1.1       cgd 		if (current == 0) {
    598   1.1       cgd 			if (! fill_input_buf(rstrm))
    599   1.1       cgd 				return (FALSE);
    600   1.1       cgd 			continue;
    601   1.1       cgd 		}
    602   1.1       cgd 		current = (cnt < current) ? cnt : current;
    603   1.1       cgd 		rstrm->in_finger += current;
    604   1.1       cgd 		cnt -= current;
    605   1.1       cgd 	}
    606   1.1       cgd 	return (TRUE);
    607   1.1       cgd }
    608   1.1       cgd 
    609  1.11     lukem static u_int
    610   1.1       cgd fix_buf_size(s)
    611  1.12     lukem 	u_int s;
    612   1.1       cgd {
    613   1.1       cgd 
    614   1.1       cgd 	if (s < 100)
    615   1.1       cgd 		s = 4000;
    616   1.1       cgd 	return (RNDUP(s));
    617   1.1       cgd }
    618