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. 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 * @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro 30 * @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC 31 * $FreeBSD: src/lib/libc/xdr/xdr_mem.c,v 1.8.2.1 2003/03/20 12:59:55 jedgar Exp $ 32 * $DragonFly: src/lib/libc/xdr/xdr_mem.c,v 1.2 2003/06/17 04:26:47 dillon Exp $ 33 */ 34 35 /* 36 * xdr_mem.h, XDR implementation using memory buffers. 37 * 38 * Copyright (C) 1984, Sun Microsystems, Inc. 39 * 40 * If you have some data to be interpreted as external data representation 41 * or to be converted to external data representation in a memory buffer, 42 * then this is the package for you. 43 * 44 */ 45 46 #include <string.h> 47 #include <rpc/types.h> 48 #include <rpc/xdr.h> 49 #include <netinet/in.h> 50 51 static bool_t xdrmem_getlong_aligned(); 52 static bool_t xdrmem_putlong_aligned(); 53 static bool_t xdrmem_getlong_unaligned(); 54 static bool_t xdrmem_putlong_unaligned(); 55 static bool_t xdrmem_getbytes(); 56 static bool_t xdrmem_putbytes(); 57 static u_int xdrmem_getpos(); /* XXX w/64-bit pointers, u_int not enough! */ 58 static bool_t xdrmem_setpos(); 59 static int32_t *xdrmem_inline_aligned(); 60 static int32_t *xdrmem_inline_unaligned(); 61 static void xdrmem_destroy(); 62 63 static struct xdr_ops xdrmem_ops_aligned = { 64 xdrmem_getlong_aligned, 65 xdrmem_putlong_aligned, 66 xdrmem_getbytes, 67 xdrmem_putbytes, 68 xdrmem_getpos, 69 xdrmem_setpos, 70 xdrmem_inline_aligned, 71 xdrmem_destroy 72 }; 73 74 static struct xdr_ops xdrmem_ops_unaligned = { 75 xdrmem_getlong_unaligned, 76 xdrmem_putlong_unaligned, 77 xdrmem_getbytes, 78 xdrmem_putbytes, 79 xdrmem_getpos, 80 xdrmem_setpos, 81 xdrmem_inline_unaligned, 82 xdrmem_destroy 83 }; 84 85 /* 86 * The procedure xdrmem_create initializes a stream descriptor for a 87 * memory buffer. 88 */ 89 void 90 xdrmem_create(xdrs, addr, size, op) 91 register XDR *xdrs; 92 caddr_t addr; 93 u_int size; 94 enum xdr_op op; 95 { 96 97 xdrs->x_op = op; 98 xdrs->x_ops = ((size_t)addr & (sizeof(int32_t) - 1)) 99 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned; 100 xdrs->x_private = xdrs->x_base = addr; 101 xdrs->x_handy = size; 102 } 103 104 static void 105 xdrmem_destroy(/*xdrs*/) 106 /*XDR *xdrs;*/ 107 { 108 109 } 110 111 static bool_t 112 xdrmem_getlong_aligned(xdrs, lp) 113 register XDR *xdrs; 114 long *lp; 115 { 116 117 if (xdrs->x_handy < sizeof(int32_t)) 118 return (FALSE); 119 xdrs->x_handy -= sizeof(int32_t); 120 *lp = ntohl(*(int32_t *)(xdrs->x_private)); 121 xdrs->x_private += sizeof(int32_t); 122 return (TRUE); 123 } 124 125 static bool_t 126 xdrmem_putlong_aligned(xdrs, lp) 127 register XDR *xdrs; 128 long *lp; 129 { 130 131 if (xdrs->x_handy < sizeof(int32_t)) 132 return (FALSE); 133 xdrs->x_handy -= sizeof(int32_t); 134 *(int32_t *)xdrs->x_private = htonl(*lp); 135 xdrs->x_private += sizeof(int32_t); 136 return (TRUE); 137 } 138 139 static bool_t 140 xdrmem_getlong_unaligned(xdrs, lp) 141 register XDR *xdrs; 142 long *lp; 143 { 144 int32_t l; 145 146 if (xdrs->x_handy < sizeof(int32_t)) 147 return (FALSE); 148 xdrs->x_handy -= sizeof(int32_t); 149 memcpy(&l, xdrs->x_private, sizeof(int32_t)); 150 *lp = ntohl(l); 151 xdrs->x_private += sizeof(int32_t); 152 return (TRUE); 153 } 154 155 static bool_t 156 xdrmem_putlong_unaligned(xdrs, lp) 157 register XDR *xdrs; 158 long *lp; 159 { 160 int32_t l; 161 162 if (xdrs->x_handy < sizeof(int32_t)) 163 return (FALSE); 164 xdrs->x_handy -= sizeof(int32_t); 165 l = htonl(*lp); 166 memcpy(xdrs->x_private, &l, sizeof(int32_t)); 167 xdrs->x_private += sizeof(int32_t); 168 return (TRUE); 169 } 170 171 static bool_t 172 xdrmem_getbytes(xdrs, addr, len) 173 register XDR *xdrs; 174 caddr_t addr; 175 register u_int len; 176 { 177 178 if (xdrs->x_handy < len) 179 return (FALSE); 180 xdrs->x_handy -= len; 181 memcpy(addr, xdrs->x_private, len); 182 xdrs->x_private += len; 183 return (TRUE); 184 } 185 186 static bool_t 187 xdrmem_putbytes(xdrs, addr, len) 188 register XDR *xdrs; 189 caddr_t addr; 190 register u_int len; 191 { 192 193 if (xdrs->x_handy < len) 194 return (FALSE); 195 xdrs->x_handy -= len; 196 memcpy(xdrs->x_private, addr, len); 197 xdrs->x_private += len; 198 return (TRUE); 199 } 200 201 static u_int 202 xdrmem_getpos(xdrs) 203 register XDR *xdrs; 204 { 205 206 /* XXX w/64-bit pointers, u_int not enough! */ 207 return ((u_long)xdrs->x_private - (u_long)xdrs->x_base); 208 } 209 210 static bool_t 211 xdrmem_setpos(xdrs, pos) 212 register XDR *xdrs; 213 u_int pos; 214 { 215 register caddr_t newaddr = xdrs->x_base + pos; 216 register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy; 217 218 if (newaddr > lastaddr) 219 return (FALSE); 220 xdrs->x_private = newaddr; 221 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */ 222 return (TRUE); 223 } 224 225 static int32_t * 226 xdrmem_inline_aligned(xdrs, len) 227 register XDR *xdrs; 228 int len; 229 { 230 int32_t *buf = 0; 231 232 if (xdrs->x_handy >= len) { 233 xdrs->x_handy -= len; 234 buf = (int32_t *) xdrs->x_private; 235 xdrs->x_private += len; 236 } 237 return (buf); 238 } 239 240 static int32_t * 241 xdrmem_inline_unaligned(xdrs, len) 242 register XDR *xdrs; 243 int len; 244 { 245 246 return (0); 247 } 248