2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file block_header_encoder.c
|
2008-12-27 18:27:49 +01:00
|
|
|
/// \brief Encodes Block Header for .xz files
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "check.h"
|
|
|
|
|
|
|
|
|
2009-02-02 19:14:03 +01:00
|
|
|
extern LZMA_API(lzma_ret)
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block_header_size(lzma_block *block)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2014-08-05 21:03:30 +02:00
|
|
|
if (block->version > 1)
|
2008-12-27 18:27:49 +01:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Block Header Size + Block Flags + CRC32.
|
2008-11-19 19:46:52 +01:00
|
|
|
uint32_t size = 1 + 1 + 4;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// Compressed Size
|
2008-12-27 18:27:49 +01:00
|
|
|
if (block->compressed_size != LZMA_VLI_UNKNOWN) {
|
|
|
|
const uint32_t add = lzma_vli_size(block->compressed_size);
|
|
|
|
if (add == 0 || block->compressed_size == 0)
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
size += add;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uncompressed Size
|
2008-12-27 18:27:49 +01:00
|
|
|
if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
|
|
|
|
const uint32_t add = lzma_vli_size(block->uncompressed_size);
|
2008-06-18 17:02:10 +02:00
|
|
|
if (add == 0)
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
size += add;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List of Filter Flags
|
2008-12-27 18:27:49 +01:00
|
|
|
if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
for (size_t i = 0; block->filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
|
2007-12-08 23:42:33 +01:00
|
|
|
// Don't allow too many filters.
|
2008-11-19 19:46:52 +01:00
|
|
|
if (i == LZMA_FILTERS_MAX)
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
uint32_t add;
|
|
|
|
return_if_error(lzma_filter_flags_size(&add,
|
2008-12-27 18:27:49 +01:00
|
|
|
block->filters + i));
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
size += add;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Pad to a multiple of four bytes.
|
2008-12-27 18:27:49 +01:00
|
|
|
block->header_size = (size + 3) & ~UINT32_C(3);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// NOTE: We don't verify that the encoded size of the Block stays
|
|
|
|
// within limits. This is because it is possible that we are called
|
|
|
|
// with exaggerated Compressed Size (e.g. LZMA_VLI_MAX) to reserve
|
|
|
|
// space for Block Header, and later called again with lower,
|
|
|
|
// real values.
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-02 19:14:03 +01:00
|
|
|
extern LZMA_API(lzma_ret)
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block_header_encode(const lzma_block *block, uint8_t *out)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2010-02-12 12:16:15 +01:00
|
|
|
// Validate everything but filters.
|
2008-12-27 18:27:49 +01:00
|
|
|
if (lzma_block_unpadded_size(block) == 0
|
|
|
|
|| !lzma_vli_is_valid(block->uncompressed_size))
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Indicate the size of the buffer _excluding_ the CRC32 field.
|
2008-12-27 18:27:49 +01:00
|
|
|
const size_t out_size = block->header_size - 4;
|
2008-06-18 17:02:10 +02:00
|
|
|
|
|
|
|
// Store the Block Header Size.
|
|
|
|
out[0] = out_size / 4;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// We write Block Flags in pieces.
|
|
|
|
out[1] = 0x00;
|
2007-12-08 23:42:33 +01:00
|
|
|
size_t out_pos = 2;
|
|
|
|
|
|
|
|
// Compressed Size
|
2008-12-27 18:27:49 +01:00
|
|
|
if (block->compressed_size != LZMA_VLI_UNKNOWN) {
|
|
|
|
return_if_error(lzma_vli_encode(block->compressed_size, NULL,
|
2008-06-18 17:02:10 +02:00
|
|
|
out, &out_pos, out_size));
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
out[1] |= 0x40;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uncompressed Size
|
2008-12-27 18:27:49 +01:00
|
|
|
if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
|
|
|
|
return_if_error(lzma_vli_encode(block->uncompressed_size, NULL,
|
2008-06-18 17:02:10 +02:00
|
|
|
out, &out_pos, out_size));
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
out[1] |= 0x80;
|
|
|
|
}
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
// Filter Flags
|
2008-12-27 18:27:49 +01:00
|
|
|
if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
size_t filter_count = 0;
|
|
|
|
do {
|
2009-09-12 13:07:36 +02:00
|
|
|
// There can be a maximum of four filters.
|
2008-11-19 19:46:52 +01:00
|
|
|
if (filter_count == LZMA_FILTERS_MAX)
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_PROG_ERROR;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
return_if_error(lzma_filter_flags_encode(
|
2008-12-27 18:27:49 +01:00
|
|
|
block->filters + filter_count,
|
2008-08-28 21:53:15 +02:00
|
|
|
out, &out_pos, out_size));
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
} while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
out[1] |= filter_count - 1;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Padding
|
|
|
|
memzero(out + out_pos, out_size - out_pos);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// CRC32
|
2019-12-31 21:41:45 +01:00
|
|
|
write32le(out + out_size, lzma_crc32(out, out_size, 0));
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|