From 0568cfabccc8a23b4d4a23266b39bf14134df434 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Wed, 27 Feb 2013 09:28:55 +0200 Subject: Add optional support for CRC64. --- linux/lib/xz/xz_crc64.c | 50 ++++++++++++++++++++++++++++++++ linux/lib/xz/xz_dec_stream.c | 68 ++++++++++++++++++++++++++++++-------------- 2 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 linux/lib/xz/xz_crc64.c (limited to 'linux/lib/xz') diff --git a/linux/lib/xz/xz_crc64.c b/linux/lib/xz/xz_crc64.c new file mode 100644 index 0000000..ca1caee --- /dev/null +++ b/linux/lib/xz/xz_crc64.c @@ -0,0 +1,50 @@ +/* + * CRC64 using the polynomial from ECMA-182 + * + * This file is similar to xz_crc32.c. See the comments there. + * + * Authors: Lasse Collin + * Igor Pavlov + * + * This file has been put into the public domain. + * You can do whatever you want with this file. + */ + +#include "xz_private.h" + +#ifndef STATIC_RW_DATA +# define STATIC_RW_DATA static +#endif + +STATIC_RW_DATA uint64_t xz_crc64_table[256]; + +XZ_EXTERN void xz_crc64_init(void) +{ + const uint64_t poly = 0xC96C5795D7870F42; + + 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; +} + +XZ_EXTERN 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; +} diff --git a/linux/lib/xz/xz_dec_stream.c b/linux/lib/xz/xz_dec_stream.c index ac809b1..d652550 100644 --- a/linux/lib/xz/xz_dec_stream.c +++ b/linux/lib/xz/xz_dec_stream.c @@ -10,6 +10,12 @@ #include "xz_private.h" #include "xz_stream.h" +#ifdef XZ_USE_CRC64 +# define IS_CRC64(check_type) ((check_type) == XZ_CHECK_CRC64) +#else +# define IS_CRC64(check_type) false +#endif + /* Hash used to validate the Index field */ struct xz_dec_hash { vli_type unpadded; @@ -42,8 +48,13 @@ struct xz_dec { size_t in_start; size_t out_start; +#ifdef XZ_USE_CRC64 + /* CRC32 or CRC64 value in Block or CRC32 value in Index */ + uint64_t crc; +#else /* CRC32 value in Block or Index */ - uint32_t crc32; + uint32_t crc; +#endif /* Type of the integrity check calculated from uncompressed data */ enum xz_check check_type; @@ -208,8 +219,8 @@ static enum xz_ret dec_vli(struct xz_dec *s, const uint8_t *in, * the observed compressed and uncompressed sizes of the Block so that * they don't exceed the values possibly stored in the Block Header * (validation assumes that no integer overflow occurs, since vli_type - * is normally uint64_t). Update the CRC32 if presence of the CRC32 - * field was indicated in Stream Header. + * is normally uint64_t). Update the CRC32 or CRC64 value if presence of + * the CRC32 or CRC64 field was indicated in Stream Header. * * Once the decoding is finished, validate that the observed sizes match * the sizes possibly stored in the Block Header. Update the hash and @@ -242,8 +253,13 @@ static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b) return XZ_DATA_ERROR; if (s->check_type == XZ_CHECK_CRC32) - s->crc32 = xz_crc32(b->out + s->out_start, - b->out_pos - s->out_start, s->crc32); + s->crc = xz_crc32(b->out + s->out_start, + b->out_pos - s->out_start, s->crc); +#ifdef XZ_USE_CRC64 + else if (s->check_type == XZ_CHECK_CRC64) + s->crc = xz_crc64(b->out + s->out_start, + b->out_pos - s->out_start, s->crc); +#endif if (ret == XZ_STREAM_END) { if (s->block_header.compressed != VLI_UNKNOWN @@ -264,6 +280,8 @@ static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b) #else if (s->check_type == XZ_CHECK_CRC32) s->block.hash.unpadded += 4; + else if (IS_CRC64(s->check_type)) + s->block.hash.unpadded += 8; #endif s->block.hash.uncompressed += s->block.uncompressed; @@ -282,7 +300,7 @@ static void index_update(struct xz_dec *s, const struct xz_buf *b) { size_t in_used = b->in_pos - s->in_start; s->index.size += in_used; - s->crc32 = xz_crc32(b->in + s->in_start, in_used, s->crc32); + s->crc = xz_crc32(b->in + s->in_start, in_used, s->crc); } /* @@ -340,23 +358,25 @@ static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b) } /* - * Validate that the next four input bytes match the value of s->crc32. - * s->pos must be zero when starting to validate the first byte. + * Validate that the next four or eight input bytes match the value + * of s->crc. s->pos must be zero when starting to validate the first byte. + * The "bits" argument allows using the same code for both CRC32 and CRC64. */ -static enum xz_ret crc32_validate(struct xz_dec *s, struct xz_buf *b) +static enum xz_ret crc_validate(struct xz_dec *s, struct xz_buf *b, + uint32_t bits) { do { if (b->in_pos == b->in_size) return XZ_OK; - if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++]) + if (((s->crc >> s->pos) & 0xFF) != b->in[b->in_pos++]) return XZ_DATA_ERROR; s->pos += 8; - } while (s->pos < 32); + } while (s->pos < bits); - s->crc32 = 0; + s->crc = 0; s->pos = 0; return XZ_STREAM_END; @@ -397,10 +417,11 @@ static enum xz_ret dec_stream_header(struct xz_dec *s) return XZ_OPTIONS_ERROR; /* - * Of integrity checks, we support only none (Check ID = 0) and - * CRC32 (Check ID = 1). However, if XZ_DEC_ANY_CHECK is defined, - * we will accept other check types too, but then the check won't - * be verified and a warning (XZ_UNSUPPORTED_CHECK) will be given. + * Of integrity checks, we support none (Check ID = 0), + * CRC32 (Check ID = 1), and optionally CRC64 (Check ID = 4). + * However, if XZ_DEC_ANY_CHECK is defined, we will accept other + * check types too, but then the check won't be verified and + * a warning (XZ_UNSUPPORTED_CHECK) will be given. */ s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1]; @@ -408,10 +429,10 @@ static enum xz_ret dec_stream_header(struct xz_dec *s) if (s->check_type > XZ_CHECK_MAX) return XZ_OPTIONS_ERROR; - if (s->check_type > XZ_CHECK_CRC32) + if (s->check_type > XZ_CHECK_CRC32 && !IS_CRC64(s->check_type)) return XZ_UNSUPPORTED_CHECK; #else - if (s->check_type > XZ_CHECK_CRC32) + if (s->check_type > XZ_CHECK_CRC32 && !IS_CRC64(s->check_type)) return XZ_OPTIONS_ERROR; #endif @@ -645,7 +666,12 @@ static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b) case SEQ_BLOCK_CHECK: if (s->check_type == XZ_CHECK_CRC32) { - ret = crc32_validate(s, b); + ret = crc_validate(s, b, 32); + if (ret != XZ_STREAM_END) + return ret; + } + else if (IS_CRC64(s->check_type)) { + ret = crc_validate(s, b, 64); if (ret != XZ_STREAM_END) return ret; } @@ -688,7 +714,7 @@ static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b) s->sequence = SEQ_INDEX_CRC32; case SEQ_INDEX_CRC32: - ret = crc32_validate(s, b); + ret = crc_validate(s, b, 32); if (ret != XZ_STREAM_END) return ret; @@ -802,7 +828,7 @@ XZ_EXTERN void xz_dec_reset(struct xz_dec *s) s->sequence = SEQ_STREAM_HEADER; s->allow_buf_error = false; s->pos = 0; - s->crc32 = 0; + s->crc = 0; memzero(&s->block, sizeof(s->block)); memzero(&s->index, sizeof(s->index)); s->temp.pos = 0; -- cgit v1.2.3