From 7c29dc8db20a3e6f1fb351c44a068171bb6c1ff1 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 18 Mar 2020 13:19:03 +0100 Subject: Cleanup: Move xxhash32 code to libutil Signed-off-by: David Oberhollenzer --- COPYING.md | 6 +-- include/util.h | 2 + lib/sqfs/Makemodule.am | 2 +- lib/sqfs/block_processor/internal.h | 2 - lib/sqfs/block_processor/xxhash.c | 103 ------------------------------------ lib/util/Makemodule.am | 1 + lib/util/xxhash.c | 103 ++++++++++++++++++++++++++++++++++++ 7 files changed, 110 insertions(+), 109 deletions(-) delete mode 100644 lib/sqfs/block_processor/xxhash.c create mode 100644 lib/util/xxhash.c diff --git a/COPYING.md b/COPYING.md index b29ab60..d929391 100644 --- a/COPYING.md +++ b/COPYING.md @@ -5,9 +5,9 @@ The `libsquashfs` library is released under the terms and conditions of the all source code in the directories `lib/sqfs`, `lib/util` and `include/sqfs` with the following exceptions: - - `lib/sqfs/block_processor/xxhash.c` contains a modified implementation of - the xxhash32 algorithm. See `licenses/xxhash.txt` for copyright and - licensing information (2 clause BSD license). + - `lib/util/xxhash.c` contains a modified implementation of the xxhash32 + algorithm. See `licenses/xxhash.txt` for copyright and licensing + information (2 clause BSD license). - `lib/sqfs/comp/lz4` contains files extracted from the LZ4 compression library. See `lib/sqfs/comp/lz4/README` for details and `licenses/LZ4.txt` for copyright and licensing information (2 clause BSD license). diff --git a/include/util.h b/include/util.h index a379c0d..6774be2 100644 --- a/include/util.h +++ b/include/util.h @@ -28,4 +28,6 @@ void *alloc_flex(size_t base_size, size_t item_size, size_t nmemb); SQFS_INTERNAL void *alloc_array(size_t item_size, size_t nmemb); +SQFS_INTERNAL sqfs_u32 xxh32(const void *input, const size_t len); + #endif /* SQFS_UTIL_H */ diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index 591eda2..942c37c 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -23,7 +23,6 @@ libsquashfs_la_SOURCES += lib/sqfs/inode.c libsquashfs_la_SOURCES += lib/sqfs/write_super.c lib/sqfs/data_reader.c libsquashfs_la_SOURCES += lib/sqfs/block_processor/internal.h libsquashfs_la_SOURCES += lib/sqfs/block_processor/common.c -libsquashfs_la_SOURCES += lib/sqfs/block_processor/xxhash.c libsquashfs_la_SOURCES += lib/sqfs/frag_table.c include/sqfs/frag_table.h libsquashfs_la_SOURCES += lib/sqfs/block_writer.c include/sqfs/block_writer.h libsquashfs_la_CPPFLAGS = $(AM_CPPFLAGS) @@ -36,6 +35,7 @@ libsquashfs_la_LIBADD += $(ZSTD_LIBS) $(PTHREAD_LIBS) # directly "import" stuff from libutil libsquashfs_la_SOURCES += lib/util/str_table.c lib/util/alloc.c +libsquashfs_la_SOURCES += lib/util/xxhash.c if WINDOWS libsquashfs_la_SOURCES += lib/sqfs/win32/io_file.c diff --git a/lib/sqfs/block_processor/internal.h b/lib/sqfs/block_processor/internal.h index 0e95551..fed0842 100644 --- a/lib/sqfs/block_processor/internal.h +++ b/lib/sqfs/block_processor/internal.h @@ -71,6 +71,4 @@ int block_processor_do_block(sqfs_block_t *block, sqfs_compressor_t *cmp, SQFS_INTERNAL int append_to_work_queue(sqfs_block_processor_t *proc, sqfs_block_t *block); -SQFS_INTERNAL sqfs_u32 xxh32(const void *input, const size_t len); - #endif /* INTERNAL_H */ diff --git a/lib/sqfs/block_processor/xxhash.c b/lib/sqfs/block_processor/xxhash.c deleted file mode 100644 index 8be4f9c..0000000 --- a/lib/sqfs/block_processor/xxhash.c +++ /dev/null @@ -1,103 +0,0 @@ -/* - * xxHash - Extremely Fast Hash algorithm - * Copyright (C) 2012-2016, Yann Collet. - * - * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * ---------------------------------------------------------------------------- - * This code has been lifted from Linux (the OS kernel) and adapted for use in - * libsquashfs. For the original, unmodified and complete source, see below. - * - * You can contact the author at: - * - xxHash homepage: http://cyan4973.github.io/xxHash/ - * - xxHash source repository: https://github.com/Cyan4973/xxHash - */ -#define SQFS_BUILDING_DLL -#include "internal.h" - -#define xxh_rotl32(x, r) ((x << r) | (x >> (32 - r))) - -static const sqfs_u32 PRIME32_1 = 2654435761U; -static const sqfs_u32 PRIME32_2 = 2246822519U; -static const sqfs_u32 PRIME32_3 = 3266489917U; -static const sqfs_u32 PRIME32_4 = 668265263U; -static const sqfs_u32 PRIME32_5 = 374761393U; - -static sqfs_u32 xxh32_round(sqfs_u32 seed, sqfs_u32 input) -{ - seed += input * PRIME32_2; - seed = xxh_rotl32(seed, 13); - seed *= PRIME32_1; - return seed; -} - -sqfs_u32 xxh32(const void *input, const size_t len) -{ - const sqfs_u8 *p = (const sqfs_u8 *)input; - const sqfs_u8 *b_end = p + len; - sqfs_u32 h32; - - if (len >= 16) { - const sqfs_u8 *const limit = b_end - 16; - sqfs_u32 v1 = PRIME32_1 + PRIME32_2; - sqfs_u32 v2 = PRIME32_2; - sqfs_u32 v3 = 0; - sqfs_u32 v4 = PRIME32_1; - - do { - v1 = xxh32_round(v1, ((sqfs_u32 *)p)[0]); - v2 = xxh32_round(v2, ((sqfs_u32 *)p)[1]); - v3 = xxh32_round(v3, ((sqfs_u32 *)p)[2]); - v4 = xxh32_round(v4, ((sqfs_u32 *)p)[3]); - p += 16; - } while (p <= limit); - - h32 = xxh_rotl32(v1, 1) + xxh_rotl32(v2, 7) + - xxh_rotl32(v3, 12) + xxh_rotl32(v4, 18); - } else { - h32 = PRIME32_5; - } - - h32 += (sqfs_u32)len; - - while (p + 4 <= b_end) { - h32 += (*((sqfs_u32 *)p)) * PRIME32_3; - h32 = xxh_rotl32(h32, 17) * PRIME32_4; - p += 4; - } - - while (p < b_end) { - h32 += (*p) * PRIME32_5; - h32 = xxh_rotl32(h32, 11) * PRIME32_1; - p++; - } - - h32 ^= h32 >> 15; - h32 *= PRIME32_2; - h32 ^= h32 >> 13; - h32 *= PRIME32_3; - h32 ^= h32 >> 16; - return h32; -} diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 87f3495..5cff2dd 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -1,6 +1,7 @@ libutil_a_SOURCES = include/util.h include/str_table.h libutil_a_SOURCES += lib/util/str_table.c lib/util/alloc.c libutil_a_SOURCES += lib/util/rbtree.c include/rbtree.h +libutil_a_SOURCES += lib/util/xxhash.c libutil_a_CFLAGS = $(AM_CFLAGS) libutil_a_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/lib/util/xxhash.c b/lib/util/xxhash.c new file mode 100644 index 0000000..4e1e0e0 --- /dev/null +++ b/lib/util/xxhash.c @@ -0,0 +1,103 @@ +/* + * xxHash - Extremely Fast Hash algorithm + * Copyright (C) 2012-2016, Yann Collet. + * + * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ---------------------------------------------------------------------------- + * This code has been lifted from Linux (the OS kernel) and adapted for use in + * libsquashfs. For the original, unmodified and complete source, see below. + * + * You can contact the author at: + * - xxHash homepage: http://cyan4973.github.io/xxHash/ + * - xxHash source repository: https://github.com/Cyan4973/xxHash + */ +#include "config.h" +#include "util.h" + +#define xxh_rotl32(x, r) ((x << r) | (x >> (32 - r))) + +static const sqfs_u32 PRIME32_1 = 2654435761U; +static const sqfs_u32 PRIME32_2 = 2246822519U; +static const sqfs_u32 PRIME32_3 = 3266489917U; +static const sqfs_u32 PRIME32_4 = 668265263U; +static const sqfs_u32 PRIME32_5 = 374761393U; + +static sqfs_u32 xxh32_round(sqfs_u32 seed, sqfs_u32 input) +{ + seed += input * PRIME32_2; + seed = xxh_rotl32(seed, 13); + seed *= PRIME32_1; + return seed; +} + +sqfs_u32 xxh32(const void *input, const size_t len) +{ + const sqfs_u8 *p = (const sqfs_u8 *)input; + const sqfs_u8 *b_end = p + len; + sqfs_u32 h32; + + if (len >= 16) { + const sqfs_u8 *const limit = b_end - 16; + sqfs_u32 v1 = PRIME32_1 + PRIME32_2; + sqfs_u32 v2 = PRIME32_2; + sqfs_u32 v3 = 0; + sqfs_u32 v4 = PRIME32_1; + + do { + v1 = xxh32_round(v1, ((sqfs_u32 *)p)[0]); + v2 = xxh32_round(v2, ((sqfs_u32 *)p)[1]); + v3 = xxh32_round(v3, ((sqfs_u32 *)p)[2]); + v4 = xxh32_round(v4, ((sqfs_u32 *)p)[3]); + p += 16; + } while (p <= limit); + + h32 = xxh_rotl32(v1, 1) + xxh_rotl32(v2, 7) + + xxh_rotl32(v3, 12) + xxh_rotl32(v4, 18); + } else { + h32 = PRIME32_5; + } + + h32 += (sqfs_u32)len; + + while (p + 4 <= b_end) { + h32 += (*((sqfs_u32 *)p)) * PRIME32_3; + h32 = xxh_rotl32(h32, 17) * PRIME32_4; + p += 4; + } + + while (p < b_end) { + h32 += (*p) * PRIME32_5; + h32 = xxh_rotl32(h32, 11) * PRIME32_1; + p++; + } + + h32 ^= h32 >> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + return h32; +} -- cgit v1.2.3