]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_flags_decoder.c
Remove bogus #includes.
[icculus/xz.git] / src / liblzma / common / stream_flags_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_flags_decoder.c
4 /// \brief      Decodes Stream Header and Stream Footer from .lzma files
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "stream_flags_common.h"
21
22
23 static bool
24 stream_flags_decode(lzma_stream_flags *options, const uint8_t *in)
25 {
26         // Reserved bits must be unset.
27         if (in[0] != 0x00 || (in[1] & 0xF0))
28                 return true;
29
30         options->check = in[1] & 0x0F;
31
32         return false;
33 }
34
35
36 extern LZMA_API lzma_ret
37 lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in)
38 {
39         // Magic
40         if (memcmp(in, lzma_header_magic, sizeof(lzma_header_magic)) != 0)
41                 return LZMA_FORMAT_ERROR;
42
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;
50
51         // Stream Flags
52         if (stream_flags_decode(options, in + sizeof(lzma_header_magic)))
53                 return LZMA_HEADER_ERROR;
54
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;
60
61         return LZMA_OK;
62 }
63
64
65 extern LZMA_API lzma_ret
66 lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in)
67 {
68         // Magic
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;
72
73         // CRC32
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;
78
79         // Stream Flags
80         if (stream_flags_decode(options, in + sizeof(uint32_t) * 2))
81                 return LZMA_HEADER_ERROR;
82
83         // Backward Size
84         options->backward_size = integer_read_32(in + sizeof(uint32_t));
85         options->backward_size = (options->backward_size + 1) * 4;
86
87         return LZMA_OK;
88 }