source: icGREP/icgrep-devel/QA/lz4d_ext_dep/decoder/FrameDecoder.py @ 5908

Last change on this file since 5908 was 5864, checked in by xwa163, 21 months ago

Add LZ4D extract deposit related kernel, target and test cases

File size: 1.3 KB
Line 
1
2# ---------------
3# Frame Format:
4# Magic Number -- 4 bytes
5# Frame Descriptor -- 3-11 bytes
6# Data Blocks
7# EndMark  -- 4 bytes
8# C. Checksum -- 0-4 bytes
9
10
11
12minFilesize = (
13    4 +         # Magic number
14    3 +         # Frame descriptor (3-11 bytes)
15    4          # End mark
16)
17
18# Little-endian.
19def decode(file):
20    result = {}
21    __check_magic_number(file)
22    return __decodeFrameDescriptor(file)
23
24MagicNumber = 0x184D2204
25
26
27def __check_magic_number(file):
28    res = 0
29    for i in range(4):
30        res = (res << 8) + file[3 - i]  # Little-endian.
31    if res != MagicNumber:
32        raise Exception('Invalid LZ4 File')
33
34
35def __decodeFrameDescriptor(file):
36
37    descriptor_offset_base = 4
38    FLG_byte = file[descriptor_offset_base]
39    has_content_checksum = (FLG_byte >> 2) & 1
40    has_content_size = (FLG_byte >> 3) & 1
41    has_block_checksum = (FLG_byte >> 4) & 1
42
43    version_number = FLG_byte >> 6
44    if version_number != 1:
45        raise Exception('Unsupport Version Number')
46    if has_content_size:
47        block_start = 15
48    else:
49        block_start = 7
50
51    return {
52        "has_content_checksum": has_content_checksum,
53        "has_content_size": has_content_size,
54        "has_block_checksum": has_block_checksum,
55        "block_start": block_start
56    }
Note: See TracBrowser for help on using the repository browser.