diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2008-12-31 00:30:49 +0200 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2008-12-31 00:30:49 +0200 |
commit | 7ed9d943b31d3ee9c5fb2387e84a241ba33afe90 (patch) | |
tree | 5f9107c718aa996be6850b431ba319584064c9d7 /src/liblzma/check | |
parent | Use 28 MiB as memory usage limit for encoding in test_compress.sh. (diff) | |
download | xz-7ed9d943b31d3ee9c5fb2387e84a241ba33afe90.tar.xz |
Remove lzma_init() and other init functions from liblzma API.
Half of developers were already forgetting to use these
functions, which could have caused total breakage in some future
liblzma version or even now if --enable-small was used. Now
liblzma uses pthread_once() to do the initializations unless
it has been built with --disable-threads which make these
initializations thread-unsafe.
When --enable-small isn't used, liblzma currently gets needlessly
linked against libpthread (on systems that have it). While it is
stupid for now, liblzma will need threads in future anyway, so
this stupidity will be temporary only.
When --enable-small is used, different code CRC32 and CRC64 is
now used than without --enable-small. This made the resulting
binary slightly smaller, but the main reason was to clean it up
and to handle the lack of lzma_init_check().
The pkg-config file lzma.pc was renamed to liblzma.pc. I'm not
sure if it works correctly and portably for static linking
(Libs.private includes -pthread or other operating system
specific flags). Hopefully someone complains if it is bad.
lzma_rc_prices[] is now included as a precomputed array even
with --enable-small. It's just 128 bytes now that it uses uint8_t
instead of uint32_t. Smaller array seemed to be at least as fast
as the more bloated uint32_t array on x86; hopefully it's not bad
on other architectures.
Diffstat (limited to '')
-rw-r--r-- | src/liblzma/check/Makefile.am | 29 | ||||
-rw-r--r-- | src/liblzma/check/check.c | 10 | ||||
-rw-r--r-- | src/liblzma/check/check.h | 25 | ||||
-rw-r--r-- | src/liblzma/check/check_init.c | 37 | ||||
-rw-r--r-- | src/liblzma/check/crc32_fast.c (renamed from src/liblzma/check/crc32.c) | 0 | ||||
-rw-r--r-- | src/liblzma/check/crc32_init.c | 55 | ||||
-rw-r--r-- | src/liblzma/check/crc32_small.c | 54 | ||||
-rw-r--r-- | src/liblzma/check/crc32_tablegen.c | 55 | ||||
-rw-r--r-- | src/liblzma/check/crc64_fast.c (renamed from src/liblzma/check/crc64.c) | 0 | ||||
-rw-r--r-- | src/liblzma/check/crc64_small.c | 54 | ||||
-rw-r--r-- | src/liblzma/check/crc64_tablegen.c | 55 |
11 files changed, 228 insertions, 146 deletions
diff --git a/src/liblzma/check/Makefile.am b/src/liblzma/check/Makefile.am index 182e0868..f323e6fe 100644 --- a/src/liblzma/check/Makefile.am +++ b/src/liblzma/check/Makefile.am @@ -13,46 +13,37 @@ noinst_LTLIBRARIES = libcheck.la libcheck_la_SOURCES = \ check.c \ check.h \ - check_init.c \ crc_macros.h libcheck_la_CPPFLAGS = \ -I@top_srcdir@/src/liblzma/api \ -I@top_srcdir@/src/liblzma/common if COND_CHECK_CRC32 - +if COND_SMALL +libcheck_la_SOURCES += crc32_small.c +else +libcheck_la_SOURCES += crc32_table.c crc32_table_le.h crc32_table_be.h if COND_ASM_X86 libcheck_la_SOURCES += crc32_x86.S else -libcheck_la_SOURCES += crc32.c +libcheck_la_SOURCES += crc32_fast.c endif - -if COND_SMALL -libcheck_la_SOURCES += crc32_init.c -else -libcheck_la_SOURCES += crc32_table.c crc32_table_le.h crc32_table_be.h endif - endif - if COND_CHECK_CRC64 - +if COND_SMALL +libcheck_la_SOURCES += crc64_small.c +else +libcheck_la_SOURCES += crc64_table.c crc64_table_le.h crc64_table_be.h if COND_ASM_X86 libcheck_la_SOURCES += crc64_x86.S else -libcheck_la_SOURCES += crc64.c +libcheck_la_SOURCES += crc64_fast.c endif - -if COND_SMALL -libcheck_la_SOURCES += crc64_init.c -else -libcheck_la_SOURCES += crc64_table.c crc64_table_le.h crc64_table_be.h endif - endif - if COND_CHECK_SHA256 libcheck_la_SOURCES += sha256.c # Hide bogus warning to allow usage of -Werror. If more issues like this diff --git a/src/liblzma/check/check.c b/src/liblzma/check/check.c index ed64fe5c..699647ed 100644 --- a/src/liblzma/check/check.c +++ b/src/liblzma/check/check.c @@ -1,7 +1,7 @@ /////////////////////////////////////////////////////////////////////////////// // /// \file check.c -/// \brief Check sizes +/// \brief Single API to access different integrity checks // // This code has been put into the public domain. // @@ -17,7 +17,7 @@ extern LZMA_API lzma_bool lzma_check_is_supported(lzma_check type) { - if ((unsigned)(type) > LZMA_CHECK_ID_MAX) + if ((unsigned int)(type) > LZMA_CHECK_ID_MAX) return false; static const lzma_bool available_checks[LZMA_CHECK_ID_MAX + 1] = { @@ -57,14 +57,14 @@ lzma_check_is_supported(lzma_check type) false, // Reserved }; - return available_checks[(unsigned)(type)]; + return available_checks[(unsigned int)(type)]; } extern LZMA_API uint32_t lzma_check_size(lzma_check type) { - if ((unsigned)(type) > LZMA_CHECK_ID_MAX) + if ((unsigned int)(type) > LZMA_CHECK_ID_MAX) return UINT32_MAX; // See file-format.txt section 2.1.1.2. @@ -77,7 +77,7 @@ lzma_check_size(lzma_check type) 64, 64, 64 }; - return check_sizes[(unsigned)(type)]; + return check_sizes[(unsigned int)(type)]; } diff --git a/src/liblzma/check/check.h b/src/liblzma/check/check.h index 8f387799..73c6391d 100644 --- a/src/liblzma/check/check.h +++ b/src/liblzma/check/check.h @@ -57,46 +57,39 @@ typedef struct { } lzma_check_state; +/// lzma_crc32_table[0] is needed by LZ encoder so we need to keep +/// the array two-dimensional. #ifdef HAVE_SMALL -extern uint32_t lzma_crc32_table[8][256]; -extern uint64_t lzma_crc64_table[4][256]; +extern uint32_t lzma_crc32_table[1][256]; #else extern const uint32_t lzma_crc32_table[8][256]; extern const uint64_t lzma_crc64_table[4][256]; #endif -/// \brief Initializes *check depending on type +/// \brief Initialize *check depending on type /// /// \return LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not /// supported by the current version or build of liblzma. /// LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX. -/// extern void lzma_check_init(lzma_check_state *check, lzma_check type); - -/// \brief Updates *check -/// +/// Update the check state extern void lzma_check_update(lzma_check_state *check, lzma_check type, const uint8_t *buf, size_t size); - -/// \brief Finishes *check -/// +/// Finish the check calculation and store the result to check->buffer.u8. extern void lzma_check_finish(lzma_check_state *check, lzma_check type); -extern void lzma_crc32_init(void); - - -extern void lzma_crc64_init(void); - - +/// Prepare SHA-256 state for new input. extern void lzma_sha256_init(lzma_check_state *check); +/// Update the SHA-256 hash state extern void lzma_sha256_update( const uint8_t *buf, size_t size, lzma_check_state *check); +/// Finish the SHA-256 calculation and store the result to check->buffer.u8. extern void lzma_sha256_finish(lzma_check_state *check); #endif diff --git a/src/liblzma/check/check_init.c b/src/liblzma/check/check_init.c deleted file mode 100644 index 1b2cfe02..00000000 --- a/src/liblzma/check/check_init.c +++ /dev/null @@ -1,37 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// -/// \file check_init.c -/// \brief Static initializations for integrity checks -// -// This code has been put into the public domain. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// -/////////////////////////////////////////////////////////////////////////////// - -#include "check.h" - - -extern LZMA_API void -lzma_init_check(void) -{ -#ifdef HAVE_SMALL - static bool already_initialized = false; - if (already_initialized) - return; - -# ifdef HAVE_CHECK_CRC32 - lzma_crc32_init(); -# endif - -# ifdef HAVE_CHECK_CRC64 - lzma_crc64_init(); -# endif - - already_initialized = true; -#endif - - return; -} diff --git a/src/liblzma/check/crc32.c b/src/liblzma/check/crc32_fast.c index 698cf768..698cf768 100644 --- a/src/liblzma/check/crc32.c +++ b/src/liblzma/check/crc32_fast.c diff --git a/src/liblzma/check/crc32_init.c b/src/liblzma/check/crc32_init.c deleted file mode 100644 index 8b596091..00000000 --- a/src/liblzma/check/crc32_init.c +++ /dev/null @@ -1,55 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// -/// \file crc32_init.c -/// \brief CRC32 table initialization -// -// This code is based on various public domain sources. -// This code has been put into the public domain. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// -/////////////////////////////////////////////////////////////////////////////// - -#ifdef HAVE_CONFIG_H -# include "check.h" -#endif - -#ifdef WORDS_BIGENDIAN -# include "../../common/bswap.h" -#endif - - -uint32_t lzma_crc32_table[8][256]; - - -extern void -lzma_crc32_init(void) -{ - static const uint32_t poly32 = UINT32_C(0xEDB88320); - - for (size_t s = 0; s < 8; ++s) { - for (size_t b = 0; b < 256; ++b) { - uint32_t r = s == 0 ? b : lzma_crc32_table[s - 1][b]; - - for (size_t i = 0; i < 8; ++i) { - if (r & 1) - r = (r >> 1) ^ poly32; - else - r >>= 1; - } - - lzma_crc32_table[s][b] = r; - } - } - -#ifdef WORDS_BIGENDIAN - for (size_t s = 0; s < 8; ++s) - for (size_t b = 0; b < 256; ++b) - lzma_crc32_table[s][b] - = bswap_32(lzma_crc32_table[s][b]); -#endif - - return; -} diff --git a/src/liblzma/check/crc32_small.c b/src/liblzma/check/crc32_small.c new file mode 100644 index 00000000..db26f8d4 --- /dev/null +++ b/src/liblzma/check/crc32_small.c @@ -0,0 +1,54 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file crc32_small.c +/// \brief CRC32 calculation (size-optimized) +// +// This code has been put into the public domain. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "check.h" + + +uint32_t lzma_crc32_table[1][256]; + + +static void +crc32_init(void) +{ + static const uint32_t poly32 = UINT32_C(0xEDB88320); + + for (size_t b = 0; b < 256; ++b) { + uint32_t r = b; + for (size_t i = 0; i < 8; ++i) { + if (r & 1) + r = (r >> 1) ^ poly32; + else + r >>= 1; + } + + lzma_crc32_table[0][b] = r; + } + + return; +} + + +extern LZMA_API uint32_t +lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc) +{ + mythread_once(crc32_init); + + crc = ~crc; + + while (size != 0) { + crc = lzma_crc32_table[0][*buf++ ^ (crc & 0xFF)] ^ (crc >> 8); + --size; + } + + return ~crc; +} diff --git a/src/liblzma/check/crc32_tablegen.c b/src/liblzma/check/crc32_tablegen.c index f793d594..d0c41caa 100644 --- a/src/liblzma/check/crc32_tablegen.c +++ b/src/liblzma/check/crc32_tablegen.c @@ -1,7 +1,7 @@ /////////////////////////////////////////////////////////////////////////////// // /// \file crc32_tablegen.c -/// \brief Generates CRC32 crc32_table.c +/// \brief Generate crc32_table_le.h and crc32_table_be.h /// /// Compiling: gcc -std=c99 -o crc32_tablegen crc32_tablegen.c /// Add -DWORDS_BIGENDIAN to generate big endian table. @@ -14,18 +14,50 @@ // /////////////////////////////////////////////////////////////////////////////// -#include <sys/types.h> #include <inttypes.h> #include <stdio.h> -#include "crc32_init.c" +#ifdef WORDS_BIGENDIAN +# include "../../common/bswap.h" +#endif -int -main() +static uint32_t crc32_table[8][256]; + + +static void +init_crc32_table(void) { - lzma_crc32_init(); + static const uint32_t poly32 = UINT32_C(0xEDB88320); + + for (size_t s = 0; s < 8; ++s) { + for (size_t b = 0; b < 256; ++b) { + uint32_t r = s == 0 ? b : crc32_table[s - 1][b]; + + for (size_t i = 0; i < 8; ++i) { + if (r & 1) + r = (r >> 1) ^ poly32; + else + r >>= 1; + } + + crc32_table[s][b] = r; + } + } +#ifdef WORDS_BIGENDIAN + for (size_t s = 0; s < 8; ++s) + for (size_t b = 0; b < 256; ++b) + crc32_table[s][b] = bswap_32(crc32_table[s][b]); +#endif + + return; +} + + +static void +print_crc32_table(void) +{ printf("/* This file has been automatically generated by " "crc32_tablegen.c. */\n\n" "const uint32_t lzma_crc32_table[8][256] = {\n\t{"); @@ -35,7 +67,7 @@ main() if ((b % 4) == 0) printf("\n\t\t"); - printf("0x%08" PRIX32, lzma_crc32_table[s][b]); + printf("0x%08" PRIX32, crc32_table[s][b]); if (b != 255) printf(", "); @@ -47,5 +79,14 @@ main() printf("\n\t}, {"); } + return; +} + + +int +main(void) +{ + init_crc32_table(); + print_crc32_table(); return 0; } diff --git a/src/liblzma/check/crc64.c b/src/liblzma/check/crc64_fast.c index 00fbfd53..00fbfd53 100644 --- a/src/liblzma/check/crc64.c +++ b/src/liblzma/check/crc64_fast.c diff --git a/src/liblzma/check/crc64_small.c b/src/liblzma/check/crc64_small.c new file mode 100644 index 00000000..112bc032 --- /dev/null +++ b/src/liblzma/check/crc64_small.c @@ -0,0 +1,54 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file crc64_small.c +/// \brief CRC64 calculation (size-optimized) +// +// This code has been put into the public domain. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "check.h" + + +static uint64_t crc64_table[256]; + + +static void +crc64_init(void) +{ + static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42); + + for (size_t b = 0; b < 256; ++b) { + uint64_t r = b; + for (size_t i = 0; i < 8; ++i) { + if (r & 1) + r = (r >> 1) ^ poly64; + else + r >>= 1; + } + + crc64_table[b] = r; + } + + return; +} + + +extern LZMA_API uint64_t +lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc) +{ + mythread_once(crc64_init); + + crc = ~crc; + + while (size != 0) { + crc = crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8); + --size; + } + + return ~crc; +} diff --git a/src/liblzma/check/crc64_tablegen.c b/src/liblzma/check/crc64_tablegen.c index 78e39068..b20086f8 100644 --- a/src/liblzma/check/crc64_tablegen.c +++ b/src/liblzma/check/crc64_tablegen.c @@ -1,7 +1,7 @@ /////////////////////////////////////////////////////////////////////////////// // /// \file crc64_tablegen.c -/// \brief Generates CRC64 crc64_table.c +/// \brief Generate crc64_table_le.h and crc64_table_be.h /// /// Compiling: gcc -std=c99 -o crc64_tablegen crc64_tablegen.c /// Add -DWORDS_BIGENDIAN to generate big endian table. @@ -14,18 +14,50 @@ // /////////////////////////////////////////////////////////////////////////////// -#include <sys/types.h> #include <inttypes.h> #include <stdio.h> -#include "crc64_init.c" +#ifdef WORDS_BIGENDIAN +# include "../../common/bswap.h" +#endif -int -main() +static uint64_t crc64_table[4][256]; + + +extern void +init_crc64_table(void) { - lzma_crc64_init(); + static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42); + + for (size_t s = 0; s < 4; ++s) { + for (size_t b = 0; b < 256; ++b) { + uint64_t r = s == 0 ? b : crc64_table[s - 1][b]; + + for (size_t i = 0; i < 8; ++i) { + if (r & 1) + r = (r >> 1) ^ poly64; + else + r >>= 1; + } + + crc64_table[s][b] = r; + } + } +#ifdef WORDS_BIGENDIAN + for (size_t s = 0; s < 4; ++s) + for (size_t b = 0; b < 256; ++b) + crc64_table[s][b] = bswap_64(crc64_table[s][b]); +#endif + + return; +} + + +static void +print_crc64_table(void) +{ printf("/* This file has been automatically generated by " "crc64_tablegen.c. */\n\n" "const uint64_t lzma_crc64_table[4][256] = {\n\t{"); @@ -36,7 +68,7 @@ main() printf("\n\t\t"); printf("UINT64_C(0x%016" PRIX64 ")", - lzma_crc64_table[s][b]); + crc64_table[s][b]); if (b != 255) printf(", "); @@ -48,5 +80,14 @@ main() printf("\n\t}, {"); } + return; +} + + +int +main(void) +{ + init_crc64_table(); + print_crc64_table(); return 0; } |