From 92d96f9e2db42196a778b727cfa91d18a5cc6f49 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Thu, 28 Mar 2013 11:55:53 -0500 Subject: [PATCH] Export compression and decompression functions --- programs/imagex.c | 2 +- src/lzx-compress.c | 25 +++++-------- src/lzx-decompress.c | 26 +++----------- src/lzx.h | 8 ----- src/resource.c | 6 ++-- src/wimlib.h | 77 +++++++++++++++++++++++++++++++++++++++-- src/write.c | 12 +++---- src/xpress-compress.c | 22 +++++------- src/xpress-decompress.c | 7 ++-- src/xpress.h | 8 ----- 10 files changed, 109 insertions(+), 84 deletions(-) diff --git a/programs/imagex.c b/programs/imagex.c index ffc841f6..d9ac676f 100644 --- a/programs/imagex.c +++ b/programs/imagex.c @@ -23,8 +23,8 @@ */ #include "config.h" -#include "wimlib_tchar.h" #include "wimlib.h" +#include "wimlib_tchar.h" #include #include diff --git a/src/lzx-compress.c b/src/lzx-compress.c index eb84d0ce..a811d51c 100644 --- a/src/lzx-compress.c +++ b/src/lzx-compress.c @@ -27,8 +27,8 @@ /* - * This file provides lzx_compress(), a function to compress an in-memory buffer - * of data using LZX compression, as used in the WIM file format. + * This file provides wimlib_lzx_compress(), a function to compress an in-memory + * buffer of data using LZX compression, as used in the WIM file format. * * Please see the comments in lzx-decompress.c for more information about this * compression format. @@ -57,6 +57,7 @@ * blocks from one input chunk is not yet implemented. */ +#include "wimlib.h" #include "lzx.h" #include "compress.h" #include @@ -638,18 +639,10 @@ static const struct lz_params lzx_lz_params = { .too_far = 4096, }; -/* - * Performs LZX compression on a block of data. - * - * Please see the documentation for the 'compress_func_t' type in write.c for - * the exact behavior of this function and how to call it. - */ -#ifdef EXPORT_COMPRESSION_FUNCTIONS -WIMLIBAPI -#endif -unsigned -lzx_compress(const void *__uncompressed_data, unsigned uncompressed_len, - void *compressed_data) +/* Documented in wimlib.h */ +WIMLIBAPI unsigned +wimlib_lzx_compress(const void *__uncompressed_data, unsigned uncompressed_len, + void *compressed_data) { struct output_bitstream ostream; u8 uncompressed_data[uncompressed_len + 8]; @@ -754,8 +747,8 @@ lzx_compress(const void *__uncompressed_data, unsigned uncompressed_len, #ifdef ENABLE_VERIFY_COMPRESSION /* Verify that we really get the same thing back when decompressing. */ u8 buf[uncompressed_len]; - ret = lzx_decompress(compressed_data, compressed_len, buf, - uncompressed_len); + ret = wimlib_lzx_decompress(compressed_data, compressed_len, + buf, uncompressed_len); if (ret != 0) { ERROR("lzx_compress(): Failed to decompress data we compressed"); abort(); diff --git a/src/lzx-decompress.c b/src/lzx-decompress.c index da462186..18fa3d00 100644 --- a/src/lzx-decompress.c +++ b/src/lzx-decompress.c @@ -107,6 +107,7 @@ */ #include "util.h" +#include "wimlib.h" #include "lzx.h" #include "decompress.h" #include @@ -786,27 +787,10 @@ lzx_decompress_block(int block_type, unsigned block_size, return 0; } -/* - * Decompresses a block of LZX-compressed data as used in the WIM file format. - * - * Note that this will NOT work unmodified for LZX as used in the cabinet - * format, which is not the same as in the WIM format! - * - * @compressed_data: A pointer to the compressed data. - * - * @compressed_len: The length of the compressed data, in bytes. - * - * @uncompressed_data: A pointer to the buffer into which to write the - * uncompressed data. - * - * @uncompressed_len: The length of the uncompressed data. It must be - * 32768 bytes or less. - * - * Return 0 on success; non-zero on failure. - */ -int -lzx_decompress(const void *compressed_data, unsigned compressed_len, - void *uncompressed_data, unsigned uncompressed_len) +/* Documented in wimlib.h */ +WIMLIBAPI int +wimlib_lzx_decompress(const void *compressed_data, unsigned compressed_len, + void *uncompressed_data, unsigned uncompressed_len) { struct lzx_tables tables; struct input_bitstream istream; diff --git a/src/lzx.h b/src/lzx.h index ed6a4d4e..5f87076e 100644 --- a/src/lzx.h +++ b/src/lzx.h @@ -102,12 +102,4 @@ struct lru_queue { u32 R2; }; -extern int -lzx_decompress(const void *compressed_data, unsigned compressed_len, - void *uncompressed_data, unsigned uncompressed_len); - -extern unsigned -lzx_compress(const void *uncompressed_data, unsigned uncompressed_len, - void *compressed_data); - #endif /* _WIMLIB_LZX_H */ diff --git a/src/resource.c b/src/resource.c index 7df50e72..a8e98ca4 100644 --- a/src/resource.c +++ b/src/resource.c @@ -26,8 +26,6 @@ #include "dentry.h" #include "lookup_table.h" #include "buffer_io.h" -#include "lzx.h" -#include "xpress.h" #include "sha1.h" #ifdef __WIN32__ @@ -89,9 +87,9 @@ read_compressed_resource(FILE *fp, u64 resource_compressed_size, int (*decompress)(const void *, unsigned, void *, unsigned); /* Set the appropriate decompress function. */ if (resource_ctype == WIMLIB_COMPRESSION_TYPE_LZX) - decompress = lzx_decompress; + decompress = wimlib_lzx_decompress; else - decompress = xpress_decompress; + decompress = wimlib_xpress_decompress; /* The structure of a compressed resource consists of a table of chunk * offsets followed by the chunks themselves. Each chunk consists of diff --git a/src/wimlib.h b/src/wimlib.h index d3b0c9a6..f0be7acd 100644 --- a/src/wimlib.h +++ b/src/wimlib.h @@ -613,7 +613,7 @@ struct wimlib_pattern_list { * but the @a pats pointer itself will not. See the man page for * wimlib-imagex capture for more information about allowed * patterns. */ - tchar **pats; + wimlib_tchar **pats; /** Number of patterns in the @a pats array. */ size_t num_pats; @@ -641,7 +641,7 @@ struct wimlib_capture_config { struct wimlib_pattern_list reserved2; /** Library internal use only. */ - tchar *_prefix; + wimlib_tchar *_prefix; /** Library internal use only. */ size_t _prefix_num_tchars; @@ -1656,6 +1656,64 @@ wimlib_join(const wimlib_tchar * const *swms, int wim_write_flags, wimlib_progress_func_t progress_func); +/** + * Compress a chunk of a WIM resource using LZX compression. + * + * This function is exported for convenience only and need not be used. + * + * @param chunk + * Uncompressed data of the chunk. + * @param chunk_size + * Size of the uncompressed chunk, in bytes. + * @param out + * Pointer to output buffer of size at least (@a chunk_size - 1) bytes. + * + * @return + * The size of the compressed data written to @a out in bytes, or 0 if the + * data could not be compressed to (@a chunk_size - 1) bytes or fewer. + * + * As a special requirement, the compression code is optimized for the WIM + * format and therefore requires (@a chunk_size <= 32768). + * + * As another special requirement, the compression code will read up to 8 bytes + * off the end of the @a chunk array for performance reasons. The values of + * these bytes will not affect the output of the compression, but the calling + * code must make sure that the buffer holding the uncompressed chunk is + * actually at least (@a chunk_size + 8) bytes, or at least that these extra + * bytes are in mapped memory that will not cause a memory access violation if + * accessed. + */ +extern unsigned +wimlib_lzx_compress(const void *chunk, unsigned chunk_size, void *out); + +/** + * Decompresses a block of LZX-compressed data as used in the WIM file format. + * + * Note that this will NOT work unmodified for LZX as used in the cabinet + * format, which is not the same as in the WIM format! + * + * This function is exported for convenience only and need not be used. + * + * @param compressed_data + * Pointer to the compressed data. + * + * @param compressed_len + * Length of the compressed data, in bytes. + * + * @param uncompressed_data + * Pointer to the buffer into which to write the uncompressed data. + * + * @param uncompressed_len + * Length of the uncompressed data. It must be 32768 bytes or less. + * + * @return + * 0 on success; non-zero on failure. + */ +extern int +wimlib_lzx_decompress(const void *compressed_data, unsigned compressed_len, + void *uncompressed_data, unsigned uncompressed_len); + + /** * Mounts an image in a WIM file on a directory read-only or read-write. * @@ -2410,4 +2468,19 @@ wimlib_write(WIMStruct *wim, unsigned num_threads, wimlib_progress_func_t progress_func); +/** + * This function is equivalent to wimlib_lzx_compress(), but instead compresses + * the data using "XPRESS" compression. + */ +extern unsigned +wimlib_xpress_compress(const void *chunk, unsigned chunk_size, void *out); + +/** + * This function is equivalent to wimlib_lzx_decompress(), but instead assumes + * the data is compressed using "XPRESS" compression. + */ +extern int +wimlib_xpress_decompress(const void *compressed_data, unsigned compressed_len, + void *uncompressed_data, unsigned uncompressed_len); + #endif /* _WIMLIB_H */ diff --git a/src/write.c b/src/write.c index d985aafe..184489b1 100644 --- a/src/write.c +++ b/src/write.c @@ -42,9 +42,6 @@ #include "dentry.h" #include "lookup_table.h" #include "xml.h" -#include "lzx.h" -#include "xpress.h" - #ifdef ENABLE_MULTITHREADED_COMPRESSION # include @@ -152,8 +149,9 @@ out: /* * compress_func_t- Pointer to a function to compresses a chunk - * of a WIM resource. This may be either xpress_compress() - * (xpress-compress.c) or lzx_compress() (lzx-compress.c). + * of a WIM resource. This may be either + * wimlib_xpress_compress() (xpress-compress.c) or + * wimlib_lzx_compress() (lzx-compress.c). * * @chunk: Uncompressed data of the chunk. * @chunk_size: Size of the uncompressed chunk, in bytes. @@ -179,9 +177,9 @@ compress_func_t get_compress_func(int out_ctype) { if (out_ctype == WIMLIB_COMPRESSION_TYPE_LZX) - return lzx_compress; + return wimlib_lzx_compress; else - return xpress_compress; + return wimlib_xpress_compress; } /* diff --git a/src/xpress-compress.c b/src/xpress-compress.c index 466dd755..68d28906 100644 --- a/src/xpress-compress.c +++ b/src/xpress-compress.c @@ -26,7 +26,9 @@ */ #include "xpress.h" +#include "wimlib.h" #include "compress.h" + #include #include @@ -139,18 +141,10 @@ static const struct lz_params xpress_lz_params = { .too_far = 4096, }; -/* - * Performs XPRESS compression on a block of data. - * - * Please see the documentation for the 'compress_func_t' type in write.c for - * the exact behavior of this function and how to call it. - */ -#ifdef EXPORT_COMPRESSION_FUNCTIONS -WIMLIBAPI -#endif -unsigned -xpress_compress(const void *__uncompressed_data, unsigned uncompressed_len, - void *__compressed_data) +/* Documented in wimlib.h */ +WIMLIBAPI unsigned +wimlib_xpress_compress(const void *__uncompressed_data, + unsigned uncompressed_len, void *__compressed_data) { const u8 *uncompressed_data = __uncompressed_data; u8 *compressed_data = __compressed_data; @@ -245,8 +239,8 @@ xpress_compress(const void *__uncompressed_data, unsigned uncompressed_len, #ifdef ENABLE_VERIFY_COMPRESSION /* Verify that we really get the same thing back when decompressing. */ u8 buf[uncompressed_len]; - ret = xpress_decompress(__compressed_data, compressed_len, buf, - uncompressed_len); + ret = wimlib_xpress_decompress(__compressed_data, compressed_len, + buf, uncompressed_len); if (ret) { ERROR("xpress_compress(): Failed to decompress data we " "compressed"); diff --git a/src/xpress-decompress.c b/src/xpress-decompress.c index c16b5e32..380f017d 100644 --- a/src/xpress-decompress.c +++ b/src/xpress-decompress.c @@ -200,9 +200,10 @@ xpress_decompress_block(struct input_bitstream *istream, } -int -xpress_decompress(const void *__compressed_data, unsigned compressed_len, - void *uncompressed_data, unsigned uncompressed_len) +/* Documented in wimlib.h */ +WIMLIBAPI int +wimlib_xpress_decompress(const void *__compressed_data, unsigned compressed_len, + void *uncompressed_data, unsigned uncompressed_len) { u8 lens[XPRESS_NUM_SYMBOLS]; u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS]; diff --git a/src/xpress.h b/src/xpress.h index c457a72e..2949780c 100644 --- a/src/xpress.h +++ b/src/xpress.h @@ -25,12 +25,4 @@ #define XPRESS_MIN_MATCH 3 #define XPRESS_MAX_MATCH 65538 -extern int -xpress_decompress(const void *compressed_data, unsigned compressed_len, - void *uncompressed_data, unsigned uncompressed_len); - -extern unsigned -xpress_compress(const void *uncompressed_data, unsigned uncompressed_len, - void *compressed_data); - #endif /* _WIMLIB_XPRESS_H */ -- 2.43.0