• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009, 2010
3  * Phillip Lougher <phillip@squashfs.org.uk>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2,
8  * or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * swap.c
20  */
21 
22 #ifndef linux
23 #define __BYTE_ORDER BYTE_ORDER
24 #define __BIG_ENDIAN BIG_ENDIAN
25 #define __LITTLE_ENDIAN LITTLE_ENDIAN
26 #else
27 #include <endian.h>
28 #endif
29 
30 #if __BYTE_ORDER == __BIG_ENDIAN
swap_le16(void * src,void * dest)31 void swap_le16(void *src, void *dest)
32 {
33 	unsigned char *s = src;
34 	unsigned char *d = dest;
35 
36 	d[0] = s[1];
37 	d[1] = s[0];
38 }
39 
40 
swap_le32(void * src,void * dest)41 void swap_le32(void *src, void *dest)
42 {
43 	unsigned char *s = src;
44 	unsigned char *d = dest;
45 
46 	d[0] = s[3];
47 	d[1] = s[2];
48 	d[2] = s[1];
49 	d[3] = s[0];
50 }
51 
52 
swap_le64(void * src,void * dest)53 void swap_le64(void *src, void *dest)
54 {
55 	unsigned char *s = src;
56 	unsigned char *d = dest;
57 
58 	d[0] = s[7];
59 	d[1] = s[6];
60 	d[2] = s[5];
61 	d[3] = s[4];
62 	d[4] = s[3];
63 	d[5] = s[2];
64 	d[6] = s[1];
65 	d[7] = s[0];
66 }
67 
68 
inswap_le16(unsigned short num)69 unsigned short inswap_le16(unsigned short num)
70 {
71 	return (num >> 8) |
72 		((num & 0xff) << 8);
73 }
74 
75 
inswap_le32(unsigned int num)76 unsigned int inswap_le32(unsigned int num)
77 {
78 	return (num >> 24) |
79 		((num & 0xff0000) >> 8) |
80 		((num & 0xff00) << 8) |
81 		((num & 0xff) << 24);
82 }
83 
84 
inswap_le64(long long n)85 long long inswap_le64(long long n)
86 {
87 	unsigned long long num = n;
88 
89 	return (num >> 56) |
90 		((num & 0xff000000000000LL) >> 40) |
91 		((num & 0xff0000000000LL) >> 24) |
92 		((num & 0xff00000000LL) >> 8) |
93 		((num & 0xff000000) << 8) |
94 		((num & 0xff0000) << 24) |
95 		((num & 0xff00) << 40) |
96 		((num & 0xff) << 56);
97 }
98 
99 
100 #define SWAP_LE_NUM(BITS) \
101 void swap_le##BITS##_num(void *s, void *d, int n) \
102 {\
103 	int i;\
104 	for(i = 0; i < n; i++, s += BITS / 8, d += BITS / 8)\
105 		swap_le##BITS(s, d);\
106 }
107 
108 SWAP_LE_NUM(16)
109 SWAP_LE_NUM(32)
110 SWAP_LE_NUM(64)
111 
112 #define INSWAP_LE_NUM(BITS, TYPE) \
113 void inswap_le##BITS##_num(TYPE *s, int n) \
114 {\
115 	int i;\
116 	for(i = 0; i < n; i++)\
117 		s[i] = inswap_le##BITS(s[i]);\
118 }
119 
120 INSWAP_LE_NUM(16, unsigned short)
121 INSWAP_LE_NUM(32, unsigned int)
122 INSWAP_LE_NUM(64, long long)
123 #endif
124