r114: *** empty log message ***
[ctsim.git] / libctsupport / byteorder.cpp
1 #if HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #if HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8
9 #include "ctsupport.h"
10 #include "byteorder.h"
11
12
13 inline void
14 SwapBytes2 (void* buffer)
15 {
16   unsigned char* p = static_cast<unsigned char*>(buffer);
17   unsigned char temp = p[0];
18   p[0] = p[1];
19   p[1] = temp;
20 }
21
22 // 0<->3  1<->2 = 0123 -> 3210
23 inline void
24 SwapBytes4 (void* buffer)
25 {
26   unsigned char* p = static_cast<unsigned char*>(buffer);
27   unsigned char temp = p[0];
28   p[0] = p[3];
29   p[3] = temp;
30   temp = p[1];
31   p[1] = p[2];
32   p[2] = temp;
33 }
34
35 // 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
36 inline void
37 SwapBytes8 (void* buffer)
38 {
39   unsigned char* p = static_cast<unsigned char*>(buffer);
40   unsigned char temp = p[0];
41   p[0] = p[7];
42   p[7] = temp;
43   temp = p[1];
44   p[1] = p[6];
45   p[6] = temp;
46   temp = p[2];
47   p[2] = p[5];
48   p[5] = temp;
49   temp = p[3];
50   p[3] = p[4];
51   p[4] = temp;
52 }
53
54 void 
55 ConvertNetworkOrder (void* buffer, size_t bytes)
56 {
57 #if ! defined (WORDS_BIGENDIAN)
58     if (bytes < 2)
59         return;
60
61     char* start = static_cast<char*>(buffer);
62     char* end = start + bytes - 1;   // last byte
63     size_t nSwap = bytes / 2;
64     
65     while (nSwap-- > 0) {
66         unsigned char c = *start;
67         *start++ = *end;
68         *end-- = c;
69     }
70 #endif    
71 }
72
73 void 
74 ConvertReverseNetworkOrder (void* buffer, size_t bytes)
75 {
76 #if defined (WORDS_BIGENDIAN)
77     if (bytes < 2)
78         return;
79
80     char* start = static_cast<char*>(buffer);
81     char* end = start + bytes - 1;  // last byte 
82     size_t nSwap = bytes / 2;
83     
84     while (nSwap-- > 0) {
85         unsigned char c = *start;
86         *start++ = *end;
87         *end-- = c;
88     }
89 #endif    
90 }
91
92 bool
93 write_nint16 (kuint16 const *n_in, int fd)
94 {
95     kuint16 n = *n_in;
96
97 #ifndef WORDS_BIGENDIAN
98     SwapBytes2 (&n);
99 #endif
100
101     if (write (fd, &n, 2) != 2)
102         return false;
103     else
104         return true;
105 }
106
107 bool 
108 read_nint16 (kuint16 *n_out, int fd)
109 {
110     if (read (fd, n_out, 2) != 2)
111       return false;
112
113 #ifndef WORDS_BIGENDIAN
114     SwapBytes2 (n_out);
115 #endif
116     
117     return true;
118 }
119
120 bool 
121 write_nint32 (kuint32 const *n_in, int fd)
122 {
123     kuint32 n = *n_in;
124
125 #ifndef WORDS_BIGENDIAN
126     SwapBytes4(&n);
127 #endif
128
129     if (write (fd, &n, 4) != 4)
130       return false;
131     else
132       return true;
133 }
134
135 bool 
136 read_nint32 (kuint32 *n_out, int fd)
137 {
138   if (read (fd, n_out, 4) != 4)
139     return false;
140
141 #ifndef WORDS_BIGENDIAN
142   SwapBytes4 (n_out);
143 #endif
144
145     return true;
146 }
147
148 bool 
149 write_nfloat32 (kfloat32 const *f_in, int fd)
150 {
151   kfloat32 f = *f_in;
152
153 #ifndef WORDS_BIGENDIAN
154   SwapBytes4 (&f);
155 #endif
156
157   if (write (fd, &f, 4) != 4)
158     return false;
159   else
160     return true;
161 }
162
163 bool 
164 read_nfloat32 (kfloat32 *f_out, int fd)
165 {
166   if (read (fd, f_out, 4) != 4)
167     return false;
168
169 #ifndef WORDS_BIGENDIAN
170   SwapBytes4(f_out);
171 #endif
172
173   return true;
174 }
175
176 bool 
177 write_nfloat64 (kfloat64 const *f_in, int fd)
178 {
179   kfloat64 f = *f_in;
180
181 #ifndef WORDS_BIGENDIAN
182   SwapBytes8 (&f);
183 #endif
184
185   if (write (fd, &f, 8) != 8)
186     return false;
187   else
188     return true;
189 }
190
191 bool 
192 read_nfloat64 (kfloat64 *f_out, int fd)
193 {
194   if (read (fd, f_out, 8) != 8)
195     return false;
196
197 #ifndef WORDS_BIGENDIAN
198   SwapBytes8 (f_out);
199 #endif
200     
201   return true;
202 }
203
204
205
206 onetorderstream& onetorderstream::writeInt16 (kuint16 n) {
207 #ifndef WORDS_BIGENDIAN
208   SwapBytes2 (&n);
209 #endif
210   write (&n, 2);
211   return (*this);
212 }
213
214 inetorderstream& inetorderstream::readInt16 (kuint16& n) {
215   read (&n, 2);
216 #ifndef WORDS_BIGENDIAN
217   SwapBytes2 (&n);
218 #endif
219   return (*this);
220 }
221
222 onetorderstream& onetorderstream::writeInt32 (kuint32 n) {
223 #ifndef WORDS_BIGENDIAN
224   SwapBytes4(&n);
225 #endif
226   write (&n, 4);
227   return (*this);
228 }
229
230 inetorderstream& inetorderstream::readInt32 (kuint32& n) {
231   read (&n, 4);
232 #ifndef WORDS_BIGENDIAN
233   SwapBytes4 (&n);
234 #endif
235   return (*this);
236 }
237
238 onetorderstream& onetorderstream::writeFloat32 (kfloat32 n) {
239 #ifndef WORDS_BIGENDIAN
240   SwapBytes4 (&n);
241 #endif
242   write (&n, 4);
243   return (*this);
244 }
245
246 inetorderstream& inetorderstream::readFloat32 (kfloat32& n) {
247   read (&n, 4);
248 #ifndef WORDS_BIGENDIAN
249   SwapBytes4 (&n);
250 #endif
251   return (*this);
252 }
253
254 onetorderstream& onetorderstream::writeFloat64 (kfloat64 n) {
255 #ifndef WORDS_BIGENDIAN
256   SwapBytes8 (&n);
257 #endif
258   write (&n, 8);
259   return (*this);
260 }
261
262 inetorderstream& inetorderstream::readFloat64 (kfloat64& n) {
263   read (&n, 8);
264 #ifndef WORDS_BIGENDIAN
265   SwapBytes8 (&n);
266 #endif
267   return (*this);
268 }
269
270
271
272 void
273 write_rnint16 (kuint16 n, ostream& ostr)
274 {
275 #ifdef WORDS_BIGENDIAN
276   SwapBytes2 (&n);
277 #endif
278   ostr.write (&n, 2);
279 }
280
281 void
282 read_rnint16 (kuint16& n, istream& istr)
283 {
284   istr.read (&n, 2);
285 #ifdef WORDS_BIGENDIAN
286   SwapBytes2 (&n);
287 #endif
288 }
289
290 void
291 write_rnint32 (kuint32 n, ostream& ostr)
292 {
293 #ifdef WORDS_BIGENDIAN
294   SwapBytes4(&n);
295 #endif
296   ostr.write (&n, 4);
297 }
298
299 void
300 read_rnint32 (kuint32& n, istream istr)
301 {
302   istr.read (&n, 4);
303 #ifdef WORDS_BIGENDIAN
304   SwapBytes4 (&n);
305 #endif
306 }
307
308 void
309 write_rnfloat32 (kfloat32 n, ostream ostr)
310 {
311 #ifdef WORDS_BIGENDIAN
312   SwapBytes4 (&n);
313 #endif
314   ostr.write (&n, 4);
315 }
316
317 void
318 read_rnfloat32 (kfloat32& n, istream istr)
319 {
320   istr.read (&n, 4);
321 #ifdef WORDS_BIGENDIAN
322   SwapBytes4 (&n);
323 #endif
324 }
325
326 void
327 write_rnfloat64 (kfloat64 n, ostream ostr)
328 {
329 #ifdef WORDS_BIGENDIAN
330   SwapBytes8 (&n);
331 #endif
332   ostr.write (&n, 8);
333 }
334
335 void
336 read_rnfloat64 (kfloat64& n, istream istr)
337 {
338   istr.read (&n, 8);
339 #ifdef WORDS_BIGENDIAN
340   SwapBytes8 (&n);
341 #endif
342 }
343