libarmutils  1.4
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups
endian_swap.c
Go to the documentation of this file.
1 /*******************************************************************************
2 *
3 * COPYRIGHT (C) 2010 Battelle Memorial Institute. All Rights Reserved.
4 *
5 ********************************************************************************
6 *
7 * Author:
8 * name: Brian Ermold
9 * phone: (509) 375-2277
10 * email: brian.ermold@pnl.gov
11 *
12 ********************************************************************************
13 *
14 * REPOSITORY INFORMATION:
15 * $Revision: 9450 $
16 * $Author: ermold $
17 * $Date: 2011-10-14 16:50:01 +0000 (Fri, 14 Oct 2011) $
18 *
19 ********************************************************************************
20 *
21 * NOTE: DOXYGEN is used to generate documentation for this file.
22 *
23 *******************************************************************************/
24 
25 /** @file endian_swap.c
26  * Endian Swapping Functions.
27  */
28 
29 #include "armutils.h"
30 
31 /*******************************************************************************
32  * Private Functions
33  */
34 /** @privatesection */
35 
36 /*******************************************************************************
37  * Public Functions
38  */
39 /** @publicsection */
40 
41 /**
42  * Convert array of 16 bit big endian values to native byte order.
43  *
44  * @param data - pointer to the array of data values
45  * @param nvals - number of values in the data array
46  *
47  * @return pointer to the array of data values
48  */
49 void *bton_16(void *data, size_t nvals)
50 {
51 #ifndef _BIG_ENDIAN
52  uint16_t *dp = (uint16_t *)data;
53  size_t i;
54 
55  for (i = 0; i < nvals; ++i) {
56  dp[i] = SWAP_BYTES_16(dp[i]);
57  }
58 
59 #endif
60  return(data);
61 }
62 
63 /**
64  * Convert array of 32 bit big endian values to native byte order.
65  *
66  * @param data - pointer to the array of data values
67  * @param nvals - number of values in the data array
68  *
69  * @return pointer to the array of data values
70  */
71 void *bton_32(void *data, size_t nvals)
72 {
73 #ifndef _BIG_ENDIAN
74  uint32_t *dp = (uint32_t *)data;
75  size_t i;
76 
77  for (i = 0; i < nvals; ++i) {
78  dp[i] = SWAP_BYTES_32(dp[i]);
79  }
80 
81 #endif
82  return(data);
83 }
84 
85 /**
86  * Convert array of 64 bit big endian values to native byte order.
87  *
88  * @param data - pointer to the array of data values
89  * @param nvals - number of values in the data array
90  *
91  * @return pointer to the array of data values
92  */
93 void *bton_64(void *data, size_t nvals)
94 {
95 #ifndef _BIG_ENDIAN
96  uint64_t *dp = (uint64_t *)data;
97  size_t i;
98 
99  for (i = 0; i < nvals; ++i) {
100  dp[i] = SWAP_BYTES_64(dp[i]);
101  }
102 
103 #endif
104  return(data);
105 }
106 
107 /**
108  * Convert array of 16 bit little endian values to native byte order.
109  *
110  * @param data - pointer to the array of data values
111  * @param nvals - number of values in the data array
112  *
113  * @return pointer to the array of data values
114  */
115 void *lton_16(void *data, size_t nvals)
116 {
117 #ifdef _BIG_ENDIAN
118  uint16_t *dp = (uint16_t *)data;
119  size_t i;
120 
121  for (i = 0; i < nvals; ++i) {
122  dp[i] = SWAP_BYTES_16(dp[i]);
123  }
124 
125 #endif
126  return(data);
127 }
128 
129 /**
130  * Convert array of 32 bit little endian values to native byte order.
131  *
132  * @param data - pointer to the array of data values
133  * @param nvals - number of values in the data array
134  *
135  * @return pointer to the array of data values
136  */
137 void *lton_32(void *data, size_t nvals)
138 {
139 #ifdef _BIG_ENDIAN
140  uint32_t *dp = (uint32_t *)data;
141  size_t i;
142 
143  for (i = 0; i < nvals; ++i) {
144  dp[i] = SWAP_BYTES_32(dp[i]);
145  }
146 
147 #endif
148  return(data);
149 }
150 
151 /**
152  * Convert array of 64 bit little endian values to native byte order.
153  *
154  * @param data - pointer to the array of data values
155  * @param nvals - number of values in the data array
156  *
157  * @return pointer to the array of data values
158  */
159 void *lton_64(void *data, size_t nvals)
160 {
161 #ifdef _BIG_ENDIAN
162  uint64_t *dp = (uint64_t *)data;
163  size_t i;
164 
165  for (i = 0; i < nvals; ++i) {
166  dp[i] = SWAP_BYTES_64(dp[i]);
167  }
168 
169 #endif
170  return(data);
171 }
172 
173 /**
174  * Read 16 bit values from a big endian binary file.
175  *
176  * This function will read 16 bit values from a big endian binary file
177  * and convert them to the native byte order.
178  *
179  * Error messages from this function are sent to the message handler
180  * (see msngr_init_log() and msngr_init_mail()).
181  *
182  * @param fd - file descriptor
183  * @param data - pointer to the output data array
184  * @param nvals - number of data values to read
185  *
186  * @return
187  * - number of data values successfully read
188  * - -1 if an error occurred
189  */
190 int bton_read_16(int fd, void *data, size_t nvals)
191 {
192  ssize_t bytes_read;
193  int vals_read;
194 
195  bytes_read = read(fd, data, 2*nvals);
196  if (bytes_read < 0) {
197 
199  "Could not read data from file: %s\n", strerror(errno));
200 
201  return(-1);
202  }
203 
204  vals_read = bytes_read/2;
205 
206 #ifndef _BIG_ENDIAN
207  {
208  uint16_t *dp = (uint16_t *)data;
209  int i;
210 
211  for (i = 0; i < vals_read; ++i) {
212  dp[i] = SWAP_BYTES_16(dp[i]);
213  }
214  }
215 #endif
216 
217  return(vals_read);
218 }
219 
220 /**
221  * Read 32 bit values from a big endian binary file.
222  *
223  * This function will read 32 bit values from a big endian binary file
224  * and convert them to the native byte order.
225  *
226  * Error messages from this function are sent to the message handler
227  * (see msngr_init_log() and msngr_init_mail()).
228  *
229  * @param fd - file descriptor
230  * @param data - pointer to the output data array
231  * @param nvals - number of data values to read
232  *
233  * @return
234  * - number of data values successfully read
235  * - -1 if an error occurred
236  */
237 int bton_read_32(int fd, void *data, size_t nvals)
238 {
239  ssize_t bytes_read;
240  int vals_read;
241 
242  bytes_read = read(fd, data, 4*nvals);
243  if (bytes_read < 0) {
244 
246  "Could not read data from file: %s\n", strerror(errno));
247 
248  return(-1);
249  }
250 
251  vals_read = bytes_read/4;
252 
253 #ifndef _BIG_ENDIAN
254  {
255  uint32_t *dp = (uint32_t *)data;
256  int i;
257 
258  for (i = 0; i < vals_read; ++i) {
259  dp[i] = SWAP_BYTES_32(dp[i]);
260  }
261  }
262 #endif
263 
264  return(vals_read);
265 }
266 
267 /**
268  * Read 64 bit values from a big endian binary file.
269  *
270  * This function will read 64 bit values from a big endian binary file
271  * and convert them to the native byte order.
272  *
273  * Error messages from this function are sent to the message handler
274  * (see msngr_init_log() and msngr_init_mail()).
275  *
276  * @param fd - file descriptor
277  * @param data - pointer to the output data array
278  * @param nvals - number of data values to read
279  *
280  * @return
281  * - number of data values successfully read
282  * - -1 if an error occurred
283  */
284 int bton_read_64(int fd, void *data, size_t nvals)
285 {
286  ssize_t bytes_read;
287  int vals_read;
288 
289  bytes_read = read(fd, data, 8*nvals);
290  if (bytes_read < 0) {
291 
293  "Could not read data from file: %s\n", strerror(errno));
294 
295  return(-1);
296  }
297 
298  vals_read = bytes_read/8;
299 
300 #ifndef _BIG_ENDIAN
301  {
302  uint64_t *dp = (uint64_t *)data;
303  int i;
304 
305  for (i = 0; i < vals_read; ++i) {
306  dp[i] = SWAP_BYTES_64(dp[i]);
307  }
308  }
309 #endif
310 
311  return(vals_read);
312 }
313 
314 /**
315  * Read 16 bit values from a little endian binary file.
316  *
317  * This function will read 16 bit values from a little endian binary file
318  * and convert them to the native byte order.
319  *
320  * Error messages from this function are sent to the message handler
321  * (see msngr_init_log() and msngr_init_mail()).
322  *
323  * @param fd - file descriptor
324  * @param data - pointer to the output data array
325  * @param nvals - number of data values to read
326  *
327  * @return
328  * - number of data values successfully read
329  * - -1 if an error occurred
330  */
331 int lton_read_16(int fd, void *data, size_t nvals)
332 {
333  ssize_t bytes_read;
334  int vals_read;
335 
336  bytes_read = read(fd, data, 2*nvals);
337  if (bytes_read < 0) {
338 
340  "Could not read data from file: %s\n", strerror(errno));
341 
342  return(-1);
343  }
344 
345  vals_read = bytes_read/2;
346 
347 #ifdef _BIG_ENDIAN
348  {
349  uint16_t *dp = (uint16_t *)data;
350  int i;
351 
352  for (i = 0; i < vals_read; ++i) {
353  dp[i] = SWAP_BYTES_16(dp[i]);
354  }
355  }
356 #endif
357 
358  return(vals_read);
359 }
360 
361 /**
362  * Read 32 bit values from a little endian binary file.
363  *
364  * This function will read 32 bit values from a little endian binary file
365  * and convert them to the native byte order.
366  *
367  * Error messages from this function are sent to the message handler
368  * (see msngr_init_log() and msngr_init_mail()).
369  *
370  * @param fd - file descriptor
371  * @param data - pointer to the output data array
372  * @param nvals - number of data values to read
373  *
374  * @return
375  * - number of data values successfully read
376  * - -1 if an error occurred
377  */
378 int lton_read_32(int fd, void *data, size_t nvals)
379 {
380  ssize_t bytes_read;
381  int vals_read;
382 
383  bytes_read = read(fd, data, 4*nvals);
384  if (bytes_read < 0) {
385 
387  "Could not read data from file: %s\n", strerror(errno));
388 
389  return(-1);
390  }
391 
392  vals_read = bytes_read/4;
393 
394 #ifdef _BIG_ENDIAN
395  {
396  uint32_t *dp = (uint32_t *)data;
397  int i;
398 
399  for (i = 0; i < vals_read; ++i) {
400  dp[i] = SWAP_BYTES_32(dp[i]);
401  }
402  }
403 #endif
404 
405  return(vals_read);
406 }
407 
408 /**
409  * Read 64 bit values from a little endian binary file.
410  *
411  * This function will read 64 bit values from a little endian binary file
412  * and convert them to the native byte order.
413  *
414  * Error messages from this function are sent to the message handler
415  * (see msngr_init_log() and msngr_init_mail()).
416  *
417  * @param fd - file descriptor
418  * @param data - pointer to the output data array
419  * @param nvals - number of data values to read
420  *
421  * @return
422  * - number of data values successfully read
423  * - -1 if an error occurred
424  */
425 int lton_read_64(int fd, void *data, size_t nvals)
426 {
427  ssize_t bytes_read;
428  int vals_read;
429 
430  bytes_read = read(fd, data, 8*nvals);
431  if (bytes_read < 0) {
432 
434  "Could not read data from file: %s\n", strerror(errno));
435 
436  return(-1);
437  }
438 
439  vals_read = bytes_read/8;
440 
441 #ifdef _BIG_ENDIAN
442  {
443  uint64_t *dp = (uint64_t *)data;
444  int i;
445 
446  for (i = 0; i < vals_read; ++i) {
447  dp[i] = SWAP_BYTES_64(dp[i]);
448  }
449  }
450 #endif
451 
452  return(vals_read);
453 }