1 /* lib/rpc/xdr_alloc.c */
2 /*
3 * Copyright (c) 2010, Oracle America, Inc.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * * Neither the name of the "Oracle America, Inc." nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 /*
35 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
36 */
37
38 #include <gssrpc/types.h>
39 #include <gssrpc/xdr.h>
40 #include "dyn.h"
41
42 static bool_t xdralloc_putlong(XDR *, long *);
43 static bool_t xdralloc_putbytes(XDR *, caddr_t, unsigned int);
44 static unsigned int xdralloc_getpos(XDR *);
45 static rpc_inline_t * xdralloc_inline(XDR *, int);
46 static void xdralloc_destroy(XDR *);
47 static bool_t xdralloc_notsup_getlong(XDR *, long *);
48 static bool_t xdralloc_notsup_getbytes(XDR *, caddr_t, unsigned int);
49 static bool_t xdralloc_notsup_setpos(XDR *, unsigned int);
50 static struct xdr_ops xdralloc_ops = {
51 xdralloc_notsup_getlong,
52 xdralloc_putlong,
53 xdralloc_notsup_getbytes,
54 xdralloc_putbytes,
55 xdralloc_getpos,
56 xdralloc_notsup_setpos,
57 xdralloc_inline,
58 xdralloc_destroy,
59 };
60
61 /*
62 * The procedure xdralloc_create initializes a stream descriptor for a
63 * memory buffer.
64 */
xdralloc_create(XDR * xdrs,enum xdr_op op)65 void xdralloc_create(XDR *xdrs, enum xdr_op op)
66 {
67 xdrs->x_op = op;
68 xdrs->x_ops = &xdralloc_ops;
69 xdrs->x_private = (caddr_t) DynCreate(sizeof(char), -4);
70 /* not allowed to fail */
71 }
72
xdralloc_getdata(XDR * xdrs)73 caddr_t xdralloc_getdata(XDR *xdrs)
74 {
75 return (caddr_t) DynGet((DynObject) xdrs->x_private, 0);
76 }
77
xdralloc_release(XDR * xdrs)78 void xdralloc_release(XDR *xdrs)
79 {
80 DynRelease((DynObject) xdrs->x_private);
81 }
82
xdralloc_destroy(XDR * xdrs)83 static void xdralloc_destroy(XDR *xdrs)
84 {
85 DynDestroy((DynObject) xdrs->x_private);
86 }
87
xdralloc_notsup_getlong(XDR * xdrs,long * lp)88 static bool_t xdralloc_notsup_getlong(
89 XDR *xdrs,
90 long *lp)
91 {
92 return FALSE;
93 }
94
xdralloc_putlong(XDR * xdrs,long * lp)95 static bool_t xdralloc_putlong(
96 XDR *xdrs,
97 long *lp)
98 {
99 int l = htonl((uint32_t) *lp); /* XXX need bounds checking */
100
101 /* XXX assumes sizeof(int)==4 */
102 if (DynInsert((DynObject) xdrs->x_private,
103 DynSize((DynObject) xdrs->x_private), &l,
104 sizeof(int)) != DYN_OK)
105 return FALSE;
106 return (TRUE);
107 }
108
109
xdralloc_notsup_getbytes(XDR * xdrs,caddr_t addr,unsigned int len)110 static bool_t xdralloc_notsup_getbytes(
111 XDR *xdrs,
112 caddr_t addr,
113 unsigned int len)
114 {
115 return FALSE;
116 }
117
118
xdralloc_putbytes(XDR * xdrs,caddr_t addr,unsigned int len)119 static bool_t xdralloc_putbytes(
120 XDR *xdrs,
121 caddr_t addr,
122 unsigned int len)
123 {
124 if (DynInsert((DynObject) xdrs->x_private,
125 DynSize((DynObject) xdrs->x_private),
126 addr, (int) len) != DYN_OK)
127 return FALSE;
128 return TRUE;
129 }
130
xdralloc_getpos(XDR * xdrs)131 static unsigned int xdralloc_getpos(XDR *xdrs)
132 {
133 return DynSize((DynObject) xdrs->x_private);
134 }
135
xdralloc_notsup_setpos(XDR * xdrs,unsigned int lp)136 static bool_t xdralloc_notsup_setpos(
137 XDR *xdrs,
138 unsigned int lp)
139 {
140 return FALSE;
141 }
142
143
144
xdralloc_inline(XDR * xdrs,int len)145 static rpc_inline_t *xdralloc_inline(
146 XDR *xdrs,
147 int len)
148 {
149 return (rpc_inline_t *) 0;
150 }
151