175366Sdeischen/*	$NetBSD: xdr_reference.c,v 1.13 2000/01/22 22:19:18 mycroft Exp	$ */
274462Salfred
31902Swollman/*
41902Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
51902Swollman * unrestricted use provided that this legend is included on all tape
61902Swollman * media and as a part of the software program in whole or part.  Users
71902Swollman * may copy or modify Sun RPC without charge, but are not authorized
81902Swollman * to license or distribute it to anyone else except as part of a product or
91902Swollman * program developed by the user.
108870Srgrimes *
111902Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
121902Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
131902Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
148870Srgrimes *
151902Swollman * Sun RPC is provided with no support and without any obligation on the
161902Swollman * part of Sun Microsystems, Inc. to assist in its use, correction,
171902Swollman * modification or enhancement.
188870Srgrimes *
191902Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
201902Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
211902Swollman * OR ANY PART THEREOF.
228870Srgrimes *
231902Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue
241902Swollman * or profits or other special, indirect and consequential damages, even if
251902Swollman * Sun has been advised of the possibility of such damages.
268870Srgrimes *
271902Swollman * Sun Microsystems, Inc.
281902Swollman * 2550 Garcia Avenue
291902Swollman * Mountain View, California  94043
301902Swollman */
311902Swollman
3274462Salfred#if defined(LIBC_SCCS) && !defined(lint)
33136582Sobrienstatic char *sccsid2 = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
3492986Sobrienstatic char *sccsid = "@(#)xdr_reference.c	2.1 88/07/29 4.0 RPCSRC";
351902Swollman#endif
3692986Sobrien#include <sys/cdefs.h>
3792986Sobrien__FBSDID("$FreeBSD$");
381902Swollman
391902Swollman/*
401902Swollman * xdr_reference.c, Generic XDR routines impelmentation.
411902Swollman *
421902Swollman * Copyright (C) 1987, Sun Microsystems, Inc.
431902Swollman *
441902Swollman * These are the "non-trivial" xdr primitives used to serialize and de-serialize
451902Swollman * "pointers".  See xdr.h for more info on the interface to xdr.
461902Swollman */
471902Swollman
4874462Salfred#include "namespace.h"
4974462Salfred#include <err.h>
501902Swollman#include <stdio.h>
5111669Sphk#include <stdlib.h>
5211669Sphk#include <string.h>
5374462Salfred
541902Swollman#include <rpc/types.h>
551902Swollman#include <rpc/xdr.h>
5674462Salfred#include "libc_private.h"
571902Swollman
581902Swollman/*
591902Swollman * XDR an indirect pointer
601902Swollman * xdr_reference is for recursively translating a structure that is
611902Swollman * referenced by a pointer inside the structure that is currently being
621902Swollman * translated.  pp references a pointer to storage. If *pp is null
631902Swollman * the  necessary storage is allocated.
641902Swollman * size is the sizeof the referneced structure.
651902Swollman * proc is the routine to handle the referenced structure.
661902Swollman */
671902Swollmanbool_t
681902Swollmanxdr_reference(xdrs, pp, size, proc)
6974462Salfred	XDR *xdrs;
701902Swollman	caddr_t *pp;		/* the pointer to work on */
711902Swollman	u_int size;		/* size of the object pointed to */
721902Swollman	xdrproc_t proc;		/* xdr routine to handle the object */
731902Swollman{
7474462Salfred	caddr_t loc = *pp;
7574462Salfred	bool_t stat;
761902Swollman
771902Swollman	if (loc == NULL)
781902Swollman		switch (xdrs->x_op) {
791902Swollman		case XDR_FREE:
801902Swollman			return (TRUE);
811902Swollman
821902Swollman		case XDR_DECODE:
831902Swollman			*pp = loc = (caddr_t) mem_alloc(size);
841902Swollman			if (loc == NULL) {
8574462Salfred				warnx("xdr_reference: out of memory");
861902Swollman				return (FALSE);
871902Swollman			}
8874462Salfred			memset(loc, 0, size);
891902Swollman			break;
901902Swollman
9174462Salfred		case XDR_ENCODE:
9274462Salfred			break;
9374462Salfred		}
941902Swollman
9574462Salfred	stat = (*proc)(xdrs, loc);
9674462Salfred
971902Swollman	if (xdrs->x_op == XDR_FREE) {
981902Swollman		mem_free(loc, size);
991902Swollman		*pp = NULL;
1001902Swollman	}
1011902Swollman	return (stat);
1021902Swollman}
1031902Swollman
1041902Swollman
1051902Swollman/*
1061902Swollman * xdr_pointer():
1071902Swollman *
1081902Swollman * XDR a pointer to a possibly recursive data structure. This
1091902Swollman * differs with xdr_reference in that it can serialize/deserialiaze
1101902Swollman * trees correctly.
1111902Swollman *
1121902Swollman *  What's sent is actually a union:
1131902Swollman *
1141902Swollman *  union object_pointer switch (boolean b) {
1151902Swollman *  case TRUE: object_data data;
1161902Swollman *  case FALSE: void nothing;
1171902Swollman *  }
1181902Swollman *
1191902Swollman * > objpp: Pointer to the pointer to the object.
1201902Swollman * > obj_size: size of the object.
1211902Swollman * > xdr_obj: routine to XDR an object.
1221902Swollman *
1231902Swollman */
1241902Swollmanbool_t
1251902Swollmanxdr_pointer(xdrs,objpp,obj_size,xdr_obj)
12674462Salfred	XDR *xdrs;
1271902Swollman	char **objpp;
1281902Swollman	u_int obj_size;
1291902Swollman	xdrproc_t xdr_obj;
1301902Swollman{
1311902Swollman
1321902Swollman	bool_t more_data;
1331902Swollman
1341902Swollman	more_data = (*objpp != NULL);
1351902Swollman	if (! xdr_bool(xdrs,&more_data)) {
1361902Swollman		return (FALSE);
1371902Swollman	}
1381902Swollman	if (! more_data) {
1391902Swollman		*objpp = NULL;
1401902Swollman		return (TRUE);
1411902Swollman	}
1421902Swollman	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
1431902Swollman}
144