1
0
Fork 0
mirror of https://git.tukaani.org/xz.git synced 2024-04-04 12:36:23 +02:00

Stream decoder cleanups

This commit is contained in:
Lasse Collin 2008-09-02 14:56:52 +03:00
parent fc68165745
commit 80c4158f19

View file

@ -66,9 +66,9 @@ struct lzma_coder_s {
/// If true, we will decode concatenated Streams that possibly have /// If true, we will decode concatenated Streams that possibly have
/// Stream Padding between or after them. LZMA_STREAM_END is returned /// Stream Padding between or after them. LZMA_STREAM_END is returned
/// once the application isn't giving us any new input and we aren't /// once the application isn't giving us any new input, and we aren't
/// in the middle of a Stream and possible Stream Padding is a /// in the middle of a Stream, and possible Stream Padding is a
/// multiple of four bytes. FIXME /// multiple of four bytes.
bool concatenated; bool concatenated;
/// When decoding concatenated Streams, this is true as long as we /// When decoding concatenated Streams, this is true as long as we
@ -152,10 +152,10 @@ stream_decode(lzma_coder *coder, lzma_allocator *allocator,
if (coder->tell_check) if (coder->tell_check)
return LZMA_SEE_CHECK; return LZMA_SEE_CHECK;
break;
} }
// Fall through
case SEQ_BLOCK_HEADER: { case SEQ_BLOCK_HEADER: {
if (*in_pos >= in_size) if (*in_pos >= in_size)
return LZMA_OK; return LZMA_OK;
@ -225,9 +225,10 @@ stream_decode(lzma_coder *coder, lzma_allocator *allocator,
return ret; return ret;
coder->sequence = SEQ_BLOCK; coder->sequence = SEQ_BLOCK;
break;
} }
// Fall through
case SEQ_BLOCK: { case SEQ_BLOCK: {
const lzma_ret ret = coder->block_decoder.code( const lzma_ret ret = coder->block_decoder.code(
coder->block_decoder.coder, allocator, coder->block_decoder.coder, allocator,
@ -263,9 +264,10 @@ stream_decode(lzma_coder *coder, lzma_allocator *allocator,
return ret; return ret;
coder->sequence = SEQ_STREAM_FOOTER; coder->sequence = SEQ_STREAM_FOOTER;
break;
} }
// Fall through
case SEQ_STREAM_FOOTER: case SEQ_STREAM_FOOTER:
// Copy the Stream Footer to the internal buffer. // Copy the Stream Footer to the internal buffer.
lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos, lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
@ -277,11 +279,15 @@ stream_decode(lzma_coder *coder, lzma_allocator *allocator,
coder->pos = 0; coder->pos = 0;
// Decode the Stream Footer. // Decode the Stream Footer. The decoder gives
// FIXME LZMA_FORMAT_ERROR doesn't make sense here. // LZMA_FORMAT_ERROR if the magic bytes don't match,
// so convert that return code to LZMA_DATA_ERROR.
lzma_stream_flags footer_flags; lzma_stream_flags footer_flags;
return_if_error(lzma_stream_footer_decode( const lzma_ret ret = lzma_stream_footer_decode(
&footer_flags, coder->buffer)); &footer_flags, coder->buffer);
if (ret != LZMA_OK)
return ret == LZMA_FORMAT_ERROR
? LZMA_DATA_ERROR : ret;
// Check that Index Size stored in the Stream Footer matches // Check that Index Size stored in the Stream Footer matches
// the real size of the Index field. // the real size of the Index field.
@ -299,11 +305,13 @@ stream_decode(lzma_coder *coder, lzma_allocator *allocator,
return LZMA_STREAM_END; return LZMA_STREAM_END;
coder->sequence = SEQ_STREAM_PADDING; coder->sequence = SEQ_STREAM_PADDING;
break;
// Fall through
case SEQ_STREAM_PADDING: case SEQ_STREAM_PADDING:
assert(coder->concatenated); assert(coder->concatenated);
// Skip over possible Stream Padding.
while (true) { while (true) {
if (*in_pos >= in_size) { if (*in_pos >= in_size) {
// Unless LZMA_FINISH was used, we cannot // Unless LZMA_FINISH was used, we cannot
@ -318,24 +326,24 @@ stream_decode(lzma_coder *coder, lzma_allocator *allocator,
: LZMA_DATA_ERROR; : LZMA_DATA_ERROR;
} }
if (in[*in_pos] != 0x00) { // If the byte is not zero, it probably indicates
if (coder->pos != 0) { // beginning of a new Stream (or the file is corrupt).
// Stream Padding is not a multiple of if (in[*in_pos] != 0x00)
// four bytes.
++*in_pos;
return LZMA_DATA_ERROR;
}
// Prepare to decode the next Stream.
return_if_error(stream_decoder_reset(
coder, allocator));
break; break;
}
++*in_pos; ++*in_pos;
coder->pos = (coder->pos + 1) & 3; coder->pos = (coder->pos + 1) & 3;
} }
// Stream Padding must be a multiple of four bytes (empty
// Stream Padding is OK).
if (coder->pos != 0) {
++*in_pos;
return LZMA_DATA_ERROR;
}
// Prepare to decode the next Stream.
return_if_error(stream_decoder_reset(coder, allocator));
break; break;
default: default:
@ -404,7 +412,6 @@ lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags); lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
strm->internal->supported_actions[LZMA_RUN] = true; strm->internal->supported_actions[LZMA_RUN] = true;
// strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true; // FIXME
strm->internal->supported_actions[LZMA_FINISH] = true; strm->internal->supported_actions[LZMA_FINISH] = true;
return LZMA_OK; return LZMA_OK;