lib/compression: Add helper function lzxpress_huffman_max_compressed_size()
authorAndrew Bartlett <abartlet@samba.org>
Tue, 28 Mar 2023 02:42:39 +0000 (15:42 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 31 Mar 2023 01:48:30 +0000 (01:48 +0000)
This allows the calculation of the worst case to be shared with callers.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
lib/compression/lzxpress_huffman.c
lib/compression/lzxpress_huffman.h

index 3eac8e3b2b6cd89a0232b540e642bea31801ebf7..6d383e4f8cab1e5af383226c42c2372732b3b803 100644 (file)
@@ -1210,6 +1210,21 @@ static ssize_t lzx_huffman_compress_block(struct lzxhuff_compressor_context *cmp
        return bytes_written;
 }
 
+/*
+ * lzxpress_huffman_max_compressed_size()
+ *
+ * Return the most bytes the compression can take, to allow
+ * pre-allocation.
+ */
+size_t lzxpress_huffman_max_compressed_size(size_t input_size)
+{
+       /*
+        * In the worst case, the output size should be about the same as the
+        * input size, plus the 256 byte header per 64k block. We aim for
+        * ample, but within the order of magnitude.
+        */
+       return input_size + (input_size / 8) + 270;
+}
 
 /*
  * lzxpress_huffman_compress_talloc()
@@ -1236,12 +1251,8 @@ ssize_t lzxpress_huffman_compress_talloc(TALLOC_CTX *mem_ctx,
                                         uint8_t **output)
 {
        struct lzxhuff_compressor_mem *cmp = NULL;
-       /*
-        * In the worst case, the output size should be about the same as the
-        * input size, plus the 256 byte header per 64k block. We aim for
-        * ample, but within the order of magnitude.
-        */
-       size_t alloc_size = input_size + (input_size / 8) + 270;
+       size_t alloc_size = lzxpress_huffman_max_compressed_size(input_size);
+
        ssize_t output_size;
 
        *output = talloc_array(mem_ctx, uint8_t, alloc_size);
index 04de448bcce7da22b4738618acabd5c347e7afbe..232e58920f5d12b05aa9944849ad2384c95db46b 100644 (file)
@@ -83,5 +83,13 @@ uint8_t *lzxpress_huffman_decompress_talloc(TALLOC_CTX *mem_ctx,
                                            size_t input_size,
                                            size_t output_size);
 
+/*
+ * lzxpress_huffman_max_compressed_size()
+ *
+ * Return the most bytes the compression can take, to allow
+ * pre-allocation.
+ */
+size_t lzxpress_huffman_max_compressed_size(size_t input_size);
+
 
 #endif /* HAVE_LZXPRESS_HUFFMAN_H */