xref: /openbsd/lib/libc/rpc/xdr_float.c (revision 696cd27b)
1*696cd27bSderaadt /*	$OpenBSD: xdr_float.c,v 1.23 2016/03/09 16:28:47 deraadt Exp $ */
2cb7760d1Smillert 
3df930be7Sderaadt /*
4cb7760d1Smillert  * Copyright (c) 2010, Oracle America, Inc.
5df930be7Sderaadt  *
6cb7760d1Smillert  * Redistribution and use in source and binary forms, with or without
7cb7760d1Smillert  * modification, are permitted provided that the following conditions are
8cb7760d1Smillert  * met:
9df930be7Sderaadt  *
10cb7760d1Smillert  *     * Redistributions of source code must retain the above copyright
11cb7760d1Smillert  *       notice, this list of conditions and the following disclaimer.
12cb7760d1Smillert  *     * Redistributions in binary form must reproduce the above
13cb7760d1Smillert  *       copyright notice, this list of conditions and the following
14cb7760d1Smillert  *       disclaimer in the documentation and/or other materials
15cb7760d1Smillert  *       provided with the distribution.
16cb7760d1Smillert  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17cb7760d1Smillert  *       contributors may be used to endorse or promote products derived
18cb7760d1Smillert  *       from this software without specific prior written permission.
19df930be7Sderaadt  *
20cb7760d1Smillert  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21cb7760d1Smillert  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22cb7760d1Smillert  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23cb7760d1Smillert  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24cb7760d1Smillert  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25cb7760d1Smillert  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26cb7760d1Smillert  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27cb7760d1Smillert  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28cb7760d1Smillert  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29cb7760d1Smillert  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30cb7760d1Smillert  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31cb7760d1Smillert  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32df930be7Sderaadt  */
33df930be7Sderaadt 
34df930be7Sderaadt /*
35c8ba362dSespie  * xdr_float.c, Generic XDR routines implementation.
36df930be7Sderaadt  *
37df930be7Sderaadt  * These are the "floating point" xdr routines used to (de)serialize
38df930be7Sderaadt  * most common data items.  See xdr.h for more info on the interface to
39df930be7Sderaadt  * xdr.
40df930be7Sderaadt  */
41df930be7Sderaadt 
42df930be7Sderaadt #include <stdio.h>
43df930be7Sderaadt #include <sys/types.h>
44df930be7Sderaadt #include <rpc/types.h>
45df930be7Sderaadt #include <rpc/xdr.h>
46df930be7Sderaadt 
47df930be7Sderaadt /*
48df930be7Sderaadt  * NB: Not portable.
49*696cd27bSderaadt  * This routine works on machines with IEEE754 FP.
50df930be7Sderaadt  */
51df930be7Sderaadt 
52be9b7050Sguenther #include <endian.h>
53df930be7Sderaadt 
54df930be7Sderaadt bool_t
xdr_float(XDR * xdrs,float * fp)55384ec30aSotto xdr_float(XDR *xdrs, float *fp)
56df930be7Sderaadt {
57df930be7Sderaadt 	bool_t rv;
58df930be7Sderaadt 	long tmpl;
59df930be7Sderaadt 	switch (xdrs->x_op) {
60df930be7Sderaadt 
61df930be7Sderaadt 	case XDR_ENCODE:
62df930be7Sderaadt 		tmpl = *(int32_t *)fp;
63df930be7Sderaadt 		return (XDR_PUTLONG(xdrs, &tmpl));
64df930be7Sderaadt 
65df930be7Sderaadt 	case XDR_DECODE:
66df930be7Sderaadt 		rv = XDR_GETLONG(xdrs, &tmpl);
67df930be7Sderaadt 		*(int32_t *)fp = tmpl;
68df930be7Sderaadt 		return (rv);
69df930be7Sderaadt 
70df930be7Sderaadt 	case XDR_FREE:
71df930be7Sderaadt 		return (TRUE);
72df930be7Sderaadt 	}
73df930be7Sderaadt 	return (FALSE);
74df930be7Sderaadt }
75df930be7Sderaadt 
76df930be7Sderaadt bool_t
xdr_double(XDR * xdrs,double * dp)77384ec30aSotto xdr_double(XDR *xdrs, double *dp)
78df930be7Sderaadt {
7941aa8645Sderaadt 	int32_t *i32p;
80df930be7Sderaadt 	bool_t rv;
81df930be7Sderaadt 	long tmpl;
82df930be7Sderaadt 
83df930be7Sderaadt 	switch (xdrs->x_op) {
84df930be7Sderaadt 
85df930be7Sderaadt 	case XDR_ENCODE:
86df930be7Sderaadt 		i32p = (int32_t *)dp;
87016765a7Smartynas #if (BYTE_ORDER == BIG_ENDIAN) || (defined(__arm__) && !defined(__VFP_FP__))
88df930be7Sderaadt 		tmpl = *i32p++;
89df930be7Sderaadt 		rv = XDR_PUTLONG(xdrs, &tmpl);
90df930be7Sderaadt 		if (!rv)
91df930be7Sderaadt 			return (rv);
92df930be7Sderaadt 		tmpl = *i32p;
93df930be7Sderaadt 		rv = XDR_PUTLONG(xdrs, &tmpl);
94df930be7Sderaadt #else
95df930be7Sderaadt 		tmpl = *(i32p+1);
96df930be7Sderaadt 		rv = XDR_PUTLONG(xdrs, &tmpl);
97df930be7Sderaadt 		if (!rv)
98df930be7Sderaadt 			return (rv);
99df930be7Sderaadt 		tmpl = *i32p;
100df930be7Sderaadt 		rv = XDR_PUTLONG(xdrs, &tmpl);
101df930be7Sderaadt #endif
102df930be7Sderaadt 		return (rv);
103df930be7Sderaadt 
104df930be7Sderaadt 	case XDR_DECODE:
105df930be7Sderaadt 		i32p = (int32_t *)dp;
106016765a7Smartynas #if (BYTE_ORDER == BIG_ENDIAN) || (defined(__arm__) && !defined(__VFP_FP__))
107df930be7Sderaadt 		rv = XDR_GETLONG(xdrs, &tmpl);
108df930be7Sderaadt 		*i32p++ = tmpl;
109df930be7Sderaadt 		if (!rv)
110df930be7Sderaadt 			return (rv);
111df930be7Sderaadt 		rv = XDR_GETLONG(xdrs, &tmpl);
112df930be7Sderaadt 		*i32p = tmpl;
113df930be7Sderaadt #else
114df930be7Sderaadt 		rv = XDR_GETLONG(xdrs, &tmpl);
115df930be7Sderaadt 		*(i32p+1) = tmpl;
116df930be7Sderaadt 		if (!rv)
117df930be7Sderaadt 			return (rv);
118df930be7Sderaadt 		rv = XDR_GETLONG(xdrs, &tmpl);
119df930be7Sderaadt 		*i32p = tmpl;
120df930be7Sderaadt #endif
121df930be7Sderaadt 		return (rv);
122df930be7Sderaadt 
123df930be7Sderaadt 	case XDR_FREE:
124df930be7Sderaadt 		return (TRUE);
125df930be7Sderaadt 	}
126df930be7Sderaadt 	return (FALSE);
127df930be7Sderaadt }
128