]>
Commit | Line | Data |
---|---|---|
fbaec83b | 1 | /* |
9e95aa12 KZ |
2 | * SPDX-License-Identifier: GPL-2.0-or-later |
3 | * | |
fbaec83b SRP |
4 | * cramfs_common - cramfs common code |
5 | * | |
6 | * Copyright (c) 2008 Roy Peled, the.roy.peled -at- gmail.com | |
cd42d02f | 7 | * Copyright (c) 2004-2006 by Juliane Holzt, kju -at- fqdn.org |
fbaec83b SRP |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program 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 | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | */ | |
20 | ||
21 | #include <string.h> | |
06f25585 | 22 | #include "cramfs.h" |
fbaec83b SRP |
23 | #include "../include/bitops.h" |
24 | ||
d51f37a3 | 25 | uint32_t u32_toggle_endianness(int big_endian, uint32_t what) |
fbaec83b SRP |
26 | { |
27 | return big_endian == HOST_IS_BIG_ENDIAN ? what : swab32(what); | |
28 | } | |
29 | ||
30 | void super_toggle_endianness(int big_endian, struct cramfs_super *super) | |
31 | { | |
32 | if (big_endian != HOST_IS_BIG_ENDIAN) { | |
33 | super->magic = swab32(super->magic); | |
34 | super->size = swab32(super->size); | |
35 | super->flags = swab32(super->flags); | |
36 | super->future = swab32(super->future); | |
37 | super->fsid.crc = swab32(super->fsid.crc); | |
38 | super->fsid.edition = swab32(super->fsid.edition); | |
39 | super->fsid.blocks = swab32(super->fsid.blocks); | |
fc2798c5 | 40 | super->fsid.files = swab32(super->fsid.files); |
fbaec83b SRP |
41 | } |
42 | } | |
43 | ||
cc924cc0 | 44 | static void inode_toggle_endianness(int input_big_endian, int output_big_endian, |
fc2798c5 SK |
45 | struct cramfs_inode *inode_in, |
46 | struct cramfs_inode *inode_out) | |
fbaec83b SRP |
47 | { |
48 | if (input_big_endian == output_big_endian) { | |
49 | memmove(inode_out, inode_in, sizeof(*inode_out)); | |
fc2798c5 | 50 | } else { |
fbaec83b SRP |
51 | unsigned char inode_out_buf[sizeof(*inode_in)]; |
52 | unsigned char *inode_in_buf = (unsigned char*)inode_in; | |
53 | ||
54 | inode_out_buf[0] = inode_in_buf[1]; /* 16 bit: mode */ | |
55 | inode_out_buf[1] = inode_in_buf[0]; | |
56 | ||
57 | inode_out_buf[2] = inode_in_buf[3]; /* 16 bit: uid */ | |
fc2798c5 | 58 | inode_out_buf[3] = inode_in_buf[2]; |
fbaec83b SRP |
59 | |
60 | inode_out_buf[4] = inode_in_buf[6]; /* 24 bit: size */ | |
61 | inode_out_buf[5] = inode_in_buf[5]; | |
62 | inode_out_buf[6] = inode_in_buf[4]; | |
63 | ||
64 | inode_out_buf[7] = inode_in_buf[7]; /* 8 bit: gid width */ | |
65 | ||
fc2798c5 SK |
66 | /* |
67 | * Stop the madness! Outlaw C bitfields! They are unportable | |
68 | * and nasty! See for yourself what a mess this is: | |
69 | */ | |
fbaec83b | 70 | if (output_big_endian) { |
fc2798c5 | 71 | inode_out_buf[ 8] = ( (inode_in_buf[ 8]&0x3F) << 2 ) | |
fbaec83b SRP |
72 | ( (inode_in_buf[11]&0xC0) >> 6 ); |
73 | ||
74 | inode_out_buf[ 9] = ( (inode_in_buf[11]&0x3F) << 2 ) | | |
75 | ( (inode_in_buf[10]&0xC0) >> 6 ); | |
76 | ||
77 | inode_out_buf[10] = ( (inode_in_buf[10]&0x3F) << 2 ) | | |
78 | ( (inode_in_buf[ 9]&0xC0) >> 6 ); | |
79 | ||
80 | inode_out_buf[11] = ( (inode_in_buf[ 9]&0x3F) << 2 ) | | |
81 | ( (inode_in_buf[ 8]&0xC0) >> 6 ); | |
fc2798c5 SK |
82 | } else { |
83 | inode_out_buf[ 8] = ( (inode_in_buf[ 8]&0xFD) >> 2 ) | | |
fbaec83b SRP |
84 | ( (inode_in_buf[11]&0x03) << 6 ); |
85 | ||
fc2798c5 | 86 | inode_out_buf[ 9] = ( (inode_in_buf[11]&0xFD) >> 2 ) | |
fbaec83b SRP |
87 | ( (inode_in_buf[10]&0x03) << 6 ); |
88 | ||
fc2798c5 | 89 | inode_out_buf[10] = ( (inode_in_buf[10]&0xFD) >> 2 ) | |
fbaec83b SRP |
90 | ( (inode_in_buf[ 9]&0x03) << 6 ); |
91 | ||
fc2798c5 | 92 | inode_out_buf[11] = ( (inode_in_buf[ 9]&0xFD) >> 2 ) | |
fbaec83b SRP |
93 | ( (inode_in_buf[ 8]&0x03) << 6 ); |
94 | } | |
fbaec83b SRP |
95 | memmove(inode_out, inode_out_buf, sizeof(*inode_out)); |
96 | } | |
97 | } | |
98 | ||
fc2798c5 SK |
99 | void inode_to_host(int from_big_endian, struct cramfs_inode *inode_in, |
100 | struct cramfs_inode *inode_out) | |
fbaec83b | 101 | { |
fc2798c5 SK |
102 | inode_toggle_endianness(from_big_endian, HOST_IS_BIG_ENDIAN, inode_in, |
103 | inode_out); | |
fbaec83b SRP |
104 | } |
105 | ||
fc2798c5 SK |
106 | void inode_from_host(int to_big_endian, struct cramfs_inode *inode_in, |
107 | struct cramfs_inode *inode_out) | |
fbaec83b | 108 | { |
fc2798c5 SK |
109 | inode_toggle_endianness(HOST_IS_BIG_ENDIAN, to_big_endian, inode_in, |
110 | inode_out); | |
fbaec83b | 111 | } |