xref: /minix3/lib/libc/rpc/xdr_reference.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: xdr_reference.c,v 1.18 2013/03/11 20:19:30 tron Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc  * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras  *
6*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc  * met:
92fe8fb19SBen Gras  *
10*84d9c625SLionel Sambuc  *     * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc  *       notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc  *     * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc  *       copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc  *       disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc  *       provided with the distribution.
16*84d9c625SLionel Sambuc  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc  *       contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc  *       from this software without specific prior written permission.
192fe8fb19SBen Gras  *
20*84d9c625SLionel Sambuc  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
382fe8fb19SBen Gras static char *sccsid = "@(#)xdr_reference.c	2.1 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: xdr_reference.c,v 1.18 2013/03/11 20:19:30 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras 
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras  * xdr_reference.c, Generic XDR routines implementation.
462fe8fb19SBen Gras  *
472fe8fb19SBen Gras  * Copyright (C) 1987, Sun Microsystems, Inc.
482fe8fb19SBen Gras  *
492fe8fb19SBen Gras  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
502fe8fb19SBen Gras  * "pointers".  See xdr.h for more info on the interface to xdr.
512fe8fb19SBen Gras  */
522fe8fb19SBen Gras 
532fe8fb19SBen Gras #include "namespace.h"
542fe8fb19SBen Gras 
552fe8fb19SBen Gras #include <err.h>
562fe8fb19SBen Gras #include <stdio.h>
572fe8fb19SBen Gras #include <stdlib.h>
582fe8fb19SBen Gras #include <string.h>
592fe8fb19SBen Gras 
602fe8fb19SBen Gras #include <rpc/types.h>
612fe8fb19SBen Gras #include <rpc/xdr.h>
622fe8fb19SBen Gras 
632fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(xdr_pointer,_xdr_pointer)642fe8fb19SBen Gras __weak_alias(xdr_pointer,_xdr_pointer)
652fe8fb19SBen Gras __weak_alias(xdr_reference,_xdr_reference)
662fe8fb19SBen Gras #endif
672fe8fb19SBen Gras 
682fe8fb19SBen Gras /*
692fe8fb19SBen Gras  * XDR an indirect pointer
702fe8fb19SBen Gras  * xdr_reference is for recursively translating a structure that is
712fe8fb19SBen Gras  * referenced by a pointer inside the structure that is currently being
722fe8fb19SBen Gras  * translated.  pp references a pointer to storage. If *pp is null
732fe8fb19SBen Gras  * the  necessary storage is allocated.
742fe8fb19SBen Gras  * size is the sizeof the referneced structure.
752fe8fb19SBen Gras  * proc is the routine to handle the referenced structure.
762fe8fb19SBen Gras  */
772fe8fb19SBen Gras bool_t
78f14fb602SLionel Sambuc xdr_reference(XDR *xdrs, caddr_t *pp, u_int size, xdrproc_t proc)
792fe8fb19SBen Gras {
802fe8fb19SBen Gras 	caddr_t loc = *pp;
812fe8fb19SBen Gras 	bool_t stat;
822fe8fb19SBen Gras 
832fe8fb19SBen Gras 	if (loc == NULL)
842fe8fb19SBen Gras 		switch (xdrs->x_op) {
852fe8fb19SBen Gras 		case XDR_FREE:
862fe8fb19SBen Gras 			return (TRUE);
872fe8fb19SBen Gras 
882fe8fb19SBen Gras 		case XDR_DECODE:
892fe8fb19SBen Gras 			*pp = loc = mem_alloc(size);
902fe8fb19SBen Gras 			if (loc == NULL) {
91*84d9c625SLionel Sambuc 				warn("%s: out of memory", __func__);
922fe8fb19SBen Gras 				return (FALSE);
932fe8fb19SBen Gras 			}
942fe8fb19SBen Gras 			memset(loc, 0, size);
952fe8fb19SBen Gras 			break;
962fe8fb19SBen Gras 
972fe8fb19SBen Gras 		case XDR_ENCODE:
982fe8fb19SBen Gras 			break;
992fe8fb19SBen Gras 		}
1002fe8fb19SBen Gras 
1012fe8fb19SBen Gras 	stat = (*proc)(xdrs, loc);
1022fe8fb19SBen Gras 
1032fe8fb19SBen Gras 	if (xdrs->x_op == XDR_FREE) {
1042fe8fb19SBen Gras 		mem_free(loc, size);
1052fe8fb19SBen Gras 		*pp = NULL;
1062fe8fb19SBen Gras 	}
1072fe8fb19SBen Gras 	return (stat);
1082fe8fb19SBen Gras }
1092fe8fb19SBen Gras 
1102fe8fb19SBen Gras 
1112fe8fb19SBen Gras /*
1122fe8fb19SBen Gras  * xdr_pointer():
1132fe8fb19SBen Gras  *
1142fe8fb19SBen Gras  * XDR a pointer to a possibly recursive data structure. This
1152fe8fb19SBen Gras  * differs with xdr_reference in that it can serialize/deserialiaze
1162fe8fb19SBen Gras  * trees correctly.
1172fe8fb19SBen Gras  *
1182fe8fb19SBen Gras  *  What's sent is actually a union:
1192fe8fb19SBen Gras  *
1202fe8fb19SBen Gras  *  union object_pointer switch (boolean b) {
1212fe8fb19SBen Gras  *  case TRUE: object_data data;
1222fe8fb19SBen Gras  *  case FALSE: void nothing;
1232fe8fb19SBen Gras  *  }
1242fe8fb19SBen Gras  *
1252fe8fb19SBen Gras  * > objpp: Pointer to the pointer to the object.
1262fe8fb19SBen Gras  * > obj_size: size of the object.
1272fe8fb19SBen Gras  * > xdr_obj: routine to XDR an object.
1282fe8fb19SBen Gras  *
1292fe8fb19SBen Gras  */
1302fe8fb19SBen Gras bool_t
xdr_pointer(XDR * xdrs,char ** objpp,u_int obj_size,xdrproc_t xdr_obj)131f14fb602SLionel Sambuc xdr_pointer(XDR *xdrs, char **objpp, u_int obj_size, xdrproc_t xdr_obj)
1322fe8fb19SBen Gras {
1332fe8fb19SBen Gras 
1342fe8fb19SBen Gras 	bool_t more_data;
1352fe8fb19SBen Gras 
1362fe8fb19SBen Gras 	more_data = (*objpp != NULL);
1372fe8fb19SBen Gras 	if (! xdr_bool(xdrs,&more_data)) {
1382fe8fb19SBen Gras 		return (FALSE);
1392fe8fb19SBen Gras 	}
1402fe8fb19SBen Gras 	if (! more_data) {
1412fe8fb19SBen Gras 		*objpp = NULL;
1422fe8fb19SBen Gras 		return (TRUE);
1432fe8fb19SBen Gras 	}
1442fe8fb19SBen Gras 	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
1452fe8fb19SBen Gras }
146