1177633Sdfr/*	$NetBSD: xdr_reference.c,v 1.13 2000/01/22 22:19:18 mycroft Exp	$ */
2177633Sdfr
3177633Sdfr/*
4177633Sdfr * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5177633Sdfr * unrestricted use provided that this legend is included on all tape
6177633Sdfr * media and as a part of the software program in whole or part.  Users
7177633Sdfr * may copy or modify Sun RPC without charge, but are not authorized
8177633Sdfr * to license or distribute it to anyone else except as part of a product or
9177633Sdfr * program developed by the user.
10177633Sdfr *
11177633Sdfr * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12177633Sdfr * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13177633Sdfr * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14177633Sdfr *
15177633Sdfr * Sun RPC is provided with no support and without any obligation on the
16177633Sdfr * part of Sun Microsystems, Inc. to assist in its use, correction,
17177633Sdfr * modification or enhancement.
18177633Sdfr *
19177633Sdfr * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20177633Sdfr * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21177633Sdfr * OR ANY PART THEREOF.
22177633Sdfr *
23177633Sdfr * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24177633Sdfr * or profits or other special, indirect and consequential damages, even if
25177633Sdfr * Sun has been advised of the possibility of such damages.
26177633Sdfr *
27177633Sdfr * Sun Microsystems, Inc.
28177633Sdfr * 2550 Garcia Avenue
29177633Sdfr * Mountain View, California  94043
30177633Sdfr */
31177633Sdfr
32177633Sdfr#if defined(LIBC_SCCS) && !defined(lint)
33177633Sdfrstatic char *sccsid2 = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
34177633Sdfrstatic char *sccsid = "@(#)xdr_reference.c	2.1 88/07/29 4.0 RPCSRC";
35177633Sdfr#endif
36177633Sdfr#include <sys/cdefs.h>
37177633Sdfr__FBSDID("$FreeBSD$");
38177633Sdfr
39177633Sdfr/*
40177633Sdfr * xdr_reference.c, Generic XDR routines impelmentation.
41177633Sdfr *
42177633Sdfr * Copyright (C) 1987, Sun Microsystems, Inc.
43177633Sdfr *
44177633Sdfr * These are the "non-trivial" xdr primitives used to serialize and de-serialize
45177633Sdfr * "pointers".  See xdr.h for more info on the interface to xdr.
46177633Sdfr */
47177633Sdfr
48177633Sdfr#include <sys/param.h>
49177633Sdfr#include <sys/systm.h>
50177633Sdfr#include <sys/malloc.h>
51177633Sdfr
52177633Sdfr#include <rpc/types.h>
53177633Sdfr#include <rpc/xdr.h>
54177633Sdfr
55177633Sdfr/*
56177633Sdfr * XDR an indirect pointer
57177633Sdfr * xdr_reference is for recursively translating a structure that is
58177633Sdfr * referenced by a pointer inside the structure that is currently being
59177633Sdfr * translated.  pp references a pointer to storage. If *pp is null
60177633Sdfr * the  necessary storage is allocated.
61177633Sdfr * size is the sizeof the referneced structure.
62177633Sdfr * proc is the routine to handle the referenced structure.
63177633Sdfr */
64177633Sdfrbool_t
65177633Sdfrxdr_reference(XDR *xdrs,
66177633Sdfr    caddr_t *pp,		/* the pointer to work on */
67177633Sdfr    u_int size,			/* size of the object pointed to */
68177633Sdfr    xdrproc_t proc)		/* xdr routine to handle the object */
69177633Sdfr{
70177633Sdfr	caddr_t loc = *pp;
71177633Sdfr	bool_t stat;
72177633Sdfr
73177633Sdfr	if (loc == NULL)
74177633Sdfr		switch (xdrs->x_op) {
75177633Sdfr		case XDR_FREE:
76177633Sdfr			return (TRUE);
77177633Sdfr
78177633Sdfr		case XDR_DECODE:
79177633Sdfr			*pp = loc = (caddr_t) mem_alloc(size);
80177633Sdfr			if (loc == NULL) {
81177633Sdfr				printf("xdr_reference: out of memory");
82177633Sdfr				return (FALSE);
83177633Sdfr			}
84177633Sdfr			memset(loc, 0, size);
85177633Sdfr			break;
86177633Sdfr
87177633Sdfr		case XDR_ENCODE:
88177633Sdfr			break;
89177633Sdfr		}
90177633Sdfr
91177633Sdfr	stat = (*proc)(xdrs, loc);
92177633Sdfr
93177633Sdfr	if (xdrs->x_op == XDR_FREE) {
94177633Sdfr		mem_free(loc, size);
95177633Sdfr		*pp = NULL;
96177633Sdfr	}
97177633Sdfr	return (stat);
98177633Sdfr}
99177633Sdfr
100177633Sdfr
101177633Sdfr/*
102177633Sdfr * xdr_pointer():
103177633Sdfr *
104177633Sdfr * XDR a pointer to a possibly recursive data structure. This
105177633Sdfr * differs with xdr_reference in that it can serialize/deserialiaze
106177633Sdfr * trees correctly.
107177633Sdfr *
108177633Sdfr *  What's sent is actually a union:
109177633Sdfr *
110177633Sdfr *  union object_pointer switch (boolean b) {
111177633Sdfr *  case TRUE: object_data data;
112177633Sdfr *  case FALSE: void nothing;
113177633Sdfr *  }
114177633Sdfr *
115177633Sdfr * > objpp: Pointer to the pointer to the object.
116177633Sdfr * > obj_size: size of the object.
117177633Sdfr * > xdr_obj: routine to XDR an object.
118177633Sdfr *
119177633Sdfr */
120177633Sdfrbool_t
121177633Sdfrxdr_pointer(XDR *xdrs, char **objpp, u_int obj_size, xdrproc_t xdr_obj)
122177633Sdfr{
123177633Sdfr
124177633Sdfr	bool_t more_data;
125177633Sdfr
126177633Sdfr	more_data = (*objpp != NULL);
127177633Sdfr	if (! xdr_bool(xdrs,&more_data)) {
128177633Sdfr		return (FALSE);
129177633Sdfr	}
130177633Sdfr	if (! more_data) {
131177633Sdfr		*objpp = NULL;
132177633Sdfr		return (TRUE);
133177633Sdfr	}
134177633Sdfr	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
135177633Sdfr}
136