-
Notifications
You must be signed in to change notification settings - Fork 89
feat(handler): add partclone handler #1155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import binascii | ||
import io | ||
from math import ceil | ||
from typing import Optional | ||
|
||
from unblob.extractors import Command | ||
from unblob.file_utils import File, InvalidInputFormat, get_endian_short | ||
from unblob.models import Regex, StructHandler, ValidChunk | ||
|
||
C_DEFINITIONS = r""" | ||
typedef struct partclone_header{ | ||
char magic[16]; | ||
char partclone_version[14]; | ||
char image_version_txt[4]; | ||
char endian[2]; | ||
char fs_type[16]; | ||
uint64 fs_size; | ||
uint64 fs_total_block_count; | ||
uint64 fs_used_block_count_superblock; | ||
uint64 fs_used_block_count_bitmap; | ||
uint32 fs_block_size; | ||
uint32 feature_size; | ||
uint16 image_version; | ||
uint16 number_of_bits_for_CPU; | ||
uint16 checksum_mode; | ||
uint16 checksum_size; | ||
uint32 blocks_per_checksum; | ||
uint8 reseed_checksum; | ||
uint8 bitmap_mode; | ||
uint32 crc32; | ||
} partclone_header_t; | ||
""" | ||
|
||
HEADER_STRUCT = "partclone_header_t" | ||
BIG_ENDIAN_MAGIC = 0xC0DE | ||
ENDIAN_OFFSET = 34 | ||
|
||
|
||
class PartcloneHandler(StructHandler): | ||
NAME = "partclone" | ||
PATTERNS = [Regex(r"partclone-image\x00\d+\.\d+\.\d+.*?0002(\xde\xc0|\xc0\xde)")] | ||
HEADER_STRUCT = HEADER_STRUCT | ||
C_DEFINITIONS = C_DEFINITIONS | ||
EXTRACTOR = Command( | ||
"partclone.restore", | ||
"-W", | ||
"-s", | ||
"{inpath}", | ||
"-o", | ||
"{outdir}/partclone.restored", | ||
"-L", | ||
"/dev/stdout", | ||
) | ||
|
||
def is_valid_header(self, header) -> bool: | ||
calculated_crc = binascii.crc32(header.dumps()[0:-4]) | ||
return ( | ||
header.crc32 ^ 0xFFFFFFFF | ||
) == calculated_crc # partclone does not final XOR | ||
|
||
def calculate_chunk(self, file: File, start_offset: int) -> Optional[ValidChunk]: | ||
file.seek(start_offset + ENDIAN_OFFSET, io.SEEK_SET) # go to endian | ||
endian = get_endian_short(file, BIG_ENDIAN_MAGIC) | ||
file.seek(start_offset, io.SEEK_SET) # go to beginning of file | ||
header = self.parse_header(file, endian) | ||
|
||
if not self.is_valid_header(header): | ||
raise InvalidInputFormat("Invalid partclone header.") | ||
|
||
end_offset = start_offset + len(header) # header | ||
end_offset += header.checksum_size # checksum size | ||
end_offset += ceil(header.fs_total_block_count / 8) # bitmap, as bytes | ||
|
||
if header.checksum_mode != 0: | ||
checksum_blocks = ceil( | ||
header.fs_used_block_count_bitmap / header.blocks_per_checksum | ||
) | ||
end_offset += checksum_blocks * header.checksum_size | ||
|
||
end_offset += header.fs_used_block_count_bitmap * header.fs_block_size # Data | ||
return ValidChunk(start_offset=start_offset, end_offset=end_offset) |
3 changes: 3 additions & 0 deletions
3
tests/integration/archive/partclone/__input__/floppy-144m.img
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/archive/partclone/__input__/fs_dev0.partclone.img
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/archive/partclone/__output__/floppy-144m.img_extract/partclone.restored
Git LFS file not shown
Empty file.
3 changes: 3 additions & 0 deletions
3
...integration/archive/partclone/__output__/fs_dev0.partclone.img_extract/partclone.restored
Git LFS file not shown
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.