-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
rar.hexpat
361 lines (317 loc) · 9.92 KB
/
rar.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#pragma author targodan
#pragma description RAR archive file format
#pragma MIME application/x-rar
import type.magic;
import type.byte;
import std.mem;
import std.math;
import std.string;
import std.time;
// Source: https://www.rarlab.com/technote.htm
#define RARv5_MAGIC "Rar!\x1a\x07\x01\x00"
using FILETIME = u64 [[format("format_windows_time")]];
using time_t = std::time::EpochTime [[format("format_unix_time")]];
using nanotime_t = u64 [[format("format_unix_time")]];
fn format_unix_time(time_t t) {
return std::time::format(std::time::to_local(t), "%c");
};
fn format_windows_time(FILETIME t) {
return format_unix_time(std::time::filetime_to_unix(t));
};
struct vint {
u8 data[while(std::mem::read_unsigned($, 1) & 0x80 != 0)];
u8 last;
} [[sealed, transform("vint_value"), format("vint_value")]];
fn vint_value(vint vi) {
u64 value = 0;
u8 i = 0;
for(i = 0, i < sizeof(vi.data), i = i + 1) {
value |= (vi.data[i] & 0x7F) << (i * 7);
}
value |= vi.last << (i * 7);
return value;
};
bitfield LocatorRecordFlags {
bool quickOpenRecordOffsetIsPresent : 1;
bool recoveryRecordOffsetIsPresent : 1;
};
struct LocatorRecord {
LocatorRecordFlags flags; // should be vint but not compatible with bitfield
if (flags.quickOpenRecordOffsetIsPresent) {
vint quickOpenRecordOffset;
}
if (flags.recoveryRecordOffsetIsPresent) {
vint recoveryRecordOffset;
}
};
bitfield MetadataRecordFlags {
bool archiveNameIsPresent : 1;
bool archiveOriginalCreationTimeIsPresent : 1;
bool timeIsUnixTime : 1;
bool unixTimeIsNanosecondPrecision : 1;
};
struct MetadataRecord {
MetadataRecordFlags flags; // should be vint but not compatible with bitfield
if (flags.archiveNameIsPresent) {
vint nameLength;
char name[nameLength];
}
if (flags.archiveOriginalCreationTimeIsPresent) {
match (flags.timeIsUnixTime, flags.unixTimeIsNanosecondPrecision) {
(true, true): nanotime_t originalCreationTime;
(true, false): time_t originalCreationTime;
(false, _): FILETIME originalCreationTime;
}
}
};
enum MainHeaderExtraRecordType : vint {
Locator = 1,
Metadata = 2
};
struct MainHeaderExtraRecord {
vint size;
MainHeaderExtraRecordType type;
match(type) {
(MainHeaderExtraRecordType::Locator): LocatorRecord record [[inline]];
(MainHeaderExtraRecordType::Metadata): MetadataRecord record [[inline]];
}
};
bitfield HeaderFlags {
bool extraAreaIsPresent : 1;
bool dataAreaIsPresent : 1;
};
bitfield ArchiveFlags {
bool archiveIsPartOfMultiVolumeSet : 1;
bool volumeNumberFieldIsPresent : 1;
bool isSolidArchive : 1;
bool recoveryRecordIsPresent : 1;
bool isLockedArchive : 1;
};
enum HeaderType : vint {
MainHeader = 1,
FileHeader,
ServiceHeader,
EncryptionHeader,
EndOfArchive
};
struct MainArchiveHeader {
HeaderFlags flags; // should be vint but not compatible with bitfield
if (flags.extraAreaIsPresent) {
vint extraAreaSize;
}
ArchiveFlags archiveFlags; // should be vint but not compatible with bitfield
if (archiveFlags.volumeNumberFieldIsPresent) {
vint volumeNumber;
}
if (flags.extraAreaIsPresent) {
u64 extraEnd = $ + extraAreaSize;
MainHeaderExtraRecord extraArea[while($ < extraEnd)];
}
};
bitfield EncryptionHeaderFlags {
bool passwordCheckDataIsPresent : 1;
};
struct EncryptionHeader {
HeaderFlags flags; // should be vint but not compatible with bitfield
vint encryptionVersion; // only 0 (AES-256) is supported
EncryptionHeaderFlags encryptionFlags;
u8 kdfCount;
u8 salt[16];
if (encryptionFlags.passwordCheckDataIsPresent) {
u8 checkValue[12];
}
u8 iv[16];
// if this header is present, all following headers are encrypted => gobble up all data
u8 encryptedHeaders[while(!std::mem::eof())];
// With a bit of guess work, I figured out the exact crypto:
// key = PBKDF2(password, salt, keysize=256, iterations=2^kdfCount, hash=sha256)
// plainHeaders = AES256Decrypt(key, iv, mode=CBC)
};
bitfield FileAndServiceHeaderFlags {
bool isDirectory : 1;
bool timeFieldIsPresent : 1;
bool crc32FieldIsPresent : 1;
bool unpackedSizeIsUnknown : 1;
};
enum HostOS : vint {
Windows = 0,
Unix = 1
};
bitfield FileEncryptionFlags {
bool passwordCheckDataIsPresent : 1;
bool useTweakedChecksums : 1;
};
struct FileEncryptionRecord {
vint encryptionVersion; // only 0 (AES-256) is supported
FileEncryptionFlags flags; // should be vint but not compatible with bitfield
u8 kdfCount;
u8 salt[16];
u8 iv[16];
u8 checkValue[12];
};
struct FileHashRecord {
vint hashType; // only 0 (BLAKE2sp) hash is documented
u8 hashData[32]; // depends on hashType, but only one type is supported
};
bitfield FileTimeFlags {
bool timeIsStoredInUnixTime : 1;
bool mtimeIsPresent : 1;
bool ctimeIsPresent : 1;
bool atimeIsPresent : 1;
bool unixTimeIsNanosecondPrecision : 1;
};
struct FileTimeRecord {
FileTimeFlags flags; // should be vint but not compatible with bitfield
if (flags.mtimeIsPresent) {
match (flags.timeIsStoredInUnixTime, flags.unixTimeIsNanosecondPrecision) {
(true, true): nanotime_t mtime;
(true, false): time_t mtime;
(false, _): FILETIME mtime;
}
}
if (flags.ctimeIsPresent) {
match (flags.timeIsStoredInUnixTime, flags.unixTimeIsNanosecondPrecision) {
(true, true): nanotime_t ctime;
(true, false): time_t ctime;
(false, _): FILETIME ctime;
}
}
if (flags.atimeIsPresent) {
match (flags.timeIsStoredInUnixTime, flags.unixTimeIsNanosecondPrecision) {
(true, true): nanotime_t atime;
(true, false): time_t atime;
(false, _): FILETIME atime;
}
}
};
struct FileVersionRecord {
vint flags;
vint versionNumber;
};
enum RedirectionType : vint {
unixSymlink = 1,
windowsSymlink,
windowsJunktion,
hardLink,
fileCopy
};
struct RedirectionRecord {
RedirectionType type;
vint flags;
vint targetLength;
char target[targetLength];
};
bitfield UnixOwnerFlags {
bool userNameStringIsPresent : 1;
bool groupNameStringIsPresent : 1;
bool numericUserIDIsPresent : 1;
bool numericGroupIDIsPresent : 1;
};
struct UnixOwnerRecord {
UnixOwnerFlags flags; // should be vint but not compatible with bitfield
if (flags.userNameStringIsPresent) {
vint userNameLength;
char userName[usernameLength];
}
if (flags.groupNameStringIsPresent) {
vint groupNameLength;
char groupName[groupNameLength];
}
if (flags.numericUserIDIsPresent) {
vint uid;
}
if (flags.numericGroupIDIsPresent) {
vint gid;
}
};
enum FSHeaderExtraRecordType : vint {
FileEncryption = 1,
FileHash,
FileTime,
FileVersion,
Redirection,
UnixOwner,
ServiceData
};
struct FSHeaderExtraRecord {
vint size;
FSHeaderExtraRecordType type;
match(type) {
(FSHeaderExtraRecordType::FileEncryption): FileEncryptionRecord record [[inline]];
(FSHeaderExtraRecordType::FileHash): FileHashRecord record [[inline]];
(FSHeaderExtraRecordType::FileTime): FileTimeRecord record [[inline]];
(FSHeaderExtraRecordType::FileVersion): FileVersionRecord record [[inline]];
(FSHeaderExtraRecordType::Redirection): RedirectionRecord record [[inline]];
(FSHeaderExtraRecordType::UnixOwner): UnixOwnerRecord record [[inline]];
(FSHeaderExtraRecordType::ServiceData): u8 data[size - sizeof(type)]; // couldn't find any info on these records
}
};
bitfield _CompressionInformation {
version : 6;
bool isSolid : 1;
method : 3;
minDictionarySizeExponent : 5; // min dict size is `128 KB * 2^minDictionarySizeExponent`
additionalDictionarySize : 5;
bool compressionAlgorithmIsZeroButSizeIsV1 : 1;
};
struct CompressionInformation {
vint value; // TODO: I'd like to somehow cast the resulting value to _CompressionInformation
};
struct FileOrServiceHeader {
HeaderFlags flags; // should be vint but not compatible with bitfield
if (flags.extraAreaIsPresent) {
vint extraAreaSize;
}
if (flags.dataAreaIsPresent) {
vint dataAreaSize;
}
FileAndServiceHeaderFlags fileOrServiceFlags; // should be vint but not compatible with bitfield
vint unpackedSize;
vint fileAttributes;
if (fileOrServiceFlags.timeFieldIsPresent) {
u32 fileModificationUnixTime;
}
if (fileOrServiceFlags.crc32FieldIsPresent) {
u32 unpackedCrc32;
}
CompressionInformation compressionInformation;
try {
HostOS hostOS;
vint nameLength;
char name[nameLength];
if (flags.extraAreaIsPresent) {
u64 extraEnd = $ + extraAreaSize;
FSHeaderExtraRecord extraArea[while($ < extraEnd)];
}
if (flags.dataAreaIsPresent) {
u8 data[dataAreaSize] [[sealed]];
}
} catch {}
};
bitfield EOAHeaderFlags {
bool archiveIsVolumeAndNotLastInVolumeSet : 1;
};
struct EndOfArchiveHeader {
HeaderFlags flags;
EOAHeaderFlags eoaFlags;
};
struct Header {
u32 crc32;
vint headerSize;
HeaderType type;
match (type) {
(HeaderType::EncryptionHeader): EncryptionHeader [[inline]];
(HeaderType::MainHeader): MainArchiveHeader [[inline]];
(HeaderType::FileHeader): FileOrServiceHeader [[inline]];
(HeaderType::ServiceHeader): FileOrServiceHeader [[inline]];
(HeaderType::EndOfArchive): EndOfArchiveHeader [[inline]];
}
if (type == HeaderType::EndOfArchive) {
break;
}
};
struct RARv5 {
type::Magic<RARv5_MAGIC> magic;
Header headers[while(!std::mem::eof())]; // TODO: would be better to read until EOAHeader
};
RARv5 archive @ std::mem::find_string(0, RARv5_MAGIC);