forked from WerWolv/ImHex-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zstd.hexpat
120 lines (100 loc) · 3.07 KB
/
zstd.hexpat
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma description Zstandard compressed data format
// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
#pragma MIME application/zstd
import std.core;
import std.io;
import std.mem;
import std.sys;
using BitfieldOrder = std::core::BitfieldOrder;
#define ZSTD_MAGIC_NUMBER 0xFD2FB528
bitfield frame_header_descriptor_t {
frame_content_size_flag : 2;
single_segment_flag : 1;
unused_bit : 1;
reserved_bit : 1;
content_checksum_flag : 1;
dictionary_id_flag : 2;
} [[bitfield_order(BitfieldOrder::MostToLeastSignificant, 8)]];
bitfield window_descriptor_t {
exponent : 5;
mantissa : 3;
} [[bitfield_order(BitfieldOrder::MostToLeastSignificant, 8)]];
fn window_size(window_descriptor_t window_descriptor) {
u64 window_log = 10 + window_descriptor.exponent;
u64 window_base = 1 << window_log;
u64 window_add = (window_base / 8) * window_descriptor.mantissa;
u64 window_size = window_base + window_add;
return window_size;
};
struct frame_header_t {
frame_header_descriptor_t frame_header_descriptor;
if (!frame_header_descriptor.single_segment_flag){
window_descriptor_t window_descriptor;
}
if (frame_header_descriptor.dictionary_id_flag == 1) {
le u8 dictionary_id;
}
else if (frame_header_descriptor.dictionary_id_flag == 2) {
le u16 dictionary_id;
}
else if (frame_header_descriptor.dictionary_id_flag == 3) {
le u32 dictionary_id;
}
if (frame_header_descriptor.frame_content_size_flag == 0) { // 0
if (frame_header_descriptor.single_segment_flag) {
le u8 content_size;
}
}
else if (frame_header_descriptor.frame_content_size_flag == 1) {
le u16 content_size;
}
else if (frame_header_descriptor.frame_content_size_flag == 2) {
le u32 content_size;
}
else {
le u64 content_size;
}
};
bitfield block_header_t {
last_block : 1;
block_type : 2;
block_size : 21;
};
enum block_type : u8 {
raw_block = 0,
rle_block = 1,
compressed_block = 2,
reserved = 3
};
struct data_block_t {
block_header_t block_header;
if (block_header.last_block) {
last_block_flag = true;
}
if (block_header.block_type == block_type::raw_block) {
le u8 block_content[block_header.block_size];
}
else if (block_header.block_type == block_type::rle_block) {
le u8 block_content[1];
}
else if (block_header.block_type == block_type::compressed_block) {
le u8 block_content[block_header.block_size];
}
else {
std::error("The data block seems to be corrupted!");
}
};
struct content_checksum_t {
le u32 xxh64_hash;
};
struct zstd_frame_t {
le u32 magic_number;
std::assert(magic_number == ZSTD_MAGIC_NUMBER, "Invalid magic number!");
frame_header_t frame_header;
data_block_t data_block[while(!last_block_flag)];
if (frame_header.frame_header_descriptor.content_checksum_flag) {
content_checksum_t content_checksum;
}
};
bool last_block_flag = false;
zstd_frame_t zstd_frame @ 0x00;