1 /* @(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC */
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 #if !defined(lint) && defined(SCCSIDS)
35 static char sccsid[] = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
36 #endif
37
38 /*
39 * xdr_stdio.c, XDR implementation on standard i/o file.
40 *
41 * This set of routines implements a XDR on a stdio stream.
42 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
43 * from the stream.
44 */
45
46 #include <gssrpc/types.h>
47 #include <stdio.h>
48 #include <gssrpc/xdr.h>
49
50 static bool_t xdrstdio_getlong(XDR *, long *);
51 static bool_t xdrstdio_putlong(XDR *, long *);
52 static bool_t xdrstdio_getbytes(XDR *, caddr_t, u_int);
53 static bool_t xdrstdio_putbytes(XDR *, caddr_t, u_int);
54 static u_int xdrstdio_getpos(XDR *);
55 static bool_t xdrstdio_setpos(XDR *, u_int);
56 static rpc_inline_t * xdrstdio_inline(XDR *, int);
57 static void xdrstdio_destroy(XDR *);
58
59 /*
60 * Ops vector for stdio type XDR
61 */
62 static struct xdr_ops xdrstdio_ops = {
63 xdrstdio_getlong, /* deseraialize a long int */
64 xdrstdio_putlong, /* seraialize a long int */
65 xdrstdio_getbytes, /* deserialize counted bytes */
66 xdrstdio_putbytes, /* serialize counted bytes */
67 xdrstdio_getpos, /* get offset in the stream */
68 xdrstdio_setpos, /* set offset in the stream */
69 xdrstdio_inline, /* prime stream for inline macros */
70 xdrstdio_destroy /* destroy stream */
71 };
72
73 /*
74 * Initialize a stdio xdr stream.
75 * Sets the xdr stream handle xdrs for use on the stream file.
76 * Operation flag is set to op.
77 */
78 void
xdrstdio_create(XDR * xdrs,FILE * file,enum xdr_op op)79 xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op)
80 {
81
82 xdrs->x_op = op;
83 xdrs->x_ops = &xdrstdio_ops;
84 xdrs->x_private = (caddr_t)file;
85 xdrs->x_handy = 0;
86 xdrs->x_base = 0;
87 }
88
89 /*
90 * Destroy a stdio xdr stream.
91 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
92 */
93 static void
xdrstdio_destroy(XDR * xdrs)94 xdrstdio_destroy(XDR *xdrs)
95 {
96 (void)fflush((FILE *)xdrs->x_private);
97 /* xx should we close the file ?? */
98 }
99
100 static bool_t
xdrstdio_getlong(XDR * xdrs,long * lp)101 xdrstdio_getlong(XDR *xdrs, long *lp)
102 {
103 uint32_t tmp;
104 if (fread((caddr_t)&tmp,
105 sizeof(uint32_t), 1, (FILE *)xdrs->x_private) != 1)
106 return (FALSE);
107
108 *lp = (long)ntohl(tmp);
109
110 return (TRUE);
111 }
112
113 static bool_t
xdrstdio_putlong(XDR * xdrs,long * lp)114 xdrstdio_putlong(XDR *xdrs, long *lp)
115 {
116 uint32_t mycopy = htonl((uint32_t)*lp);
117
118 if (fwrite((caddr_t)&mycopy, sizeof(uint32_t), 1, (FILE *)xdrs->x_private) != 1)
119 return (FALSE);
120 return (TRUE);
121 }
122
123 static bool_t
xdrstdio_getbytes(XDR * xdrs,caddr_t addr,u_int len)124 xdrstdio_getbytes(XDR *xdrs, caddr_t addr, u_int len)
125 {
126
127 if ((len != 0) && (fread(addr, (size_t)len, 1,
128 (FILE *)xdrs->x_private) != 1))
129 return (FALSE);
130 return (TRUE);
131 }
132
133 static bool_t
xdrstdio_putbytes(XDR * xdrs,caddr_t addr,u_int len)134 xdrstdio_putbytes(XDR *xdrs, caddr_t addr, u_int len)
135 {
136
137 if ((len != 0) && (fwrite(addr, (size_t)len, 1,
138 (FILE *)xdrs->x_private) != 1))
139 return (FALSE);
140 return (TRUE);
141 }
142
143 static u_int
xdrstdio_getpos(XDR * xdrs)144 xdrstdio_getpos(XDR *xdrs)
145 {
146
147 return ((u_int) ftell((FILE *)xdrs->x_private));
148 }
149
150 static bool_t
xdrstdio_setpos(XDR * xdrs,u_int pos)151 xdrstdio_setpos(XDR *xdrs, u_int pos)
152 {
153
154 return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
155 FALSE : TRUE);
156 }
157
158 static rpc_inline_t *
xdrstdio_inline(XDR * xdrs,int len)159 xdrstdio_inline(XDR *xdrs, int len)
160 {
161
162 /*
163 * Must do some work to implement this: must insure
164 * enough data in the underlying stdio buffer,
165 * that the buffer is aligned so that we can indirect through a
166 * long *, and stuff this pointer in xdrs->x_buf. Doing
167 * a fread or fwrite to a scratch buffer would defeat
168 * most of the gains to be had here and require storage
169 * management on this buffer, so we don't do this.
170 */
171 return (NULL);
172 }
173