2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file stream_flags_encoder.c
|
2008-12-27 17:27:49 +00:00
|
|
|
/// \brief Encodes Stream Header and Stream Footer for .xz files
|
2007-12-08 22:42:33 +00: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 19:53:15 +00:00
|
|
|
#include "stream_flags_common.h"
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
static bool
|
2008-06-18 15:02:10 +00:00
|
|
|
stream_flags_encode(const lzma_stream_flags *options, uint8_t *out)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
|
|
|
if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
|
|
|
|
return true;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
out[0] = 0x00;
|
|
|
|
out[1] = options->check;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern LZMA_API lzma_ret
|
2008-06-18 15:02:10 +00:00
|
|
|
lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-06-18 15:02:10 +00:00
|
|
|
assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE
|
|
|
|
+ 4 == LZMA_STREAM_HEADER_SIZE);
|
|
|
|
|
2008-09-12 19:41:40 +00:00
|
|
|
if (options->version != 0)
|
2008-09-13 09:10:43 +00:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
2008-09-12 19:41:40 +00:00
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
// Magic
|
|
|
|
memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
|
|
|
|
|
|
|
|
// Stream Flags
|
2008-06-18 15:02:10 +00:00
|
|
|
if (stream_flags_encode(options, out + sizeof(lzma_header_magic)))
|
|
|
|
return LZMA_PROG_ERROR;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
// CRC32 of the Stream Header
|
2008-06-18 15:02:10 +00:00
|
|
|
const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic),
|
|
|
|
LZMA_STREAM_FLAGS_SIZE, 0);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
integer_write_32(out + sizeof(lzma_header_magic)
|
|
|
|
+ LZMA_STREAM_FLAGS_SIZE, crc);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern LZMA_API lzma_ret
|
2008-06-18 15:02:10 +00:00
|
|
|
lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-06-18 15:02:10 +00:00
|
|
|
assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic)
|
|
|
|
== LZMA_STREAM_HEADER_SIZE);
|
|
|
|
|
2008-09-12 19:41:40 +00:00
|
|
|
if (options->version != 0)
|
2008-09-13 09:10:43 +00:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
2008-09-12 19:41:40 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Backward Size
|
2008-09-12 19:41:40 +00:00
|
|
|
if (!is_backward_size_valid(options))
|
2008-06-18 15:02:10 +00:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
|
|
|
integer_write_32(out + 4, options->backward_size / 4 - 1);
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
// Stream Flags
|
2008-06-18 15:02:10 +00:00
|
|
|
if (stream_flags_encode(options, out + 2 * 4))
|
2007-12-08 22:42:33 +00:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// CRC32
|
|
|
|
const uint32_t crc = lzma_crc32(
|
|
|
|
out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
|
|
|
|
|
|
|
|
integer_write_32(out, crc);
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
// Magic
|
2008-06-18 15:02:10 +00:00
|
|
|
memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
|
|
|
|
lzma_footer_magic, sizeof(lzma_footer_magic));
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|