lzma_rs/decode/
xz.rs

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
//! Decoder for the `.xz` file format.

use crate::decode::lzma2::Lzma2Decoder;
use crate::decode::util;
use crate::error;
use crate::xz::crc::{CRC32, CRC64};
use crate::xz::{footer, header, CheckMethod, StreamFlags};
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use std::io;
use std::io::Read;

#[derive(Debug)]
struct Record {
    unpadded_size: u64,
    unpacked_size: u64,
}

pub fn decode_stream<R, W>(input: &mut R, output: &mut W) -> error::Result<()>
where
    R: io::BufRead,
    W: io::Write,
{
    let header = header::StreamHeader::parse(input)?;

    let mut records: Vec<Record> = vec![];
    let index_size = loop {
        let mut count_input = util::CountBufRead::new(input);
        let header_size = count_input.read_u8()?;
        lzma_info!("XZ block header_size byte: 0x{:02x}", header_size);

        if header_size == 0 {
            lzma_info!("XZ records: {:?}", records);
            check_index(&mut count_input, &records)?;
            let index_size = count_input.count();
            break index_size;
        }

        read_block(
            &mut count_input,
            output,
            header.stream_flags.check_method,
            &mut records,
            header_size,
        )?;
    };

    let crc32 = input.read_u32::<LittleEndian>()?;
    let mut digest = CRC32.digest();
    {
        let mut digested = util::CrcDigestRead::new(input, &mut digest);
        let backward_size = digested.read_u32::<LittleEndian>()?;
        if index_size as u32 != (backward_size + 1) << 2 {
            return Err(error::Error::XzError(format!(
                "Invalid index size: expected {} but got {}",
                (backward_size + 1) << 2,
                index_size
            )));
        }

        let stream_flags = {
            let field = digested.read_u16::<BigEndian>()?;
            StreamFlags::parse(field)?
        };

        if header.stream_flags != stream_flags {
            return Err(error::Error::XzError(format!(
                "Flags in header ({:?}) does not match footer ({:?})",
                header.stream_flags, stream_flags
            )));
        }
    }

    let digest_crc32 = digest.finalize();
    if crc32 != digest_crc32 {
        return Err(error::Error::XzError(format!(
            "Invalid footer CRC32: expected 0x{:08x} but got 0x{:08x}",
            crc32, digest_crc32
        )));
    }

    if !util::read_tag(input, footer::XZ_MAGIC_FOOTER)? {
        return Err(error::Error::XzError(format!(
            "Invalid footer magic, expected {:?}",
            footer::XZ_MAGIC_FOOTER
        )));
    }

    if !util::is_eof(input)? {
        return Err(error::Error::XzError(
            "Unexpected data after last XZ block".to_string(),
        ));
    }
    Ok(())
}

fn check_index<'a, R>(
    count_input: &mut util::CountBufRead<'a, R>,
    records: &[Record],
) -> error::Result<()>
where
    R: io::BufRead,
{
    let mut digest = CRC32.digest();
    let index_tag = 0u8;
    digest.update(&[index_tag]);
    {
        let mut digested = util::CrcDigestRead::new(count_input, &mut digest);

        let num_records = get_multibyte(&mut digested)?;
        if num_records != records.len() as u64 {
            return Err(error::Error::XzError(format!(
                "Expected {} records but got {} records",
                num_records,
                records.len()
            )));
        }

        for (i, record) in records.iter().enumerate() {
            lzma_info!("XZ index checking record {}: {:?}", i, record);

            let unpadded_size = get_multibyte(&mut digested)?;
            if unpadded_size != record.unpadded_size {
                return Err(error::Error::XzError(format!(
                    "Invalid index for record {}: unpadded size ({}) does not match index ({})",
                    i, record.unpadded_size, unpadded_size
                )));
            }

            let unpacked_size = get_multibyte(&mut digested)?;
            if unpacked_size != record.unpacked_size {
                return Err(error::Error::XzError(format!(
                    "Invalid index for record {}: unpacked size ({}) does not match index ({})",
                    i, record.unpacked_size, unpacked_size
                )));
            }
        }
    };
    // TODO: create padding parser function
    let count = count_input.count();
    let padding_size = ((count ^ 0x03) + 1) & 0x03;
    lzma_info!(
        "XZ index: {} byte(s) read, {} byte(s) of padding",
        count,
        padding_size
    );

    {
        let mut digested = util::CrcDigestRead::new(count_input, &mut digest);
        for _ in 0..padding_size {
            let byte = digested.read_u8()?;
            if byte != 0 {
                return Err(error::Error::XzError(
                    "Invalid index padding, must be null bytes".to_string(),
                ));
            }
        }
    };

    let digest_crc32 = digest.finalize();
    lzma_info!("XZ index checking digest 0x{:08x}", digest_crc32);

    let crc32 = count_input.read_u32::<LittleEndian>()?;
    if crc32 != digest_crc32 {
        return Err(error::Error::XzError(format!(
            "Invalid index CRC32: expected 0x{:08x} but got 0x{:08x}",
            crc32, digest_crc32
        )));
    }

    Ok(())
}

