zesven 1.1.0

A pure Rust implementation of the 7z archive format
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
//! Zstandard (ZSTD) compression codec.
//!
//! This module provides ZSTD compression support for 7z archives.
//! ZSTD provides excellent compression ratios with fast decompression.
//!
//! ## Dictionary Support
//!
//! ZSTD dictionaries can significantly improve compression ratios for small files
//! that share common patterns. This module provides:
//!
//! - [`ZstdDictionary`]: A trained dictionary that can be reused
//! - [`ZstdDecoderWithDict`]: Decoder that uses a pre-loaded dictionary
//! - [`ZstdEncoderWithDict`]: Encoder that uses a dictionary
//!
//! ### Example: Training and Using a Dictionary
//!
//! ```rust,ignore
//! use zesven::codec::zstd::{ZstdDictionary, ZstdEncoderWithDict, ZstdDecoderWithDict};
//!
//! // Train a dictionary from sample data
//! let samples = vec![
//!     b"common prefix data 1".to_vec(),
//!     b"common prefix data 2".to_vec(),
//!     b"common prefix data 3".to_vec(),
//! ];
//! let dict = ZstdDictionary::train(&samples, 4096)?;
//!
//! // Compress with the dictionary
//! let mut compressed = Vec::new();
//! {
//!     let mut encoder = ZstdEncoderWithDict::new(&mut compressed, 3, &dict)?;
//!     encoder.write_all(b"common prefix data 4")?;
//!     encoder.try_finish()?;
//! }
//!
//! // Decompress with the dictionary
//! let mut decoder = ZstdDecoderWithDict::new(Cursor::new(compressed), &dict)?;
//! let mut output = Vec::new();
//! decoder.read_to_end(&mut output)?;
//! ```

use std::io::{self, BufReader, Read, Write};
use std::sync::Arc;

use zstd::stream::{Decoder as ZstdDecoder, Encoder as ZstdEncoderInner};

use super::{Decoder, Encoder, method};

/// ZSTD decoder.
pub struct ZstdStreamDecoder<R> {
    inner: ZstdDecoder<'static, BufReader<R>>,
}

impl<R> std::fmt::Debug for ZstdStreamDecoder<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ZstdStreamDecoder").finish_non_exhaustive()
    }
}

impl<R: Read + Send> ZstdStreamDecoder<R> {
    /// Creates a new ZSTD decoder.
    pub fn new(input: R) -> io::Result<Self> {
        let decoder = ZstdDecoder::new(input)?;
        Ok(Self { inner: decoder })
    }
}

impl<R: Read + Send> Read for ZstdStreamDecoder<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

impl<R: Read + Send> Decoder for ZstdStreamDecoder<R> {
    fn method_id(&self) -> &'static [u8] {
        method::ZSTD
    }
}

/// ZSTD encoder options.
#[derive(Debug, Clone)]
pub struct ZstdEncoderOptions {
    /// Compression level (1-22, default 3).
    pub level: i32,
}

impl Default for ZstdEncoderOptions {
    fn default() -> Self {
        Self { level: 3 }
    }
}

/// ZSTD encoder.
pub struct ZstdStreamEncoder<'a, W: Write> {
    inner: ZstdEncoderInner<'a, W>,
}

impl<'a, W: Write + Send> ZstdStreamEncoder<'a, W> {
    /// Creates a new ZSTD encoder.
    pub fn new(output: W, options: &ZstdEncoderOptions) -> io::Result<Self> {
        let encoder = ZstdEncoderInner::new(output, options.level)?;
        Ok(Self { inner: encoder })
    }

    /// Finishes encoding and returns the underlying writer.
    pub fn try_finish(self) -> io::Result<W> {
        self.inner.finish()
    }
}

impl<'a, W: Write + Send> Write for ZstdStreamEncoder<'a, W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.inner.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

impl<'a, W: Write + Send + 'a> Encoder for ZstdStreamEncoder<'a, W> {
    fn method_id(&self) -> &'static [u8] {
        method::ZSTD
    }

    fn finish(self: Box<Self>) -> io::Result<()> {
        self.inner.finish()?;
        Ok(())
    }
}

// =============================================================================
// Dictionary Support
// =============================================================================

