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