]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sunrpc/xdr_rec.c
Sync "language", "lang_name", "territory", "country_name" with CLDR/langtable
[thirdparty/glibc.git] / sunrpc / xdr_rec.c
index 4809589d07216382272e62f2981d7bd6ebc9775e..c9a1eaad6349ba5f374b8f2880ff7b08f5ab37a7 100644 (file)
@@ -1,37 +1,35 @@
 /*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part.  Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
+ * layer above tcp (for rpc's use).
  *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
+ * Copyright (c) 2010, Oracle America, Inc.
  *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
  *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials
+ *       provided with the distribution.
+ *     * Neither the name of the "Oracle America, Inc." nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
  *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California  94043
- */
-
-/*
- * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
- * layer above tcp (for rpc's use).
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * These routines interface XDRSTREAMS to a tcp/ip connection.
  * There is a record marking layer between the xdr stream
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <stdint.h>
 #include <rpc/rpc.h>
-
-#ifdef USE_IN_LIBIO
-# include <libio/iolibio.h>
-# define fputs(s, f) _IO_fputs (s, f)
-#endif
+#include <libintl.h>
+#include <wchar.h>
+#include <libio/iolibio.h>
+#include <shlib-compat.h>
 
 static bool_t xdrrec_getlong (XDR *, long *);
 static bool_t xdrrec_putlong (XDR *, const long *);
@@ -60,13 +58,12 @@ static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
 static u_int xdrrec_getpos (const XDR *);
 static bool_t xdrrec_setpos (XDR *, u_int);
-static int32_t *xdrrec_inline (XDR *, int);
+static int32_t *xdrrec_inline (XDR *, u_int);
 static void xdrrec_destroy (XDR *);
 static bool_t xdrrec_getint32 (XDR *, int32_t *);
 static bool_t xdrrec_putint32 (XDR *, const int32_t *);
 
-static const struct xdr_ops xdrrec_ops =
-{
+static const struct xdr_ops xdrrec_ops = {
   xdrrec_getlong,
   xdrrec_putlong,
   xdrrec_getbytes,
@@ -105,7 +102,7 @@ typedef struct rec_strm
     caddr_t out_base;          /* output buffer (points to frag header) */
     caddr_t out_finger;                /* next output position */
     caddr_t out_boundry;       /* data cannot up to this address */
-    u_int32_t *frag_header;    /* beginning of curren fragment */
+    uint32_t *frag_header;     /* beginning of curren fragment */
     bool_t frag_sent;          /* true if buffer sent in middle of record */
     /*
      * in-coming bits
@@ -122,11 +119,11 @@ typedef struct rec_strm
   }
 RECSTREAM;
 
-static u_int fix_buf_size (u_int) internal_function;
-static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
-static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
-static bool_t set_input_fragment (RECSTREAM *) internal_function;
-static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
+static u_int fix_buf_size (u_int);
+static bool_t skip_input_bytes (RECSTREAM *, long);
+static bool_t flush_out (RECSTREAM *, bool_t);
+static bool_t set_input_fragment (RECSTREAM *);
+static bool_t get_input_bytes (RECSTREAM *, caddr_t, int);
 
 /*
  * Create an xdr handle for xdrrec
@@ -145,10 +142,17 @@ xdrrec_create (XDR *xdrs, u_int sendsize,
 {
   RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
   caddr_t tmp;
+  char *buf;
+
+  sendsize = fix_buf_size (sendsize);
+  recvsize = fix_buf_size (recvsize);
+  buf = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
 
-  if (rstrm == NULL)
+  if (rstrm == NULL || buf == NULL)
     {
-      (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
+      (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
+      mem_free (rstrm, sizeof (RECSTREAM));
+      mem_free (buf, sendsize + recvsize + BYTES_PER_XDR_UNIT);
       /*
        *  This is bad.  Should rework xdrrec_create to
        *  return a handle, and in this case return NULL
@@ -158,14 +162,9 @@ xdrrec_create (XDR *xdrs, u_int sendsize,
   /*
    * adjust sizes and allocate buffer quad byte aligned
    */
