xref: /freebsd/lib/libc/xdr/xdr_sizeof.c (revision 8360efbd6c932013ffdb2f83d2f2de4278febb5e)
17d0a5a39SBill Paul /*
28360efbdSAlfred Perlstein  * $FreeBSD$
38360efbdSAlfred Perlstein  *
47d0a5a39SBill Paul  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
57d0a5a39SBill Paul  * unrestricted use provided that this legend is included on all tape
67d0a5a39SBill Paul  * media and as a part of the software program in whole or part.  Users
77d0a5a39SBill Paul  * may copy or modify Sun RPC without charge, but are not authorized
87d0a5a39SBill Paul  * to license or distribute it to anyone else except as part of a product or
97d0a5a39SBill Paul  * program developed by the user.
107d0a5a39SBill Paul  *
117d0a5a39SBill Paul  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
127d0a5a39SBill Paul  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
137d0a5a39SBill Paul  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
147d0a5a39SBill Paul  *
157d0a5a39SBill Paul  * Sun RPC is provided with no support and without any obligation on the
167d0a5a39SBill Paul  * part of Sun Microsystems, Inc. to assist in its use, correction,
177d0a5a39SBill Paul  * modification or enhancement.
187d0a5a39SBill Paul  *
197d0a5a39SBill Paul  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
207d0a5a39SBill Paul  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
217d0a5a39SBill Paul  * OR ANY PART THEREOF.
227d0a5a39SBill Paul  *
237d0a5a39SBill Paul  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
247d0a5a39SBill Paul  * or profits or other special, indirect and consequential damages, even if
257d0a5a39SBill Paul  * Sun has been advised of the possibility of such damages.
267d0a5a39SBill Paul  *
277d0a5a39SBill Paul  * Sun Microsystems, Inc.
287d0a5a39SBill Paul  * 2550 Garcia Avenue
297d0a5a39SBill Paul  * Mountain View, California  94043
307d0a5a39SBill Paul  */
317d0a5a39SBill Paul /*
327d0a5a39SBill Paul  * xdr_sizeof.c
337d0a5a39SBill Paul  *
347d0a5a39SBill Paul  * Copyright 1990 Sun Microsystems, Inc.
357d0a5a39SBill Paul  *
367d0a5a39SBill Paul  * General purpose routine to see how much space something will use
377d0a5a39SBill Paul  * when serialized using XDR.
387d0a5a39SBill Paul  */
397d0a5a39SBill Paul 
408360efbdSAlfred Perlstein #include "namespace.h"
417d0a5a39SBill Paul #include <rpc/types.h>
427d0a5a39SBill Paul #include <rpc/xdr.h>
437d0a5a39SBill Paul #include <sys/types.h>
447d0a5a39SBill Paul #include <stdlib.h>
458360efbdSAlfred Perlstein #include "un-namespace.h"
467d0a5a39SBill Paul 
477d0a5a39SBill Paul /* ARGSUSED */
487d0a5a39SBill Paul static bool_t
497d0a5a39SBill Paul x_putlong(xdrs, longp)
507d0a5a39SBill Paul 	XDR *xdrs;
517d0a5a39SBill Paul 	long *longp;
527d0a5a39SBill Paul {
537d0a5a39SBill Paul 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
547d0a5a39SBill Paul 	return (TRUE);
557d0a5a39SBill Paul }
567d0a5a39SBill Paul 
577d0a5a39SBill Paul /* ARGSUSED */
587d0a5a39SBill Paul static bool_t
597d0a5a39SBill Paul x_putbytes(xdrs, bp, len)
607d0a5a39SBill Paul 	XDR *xdrs;
617d0a5a39SBill Paul 	char  *bp;
627d0a5a39SBill Paul 	int len;
637d0a5a39SBill Paul {
647d0a5a39SBill Paul 	xdrs->x_handy += len;
657d0a5a39SBill Paul 	return (TRUE);
667d0a5a39SBill Paul }
677d0a5a39SBill Paul 
687d0a5a39SBill Paul static u_int
697d0a5a39SBill Paul x_getpostn(xdrs)
707d0a5a39SBill Paul 	XDR *xdrs;
717d0a5a39SBill Paul {
727d0a5a39SBill Paul 	return (xdrs->x_handy);
737d0a5a39SBill Paul }
747d0a5a39SBill Paul 
757d0a5a39SBill Paul /* ARGSUSED */
767d0a5a39SBill Paul static bool_t
777d0a5a39SBill Paul x_setpostn(xdrs, pos)
787d0a5a39SBill Paul 	XDR *xdrs;
797d0a5a39SBill Paul 	u_int pos;
807d0a5a39SBill Paul {
817d0a5a39SBill Paul 	/* This is not allowed */
827d0a5a39SBill Paul 	return (FALSE);
837d0a5a39SBill Paul }
847d0a5a39SBill Paul 
857d0a5a39SBill Paul static int32_t *
867d0a5a39SBill Paul x_inline(xdrs, len)
877d0a5a39SBill Paul 	XDR *xdrs;
889a6a1cbeSJohn Birrell 	long len;
897d0a5a39SBill Paul {
907d0a5a39SBill Paul 	if (len == 0) {
917d0a5a39SBill Paul 		return (NULL);
927d0a5a39SBill Paul 	}
937d0a5a39SBill Paul 	if (xdrs->x_op != XDR_ENCODE) {
947d0a5a39SBill Paul 		return (NULL);
957d0a5a39SBill Paul 	}
969a6a1cbeSJohn Birrell 	if (len < (long) xdrs->x_base) {
977d0a5a39SBill Paul 		/* x_private was already allocated */
987d0a5a39SBill Paul 		xdrs->x_handy += len;
997d0a5a39SBill Paul 		return ((int32_t *) xdrs->x_private);
1007d0a5a39SBill Paul 	} else {
1017d0a5a39SBill Paul 		/* Free the earlier space and allocate new area */
1027d0a5a39SBill Paul 		if (xdrs->x_private)
1037d0a5a39SBill Paul 			free(xdrs->x_private);
1047d0a5a39SBill Paul 		if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
1057d0a5a39SBill Paul 			xdrs->x_base = 0;
1067d0a5a39SBill Paul 			return (NULL);
1077d0a5a39SBill Paul 		}
1087d0a5a39SBill Paul 		xdrs->x_base = (caddr_t) len;
1097d0a5a39SBill Paul 		xdrs->x_handy += len;
1107d0a5a39SBill Paul 		return ((int32_t *) xdrs->x_private);
1117d0a5a39SBill Paul 	}
1127d0a5a39SBill Paul }
1137d0a5a39SBill Paul 
1147d0a5a39SBill Paul static int
1157d0a5a39SBill Paul harmless()
1167d0a5a39SBill Paul {
1177d0a5a39SBill Paul 	/* Always return FALSE/NULL, as the case may be */
1187d0a5a39SBill Paul 	return (0);
1197d0a5a39SBill Paul }
1207d0a5a39SBill Paul 
1217d0a5a39SBill Paul static void
1227d0a5a39SBill Paul x_destroy(xdrs)
1237d0a5a39SBill Paul 	XDR *xdrs;
1247d0a5a39SBill Paul {
1257d0a5a39SBill Paul 	xdrs->x_handy = 0;
1267d0a5a39SBill Paul 	xdrs->x_base = 0;
1277d0a5a39SBill Paul 	if (xdrs->x_private) {
1287d0a5a39SBill Paul 		free(xdrs->x_private);
1297d0a5a39SBill Paul 		xdrs->x_private = NULL;
1307d0a5a39SBill Paul 	}
1317d0a5a39SBill Paul 	return;
1327d0a5a39SBill Paul }
1337d0a5a39SBill Paul 
1347d0a5a39SBill Paul unsigned long
1357d0a5a39SBill Paul xdr_sizeof(func, data)
1367d0a5a39SBill Paul 	xdrproc_t func;
1377d0a5a39SBill Paul 	void *data;
1387d0a5a39SBill Paul {
1397d0a5a39SBill Paul 	XDR x;
1407d0a5a39SBill Paul 	struct xdr_ops ops;
1417d0a5a39SBill Paul 	bool_t stat;
1427d0a5a39SBill Paul 	/* to stop ANSI-C compiler from complaining */
1437d0a5a39SBill Paul 	typedef  bool_t (* dummyfunc1)(XDR *, long *);
1447d0a5a39SBill Paul 	typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
1457d0a5a39SBill Paul 
1467d0a5a39SBill Paul 	ops.x_putlong = x_putlong;
1477d0a5a39SBill Paul 	ops.x_putbytes = x_putbytes;
1487d0a5a39SBill Paul 	ops.x_inline = x_inline;
1497d0a5a39SBill Paul 	ops.x_getpostn = x_getpostn;
1507d0a5a39SBill Paul 	ops.x_setpostn = x_setpostn;
1517d0a5a39SBill Paul 	ops.x_destroy = x_destroy;
1527d0a5a39SBill Paul 
1537d0a5a39SBill Paul 	/* the other harmless ones */
1547d0a5a39SBill Paul 	ops.x_getlong =  (dummyfunc1) harmless;
1557d0a5a39SBill Paul 	ops.x_getbytes = (dummyfunc2) harmless;
1567d0a5a39SBill Paul 
1577d0a5a39SBill Paul 	x.x_op = XDR_ENCODE;
1587d0a5a39SBill Paul 	x.x_ops = &ops;
1597d0a5a39SBill Paul 	x.x_handy = 0;
1607d0a5a39SBill Paul 	x.x_private = (caddr_t) NULL;
1617d0a5a39SBill Paul 	x.x_base = (caddr_t) 0;
1627d0a5a39SBill Paul 
1637d0a5a39SBill Paul 	stat = func(&x, data);
1647d0a5a39SBill Paul 	if (x.x_private)
1657d0a5a39SBill Paul 		free(x.x_private);
1667d0a5a39SBill Paul 	return (stat == TRUE ? (unsigned) x.x_handy: 0);
1677d0a5a39SBill Paul }
168