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