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