/// A trained ZSTD dictionary for improved compression of similar data.
///
/// Dictionaries work best when:
/// - Compressing many small files (< 128 KB each)
/// - Files share common patterns or structure
/// - The dictionary is trained on representative samples
///
/// # Example
///
/// ```rust,ignore
/// use zesven::codec::zstd::ZstdDictionary;
///
/// // Collect sample data for training
/// let samples: Vec<Vec<u8>> = vec![
///     b"JSON: {\"type\": \"user\", \"id\": 1}".to_vec(),
///     b"JSON: {\"type\": \"user\", \"id\": 2}".to_vec(),
///     b"JSON: {\"type\": \"admin\", \"id\": 3}".to_vec(),
/// ];
///
/// // Train a 4KB dictionary
/// let dict = ZstdDictionary::train(&samples, 4096)?;
/// println!("Dictionary ID: {}", dict.id());
/// ```
#[derive(Clone)]
pub struct ZstdDictionary {
    /// Raw dictionary data.
    data: Arc<Vec<u8>>,
    /// Dictionary ID (extracted from header).
    id: u32,
}

impl std::fmt::Debug for ZstdDictionary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ZstdDictionary")
            .field("id", &self.id)
            .field("size", &self.data.len())
            .finish()
    }
}

impl ZstdDictionary {
    /// Trains a dictionary from sample data.
    ///
    /// # Arguments
    ///
    /// * `samples` - Collection of sample data to train on
    /// * `dict_size` - Target dictionary size in bytes (typically 4KB-128KB)
    ///
    /// # Returns
    ///
    /// A trained dictionary, or an error if training fails.
    ///
    /// # Notes
    ///
    /// - More samples generally produce better dictionaries
    /// - Samples should be representative of the data to compress
    /// - Larger dictionaries can provide better compression but use more memory
    pub fn train(samples: &[Vec<u8>], dict_size: usize) -> io::Result<Self> {
        let sample_refs: Vec<&[u8]> = samples.iter().map(|s| s.as_slice()).collect();
        let dict_data =
            zstd::dict::from_samples(&sample_refs, dict_size).map_err(io::Error::other)?;

        Self::from_bytes(dict_data)
    }

    /// Creates a dictionary from raw dictionary data.
    ///
    /// The data should be a valid ZSTD dictionary, either trained or
    /// loaded from a file.
    pub fn from_bytes(data: Vec<u8>) -> io::Result<Self> {
        if data.len() < 8 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "dictionary too small",
            ));
        }

        // Extract dictionary ID from header
        // ZSTD dictionaries start with magic 0xEC30A437, followed by dict ID
        let id = if data.len() >= 8
            && data[0] == 0x37
            && data[1] == 0xA4
            && data[2] == 0x30
            && data[3] == 0xEC
        {
            u32::from_le_bytes([data[4], data[5], data[6], data[7]])
        } else {
            // Content-only dictionary or raw dictionary
            0
        };

        Ok(Self {
            data: Arc::new(data),
            id,
        })
    }

    /// Loads a dictionary from a file.
    pub fn from_file(path: impl AsRef<std::path::Path>) -> io::Result<Self> {
        let data = std::fs::read(path)?;
        Self::from_bytes(data)
    }

    /// Returns the dictionary ID.
    ///
    /// Returns 0 for content-only dictionaries.
    pub fn id(&self) -> u32 {
        self.id
    }

    /// Returns the dictionary size in bytes.
    pub fn size(&self) -> usize {
        self.data.len()
    }

    /// Returns the raw dictionary data.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }

    /// Saves the dictionary to a file.
    pub fn save(&self, path: impl AsRef<std::path::Path>) -> io::Result<()> {
        std::fs::write(path, &*self.data)
    }
}

/// ZSTD decoder that uses a pre-loaded dictionary.
///
/// Dictionary decompression is faster than loading the dictionary for each
/// decompression operation because the dictionary can be prepared once.
pub struct ZstdDecoderWithDict<'d, R: Read> {
    inner: ZstdDecoder<'d, BufReader<R>>,
}

impl<'d, R: Read> std::fmt::Debug for ZstdDecoderWithDict<'d, R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ZstdDecoderWithDict")
            .finish_non_exhaustive()
    }
}

impl<'d, R: Read + Send> ZstdDecoderWithDict<'d, R> {
    /// Creates a new decoder using the given dictionary.
    pub fn new(input: R, dict: &'d ZstdDictionary) -> io::Result<Self> {
        let buf_reader = BufReader::new(input);
        let decoder = ZstdDecoder::with_dictionary(buf_reader, dict.as_bytes())?;
        Ok(Self { inner: decoder })
    }
}