-  rstrm->sendsize = sendsize = fix_buf_size (sendsize);
-  rstrm->recvsize = recvsize = fix_buf_size (recvsize);
-  rstrm->the_buffer = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
-  if (rstrm->the_buffer == NULL)
-    {
-      (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
-      return;
-    }
+  rstrm->sendsize = sendsize;
+  rstrm->recvsize = recvsize;
+  rstrm->the_buffer = buf;
   tmp = rstrm->the_buffer;
   if ((size_t)tmp % BYTES_PER_XDR_UNIT)
     tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
@@ -174,7 +173,7 @@ xdrrec_create (XDR *xdrs, u_int sendsize,
   /*
    * now the rest ...
    */
-  /* We have to add the const since the `struct xdr_ops' in `struct XDR'
+  /* We have to add the cast since the `struct xdr_ops' in `struct XDR'
      is not `const'.  */
   xdrs->x_ops = (struct xdr_ops *) &xdrrec_ops;
   xdrs->x_private = (caddr_t) rstrm;
@@ -182,7 +181,7 @@ xdrrec_create (XDR *xdrs, u_int sendsize,
   rstrm->readit = readit;
   rstrm->writeit = writeit;
   rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
-  rstrm->frag_header = (u_int32_t *) rstrm->out_base;
+  rstrm->frag_header = (uint32_t *) rstrm->out_base;
   rstrm->out_finger += 4;
   rstrm->out_boundry += sendsize;
   rstrm->frag_sent = FALSE;
@@ -192,6 +191,7 @@ xdrrec_create (XDR *xdrs, u_int sendsize,
   rstrm->fbtbc = 0;
   rstrm->last_frag = TRUE;
 }
+libc_hidden_nolink_sunrpc (xdrrec_create, GLIBC_2_0)
 
 
 /*
@@ -284,11 +284,11 @@ xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
     {
       current = rstrm->out_boundry - rstrm->out_finger;
       current = (len < current) ? len : current;
-      bcopy (addr, rstrm->out_finger, current);
+      memcpy (rstrm->out_finger, addr, current);
       rstrm->out_finger += current;
       addr += current;
       len -= current;
-      if (rstrm->out_finger == rstrm->out_boundry)
+      if (rstrm->out_finger == rstrm->out_boundry && len > 0)
        {
          rstrm->frag_sent = TRUE;
          if (!flush_out (rstrm, FALSE))
@@ -304,7 +304,7 @@ xdrrec_getpos (const XDR *xdrs)
   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
   long pos;
 
-  pos = __lseek ((int) rstrm->tcp_handle, (long) 0, 1);
+  pos = __lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
   if (pos != -1)
     switch (xdrs->x_op)
       {
@@ -365,7 +365,7 @@ xdrrec_setpos (XDR *xdrs, u_int pos)
 }
 
 static int32_t *
-xdrrec_inline (XDR *xdrs, int len)
+xdrrec_inline (XDR *xdrs, u_int len)
 {
   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
   int32_t *buf = NULL;
@@ -464,8 +464,7 @@ xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
  * this procedure to guarantee proper record alignment.
  */
 bool_t
-xdrrec_skiprecord (xdrs)
-     XDR *xdrs;
+xdrrec_skiprecord (XDR *xdrs)
 {
   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
 
@@ -480,6 +479,7 @@ xdrrec_skiprecord (xdrs)
   rstrm->last_frag = FALSE;
   return TRUE;
 }
+libc_hidden_nolink_sunrpc (xdrrec_skiprecord, GLIBC_2_0)
 
 /*
  * Lookahead function.
@@ -487,8 +487,7 @@ xdrrec_skiprecord (xdrs)
  * after consuming the rest of the current record.
  */
 bool_t
-xdrrec_eof (xdrs)
-     XDR *xdrs;
+xdrrec_eof (XDR *xdrs)
 {
   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
 
@@ -504,6 +503,7 @@ xdrrec_eof (xdrs)
     return TRUE;
   return FALSE;
 }
+libc_hidden_nolink_sunrpc (xdrrec_eof, GLIBC_2_0)
 
 /*
  * The client must tell the package when an end-of-record has occurred.
@@ -512,9 +512,7 @@ xdrrec_eof (xdrs)
  * pipelined procedure calls.)  TRUE => immediate flush to tcp connection.
  */
 bool_t
-xdrrec_endofrecord (xdrs, sendnow)
-     XDR *xdrs;
-     bool_t sendnow;
+xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
 {
   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
   u_long len;          /* fragment length */
@@ -528,17 +526,17 @@ xdrrec_endofrecord (xdrs, sendnow)
   len = (rstrm->out_finger - (char *) rstrm->frag_header
         - BYTES_PER_XDR_UNIT);
   *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
-  rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
+  rstrm->frag_header = (uint32_t *) rstrm->out_finger;
   rstrm->out_finger += BYTES_PER_XDR_UNIT;
   return TRUE;
 }
+libc_hidden_nolink_sunrpc (xdrrec_endofrecord, GLIBC_2_0)
 
 
 /*
  * Internal useful routines
  */
 static bool_t
-internal_function
 flush_out (RECSTREAM *rstrm, bool_t eor)
 {
   u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
@@ -550,7 +548,7 @@ flush_out (RECSTREAM *rstrm, bool_t eor)
   if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
       != (int) len)
     return FALSE;
-  rstrm->frag_header = (u_int32_t *) rstrm->out_base;
+  rstrm->frag_header = (uint32_t *) rstrm->out_base;
   rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
   return TRUE;
 }
@@ -575,7 +573,6 @@ fill_input_buf (RECSTREAM *rstrm)
 }
 
 static bool_t  /* knows nothing about records!  Only about input buffers */
-internal_function
 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
 {
   int current;
@@ -590,7 +587,7 @@ get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
          continue;
        }
       current = (len < current) ? len : current;
-      bcopy (rstrm->in_finger, addr, current);
+      memcpy (addr, rstrm->in_finger, current);
       rstrm->in_finger += current;
       addr += current;
       len -= current;
@@ -599,31 +596,31 @@ get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
 }
 
 static bool_t /* next two bytes of the input stream are treated as a header */
