tact-parser 0.4.3

Parser for TACT (Trusted Application Content Transfer) files used in Blizzard's NGDP distribution system
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! ESpec (Encoding Specification) parser for BLTE compression
//!
//! ESpec strings define how data should be encoded/compressed in BLTE format.
//! This module implements a parser for the EBNF grammar defined at:
//! <https://wowdev.wiki/BLTE#Encoding_Specification_(ESpec)>

use std::fmt;
use std::str::FromStr;

use crate::{Error, Result};

/// Encoding specification (ESpec) defining how to encode/compress data
#[derive(Debug, Clone, PartialEq)]
pub enum ESpec {
    /// No compression ('n')
    None,
    /// ZLib compression ('z')
    ZLib {
        level: Option<u8>,
        bits: Option<ZLibBits>,
    },
    /// Encryption ('e')
    Encrypted {
        key: String,
        iv: Vec<u8>,
        spec: Box<ESpec>,
    },
    /// Block table ('b')
    BlockTable { chunks: Vec<BlockChunk> },
    /// BCPack compression ('c')
    BCPack { bcn: u8 },
    /// GDeflate compression ('g')
    GDeflate { level: u8 },
}

/// Block chunk specification
#[derive(Debug, Clone, PartialEq)]
pub struct BlockChunk {
    /// Block size specification (optional for final chunk)
    pub size_spec: Option<BlockSizeSpec>,
    /// Encoding specification for this chunk
    pub spec: ESpec,
}

/// Block size specification
#[derive(Debug, Clone, PartialEq)]
pub struct BlockSizeSpec {
    /// Block size in bytes
    pub size: u64,
    /// Number of blocks (optional)
    pub count: Option<u32>,
}

/// ZLib compression bits specification
#[derive(Debug, Clone, PartialEq)]
pub enum ZLibBits {
    /// Numeric window bits
    Bits(u8),
    /// MPQ compression
    MPQ,
    /// ZLib compression
    ZLib,
    /// LZ4HC compression
    LZ4HC,
}

impl ESpec {
    /// Parse an ESpec string
    pub fn parse(input: &str) -> Result<Self> {
        Parser::new(input).parse_espec()
    }

    /// Check if this ESpec uses encryption
    pub fn is_encrypted(&self) -> bool {
        matches!(self, ESpec::Encrypted { .. })
    }

    /// Check if this ESpec uses compression
    pub fn is_compressed(&self) -> bool {
        match self {
            ESpec::None => false,
            ESpec::ZLib { .. } | ESpec::BCPack { .. } | ESpec::GDeflate { .. } => true,
            ESpec::BlockTable { chunks } => chunks.iter().any(|c| c.spec.is_compressed()),
            ESpec::Encrypted { spec, .. } => spec.is_compressed(),
        }
    }

    /// Get the compression type as a string
    pub fn compression_type(&self) -> &str {
        match self {
            ESpec::None => "none",
            ESpec::ZLib { .. } => "zlib",
            ESpec::BCPack { .. } => "bcpack",
            ESpec::GDeflate { .. } => "gdeflate",
            ESpec::BlockTable { .. } => "block",
            ESpec::Encrypted { .. } => "encrypted",
        }
    }
}

impl fmt::Display for ESpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ESpec::None => write!(f, "n"),
            ESpec::ZLib { level, bits } => {
                write!(f, "z")?;
                if let Some(level) = level {
                    write!(f, ":{}", level)?;
                    if let Some(bits) = bits {
                        write!(f, ",{}", bits)?;
                    }
                }
                Ok(())
            }
            ESpec::Encrypted { key, iv, spec } => {
                write!(f, "e:{{{},{},{}}}", key, hex::encode(iv), spec)
            }
            ESpec::BlockTable { chunks } => {
                write!(f, "b:")?;
                if chunks.len() == 1 && chunks[0].size_spec.is_none() {
                    write!(f, "{}", chunks[0].spec)
                } else {
                    write!(f, "{{")?;
                    for (i, chunk) in chunks.iter().enumerate() {
                        if i > 0 {
                            write!(f, ",")?;
                        }
                        if let Some(size_spec) = &chunk.size_spec {
                            write!(f, "{}=", size_spec)?;
                        } else {
                            write!(f, "*=")?;
                        }
                        write!(f, "{}", chunk.spec)?;
                    }
                    write!(f, "}}")
                }
            }
            ESpec::BCPack { bcn } => write!(f, "c:{{{}}}", bcn),
            ESpec::GDeflate { level } => write!(f, "g:{{{}}}", level),
        }
    }
}

