]> Pileus Git - ~andy/rsl/blob - endian.c
RSL v1.44
[~andy/rsl] / endian.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996, 1997
5             John H. Merritt
6             Space Applications Corporation
7             Vienna, Virginia
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU Library General Public
11     License as published by the Free Software Foundation; either
12     version 2 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     Library General Public License for more details.
18
19     You should have received a copy of the GNU Library General Public
20     License along with this library; if not, write to the Free
21     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 int big_endian(void);
24 int little_endian(void);
25 void swap_4_bytes(void *word);
26 void swap_2_bytes(void *word);
27
28 int big_endian(void)
29 {
30   union {
31     unsigned char byte[4];
32     int val;
33   } word;
34
35   word.val = 0;
36   word.byte[3] = 0x1;
37   return word.val == 1;
38 }
39
40 int little_endian(void)
41 {
42   union {
43     unsigned char byte[4];
44     int val;
45   } word;
46   word.val = 0;
47   word.byte[3] = 0x1;
48   return word.val != 1;
49 }
50
51
52 void swap_4_bytes(void *word)
53 {
54   unsigned char *byte;
55   unsigned char temp;
56   byte    = word;
57   temp    = byte[0];
58   byte[0] = byte[3];
59   byte[3] = temp;
60   temp    = byte[1];
61   byte[1] = byte[2];
62   byte[2] = temp;
63 }
64
65 void swap_2_bytes(void *word)
66 {
67   unsigned char *byte;
68   unsigned char temp;
69   byte    = word;
70   temp    = byte[0];
71   byte[0] = byte[1];
72   byte[1] = temp;
73 }