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