* sysdeps/m68k/setjmp.c: Also define setjmp and _setjmp if
[glibc.git] / sunrpc / xdr_rec.c
blob941afa35de0a53a11591674394fa88fbae1537eb
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
31 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
32 * layer above tcp (for rpc's use).
34 * Copyright (C) 1984, Sun Microsystems, Inc.
36 * These routines interface XDRSTREAMS to a tcp/ip connection.
37 * There is a record marking layer between the xdr stream
38 * and the tcp transport level. A record is composed on one or more
39 * record fragments. A record fragment is a thirty-two bit header followed
40 * by n bytes of data, where n is contained in the header. The header
41 * is represented as a htonl(u_long). The high order bit encodes
42 * whether or not the fragment is the last fragment of the record
43 * (1 => fragment is last, 0 => more fragments to follow.
44 * The other 31 bits encode the byte length of the fragment.
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <rpc/rpc.h>
51 #include <libintl.h>
53 #ifdef USE_IN_LIBIO
54 # include <wchar.h>
55 # include <libio/iolibio.h>
56 # define fputs(s, f) _IO_fputs (s, f)
57 #endif
59 static bool_t xdrrec_getlong (XDR *, long *);
60 static bool_t xdrrec_putlong (XDR *, const long *);
61 static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
62 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
63 static u_int xdrrec_getpos (const XDR *);
64 static bool_t xdrrec_setpos (XDR *, u_int);
65 static int32_t *xdrrec_inline (XDR *, int);
66 static void xdrrec_destroy (XDR *);
67 static bool_t xdrrec_getint32 (XDR *, int32_t *);
68 static bool_t xdrrec_putint32 (XDR *, const int32_t *);
70 static const struct xdr_ops xdrrec_ops = {
71 xdrrec_getlong,
72 xdrrec_putlong,
73 xdrrec_getbytes,
74 xdrrec_putbytes,
75 xdrrec_getpos,
76 xdrrec_setpos,
77 xdrrec_inline,
78 xdrrec_destroy,
79 xdrrec_getint32,
80 xdrrec_putint32
84 * A record is composed of one or more record fragments.
85 * A record fragment is a two-byte header followed by zero to
86 * 2**32-1 bytes. The header is treated as a long unsigned and is
87 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
88 * are a byte count of the fragment. The highest order bit is a boolean:
89 * 1 => this fragment is the last fragment of the record,
90 * 0 => this fragment is followed by more fragment(s).
92 * The fragment/record machinery is not general; it is constructed to
93 * meet the needs of xdr and rpc based on tcp.
96 #define LAST_FRAG (1UL << 31)
98 typedef struct rec_strm
100 caddr_t tcp_handle;
101 caddr_t the_buffer;
103 * out-going bits
105 int (*writeit) (char *, char *, int);
106 caddr_t out_base; /* output buffer (points to frag header) */
107 caddr_t out_finger; /* next output position */
108 caddr_t out_boundry; /* data cannot up to this address */
109 u_int32_t *frag_header; /* beginning of curren fragment */
110 bool_t frag_sent; /* true if buffer sent in middle of record */
112 * in-coming bits
114 int (*readit) (char *, char *, int);
115 u_long in_size; /* fixed size of the input buffer */
116 caddr_t in_base;
117 caddr_t in_finger; /* location of next byte to be had */
118 caddr_t in_boundry; /* can read up to this location */
119 long fbtbc; /* fragment bytes to be consumed */
120 bool_t last_frag;
121 u_int sendsize;
122 u_int recvsize;
124 RECSTREAM;
126 static u_int fix_buf_size (u_int) internal_function;
127 static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
128 static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
129 static bool_t set_input_fragment (RECSTREAM *) internal_function;
130 static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
133 * Create an xdr handle for xdrrec
134 * xdrrec_create fills in xdrs. Sendsize and recvsize are
135 * send and recv buffer sizes (0 => use default).
136 * tcp_handle is an opaque handle that is passed as the first parameter to
137 * the procedures readit and writeit. Readit and writeit are read and
138 * write respectively. They are like the system
139 * calls expect that they take an opaque handle rather than an fd.
141 void
142 xdrrec_create (XDR *xdrs, u_int sendsize,
143 u_int recvsize, caddr_t tcp_handle,
144 int (*readit) (char *, char *, int),
145 int (*writeit) (char *, char *, int))
147 RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
148 caddr_t tmp;
149 char *buf;
151 sendsize = fix_buf_size (sendsize);
152 recvsize = fix_buf_size (recvsize);
153 buf = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
155 if (rstrm == NULL || buf == NULL)
157 #ifdef USE_IN_LIBIO
158 if (_IO_fwide (stderr, 0) > 0)
159 (void) __fwprintf (stderr, L"%s", _("xdrrec_create: out of memory\n"));
160 else
161 #endif
162 (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
163 mem_free (rstrm, sizeof (RECSTREAM));
164 mem_free (buf, sendsize + recvsize + BYTES_PER_XDR_UNIT);
166 * This is bad. Should rework xdrrec_create to
167 * return a handle, and in this case return NULL
169 return;
172 * adjust sizes and allocate buffer quad byte aligned
174 rstrm->sendsize = sendsize;
175 rstrm->recvsize = recvsize;
176 rstrm->the_buffer = buf;
177 tmp = rstrm->the_buffer;
178 if ((size_t)tmp % BYTES_PER_XDR_UNIT)
179 tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
180 rstrm->out_base = tmp;
181 rstrm->in_base = tmp + sendsize;
183 * now the rest ...
185 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
186 is not `const'. */
187 xdrs->x_ops = (struct xdr_ops *) &xdrrec_ops;
188 xdrs->x_private = (caddr_t) rstrm;
189 rstrm->tcp_handle = tcp_handle;
190 rstrm->readit = readit;
191 rstrm->writeit = writeit;
192 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
193 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
194 rstrm->out_finger += 4;
195 rstrm->out_boundry += sendsize;
196 rstrm->frag_sent = FALSE;
197 rstrm->in_size = recvsize;
198 rstrm->in_boundry = rstrm->in_base;
199 rstrm->in_finger = (rstrm->in_boundry += recvsize);
200 rstrm->fbtbc = 0;
201 rstrm->last_frag = TRUE;
206 * The routines defined below are the xdr ops which will go into the
207 * xdr handle filled in by xdrrec_create.
210 static bool_t
211 xdrrec_getlong (XDR *xdrs, long *lp)
213 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
214 int32_t *buflp = (int32_t *) rstrm->in_finger;
215 int32_t mylong;
217 /* first try the inline, fast case */
218 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
219 rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
221 *lp = (int32_t) ntohl (*buflp);
222 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
223 rstrm->in_finger += BYTES_PER_XDR_UNIT;
225 else
227 if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
228 BYTES_PER_XDR_UNIT))
229 return FALSE;
230 *lp = (int32_t) ntohl (mylong);
232 return TRUE;
235 static bool_t
236 xdrrec_putlong (XDR *xdrs, const long *lp)
238 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
239 int32_t *dest_lp = (int32_t *) rstrm->out_finger;
241 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
244 * this case should almost never happen so the code is
245 * inefficient
247 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
248 rstrm->frag_sent = TRUE;
249 if (!flush_out (rstrm, FALSE))
250 return FALSE;
251 dest_lp = (int32_t *) rstrm->out_finger;
252 rstrm->out_finger += BYTES_PER_XDR_UNIT;
254 *dest_lp = htonl (*lp);
255 return TRUE;
258 static bool_t /* must manage buffers, fragments, and records */
259 xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
261 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
262 u_int current;
264 while (len > 0)
266 current = rstrm->fbtbc;
267 if (current == 0)
269 if (rstrm->last_frag)
270 return FALSE;
271 if (!set_input_fragment (rstrm))
272 return FALSE;
273 continue;
275 current = (len < current) ? len : current;
276 if (!get_input_bytes (rstrm, addr, current))
277 return FALSE;
278 addr += current;
279 rstrm->fbtbc -= current;
280 len -= current;
282 return TRUE;
285 static bool_t
286 xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
288 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
289 u_int current;
291 while (len > 0)
293 current = rstrm->out_boundry - rstrm->out_finger;
294 current = (len < current) ? len : current;
295 memcpy (rstrm->out_finger, addr, current);
296 rstrm->out_finger += current;
297 addr += current;
298 len -= current;
299 if (rstrm->out_finger == rstrm->out_boundry && len > 0)
301 rstrm->frag_sent = TRUE;
302 if (!flush_out (rstrm, FALSE))
303 return FALSE;
306 return TRUE;
309 static u_int
310 xdrrec_getpos (const XDR *xdrs)
312 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
313 long pos;
315 pos = __lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
316 if (pos != -1)
317 switch (xdrs->x_op)
320 case XDR_ENCODE:
321 pos += rstrm->out_finger - rstrm->out_base;
322 break;
324 case XDR_DECODE:
325 pos -= rstrm->in_boundry - rstrm->in_finger;
326 break;
328 default:
329 pos = (u_int) - 1;
330 break;
332 return (u_int) pos;
335 static bool_t
336 xdrrec_setpos (XDR *xdrs, u_int pos)
338 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
339 u_int currpos = xdrrec_getpos (xdrs);
340 int delta = currpos - pos;
341 caddr_t newpos;
343 if ((int) currpos != -1)
344 switch (xdrs->x_op)
347 case XDR_ENCODE:
348 newpos = rstrm->out_finger - delta;
349 if (newpos > (caddr_t) rstrm->frag_header &&
350 newpos < rstrm->out_boundry)
352 rstrm->out_finger = newpos;
353 return TRUE;
355 break;
357 case XDR_DECODE:
358 newpos = rstrm->in_finger - delta;
359 if ((delta < (int) (rstrm->fbtbc)) &&
360 (newpos <= rstrm->in_boundry) &&
361 (newpos >= rstrm->in_base))
363 rstrm->in_finger = newpos;
364 rstrm->fbtbc -= delta;
365 return TRUE;
367 break;
369 default:
370 break;
372 return FALSE;
375 static int32_t *
376 xdrrec_inline (XDR *xdrs, int len)
378 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
379 int32_t *buf = NULL;
381 switch (xdrs->x_op)
384 case XDR_ENCODE:
385 if ((rstrm->out_finger + len) <= rstrm->out_boundry)
387 buf = (int32_t *) rstrm->out_finger;
388 rstrm->out_finger += len;
390 break;
392 case XDR_DECODE:
393 if ((len <= rstrm->fbtbc) &&
394 ((rstrm->in_finger + len) <= rstrm->in_boundry))
396 buf = (int32_t *) rstrm->in_finger;
397 rstrm->fbtbc -= len;
398 rstrm->in_finger += len;
400 break;
402 default:
403 break;
405 return buf;
408 static void
409 xdrrec_destroy (XDR *xdrs)
411 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
413 mem_free (rstrm->the_buffer,
414 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
415 mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
418 static bool_t
419 xdrrec_getint32 (XDR *xdrs, int32_t *ip)
421 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
422 int32_t *bufip = (int32_t *) rstrm->in_finger;
423 int32_t mylong;
425 /* first try the inline, fast case */
426 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
427 rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
429 *ip = ntohl (*bufip);
430 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
431 rstrm->in_finger += BYTES_PER_XDR_UNIT;
433 else
435 if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
436 BYTES_PER_XDR_UNIT))
437 return FALSE;
438 *ip = ntohl (mylong);
440 return TRUE;
443 static bool_t
444 xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
446 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
447 int32_t *dest_ip = (int32_t *) rstrm->out_finger;
449 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
452 * this case should almost never happen so the code is
453 * inefficient
455 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
456 rstrm->frag_sent = TRUE;
457 if (!flush_out (rstrm, FALSE))
458 return FALSE;
459 dest_ip = (int32_t *) rstrm->out_finger;
460 rstrm->out_finger += BYTES_PER_XDR_UNIT;
462 *dest_ip = htonl (*ip);
463 return TRUE;
467 * Exported routines to manage xdr records
471 * Before reading (deserializing from the stream, one should always call
472 * this procedure to guarantee proper record alignment.
474 bool_t
475 xdrrec_skiprecord (XDR *xdrs)
477 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
479 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
481 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
482 return FALSE;
483 rstrm->fbtbc = 0;
484 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
485 return FALSE;
487 rstrm->last_frag = FALSE;
488 return TRUE;
492 * Lookahead function.
493 * Returns TRUE iff there is no more input in the buffer
494 * after consuming the rest of the current record.
496 bool_t
497 xdrrec_eof (XDR *xdrs)
499 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
501 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
503 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
504 return TRUE;
505 rstrm->fbtbc = 0;
506 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
507 return TRUE;
509 if (rstrm->in_finger == rstrm->in_boundry)
510 return TRUE;
511 return FALSE;
515 * The client must tell the package when an end-of-record has occurred.
516 * The second parameter tells whether the record should be flushed to the
517 * (output) tcp stream. (This lets the package support batched or
518 * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
520 bool_t
521 xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
523 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
524 u_long len; /* fragment length */
526 if (sendnow || rstrm->frag_sent
527 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
529 rstrm->frag_sent = FALSE;
530 return flush_out (rstrm, TRUE);
532 len = (rstrm->out_finger - (char *) rstrm->frag_header
533 - BYTES_PER_XDR_UNIT);
534 *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
535 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
536 rstrm->out_finger += BYTES_PER_XDR_UNIT;
537 return TRUE;
542 * Internal useful routines
544 static bool_t
545 internal_function
546 flush_out (RECSTREAM *rstrm, bool_t eor)
548 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
549 u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
550 - BYTES_PER_XDR_UNIT);
552 *rstrm->frag_header = htonl (len | eormask);
553 len = rstrm->out_finger - rstrm->out_base;
554 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
555 != (int) len)
556 return FALSE;
557 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
558 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
559 return TRUE;
562 static bool_t /* knows nothing about records! Only about input buffers */
563 fill_input_buf (RECSTREAM *rstrm)
565 caddr_t where;
566 size_t i;
567 int len;
569 where = rstrm->in_base;
570 i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
571 where += i;
572 len = rstrm->in_size - i;
573 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
574 return FALSE;
575 rstrm->in_finger = where;
576 where += len;
577 rstrm->in_boundry = where;
578 return TRUE;
581 static bool_t /* knows nothing about records! Only about input buffers */
582 internal_function
583 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
585 int current;
587 while (len > 0)
589 current = rstrm->in_boundry - rstrm->in_finger;
590 if (current == 0)
592 if (!fill_input_buf (rstrm))
593 return FALSE;
594 continue;
596 current = (len < current) ? len : current;
597 memcpy (addr, rstrm->in_finger, current);
598 rstrm->in_finger += current;
599 addr += current;
600 len -= current;
602 return TRUE;
605 static bool_t /* next two bytes of the input stream are treated as a header */
606 internal_function
607 set_input_fragment (RECSTREAM *rstrm)
609 uint32_t header;
611 if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
612 return FALSE;
613 header = ntohl (header);
614 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
616 * Sanity check. Try not to accept wildly incorrect fragment
617 * sizes. Unfortunately, only a size of zero can be identified as
618 * 'wildely incorrect', and this only, if it is not the last
619 * fragment of a message. Ridiculously large fragment sizes may look
620 * wrong, but we don't have any way to be certain that they aren't
621 * what the client actually intended to send us. Many existing RPC
622 * implementations may sent a fragment of size zero as the last
623 * fragment of a message.
625 if (header == 0)
626 return FALSE;
627 rstrm->fbtbc = header & ~LAST_FRAG;
628 return TRUE;
631 static bool_t /* consumes input bytes; knows nothing about records! */
632 internal_function
633 skip_input_bytes (RECSTREAM *rstrm, long cnt)
635 int current;
637 while (cnt > 0)
639 current = rstrm->in_boundry - rstrm->in_finger;
640 if (current == 0)
642 if (!fill_input_buf (rstrm))
643 return FALSE;
644 continue;
646 current = (cnt < current) ? cnt : current;
647 rstrm->in_finger += current;
648 cnt -= current;
650 return TRUE;
653 static u_int
654 internal_function
655 fix_buf_size (u_int s)
657 if (s < 100)
658 s = 4000;
659 return RNDUP (s);