impl fmt::Display for BlockSizeSpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.size % (1024 * 1024) == 0 {
            write!(f, "{}M", self.size / (1024 * 1024))?;
        } else if self.size % 1024 == 0 {
            write!(f, "{}K", self.size / 1024)?;
        } else {
            write!(f, "{}", self.size)?;
        }
        if let Some(count) = self.count {
            write!(f, "*{}", count)?;
        }
        Ok(())
    }
}

impl fmt::Display for ZLibBits {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ZLibBits::Bits(n) => write!(f, "{}", n),
            ZLibBits::MPQ => write!(f, "mpq"),
            ZLibBits::ZLib => write!(f, "zlib"),
            ZLibBits::LZ4HC => write!(f, "lz4hc"),
        }
    }
}

/// Parser for ESpec strings
struct Parser<'a> {
    input: &'a str,
    pos: usize,
}

impl<'a> Parser<'a> {
    fn new(input: &'a str) -> Self {
        Self { input, pos: 0 }
    }

    fn peek(&self) -> Option<char> {
        self.input[self.pos..].chars().next()
    }

    fn consume(&mut self, ch: char) -> Result<()> {
        if self.peek() == Some(ch) {
            self.pos += ch.len_utf8();
            Ok(())
        } else {
            Err(Error::IOError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Expected '{}' at position {}", ch, self.pos),
            )))
        }
    }

    fn parse_number(&mut self) -> Result<u64> {
        let start = self.pos;
        while let Some(ch) = self.peek() {
            if ch.is_ascii_digit() {
                self.pos += 1;
            } else {
                break;
            }
        }
        if self.pos == start {
            return Err(Error::IOError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Expected number at position {}", self.pos),
            )));
        }
        self.input[start..self.pos].parse().map_err(|e| {
            Error::IOError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Invalid number: {}", e),
            ))
        })
    }

    fn parse_hex_string(&mut self, len: usize) -> Result<Vec<u8>> {
        let start = self.pos;
        self.pos = (self.pos + len * 2).min(self.input.len());
        let hex_str = &self.input[start..self.pos];
        hex::decode(hex_str).map_err(|e| {
            Error::IOError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Invalid hex string: {}", e),
            ))
        })
    }

    fn parse_identifier(&mut self) -> String {
        let start = self.pos;
        while let Some(ch) = self.peek() {
            if ch.is_ascii_alphanumeric() {
                self.pos += 1;
            } else {
                break;
            }
        }
        self.input[start..self.pos].to_string()
    }

    fn parse_espec(&mut self) -> Result<ESpec> {
        match self.peek() {
            Some('n') => {
                self.consume('n')?;
                Ok(ESpec::None)
            }
            Some('z') => self.parse_zlib(),
            Some('e') => self.parse_encrypted(),
            Some('b') => self.parse_block_table(),
            Some('c') => self.parse_bcpack(),
            Some('g') => self.parse_gdeflate(),
            _ => Err(Error::IOError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Unknown ESpec type at position {}", self.pos),
            ))),
        }
    }

    fn parse_zlib(&mut self) -> Result<ESpec> {
        self.consume('z')?;

        if self.peek() != Some(':') {
            return Ok(ESpec::ZLib {
                level: None,
                bits: None,
            });
        }

        self.consume(':')?;

        // Check for braces (optional)
        let has_braces = self.peek() == Some('{');
        if has_braces {
            self.consume('{')?;
        }

        // Parse level if present and it's a number
        let level = if self.peek().is_some_and(|c| c.is_ascii_digit()) {
            Some(self.parse_number()? as u8)
        } else {
            None
        };

        // Only parse bits if we have a comma AND we're inside braces
        // (bits require braces in the format)
        let bits = if has_braces && self.peek() == Some(',') {
            self.consume(',')?;
            Some(self.parse_zlib_bits()?)
        } else {
            None
        };

        if has_braces {
            self.consume('}')?;
        }

        Ok(ESpec::ZLib { level, bits })
    }

    fn parse_zlib_bits(&mut self) -> Result<ZLibBits> {
        if self.peek().is_some_and(|c| c.is_ascii_digit()) {
            Ok(ZLibBits::Bits(self.parse_number()? as u8))
        } else {
            let ident = self.parse_identifier();
            if ident.is_empty() {
                // No bits specified
                return Err(Error::IOError(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "Expected zlib bits specification after comma",
                )));
            }
            match ident.as_str() {
                "mpq" => Ok(ZLibBits::MPQ),
                "zlib" => Ok(ZLibBits::ZLib),
                "lz4hc" => Ok(ZLibBits::LZ4HC),
                _ => Err(Error::IOError(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("Unknown zlib bits type: {}", ident),
                ))),
            }
        }
    }

    fn parse_encrypted(&mut self) -> Result<ESpec> {
        self.consume('e')?;
        self.consume(':')?;
        self.consume('{')?;

        // Parse 8-byte hex key name
        let key = self.parse_identifier();
        if key.len() != 16 {
            return Err(Error::IOError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Encryption key must be 16 hex chars, got {}", key.len()),
            )));
        }

        self.consume(',')?;

        // Parse 4-byte hex IV
        let iv = self.parse_hex_string(4)?;

        self.consume(',')?;

        // Parse nested ESpec
        let spec = Box::new(self.parse_espec()?);

        self.consume('}')?;

        Ok(ESpec::Encrypted { key, iv, spec })
    }

    fn parse_block_table(&mut self) -> Result<ESpec> {
        self.consume('b')?;
        self.consume(':')?;

        // Check if it's a single spec or multiple chunks
        if self.peek() != Some('{') {
            // Single spec without size
            let spec = self.parse_espec()?;
            return Ok(ESpec::BlockTable {
                chunks: vec![BlockChunk {
                    size_spec: None,
                    spec,
                }],
            });
        }

        self.consume('{')?;

        let mut chunks = Vec::new();

        loop {
            // Parse size spec or final chunk
            let size_spec = if self.peek() == Some('*') {
                self.consume('*')?;
                if self.peek() == Some('=') {
                    None // Final chunk with no size
                } else {
                    // Parse count after *
                    let count = self.parse_number()? as u32;
                    Some(BlockSizeSpec {
                        size: 0, // Will be determined by total size
                        count: Some(count),
                    })
                }
            } else {
                Some(self.parse_block_size_spec()?)
            };

            self.consume('=')?;

            let spec = self.parse_espec()?;
            chunks.push(BlockChunk { size_spec, spec });

            if self.peek() == Some(',') {
                self.consume(',')?;
            } else {
                break;
            }
        }

        self.consume('}')?;

        Ok(ESpec::BlockTable { chunks })
    }

    fn parse_block_size_spec(&mut self) -> Result<BlockSizeSpec> {
        let mut size = self.parse_number()?;

        // Check for unit (K or M)
        if let Some(unit) = self.peek() {
            if unit == 'K' {
                self.consume('K')?;
                size *= 1024;
            } else if unit == 'M' {
                self.consume('M')?;
                size *= 1024 * 1024;
            }
        }

        // Check for count
        let count = if self.peek() == Some('*') {
            self.consume('*')?;
            if self.peek().is_some_and(|c| c.is_ascii_digit()) {
                Some(self.parse_number()? as u32)
            } else {
                None // * without number means "rest of file"
            }
        } else {
            None
        };

        Ok(BlockSizeSpec { size, count })
    }

    fn parse_bcpack(&mut self) -> Result<ESpec> {
        self.consume('c')?;
        self.consume(':')?;
        self.consume('{')?;
        let bcn = self.parse_number()? as u8;
        self.consume('}')?;
        Ok(ESpec::BCPack { bcn })
    }

    fn parse_gdeflate(&mut self) -> Result<ESpec> {
        self.consume('g')?;
        self.consume(':')?;
        self.consume('{')?;
        let level = self.parse_number()? as u8;
        self.consume('}')?;
        Ok(ESpec::GDeflate { level })
    }
}

