# HG changeset patch # User Isaac Dunham # Date 1365697911 18000 # Node ID fef134bc206c826606f60ab313a9e892880d7b6f # Parent 917d6e051b82a19489bcd46ccd1cb65ba8922d38 More of same. Actually, it's not quite the same: -use crc_init(xz_crc32_table,1) -- I guessed on this one based on a match between magic constants, then tested it. First try was wrong, but this works. (This is the sole difference in terms of actual _code_, as opposed to cleaning up defines and the like--here, it adds 48 bytes to final size if xzcat is the only toy, but drops 32 bytes if bzcat is also enabled...) -Move some crc64 code to where the comments are. This puts them near the area where they're used. -Remove some relics of separate files (we had half a dozen "this file is in the public domain" notices, a duplicate include, and used extern declarations for functions in the samefile) diff -r 917d6e051b82 -r fef134bc206c toys/pending/xzcat.c --- a/toys/pending/xzcat.c Wed Apr 10 22:30:02 2013 -0500 +++ b/toys/pending/xzcat.c Thu Apr 11 11:31:51 2013 -0500 @@ -23,18 +23,11 @@ #include // BEGIN xz.h -/* - * XZ decompressor - * - * Authors: Lasse Collin - * Igor Pavlov - * - * This file has been put into the public domain. - * You can do whatever you want with this file. - */ #include #include +#include +#include /** @@ -246,13 +239,6 @@ */ void xz_dec_end(struct xz_dec *s); - -/* - * This must be called before any other xz_* function to initialize - * the CRC32 lookup table. - */ -void xz_crc32_init(void); - /* * Update CRC32 value using the polynomial from IEEE-802.3. To start a new * calculation, the third argument must be zero. To continue the calculation, @@ -261,19 +247,49 @@ uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc); /* - * This must be called before any other xz_* function (except xz_crc32_init()) + * This must be called before any other xz_* function (but after crc_init()) * to initialize the CRC64 lookup table. */ -void xz_crc64_init(void); +static uint64_t xz_crc64_table[256]; + +void xz_crc64_init(void) +{ + const uint64_t poly = 0xC96C5795D7870F42ULL; + + uint32_t i; + uint32_t j; + uint64_t r; + + for (i = 0; i < 256; ++i) { + r = i; + for (j = 0; j < 8; ++j) + r = (r >> 1) ^ (poly & ~((r & 1) - 1)); + + xz_crc64_table[i] = r; + } + + return; +} /* * Update CRC64 value using the polynomial from ECMA-182. To start a new * calculation, the third argument must be zero. To continue the calculation, * the previously returned value is passed as the third argument. */ -uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc); +uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc) +{ + crc = ~crc; + + while (size != 0) { + crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8); + --size; + } + + return ~crc; +} // END xz.h +static uint32_t xz_crc32_table[256]; static uint8_t in[BUFSIZ]; static uint8_t out[BUFSIZ]; @@ -285,7 +301,7 @@ enum xz_ret ret; const char *msg; - xz_crc32_init(); + crc_init(xz_crc32_table, 1); xz_crc64_init(); /* @@ -370,41 +386,9 @@ xz_dec_end(s); error_exit("%s", msg); } -/* - * CRC32 using the polynomial from IEEE-802.3 - * CRC64 using the polynomial from ECMA-182 - * - * Authors: Lasse Collin - * Igor Pavlov - * - * This file has been put into the public domain. - * You can do whatever you want with this file. - */ - -/* - * This is not the fastest implementation, but it is pretty compact. - * The fastest versions of xz_crc32() on modern CPUs without hardware - * accelerated CRC instruction are 3-5 times as fast as this version, - * but they are bigger and use more memory for the lookup table. - */ // BEGIN xz_private.h -/* - * Private includes and definitions - * - * Author: Lasse Collin - * - * This file has been put into the public domain. - * You can do whatever you want with this file. - * - * Modified for toybox by Isaac Dunham. - */ - -#ifndef XZ_PRIVATE_H -#define XZ_PRIVATE_H - -/* Enable CRC64 support. */ -#define XZ_USE_CRC64 + /* Uncomment as needed to enable BCJ filter decoders. * These cost about 2.5 k when all are enabled; SPARC and IA64 make 0.7 k @@ -417,9 +401,6 @@ #define XZ_DEC_ARMTHUMB #define XZ_DEC_SPARC -#include -#include -#include #define memeq(a, b, size) (memcmp(a, b, size) == 0) #define memzero(buf, size) memset(buf, 0, size) @@ -601,39 +582,8 @@ #define xz_dec_bcj_end(s) free(s) #endif -#endif - // END "xz_private.h" -/* - * STATIC_RW_DATA is used in the pre-boot environment on some architectures. - * See for details. - */ -#ifndef STATIC_RW_DATA -# define STATIC_RW_DATA static -#endif - -STATIC_RW_DATA uint32_t xz_crc32_table[256]; - -void xz_crc32_init(void) -{ - const uint32_t poly = 0xEDB88320; - - uint32_t i; - uint32_t j; - uint32_t r; - - for (i = 0; i < 256; ++i) { - r = i; - for (j = 0; j < 8; ++j) - r = (r >> 1) ^ (poly & ~((r & 1) - 1)); - - xz_crc32_table[i] = r; - } - - return; -} - uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc) { crc = ~crc; @@ -648,50 +598,9 @@ -STATIC_RW_DATA uint64_t xz_crc64_table[256]; - -void xz_crc64_init(void) -{ - const uint64_t poly = 0xC96C5795D7870F42ULL; - - uint32_t i; - uint32_t j; - uint64_t r; - - for (i = 0; i < 256; ++i) { - r = i; - for (j = 0; j < 8; ++j) - r = (r >> 1) ^ (poly & ~((r & 1) - 1)); - - xz_crc64_table[i] = r; - } - - return; -} - -uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc) -{ - crc = ~crc; - - while (size != 0) { - crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8); - --size; - } - - return ~crc; -} /* * Branch/Call/Jump (BCJ) filter decoders - * - * Authors: Lasse Collin - * Igor Pavlov - * - * This file has been put into the public domain. - * You can do whatever you want with this file. - */ - -/* - * The rest of the file is inside this ifdef. It makes things a little more + * The rest of the code is inside this ifdef. It makes things a little more * convenient when building without support for any BCJ filters. */ #ifdef XZ_DEC_BCJ @@ -1254,12 +1163,6 @@ #endif /* * LZMA2 decoder - * - * Authors: Lasse Collin - * Igor Pavlov - * - * This file has been put into the public domain. - * You can do whatever you want with this file. */ @@ -1267,15 +1170,8 @@ /* * LZMA2 definitions * - * Authors: Lasse Collin - * Igor Pavlov - * - * This file has been put into the public domain. - * You can do whatever you want with this file. */ -#ifndef XZ_LZMA2_H -#define XZ_LZMA2_H /* Range coder constants */ #define RC_SHIFT_BITS 8 @@ -1467,7 +1363,6 @@ */ #define REPS 4 -#endif // END xz_lzma2.h @@ -2631,27 +2526,14 @@ } /* * .xz Stream decoder - * - * Author: Lasse Collin - * - * This file has been put into the public domain. - * You can do whatever you want with this file. */ // BEGIN xz_stream.h /* * Definitions for handling the .xz file format - * - * Author: Lasse Collin - * - * This file has been put into the public domain. - * You can do whatever you want with this file. */ -#ifndef XZ_STREAM_H -#define XZ_STREAM_H - /* * See the .xz file format specification at * http://tukaani.org/xz/xz-file-format.txt @@ -2693,8 +2575,6 @@ /* Maximum possible Check ID */ #define XZ_CHECK_MAX 15 - -#endif // END xz_stream.h #define IS_CRC64(check_type) ((check_type) == XZ_CHECK_CRC64)