#[derive(Debug)]
enum FilterId {
    Lzma2,
}

fn get_filter_id(id: u64) -> error::Result<FilterId> {
    match id {
        0x21 => Ok(FilterId::Lzma2),
        _ => Err(error::Error::XzError(format!("Unknown filter id {}", id))),
    }
}

struct Filter {
    filter_id: FilterId,
    props: Vec<u8>,
}

struct BlockHeader {
    filters: Vec<Filter>,
    packed_size: Option<u64>,
    unpacked_size: Option<u64>,
}

fn read_block<'a, R, W>(
    count_input: &mut util::CountBufRead<'a, R>,
    output: &mut W,
    check_method: CheckMethod,
    records: &mut Vec<Record>,
    header_size: u8,
) -> error::Result<bool>
where
    R: io::BufRead,
    W: io::Write,
{
    let mut digest = CRC32.digest();
    digest.update(&[header_size]);
    let header_size = ((header_size as u64) << 2) - 1;

    let block_header = {
        let mut taken = count_input.take(header_size);
        let mut digested = io::BufReader::new(util::CrcDigestRead::new(&mut taken, &mut digest));
        read_block_header(&mut digested, header_size)?
    };

    let crc32 = count_input.read_u32::<LittleEndian>()?;
    let digest_crc32 = digest.finalize();
    if crc32 != digest_crc32 {
        return Err(error::Error::XzError(format!(
            "Invalid header CRC32: expected 0x{:08x} but got 0x{:08x}",
            crc32, digest_crc32
        )));
    }

    let mut tmpbuf: Vec<u8> = Vec::new();
    let filters = block_header.filters;
    for (i, filter) in filters.iter().enumerate() {
        if i == 0 {
            // TODO: use SubBufRead on input if packed_size is known?
            let packed_size = decode_filter(count_input, &mut tmpbuf, filter)?;
            if let Some(expected_packed_size) = block_header.packed_size {
                if (packed_size as u64) != expected_packed_size {
                    return Err(error::Error::XzError(format!(
                        "Invalid compressed size: expected {} but got {}",
                        expected_packed_size, packed_size
                    )));
                }
            }
        } else {
            let mut newbuf: Vec<u8> = Vec::new();
            decode_filter(
                &mut io::BufReader::new(tmpbuf.as_slice()),
                &mut newbuf,
                filter,
            )?;
            // TODO: does this move or copy?
            tmpbuf = newbuf;
        }
    }

    let unpacked_size = tmpbuf.len();
    lzma_info!("XZ block decompressed to {} byte(s)", tmpbuf.len());

    if let Some(expected_unpacked_size) = block_header.unpacked_size {
        if (unpacked_size as u64) != expected_unpacked_size {
            return Err(error::Error::XzError(format!(
                "Invalid decompressed size: expected {} but got {}",
                expected_unpacked_size, unpacked_size
            )));
        }
    }

    let count = count_input.count();
    let padding_size = ((count ^ 0x03) + 1) & 0x03;
    lzma_info!(
        "XZ block: {} byte(s) read, {} byte(s) of padding, check method {:?}",
        count,
        padding_size,
        check_method
    );
    for _ in 0..padding_size {
        let byte = count_input.read_u8()?;
        if byte != 0 {
            return Err(error::Error::XzError(
                "Invalid block padding, must be null bytes".to_string(),
            ));
        }
    }
    validate_block_check(count_input, tmpbuf.as_slice(), check_method)?;

    output.write_all(tmpbuf.as_slice())?;
    records.push(Record {
        unpadded_size: (count_input.count() - padding_size) as u64,
        unpacked_size: unpacked_size as u64,
    });

    let finished = false;
    Ok(finished)
}

