ReactOS 0.4.16-dev-320-g3bd9ddc
xdr_stdio.c
Go to the documentation of this file.
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 <wintirpc.h>
42#include "namespace.h"
43#include <stdio.h>
44
45//#include <arpa/inet.h>
46#include <rpc/types.h>
47#include <rpc/xdr.h>
48#include "un-namespace.h"
49
50static void xdrstdio_destroy(XDR *);
51static bool_t xdrstdio_getlong(XDR *, long *);
52static bool_t xdrstdio_putlong(XDR *, const long *);
53static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
54static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
55static u_int xdrstdio_getpos(XDR *);
58
59/*
60 * Ops vector for stdio type XDR
61 */
62static const 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 */
78void
80 XDR *xdrs;
81 FILE *file;
82 enum xdr_op op;
83{
84
85 xdrs->x_op = op;
86 xdrs->x_ops = &xdrstdio_ops;
87 xdrs->x_private = file;
88 xdrs->x_handy = 0;
89 xdrs->x_base = 0;
90}
91
92/*
93 * Destroy a stdio xdr stream.
94 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
95 */
96static void
98 XDR *xdrs;
99{
100 (void)fflush((FILE *)xdrs->x_private);
101 /* XXX: should we close the file ?? */
102}
103
104static bool_t
106 XDR *xdrs;
107 long *lp;
108{
109
110 if (fread(lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
111 return (FALSE);
112 *lp = (long)ntohl((u_int32_t)*lp);
113 return (TRUE);
114}
115
116static bool_t
118 XDR *xdrs;
119 const long *lp;
120{
121 long mycopy = (long)htonl((u_int32_t)*lp);
122
123 if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
124 return (FALSE);
125 return (TRUE);
126}
127
128static bool_t
130 XDR *xdrs;
131 char *addr;
132 u_int len;
133{
134
135 if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
136 return (FALSE);
137 return (TRUE);
138}
139
140static bool_t
142 XDR *xdrs;
143 const char *addr;
144 u_int len;
145{
146
147 if ((len != 0) && (fwrite(addr, (size_t)len, 1,
148 (FILE *)xdrs->x_private) != 1))
149 return (FALSE);
150 return (TRUE);
151}
152
153static u_int
155 XDR *xdrs;
156{
157
158 return ((u_int) ftell((FILE *)xdrs->x_private));
159}
160
161static bool_t
163 XDR *xdrs;
164 u_int pos;
165{
166
167 return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
168 FALSE : TRUE);
169}
170
171/* ARGSUSED */
172static int32_t *
174 XDR *xdrs;
175 u_int len;
176{
177
178 /*
179 * Must do some work to implement this: must insure
180 * enough data in the underlying stdio buffer,
181 * that the buffer is aligned so that we can indirect through a
182 * long *, and stuff this pointer in xdrs->x_buf. Doing
183 * a fread or fwrite to a scratch buffer would defeat
184 * most of the gains to be had here and require storage
185 * management on this buffer, so we don't do this.
186 */
187 return (NULL);
188}
UINT32 u_int
Definition: types.h:82
#define NULL
Definition: types.h:112
int32_t bool_t
Definition: types.h:101
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
INT32 int32_t
Definition: types.h:71
UINT op
Definition: effect.c:236
GLenum const GLvoid * addr
Definition: glext.h:9621
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
#define ntohl(x)
Definition: module.h:205
#define htonl(x)
Definition: module.h:214
#define long
Definition: qsort.c:33
unsigned int u_int32_t
Definition: rosdhcp.h:35
Definition: xdr.h:103
const struct __rpc_xdr::xdr_ops * x_ops
enum xdr_op x_op
Definition: xdr.h:104
char * x_base
Definition: xdr.h:126
void * x_private
Definition: xdr.h:125
u_int x_handy
Definition: xdr.h:127
Definition: fci.c:127
xdr_op
Definition: xdr.h:84
static const struct xdr_ops xdrstdio_ops
Definition: xdr_stdio.c:62
void xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op)
Definition: xdr_stdio.c:79
static bool_t xdrstdio_setpos(XDR *, u_int)
Definition: xdr_stdio.c:162
static bool_t xdrstdio_getlong(XDR *, long *)
Definition: xdr_stdio.c:105
static int32_t * xdrstdio_inline(XDR *, u_int)
Definition: xdr_stdio.c:173
static void xdrstdio_destroy(XDR *)
Definition: xdr_stdio.c:97
static u_int xdrstdio_getpos(XDR *)
Definition: xdr_stdio.c:154
static bool_t xdrstdio_putlong(XDR *, const long *)
Definition: xdr_stdio.c:117
static bool_t xdrstdio_putbytes(XDR *, const char *, u_int)
Definition: xdr_stdio.c:141
static bool_t xdrstdio_getbytes(XDR *, char *, u_int)
Definition: xdr_stdio.c:129