sit-rs 0.3.0

Rust-native extraction for StuffIt Expander archive files
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use std::io;

use binrw::{BinReaderExt, binread};
use bitflags::bitflags;
use fourcc::FourCC;
use macintosh_utils::{FinderFlags, Fork, decode_string};

use super::{Algorithm, Version};

bitflags! {
    #[derive(Debug, Default)]
    pub struct ArchiveFlags: u8 {
        const RECEIPT = 1<<3;
        const PADDING = 1<<4;
        const COMMENT = 1<<5;
        const FLAG_40 = 1<<6;
        const ENCRYPTED = 1<<7;
    }
}

macro_rules! derive_binread_bitflags {
    ($name: ident) => {
        impl binrw::BinRead for $name {
            type Args<'a> = ();

            fn read_options<R: io::Read + io::Seek>(
                reader: &mut R,
                _endian: binrw::Endian,
                _args: Self::Args<'_>,
            ) -> binrw::BinResult<Self> {
                let flags = reader.read_be()?;
                if let Some(flags) = Self::from_bits(flags) {
                    return Ok(flags);
                }

                log::warn!(
                    "{} has unknown bits (0x{:02x}) set, something has probably gone wrong",
                    stringify!($name),
                    !Self::all().bits() & flags
                );

                Ok(Self::from_bits(Self::all().bits() & flags).unwrap())
            }
        }
    };
}

derive_binread_bitflags!(ArchiveFlags);

bitflags! {
    #[derive(Debug, Default, Clone, Copy)]
    pub struct EntryFlags: u8 {
        const UNKNOWN1 = 1<<2;
        const COMMENT = 1<<3;
        const RESOURCE_FORK=1<<4;
        const DIRECTORY=1<<6;
        const ENCRYPTED=1<<5;
        const LOCKED=1<<7;
    }
}

derive_binread_bitflags!(EntryFlags);

bitflags! {
    #[derive(Debug, Default, Clone, Copy)]
    pub struct AdditionalFlags: u8 {
        const RESOURCE_DATA=0x01;
        const UNKNOWN=0x0C;
    }
}

derive_binread_bitflags!(AdditionalFlags);

#[binread]
#[derive(Debug)]
#[br(big)]
pub struct Unknown {
    pub unknown1: u32,
    pub unknown2: u16,
    pub unknown3: u16,
    pub unknown4: u16,
    pub unknown5: u16,
    pub unknown6: u8,
    pub unknown7: u8,
    pub unknown8: u16,
    pub unknown9: u16,
    pub unknown10: u16,
}

#[binread]
#[derive(Debug)]
#[br(big)]
/// Header at the start of the an archive
pub struct ArchiveHeader {
    #[br(temp, assert(magic1 == *b"StuffIt (c)1997-"))]
    magic1: [u8; 16],
    pub copyright_year: FourCC,
    #[br(temp, assert(magic2 == *b" Aladdin Systems, Inc., http://www.aladdinsys.com/StuffIt/"))]
    magic2: [u8; 58],
    /// Two newline characters (seen as \r\n and \n\n")
    #[br(temp)]
    _newlinenewline: u16,

    pub unknown: [u8; 2],
    pub version: Version,
    pub flags: ArchiveFlags,
    pub total_archive_size: u32,
    pub catalog_offset: u32,
    pub entry_count: u16,
    #[br(assert(catalog_offset == catalog_offset_rep), temp)]
    catalog_offset_rep: u32,

    pub archive_header_crc: u16,

    #[br(calc(true))]
    /// TODO: Actually check crc
    pub checksum_valid: bool,

    #[br(if(flags.contains(ArchiveFlags::PADDING)))]
    pub reserved: Option<[u8; 14]>, // "\r¥¥Reserved¥¥\u{0}"

    #[br(temp, if(flags.contains(ArchiveFlags::COMMENT), 0))]
    comment_length: u16,