-internal_function
 set_input_fragment (RECSTREAM *rstrm)
 {
-  u_long header;
+  uint32_t header;
 
   if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
     return FALSE;
   header = ntohl (header);
   rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
   /*
-   * Sanity check. Try not to accept wildly incorrect
-   * record sizes. Unfortunately, the only record size
-   * we can positively identify as being 'wildly incorrect'
-   * is zero.  Ridiculously large record sizes may look wrong,
-   * but we don't have any way to be certain that they aren't
-   * what the client actually intended to send us.
+   * Sanity check. Try not to accept wildly incorrect fragment
+   * sizes. Unfortunately, only a size of zero can be identified as
+   * 'wildely incorrect', and this only, if it is not the last
+   * fragment of a message. Ridiculously large fragment sizes may look
+   * wrong, but we don't have any way to be certain that they aren't
+   * what the client actually intended to send us. Many existing RPC
+   * implementations may sent a fragment of size zero as the last
+   * fragment of a message.
    */
-  if ((header & (~LAST_FRAG)) == 0)
+  if (header == 0)
     return FALSE;
   rstrm->fbtbc = header & ~LAST_FRAG;
   return TRUE;
 }
 
 static bool_t  /* consumes input bytes; knows nothing about records! */
-internal_function
 skip_input_bytes (RECSTREAM *rstrm, long cnt)
 {
   int current;
@@ -645,7 +642,6 @@ skip_input_bytes (RECSTREAM *rstrm, long cnt)
 }
 
 static u_int
-internal_function
 fix_buf_size (u_int s)
 {
   if (s < 100)