aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/common/block_private.h
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2008-01-25 23:12:36 +0200
committerLasse Collin <lasse.collin@tukaani.org>2008-01-25 23:12:36 +0200
commit4441e004185cd4c61bda184010eca5924c9dec87 (patch)
treecaa5306e1d0807bdeca57ca2d7e159360405702f /src/liblzma/common/block_private.h
parentAdded test_memlimit.c. (diff)
downloadxz-4441e004185cd4c61bda184010eca5924c9dec87.tar.xz
Combine lzma_options_block validation needed by both Block
encoder and decoder, and put the shared things to block_private.h. Improved the checks a little so that they may detect too big Compressed Size at initialization time if lzma_options_block.total_size or .total_limit is known. Allow encoding and decoding Blocks with combinations of fields that are not allowed by the file format specification. Doing this requires that the application passes such a combination in lzma_options_lzma; liblzma doesn't do that, but it's not impossible that someone could find them useful in some custom file format.
Diffstat (limited to 'src/liblzma/common/block_private.h')
-rw-r--r--src/liblzma/common/block_private.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/liblzma/common/block_private.h b/src/liblzma/common/block_private.h
index 8e2db319..16d95b9f 100644
--- a/src/liblzma/common/block_private.h
+++ b/src/liblzma/common/block_private.h
@@ -43,4 +43,54 @@ is_size_valid(lzma_vli size, lzma_vli reference)
return reference == LZMA_VLI_VALUE_UNKNOWN || reference == size;
}
+
+/// If any of these tests fail, the caller has to return LZMA_PROG_ERROR.
+static inline bool
+validate_options_1(const lzma_options_block *options)
+{
+ return options == NULL
+ || !lzma_vli_is_valid(options->compressed_size)
+ || !lzma_vli_is_valid(options->uncompressed_size)
+ || !lzma_vli_is_valid(options->total_size)
+ || !lzma_vli_is_valid(options->total_limit)
+ || !lzma_vli_is_valid(options->uncompressed_limit);
+}
+
+
+/// If any of these tests fail, the encoder has to return LZMA_PROG_ERROR
+/// because something is going horribly wrong if such values get passed
+/// to the encoder. In contrast, the decoder has to return LZMA_DATA_ERROR,
+/// since these tests failing indicate that something is wrong in the Stream.
+static inline bool
+validate_options_2(const lzma_options_block *options)
+{
+ if ((options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN
+ && options->uncompressed_size
+ > options->uncompressed_limit)
+ || (options->total_size != LZMA_VLI_VALUE_UNKNOWN
+ && options->total_size
+ > options->total_limit)
+ || (!options->has_eopm && options->uncompressed_size
+ == LZMA_VLI_VALUE_UNKNOWN)
+ || options->header_size > options->total_size)
+ return true;
+
+ if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN) {
+ // Calculate a rough minimum possible valid Total Size of
+ // this Block, and check that total_size and total_limit
+ // are big enough. Note that the real minimum size can be
+ // bigger due to the Check, Uncompressed Size, Backwards
+ // Size, pr Padding being present. A rough check here is
+ // enough for us to catch the most obvious errors as early
+ // as possible.
+ const lzma_vli total_min = options->compressed_size
+ + (lzma_vli)(options->header_size);
+ if (total_min > options->total_size
+ || total_min > options->total_limit)
+ return true;
+ }
+
+ return false;
+}
+
#endif