xdr_reference.c revision 1.15.24.1 1 1.15.24.1 riz /* $NetBSD: xdr_reference.c,v 1.15.24.1 2013/03/14 22:03:15 riz Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.15.24.1 riz * Copyright (c) 2010, Oracle America, Inc.
5 1.15.24.1 riz *
6 1.15.24.1 riz * Redistribution and use in source and binary forms, with or without
7 1.15.24.1 riz * modification, are permitted provided that the following conditions are
8 1.15.24.1 riz * met:
9 1.15.24.1 riz *
10 1.15.24.1 riz * * Redistributions of source code must retain the above copyright
11 1.15.24.1 riz * notice, this list of conditions and the following disclaimer.
12 1.15.24.1 riz * * Redistributions in binary form must reproduce the above
13 1.15.24.1 riz * copyright notice, this list of conditions and the following
14 1.15.24.1 riz * disclaimer in the documentation and/or other materials
15 1.15.24.1 riz * provided with the distribution.
16 1.15.24.1 riz * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.15.24.1 riz * contributors may be used to endorse or promote products derived
18 1.15.24.1 riz * from this software without specific prior written permission.
19 1.15.24.1 riz *
20 1.15.24.1 riz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.15.24.1 riz * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.15.24.1 riz * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.15.24.1 riz * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.15.24.1 riz * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.15.24.1 riz * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.15.24.1 riz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.15.24.1 riz * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.15.24.1 riz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.15.24.1 riz * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.15.24.1 riz * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.15.24.1 riz * 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.15.24.1 riz __RCSID("$NetBSD: xdr_reference.c,v 1.15.24.1 2013/03/14 22:03:15 riz 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.1 cgd * size is the sizeof the referneced 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.1 cgd xdr_reference(xdrs, pp, size, proc)
79 1.11 lukem XDR *xdrs;
80 1.1 cgd caddr_t *pp; /* the pointer to work on */
81 1.10 lukem u_int size; /* size of the object pointed to */
82 1.1 cgd xdrproc_t proc; /* xdr routine to handle the object */
83 1.1 cgd {
84 1.11 lukem caddr_t loc = *pp;
85 1.11 lukem bool_t stat;
86 1.1 cgd
87 1.1 cgd if (loc == NULL)
88 1.1 cgd switch (xdrs->x_op) {
89 1.1 cgd case XDR_FREE:
90 1.1 cgd return (TRUE);
91 1.1 cgd
92 1.1 cgd case XDR_DECODE:
93 1.15 christos *pp = loc = mem_alloc(size);
94 1.1 cgd if (loc == NULL) {
95 1.11 lukem warnx("xdr_reference: out of memory");
96 1.1 cgd return (FALSE);
97 1.1 cgd }
98 1.12 christos memset(loc, 0, size);
99 1.1 cgd break;
100 1.6 christos
101 1.6 christos case XDR_ENCODE:
102 1.6 christos break;
103 1.6 christos }
104 1.1 cgd
105 1.5 jtc stat = (*proc)(xdrs, loc);
106 1.1 cgd
107 1.1 cgd if (xdrs->x_op == XDR_FREE) {
108 1.1 cgd mem_free(loc, size);
109 1.1 cgd *pp = NULL;
110 1.1 cgd }
111 1.1 cgd return (stat);
112 1.1 cgd }
113 1.1 cgd
114 1.1 cgd
115 1.1 cgd /*
116 1.1 cgd * xdr_pointer():
117 1.1 cgd *
118 1.1 cgd * XDR a pointer to a possibly recursive data structure. This
119 1.1 cgd * differs with xdr_reference in that it can serialize/deserialiaze
120 1.1 cgd * trees correctly.
121 1.1 cgd *
122 1.1 cgd * What's sent is actually a union:
123 1.1 cgd *
124 1.1 cgd * union object_pointer switch (boolean b) {
125 1.1 cgd * case TRUE: object_data data;
126 1.1 cgd * case FALSE: void nothing;
127 1.1 cgd * }
128 1.1 cgd *
129 1.1 cgd * > objpp: Pointer to the pointer to the object.
130 1.1 cgd * > obj_size: size of the object.
131 1.1 cgd * > xdr_obj: routine to XDR an object.
132 1.1 cgd *
133 1.1 cgd */
134 1.1 cgd bool_t
135 1.1 cgd xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
136 1.11 lukem XDR *xdrs;
137 1.10 lukem char **objpp;
138 1.10 lukem u_int obj_size;
139 1.1 cgd xdrproc_t xdr_obj;
140 1.1 cgd {
141 1.10 lukem
142 1.1 cgd bool_t more_data;
143 1.1 cgd
144 1.1 cgd more_data = (*objpp != NULL);
145 1.1 cgd if (! xdr_bool(xdrs,&more_data)) {
146 1.1 cgd return (FALSE);
147 1.1 cgd }
148 1.1 cgd if (! more_data) {
149 1.1 cgd *objpp = NULL;
150 1.1 cgd return (TRUE);
151 1.1 cgd }
152 1.10 lukem return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
153 1.1 cgd }
154