1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_flags_decoder.c
4 /// \brief Decodes Stream Header and Stream Footer from .xz files
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "stream_flags_common.h"
17 stream_flags_decode(lzma_stream_flags *options, const uint8_t *in)
19 // Reserved bits must be unset.
20 if (in[0] != 0x00 || (in[1] & 0xF0))
24 options->check = in[1] & 0x0F;
30 extern LZMA_API(lzma_ret)
31 lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in)
34 if (memcmp(in, lzma_header_magic, sizeof(lzma_header_magic)) != 0)
35 return LZMA_FORMAT_ERROR;
37 // Verify the CRC32 so we can distinguish between corrupt
38 // and unsupported files.
39 const uint32_t crc = lzma_crc32(in + sizeof(lzma_header_magic),
40 LZMA_STREAM_FLAGS_SIZE, 0);
41 if (crc != unaligned_read32le(in + sizeof(lzma_header_magic)
42 + LZMA_STREAM_FLAGS_SIZE))
43 return LZMA_DATA_ERROR;
46 if (stream_flags_decode(options, in + sizeof(lzma_header_magic)))
47 return LZMA_OPTIONS_ERROR;
49 // Set Backward Size to indicate unknown value. That way
50 // lzma_stream_flags_compare() can be used to compare Stream Header
51 // and Stream Footer while keeping it useful also for comparing
52 // two Stream Footers.
53 options->backward_size = LZMA_VLI_UNKNOWN;
59 extern LZMA_API(lzma_ret)
60 lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in)
63 if (memcmp(in + sizeof(uint32_t) * 2 + LZMA_STREAM_FLAGS_SIZE,
64 lzma_footer_magic, sizeof(lzma_footer_magic)) != 0)
65 return LZMA_FORMAT_ERROR;
68 const uint32_t crc = lzma_crc32(in + sizeof(uint32_t),
69 sizeof(uint32_t) + LZMA_STREAM_FLAGS_SIZE, 0);
70 if (crc != unaligned_read32le(in))
71 return LZMA_DATA_ERROR;
74 if (stream_flags_decode(options, in + sizeof(uint32_t) * 2))
75 return LZMA_OPTIONS_ERROR;
78 options->backward_size = unaligned_read32le(in + sizeof(uint32_t));
79 options->backward_size = (options->backward_size + 1) * 4;