2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file stream_flags_decoder.c
|
2008-06-18 17:02:10 +02:00
|
|
|
/// \brief Decodes Stream Header and Stream Footer from .lzma files
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
// Copyright (C) 2007 Lasse Collin
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
#include "stream_flags_common.h"
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
static bool
|
2008-06-18 17:02:10 +02:00
|
|
|
stream_flags_decode(lzma_stream_flags *options, const uint8_t *in)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
// Reserved bits must be unset.
|
2008-06-18 17:02:10 +02:00
|
|
|
if (in[0] != 0x00 || (in[1] & 0xF0))
|
2007-12-08 23:42:33 +01:00
|
|
|
return true;
|
|
|
|
|
2008-09-12 21:41:40 +02:00
|
|
|
options->version = 0;
|
2008-06-18 17:02:10 +02:00
|
|
|
options->check = in[1] & 0x0F;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern LZMA_API lzma_ret
|
2008-06-18 17:02:10 +02:00
|
|
|
lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
// Magic
|
|
|
|
if (memcmp(in, lzma_header_magic, sizeof(lzma_header_magic)) != 0)
|
|
|
|
return LZMA_FORMAT_ERROR;
|
|
|
|
|
|
|
|
// Verify the CRC32 so we can distinguish between corrupt
|
|
|
|
// and unsupported files.
|
|
|
|
const uint32_t crc = lzma_crc32(in + sizeof(lzma_header_magic),
|
|
|
|
LZMA_STREAM_FLAGS_SIZE, 0);
|
|
|
|
if (crc != integer_read_32(in + sizeof(lzma_header_magic)
|
|
|
|
+ LZMA_STREAM_FLAGS_SIZE))
|
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
|
|
|
// Stream Flags
|
|
|
|
if (stream_flags_decode(options, in + sizeof(lzma_header_magic)))
|
|
|
|
return LZMA_HEADER_ERROR;
|
|
|
|
|
|
|
|
// Set Backward Size to indicate unknown value. That way
|
2008-09-12 21:41:40 +02:00
|
|
|
// lzma_stream_flags_compare() can be used to compare Stream Header
|
2008-06-18 17:02:10 +02:00
|
|
|
// and Stream Footer while keeping it useful also for comparing
|
|
|
|
// two Stream Footers.
|
|
|
|
options->backward_size = LZMA_VLI_VALUE_UNKNOWN;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern LZMA_API lzma_ret
|
2008-06-18 17:02:10 +02:00
|
|
|
lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
// Magic
|
|
|
|
if (memcmp(in + sizeof(uint32_t) * 2 + LZMA_STREAM_FLAGS_SIZE,
|
|
|
|
lzma_footer_magic, sizeof(lzma_footer_magic)) != 0)
|
|
|
|
return LZMA_FORMAT_ERROR;
|
|
|
|
|
|
|
|
// CRC32
|
|
|
|
const uint32_t crc = lzma_crc32(in + sizeof(uint32_t),
|
|
|
|
sizeof(uint32_t) + LZMA_STREAM_FLAGS_SIZE, 0);
|
|
|
|
if (crc != integer_read_32(in))
|
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
|
|
|
// Stream Flags
|
|
|
|
if (stream_flags_decode(options, in + sizeof(uint32_t) * 2))
|
|
|
|
return LZMA_HEADER_ERROR;
|
|
|
|
|
|
|
|
// Backward Size
|
|
|
|
options->backward_size = integer_read_32(in + sizeof(uint32_t));
|
|
|
|
options->backward_size = (options->backward_size + 1) * 4;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|