    #[br(temp, if(flags.contains(ArchiveFlags::COMMENT), 0))]
    padding_length: u16,

    #[br(temp, if(flags.contains(ArchiveFlags::ENCRYPTED), 5), assert(hash_length == 5))]
    hash_length: u8,

    #[br(if(flags.contains(ArchiveFlags::ENCRYPTED)))]
    pub hash: Option<[u8; 5]>,

    #[br(temp, if(flags.contains(ArchiveFlags::FLAG_40), 0))]
    unknown_count: u16,

    #[br(if(flags.contains(ArchiveFlags::FLAG_40)), count(unknown_count))]
    pub unknown_items: Vec<Unknown>,

    #[br(count(comment_length))]
    pub comment: Vec<u8>,

    #[br(count(padding_length))]
    pub padding: Vec<u8>,
}

impl ArchiveHeader {
    pub fn first_entry_offset(&self) -> u64 {
        self.catalog_offset as u64
    }
}

#[binread]
#[derive(Debug, Clone)]
#[br(big, import {offset: u64})]
/// A directory entry in v5 archives
pub struct Directory {
    #[br(temp, assert(entry_id==fourcc::fourcc!(0xa5a5a5a5u32)))]
    pub entry_id: FourCC,
    pub version: u8,
    //#[br(assert(unknown1==0))]
    pub unknown1: u8,
    pub header_len: u16,
    //#[br(assert(unknown1==0))]
    pub unknown2: u8,
    #[br(assert(flags.contains(EntryFlags::DIRECTORY)))]
    pub flags: EntryFlags,
    #[br(map(macintosh_utils::date))]
    pub creation_date: chrono::DateTime<chrono::Utc>,
    #[br(map(macintosh_utils::date))]
    pub modification_date: chrono::DateTime<chrono::Utc>,
    /// Offset of previous entry on the same level
    pub previous_entry_offset: u32,
    /// Offset of next entry on the same level
    pub next_entry_offset: u32,
    /// Offset to parent directory entry
    pub directory_entry_offset: u32,
    pub file_name_len: u16,
    pub header_crc: u16,
    pub data_uncompressed_len: u32,
    pub data_compressed_len: u32,
    pub data_crc: u16,
    pub unknown3: u16,
    #[br(map(|v:u16| v as u32 + 1))]
    pub child_count: u32,
    #[br(if(data_uncompressed_len != 0xFFFFFFFF), args {  version, file_name_len, flags})]
    pub real_dir: Option<RealDirectory>,
    #[br(calc(offset + header_len as u64 + 36))]
    pub first_child_offset: u64,
}

impl Directory {
    #[inline]
    pub fn finder_flags(&self) -> FinderFlags {
        if let Some(dir) = &self.real_dir {
            dir.finder_flags
        } else {
            FinderFlags::default()
        }
    }

    #[inline]
    pub fn comment(&self) -> &str {
        if let Some(dir) = &self.real_dir {
            dir.comment.as_str()
        } else {
            ""
        }
    }

    #[inline]
    pub fn file_name(&self) -> &str {
        if let Some(dir) = &self.real_dir {
            dir.file_name.as_str()
        } else {
            ""
        }
    }

    #[inline]
    pub fn file_code(&self) -> FourCC {
        if let Some(dir) = &self.real_dir {
            dir.file_code
        } else {
            FourCC::new(0)
        }
    }

    #[inline]
    pub fn creator_code(&self) -> FourCC {
        if let Some(dir) = &self.real_dir {
            dir.creator_code
        } else {
            FourCC::new(0)
        }
    }

    #[inline]
    pub(crate) fn marks_end(&self) -> bool {
        self.real_dir.is_none()
    }

    #[inline]
    pub fn checksum(&self, fork: Fork) -> u16 {
        if !self.marks_end() {
            match fork {
                Fork::Data => self.data_crc,
                Fork::Resource => 0,
            }
        } else {
            0
        }
    }

