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.4 2005/12/05 00:47:57 swildner 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(XDR *, long *); 52 static bool_t xdrmem_putlong_aligned(XDR *, long *); 53 static bool_t xdrmem_getlong_unaligned(XDR *, long *); 54 static bool_t xdrmem_putlong_unaligned(XDR *, long *); 55 static bool_t xdrmem_getbytes(XDR *, caddr_t, u_int); 56 static bool_t xdrmem_putbytes(XDR *, caddr_t, u_int); 57 static u_int xdrmem_getpos(XDR *); /* XXX w/64-bit pointers, u_int not enough! */ 58 static bool_t xdrmem_setpos(XDR *, u_int); 59 static int32_t *xdrmem_inline_aligned(XDR *, int); 60 static int32_t *xdrmem_inline_unaligned(XDR *, int); 61 static void xdrmem_destroy(void); 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(XDR *xdrs, caddr_t addr, u_int size, enum xdr_op op) 91 { 92 93 xdrs->x_op = op; 94 xdrs->x_ops = ((size_t)addr & (sizeof(int32_t) - 1)) 95 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned; 96 xdrs->x_private = xdrs->x_base = addr; 97 xdrs->x_handy = size; 98 } 99 100 static void 101 xdrmem_destroy(void) 102 { 103 } 104 105 static bool_t 106 xdrmem_getlong_aligned(XDR *xdrs, long *lp) 107 { 108 109 if (xdrs->x_handy < sizeof(int32_t)) 110 return (FALSE); 111 xdrs->x_handy -= sizeof(int32_t); 112 *lp = ntohl(*(int32_t *)(xdrs->x_private)); 113 xdrs->x_private += sizeof(int32_t); 114 return (TRUE); 115 } 116 117 static bool_t 118 xdrmem_putlong_aligned(XDR *xdrs, long *lp) 119 { 120 121 if (xdrs->x_handy < sizeof(int32_t)) 122 return (FALSE); 123 xdrs->x_handy -= sizeof(int32_t); 124 *(int32_t *)xdrs->x_private = htonl(*lp); 125 xdrs->x_private += sizeof(int32_t); 126 return (TRUE); 127 } 128 129 static bool_t 130 xdrmem_getlong_unaligned(XDR *xdrs, long *lp) 131 { 132 int32_t l; 133 134 if (xdrs->x_handy < sizeof(int32_t)) 135 return (FALSE); 136 xdrs->x_handy -= sizeof(int32_t); 137 memcpy(&l, xdrs->x_private, sizeof(int32_t)); 138 *lp = ntohl(l); 139 xdrs->x_private += sizeof(int32_t); 140 return (TRUE); 141 } 142 143 static bool_t 144 xdrmem_putlong_unaligned(XDR *xdrs, long *lp) 145 { 146 int32_t l; 147 148 if (xdrs->x_handy < sizeof(int32_t)) 149 return (FALSE); 150 xdrs->x_handy -= sizeof(int32_t); 151 l = htonl(*lp); 152 memcpy(xdrs->x_private, &l, sizeof(int32_t)); 153 xdrs->x_private += sizeof(int32_t); 154 return (TRUE); 155 } 156 157 static bool_t 158 xdrmem_getbytes(XDR *xdrs, caddr_t addr, u_int len) 159 { 160 161 if (xdrs->x_handy < len) 162 return (FALSE); 163 xdrs->x_handy -= len; 164 memcpy(addr, xdrs->x_private, len); 165 xdrs->x_private += len; 166 return (TRUE); 167 } 168 169 static bool_t 170 xdrmem_putbytes(XDR *xdrs, caddr_t addr, u_int len) 171 { 172 173 if (xdrs->x_handy < len) 174 return (FALSE); 175 xdrs->x_handy -= len; 176 memcpy(xdrs->x_private, addr, len); 177 xdrs->x_private += len; 178 return (TRUE); 179 } 180 181 static u_int 182 xdrmem_getpos(XDR *xdrs) 183 { 184 185 /* XXX w/64-bit pointers, u_int not enough! */ 186 return ((u_long)xdrs->x_private - (u_long)xdrs->x_base); 187 } 188 189 static bool_t 190 xdrmem_setpos(XDR *xdrs, u_int pos) 191 { 192 caddr_t newaddr = xdrs->x_base + pos; 193 caddr_t lastaddr = xdrs->x_private + xdrs->x_handy; 194 195 if (newaddr > lastaddr) 196 return (FALSE); 197 xdrs->x_private = newaddr; 198 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */ 199 return (TRUE); 200 } 201 202 static int32_t * 203 xdrmem_inline_aligned(XDR *xdrs, int len) 204 { 205 int32_t *buf = 0; 206 207 if (xdrs->x_handy >= len) { 208 xdrs->x_handy -= len; 209 buf = (int32_t *) xdrs->x_private; 210 xdrs->x_private += len; 211 } 212 return (buf); 213 } 214 215 static int32_t * 216 xdrmem_inline_unaligned(XDR *xdrs __unused, int len __unused) 217 { 218 return (0); 219 } 220