| 1 | 1325 | phoenix | /*
 | 
      
         | 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.
 | 
      
         | 8 |  |  |  *
 | 
      
         | 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.
 | 
      
         | 12 |  |  |  *
 | 
      
         | 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.
 | 
      
         | 16 |  |  |  *
 | 
      
         | 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.
 | 
      
         | 20 |  |  |  *
 | 
      
         | 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.
 | 
      
         | 24 |  |  |  *
 | 
      
         | 25 |  |  |  * Sun Microsystems, Inc.
 | 
      
         | 26 |  |  |  * 2550 Garcia Avenue
 | 
      
         | 27 |  |  |  * Mountain View, California  94043
 | 
      
         | 28 |  |  |  */
 | 
      
         | 29 |  |  |  
 | 
      
         | 30 |  |  | /*
 | 
      
         | 31 |  |  |  * xdr_mem.h, XDR implementation using memory buffers.
 | 
      
         | 32 |  |  |  *
 | 
      
         | 33 |  |  |  * Copyright (C) 1984, Sun Microsystems, Inc.
 | 
      
         | 34 |  |  |  *
 | 
      
         | 35 |  |  |  * If you have some data to be interpreted as external data representation
 | 
      
         | 36 |  |  |  * or to be converted to external data representation in a memory buffer,
 | 
      
         | 37 |  |  |  * then this is the package for you.
 | 
      
         | 38 |  |  |  *
 | 
      
         | 39 |  |  |  */
 | 
      
         | 40 |  |  |  
 | 
      
         | 41 |  |  | #define __FORCE_GLIBC
 | 
      
         | 42 |  |  | #define _GNU_SOURCE
 | 
      
         | 43 |  |  | #include <features.h>
 | 
      
         | 44 |  |  |  
 | 
      
         | 45 |  |  |  
 | 
      
         | 46 |  |  | #include <string.h>
 | 
      
         | 47 |  |  | #include <rpc/rpc.h>
 | 
      
         | 48 |  |  |  
 | 
      
         | 49 |  |  | static bool_t xdrmem_getlong (XDR *, long *);
 | 
      
         | 50 |  |  | static bool_t xdrmem_putlong (XDR *, const long *);
 | 
      
         | 51 |  |  | static bool_t xdrmem_getbytes (XDR *, caddr_t, u_int);
 | 
      
         | 52 |  |  | static bool_t xdrmem_putbytes (XDR *, const char *, u_int);
 | 
      
         | 53 |  |  | static u_int xdrmem_getpos (const XDR *);
 | 
      
         | 54 |  |  | static bool_t xdrmem_setpos (XDR *, u_int);
 | 
      
         | 55 |  |  | static int32_t *xdrmem_inline (XDR *, int);
 | 
      
         | 56 |  |  | static void xdrmem_destroy (XDR *);
 | 
      
         | 57 |  |  | static bool_t xdrmem_getint32 (XDR *, int32_t *);
 | 
      
         | 58 |  |  | static bool_t xdrmem_putint32 (XDR *, const int32_t *);
 | 
      
         | 59 |  |  |  
 | 
      
         | 60 |  |  | static const struct xdr_ops xdrmem_ops =
 | 
      
         | 61 |  |  | {
 | 
      
         | 62 |  |  |   xdrmem_getlong,
 | 
      
         | 63 |  |  |   xdrmem_putlong,
 | 
      
         | 64 |  |  |   xdrmem_getbytes,
 | 
      
         | 65 |  |  |   xdrmem_putbytes,
 | 
      
         | 66 |  |  |   xdrmem_getpos,
 | 
      
         | 67 |  |  |   xdrmem_setpos,
 | 
      
         | 68 |  |  |   xdrmem_inline,
 | 
      
         | 69 |  |  |   xdrmem_destroy,
 | 
      
         | 70 |  |  |   xdrmem_getint32,
 | 
      
         | 71 |  |  |   xdrmem_putint32
 | 
      
         | 72 |  |  | };
 | 
      
         | 73 |  |  |  
 | 
      
         | 74 |  |  | /*
 | 
      
         | 75 |  |  |  * The procedure xdrmem_create initializes a stream descriptor for a
 | 
      
         | 76 |  |  |  * memory buffer.
 | 
      
         | 77 |  |  |  */
 | 
      
         | 78 |  |  | void
 | 
      
         | 79 |  |  | xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum xdr_op op)
 | 
      
         | 80 |  |  | {
 | 
      
         | 81 |  |  |   xdrs->x_op = op;
 | 
      
         | 82 |  |  |   /* We have to add the const since the `struct xdr_ops' in `struct XDR'
 | 
      
         | 83 |  |  |      is not `const'.  */
 | 
      
         | 84 |  |  |   xdrs->x_ops = (struct xdr_ops *) &xdrmem_ops;
 | 
      
         | 85 |  |  |   xdrs->x_private = xdrs->x_base = addr;
 | 
      
         | 86 |  |  |   xdrs->x_handy = size;
 | 
      
         | 87 |  |  | }
 | 
      
         | 88 |  |  |  
 | 
      
         | 89 |  |  | /*
 | 
      
         | 90 |  |  |  * Nothing needs to be done for the memory case.  The argument is clearly
 | 
      
         | 91 |  |  |  * const.
 | 
      
         | 92 |  |  |  */
 | 
      
         | 93 |  |  |  
 | 
      
         | 94 |  |  | static void
 | 
      
         | 95 |  |  | xdrmem_destroy (XDR *xdrs)
 | 
      
         | 96 |  |  | {
 | 
      
         | 97 |  |  | }
 | 
      
         | 98 |  |  |  
 | 
      
         | 99 |  |  | /*
 | 
      
         | 100 |  |  |  * Gets the next word from the memory referenced by xdrs and places it
 | 
      
         | 101 |  |  |  * in the long pointed to by lp.  It then increments the private word to
 | 
      
         | 102 |  |  |  * point at the next element.  Neither object pointed to is const
 | 
      
         | 103 |  |  |  */
 | 
      
         | 104 |  |  | static bool_t
 | 
      
         | 105 |  |  | xdrmem_getlong (XDR *xdrs, long *lp)
 | 
      
         | 106 |  |  | {
 | 
      
         | 107 |  |  |   if ((xdrs->x_handy -= 4) < 0)
 | 
      
         | 108 |  |  |     return FALSE;
 | 
      
         | 109 |  |  |   *lp = (int32_t) ntohl ((*((int32_t *) (xdrs->x_private))));
 | 
      
         | 110 |  |  |   xdrs->x_private += 4;
 | 
      
         | 111 |  |  |   return TRUE;
 | 
      
         | 112 |  |  | }
 | 
      
         | 113 |  |  |  
 | 
      
         | 114 |  |  | /*
 | 
      
         | 115 |  |  |  * Puts the long pointed to by lp in the memory referenced by xdrs.  It
 | 
      
         | 116 |  |  |  * then increments the private word to point at the next element.  The
 | 
      
         | 117 |  |  |  * long pointed at is const
 | 
      
         | 118 |  |  |  */
 | 
      
         | 119 |  |  | static bool_t
 | 
      
         | 120 |  |  | xdrmem_putlong (XDR *xdrs, const long *lp)
 | 
      
         | 121 |  |  | {
 | 
      
         | 122 |  |  |   if ((xdrs->x_handy -= 4) < 0)
 | 
      
         | 123 |  |  |     return FALSE;
 | 
      
         | 124 |  |  |   *(int32_t *) xdrs->x_private = htonl (*lp);
 | 
      
         | 125 |  |  |   xdrs->x_private += 4;
 | 
      
         | 126 |  |  |   return TRUE;
 | 
      
         | 127 |  |  | }
 | 
      
         | 128 |  |  |  
 | 
      
         | 129 |  |  | /*
 | 
      
         | 130 |  |  |  * Gets an unaligned number of bytes from the xdrs structure and writes them
 | 
      
         | 131 |  |  |  * to the address passed in addr.  Be very careful when calling this routine
 | 
      
         | 132 |  |  |  * as it could leave the xdrs pointing to an unaligned structure which is not
 | 
      
         | 133 |  |  |  * a good idea.  None of the things pointed to are const.
 | 
      
         | 134 |  |  |  */
 | 
      
         | 135 |  |  | static bool_t
 | 
      
         | 136 |  |  | xdrmem_getbytes (XDR *xdrs, caddr_t addr, u_int len)
 | 
      
         | 137 |  |  | {
 | 
      
         | 138 |  |  |   if ((xdrs->x_handy -= len) < 0)
 | 
      
         | 139 |  |  |     return FALSE;
 | 
      
         | 140 |  |  |   memcpy (addr, xdrs->x_private, len);
 | 
      
         | 141 |  |  |   xdrs->x_private += len;
 | 
      
         | 142 |  |  |   return TRUE;
 | 
      
         | 143 |  |  | }
 | 
      
         | 144 |  |  |  
 | 
      
         | 145 |  |  | /*
 | 
      
         | 146 |  |  |  * The complementary function to the above.  The same warnings apply about
 | 
      
         | 147 |  |  |  * unaligned data.  The source address is const.
 | 
      
         | 148 |  |  |  */
 | 
      
         | 149 |  |  | static bool_t
 | 
      
         | 150 |  |  | xdrmem_putbytes (XDR *xdrs, const char *addr, u_int len)
 | 
      
         | 151 |  |  | {
 | 
      
         | 152 |  |  |   if ((xdrs->x_handy -= len) < 0)
 | 
      
         | 153 |  |  |     return FALSE;
 | 
      
         | 154 |  |  |   memcpy (xdrs->x_private, addr, len);
 | 
      
         | 155 |  |  |   xdrs->x_private += len;
 | 
      
         | 156 |  |  |   return TRUE;
 | 
      
         | 157 |  |  | }
 | 
      
         | 158 |  |  |  
 | 
      
         | 159 |  |  | /*
 | 
      
         | 160 |  |  |  * Not sure what this one does.  But it clearly doesn't modify the contents
 | 
      
         | 161 |  |  |  * of xdrs.  **FIXME** does this not assume u_int == u_long?
 | 
      
         | 162 |  |  |  */
 | 
      
         | 163 |  |  | static u_int
 | 
      
         | 164 |  |  | xdrmem_getpos (const XDR *xdrs)
 | 
      
         | 165 |  |  | {
 | 
      
         | 166 |  |  |   return (u_long) xdrs->x_private - (u_long) xdrs->x_base;
 | 
      
         | 167 |  |  | }
 | 
      
         | 168 |  |  |  
 | 
      
         | 169 |  |  | /*
 | 
      
         | 170 |  |  |  * xdrs modified
 | 
      
         | 171 |  |  |  */
 | 
      
         | 172 |  |  | static bool_t
 | 
      
         | 173 |  |  | xdrmem_setpos (xdrs, pos)
 | 
      
         | 174 |  |  |      XDR *xdrs;
 | 
      
         | 175 |  |  |      u_int pos;
 | 
      
         | 176 |  |  | {
 | 
      
         | 177 |  |  |   caddr_t newaddr = xdrs->x_base + pos;
 | 
      
         | 178 |  |  |   caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
 | 
      
         | 179 |  |  |  
 | 
      
         | 180 |  |  |   if ((long) newaddr > (long) lastaddr)
 | 
      
         | 181 |  |  |     return FALSE;
 | 
      
         | 182 |  |  |   xdrs->x_private = newaddr;
 | 
      
         | 183 |  |  |   xdrs->x_handy = (long) lastaddr - (long) newaddr;
 | 
      
         | 184 |  |  |   return TRUE;
 | 
      
         | 185 |  |  | }
 | 
      
         | 186 |  |  |  
 | 
      
         | 187 |  |  | /*
 | 
      
         | 188 |  |  |  * xdrs modified
 | 
      
         | 189 |  |  |  */
 | 
      
         | 190 |  |  | static int32_t *
 | 
      
         | 191 |  |  | xdrmem_inline (XDR *xdrs, int len)
 | 
      
         | 192 |  |  | {
 | 
      
         | 193 |  |  |   int32_t *buf = 0;
 | 
      
         | 194 |  |  |  
 | 
      
         | 195 |  |  |   if (xdrs->x_handy >= len)
 | 
      
         | 196 |  |  |     {
 | 
      
         | 197 |  |  |       xdrs->x_handy -= len;
 | 
      
         | 198 |  |  |       buf = (int32_t *) xdrs->x_private;
 | 
      
         | 199 |  |  |       xdrs->x_private += len;
 | 
      
         | 200 |  |  |     }
 | 
      
         | 201 |  |  |   return buf;
 | 
      
         | 202 |  |  | }
 | 
      
         | 203 |  |  |  
 | 
      
         | 204 |  |  | /*
 | 
      
         | 205 |  |  |  * Gets the next word from the memory referenced by xdrs and places it
 | 
      
         | 206 |  |  |  * in the int pointed to by ip.  It then increments the private word to
 | 
      
         | 207 |  |  |  * point at the next element.  Neither object pointed to is const
 | 
      
         | 208 |  |  |  */
 | 
      
         | 209 |  |  | static bool_t
 | 
      
         | 210 |  |  | xdrmem_getint32 (XDR *xdrs, int32_t *ip)
 | 
      
         | 211 |  |  | {
 | 
      
         | 212 |  |  |   if ((xdrs->x_handy -= 4) < 0)
 | 
      
         | 213 |  |  |     return FALSE;
 | 
      
         | 214 |  |  |   *ip = ntohl ((*((int32_t *) (xdrs->x_private))));
 | 
      
         | 215 |  |  |   xdrs->x_private += 4;
 | 
      
         | 216 |  |  |   return TRUE;
 | 
      
         | 217 |  |  | }
 | 
      
         | 218 |  |  |  
 | 
      
         | 219 |  |  | /*
 | 
      
         | 220 |  |  |  * Puts the long pointed to by lp in the memory referenced by xdrs.  It
 | 
      
         | 221 |  |  |  * then increments the private word to point at the next element.  The
 | 
      
         | 222 |  |  |  * long pointed at is const
 | 
      
         | 223 |  |  |  */
 | 
      
         | 224 |  |  | static bool_t
 | 
      
         | 225 |  |  | xdrmem_putint32 (XDR *xdrs, const int32_t *ip)
 | 
      
         | 226 |  |  | {
 | 
      
         | 227 |  |  |   if ((xdrs->x_handy -= 4) < 0)
 | 
      
         | 228 |  |  |     return FALSE;
 | 
      
         | 229 |  |  |   *(int32_t *) xdrs->x_private = htonl (*ip);
 | 
      
         | 230 |  |  |   xdrs->x_private += 4;
 | 
      
         | 231 |  |  |   return TRUE;
 | 
      
         | 232 |  |  | }
 |