1177633Sdfr/* 2177633Sdfr * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3177633Sdfr * unrestricted use provided that this legend is included on all tape 4177633Sdfr * media and as a part of the software program in whole or part. Users 5177633Sdfr * may copy or modify Sun RPC without charge, but are not authorized 6177633Sdfr * to license or distribute it to anyone else except as part of a product or 7177633Sdfr * program developed by the user. 8177633Sdfr * 9177633Sdfr * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10177633Sdfr * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11177633Sdfr * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12177633Sdfr * 13177633Sdfr * Sun RPC is provided with no support and without any obligation on the 14177633Sdfr * part of Sun Microsystems, Inc. to assist in its use, correction, 15177633Sdfr * modification or enhancement. 16177633Sdfr * 17177633Sdfr * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18177633Sdfr * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19177633Sdfr * OR ANY PART THEREOF. 20177633Sdfr * 21177633Sdfr * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22177633Sdfr * or profits or other special, indirect and consequential damages, even if 23177633Sdfr * Sun has been advised of the possibility of such damages. 24177633Sdfr * 25177633Sdfr * Sun Microsystems, Inc. 26177633Sdfr * 2550 Garcia Avenue 27177633Sdfr * Mountain View, California 94043 28177633Sdfr */ 29177633Sdfr/* 30177633Sdfr * xdr_sizeof.c 31177633Sdfr * 32177633Sdfr * Copyright 1990 Sun Microsystems, Inc. 33177633Sdfr * 34177633Sdfr * General purpose routine to see how much space something will use 35177633Sdfr * when serialized using XDR. 36177633Sdfr */ 37177633Sdfr 38177633Sdfr#include <sys/cdefs.h> 39177633Sdfr__FBSDID("$FreeBSD: releng/10.2/sys/xdr/xdr_sizeof.c 177633 2008-03-26 15:23:12Z dfr $"); 40177633Sdfr 41177633Sdfr#include <sys/param.h> 42177633Sdfr#include <sys/systm.h> 43177633Sdfr#include <sys/malloc.h> 44177633Sdfr 45177633Sdfr#include <rpc/types.h> 46177633Sdfr#include <rpc/xdr.h> 47177633Sdfr 48177633Sdfr/* ARGSUSED */ 49177633Sdfrstatic bool_t 50177633Sdfrx_putlong(XDR *xdrs, const long *longp) 51177633Sdfr{ 52177633Sdfr 53177633Sdfr xdrs->x_handy += BYTES_PER_XDR_UNIT; 54177633Sdfr return (TRUE); 55177633Sdfr} 56177633Sdfr 57177633Sdfr/* ARGSUSED */ 58177633Sdfrstatic bool_t 59177633Sdfrx_putbytes(XDR *xdrs, const char *bp, u_int len) 60177633Sdfr{ 61177633Sdfr 62177633Sdfr xdrs->x_handy += len; 63177633Sdfr return (TRUE); 64177633Sdfr} 65177633Sdfr 66177633Sdfrstatic u_int 67177633Sdfrx_getpostn(XDR *xdrs) 68177633Sdfr{ 69177633Sdfr 70177633Sdfr return (xdrs->x_handy); 71177633Sdfr} 72177633Sdfr 73177633Sdfr/* ARGSUSED */ 74177633Sdfrstatic bool_t 75177633Sdfrx_setpostn(XDR *xdrs, u_int pos) 76177633Sdfr{ 77177633Sdfr 78177633Sdfr /* This is not allowed */ 79177633Sdfr return (FALSE); 80177633Sdfr} 81177633Sdfr 82177633Sdfrstatic int32_t * 83177633Sdfrx_inline(XDR *xdrs, u_int len) 84177633Sdfr{ 85177633Sdfr 86177633Sdfr if (len == 0) { 87177633Sdfr return (NULL); 88177633Sdfr } 89177633Sdfr if (xdrs->x_op != XDR_ENCODE) { 90177633Sdfr return (NULL); 91177633Sdfr } 92177633Sdfr if (len < (u_int)(uintptr_t)xdrs->x_base) { 93177633Sdfr /* x_private was already allocated */ 94177633Sdfr xdrs->x_handy += len; 95177633Sdfr return ((int32_t *) xdrs->x_private); 96177633Sdfr } else { 97177633Sdfr /* Free the earlier space and allocate new area */ 98177633Sdfr if (xdrs->x_private) 99177633Sdfr free(xdrs->x_private, M_RPC); 100177633Sdfr if ((xdrs->x_private = (caddr_t) malloc(len, M_RPC, M_WAITOK)) == NULL) { 101177633Sdfr xdrs->x_base = 0; 102177633Sdfr return (NULL); 103177633Sdfr } 104177633Sdfr xdrs->x_base = (caddr_t)(uintptr_t) len; 105177633Sdfr xdrs->x_handy += len; 106177633Sdfr return ((int32_t *) xdrs->x_private); 107177633Sdfr } 108177633Sdfr} 109177633Sdfr 110177633Sdfrstatic int 111177633Sdfrharmless(void) 112177633Sdfr{ 113177633Sdfr 114177633Sdfr /* Always return FALSE/NULL, as the case may be */ 115177633Sdfr return (0); 116177633Sdfr} 117177633Sdfr 118177633Sdfrstatic void 119177633Sdfrx_destroy(XDR *xdrs) 120177633Sdfr{ 121177633Sdfr 122177633Sdfr xdrs->x_handy = 0; 123177633Sdfr xdrs->x_base = 0; 124177633Sdfr if (xdrs->x_private) { 125177633Sdfr free(xdrs->x_private, M_RPC); 126177633Sdfr xdrs->x_private = NULL; 127177633Sdfr } 128177633Sdfr return; 129177633Sdfr} 130177633Sdfr 131177633Sdfrunsigned long 132177633Sdfrxdr_sizeof(xdrproc_t func, void *data) 133177633Sdfr{ 134177633Sdfr XDR x; 135177633Sdfr struct xdr_ops ops; 136177633Sdfr bool_t stat; 137177633Sdfr /* to stop ANSI-C compiler from complaining */ 138177633Sdfr typedef bool_t (* dummyfunc1)(XDR *, long *); 139177633Sdfr typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int); 140177633Sdfr 141177633Sdfr ops.x_putlong = x_putlong; 142177633Sdfr ops.x_putbytes = x_putbytes; 143177633Sdfr ops.x_inline = x_inline; 144177633Sdfr ops.x_getpostn = x_getpostn; 145177633Sdfr ops.x_setpostn = x_setpostn; 146177633Sdfr ops.x_destroy = x_destroy; 147177633Sdfr 148177633Sdfr /* the other harmless ones */ 149177633Sdfr ops.x_getlong = (dummyfunc1) harmless; 150177633Sdfr ops.x_getbytes = (dummyfunc2) harmless; 151177633Sdfr 152177633Sdfr x.x_op = XDR_ENCODE; 153177633Sdfr x.x_ops = &ops; 154177633Sdfr x.x_handy = 0; 155177633Sdfr x.x_private = (caddr_t) NULL; 156177633Sdfr x.x_base = (caddr_t) 0; 157177633Sdfr 158177633Sdfr stat = func(&x, data); 159177633Sdfr if (x.x_private) 160177633Sdfr free(x.x_private, M_RPC); 161177633Sdfr return (stat == TRUE ? (unsigned) x.x_handy: 0); 162177633Sdfr} 163