xref: /freebsd/lib/libc/xdr/xdr_reference.c (revision 29285d6cc55f43cb38a16fa40491c8d6a1006958)
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";*/
3329285d6cSPoul-Henning Kamp static char *rcsid = "$Id: xdr_reference.c,v 1.2 1995/05/30 05:42:12 rgrimes 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>
4629285d6cSPoul-Henning Kamp #include <stdlib.h>
4729285d6cSPoul-Henning Kamp #include <string.h>
48eae561b3SGarrett Wollman #include <rpc/types.h>
49eae561b3SGarrett Wollman #include <rpc/xdr.h>
50eae561b3SGarrett Wollman 
51eae561b3SGarrett Wollman #define LASTUNSIGNED	((u_int)0-1)
52eae561b3SGarrett Wollman 
53eae561b3SGarrett Wollman /*
54eae561b3SGarrett Wollman  * XDR an indirect pointer
55eae561b3SGarrett Wollman  * xdr_reference is for recursively translating a structure that is
56eae561b3SGarrett Wollman  * referenced by a pointer inside the structure that is currently being
57eae561b3SGarrett Wollman  * translated.  pp references a pointer to storage. If *pp is null
58eae561b3SGarrett Wollman  * the  necessary storage is allocated.
59eae561b3SGarrett Wollman  * size is the sizeof the referneced structure.
60eae561b3SGarrett Wollman  * proc is the routine to handle the referenced structure.
61eae561b3SGarrett Wollman  */
62eae561b3SGarrett Wollman bool_t
63eae561b3SGarrett Wollman xdr_reference(xdrs, pp, size, proc)
64eae561b3SGarrett Wollman 	register XDR *xdrs;
65eae561b3SGarrett Wollman 	caddr_t *pp;		/* the pointer to work on */
66eae561b3SGarrett Wollman 	u_int size;		/* size of the object pointed to */
67eae561b3SGarrett Wollman 	xdrproc_t proc;		/* xdr routine to handle the object */
68eae561b3SGarrett Wollman {
69eae561b3SGarrett Wollman 	register caddr_t loc = *pp;
70eae561b3SGarrett Wollman 	register bool_t stat;
71eae561b3SGarrett Wollman 
72eae561b3SGarrett Wollman 	if (loc == NULL)
73eae561b3SGarrett Wollman 		switch (xdrs->x_op) {
74eae561b3SGarrett Wollman 		case XDR_FREE:
75eae561b3SGarrett Wollman 			return (TRUE);
76eae561b3SGarrett Wollman 
77eae561b3SGarrett Wollman 		case XDR_DECODE:
78eae561b3SGarrett Wollman 			*pp = loc = (caddr_t) mem_alloc(size);
79eae561b3SGarrett Wollman 			if (loc == NULL) {
80eae561b3SGarrett Wollman 				(void) fprintf(stderr,
81eae561b3SGarrett Wollman 				    "xdr_reference: out of memory\n");
82eae561b3SGarrett Wollman 				return (FALSE);
83eae561b3SGarrett Wollman 			}
84eae561b3SGarrett Wollman 			bzero(loc, (int)size);
85eae561b3SGarrett Wollman 			break;
86eae561b3SGarrett Wollman 	}
87eae561b3SGarrett Wollman 
88eae561b3SGarrett Wollman 	stat = (*proc)(xdrs, loc, LASTUNSIGNED);
89eae561b3SGarrett Wollman 
90eae561b3SGarrett Wollman 	if (xdrs->x_op == XDR_FREE) {
91eae561b3SGarrett Wollman 		mem_free(loc, size);
92eae561b3SGarrett Wollman 		*pp = NULL;
93eae561b3SGarrett Wollman 	}
94eae561b3SGarrett Wollman 	return (stat);
95eae561b3SGarrett Wollman }
96eae561b3SGarrett Wollman 
97eae561b3SGarrett Wollman 
98eae561b3SGarrett Wollman /*
99eae561b3SGarrett Wollman  * xdr_pointer():
100eae561b3SGarrett Wollman  *
101eae561b3SGarrett Wollman  * XDR a pointer to a possibly recursive data structure. This
102eae561b3SGarrett Wollman  * differs with xdr_reference in that it can serialize/deserialiaze
103eae561b3SGarrett Wollman  * trees correctly.
104eae561b3SGarrett Wollman  *
105eae561b3SGarrett Wollman  *  What's sent is actually a union:
106eae561b3SGarrett Wollman  *
107eae561b3SGarrett Wollman  *  union object_pointer switch (boolean b) {
108eae561b3SGarrett Wollman  *  case TRUE: object_data data;
109eae561b3SGarrett Wollman  *  case FALSE: void nothing;
110eae561b3SGarrett Wollman  *  }
111eae561b3SGarrett Wollman  *
112eae561b3SGarrett Wollman  * > objpp: Pointer to the pointer to the object.
113eae561b3SGarrett Wollman  * > obj_size: size of the object.
114eae561b3SGarrett Wollman  * > xdr_obj: routine to XDR an object.
115eae561b3SGarrett Wollman  *
116eae561b3SGarrett Wollman  */
117eae561b3SGarrett Wollman bool_t
118eae561b3SGarrett Wollman xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
119eae561b3SGarrett Wollman 	register XDR *xdrs;
120eae561b3SGarrett Wollman 	char **objpp;
121eae561b3SGarrett Wollman 	u_int obj_size;
122eae561b3SGarrett Wollman 	xdrproc_t xdr_obj;
123eae561b3SGarrett Wollman {
124eae561b3SGarrett Wollman 
125eae561b3SGarrett Wollman 	bool_t more_data;
126eae561b3SGarrett Wollman 
127eae561b3SGarrett Wollman 	more_data = (*objpp != NULL);
128eae561b3SGarrett Wollman 	if (! xdr_bool(xdrs,&more_data)) {
129eae561b3SGarrett Wollman 		return (FALSE);
130eae561b3SGarrett Wollman 	}
131eae561b3SGarrett Wollman 	if (! more_data) {
132eae561b3SGarrett Wollman 		*objpp = NULL;
133eae561b3SGarrett Wollman 		return (TRUE);
134eae561b3SGarrett Wollman 	}
135eae561b3SGarrett Wollman 	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
136eae561b3SGarrett Wollman }
137