Imported Upstream version 0.2.2
[platform/upstream/libtirpc.git] / src / xdr_stdio.c
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30
31 /*
32  * xdr_stdio.c, XDR implementation on standard i/o file.
33  *
34  * Copyright (C) 1984, Sun Microsystems, Inc.
35  *
36  * This set of routines implements a XDR on a stdio stream.
37  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
38  * from the stream.
39  */
40
41 #include "namespace.h"
42 #include <stdio.h>
43
44 #include <arpa/inet.h>
45 #include <rpc/types.h>
46 #include <rpc/xdr.h>
47 #include "un-namespace.h"
48
49 static void xdrstdio_destroy(XDR *);
50 static bool_t xdrstdio_getlong(XDR *, long *);
51 static bool_t xdrstdio_putlong(XDR *, const long *);
52 static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
53 static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
54 static u_int xdrstdio_getpos(XDR *);
55 static bool_t xdrstdio_setpos(XDR *, u_int);
56 static int32_t *xdrstdio_inline(XDR *, u_int);
57
58 /*
59  * Ops vector for stdio type XDR
60  */
61 static const struct xdr_ops     xdrstdio_ops = {
62         xdrstdio_getlong,       /* deseraialize a long int */
63         xdrstdio_putlong,       /* seraialize a long int */
64         xdrstdio_getbytes,      /* deserialize counted bytes */
65         xdrstdio_putbytes,      /* serialize counted bytes */
66         xdrstdio_getpos,        /* get offset in the stream */
67         xdrstdio_setpos,        /* set offset in the stream */
68         xdrstdio_inline,        /* prime stream for inline macros */
69         xdrstdio_destroy        /* destroy stream */
70 };
71
72 /*
73  * Initialize a stdio xdr stream.
74  * Sets the xdr stream handle xdrs for use on the stream file.
75  * Operation flag is set to op.
76  */
77 void
78 xdrstdio_create(xdrs, file, op)
79         XDR *xdrs;
80         FILE *file;
81         enum xdr_op op;
82 {
83
84         xdrs->x_op = op;
85         xdrs->x_ops = &xdrstdio_ops;
86         xdrs->x_private = file;
87         xdrs->x_handy = 0;
88         xdrs->x_base = 0;
89 }
90
91 /*
92  * Destroy a stdio xdr stream.
93  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
94  */
95 static void
96 xdrstdio_destroy(xdrs)
97         XDR *xdrs;
98 {
99         (void)fflush((FILE *)xdrs->x_private);
100                 /* XXX: should we close the file ?? */
101 }
102
103 static bool_t
104 xdrstdio_getlong(xdrs, lp)
105         XDR *xdrs;
106         long *lp;
107 {
108
109         if (fread(lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
110                 return (FALSE);
111         *lp = (long)ntohl((u_int32_t)*lp);
112         return (TRUE);
113 }
114
115 static bool_t
116 xdrstdio_putlong(xdrs, lp)
117         XDR *xdrs;
118         const long *lp;
119 {
120         long mycopy = (long)htonl((u_int32_t)*lp);
121
122         if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
123                 return (FALSE);
124         return (TRUE);
125 }
126
127 static bool_t
128 xdrstdio_getbytes(xdrs, addr, len)
129         XDR *xdrs;
130         char *addr;
131         u_int len;
132 {
133
134         if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
135                 return (FALSE);
136         return (TRUE);
137 }
138
139 static bool_t
140 xdrstdio_putbytes(xdrs, addr, len)
141         XDR *xdrs;
142         const char *addr;
143         u_int len;
144 {
145
146         if ((len != 0) && (fwrite(addr, (size_t)len, 1,
147             (FILE *)xdrs->x_private) != 1))
148                 return (FALSE);
149         return (TRUE);
150 }
151
152 static u_int
153 xdrstdio_getpos(xdrs)
154         XDR *xdrs;
155 {
156
157         return ((u_int) ftell((FILE *)xdrs->x_private));
158 }
159
160 static bool_t
161 xdrstdio_setpos(xdrs, pos) 
162         XDR *xdrs;
163         u_int pos;
164
165
166         return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
167                 FALSE : TRUE);
168 }
169
170 /* ARGSUSED */
171 static int32_t *
172 xdrstdio_inline(xdrs, len)
173         XDR *xdrs;
174         u_int len;
175 {
176
177         /*
178          * Must do some work to implement this: must insure
179          * enough data in the underlying stdio buffer,
180          * that the buffer is aligned so that we can indirect through a
181          * long *, and stuff this pointer in xdrs->x_buf.  Doing
182          * a fread or fwrite to a scratch buffer would defeat
183          * most of the gains to be had here and require storage
184          * management on this buffer, so we don't do this.
185          */
186         return (NULL);
187 }