impl<'d, R: Read + Send> Read for ZstdDecoderWithDict<'d, R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

impl<'d, R: Read + Send> Decoder for ZstdDecoderWithDict<'d, R> {
    fn method_id(&self) -> &'static [u8] {
        method::ZSTD
    }
}

/// ZSTD encoder that uses a pre-loaded dictionary.
///
/// Dictionary compression can significantly improve compression ratios
/// for small files that share common patterns.
pub struct ZstdEncoderWithDict<'d, W: Write> {
    inner: ZstdEncoderInner<'d, W>,
}

impl<'d, W: Write> std::fmt::Debug for ZstdEncoderWithDict<'d, W> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ZstdEncoderWithDict")
            .finish_non_exhaustive()
    }
}

impl<'d, W: Write + Send> ZstdEncoderWithDict<'d, W> {
    /// Creates a new encoder using the given dictionary.
    ///
    /// # Arguments
    ///
    /// * `output` - The writer to write compressed data to
    /// * `level` - Compression level (1-22)
    /// * `dict` - The dictionary to use for compression
    pub fn new(output: W, level: i32, dict: &'d ZstdDictionary) -> io::Result<Self> {
        let encoder = ZstdEncoderInner::with_dictionary(output, level, dict.as_bytes())?;
        Ok(Self { inner: encoder })
    }

    /// Finishes encoding and returns the underlying writer.
    pub fn try_finish(self) -> io::Result<W> {
        self.inner.finish()
    }
}

impl<'d, W: Write + Send> Write for ZstdEncoderWithDict<'d, W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.inner.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

impl<'d, W: Write + Send + 'd> Encoder for ZstdEncoderWithDict<'d, W> {
    fn method_id(&self) -> &'static [u8] {
        method::ZSTD
    }

    fn finish(self: Box<Self>) -> io::Result<()> {
        self.inner.finish()?;
        Ok(())
    }
}

/// Options for ZSTD encoding with optional dictionary.
#[derive(Debug, Clone, Default)]
pub struct ZstdEncoderOptionsWithDict {
    /// Compression level (1-22, default 3).
    pub level: i32,
    /// Optional dictionary for improved compression.
    pub dictionary: Option<ZstdDictionary>,
}

impl ZstdEncoderOptionsWithDict {
    /// Creates new options with default settings.
    pub fn new() -> Self {
        Self {
            level: 3,
            dictionary: None,
        }
    }

    /// Sets the compression level.
    pub fn level(mut self, level: i32) -> Self {
        self.level = level.clamp(1, 22);
        self
    }

