lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out) { assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic) == LZMA_STREAM_HEADER_SIZE); if (options->version != 0) return LZMA_OPTIONS_ERROR; // Backward Size if (!is_backward_size_valid(options)) return LZMA_PROG_ERROR; unaligned_write32le(out + 4, options->backward_size / 4 - 1); // Stream Flags if (stream_flags_encode(options, out + 2 * 4)) return LZMA_PROG_ERROR; // CRC32 const uint32_t crc = lzma_crc32( out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0); unaligned_write32le(out, crc); // Magic memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE, lzma_footer_magic, sizeof(lzma_footer_magic)); return LZMA_OK; }
lzma_block_header_encode(const lzma_block *block, uint8_t *out) { // Validate everything but filters. if (lzma_block_unpadded_size(block) == 0 || !lzma_vli_is_valid(block->uncompressed_size)) return LZMA_PROG_ERROR; // Indicate the size of the buffer _excluding_ the CRC32 field. const size_t out_size = block->header_size - 4; // Store the Block Header Size. out[0] = out_size / 4; // We write Block Flags in pieces. out[1] = 0x00; size_t out_pos = 2; // Compressed Size if (block->compressed_size != LZMA_VLI_UNKNOWN) { return_if_error(lzma_vli_encode(block->compressed_size, NULL, out, &out_pos, out_size)); out[1] |= 0x40; } // Uncompressed Size if (block->uncompressed_size != LZMA_VLI_UNKNOWN) { return_if_error(lzma_vli_encode(block->uncompressed_size, NULL, out, &out_pos, out_size)); out[1] |= 0x80; } // Filter Flags if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN) return LZMA_PROG_ERROR; size_t filter_count = 0; do { // There can be a maximum of four filters. if (filter_count == LZMA_FILTERS_MAX) return LZMA_PROG_ERROR; return_if_error(lzma_filter_flags_encode( block->filters + filter_count, out, &out_pos, out_size)); } while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN); out[1] |= filter_count - 1; // Padding memzero(out + out_pos, out_size - out_pos); // CRC32 unaligned_write32le(out + out_size, lzma_crc32(out, out_size, 0)); return LZMA_OK; }
static void test_decode_invalid(void) { known_flags.check = LZMA_CHECK_NONE; known_flags.backward_size = 1024; expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK); // Test 1 (invalid Magic Bytes) buffer[5] ^= 1; succeed(test_header_decoder(LZMA_FORMAT_ERROR)); buffer[5] ^= 1; // Test 2a (valid CRC32) uint32_t crc = lzma_crc32(buffer + 6, 2, 0); unaligned_write32le(buffer + 8, crc); succeed(test_header_decoder(LZMA_OK)); // Test 2b (invalid Stream Flags with valid CRC32) buffer[6] ^= 0x20; crc = lzma_crc32(buffer + 6, 2, 0); unaligned_write32le(buffer + 8, crc); succeed(test_header_decoder(LZMA_OPTIONS_ERROR)); // Test 3 (invalid CRC32) expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK); buffer[9] ^= 1; succeed(test_header_decoder(LZMA_DATA_ERROR)); // Test 4 (invalid Stream Flags with valid CRC32) expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK); buffer[9] ^= 0x40; crc = lzma_crc32(buffer + 4, 6, 0); unaligned_write32le(buffer, crc); succeed(test_footer_decoder(LZMA_OPTIONS_ERROR)); // Test 5 (invalid Magic Bytes) expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK); buffer[11] ^= 1; succeed(test_footer_decoder(LZMA_FORMAT_ERROR)); }
extern lzma_ret lzma_simple_props_encode(const void *options, uint8_t *out) { const lzma_options_bcj *const opt = options; // The default start offset is zero, so we don't need to store any // options unless the start offset is non-zero. if (opt == NULL || opt->start_offset == 0) return LZMA_OK; unaligned_write32le(out, opt->start_offset); return LZMA_OK; }
lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out) { assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE + 4 == LZMA_STREAM_HEADER_SIZE); if (options->version != 0) return LZMA_OPTIONS_ERROR; // Magic memcpy(out, lzma_header_magic, sizeof(lzma_header_magic)); // Stream Flags if (stream_flags_encode(options, out + sizeof(lzma_header_magic))) return LZMA_PROG_ERROR; // CRC32 of the Stream Header const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic), LZMA_STREAM_FLAGS_SIZE, 0); unaligned_write32le(out + sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE, crc); return LZMA_OK; }