1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 */ 9 /* @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC */ 10 /* 11 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 12 * unrestricted use provided that this legend is included on all tape 13 * media and as a part of the software program in whole or part. Users 14 * may copy or modify Sun RPC without charge, but are not authorized 15 * to license or distribute it to anyone else except as part of a product or 16 * program developed by the user. 17 * 18 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 19 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 21 * 22 * Sun RPC is provided with no support and without any obligation on the 23 * part of Sun Microsystems, Inc. to assist in its use, correction, 24 * modification or enhancement. 25 * 26 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 27 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 28 * OR ANY PART THEREOF. 29 * 30 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 31 * or profits or other special, indirect and consequential damages, even if 32 * Sun has been advised of the possibility of such damages. 33 * 34 * Sun Microsystems, Inc. 35 * 2550 Garcia Avenue 36 * Mountain View, California 94043 37 */ 38 #if !defined(lint) && defined(SCCSIDS) 39 static char sccsid[] = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro"; 40 #endif 41 42 /* 43 * xdr_mem.h, XDR implementation using memory buffers. 44 * 45 * Copyright (C) 1984, Sun Microsystems, Inc. 46 * 47 * If you have some data to be interpreted as external data representation 48 * or to be converted to external data representation in a memory buffer, 49 * then this is the package for you. 50 * 51 */ 52 53 #include <rpc/types.h> 54 #include <rpc/xdr.h> 55 #include <string.h> 56 #include <limits.h> 57 58 static bool_t xdrmem_getlong (XDR *, long *); 59 static bool_t xdrmem_putlong (XDR *, const long *); 60 static bool_t xdrmem_getbytes (XDR *, char *, unsigned int); 61 static bool_t xdrmem_putbytes (XDR *, const char *, unsigned int); 62 static unsigned int xdrmem_getpos (const XDR *); 63 static bool_t xdrmem_setpos (XDR *, unsigned int); 64 static int32_t *xdrmem_inline (XDR *, unsigned int); 65 static void xdrmem_destroy (XDR *); 66 67 static struct xdr_ops xdrmem_ops = { 68 xdrmem_getlong, 69 xdrmem_putlong, 70 xdrmem_getbytes, 71 xdrmem_putbytes, 72 xdrmem_getpos, 73 xdrmem_setpos, 74 xdrmem_inline, 75 xdrmem_destroy, 76 NULL, 77 NULL 78 }; 79 80 81 /* 82 * The procedure xdrmem_create initializes a stream descriptor for a 83 * memory buffer. 84 */ 85 void 86 xdrmem_create (XDR *xdrs, const char* addr, unsigned int size, enum xdr_op op) 87 { 88 xdrs->x_op = op; 89 xdrs->x_ops = &xdrmem_ops; 90 xdrs->x_private = xdrs->x_base = (char*)addr; 91 xdrs->x_handy = size; 92 } 93 94 static void 95 xdrmem_destroy (XDR *xdrs) 96 { 97 } 98 99 static bool_t 100 xdrmem_getlong (XDR *xdrs, long *lp) 101 { 102 if (xdrs->x_handy < 4) return FALSE; 103 xdrs->x_handy -= 4; 104 105 *lp = (int32_t) ntohl((*((int32_t *) (xdrs->x_private)))); 106 xdrs->x_private += 4; 107 return TRUE; 108 } 109 110 static bool_t 111 xdrmem_putlong (XDR *xdrs, const long *lp) 112 { 113 if (xdrs->x_handy < 4) return FALSE; 114 xdrs->x_handy -= 4; 115 116 *(int32_t *) xdrs->x_private = htonl(*lp); 117 xdrs->x_private += 4; 118 return (TRUE); 119 } 120 121 static bool_t 122 xdrmem_getbytes (XDR *xdrs, char *addr, unsigned int len) 123 { 124 if (xdrs->x_handy < len) return FALSE; 125 xdrs->x_handy -= len; 126 memmove(addr, xdrs->x_private, len); 127 xdrs->x_private += len; 128 return TRUE; 129 } 130 131 static bool_t 132 xdrmem_putbytes (XDR *xdrs, const char *addr, unsigned int len) 133 { 134 if (xdrs->x_handy < len) return FALSE; 135 xdrs->x_handy -= len; 136 memmove(xdrs->x_private, addr, len); 137 xdrs->x_private += len; 138 return (TRUE); 139 } 140 141 static unsigned int xdrmem_getpos (const XDR *xdrs) 142 { 143 return ((unsigned long) xdrs->x_private - (unsigned long) xdrs->x_base); 144 } 145 146 static bool_t xdrmem_setpos(XDR *xdrs, unsigned int pos) 147 { 148 register char* newaddr = xdrs->x_base + pos; 149 register char* lastaddr = xdrs->x_private + xdrs->x_handy; 150 151 if ((long) newaddr > (long) lastaddr 152 || (UINT_MAX < LONG_MAX 153 && (long) UINT_MAX < (long) lastaddr - (long) newaddr)) 154 return (FALSE); 155 xdrs->x_private = newaddr; 156 xdrs->x_handy = (long) lastaddr - (long) newaddr; 157 return (TRUE); 158 } 159 160 static int32_t * 161 xdrmem_inline (XDR *xdrs, unsigned int len) 162 { 163 int32_t *buf = 0; 164 165 if (xdrs->x_handy >= len) { 166 xdrs->x_handy -= len; 167 buf = (int32_t *) xdrs->x_private; 168 xdrs->x_private += len; 169 } 170 return (buf); 171 } 172 173