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