1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_flags_decoder.c
4 /// \brief Decodes Stream Header and Stream Footer from .lzma files
6 // Copyright (C) 2007 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "stream_common.h"
24 stream_flags_decode(lzma_stream_flags *options, const uint8_t *in)
26 // Reserved bits must be unset.
27 if (in[0] != 0x00 || (in[1] & 0xF0))
30 options->check = in[1] & 0x0F;
36 extern LZMA_API lzma_ret
37 lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in)
40 if (memcmp(in, lzma_header_magic, sizeof(lzma_header_magic)) != 0)
41 return LZMA_FORMAT_ERROR;
43 // Verify the CRC32 so we can distinguish between corrupt
44 // and unsupported files.
45 const uint32_t crc = lzma_crc32(in + sizeof(lzma_header_magic),
46 LZMA_STREAM_FLAGS_SIZE, 0);
47 if (crc != integer_read_32(in + sizeof(lzma_header_magic)
48 + LZMA_STREAM_FLAGS_SIZE))
49 return LZMA_DATA_ERROR;
52 if (stream_flags_decode(options, in + sizeof(lzma_header_magic)))
53 return LZMA_HEADER_ERROR;
55 // Set Backward Size to indicate unknown value. That way
56 // lzma_stream_flags_equal can be used to compare Stream Header
57 // and Stream Footer while keeping it useful also for comparing
58 // two Stream Footers.
59 options->backward_size = LZMA_VLI_VALUE_UNKNOWN;
65 extern LZMA_API lzma_ret
66 lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in)
69 if (memcmp(in + sizeof(uint32_t) * 2 + LZMA_STREAM_FLAGS_SIZE,
70 lzma_footer_magic, sizeof(lzma_footer_magic)) != 0)
71 return LZMA_FORMAT_ERROR;
74 const uint32_t crc = lzma_crc32(in + sizeof(uint32_t),
75 sizeof(uint32_t) + LZMA_STREAM_FLAGS_SIZE, 0);
76 if (crc != integer_read_32(in))
77 return LZMA_DATA_ERROR;
80 if (stream_flags_decode(options, in + sizeof(uint32_t) * 2))
81 return LZMA_HEADER_ERROR;
84 options->backward_size = integer_read_32(in + sizeof(uint32_t));
85 options->backward_size = (options->backward_size + 1) * 4;