1a204967aSHiroki Sato /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 4a204967aSHiroki Sato * Copyright (c) 2010, Oracle America, Inc. 57d0a5a39SBill Paul * 6a204967aSHiroki Sato * Redistribution and use in source and binary forms, with or without 7a204967aSHiroki Sato * modification, are permitted provided that the following conditions are 8a204967aSHiroki Sato * met: 97d0a5a39SBill Paul * 10a204967aSHiroki Sato * * Redistributions of source code must retain the above copyright 11a204967aSHiroki Sato * notice, this list of conditions and the following disclaimer. 12a204967aSHiroki Sato * * Redistributions in binary form must reproduce the above 13a204967aSHiroki Sato * copyright notice, this list of conditions and the following 14a204967aSHiroki Sato * disclaimer in the documentation and/or other materials 15a204967aSHiroki Sato * provided with the distribution. 16a204967aSHiroki Sato * * Neither the name of the "Oracle America, Inc." nor the names of its 17a204967aSHiroki Sato * contributors may be used to endorse or promote products derived 18a204967aSHiroki Sato * from this software without specific prior written permission. 197d0a5a39SBill Paul * 20a204967aSHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21a204967aSHiroki Sato * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22a204967aSHiroki Sato * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23a204967aSHiroki Sato * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24a204967aSHiroki Sato * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25a204967aSHiroki Sato * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26a204967aSHiroki Sato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27a204967aSHiroki Sato * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28a204967aSHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29a204967aSHiroki Sato * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30a204967aSHiroki Sato * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31a204967aSHiroki Sato * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 327d0a5a39SBill Paul */ 337d0a5a39SBill Paul /* 347d0a5a39SBill Paul * xdr_sizeof.c 357d0a5a39SBill Paul * 367d0a5a39SBill Paul * General purpose routine to see how much space something will use 377d0a5a39SBill Paul * when serialized using XDR. 387d0a5a39SBill Paul */ 397d0a5a39SBill Paul 40333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 41333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 42333fc21eSDavid E. O'Brien 438360efbdSAlfred Perlstein #include "namespace.h" 447d0a5a39SBill Paul #include <rpc/types.h> 457d0a5a39SBill Paul #include <rpc/xdr.h> 467d0a5a39SBill Paul #include <sys/types.h> 477d0a5a39SBill Paul #include <stdlib.h> 488360efbdSAlfred Perlstein #include "un-namespace.h" 497d0a5a39SBill Paul 507d0a5a39SBill Paul /* ARGSUSED */ 517d0a5a39SBill Paul static bool_t 529c75ed7fSCraig Rodrigues x_putlong(XDR *xdrs, const long *longp) 537d0a5a39SBill Paul { 547d0a5a39SBill Paul xdrs->x_handy += BYTES_PER_XDR_UNIT; 557d0a5a39SBill Paul return (TRUE); 567d0a5a39SBill Paul } 577d0a5a39SBill Paul 587d0a5a39SBill Paul /* ARGSUSED */ 597d0a5a39SBill Paul static bool_t 609c75ed7fSCraig Rodrigues x_putbytes(XDR *xdrs, const char *bp, u_int len) 617d0a5a39SBill Paul { 627d0a5a39SBill Paul xdrs->x_handy += len; 637d0a5a39SBill Paul return (TRUE); 647d0a5a39SBill Paul } 657d0a5a39SBill Paul 667d0a5a39SBill Paul static u_int 67d660d38dSCraig Rodrigues x_getpostn(XDR *xdrs) 687d0a5a39SBill Paul { 697d0a5a39SBill Paul return (xdrs->x_handy); 707d0a5a39SBill Paul } 717d0a5a39SBill Paul 727d0a5a39SBill Paul /* ARGSUSED */ 737d0a5a39SBill Paul static bool_t 74d660d38dSCraig Rodrigues x_setpostn(XDR *xdrs, u_int pos) 757d0a5a39SBill Paul { 767d0a5a39SBill Paul /* This is not allowed */ 777d0a5a39SBill Paul return (FALSE); 787d0a5a39SBill Paul } 797d0a5a39SBill Paul 807d0a5a39SBill Paul static int32_t * 81d660d38dSCraig Rodrigues x_inline(XDR *xdrs, u_int len) 827d0a5a39SBill Paul { 837d0a5a39SBill Paul if (len == 0) { 847d0a5a39SBill Paul return (NULL); 857d0a5a39SBill Paul } 867d0a5a39SBill Paul if (xdrs->x_op != XDR_ENCODE) { 877d0a5a39SBill Paul return (NULL); 887d0a5a39SBill Paul } 89d98b6d6eSKevin Lo if (len < (u_int)(uintptr_t)xdrs->x_base) { 907d0a5a39SBill Paul /* x_private was already allocated */ 917d0a5a39SBill Paul xdrs->x_handy += len; 927d0a5a39SBill Paul return ((int32_t *) xdrs->x_private); 937d0a5a39SBill Paul } else { 947d0a5a39SBill Paul /* Free the earlier space and allocate new area */ 957d0a5a39SBill Paul if (xdrs->x_private) 967d0a5a39SBill Paul free(xdrs->x_private); 977d0a5a39SBill Paul if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) { 987d0a5a39SBill Paul xdrs->x_base = 0; 997d0a5a39SBill Paul return (NULL); 1007d0a5a39SBill Paul } 101d98b6d6eSKevin Lo xdrs->x_base = (caddr_t)(uintptr_t)len; 1027d0a5a39SBill Paul xdrs->x_handy += len; 1037d0a5a39SBill Paul return ((int32_t *) xdrs->x_private); 1047d0a5a39SBill Paul } 1057d0a5a39SBill Paul } 1067d0a5a39SBill Paul 1077d0a5a39SBill Paul static int 10873fe1304SCraig Rodrigues harmless(void) 1097d0a5a39SBill Paul { 1107d0a5a39SBill Paul /* Always return FALSE/NULL, as the case may be */ 1117d0a5a39SBill Paul return (0); 1127d0a5a39SBill Paul } 1137d0a5a39SBill Paul 1147d0a5a39SBill Paul static void 115d660d38dSCraig Rodrigues x_destroy(XDR *xdrs) 1167d0a5a39SBill Paul { 1177d0a5a39SBill Paul xdrs->x_handy = 0; 1187d0a5a39SBill Paul xdrs->x_base = 0; 1197d0a5a39SBill Paul if (xdrs->x_private) { 1207d0a5a39SBill Paul free(xdrs->x_private); 1217d0a5a39SBill Paul xdrs->x_private = NULL; 1227d0a5a39SBill Paul } 1237d0a5a39SBill Paul return; 1247d0a5a39SBill Paul } 1257d0a5a39SBill Paul 1267d0a5a39SBill Paul unsigned long 127d660d38dSCraig Rodrigues xdr_sizeof(xdrproc_t func, void *data) 1287d0a5a39SBill Paul { 1297d0a5a39SBill Paul XDR x; 1307d0a5a39SBill Paul struct xdr_ops ops; 1317d0a5a39SBill Paul bool_t stat; 1327d0a5a39SBill Paul /* to stop ANSI-C compiler from complaining */ 1337d0a5a39SBill Paul typedef bool_t (* dummyfunc1)(XDR *, long *); 1347d0a5a39SBill Paul typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int); 1357d0a5a39SBill Paul 1367d0a5a39SBill Paul ops.x_putlong = x_putlong; 1377d0a5a39SBill Paul ops.x_putbytes = x_putbytes; 1387d0a5a39SBill Paul ops.x_inline = x_inline; 1397d0a5a39SBill Paul ops.x_getpostn = x_getpostn; 1407d0a5a39SBill Paul ops.x_setpostn = x_setpostn; 1417d0a5a39SBill Paul ops.x_destroy = x_destroy; 1427d0a5a39SBill Paul 1437d0a5a39SBill Paul /* the other harmless ones */ 1447d0a5a39SBill Paul ops.x_getlong = (dummyfunc1) harmless; 1457d0a5a39SBill Paul ops.x_getbytes = (dummyfunc2) harmless; 1467d0a5a39SBill Paul 1477d0a5a39SBill Paul x.x_op = XDR_ENCODE; 1487d0a5a39SBill Paul x.x_ops = &ops; 1497d0a5a39SBill Paul x.x_handy = 0; 1507d0a5a39SBill Paul x.x_private = (caddr_t) NULL; 1517d0a5a39SBill Paul x.x_base = (caddr_t) 0; 1527d0a5a39SBill Paul 1537d0a5a39SBill Paul stat = func(&x, data); 1547d0a5a39SBill Paul if (x.x_private) 1557d0a5a39SBill Paul free(x.x_private); 1567d0a5a39SBill Paul return (stat == TRUE ? (unsigned) x.x_handy: 0); 1577d0a5a39SBill Paul } 158