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