    /// Sets the dictionary to use.
    pub fn dictionary(mut self, dict: ZstdDictionary) -> Self {
        self.dictionary = Some(dict);
        self
    }
}

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

    #[test]
    fn test_zstd_round_trip() {
        let original = b"Hello, World! This is a test of ZSTD compression.";

        // Compress
        let mut compressed = Vec::new();
        {
            let mut encoder =
                ZstdStreamEncoder::new(&mut compressed, &ZstdEncoderOptions::default()).unwrap();
            encoder.write_all(original).unwrap();
            encoder.try_finish().unwrap();
        }

        // Decompress
        let mut decoder = ZstdStreamDecoder::new(Cursor::new(compressed)).unwrap();
        let mut decompressed = Vec::new();
        decoder.read_to_end(&mut decompressed).unwrap();

        assert_eq!(decompressed, original);
    }

    #[test]
    fn test_zstd_decoder_method_id() {
        let data = zstd::encode_all(&b"test"[..], 3).unwrap();
        let decoder = ZstdStreamDecoder::new(Cursor::new(data)).unwrap();
        assert_eq!(decoder.method_id(), method::ZSTD);
    }

    // Dictionary tests

    #[test]
    fn test_zstd_dictionary_train() {
        // Create sample data with common patterns
        let samples: Vec<Vec<u8>> = (0..100)
            .map(|i| {
                format!(
                    "{{\"type\": \"user\", \"id\": {}, \"name\": \"User{}\"}}",
                    i, i
                )
                .into_bytes()
            })
            .collect();

        let dict = ZstdDictionary::train(&samples, 4096).unwrap();
        assert!(dict.size() > 0);
        assert!(dict.size() <= 4096);
    }

    #[test]
    fn test_zstd_dictionary_from_bytes() {
        // Create a simple dictionary (this is content-only, no magic header)
        let dict_data = vec![0u8; 1024];
        let dict = ZstdDictionary::from_bytes(dict_data).unwrap();
        assert_eq!(dict.id(), 0); // Content-only dictionary
        assert_eq!(dict.size(), 1024);
    }

    #[test]
    fn test_zstd_dictionary_too_small() {
        let dict_data = vec![0u8; 4];
        let result = ZstdDictionary::from_bytes(dict_data);
        assert!(result.is_err());
    }

    #[test]
    fn test_zstd_dictionary_with_magic() {
        // Create dictionary with ZSTD magic header
        let mut dict_data = vec![0u8; 128];
        // ZSTD dictionary magic: 0xEC30A437 (little-endian: 37 A4 30 EC)
        dict_data[0] = 0x37;
        dict_data[1] = 0xA4;
        dict_data[2] = 0x30;
        dict_data[3] = 0xEC;
        // Dictionary ID: 0x12345678
        dict_data[4] = 0x78;
        dict_data[5] = 0x56;
        dict_data[6] = 0x34;
        dict_data[7] = 0x12;

        let dict = ZstdDictionary::from_bytes(dict_data).unwrap();
        assert_eq!(dict.id(), 0x12345678);
    }

    #[test]
    fn test_zstd_dictionary_round_trip() {
        // Train a dictionary
        let samples: Vec<Vec<u8>> = (0..50)
            .map(|i| format!("prefix_data_{}_suffix", i).into_bytes())
            .collect();

        let dict = ZstdDictionary::train(&samples, 4096).unwrap();

        // Compress with dictionary
        let original = b"prefix_data_999_suffix with some extra content";
        let mut compressed = Vec::new();
        {
            let mut encoder = ZstdEncoderWithDict::new(&mut compressed, 3, &dict).unwrap();
            encoder.write_all(original).unwrap();
            encoder.try_finish().unwrap();
        }

        // Decompress with dictionary
        let mut decoder = ZstdDecoderWithDict::new(Cursor::new(compressed), &dict).unwrap();
        let mut decompressed = Vec::new();
        decoder.read_to_end(&mut decompressed).unwrap();

        assert_eq!(decompressed, original);
    }

    #[test]
    fn test_zstd_dictionary_compression_improvement() {
        // Train a dictionary on specific data
        let samples: Vec<Vec<u8>> = (0..100)
            .map(|i| {
                format!(
                    "{{\"type\": \"event\", \"id\": {}, \"timestamp\": 1234567890}}",
                    i
                )
                .into_bytes()
            })
            .collect();

        let dict = ZstdDictionary::train(&samples, 8192).unwrap();

        // Compress test data
        let test_data = b"{\"type\": \"event\", \"id\": 500, \"timestamp\": 1234567891}";

        // Compress without dictionary
        let compressed_no_dict = zstd::encode_all(&test_data[..], 3).unwrap();

        // Compress with dictionary
        let mut compressed_with_dict = Vec::new();
        {
            let mut encoder =
                ZstdEncoderWithDict::new(&mut compressed_with_dict, 3, &dict).unwrap();
            encoder.write_all(test_data).unwrap();
            encoder.try_finish().unwrap();
        }

        // Dictionary compression should be smaller or equal for similar data
        // Note: For very small data, dictionary might not help much
        println!(
            "Without dict: {} bytes, with dict: {} bytes",
            compressed_no_dict.len(),
            compressed_with_dict.len()
        );
    }

    #[test]
    fn test_zstd_encoder_options_with_dict() {
        let samples: Vec<Vec<u8>> = (0..10)
            .map(|i| format!("sample_{}", i).into_bytes())
            .collect();
        let dict = ZstdDictionary::train(&samples, 1024).unwrap();

        let options = ZstdEncoderOptionsWithDict::new().level(5).dictionary(dict);

        assert_eq!(options.level, 5);
        assert!(options.dictionary.is_some());
    }

    #[test]
    fn test_zstd_dictionary_clone() {
        let samples: Vec<Vec<u8>> = (0..10)
            .map(|i| format!("data_{}", i).into_bytes())
            .collect();
        let dict = ZstdDictionary::train(&samples, 1024).unwrap();
        let dict_clone = dict.clone();

        assert_eq!(dict.id(), dict_clone.id());
        assert_eq!(dict.size(), dict_clone.size());
    }
}