configure.ac: clean up; byteswap.h: avoid redefinition errors
[portablexdr.git] / xdr_intXX_t.c
1 /* Copyright (c) 1998, 1999, 2000, 2004, 2005 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <rpc/types.h>
21 #include <rpc/xdr.h>
22
23 /* XDR 64bit integers */
24 bool_t
25 xdr_int64_t (XDR *xdrs, int64_t *ip)
26 {
27   int32_t t1, t2;
28
29   switch (xdrs->x_op)
30     {
31     case XDR_ENCODE:
32       t1 = (int32_t) ((*ip) >> 32);
33       t2 = (int32_t) (*ip);
34       return (XDR_PUTINT32(xdrs, &t1) && XDR_PUTINT32(xdrs, &t2));
35     case XDR_DECODE:
36       if (!XDR_GETINT32(xdrs, &t1) || !XDR_GETINT32(xdrs, &t2))
37         return FALSE;
38       *ip = ((int64_t) t1) << 32;
39       *ip |= (uint32_t) t2;     /* Avoid sign extension.  */
40       return TRUE;
41     case XDR_FREE:
42       return TRUE;
43     default:
44       return FALSE;
45     }
46 }
47
48 /* XDR 64bit unsigned integers */
49 bool_t
50 xdr_uint64_t (XDR *xdrs, uint64_t *uip)
51 {
52   uint32_t t1;
53   uint32_t t2;
54
55   switch (xdrs->x_op)
56     {
57     case XDR_ENCODE:
58       t1 = (uint32_t) ((*uip) >> 32);
59       t2 = (uint32_t) (*uip);
60       return (XDR_PUTINT32 (xdrs, (int32_t *) &t1) &&
61               XDR_PUTINT32(xdrs, (int32_t *) &t2));
62     case XDR_DECODE:
63       if (!XDR_GETINT32(xdrs, (int32_t *) &t1) ||
64           !XDR_GETINT32(xdrs, (int32_t *) &t2))
65         return FALSE;
66       *uip = ((uint64_t) t1) << 32;
67       *uip |= t2;
68       return TRUE;
69     case XDR_FREE:
70       return TRUE;
71     default:
72       return FALSE;
73     }
74 }
75
76 /* XDR 32bit integers */
77 bool_t
78 xdr_int32_t (XDR *xdrs, int32_t *lp)
79 {
80   switch (xdrs->x_op)
81     {
82     case XDR_ENCODE:
83       return XDR_PUTINT32 (xdrs, lp);
84     case XDR_DECODE:
85       return XDR_GETINT32 (xdrs, lp);
86     case XDR_FREE:
87       return TRUE;
88     default:
89       return FALSE;
90     }
91 }
92
93 /* XDR 32bit unsigned integers */
94 bool_t
95 xdr_uint32_t (XDR *xdrs, uint32_t *ulp)
96 {
97   switch (xdrs->x_op)
98     {
99     case XDR_ENCODE:
100       return XDR_PUTINT32 (xdrs, (int32_t *) ulp);
101     case XDR_DECODE:
102       return XDR_GETINT32 (xdrs, (int32_t *) ulp);
103     case XDR_FREE:
104       return TRUE;
105     default:
106       return FALSE;
107     }
108 }
109
110 /* XDR 16bit integers */
111 bool_t
112 xdr_int16_t (XDR *xdrs, int16_t *ip)
113 {
114   int32_t t;
115
116   switch (xdrs->x_op)
117     {
118     case XDR_ENCODE:
119       t = (int32_t) *ip;
120       return XDR_PUTINT32 (xdrs, &t);
121     case XDR_DECODE:
122       if (!XDR_GETINT32 (xdrs, &t))
123         return FALSE;
124       *ip = (int16_t) t;
125       return TRUE;
126     case XDR_FREE:
127       return TRUE;
128     default:
129       return FALSE;
130     }
131 }
132
133 /* XDR 16bit unsigned integers */
134 bool_t
135 xdr_uint16_t (XDR *xdrs, uint16_t *uip)
136 {
137   uint32_t ut;
138
139   switch (xdrs->x_op)
140     {
141     case XDR_ENCODE:
142       ut = (uint32_t) *uip;
143       return XDR_PUTINT32 (xdrs, (int32_t *) &ut);
144     case XDR_DECODE:
145       if (!XDR_GETINT32 (xdrs, (int32_t *) &ut))
146         return FALSE;
147       *uip = (uint16_t) ut;
148       return TRUE;
149     case XDR_FREE:
150       return TRUE;
151     default:
152       return FALSE;
153     }
154 }
155
156 /* XDR 8bit integers */
157 bool_t
158 xdr_int8_t (XDR *xdrs, int8_t *ip)
159 {
160   int32_t t;
161
162   switch (xdrs->x_op)
163     {
164     case XDR_ENCODE:
165       t = (int32_t) *ip;
166       return XDR_PUTINT32 (xdrs, &t);
167     case XDR_DECODE:
168       if (!XDR_GETINT32 (xdrs, &t))
169         return FALSE;
170       *ip = (int8_t) t;
171       return TRUE;
172     case XDR_FREE:
173       return TRUE;
174     default:
175       return FALSE;
176     }
177 }
178
179 /* XDR 8bit unsigned integers */
180 bool_t
181 xdr_uint8_t (XDR *xdrs, uint8_t *uip)
182 {
183   uint32_t ut;
184
185   switch (xdrs->x_op)
186     {
187     case XDR_ENCODE:
188       ut = (uint32_t) *uip;
189       return XDR_PUTINT32 (xdrs, (int32_t *) &ut);
190     case XDR_DECODE:
191       if (!XDR_GETINT32 (xdrs, (int32_t *) &ut))
192         return FALSE;
193       *uip = (uint8_t) ut;
194       return TRUE;
195     case XDR_FREE:
196       return TRUE;
197     default:
198       return FALSE;
199     }
200 }