1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7 * Reserved.  This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License").  You may not use this file
10 * except in compliance with the License.  Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25/*
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part.  Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
32 *
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 *
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
40 *
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
44 *
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California  94043
52 */
53/*
54 * xdr_sizeof.c
55 *
56 * Copyright 1990 Sun Microsystems, Inc.
57 *
58 * General purpose routine to see how much space something will use
59 * when serialized using XDR.
60 */
61
62#include <sys/cdefs.h>
63
64#include <rpc/types.h>
65#include <rpc/xdr.h>
66#include <sys/types.h>
67#include <stdlib.h>
68
69/* ARGSUSED */
70static bool_t
71x_putlong(xdrs, longp)
72	XDR *xdrs;
73#ifdef __LP64__
74	int *longp;
75#else
76	long *longp;
77#endif
78{
79	xdrs->x_handy += BYTES_PER_XDR_UNIT;
80	return (TRUE);
81}
82
83/* ARGSUSED */
84static bool_t
85x_putbytes(xdrs, bp, len)
86	XDR *xdrs;
87	char  *bp;
88	u_int len;
89{
90	xdrs->x_handy += len;
91	return (TRUE);
92}
93
94static u_int
95x_getpostn(xdrs)
96	XDR *xdrs;
97{
98	return (xdrs->x_handy);
99}
100
101/* ARGSUSED */
102static bool_t
103x_setpostn(xdrs, pos)
104	XDR *xdrs;
105	u_int pos;
106{
107	/* This is not allowed */
108	return (FALSE);
109}
110
111static int32_t *
112x_inline(xdrs, len)
113	XDR *xdrs;
114	u_int len;
115{
116	size_t llen;
117
118	if (len == 0) {
119		return (NULL);
120	}
121	if (xdrs->x_op != XDR_ENCODE) {
122		return (NULL);
123	}
124
125	llen = len;
126
127	if (llen < (size_t)xdrs->x_base) {
128		/* x_private was already allocated */
129		xdrs->x_handy += llen;
130		return ((int32_t *) xdrs->x_private);
131	} else {
132		/* Free the earlier space and allocate new area */
133		if (xdrs->x_private)
134			free(xdrs->x_private);
135		if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
136			xdrs->x_base = 0;
137			return (NULL);
138		}
139		xdrs->x_base = (caddr_t)llen;
140		xdrs->x_handy += llen;
141		return ((int32_t *) xdrs->x_private);
142	}
143}
144
145static int
146harmless()
147{
148	/* Always return FALSE/NULL, as the case may be */
149	return (0);
150}
151
152static void
153x_destroy(xdrs)
154	XDR *xdrs;
155{
156	xdrs->x_handy = 0;
157	xdrs->x_base = 0;
158	if (xdrs->x_private) {
159		free(xdrs->x_private);
160		xdrs->x_private = NULL;
161	}
162	return;
163}
164
165#ifdef __LP64__
166unsigned int
167#else
168unsigned long
169#endif
170xdr_sizeof(func, data)
171	xdrproc_t func;
172	void *data;
173{
174	XDR x;
175	struct xdr_ops ops;
176	bool_t stat;
177	/* to stop ANSI-C compiler from complaining */
178#ifdef __LP64__
179	typedef  bool_t (* dummyfunc1)(XDR *, int *);
180#else
181	typedef  bool_t (* dummyfunc1)(XDR *, long *);
182#endif
183	typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
184
185	ops.x_putlong = x_putlong;
186	ops.x_putbytes = x_putbytes;
187	ops.x_inline = x_inline;
188	ops.x_getpostn = x_getpostn;
189	ops.x_setpostn = x_setpostn;
190	ops.x_destroy = x_destroy;
191
192	/* the other harmless ones */
193	ops.x_getlong =  (dummyfunc1) harmless;
194	ops.x_getbytes = (dummyfunc2) harmless;
195
196	x.x_op = XDR_ENCODE;
197	x.x_ops = &ops;
198	x.x_handy = 0;
199	x.x_private = (caddr_t) NULL;
200	x.x_base = (caddr_t) 0;
201
202	stat = func(&x, data, 0);
203	if (x.x_private)
204		free(x.x_private);
205	return (stat == TRUE ? (unsigned) x.x_handy: 0);
206}
207