/// Verify block checksum against the "Block Check" field.
///
/// See spec section 3.4 for details.
fn validate_block_check<R>(
    input: &mut R,
    buf: &[u8],
    check_method: CheckMethod,
) -> error::Result<()>
where
    R: io::BufRead,
{
    match check_method {
        CheckMethod::None => (),
        CheckMethod::Crc32 => {
            let crc32 = input.read_u32::<LittleEndian>()?;
            let digest_crc32 = CRC32.checksum(buf);
            if crc32 != digest_crc32 {
                return Err(error::Error::XzError(format!(
                    "Invalid block CRC32, expected 0x{:08x} but got 0x{:08x}",
                    crc32, digest_crc32
                )));
            }
        }
        CheckMethod::Crc64 => {
            let crc64 = input.read_u64::<LittleEndian>()?;
            let digest_crc64 = CRC64.checksum(buf);
            if crc64 != digest_crc64 {
                return Err(error::Error::XzError(format!(
                    "Invalid block CRC64, expected 0x{:016x} but got 0x{:016x}",
                    crc64, digest_crc64
                )));
            }
        }
        // TODO
        CheckMethod::Sha256 => {
            return Err(error::Error::XzError(
                "Unsupported SHA-256 checksum (not yet implemented)".to_string(),
            ));
        }
    }
    Ok(())
}

fn decode_filter<R, W>(input: &mut R, output: &mut W, filter: &Filter) -> error::Result<usize>
where
    R: io::BufRead,
    W: io::Write,
{
    let mut count_input = util::CountBufRead::new(input);
    match filter.filter_id {
        FilterId::Lzma2 => {
            if filter.props.len() != 1 {
                return Err(error::Error::XzError(format!(
                    "Invalid properties for filter {:?}",
                    filter.filter_id
                )));
            }
            // TODO: properties??
            Lzma2Decoder::new().decompress(&mut count_input, output)?;
            Ok(count_input.count())
        }
    }
}

fn read_block_header<R>(input: &mut R, header_size: u64) -> error::Result<BlockHeader>
where
    R: io::BufRead,
{
    let flags = input.read_u8()?;
    let num_filters = (flags & 0x03) + 1;
    let reserved = flags & 0x3C;
    let has_packed_size = flags & 0x40 != 0;
    let has_unpacked_size = flags & 0x80 != 0;

    lzma_info!(
        "XZ block header: {{ header_size: {}, flags: {}, num_filters: {}, has_packed_size: {}, has_unpacked_size: {} }}",
        header_size,
        flags,
        num_filters,
        has_packed_size,
        has_unpacked_size
    );

    if reserved != 0 {
        return Err(error::Error::XzError(format!(
            "Invalid block flags {}, reserved bits (mask 0x3C) must be zero",
            flags
        )));
    }

    let packed_size = if has_packed_size {
        Some(get_multibyte(input)?)
    } else {
        None
    };

    let unpacked_size = if has_unpacked_size {
        Some(get_multibyte(input)?)
    } else {
        None
    };

    lzma_info!(
        "XZ block header: {{ packed_size: {:?}, unpacked_size: {:?} }}",
        packed_size,
        unpacked_size
    );

    let mut filters: Vec<Filter> = vec![];
    for _ in 0..num_filters {
        let filter_id = get_filter_id(get_multibyte(input)?)?;
        let size_of_properties = get_multibyte(input)?;

        lzma_info!(
            "XZ filter: {{ filter_id: {:?}, size_of_properties: {} }}",
            filter_id,
            size_of_properties
        );

        // Early abort to avoid allocating a large vector
        if size_of_properties > header_size {
            return Err(error::Error::XzError(format!(
                "Size of filter properties exceeds block header size ({} > {})",
                size_of_properties, header_size
            )));
        }

        let mut buf = vec![0; size_of_properties as usize];
        input.read_exact(buf.as_mut_slice()).map_err(|e| {
            error::Error::XzError(format!(
                "Could not read filter properties of size {}: {}",
                size_of_properties, e
            ))
        })?;

        lzma_info!("XZ filter properties: {:?}", buf);

        filters.push(Filter {
            filter_id,
            props: buf,
        })
    }

    if !util::flush_zero_padding(input)? {
        return Err(error::Error::XzError(
            "Invalid block header padding, must be null bytes".to_string(),
        ));
    }

    Ok(BlockHeader {
        filters,
        packed_size,
        unpacked_size,
    })
}

pub fn get_multibyte<R>(input: &mut R) -> error::Result<u64>
where
    R: io::Read,
{
    let mut result = 0;
    for i in 0..9 {
        let byte = input.read_u8()?;
        result ^= ((byte & 0x7F) as u64) << (i * 7);
        if (byte & 0x80) == 0 {
            return Ok(result);
        }
    }

    Err(error::Error::XzError(
        "Invalid multi-byte encoding".to_string(),
    ))
}