xref: /freebsd/lib/libc/xdr/xdr_reference.c (revision eae561b30ec984ce171d99b1fd182575acc2c639)
1eae561b3SGarrett Wollman /*
2eae561b3SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3eae561b3SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
4eae561b3SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
5eae561b3SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
6eae561b3SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
7eae561b3SGarrett Wollman  * program developed by the user.
8eae561b3SGarrett Wollman  *
9eae561b3SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10eae561b3SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11eae561b3SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12eae561b3SGarrett Wollman  *
13eae561b3SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
14eae561b3SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
15eae561b3SGarrett Wollman  * modification or enhancement.
16eae561b3SGarrett Wollman  *
17eae561b3SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18eae561b3SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19eae561b3SGarrett Wollman  * OR ANY PART THEREOF.
20eae561b3SGarrett Wollman  *
21eae561b3SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22eae561b3SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
23eae561b3SGarrett Wollman  * Sun has been advised of the possibility of such damages.
24eae561b3SGarrett Wollman  *
25eae561b3SGarrett Wollman  * Sun Microsystems, Inc.
26eae561b3SGarrett Wollman  * 2550 Garcia Avenue
27eae561b3SGarrett Wollman  * Mountain View, California  94043
28eae561b3SGarrett Wollman  */
29eae561b3SGarrett Wollman 
30eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
31eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr_reference.c 1.11 87/08/11 SMI";*/
32eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr_reference.c	2.1 88/07/29 4.0 RPCSRC";*/
33eae561b3SGarrett Wollman static char *rcsid = "$Id: xdr_reference.c,v 1.1 1993/10/27 05:41:13 paul Exp $";
34eae561b3SGarrett Wollman #endif
35eae561b3SGarrett Wollman 
36eae561b3SGarrett Wollman /*
37eae561b3SGarrett Wollman  * xdr_reference.c, Generic XDR routines impelmentation.
38eae561b3SGarrett Wollman  *
39eae561b3SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
40eae561b3SGarrett Wollman  *
41eae561b3SGarrett Wollman  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
42eae561b3SGarrett Wollman  * "pointers".  See xdr.h for more info on the interface to xdr.
43eae561b3SGarrett Wollman  */
44eae561b3SGarrett Wollman 
45eae561b3SGarrett Wollman #include <stdio.h>
46eae561b3SGarrett Wollman #include <rpc/types.h>
47eae561b3SGarrett Wollman #include <rpc/xdr.h>
48eae561b3SGarrett Wollman 
49eae561b3SGarrett Wollman #define LASTUNSIGNED	((u_int)0-1)
50eae561b3SGarrett Wollman 
51eae561b3SGarrett Wollman /*
52eae561b3SGarrett Wollman  * XDR an indirect pointer
53eae561b3SGarrett Wollman  * xdr_reference is for recursively translating a structure that is
54eae561b3SGarrett Wollman  * referenced by a pointer inside the structure that is currently being
55eae561b3SGarrett Wollman  * translated.  pp references a pointer to storage. If *pp is null
56eae561b3SGarrett Wollman  * the  necessary storage is allocated.
57eae561b3SGarrett Wollman  * size is the sizeof the referneced structure.
58eae561b3SGarrett Wollman  * proc is the routine to handle the referenced structure.
59eae561b3SGarrett Wollman  */
60eae561b3SGarrett Wollman bool_t
61eae561b3SGarrett Wollman xdr_reference(xdrs, pp, size, proc)
62eae561b3SGarrett Wollman 	register XDR *xdrs;
63eae561b3SGarrett Wollman 	caddr_t *pp;		/* the pointer to work on */
64eae561b3SGarrett Wollman 	u_int size;		/* size of the object pointed to */
65eae561b3SGarrett Wollman 	xdrproc_t proc;		/* xdr routine to handle the object */
66eae561b3SGarrett Wollman {
67eae561b3SGarrett Wollman 	register caddr_t loc = *pp;
68eae561b3SGarrett Wollman 	register bool_t stat;
69eae561b3SGarrett Wollman 
70eae561b3SGarrett Wollman 	if (loc == NULL)
71eae561b3SGarrett Wollman 		switch (xdrs->x_op) {
72eae561b3SGarrett Wollman 		case XDR_FREE:
73eae561b3SGarrett Wollman 			return (TRUE);
74eae561b3SGarrett Wollman 
75eae561b3SGarrett Wollman 		case XDR_DECODE:
76eae561b3SGarrett Wollman 			*pp = loc = (caddr_t) mem_alloc(size);
77eae561b3SGarrett Wollman 			if (loc == NULL) {
78eae561b3SGarrett Wollman 				(void) fprintf(stderr,
79eae561b3SGarrett Wollman 				    "xdr_reference: out of memory\n");
80eae561b3SGarrett Wollman 				return (FALSE);
81eae561b3SGarrett Wollman 			}
82eae561b3SGarrett Wollman 			bzero(loc, (int)size);
83eae561b3SGarrett Wollman 			break;
84eae561b3SGarrett Wollman 	}
85eae561b3SGarrett Wollman 
86eae561b3SGarrett Wollman 	stat = (*proc)(xdrs, loc, LASTUNSIGNED);
87eae561b3SGarrett Wollman 
88eae561b3SGarrett Wollman 	if (xdrs->x_op == XDR_FREE) {
89eae561b3SGarrett Wollman 		mem_free(loc, size);
90eae561b3SGarrett Wollman 		*pp = NULL;
91eae561b3SGarrett Wollman 	}
92eae561b3SGarrett Wollman 	return (stat);
93eae561b3SGarrett Wollman }
94eae561b3SGarrett Wollman 
95eae561b3SGarrett Wollman 
96eae561b3SGarrett Wollman /*
97eae561b3SGarrett Wollman  * xdr_pointer():
98eae561b3SGarrett Wollman  *
99eae561b3SGarrett Wollman  * XDR a pointer to a possibly recursive data structure. This
100eae561b3SGarrett Wollman  * differs with xdr_reference in that it can serialize/deserialiaze
101eae561b3SGarrett Wollman  * trees correctly.
102eae561b3SGarrett Wollman  *
103eae561b3SGarrett Wollman  *  What's sent is actually a union:
104eae561b3SGarrett Wollman  *
105eae561b3SGarrett Wollman  *  union object_pointer switch (boolean b) {
106eae561b3SGarrett Wollman  *  case TRUE: object_data data;
107eae561b3SGarrett Wollman  *  case FALSE: void nothing;
108eae561b3SGarrett Wollman  *  }
109eae561b3SGarrett Wollman  *
110eae561b3SGarrett Wollman  * > objpp: Pointer to the pointer to the object.
111eae561b3SGarrett Wollman  * > obj_size: size of the object.
112eae561b3SGarrett Wollman  * > xdr_obj: routine to XDR an object.
113eae561b3SGarrett Wollman  *
114eae561b3SGarrett Wollman  */
115eae561b3SGarrett Wollman bool_t
116eae561b3SGarrett Wollman xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
117eae561b3SGarrett Wollman 	register XDR *xdrs;
118eae561b3SGarrett Wollman 	char **objpp;
119eae561b3SGarrett Wollman 	u_int obj_size;
120eae561b3SGarrett Wollman 	xdrproc_t xdr_obj;
121eae561b3SGarrett Wollman {
122eae561b3SGarrett Wollman 
123eae561b3SGarrett Wollman 	bool_t more_data;
124eae561b3SGarrett Wollman 
125eae561b3SGarrett Wollman 	more_data = (*objpp != NULL);
126eae561b3SGarrett Wollman 	if (! xdr_bool(xdrs,&more_data)) {
127eae561b3SGarrett Wollman 		return (FALSE);
128eae561b3SGarrett Wollman 	}
129eae561b3SGarrett Wollman 	if (! more_data) {
130eae561b3SGarrett Wollman 		*objpp = NULL;
131eae561b3SGarrett Wollman 		return (TRUE);
132eae561b3SGarrett Wollman 	}
133eae561b3SGarrett Wollman 	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
134eae561b3SGarrett Wollman }
135