    #[inline]
    pub fn compressed_size(&self, fork: Fork) -> usize {
        if !self.marks_end() {
            match fork {
                Fork::Data => 0,
                Fork::Resource => 0,
            }
        } else {
            0
        }
    }

    #[inline]
    pub fn uncompressed_size(&self, fork: Fork) -> usize {
        if !self.marks_end() {
            match fork {
                Fork::Data => 0,
                Fork::Resource => 0,
            }
        } else {
            0
        }
    }

    #[inline]
    pub fn algorithm(&self, fork: Fork) -> Algorithm {
        if !self.marks_end() {
            match fork {
                // TODO: figure out where the real algorithm is stored
                Fork::Data => Algorithm::None,
                Fork::Resource => Algorithm::None,
            }
        } else {
            Algorithm::None
        }
    }

    #[inline]
    pub fn offset(&self, fork: Fork) -> u64 {
        match fork {
            Fork::Data => self.first_child_offset + self.compressed_size(Fork::Resource) as u64,
            Fork::Resource => self.first_child_offset,
        }
    }

    pub fn uses_encryption(&self) -> bool {
        self.flags.contains(EntryFlags::ENCRYPTED)
    }
}

#[binread]
#[derive(Debug, Clone)]
#[br(big, import {  file_name_len: u16,  version: u8, flags: EntryFlags })]
pub struct RealDirectory {
    #[br(count(file_name_len), map(decode_string))]
    pub file_name: String,
    #[br(if(flags.contains(EntryFlags::COMMENT), 0))]
    pub comment_len: u16,
    #[br(if(flags.contains(EntryFlags::COMMENT)))]
    pub unknown3: Option<u16>,
    #[br(count(comment_len), map(decode_string))]
    pub comment: String,
    pub unknown4: u16,
    pub unknown5: u16,
    pub file_code: FourCC,
    pub creator_code: FourCC,
    pub finder_flags: FinderFlags,
    pub unknown7a: i16,
    pub unknown7b: i16,

    pub unknown7c: u8,
    pub unknown7d: u8,

    pub unknown7e: i16,
    pub unknown7f: i16,
    pub unknown7: [u8; 8],
    #[br(if(version == 1))]
    pub unknown6a: Option<u16>,
    #[br(if(version == 1))]
    pub unknown6b: Option<u16>,
}

#[binread]
#[derive(Debug, Clone)]
#[br(big)]
/// A file entry in v5 archives
pub struct File {
    #[br(temp, assert(entry_id==fourcc::fourcc!(0xa5a5a5a5u32)))]
    entry_id: FourCC,
    pub version: u8,
    pub unknown1: u8,
    pub header_len: u16,
    pub unknown2: u8,
    #[br(assert(!flags.contains(EntryFlags::DIRECTORY)))]
    pub flags: EntryFlags,
    #[br(map(macintosh_utils::date))]
    pub creation_date: chrono::DateTime<chrono::Utc>,
    #[br(map(macintosh_utils::date))]
    pub modification_date: chrono::DateTime<chrono::Utc>,
    pub previous_entry_offset: u32,
    pub next_entry_offset: u32,
    pub directory_entry_offset: u32,
    pub file_name_len: u16,
    pub header_crc: u16,
    pub data_uncompressed_size: u32,
    pub data_compressed_size: u32,
    pub data_crc: u16,
    pub unknown: u16,
    pub data_compression: Algorithm,
    #[br(temp)]
    data_pass_len: u8,
    #[br(count(if !flags.contains(EntryFlags::ENCRYPTED) { 0 } else {data_pass_len}))]
    pub datakey: Vec<u8>,
    #[br(count(file_name_len), map(decode_string))]
    pub file_name: String,
    #[br(if(flags.contains(EntryFlags::COMMENT), 0))]
    pub comment_len: u16,
    #[br(if(flags.contains(EntryFlags::COMMENT), 0))]
    pub unknown9: u16,
    #[br(count(comment_len), map(decode_string))]
    pub comment: String,
    pub unknown10: u8,
    pub additional_flags: AdditionalFlags,
    // TODO: Seen this as the same value (0x92ae) across all entries in two directories in
    // md5.5.sit
    pub unknown_checksum: u16,
    pub file_code: FourCC,
    pub creator_code: FourCC,
    pub finder_flags: FinderFlags,
    #[br(temp, count(if version == 1 { 22 } else { 18 }))]
    _garbage: Vec<u8>,

