18360efbdSAlfred Perlstein /* $NetBSD: xdr_stdio.c,v 1.14 2000/01/22 22:19:19 mycroft Exp $ */
28360efbdSAlfred Perlstein
3a204967aSHiroki Sato /*-
4*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni *
6a204967aSHiroki Sato * Copyright (c) 2010, Oracle America, Inc.
7eae561b3SGarrett Wollman *
8a204967aSHiroki Sato * Redistribution and use in source and binary forms, with or without
9a204967aSHiroki Sato * modification, are permitted provided that the following conditions are
10a204967aSHiroki Sato * met:
11eae561b3SGarrett Wollman *
12a204967aSHiroki Sato * * Redistributions of source code must retain the above copyright
13a204967aSHiroki Sato * notice, this list of conditions and the following disclaimer.
14a204967aSHiroki Sato * * Redistributions in binary form must reproduce the above
15a204967aSHiroki Sato * copyright notice, this list of conditions and the following
16a204967aSHiroki Sato * disclaimer in the documentation and/or other materials
17a204967aSHiroki Sato * provided with the distribution.
18a204967aSHiroki Sato * * Neither the name of the "Oracle America, Inc." nor the names of its
19a204967aSHiroki Sato * contributors may be used to endorse or promote products derived
20a204967aSHiroki Sato * from this software without specific prior written permission.
21eae561b3SGarrett Wollman *
22a204967aSHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23a204967aSHiroki Sato * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24a204967aSHiroki Sato * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25a204967aSHiroki Sato * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26a204967aSHiroki Sato * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27a204967aSHiroki Sato * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28a204967aSHiroki Sato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29a204967aSHiroki Sato * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30a204967aSHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31a204967aSHiroki Sato * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32a204967aSHiroki Sato * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33a204967aSHiroki Sato * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34eae561b3SGarrett Wollman */
35eae561b3SGarrett Wollman
36eae561b3SGarrett Wollman /*
37eae561b3SGarrett Wollman * xdr_stdio.c, XDR implementation on standard i/o file.
38eae561b3SGarrett Wollman *
39eae561b3SGarrett Wollman * This set of routines implements a XDR on a stdio stream.
40eae561b3SGarrett Wollman * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
41eae561b3SGarrett Wollman * from the stream.
42eae561b3SGarrett Wollman */
43eae561b3SGarrett Wollman
448360efbdSAlfred Perlstein #include "namespace.h"
45eae561b3SGarrett Wollman #include <stdio.h>
46eae561b3SGarrett Wollman
47fd8e4ebcSMike Barcroft #include <arpa/inet.h>
488360efbdSAlfred Perlstein #include <rpc/types.h>
498360efbdSAlfred Perlstein #include <rpc/xdr.h>
508360efbdSAlfred Perlstein #include "un-namespace.h"
518360efbdSAlfred Perlstein
52c05ac53bSDavid E. O'Brien static void xdrstdio_destroy(XDR *);
53c05ac53bSDavid E. O'Brien static bool_t xdrstdio_getlong(XDR *, long *);
54c05ac53bSDavid E. O'Brien static bool_t xdrstdio_putlong(XDR *, const long *);
55c05ac53bSDavid E. O'Brien static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
56c05ac53bSDavid E. O'Brien static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
57c05ac53bSDavid E. O'Brien static u_int xdrstdio_getpos(XDR *);
58c05ac53bSDavid E. O'Brien static bool_t xdrstdio_setpos(XDR *, u_int);
59c05ac53bSDavid E. O'Brien static int32_t *xdrstdio_inline(XDR *, u_int);
60eae561b3SGarrett Wollman
61eae561b3SGarrett Wollman /*
62eae561b3SGarrett Wollman * Ops vector for stdio type XDR
63eae561b3SGarrett Wollman */
648360efbdSAlfred Perlstein static const struct xdr_ops xdrstdio_ops = {
65eae561b3SGarrett Wollman xdrstdio_getlong, /* deseraialize a long int */
66eae561b3SGarrett Wollman xdrstdio_putlong, /* seraialize a long int */
67eae561b3SGarrett Wollman xdrstdio_getbytes, /* deserialize counted bytes */
68eae561b3SGarrett Wollman xdrstdio_putbytes, /* serialize counted bytes */
69eae561b3SGarrett Wollman xdrstdio_getpos, /* get offset in the stream */
70eae561b3SGarrett Wollman xdrstdio_setpos, /* set offset in the stream */
71eae561b3SGarrett Wollman xdrstdio_inline, /* prime stream for inline macros */
72eae561b3SGarrett Wollman xdrstdio_destroy /* destroy stream */
73eae561b3SGarrett Wollman };
74eae561b3SGarrett Wollman
75eae561b3SGarrett Wollman /*
76eae561b3SGarrett Wollman * Initialize a stdio xdr stream.
77eae561b3SGarrett Wollman * Sets the xdr stream handle xdrs for use on the stream file.
78eae561b3SGarrett Wollman * Operation flag is set to op.
79eae561b3SGarrett Wollman */
80eae561b3SGarrett Wollman void
xdrstdio_create(XDR * xdrs,FILE * file,enum xdr_op op)81d660d38dSCraig Rodrigues xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op)
82eae561b3SGarrett Wollman {
83eae561b3SGarrett Wollman
84eae561b3SGarrett Wollman xdrs->x_op = op;
85eae561b3SGarrett Wollman xdrs->x_ops = &xdrstdio_ops;
868360efbdSAlfred Perlstein xdrs->x_private = file;
87eae561b3SGarrett Wollman xdrs->x_handy = 0;
88eae561b3SGarrett Wollman xdrs->x_base = 0;
89eae561b3SGarrett Wollman }
90eae561b3SGarrett Wollman
91eae561b3SGarrett Wollman /*
92eae561b3SGarrett Wollman * Destroy a stdio xdr stream.
93eae561b3SGarrett Wollman * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
94eae561b3SGarrett Wollman */
95eae561b3SGarrett Wollman static void
xdrstdio_destroy(XDR * xdrs)96d660d38dSCraig Rodrigues xdrstdio_destroy(XDR *xdrs)
97eae561b3SGarrett Wollman {
98eae561b3SGarrett Wollman (void)fflush((FILE *)xdrs->x_private);
998360efbdSAlfred Perlstein /* XXX: should we close the file ?? */
1001ad08a09SPeter Wemm }
101eae561b3SGarrett Wollman
102eae561b3SGarrett Wollman static bool_t
xdrstdio_getlong(XDR * xdrs,long * lp)103d660d38dSCraig Rodrigues xdrstdio_getlong(XDR *xdrs, long *lp)
104eae561b3SGarrett Wollman {
10509c37f5dSMartin Blapp u_int32_t temp;
106eae561b3SGarrett Wollman
10709c37f5dSMartin Blapp if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
108eae561b3SGarrett Wollman return (FALSE);
10909c37f5dSMartin Blapp *lp = (long)ntohl(temp);
110eae561b3SGarrett Wollman return (TRUE);
111eae561b3SGarrett Wollman }
112eae561b3SGarrett Wollman
113eae561b3SGarrett Wollman static bool_t
xdrstdio_putlong(XDR * xdrs,const long * lp)114d660d38dSCraig Rodrigues xdrstdio_putlong(XDR *xdrs, const long *lp)
115eae561b3SGarrett Wollman {
11609c37f5dSMartin Blapp int32_t mycopy = htonl((u_int32_t)*lp);
117eae561b3SGarrett Wollman
1188360efbdSAlfred Perlstein if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
119eae561b3SGarrett Wollman return (FALSE);
120eae561b3SGarrett Wollman return (TRUE);
121eae561b3SGarrett Wollman }
122eae561b3SGarrett Wollman
123eae561b3SGarrett Wollman static bool_t
xdrstdio_getbytes(XDR * xdrs,char * addr,u_int len)124d660d38dSCraig Rodrigues xdrstdio_getbytes(XDR *xdrs, char *addr, u_int len)
125eae561b3SGarrett Wollman {
126eae561b3SGarrett Wollman
1278360efbdSAlfred Perlstein if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
128eae561b3SGarrett Wollman return (FALSE);
129eae561b3SGarrett Wollman return (TRUE);
130eae561b3SGarrett Wollman }
131eae561b3SGarrett Wollman
132eae561b3SGarrett Wollman static bool_t
xdrstdio_putbytes(XDR * xdrs,const char * addr,u_int len)133d660d38dSCraig Rodrigues xdrstdio_putbytes(XDR *xdrs, const char *addr, u_int len)
134eae561b3SGarrett Wollman {
135eae561b3SGarrett Wollman
1368360efbdSAlfred Perlstein if ((len != 0) && (fwrite(addr, (size_t)len, 1,
1378360efbdSAlfred Perlstein (FILE *)xdrs->x_private) != 1))
138eae561b3SGarrett Wollman return (FALSE);
139eae561b3SGarrett Wollman return (TRUE);
140eae561b3SGarrett Wollman }
141eae561b3SGarrett Wollman
142eae561b3SGarrett Wollman static u_int
xdrstdio_getpos(XDR * xdrs)143d660d38dSCraig Rodrigues xdrstdio_getpos(XDR *xdrs)
144eae561b3SGarrett Wollman {
145eae561b3SGarrett Wollman
146eae561b3SGarrett Wollman return ((u_int) ftell((FILE *)xdrs->x_private));
147eae561b3SGarrett Wollman }
148eae561b3SGarrett Wollman
149eae561b3SGarrett Wollman static bool_t
xdrstdio_setpos(XDR * xdrs,u_int pos)150d660d38dSCraig Rodrigues xdrstdio_setpos(XDR *xdrs, u_int pos)
151eae561b3SGarrett Wollman {
152eae561b3SGarrett Wollman
153eae561b3SGarrett Wollman return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
154eae561b3SGarrett Wollman FALSE : TRUE);
155eae561b3SGarrett Wollman }
156eae561b3SGarrett Wollman
1578360efbdSAlfred Perlstein /* ARGSUSED */
1581ad08a09SPeter Wemm static int32_t *
xdrstdio_inline(XDR * xdrs,u_int len)159d660d38dSCraig Rodrigues xdrstdio_inline(XDR *xdrs, u_int len)
160eae561b3SGarrett Wollman {
161eae561b3SGarrett Wollman
162eae561b3SGarrett Wollman /*
163eae561b3SGarrett Wollman * Must do some work to implement this: must insure
164eae561b3SGarrett Wollman * enough data in the underlying stdio buffer,
165eae561b3SGarrett Wollman * that the buffer is aligned so that we can indirect through a
166eae561b3SGarrett Wollman * long *, and stuff this pointer in xdrs->x_buf. Doing
167eae561b3SGarrett Wollman * a fread or fwrite to a scratch buffer would defeat
168eae561b3SGarrett Wollman * most of the gains to be had here and require storage
169eae561b3SGarrett Wollman * management on this buffer, so we don't do this.
170eae561b3SGarrett Wollman */
171eae561b3SGarrett Wollman return (NULL);
172eae561b3SGarrett Wollman }
173