Correct data for isw* functions.
[glibc/history.git] / sunrpc / xdr_rec.c
blob6b33208d76a9df29d8e58acc8da23c271ee7d3d3
1 /* @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
32 #endif
35 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
36 * layer above tcp (for rpc's use).
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * These routines interface XDRSTREAMS to a tcp/ip connection.
41 * There is a record marking layer between the xdr stream
42 * and the tcp transport level. A record is composed on one or more
43 * record fragments. A record fragment is a thirty-two bit header followed
44 * by n bytes of data, where n is contained in the header. The header
45 * is represented as a htonl(u_long). The high order bit encodes
46 * whether or not the fragment is the last fragment of the record
47 * (1 => fragment is last, 0 => more fragments to follow.
48 * The other 31 bits encode the byte length of the fragment.
51 #include <stdio.h>
52 #include <rpc/types.h>
53 #include <rpc/xdr.h>
54 #include <netinet/in.h>
56 extern long lseek();
58 static u_int fix_buf_size();
60 static bool_t xdrrec_getlong();
61 static bool_t xdrrec_putlong();
62 static bool_t xdrrec_getbytes();
63 static bool_t xdrrec_putbytes();
64 static u_int xdrrec_getpos();
65 static bool_t xdrrec_setpos();
66 static long * xdrrec_inline();
67 static void xdrrec_destroy();
69 static struct xdr_ops xdrrec_ops = {
70 xdrrec_getlong,
71 xdrrec_putlong,
72 xdrrec_getbytes,
73 xdrrec_putbytes,
74 xdrrec_getpos,
75 xdrrec_setpos,
76 xdrrec_inline,
77 xdrrec_destroy
81 * A record is composed of one or more record fragments.
82 * A record fragment is a two-byte header followed by zero to
83 * 2**32-1 bytes. The header is treated as a long unsigned and is
84 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
85 * are a byte count of the fragment. The highest order bit is a boolean:
86 * 1 => this fragment is the last fragment of the record,
87 * 0 => this fragment is followed by more fragment(s).
89 * The fragment/record machinery is not general; it is constructed to
90 * meet the needs of xdr and rpc based on tcp.
93 #define LAST_FRAG (1UL << 31)
95 typedef struct rec_strm {
96 caddr_t tcp_handle;
97 caddr_t the_buffer;
99 * out-going bits
101 int (*writeit)();
102 caddr_t out_base; /* output buffer (points to frag header) */
103 caddr_t out_finger; /* next output position */
104 caddr_t out_boundry; /* data cannot up to this address */
105 u_int32_t *frag_header; /* beginning of curren fragment */
106 bool_t frag_sent; /* true if buffer sent in middle of record */
108 * in-coming bits
110 int (*readit)();
111 u_long in_size; /* fixed size of the input buffer */
112 caddr_t in_base;
113 caddr_t in_finger; /* location of next byte to be had */
114 caddr_t in_boundry; /* can read up to this location */
115 long fbtbc; /* fragment bytes to be consumed */
116 bool_t last_frag;
117 u_int sendsize;
118 u_int recvsize;
119 } RECSTREAM;
123 * Create an xdr handle for xdrrec
124 * xdrrec_create fills in xdrs. Sendsize and recvsize are
125 * send and recv buffer sizes (0 => use default).
126 * tcp_handle is an opaque handle that is passed as the first parameter to
127 * the procedures readit and writeit. Readit and writeit are read and
128 * write respectively. They are like the system
129 * calls expect that they take an opaque handle rather than an fd.
131 void
132 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
133 register XDR *xdrs;
134 register u_int sendsize;
135 register u_int recvsize;
136 caddr_t tcp_handle;
137 int (*readit)(); /* like read, but pass it a tcp_handle, not sock */
138 int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */
140 register RECSTREAM *rstrm =
141 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
143 if (rstrm == NULL) {
144 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
146 * This is bad. Should rework xdrrec_create to
147 * return a handle, and in this case return NULL
149 return;
152 * adjust sizes and allocate buffer quad byte aligned
154 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
155 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
156 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
157 if (rstrm->the_buffer == NULL) {
158 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
159 return;
161 for (rstrm->out_base = rstrm->the_buffer;
162 (u_int)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
163 rstrm->out_base++);
164 rstrm->in_base = rstrm->out_base + sendsize;
166 * now the rest ...
168 xdrs->x_ops = &xdrrec_ops;
169 xdrs->x_private = (caddr_t)rstrm;
170 rstrm->tcp_handle = tcp_handle;
171 rstrm->readit = readit;
172 rstrm->writeit = writeit;
173 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
174 rstrm->frag_header = (u_int32_t *)rstrm->out_base;
175 rstrm->out_finger += 4;
176 rstrm->out_boundry += sendsize;
177 rstrm->frag_sent = FALSE;
178 rstrm->in_size = recvsize;
179 rstrm->in_boundry = rstrm->in_base;
180 rstrm->in_finger = (rstrm->in_boundry += recvsize);
181 rstrm->fbtbc = 0;
182 rstrm->last_frag = TRUE;
187 * The routines defined below are the xdr ops which will go into the
188 * xdr handle filled in by xdrrec_create.
191 static bool_t
192 xdrrec_getlong(xdrs, lp)
193 XDR *xdrs;
194 long *lp;
196 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
197 register int32_t *buflp = (int32_t *) rstrm->in_finger;
198 int32_t mylong;
200 /* first try the inline, fast case */
201 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
202 rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
204 *lp = (int32_t) ntohl(*buflp);
205 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
206 rstrm->in_finger += BYTES_PER_XDR_UNIT;
207 } else {
208 if (! xdrrec_getbytes(xdrs, (caddr_t) &mylong,
209 BYTES_PER_XDR_UNIT))
210 return FALSE;
211 *lp = (int32_t) ntohl(mylong);
213 return TRUE;
216 static bool_t
217 xdrrec_putlong(xdrs, lp)
218 XDR *xdrs;
219 long *lp;
221 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
222 register int32_t *dest_lp = (int32_t *) rstrm->out_finger;
224 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry) {
226 * this case should almost never happen so the code is
227 * inefficient
229 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
230 rstrm->frag_sent = TRUE;
231 if (! flush_out(rstrm, FALSE))
232 return FALSE;
233 dest_lp = (int32_t *) rstrm->out_finger;
234 rstrm->out_finger += BYTES_PER_XDR_UNIT;
236 *dest_lp = htonl(*lp);
237 return TRUE;
240 static bool_t /* must manage buffers, fragments, and records */
241 xdrrec_getbytes(xdrs, addr, len)
242 XDR *xdrs;
243 register caddr_t addr;
244 register u_int len;
246 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
247 register int current;
249 while (len > 0) {
250 current = rstrm->fbtbc;
251 if (current == 0) {
252 if (rstrm->last_frag)
253 return FALSE;
254 if (! set_input_fragment(rstrm))
255 return FALSE;
256 continue;
258 current = (len < current) ? len : current;
259 if (! get_input_bytes(rstrm, addr, current))
260 return FALSE;
261 addr += current;
262 rstrm->fbtbc -= current;
263 len -= current;
265 return TRUE;
268 static bool_t
269 xdrrec_putbytes(xdrs, addr, len)
270 XDR *xdrs;
271 register caddr_t addr;
272 register u_int len;
274 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
275 register int current;
277 while (len > 0) {
278 current = rstrm->out_boundry - rstrm->out_finger;
279 current = (len < current) ? len : current;
280 bcopy(addr, rstrm->out_finger, current);
281 rstrm->out_finger += current;
282 addr += current;
283 len -= current;
284 if (rstrm->out_finger == rstrm->out_boundry) {
285 rstrm->frag_sent = TRUE;
286 if (! flush_out(rstrm, FALSE))
287 return FALSE;
290 return TRUE;
293 static u_int
294 xdrrec_getpos(xdrs)
295 register XDR *xdrs;
297 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
298 register long pos;
300 pos = lseek((int)rstrm->tcp_handle, (long) 0, 1);
301 if (pos != -1)
302 switch (xdrs->x_op) {
304 case XDR_ENCODE:
305 pos += rstrm->out_finger - rstrm->out_base;
306 break;
308 case XDR_DECODE:
309 pos -= rstrm->in_boundry - rstrm->in_finger;
310 break;
312 default:
313 pos = (u_int) -1;
314 break;
316 return ((u_int) pos);
319 static bool_t
320 xdrrec_setpos(xdrs, pos)
321 register XDR *xdrs;
322 u_int pos;
324 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
325 u_int currpos = xdrrec_getpos(xdrs);
326 int delta = currpos - pos;
327 caddr_t newpos;
329 if ((int)currpos != -1)
330 switch (xdrs->x_op) {
332 case XDR_ENCODE:
333 newpos = rstrm->out_finger - delta;
334 if (newpos > (caddr_t) rstrm->frag_header &&
335 newpos < rstrm->out_boundry)
337 rstrm->out_finger = newpos;
338 return TRUE;
340 break;
342 case XDR_DECODE:
343 newpos = rstrm->in_finger - delta;
344 if ((delta < (int)(rstrm->fbtbc)) &&
345 (newpos <= rstrm->in_boundry) &&
346 (newpos >= rstrm->in_base)) {
347 rstrm->in_finger = newpos;
348 rstrm->fbtbc -= delta;
349 return TRUE;
351 break;
353 return FALSE;
356 static long *
357 xdrrec_inline(xdrs, len)
358 register XDR *xdrs;
359 int len;
361 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
362 long * buf = NULL;
364 switch (xdrs->x_op) {
366 case XDR_ENCODE:
367 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
368 buf = (long *) rstrm->out_finger;
369 rstrm->out_finger += len;
371 break;
373 case XDR_DECODE:
374 if ((len <= rstrm->fbtbc) &&
375 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
376 buf = (long *) rstrm->in_finger;
377 rstrm->fbtbc -= len;
378 rstrm->in_finger += len;
380 break;
382 return buf;
385 static void
386 xdrrec_destroy(xdrs)
387 register XDR *xdrs;
389 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
391 mem_free(rstrm->the_buffer,
392 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
393 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
398 * Exported routines to manage xdr records
402 * Before reading (deserializing from the stream, one should always call
403 * this procedure to guarantee proper record alignment.
405 bool_t
406 xdrrec_skiprecord(xdrs)
407 XDR *xdrs;
409 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
411 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
412 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
413 return FALSE;
414 rstrm->fbtbc = 0;
415 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
416 return FALSE;
418 rstrm->last_frag = FALSE;
419 return TRUE;
423 * Lookahead function.
424 * Returns TRUE iff there is no more input in the buffer
425 * after consuming the rest of the current record.
427 bool_t
428 xdrrec_eof(xdrs)
429 XDR *xdrs;
431 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
433 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
434 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
435 return TRUE;
436 rstrm->fbtbc = 0;
437 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
438 return TRUE;
440 if (rstrm->in_finger == rstrm->in_boundry)
441 return TRUE;
442 return FALSE;
446 * The client must tell the package when an end-of-record has occurred.
447 * The second parameter tells whether the record should be flushed to the
448 * (output) tcp stream. (This lets the package support batched or
449 * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
451 bool_t
452 xdrrec_endofrecord(xdrs, sendnow)
453 XDR *xdrs;
454 bool_t sendnow;
456 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
457 register u_long len; /* fragment length */
459 if (sendnow || rstrm->frag_sent
460 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
462 rstrm->frag_sent = FALSE;
463 return flush_out(rstrm, TRUE);
465 len = (rstrm->out_finger - (char *) rstrm->frag_header
466 - BYTES_PER_XDR_UNIT);
467 *rstrm->frag_header = htonl((u_long)len | LAST_FRAG);
468 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
469 rstrm->out_finger += BYTES_PER_XDR_UNIT;
470 return TRUE;
475 * Internal useful routines
477 static bool_t
478 flush_out(rstrm, eor)
479 register RECSTREAM *rstrm;
480 bool_t eor;
482 register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
483 register u_long len = (rstrm->out_finger
484 - (char *) rstrm->frag_header
485 - BYTES_PER_XDR_UNIT);
487 *rstrm->frag_header = htonl(len | eormask);
488 len = rstrm->out_finger - rstrm->out_base;
489 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
490 != (int)len)
491 return FALSE;
492 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
493 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
494 return TRUE;
497 static bool_t /* knows nothing about records! Only about input buffers */
498 fill_input_buf(rstrm)
499 register RECSTREAM *rstrm;
501 register caddr_t where;
502 u_int i;
503 register int len;
505 where = rstrm->in_base;
506 i = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
507 where += i;
508 len = rstrm->in_size - i;
509 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
510 return FALSE;
511 rstrm->in_finger = where;
512 where += len;
513 rstrm->in_boundry = where;
514 return TRUE;
517 static bool_t /* knows nothing about records! Only about input buffers */
518 get_input_bytes(rstrm, addr, len)
519 register RECSTREAM *rstrm;
520 register caddr_t addr;
521 register int len;
523 register int current;
525 while (len > 0) {
526 current = rstrm->in_boundry - rstrm->in_finger;
527 if (current == 0) {
528 if (! fill_input_buf(rstrm))
529 return FALSE;
530 continue;
532 current = (len < current) ? len : current;
533 bcopy(rstrm->in_finger, addr, current);
534 rstrm->in_finger += current;
535 addr += current;
536 len -= current;
538 return TRUE;
541 static bool_t /* next two bytes of the input stream are treated as a header */
542 set_input_fragment(rstrm)
543 register RECSTREAM *rstrm;
545 u_long header;
547 if (! get_input_bytes(rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
548 return FALSE;
549 header = ntohl(header);
550 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
551 rstrm->fbtbc = header & ~LAST_FRAG;
552 return TRUE;
555 static bool_t /* consumes input bytes; knows nothing about records! */
556 skip_input_bytes(rstrm, cnt)
557 register RECSTREAM *rstrm;
558 long cnt;
560 register int current;
562 while (cnt > 0) {
563 current = rstrm->in_boundry - rstrm->in_finger;
564 if (current == 0) {
565 if (! fill_input_buf(rstrm))
566 return FALSE;
567 continue;
569 current = (cnt < current) ? cnt : current;
570 rstrm->in_finger += current;
571 cnt -= current;
573 return TRUE;
576 static u_int
577 fix_buf_size(s)
578 register u_int s;
581 if (s < 100)
582 s = 4000;
583 return RNDUP(s);