summaryrefslogtreecommitdiffstats
path: root/squashfs-tools/swap.c
blob: eb0f32607381867951a4ff8aa7f3bba61db01c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2009, 2010, 2021
 * Phillip Lougher <phillip@squashfs.org.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * swap.c
 */

#include "endian_compat.h"

#if __BYTE_ORDER == __BIG_ENDIAN
void swap_le16(void *src, void *dest)
{
	unsigned char *s = src;
	unsigned char *d = dest;

	d[0] = s[1];
	d[1] = s[0];
}


void swap_le32(void *src, void *dest)
{
	unsigned char *s = src;
	unsigned char *d = dest;

	d[0] = s[3];
	d[1] = s[2];
	d[2] = s[1];
	d[3] = s[0];
}


void swap_le64(void *src, void *dest)
{
	unsigned char *s = src;
	unsigned char *d = dest;

	d[0] = s[7];
	d[1] = s[6];
	d[2] = s[5];
	d[3] = s[4];
	d[4] = s[3];
	d[5] = s[2];
	d[6] = s[1];
	d[7] = s[0];
}


unsigned short inswap_le16(unsigned short num)
{
	return (num >> 8) |
		((num & 0xff) << 8);
}


unsigned int inswap_le32(unsigned int num)
{
	return (num >> 24) |
		((num & 0xff0000) >> 8) |
		((num & 0xff00) << 8) |
		((num & 0xff) << 24);
}


long long inswap_le64(long long n)
{
	unsigned long long num = n;

	return (num >> 56) |
		((num & 0xff000000000000LL) >> 40) |
		((num & 0xff0000000000LL) >> 24) |
		((num & 0xff00000000LL) >> 8) |
		((num & 0xff000000) << 8) |
		((num & 0xff0000) << 24) |
		((num & 0xff00) << 40) |
		((num & 0xff) << 56);
}


#define SWAP_LE_NUM(BITS) \
void swap_le##BITS##_num(void *s, void *d, int n) \
{\
	int i;\
	for(i = 0; i < n; i++, s += BITS / 8, d += BITS / 8)\
		swap_le##BITS(s, d);\
}

SWAP_LE_NUM(16)
SWAP_LE_NUM(32)
SWAP_LE_NUM(64)

#define INSWAP_LE_NUM(BITS, TYPE) \
void inswap_le##BITS##_num(TYPE *s, int n) \
{\
	int i;\
	for(i = 0; i < n; i++)\
		s[i] = inswap_le##BITS(s[i]);\
}

INSWAP_LE_NUM(16, unsigned short)
INSWAP_LE_NUM(32, unsigned int)
INSWAP_LE_NUM(64, long long)
#endif