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