Skip to content

Commit

Permalink
[Update] Fix ROM Unpack
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdWindScholar committed Feb 2, 2024
1 parent 07b4d04 commit cd19f04
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
45 changes: 30 additions & 15 deletions ext4.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import queue


def wcscmp(str_a, str_b):
def wcs_cmp(str_a, str_b):
for a, b in zip(str_a, str_b):
tmp = ord(a) - ord(b)
if tmp != 0:
Expand Down Expand Up @@ -511,10 +511,33 @@ def block_size(self):
def get_block_count(self):
return self.superblock.s_blocks_count

@property
def get_info_list(self):
data = [
['Filesystem magic number', hex(self.superblock.s_magic).upper()],
["Filesystem volume name",self.superblock.s_volume_name.decode()],
["Filesystem UUID", self.uuid],
['Last mounted on', self.superblock.s_last_mounted.decode()],
["Block size", f"{1 << (10 + self.superblock.s_log_block_size)}"],
["Block count", self.superblock.s_blocks_count],
["Free inodes", self.superblock.s_free_inodes_count],
["Free blocks", self.superblock.s_free_blocks_count],
["Inodes per group", self.superblock.s_inodes_per_group],
['Blocks per group', self.superblock.s_blocks_per_group],
['Inode count', self.superblock.s_inodes_count],
['Reserved GDT blocks', self.superblock.s_reserved_gdt_blocks],
["Inode size", self.superblock.s_inode_size],
['Filesystem created', self.superblock.s_mkfs_time],
["Currect Size", self.get_block_count * self.block_size]
]
return data

def get_inode(self, inode_idx, file_type=InodeType.UNKNOWN):
group_idx, inode_table_entry_idx = self.get_inode_group(inode_idx)

inode_table_offset = self.group_descriptors[group_idx].bg_inode_table * self.block_size
try:
inode_table_offset = self.group_descriptors[group_idx].bg_inode_table * self.block_size
except Exception:
inode_table_offset = 99 * self.block_size
inode_offset = inode_table_offset + inode_table_entry_idx * self.superblock.s_inode_size

return Inode(self, inode_offset, inode_idx, file_type)
Expand Down Expand Up @@ -563,12 +586,7 @@ def __len__(self):

def __repr__(self):
if self.inode_idx is not None:
return "{type_name:s}(inode_idx = {inode!r:s}, offset = 0x{offset:X}, volume_uuid = {uuid!r:s})".format(
inode=self.inode_idx,
offset=self.offset,
type_name=type(self).__name__,
uuid=self.volume.uuid
)
return f"{type(self).__name__:s}(inode_idx = {self.inode_idx!r:s}, offset = 0x{self.offset:X}, volume_uuid = {self.volume.uuid!r:s})"
else:
return f"{type(self).__name__:s}(offset = 0x{self.offset:X}, volume_uuid = {self.volume.uuid!r:s})"

Expand Down Expand Up @@ -625,8 +643,8 @@ def directory_entry_comparator(dir_a, dir_b):
file_name_b, _, file_type_b = dir_b

if file_type_a == InodeType.DIRECTORY == file_type_b or file_type_a != InodeType.DIRECTORY != file_type_b:
tmp = wcscmp(file_name_a.lower(), file_name_b.lower())
return tmp if tmp != 0 else wcscmp(file_name_a, file_name_b)
tmp = wcs_cmp(file_name_a.lower(), file_name_b.lower())
return tmp if tmp != 0 else wcs_cmp(file_name_a, file_name_b)
else:
return -1 if file_type_a == InodeType.DIRECTORY else 1

Expand Down Expand Up @@ -805,10 +823,7 @@ def size_readable(self):
units = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
unit_idx = min(int(math.log(self.inode.i_size, 1024)), len(units))

return "{size:.2f} {unit:s}".format(
size=self.inode.i_size / (1024 ** unit_idx),
unit=units[unit_idx - 1]
)
return f"{self.inode.i_size / (1024 ** unit_idx):.2f} {units[unit_idx - 1]:s}"

def xattrs(self, check_inline=True, check_block=True, force_inline=False):
# Inline xattrs
Expand Down
8 changes: 3 additions & 5 deletions imgextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def scan_dir(root_inode, root_path=""):
link_target = root_inode.volume.read(link_target_block * root_inode.volume.block_size,
entry_inode.inode.i_size).decode("utf8")
if tmp_path.find(' ', 1, len(tmp_path)) > 0:
self.__append(tmp_path, os.path.join(self.CONFING_DIR, 'config', self.FileName + '_space.txt'))
self.__append(tmp_path, os.path.join(self.CONFING_DIR, self.FileName + '_space.txt'))
self.fs_config.append(
f"{tmp_path.replace(' ', '_')} {uid} {gid} {mode}{cap} {link_target}")
else:
Expand All @@ -137,8 +137,6 @@ def scan_dir(root_inode, root_path=""):
scan_dir(entry_inode, entry_inode_path)
elif entry_inode.is_file:
file_target = self.EXTRACT_DIR + entry_inode_path.replace(' ', '_').replace('"', '')
if os.name == 'nt':
file_target = file_target.replace('\\', '/')
try:
with open(file_target, 'wb') as out:
out.write(entry_inode.open_read().read())
Expand Down Expand Up @@ -243,14 +241,14 @@ def fix_moto(input_file):
os.remove(input_file)
os.rename(output_file, input_file)
finally:
...
pass

def fix_size(self):
orig_size = os.path.getsize(self.OUTPUT_IMAGE_FILE)
with open(self.OUTPUT_IMAGE_FILE, 'rb+') as file:
t = ext4.Volume(file)
real_size = t.get_block_count * t.block_size
if orig_size != real_size:
if orig_size < real_size:
print(f"......Wrong Size!Fixing.......\nShould:{real_size}\nYours:{orig_size}")
file.truncate(real_size)

Expand Down

0 comments on commit cd19f04

Please sign in to comment.