    #[br(temp, calc(additional_flags.contains(AdditionalFlags::RESOURCE_DATA)))]
    rsrc: bool,

    #[br(if(rsrc, 0))]
    pub rsrc_uncompressed_size: u32,
    #[br(if(rsrc, 0))]
    pub rsrc_compressed_size: u32,
    #[br(if(rsrc, 0))]
    pub rsrc_crc: u16,
    #[br(if(rsrc, 0))]
    pub rsrc_unknown: u16,
    #[br(if(rsrc, Algorithm::None))]
    pub rsrc_compression: Algorithm,
    #[br(temp, if(rsrc, 0))]
    res_key_len: u8,
    #[br(count(if !flags.contains(EntryFlags::ENCRYPTED) { 0 } else {res_key_len}))]
    pub res_key: Vec<u8>,

    #[br(calc(0))]
    /// Archive iterator will set this to the actual offset in the stream after the struct has been
    /// read
    pub(crate) payload_offset: u64,

    #[br(ignore)]
    /// Index of the file entry determine by counting previous file entries in depth-first search
    /// order
    pub index: usize,
}

impl File {
    #[inline]
    pub fn offset(&self, fork: Fork) -> u64 {
        match fork {
            Fork::Resource => self.payload_offset,
            Fork::Data => self.payload_offset + self.compressed_size(Fork::Resource) as u64,
        }
    }

    #[inline]
    pub fn uncompressed_size(&self, fork: Fork) -> usize {
        match fork {
            Fork::Resource => self.rsrc_uncompressed_size as usize,
            Fork::Data => self.data_uncompressed_size as usize,
        }
    }

    #[inline]
    pub fn compressed_size(&self, fork: Fork) -> usize {
        match fork {
            Fork::Resource => self.rsrc_compressed_size as usize,
            Fork::Data => self.data_compressed_size as usize,
        }
    }

    #[inline]
    pub fn compression_method(&self, fork: Fork) -> Algorithm {
        match fork {
            Fork::Resource => self.rsrc_compression,
            Fork::Data => self.data_compression,
        }
    }

    #[inline]
    pub fn checksum(&self, fork: Fork) -> u16 {
        match fork {
            Fork::Resource => self.rsrc_crc,
            Fork::Data => self.data_crc,
        }
    }

    #[inline]
    pub fn encrypted(&self, fork: Fork) -> bool {
        match fork {
            Fork::Resource => self.flags.contains(EntryFlags::ENCRYPTED),
            Fork::Data => self.flags.contains(EntryFlags::ENCRYPTED),
        }
    }

    pub fn uses_encryption(&self) -> bool {
        self.flags.contains(EntryFlags::ENCRYPTED)
    }
}

#[binread]
#[derive(Debug)]
#[br(big, import {offset: u64})]
pub enum Entry {
    Directory(#[br(args { offset })] Directory),
    File(File),
}

impl Entry {
    #[inline]
    pub fn next_entry_offset(&self) -> u64 {
        match self {
            Entry::Directory(entry) => entry.next_entry_offset as u64,
            Entry::File(entry) => entry.next_entry_offset as u64,
        }
    }

    #[inline]
    pub fn name(&self) -> &str {
        match self {
            Entry::Directory(directory_entry) => directory_entry.file_name(),
            Entry::File(file_entry) => file_entry.file_name.as_str(),
        }
    }
}