impl FromStr for ESpec {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        Self::parse(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_none() {
        let spec = ESpec::parse("n").unwrap();
        assert_eq!(spec, ESpec::None);
        assert_eq!(spec.to_string(), "n");
    }

    #[test]
    fn test_parse_zlib_default() {
        let spec = ESpec::parse("z").unwrap();
        assert_eq!(
            spec,
            ESpec::ZLib {
                level: None,
                bits: None
            }
        );
        assert_eq!(spec.to_string(), "z");
    }

    #[test]
    fn test_parse_zlib_with_level() {
        let spec = ESpec::parse("z:9").unwrap();
        assert_eq!(
            spec,
            ESpec::ZLib {
                level: Some(9),
                bits: None
            }
        );
        assert_eq!(spec.to_string(), "z:9");
    }

    #[test]
    fn test_parse_zlib_with_level_and_bits() {
        let spec = ESpec::parse("z:{9,15}").unwrap();
        assert_eq!(
            spec,
            ESpec::ZLib {
                level: Some(9),
                bits: Some(ZLibBits::Bits(15))
            }
        );
    }

    #[test]
    fn test_parse_zlib_with_mpq() {
        let spec = ESpec::parse("z:{9,mpq}").unwrap();
        assert_eq!(
            spec,
            ESpec::ZLib {
                level: Some(9),
                bits: Some(ZLibBits::MPQ)
            }
        );
    }

    #[test]
    fn test_parse_block_table_simple() {
        let spec = ESpec::parse("b:n").unwrap();
        match spec {
            ESpec::BlockTable { chunks } => {
                assert_eq!(chunks.len(), 1);
                assert_eq!(chunks[0].spec, ESpec::None);
                assert!(chunks[0].size_spec.is_none());
            }
            _ => panic!("Expected BlockTable"),
        }
    }

    #[test]
    fn test_parse_block_table_with_sizes() {
        let spec = ESpec::parse("b:{1M*3=z:9,*=n}").unwrap();
        match spec {
            ESpec::BlockTable { chunks } => {
                assert_eq!(chunks.len(), 2);

                // First chunk: 1M * 3 blocks with zlib level 9
                let first = &chunks[0];
                assert_eq!(
                    first.size_spec,
                    Some(BlockSizeSpec {
                        size: 1024 * 1024,
                        count: Some(3),
                    })
                );
                assert_eq!(
                    first.spec,
                    ESpec::ZLib {
                        level: Some(9),
                        bits: None
                    }
                );

                // Final chunk: rest of file uncompressed
                let second = &chunks[1];
                assert!(second.size_spec.is_none());
                assert_eq!(second.spec, ESpec::None);
            }
            _ => panic!("Expected BlockTable"),
        }
    }

    #[test]
    fn test_parse_bcpack() {
        let spec = ESpec::parse("c:{4}").unwrap();
        assert_eq!(spec, ESpec::BCPack { bcn: 4 });
        assert_eq!(spec.to_string(), "c:{4}");
    }

    #[test]
    fn test_parse_gdeflate() {
        let spec = ESpec::parse("g:{5}").unwrap();
        assert_eq!(spec, ESpec::GDeflate { level: 5 });
        assert_eq!(spec.to_string(), "g:{5}");
    }

    #[test]
    fn test_compression_detection() {
        assert!(!ESpec::None.is_compressed());
        assert!(
            ESpec::ZLib {
                level: None,
                bits: None
            }
            .is_compressed()
        );
        assert!(ESpec::BCPack { bcn: 4 }.is_compressed());
        assert!(ESpec::GDeflate { level: 5 }.is_compressed());
    }

    #[test]
    fn test_complex_block_table() {
        let spec = ESpec::parse("b:{256K=n,512K*2=z:6,*=z:9}").unwrap();
        match spec {
            ESpec::BlockTable { chunks } => {
                assert_eq!(chunks.len(), 3);

                // 256KB uncompressed
                assert_eq!(
                    chunks[0].size_spec,
                    Some(BlockSizeSpec {
                        size: 256 * 1024,
                        count: None,
                    })
                );
                assert_eq!(chunks[0].spec, ESpec::None);

                // 512KB * 2 with zlib 6
                assert_eq!(
                    chunks[1].size_spec,
                    Some(BlockSizeSpec {
                        size: 512 * 1024,
                        count: Some(2),
                    })
                );
                assert_eq!(
                    chunks[1].spec,
                    ESpec::ZLib {
                        level: Some(6),
                        bits: None,
                    }
                );

                // Rest with zlib 9
                assert!(chunks[2].size_spec.is_none());
                assert_eq!(
                    chunks[2].spec,
                    ESpec::ZLib {
                        level: Some(9),
                        bits: None,
                    }
                );
            }
            _ => panic!("Expected BlockTable"),
        }
    }
}