Back to index

glibc  2.9
xdr_sizeof.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 /*
00030  * xdr_sizeof.c
00031  *
00032  * Copyright 1990 Sun Microsystems, Inc.
00033  *
00034  * General purpose routine to see how much space something will use
00035  * when serialized using XDR.
00036  */
00037 
00038 #include <rpc/types.h>
00039 #include <rpc/xdr.h>
00040 #include <sys/types.h>
00041 #include <stdlib.h>
00042 
00043 /* ARGSUSED */
00044 static bool_t
00045 x_putlong (XDR *xdrs, const long *longp)
00046 {
00047   xdrs->x_handy += BYTES_PER_XDR_UNIT;
00048   return TRUE;
00049 }
00050 
00051 /* ARGSUSED */
00052 static bool_t
00053 x_putbytes (XDR *xdrs, const char *bp, u_int len)
00054 {
00055   xdrs->x_handy += len;
00056   return TRUE;
00057 }
00058 
00059 static u_int
00060 x_getpostn (const XDR *xdrs)
00061 {
00062   return xdrs->x_handy;
00063 }
00064 
00065 /* ARGSUSED */
00066 static bool_t
00067 x_setpostn (XDR *xdrs, u_int len)
00068 {
00069   /* This is not allowed */
00070   return FALSE;
00071 }
00072 
00073 static int32_t *
00074 x_inline (XDR *xdrs, u_int len)
00075 {
00076   if (len == 0)
00077     return NULL;
00078   if (xdrs->x_op != XDR_ENCODE)
00079     return NULL;
00080   if (len < (u_int) (long int) xdrs->x_base)
00081     {
00082       /* x_private was already allocated */
00083       xdrs->x_handy += len;
00084       return (int32_t *) xdrs->x_private;
00085     }
00086   else
00087     {
00088       /* Free the earlier space and allocate new area */
00089       free (xdrs->x_private);
00090       if ((xdrs->x_private = (caddr_t) malloc (len)) == NULL)
00091        {
00092          xdrs->x_base = 0;
00093          return NULL;
00094        }
00095       xdrs->x_base = (void *) (long) len;
00096       xdrs->x_handy += len;
00097       return (int32_t *) xdrs->x_private;
00098     }
00099 }
00100 
00101 static int
00102 harmless (void)
00103 {
00104   /* Always return FALSE/NULL, as the case may be */
00105   return 0;
00106 }
00107 
00108 static void
00109 x_destroy (XDR *xdrs)
00110 {
00111   xdrs->x_handy = 0;
00112   xdrs->x_base = 0;
00113   if (xdrs->x_private)
00114     {
00115       free (xdrs->x_private);
00116       xdrs->x_private = NULL;
00117     }
00118   return;
00119 }
00120 
00121 static bool_t
00122 x_putint32 (XDR *xdrs, const int32_t *int32p)
00123 {
00124   xdrs->x_handy += BYTES_PER_XDR_UNIT;
00125   return TRUE;
00126 }
00127 
00128 unsigned long
00129 xdr_sizeof (xdrproc_t func, void *data)
00130 {
00131   XDR x;
00132   struct xdr_ops ops;
00133   bool_t stat;
00134   /* to stop ANSI-C compiler from complaining */
00135   typedef bool_t (*dummyfunc1) (XDR *, long *);
00136   typedef bool_t (*dummyfunc2) (XDR *, caddr_t, u_int);
00137   typedef bool_t (*dummyfunc3) (XDR *, int32_t *);
00138 
00139   ops.x_putlong = x_putlong;
00140   ops.x_putbytes = x_putbytes;
00141   ops.x_inline = x_inline;
00142   ops.x_getpostn = x_getpostn;
00143   ops.x_setpostn = x_setpostn;
00144   ops.x_destroy = x_destroy;
00145   ops.x_putint32 = x_putint32;
00146 
00147   /* the other harmless ones */
00148   ops.x_getlong = (dummyfunc1) harmless;
00149   ops.x_getbytes = (dummyfunc2) harmless;
00150   ops.x_getint32 = (dummyfunc3) harmless;
00151 
00152   x.x_op = XDR_ENCODE;
00153   x.x_ops = &ops;
00154   x.x_handy = 0;
00155   x.x_private = (caddr_t) NULL;
00156   x.x_base = (caddr_t) 0;
00157 
00158   stat = func (&x, data);
00159   free (x.x_private);
00160   return stat == TRUE ? x.x_handy : 0;
00161 }