xref: /freebsd/crypto/krb5/src/lib/rpc/xdr_reference.c (revision 88b8b7f0c4e9948667a2279e78e975a784049cba)
1 /* @(#)xdr_reference.c	2.1 88/07/29 4.0 RPCSRC */
2 /*
3  * Copyright (c) 2010, Oracle America, Inc.
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *
18  *     * Neither the name of the "Oracle America, Inc." nor the names of
19  *       its contributors may be used to endorse or promote products
20  *       derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #if !defined(lint) && defined(SCCSIDS)
35 static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
36 #endif
37 
38 /*
39  * xdr_reference.c, Generic XDR routines impelmentation.
40  *
41  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
42  * "pointers".  See xdr.h for more info on the interface to xdr.
43  */
44 
45 #include <stdio.h>
46 #include <string.h>
47 #include <gssrpc/types.h>
48 #include <gssrpc/xdr.h>
49 
50 /*
51  * XDR an indirect pointer
52  * xdr_reference is for recursively translating a structure that is
53  * referenced by a pointer inside the structure that is currently being
54  * translated.  pp references a pointer to storage. If *pp is null
55  * the  necessary storage is allocated.
56  * size is the sizeof the referenced structure.
57  * proc is the routine to handle the referenced structure.
58  */
59 bool_t
60 xdr_reference(
61 	XDR *xdrs,
62 	caddr_t *pp,		/* the pointer to work on */
63 	u_int size,		/* size of the object pointed to */
64 	xdrproc_t proc		/* xdr routine to handle the object */
65 	)
66 {
67 	caddr_t loc = *pp;
68 	bool_t stat;
69 
70 	if (loc == NULL)
71 		switch (xdrs->x_op) {
72 		case XDR_FREE:
73 			return (TRUE);
74 
75 		case XDR_DECODE:
76 			*pp = loc = (caddr_t) mem_alloc(size);
77 			if (loc == NULL) {
78 				(void) fprintf(stderr,
79 				    "xdr_reference: out of memory\n");
80 				return (FALSE);
81 			}
82 			memset(loc, 0, size);
83 			break;
84 
85 		case XDR_ENCODE:
86 			break;
87 	}
88 
89 	stat = (*proc)(xdrs, loc);
90 
91 	if (xdrs->x_op == XDR_FREE) {
92 		mem_free(loc, size);
93 		*pp = NULL;
94 	}
95 	return (stat);
96 }
97 
98 
99 /*
100  * xdr_pointer():
101  *
102  * XDR a pointer to a possibly recursive data structure. This
103  * differs with xdr_reference in that it can serialize/deserialiaze
104  * trees correctly.
105  *
106  *  What's sent is actually a union:
107  *
108  *  union object_pointer switch (boolean b) {
109  *  case TRUE: object_data data;
110  *  case FALSE: void nothing;
111  *  }
112  *
113  * > objpp: Pointer to the pointer to the object.
114  * > obj_size: size of the object.
115  * > xdr_obj: routine to XDR an object.
116  *
117  */
118 bool_t
119 xdr_pointer(
120 	XDR *xdrs,
121 	char **objpp,
122 	u_int obj_size,
123 	xdrproc_t xdr_obj)
124 {
125 
126 	bool_t more_data;
127 
128 	more_data = (*objpp != NULL);
129 	if (! xdr_bool(xdrs,&more_data)) {
130 		return (FALSE);
131 	}
132 	if (! more_data) {
133 		*objpp = NULL;
134 		return (TRUE);
135 	}
136 	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
137 }
138