Home | History | Annotate | Line # | Download | only in rpc
xdr_sizeof.c revision 1.2
      1 /*
      2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      3  * unrestricted use provided that this legend is included on all tape
      4  * media and as a part of the software program in whole or part.  Users
      5  * may copy or modify Sun RPC without charge, but are not authorized
      6  * to license or distribute it to anyone else except as part of a product or
      7  * program developed by the user.
      8  *
      9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     12  *
     13  * Sun RPC is provided with no support and without any obligation on the
     14  * part of Sun Microsystems, Inc. to assist in its use, correction,
     15  * modification or enhancement.
     16  *
     17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     19  * OR ANY PART THEREOF.
     20  *
     21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     22  * or profits or other special, indirect and consequential damages, even if
     23  * Sun has been advised of the possibility of such damages.
     24  *
     25  * Sun Microsystems, Inc.
     26  * 2550 Garcia Avenue
     27  * Mountain View, California  94043
     28  */
     29 /*
     30  * xdr_sizeof.c
     31  *
     32  * Copyright 1990 Sun Microsystems, Inc.
     33  *
     34  * General purpose routine to see how much space something will use
     35  * when serialized using XDR.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 #if 0
     40 __FBSDID("$FreeBSD: src/lib/libc/xdr/xdr_sizeof.c,v 1.5.38.1 2010/12/21 17:10:29 kensmith Exp $");
     41 #else
     42 __RCSID("$NetBSD: xdr_sizeof.c,v 1.2 2011/07/04 11:01:40 mrg Exp $");
     43 #endif
     44 
     45 #include "namespace.h"
     46 #include <rpc/types.h>
     47 #include <rpc/xdr.h>
     48 #include <sys/types.h>
     49 #include <stdlib.h>
     50 
     51 #ifdef __weak_alias
     52 __weak_alias(xdr_sizeof,_xdr_sizeof)
     53 #endif
     54 
     55 static bool_t x_putlong(XDR *, const long *);
     56 static bool_t x_putbytes(XDR *, const char *, u_int);
     57 static u_int x_getpostn(XDR *);
     58 static bool_t x_setpostn(XDR *, u_int);
     59 static int32_t *x_inline(XDR *, u_int);
     60 static int harmless(void);
     61 static void x_destroy(XDR *);
     62 
     63 /* ARGSUSED */
     64 static bool_t
     65 x_putlong(xdrs, longp)
     66 	XDR *xdrs;
     67 	const long *longp;
     68 {
     69 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
     70 	return (TRUE);
     71 }
     72 
     73 /* ARGSUSED */
     74 static bool_t
     75 x_putbytes(xdrs, bp, len)
     76 	XDR *xdrs;
     77 	const char  *bp;
     78 	u_int len;
     79 {
     80 	xdrs->x_handy += len;
     81 	return (TRUE);
     82 }
     83 
     84 static u_int
     85 x_getpostn(xdrs)
     86 	XDR *xdrs;
     87 {
     88 	return (xdrs->x_handy);
     89 }
     90 
     91 /* ARGSUSED */
     92 static bool_t
     93 x_setpostn(xdrs, pos)
     94 	XDR *xdrs;
     95 	u_int pos;
     96 {
     97 	/* This is not allowed */
     98 	return (FALSE);
     99 }
    100 
    101 static int32_t *
    102 x_inline(xdrs, len)
    103 	XDR *xdrs;
    104 	u_int len;
    105 {
    106 	if (len == 0) {
    107 		return (NULL);
    108 	}
    109 	if (xdrs->x_op != XDR_ENCODE) {
    110 		return (NULL);
    111 	}
    112 	if (len < (u_int)(uintptr_t)xdrs->x_base) {
    113 		/* x_private was already allocated */
    114 		xdrs->x_handy += len;
    115 		return ((int32_t *) xdrs->x_private);
    116 	} else {
    117 		/* Free the earlier space and allocate new area */
    118 		if (xdrs->x_private)
    119 			free(xdrs->x_private);
    120 		if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
    121 			xdrs->x_base = 0;
    122 			return (NULL);
    123 		}
    124 		xdrs->x_base = (caddr_t)(uintptr_t)len;
    125 		xdrs->x_handy += len;
    126 		return ((int32_t *) xdrs->x_private);
    127 	}
    128 }
    129 
    130 static int
    131 harmless()
    132 {
    133 	/* Always return FALSE/NULL, as the case may be */
    134 	return (0);
    135 }
    136 
    137 static void
    138 x_destroy(xdrs)
    139 	XDR *xdrs;
    140 {
    141 	xdrs->x_handy = 0;
    142 	xdrs->x_base = 0;
    143 	if (xdrs->x_private) {
    144 		free(xdrs->x_private);
    145 		xdrs->x_private = NULL;
    146 	}
    147 	return;
    148 }
    149 
    150 unsigned long
    151 xdr_sizeof(func, data)
    152 	xdrproc_t func;
    153 	void *data;
    154 {
    155 	XDR x;
    156 	struct xdr_ops ops;
    157 	bool_t stat;
    158 	/* to stop ANSI-C compiler from complaining */
    159 	typedef  bool_t (* dummyfunc1)(XDR *, long *);
    160 	typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
    161 
    162 	ops.x_putlong = x_putlong;
    163 	ops.x_putbytes = x_putbytes;
    164 	ops.x_inline = x_inline;
    165 	ops.x_getpostn = x_getpostn;
    166 	ops.x_setpostn = x_setpostn;
    167 	ops.x_destroy = x_destroy;
    168 
    169 	/* the other harmless ones */
    170 	ops.x_getlong =  (dummyfunc1) harmless;
    171 	ops.x_getbytes = (dummyfunc2) harmless;
    172 
    173 	x.x_op = XDR_ENCODE;
    174 	x.x_ops = &ops;
    175 	x.x_handy = 0;
    176 	x.x_private = (caddr_t) NULL;
    177 	x.x_base = (caddr_t) 0;
    178 
    179 	stat = func(&x, data);
    180 	if (x.x_private)
    181 		free(x.x_private);
    182 	return (stat == TRUE ? (unsigned) x.x_handy: 0);
    183 }
    184