Home | History | Annotate | Line # | Download | only in rpc
xdr_sizeof.c revision 1.4
      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.4 2012/03/20 17:14:50 matt 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(XDR *xdrs, const long *longp)
     66 {
     67 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
     68 	return (TRUE);
     69 }
     70 
     71 /* ARGSUSED */
     72 static bool_t
     73 x_putbytes(XDR *xdrs, const char *bp, u_int len)
     74 {
     75 	xdrs->x_handy += len;
     76 	return (TRUE);
     77 }
     78 
     79 static u_int
     80 x_getpostn(XDR *xdrs)
     81 {
     82 	return (xdrs->x_handy);
     83 }
     84 
     85 /* ARGSUSED */
     86 static bool_t
     87 x_setpostn(XDR *xdrs, u_int pos)
     88 {
     89 	/* This is not allowed */
     90 	return (FALSE);
     91 }
     92 
     93 static int32_t *
     94 x_inline(XDR *xdrs, u_int len)
     95 {
     96 	if (len == 0) {
     97 		return (NULL);
     98 	}
     99 	if (xdrs->x_op != XDR_ENCODE) {
    100 		return (NULL);
    101 	}
    102 	if (len < (u_int)(uintptr_t)xdrs->x_base) {
    103 		/* x_private was already allocated */
    104 		xdrs->x_handy += len;
    105 		return ((int32_t *) xdrs->x_private);
    106 	} else {
    107 		/* Free the earlier space and allocate new area */
    108 		if (xdrs->x_private)
    109 			free(xdrs->x_private);
    110 		if ((xdrs->x_private = malloc(len)) == NULL) {
    111 			xdrs->x_base = 0;
    112 			return (NULL);
    113 		}
    114 		xdrs->x_base = (caddr_t)(uintptr_t)len;
    115 		xdrs->x_handy += len;
    116 		return ((int32_t *) xdrs->x_private);
    117 	}
    118 }
    119 
    120 static int
    121 harmless(void)
    122 {
    123 	/* Always return FALSE/NULL, as the case may be */
    124 	return (0);
    125 }
    126 
    127 static void
    128 x_destroy(XDR *xdrs)
    129 {
    130 	xdrs->x_handy = 0;
    131 	xdrs->x_base = 0;
    132 	if (xdrs->x_private) {
    133 		free(xdrs->x_private);
    134 		xdrs->x_private = NULL;
    135 	}
    136 	return;
    137 }
    138 
    139 unsigned long
    140 xdr_sizeof(xdrproc_t func, void *data)
    141 {
    142 	XDR x;
    143 	struct xdr_ops ops;
    144 	bool_t stat;
    145 	/* to stop ANSI-C compiler from complaining */
    146 	typedef  bool_t (* dummyfunc1)(XDR *, long *);
    147 	typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
    148 
    149 	ops.x_putlong = x_putlong;
    150 	ops.x_putbytes = x_putbytes;
    151 	ops.x_inline = x_inline;
    152 	ops.x_getpostn = x_getpostn;
    153 	ops.x_setpostn = x_setpostn;
    154 	ops.x_destroy = x_destroy;
    155 
    156 	/* the other harmless ones */
    157 	ops.x_getlong =  (dummyfunc1) harmless;
    158 	ops.x_getbytes = (dummyfunc2) harmless;
    159 
    160 	x.x_op = XDR_ENCODE;
    161 	x.x_ops = &ops;
    162 	x.x_handy = 0;
    163 	x.x_private = (caddr_t) NULL;
    164 	x.x_base = (caddr_t) 0;
    165 
    166 	stat = func(&x, data);
    167 	if (x.x_private)
    168 		free(x.x_private);
    169 	return (stat == TRUE ? (unsigned) x.x_handy: 0);
    170 }
    171