Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / xdr_mem.c
1 #ifndef lint
2 static char sfs_xdr_mem_id[] = "@(#)xdr_mem.c     2.1     97/10/23";
3 #endif
4 /* @(#)xdr_mem.c        2.1 88/07/29 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * xdr_mem.h, XDR implementation using memory buffers.
57  *
58  * Copyright (C) 1984, Sun Microsystems, Inc.
59  *
60  * If you have some data to be interpreted as external data representation
61  * or to be converted to external data representation in a memory buffer,
62  * then this is the package for you.
63  *
64  */
65
66 #include <string.h>
67 #include "rpc/types.h"
68 #include "rpc/xdr.h"
69 #include "rpc/osdep.h"
70
71 static bool_t   xdrmem_getlong(XDR *, int32_t *);
72 static bool_t   xdrmem_putlong(XDR *, int32_t *);
73 static bool_t   xdrmem_getbytes(XDR *, void *, uint_t);
74 static bool_t   xdrmem_putbytes(XDR *, void *, uint_t);
75 static uint_t   xdrmem_getpos(XDR *);
76 static bool_t   xdrmem_setpos(XDR *, uint_t);
77 static int32_t *xdrmem_inline(XDR *, uint_t);
78 static void     xdrmem_destroy(XDR *);
79
80 static struct   xdr_ops xdrmem_ops = {
81         xdrmem_getlong,
82         xdrmem_putlong,
83         xdrmem_getbytes,
84         xdrmem_putbytes,
85         xdrmem_getpos,
86         xdrmem_setpos,
87         xdrmem_inline,
88         xdrmem_destroy
89 };
90
91 /*
92  * The procedure xdrmem_create initializes a stream descriptor for a
93  * memory buffer.  
94  */
95 void
96 xdrmem_create(
97         XDR *xdrs,
98         void *addr,
99         uint_t size,
100         enum xdr_op op)
101 {
102
103         xdrs->x_op = op;
104         xdrs->x_ops = &xdrmem_ops;
105         xdrs->x_private = xdrs->x_base = addr;
106         xdrs->x_handy = size;
107 }
108
109 /* ARGSUSED */
110 static void
111 xdrmem_destroy(XDR *xdrs)
112 {
113 }
114
115 static bool_t
116 xdrmem_getlong(
117         XDR *xdrs,
118         int32_t *lp)
119 {
120
121         if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
122                 return (FALSE);
123         *lp = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
124         xdrs->x_private += sizeof(int32_t);
125         return (TRUE);
126 }
127
128 static bool_t
129 xdrmem_putlong(
130         XDR *xdrs,
131         int32_t *lp)
132 {
133
134         if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
135                 return (FALSE);
136         *(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*lp));
137         xdrs->x_private += sizeof(int32_t);
138         return (TRUE);
139 }
140
141 static bool_t
142 xdrmem_getbytes(
143         XDR *xdrs,
144         void *addr,
145         uint_t len)
146 {
147
148         if ((xdrs->x_handy -= len) < 0)
149                 return (FALSE);
150         memmove(addr, xdrs->x_private, len);
151         xdrs->x_private += len;
152         return (TRUE);
153 }
154
155 static bool_t
156 xdrmem_putbytes(
157         XDR *xdrs,
158         void *addr,
159         uint_t len)
160 {
161
162         if ((xdrs->x_handy -= len) < 0)
163                 return (FALSE);
164         memmove(xdrs->x_private, addr, len);
165         xdrs->x_private += len;
166         return (TRUE);
167 }
168
169 static uint_t
170 xdrmem_getpos(
171         XDR *xdrs)
172 {
173
174         return ((uint_t)xdrs->x_private - (uint_t)xdrs->x_base);
175 }
176
177 static bool_t
178 xdrmem_setpos(
179         XDR *xdrs,
180         uint_t pos)
181 {
182         char * newaddr = xdrs->x_base + pos;
183         char * lastaddr = xdrs->x_private + xdrs->x_handy;
184
185         if ((int32_t)newaddr > (int32_t)lastaddr)
186                 return (FALSE);
187         xdrs->x_private = newaddr;
188         xdrs->x_handy = (int)lastaddr - (int)newaddr;
189         return (TRUE);
190 }
191
192 static int32_t *
193 xdrmem_inline(
194         XDR *xdrs,
195         uint_t len)
196 {
197         int32_t *buf = 0;
198
199         if (xdrs->x_handy >= len) {
200                 xdrs->x_handy -= len;
201                 buf = (int32_t *) xdrs->x_private;
202                 xdrs->x_private += len;
203         }
204         return (buf);
205 }