-
Notifications
You must be signed in to change notification settings - Fork 4
/
fjm_consts.py
66 lines (47 loc) · 1.87 KB
/
fjm_consts.py
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
import lzma
from enum import Enum
from typing import List, Dict
"""
struct {
u16 fj_magic; // 'F' + 'J'<<8 (0x4a46)
u16 word_size; // number of bits in the memory / a memory-word. also called "w".
u64 version;
u64 segment_num;
{ // for versions > 0
u64 flags;
u32 reserved; // 0
}
struct segment {
u64 segment_start; // in memory words (w-bits)
u64 segment_length; // in memory words (w-bits)
u64 data_start; // in the outer-struct.data words (w-bits)
u64 data_length; // in the outer-struct.data words (w-bits)
} *segments; // segments[segment_num]
u8* data; // the data (might be compressed in some versions)
} fjm_file; // Flip-Jump Memory file
"""
FJ_MAGIC = ord('F') + (ord('J') << 8)
_reserved_dict_threshold = 1000
_header_base_format = '<HHQQ'
_header_base_size = 2 + 2 + 8 + 8
_header_extension_format = '<QL'
_header_extension_size = 8 + 4
_segment_format = '<QQQQ'
_segment_size = 8 + 8 + 8 + 8
class FJMVersion(Enum):
BaseVersion = 0 # initial version, minimal structure
NormalVersion = 1 # added flags and reserved
RelativeJumpVersion = 2 # compress-friendly: jumps in data are saved relative to their address
CompressedVersion = 3 # version 2 but data is lzma2-compressed
SUPPORTED_VERSIONS_NAMES = {
FJMVersion.BaseVersion: 'Base',
FJMVersion.NormalVersion: 'Normal',
FJMVersion.RelativeJumpVersion: 'RelativeJump',
FJMVersion.CompressedVersion: 'Compressed',
}
_LZMA_FORMAT = lzma.FORMAT_RAW
_LZMA_DECOMPRESSION_FILTERS: List[Dict[str, int]] = [{"id": lzma.FILTER_LZMA2}]
def _lzma_compression_filters(dw: int, preset: int) -> List[Dict[str, int]]:
return [{"id": lzma.FILTER_LZMA2, "preset": preset, "nice_len": dw}]
def _new_garbage_val() -> int:
return 0 # The value read when reading a word outside any segment.