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