0a96509fa9f861eaaf4818190a1cd6e8678b9e28
[shibboleth/cpp-sp.git] / oncrpc / xdr_mem.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)xdr_mem.c        2.1 88/07/29 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  * 
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  * 
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  * 
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  * 
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  * 
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
43 #endif
44
45 /*
46  * xdr_mem.h, XDR implementation using memory buffers.
47  *
48  * Copyright (C) 1984, Sun Microsystems, Inc.
49  *
50  * If you have some data to be interpreted as external data representation
51  * or to be converted to external data representation in a memory buffer,
52  * then this is the package for you.
53  *
54  */
55
56
57 #ifdef WIN32
58 #include <rpc/rpc.h>
59 #include <rpc/xdr.h>
60 #else
61 #include <rpc/types.h>
62 #include <rpc/xdr.h>
63 #include <netinet/in.h>
64 #endif
65
66 static bool_t   xdrmem_getlong();
67 static bool_t   xdrmem_putlong();
68 static bool_t   xdrmem_getbytes();
69 static bool_t   xdrmem_putbytes();
70 static u_int    xdrmem_getpos();
71 static bool_t   xdrmem_setpos();
72 static long *   xdrmem_inline();
73 static void     xdrmem_destroy();
74
75 static struct   xdr_ops xdrmem_ops = {
76         xdrmem_getlong,
77         xdrmem_putlong,
78         xdrmem_getbytes,
79         xdrmem_putbytes,
80         xdrmem_getpos,
81         xdrmem_setpos,
82         xdrmem_inline,
83         xdrmem_destroy
84 };
85
86 /*
87  * The procedure xdrmem_create initializes a stream descriptor for a
88  * memory buffer.  
89  */
90 void
91 xdrmem_create(xdrs, addr, size, op)
92         register XDR *xdrs;
93         caddr_t addr;
94         u_int size;
95         enum xdr_op op;
96 {
97
98         xdrs->x_op = op;
99         xdrs->x_ops = &xdrmem_ops;
100         xdrs->x_private = xdrs->x_base = addr;
101         xdrs->x_handy = size;
102 }
103
104 static void
105 xdrmem_destroy(/*xdrs*/)
106         /*XDR *xdrs;*/
107 {
108 }
109
110 static bool_t
111 xdrmem_getlong(xdrs, lp)
112         register XDR *xdrs;
113         long *lp;
114 {
115
116         if (xdrs->x_handy < sizeof(long))
117                 return (FALSE);
118         xdrs->x_handy -= sizeof(long);
119         *lp = (long)ntohl((u_long)(*((long *)(xdrs->x_private))));
120         xdrs->x_private += sizeof(long);
121         return (TRUE);
122 }
123
124 static bool_t
125 xdrmem_putlong(xdrs, lp)
126         register XDR *xdrs;
127         long *lp;
128 {
129
130         if (xdrs->x_handy < sizeof(long))
131                 return (FALSE);
132         xdrs->x_handy -= sizeof(long);
133         *(long *)xdrs->x_private = (long)htonl((u_long)(*lp));
134         xdrs->x_private += sizeof(long);
135         return (TRUE);
136 }
137
138 static bool_t
139 xdrmem_getbytes(xdrs, addr, len)
140         register XDR *xdrs;
141         caddr_t addr;
142         register u_int len;
143 {
144
145         if (xdrs->x_handy < len)
146                 return (FALSE);
147         xdrs->x_handy -= len;
148         bcopy(xdrs->x_private, addr, len);
149         xdrs->x_private += len;
150         return (TRUE);
151 }
152
153 static bool_t
154 xdrmem_putbytes(xdrs, addr, len)
155         register XDR *xdrs;
156         caddr_t addr;
157         register u_int len;
158 {
159
160         if (xdrs->x_handy < len)
161                 return (FALSE);
162         xdrs->x_handy -= len;
163         bcopy(addr, xdrs->x_private, len);
164         xdrs->x_private += len;
165         return (TRUE);
166 }
167
168 static u_int
169 xdrmem_getpos(xdrs)
170         register XDR *xdrs;
171 {
172
173         return ((u_int)xdrs->x_private - (u_int)xdrs->x_base);
174 }
175
176 static bool_t
177 xdrmem_setpos(xdrs, pos)
178         register XDR *xdrs;
179         u_int pos;
180 {
181         register caddr_t newaddr = xdrs->x_base + pos;
182         register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
183
184         if (newaddr > lastaddr)
185                 return (FALSE);
186         xdrs->x_private = newaddr;
187         xdrs->x_handy = (u_int)(lastaddr - newaddr);
188         return (TRUE);
189 }
190
191 static long *
192 xdrmem_inline(xdrs, len)
193         register XDR *xdrs;
194         int len;
195 {
196         long *buf = 0;
197
198         if (xdrs->x_handy >= len) {
199                 xdrs->x_handy -= len;
200                 buf = (long *) xdrs->x_private;
201                 xdrs->x_private += len;
202         }
203         return (buf);
204 }