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