aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2013-02-27 09:28:55 +0200
committerLasse Collin <lasse.collin@tukaani.org>2013-02-27 09:28:55 +0200
commit0568cfabccc8a23b4d4a23266b39bf14134df434 (patch)
tree42fe5283ea17c5c7e1490cf28abb6b8a7533d115
parente111c275da8b88749dc9cc8d2adce9872a611b89 (diff)
downloadxz-embedded-0568cfabccc8a23b4d4a23266b39bf14134df434.tar.gz
Add optional support for CRC64.
-rw-r--r--linux/include/linux/xz.h31
-rw-r--r--linux/lib/xz/xz_crc64.c50
-rw-r--r--linux/lib/xz/xz_dec_stream.c68
-rw-r--r--userspace/Makefile4
-rw-r--r--userspace/boottest.c3
-rw-r--r--userspace/xz_config.h3
-rw-r--r--userspace/xzminidec.c3
7 files changed, 139 insertions, 23 deletions
diff --git a/linux/include/linux/xz.h b/linux/include/linux/xz.h
index 810cb10..0a4b38d 100644
--- a/linux/include/linux/xz.h
+++ b/linux/include/linux/xz.h
@@ -251,6 +251,22 @@ XZ_EXTERN void xz_dec_end(struct xz_dec *s);
# endif
#endif
+/*
+ * If CRC64 support has been enabled with XZ_USE_CRC64, a CRC64
+ * implementation is needed too.
+ */
+#ifndef XZ_USE_CRC64
+# undef XZ_INTERNAL_CRC64
+# define XZ_INTERNAL_CRC64 0
+#endif
+#ifndef XZ_INTERNAL_CRC64
+# ifdef __KERNEL__
+# error Using CRC64 in the kernel has not been implemented.
+# else
+# define XZ_INTERNAL_CRC64 1
+# endif
+#endif
+
#if XZ_INTERNAL_CRC32
/*
* This must be called before any other xz_* function to initialize
@@ -266,6 +282,21 @@ XZ_EXTERN void xz_crc32_init(void);
XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc);
#endif
+#if XZ_INTERNAL_CRC64
+/*
+ * This must be called before any other xz_* function (except xz_crc32_init())
+ * to initialize the CRC64 lookup table.
+ */
+XZ_EXTERN void xz_crc64_init(void);
+
+/*
+ * 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.
+ */
+XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc);
+#endif
+
#ifdef __cplusplus
}
#endif
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 <lasse.collin@tukaani.org>
+ * Igor Pavlov <http://7-zip.org/>
+ *
+ * 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;
diff --git a/userspace/Makefile b/userspace/Makefile
index 9d17799..5bd6b28 100644
--- a/userspace/Makefile
+++ b/userspace/Makefile
@@ -10,11 +10,11 @@
CC = gcc -std=gnu89
BCJ_CPPFLAGS = -DXZ_DEC_X86 -DXZ_DEC_POWERPC -DXZ_DEC_IA64 \
-DXZ_DEC_ARM -DXZ_DEC_ARMTHUMB -DXZ_DEC_SPARC
-CPPFLAGS = -DXZ_DEC_ANY_CHECK
+CPPFLAGS = -DXZ_USE_CRC64 -DXZ_DEC_ANY_CHECK
CFLAGS = -ggdb3 -O2 -pedantic -Wall -Wextra
RM = rm -f
VPATH = ../linux/include/linux ../linux/lib/xz
-COMMON_SRCS = xz_crc32.c xz_dec_stream.c xz_dec_lzma2.c xz_dec_bcj.c
+COMMON_SRCS = xz_crc32.c xz_crc64.c xz_dec_stream.c xz_dec_lzma2.c xz_dec_bcj.c
COMMON_OBJS = $(COMMON_SRCS:.c=.o)
XZMINIDEC_OBJS = xzminidec.o
BYTETEST_OBJS = bytetest.o
diff --git a/userspace/boottest.c b/userspace/boottest.c
index f5bc282..1aef5ed 100644
--- a/userspace/boottest.c
+++ b/userspace/boottest.c
@@ -19,6 +19,9 @@ static void error(/*const*/ char *msg)
fprintf(stderr, "%s\n", msg);
}
+/* Disable the CRC64 support even if it was enabled in the Makefile. */
+#undef XZ_USE_CRC64
+
#include "../linux/lib/decompress_unxz.c"
static uint8_t in[1024 * 1024];
diff --git a/userspace/xz_config.h b/userspace/xz_config.h
index 71bb029..9a1dfef 100644
--- a/userspace/xz_config.h
+++ b/userspace/xz_config.h
@@ -10,6 +10,9 @@
#ifndef XZ_CONFIG_H
#define XZ_CONFIG_H
+/* Uncomment to enable CRC64 support. */
+/* #define XZ_USE_CRC64 */
+
/* Uncomment as needed to enable BCJ filter decoders. */
/* #define XZ_DEC_X86 */
/* #define XZ_DEC_POWERPC */
diff --git a/userspace/xzminidec.c b/userspace/xzminidec.c
index 2a039c6..ba07413 100644
--- a/userspace/xzminidec.c
+++ b/userspace/xzminidec.c
@@ -37,6 +37,9 @@ int main(int argc, char **argv)
}
xz_crc32_init();
+#ifdef XZ_USE_CRC64
+ xz_crc64_init();
+#endif
/*
* Support up to 64 MiB dictionary. The actually needed memory