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