aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/common/block_encoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/liblzma/common/block_encoder.c')
-rw-r--r--src/liblzma/common/block_encoder.c228
1 files changed, 51 insertions, 177 deletions
diff --git a/src/liblzma/common/block_encoder.c b/src/liblzma/common/block_encoder.c
index 78185790..3add45a9 100644
--- a/src/liblzma/common/block_encoder.c
+++ b/src/liblzma/common/block_encoder.c
@@ -34,37 +34,21 @@ struct lzma_coder_s {
enum {
SEQ_CODE,
- SEQ_CHECK_FINISH,
- SEQ_CHECK_COPY,
- SEQ_UNCOMPRESSED_SIZE,
- SEQ_BACKWARD_SIZE,
SEQ_PADDING,
+ SEQ_CHECK,
} sequence;
- /// Position in .header and .check.
- size_t pos;
-
- /// Check of the uncompressed data
- lzma_check check;
-
- /// Total Size calculated while encoding
- lzma_vli total_size;
-
/// Compressed Size calculated while encoding
lzma_vli compressed_size;
/// Uncompressed Size calculated while encoding
lzma_vli uncompressed_size;
- /// Maximum allowed total_size
- lzma_vli total_limit;
+ /// Position when writing out the Check field
+ size_t check_pos;
- /// Maximum allowed uncompressed_size
- lzma_vli uncompressed_limit;
-
- /// Backward Size - This is a copy of total_size right before
- /// the Backward Size field.
- lzma_vli backward_size;
+ /// Check of the uncompressed data
+ lzma_check check;
};
@@ -80,16 +64,16 @@ block_encode(lzma_coder *coder, lzma_allocator *allocator,
if (coder->options->uncompressed_size
- coder->uncompressed_size
!= (lzma_vli)(in_size - *in_pos))
- return LZMA_DATA_ERROR;
+ return LZMA_PROG_ERROR;
} else {
if (coder->options->uncompressed_size
- coder->uncompressed_size
< (lzma_vli)(in_size - *in_pos))
- return LZMA_DATA_ERROR;
+ return LZMA_PROG_ERROR;
}
} else if (LZMA_VLI_VALUE_MAX - coder->uncompressed_size
< (lzma_vli)(in_size - *in_pos)) {
- return LZMA_DATA_ERROR;
+ return LZMA_PROG_ERROR;
}
// Main loop
@@ -107,11 +91,10 @@ block_encode(lzma_coder *coder, lzma_allocator *allocator,
const size_t in_used = *in_pos - in_start;
const size_t out_used = *out_pos - out_start;
- if (update_size(&coder->total_size, out_used,
- coder->total_limit)
- || update_size(&coder->compressed_size,
- out_used,
- coder->options->compressed_size))
+ // FIXME We must also check that Total Size doesn't get
+ // too big.
+ if (update_size(&coder->compressed_size, out_used,
+ coder->options->compressed_size))
return LZMA_DATA_ERROR;
// No need to check for overflow because we have already
@@ -125,141 +108,54 @@ block_encode(lzma_coder *coder, lzma_allocator *allocator,
return ret;
assert(*in_pos == in_size);
+ coder->sequence = SEQ_PADDING;
+ break;
+ }
+
+ case SEQ_PADDING:
+ // Pad Compressed Data to a multiple of four bytes.
+ if (coder->compressed_size & 3) {
+ out[*out_pos] = 0x00;
+ ++*out_pos;
+
+ if (update_size(&coder->compressed_size, 1,
+ coder->options->compressed_size))
+ return LZMA_DATA_ERROR;
+
+ break;
+ }
// Compressed and Uncompressed Sizes are now at their final
- // values. Verify that they match the values give to us.
+ // values. Verify that they match the values given to us.
if (!is_size_valid(coder->compressed_size,
coder->options->compressed_size)
|| !is_size_valid(coder->uncompressed_size,
coder->options->uncompressed_size))
return LZMA_DATA_ERROR;
- coder->sequence = SEQ_CHECK_FINISH;
- break;
- }
+ // Copy the values into coder->options. The caller
+ // may use this information to construct Index.
+ coder->options->compressed_size = coder->compressed_size;
+ coder->options->uncompressed_size = coder->uncompressed_size;
- case SEQ_CHECK_FINISH:
- if (coder->options->check == LZMA_CHECK_NONE) {
- coder->sequence = SEQ_UNCOMPRESSED_SIZE;
- break;
- }
+ if (coder->options->check == LZMA_CHECK_NONE)
+ return LZMA_STREAM_END;
lzma_check_finish(&coder->check, coder->options->check);
- coder->sequence = SEQ_CHECK_COPY;
+ coder->sequence = SEQ_CHECK;
// Fall through
- case SEQ_CHECK_COPY:
- assert(lzma_check_sizes[coder->options->check] > 0);
-
- switch (coder->options->check) {
- case LZMA_CHECK_CRC32:
- out[*out_pos] = coder->check.crc32 >> (coder->pos * 8);
- break;
-
- case LZMA_CHECK_CRC64:
- out[*out_pos] = coder->check.crc64 >> (coder->pos * 8);
- break;
-
- case LZMA_CHECK_SHA256:
- out[*out_pos] = coder->check.sha256.buffer[coder->pos];
- break;
-
- default:
- assert(0);
- return LZMA_PROG_ERROR;
- }
-
+ case SEQ_CHECK:
+ out[*out_pos] = coder->check.buffer[coder->check_pos];
++*out_pos;
- if (update_size(&coder->total_size, 1, coder->total_limit))
- return LZMA_DATA_ERROR;
-
- if (++coder->pos == lzma_check_sizes[coder->options->check]) {
- coder->pos = 0;
- coder->sequence = SEQ_UNCOMPRESSED_SIZE;
- }
-
- break;
-
- case SEQ_UNCOMPRESSED_SIZE:
- if (coder->options->has_uncompressed_size_in_footer) {
- const size_t out_start = *out_pos;
-
- const lzma_ret ret = lzma_vli_encode(
- coder->uncompressed_size,
- &coder->pos, 1,
- out, out_pos, out_size);
-
- // Updating the size this way instead of doing in a
- // single chunk using lzma_vli_size(), because this
- // way we detect when exactly we are going out of
- // our limits.
- if (update_size(&coder->total_size,
- *out_pos - out_start,
- coder->total_limit))
- return LZMA_DATA_ERROR;
-
- if (ret != LZMA_STREAM_END)
- return ret;
-
- coder->pos = 0;
- }
+ if (++coder->check_pos
+ == lzma_check_sizes[coder->options->check])
+ return LZMA_STREAM_END;
- coder->backward_size = coder->total_size;
- coder->sequence = SEQ_BACKWARD_SIZE;
break;
- case SEQ_BACKWARD_SIZE:
- if (coder->options->has_backward_size) {
- const size_t out_start = *out_pos;
-
- const lzma_ret ret = lzma_vli_encode(
- coder->backward_size, &coder->pos, 1,
- out, out_pos, out_size);
-
- if (update_size(&coder->total_size,
- *out_pos - out_start,
- coder->total_limit))
- return LZMA_DATA_ERROR;
-
- if (ret != LZMA_STREAM_END)
- return ret;
- }
-
- coder->sequence = SEQ_PADDING;
- break;
-
- case SEQ_PADDING:
- if (coder->options->handle_padding) {
- assert(coder->options->total_size
- != LZMA_VLI_VALUE_UNKNOWN);
-
- if (coder->total_size < coder->options->total_size) {
- out[*out_pos] = 0x00;
- ++*out_pos;
-
- if (update_size(&coder->total_size, 1,
- coder->total_limit))
- return LZMA_DATA_ERROR;
-
- break;
- }
- }
-
- // Now also Total Size is known. Verify it.
- if (!is_size_valid(coder->total_size,
- coder->options->total_size))
- return LZMA_DATA_ERROR;
-
- // Copy the values into coder->options. The caller
- // may use this information to construct Index.
- coder->options->total_size = coder->total_size;
- coder->options->compressed_size = coder->compressed_size;
- coder->options->uncompressed_size = coder->uncompressed_size;
-
- return LZMA_STREAM_END;
-
default:
return LZMA_PROG_ERROR;
}
@@ -281,10 +177,9 @@ static lzma_ret
block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
lzma_options_block *options)
{
- // Validate some options.
- if (validate_options_1(options) || validate_options_2(options)
- || (options->handle_padding && options->total_size
- == LZMA_VLI_VALUE_UNKNOWN))
+ // While lzma_block_total_size_get() is meant to calculate the Total
+ // Size, it also validates the options excluding the filters.
+ if (lzma_block_total_size_get(options) == 0)
return LZMA_PROG_ERROR;
// Allocate and initialize *next->coder if needed.
@@ -298,40 +193,19 @@ block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
next->coder->next = LZMA_NEXT_CODER_INIT;
}
- // Initialize the check.
- return_if_error(lzma_check_init(&next->coder->check, options->check));
-
- // If End of Payload Marker is not used and Uncompressed Size is zero,
- // Compressed Data is empty. That is, we don't call the encoder at all.
- // We initialize it though; it allows detecting invalid options.
- if (!options->has_eopm && options->uncompressed_size == 0) {
- // Also Compressed Size must be zero if it has been
- // given to us.
- if (!is_size_valid(0, options->compressed_size))
- return LZMA_PROG_ERROR;
-
- next->coder->sequence = SEQ_CHECK_FINISH;
- } else {
- next->coder->sequence = SEQ_CODE;
- }
-
- // Other initializations
+ // Basic initializations
+ next->coder->sequence = SEQ_CODE;
next->coder->options = options;
- next->coder->pos = 0;
- next->coder->total_size = options->header_size;
next->coder->compressed_size = 0;
next->coder->uncompressed_size = 0;
- next->coder->total_limit
- = MIN(options->total_size, options->total_limit);
- next->coder->uncompressed_limit = MIN(options->uncompressed_size,
- options->uncompressed_limit);
+
+ // Initialize the check
+ next->coder->check_pos = 0;
+ return_if_error(lzma_check_init(&next->coder->check, options->check));
// Initialize the requested filters.
return lzma_raw_encoder_init(&next->coder->next, allocator,
- options->filters, options->has_eopm
- ? LZMA_VLI_VALUE_UNKNOWN
- : options->uncompressed_size,
- true);
+ options->filters);
}