1 /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 2010, Oracle America, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are 10 * met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer in the documentation and/or other materials 17 * provided with the distribution. 18 * * Neither the name of the "Oracle America, Inc." nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #if defined(LIBC_SCCS) && !defined(lint) 37 static char *sccsid2 = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro"; 38 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC"; 39 #endif 40 /* 41 * xdr_mem.h, XDR implementation using memory buffers. 42 * 43 * If you have some data to be interpreted as external data representation 44 * or to be converted to external data representation in a memory buffer, 45 * then this is the package for you. 46 * 47 */ 48 49 #include "namespace.h" 50 #include <sys/types.h> 51 52 #include <netinet/in.h> 53 54 #include <string.h> 55 56 #include <rpc/types.h> 57 #include <rpc/xdr.h> 58 #include "un-namespace.h" 59 60 static void xdrmem_destroy(XDR *); 61 static bool_t xdrmem_getlong_aligned(XDR *, long *); 62 static bool_t xdrmem_putlong_aligned(XDR *, const long *); 63 static bool_t xdrmem_getlong_unaligned(XDR *, long *); 64 static bool_t xdrmem_putlong_unaligned(XDR *, const long *); 65 static bool_t xdrmem_getbytes(XDR *, char *, u_int); 66 static bool_t xdrmem_putbytes(XDR *, const char *, u_int); 67 /* XXX: w/64-bit pointers, u_int not enough! */ 68 static u_int xdrmem_getpos(XDR *); 69 static bool_t xdrmem_setpos(XDR *, u_int); 70 static int32_t *xdrmem_inline_aligned(XDR *, u_int); 71 static int32_t *xdrmem_inline_unaligned(XDR *, u_int); 72 73 static const struct xdr_ops xdrmem_ops_aligned = { 74 xdrmem_getlong_aligned, 75 xdrmem_putlong_aligned, 76 xdrmem_getbytes, 77 xdrmem_putbytes, 78 xdrmem_getpos, 79 xdrmem_setpos, 80 xdrmem_inline_aligned, 81 xdrmem_destroy 82 }; 83 84 static const struct xdr_ops xdrmem_ops_unaligned = { 85 xdrmem_getlong_unaligned, 86 xdrmem_putlong_unaligned, 87 xdrmem_getbytes, 88 xdrmem_putbytes, 89 xdrmem_getpos, 90 xdrmem_setpos, 91 xdrmem_inline_unaligned, 92 xdrmem_destroy 93 }; 94 95 /* 96 * The procedure xdrmem_create initializes a stream descriptor for a 97 * memory buffer. 98 */ 99 void 100 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op) 101 { 102 103 xdrs->x_op = op; 104 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1)) 105 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned; 106 xdrs->x_private = xdrs->x_base = addr; 107 xdrs->x_handy = size; 108 } 109 110 /*ARGSUSED*/ 111 static void 112 xdrmem_destroy(XDR *xdrs) 113 { 114 115 } 116 117 static bool_t 118 xdrmem_getlong_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 *lp = ntohl(*(u_int32_t *)xdrs->x_private); 125 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t); 126 return (TRUE); 127 } 128 129 static bool_t 130 xdrmem_putlong_aligned(XDR *xdrs, const long *lp) 131 { 132 133 if (xdrs->x_handy < sizeof(int32_t)) 134 return (FALSE); 135 xdrs->x_handy -= sizeof(int32_t); 136 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp); 137 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t); 138 return (TRUE); 139 } 140 141 static bool_t 142 xdrmem_getlong_unaligned(XDR *xdrs, long *lp) 143 { 144 u_int32_t l; 145 146 if (xdrs->x_handy < sizeof(int32_t)) 147 return (FALSE); 148 xdrs->x_handy -= sizeof(int32_t); 149 memmove(&l, xdrs->x_private, sizeof(int32_t)); 150 *lp = ntohl(l); 151 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t); 152 return (TRUE); 153 } 154 155 static bool_t 156 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp) 157 { 158 u_int32_t l; 159 160 if (xdrs->x_handy < sizeof(int32_t)) 161 return (FALSE); 162 xdrs->x_handy -= sizeof(int32_t); 163 l = htonl((u_int32_t)*lp); 164 memmove(xdrs->x_private, &l, sizeof(int32_t)); 165 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t); 166 return (TRUE); 167 } 168 169 static bool_t 170 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len) 171 { 172 173 if (xdrs->x_handy < len) 174 return (FALSE); 175 xdrs->x_handy -= len; 176 memmove(addr, xdrs->x_private, len); 177 xdrs->x_private = (char *)xdrs->x_private + len; 178 return (TRUE); 179 } 180 181 static bool_t 182 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len) 183 { 184 185 if (xdrs->x_handy < len) 186 return (FALSE); 187 xdrs->x_handy -= len; 188 memmove(xdrs->x_private, addr, len); 189 xdrs->x_private = (char *)xdrs->x_private + len; 190 return (TRUE); 191 } 192 193 static u_int 194 xdrmem_getpos(XDR *xdrs) 195 { 196 197 /* XXX w/64-bit pointers, u_int not enough! */ 198 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base); 199 } 200 201 static bool_t 202 xdrmem_setpos(XDR *xdrs, u_int pos) 203 { 204 char *newaddr = xdrs->x_base + pos; 205 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy; 206 207 if (newaddr > lastaddr) 208 return (FALSE); 209 xdrs->x_private = newaddr; 210 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */ 211 return (TRUE); 212 } 213 214 static int32_t * 215 xdrmem_inline_aligned(XDR *xdrs, u_int len) 216 { 217 int32_t *buf = NULL; 218 219 if (xdrs->x_handy >= len) { 220 xdrs->x_handy -= len; 221 buf = (int32_t *)xdrs->x_private; 222 xdrs->x_private = (char *)xdrs->x_private + len; 223 } 224 return (buf); 225 } 226 227 /* ARGSUSED */ 228 static int32_t * 229 xdrmem_inline_unaligned(XDR *xdrs, u_int len) 230 { 231 232 return (0); 233 } 234