1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2010, Oracle America, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * * Neither the name of the "Oracle America, Inc." nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 /* 34 * xdr_sizeof.c 35 * 36 * General purpose routine to see how much space something will use 37 * when serialized using XDR. 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include "namespace.h" 44 #include <rpc/types.h> 45 #include <rpc/xdr.h> 46 #include <sys/types.h> 47 #include <stdlib.h> 48 #include "un-namespace.h" 49 50 /* ARGSUSED */ 51 static bool_t 52 x_putlong(XDR *xdrs, const long *longp) 53 { 54 xdrs->x_handy += BYTES_PER_XDR_UNIT; 55 return (TRUE); 56 } 57 58 /* ARGSUSED */ 59 static bool_t 60 x_putbytes(XDR *xdrs, const char *bp, u_int len) 61 { 62 xdrs->x_handy += len; 63 return (TRUE); 64 } 65 66 static u_int 67 x_getpostn(XDR *xdrs) 68 { 69 return (xdrs->x_handy); 70 } 71 72 /* ARGSUSED */ 73 static bool_t 74 x_setpostn(XDR *xdrs, u_int pos) 75 { 76 /* This is not allowed */ 77 return (FALSE); 78 } 79 80 static int32_t * 81 x_inline(XDR *xdrs, u_int len) 82 { 83 if (len == 0) { 84 return (NULL); 85 } 86 if (xdrs->x_op != XDR_ENCODE) { 87 return (NULL); 88 } 89 if (len < (u_int)(uintptr_t)xdrs->x_base) { 90 /* x_private was already allocated */ 91 xdrs->x_handy += len; 92 return ((int32_t *) xdrs->x_private); 93 } else { 94 /* Free the earlier space and allocate new area */ 95 if (xdrs->x_private) 96 free(xdrs->x_private); 97 if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) { 98 xdrs->x_base = 0; 99 return (NULL); 100 } 101 xdrs->x_base = (caddr_t)(uintptr_t)len; 102 xdrs->x_handy += len; 103 return ((int32_t *) xdrs->x_private); 104 } 105 } 106 107 static int 108 harmless(void) 109 { 110 /* Always return FALSE/NULL, as the case may be */ 111 return (0); 112 } 113 114 static void 115 x_destroy(XDR *xdrs) 116 { 117 xdrs->x_handy = 0; 118 xdrs->x_base = 0; 119 if (xdrs->x_private) { 120 free(xdrs->x_private); 121 xdrs->x_private = NULL; 122 } 123 return; 124 } 125 126 unsigned long 127 xdr_sizeof(xdrproc_t func, void *data) 128 { 129 XDR x; 130 struct xdr_ops ops; 131 bool_t stat; 132 /* to stop ANSI-C compiler from complaining */ 133 typedef bool_t (* dummyfunc1)(XDR *, long *); 134 typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int); 135 136 ops.x_putlong = x_putlong; 137 ops.x_putbytes = x_putbytes; 138 ops.x_inline = x_inline; 139 ops.x_getpostn = x_getpostn; 140 ops.x_setpostn = x_setpostn; 141 ops.x_destroy = x_destroy; 142 143 /* the other harmless ones */ 144 ops.x_getlong = (dummyfunc1) harmless; 145 ops.x_getbytes = (dummyfunc2) harmless; 146 147 x.x_op = XDR_ENCODE; 148 x.x_ops = &ops; 149 x.x_handy = 0; 150 x.x_private = (caddr_t) NULL; 151 x.x_base = (caddr_t) 0; 152 153 stat = func(&x, data); 154 if (x.x_private) 155 free(x.x_private); 156 return (stat == TRUE ? (unsigned) x.x_handy: 0); 157 } 158