diff options
author | derrior <derrior@yandex-team.ru> | 2022-02-10 16:47:29 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:29 +0300 |
commit | 9365a6d84363f5cb52059d1867c1fbf075ea63c3 (patch) | |
tree | 9a7214acba4994a6c343e6fbc99aa42411833a7a /contrib/libs | |
parent | 5aed5c3e0cbd6dc139d35a72836d39fc1896a108 (diff) | |
download | ydb-9365a6d84363f5cb52059d1867c1fbf075ea63c3.tar.gz |
Restoring authorship annotation for <derrior@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/libs')
77 files changed, 3559 insertions, 3559 deletions
diff --git a/contrib/libs/brotli/common/constants.h b/contrib/libs/brotli/common/constants.h index d1b88d12af..7675ee8c0c 100644 --- a/contrib/libs/brotli/common/constants.h +++ b/contrib/libs/brotli/common/constants.h @@ -28,25 +28,25 @@ /* "code length of 8 is repeated" */ #define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8 -/* "Large Window Brotli" */ -#define BROTLI_LARGE_MAX_DISTANCE_BITS 62U -#define BROTLI_LARGE_MIN_WBITS 10 -#define BROTLI_LARGE_MAX_WBITS 30 - +/* "Large Window Brotli" */ +#define BROTLI_LARGE_MAX_DISTANCE_BITS 62U +#define BROTLI_LARGE_MIN_WBITS 10 +#define BROTLI_LARGE_MAX_WBITS 30 + /* Specification: 4. Encoding of distances */ #define BROTLI_NUM_DISTANCE_SHORT_CODES 16 #define BROTLI_MAX_NPOSTFIX 3 #define BROTLI_MAX_NDIRECT 120 #define BROTLI_MAX_DISTANCE_BITS 24U -#define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \ - BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + \ - ((MAXNBITS) << ((NPOSTFIX) + 1))) -/* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */ -#define BROTLI_NUM_DISTANCE_SYMBOLS \ - BROTLI_DISTANCE_ALPHABET_SIZE( \ - BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS) +#define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \ + BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + \ + ((MAXNBITS) << ((NPOSTFIX) + 1))) +/* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */ +#define BROTLI_NUM_DISTANCE_SYMBOLS \ + BROTLI_DISTANCE_ALPHABET_SIZE( \ + BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS) #define BROTLI_MAX_DISTANCE 0x3FFFFFC -#define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC +#define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC /* 7.1. Context modes and context ID lookup for literals */ /* "context IDs for literals are in the range of 0..63" */ diff --git a/contrib/libs/brotli/common/dictionary.c b/contrib/libs/brotli/common/dictionary.c index 64822a381b..dc415b01dd 100644 --- a/contrib/libs/brotli/common/dictionary.c +++ b/contrib/libs/brotli/common/dictionary.c @@ -5883,7 +5883,7 @@ static BrotliDictionary kBrotliDictionary = { 122784, /* data */ -#if defined(BROTLI_EXTERNAL_DICTIONARY_DATA) +#if defined(BROTLI_EXTERNAL_DICTIONARY_DATA) NULL #else kBrotliDictionaryData diff --git a/contrib/libs/brotli/common/dictionary.h b/contrib/libs/brotli/common/dictionary.h index b1c6f7f580..38fe08d4de 100644 --- a/contrib/libs/brotli/common/dictionary.h +++ b/contrib/libs/brotli/common/dictionary.h @@ -27,13 +27,13 @@ typedef struct BrotliDictionary { * Dictionary consists of words with length of [4..24] bytes. * Values at [0..3] and [25..31] indices should not be addressed. */ - uint8_t size_bits_by_length[32]; + uint8_t size_bits_by_length[32]; /* assert(offset[i + 1] == offset[i] + (bits[i] ? (i << bits[i]) : 0)) */ - uint32_t offsets_by_length[32]; + uint32_t offsets_by_length[32]; /* assert(data_size == offsets_by_length[31]) */ - size_t data_size; + size_t data_size; /* Data array is not bound, and should obey to size_bits_by_length values. Specified size matches default (RFC 7932) dictionary. Its size is @@ -41,7 +41,7 @@ typedef struct BrotliDictionary { const uint8_t* data; } BrotliDictionary; -BROTLI_COMMON_API const BrotliDictionary* BrotliGetDictionary(void); +BROTLI_COMMON_API const BrotliDictionary* BrotliGetDictionary(void); /** * Sets dictionary data. diff --git a/contrib/libs/brotli/common/platform.h b/contrib/libs/brotli/common/platform.h index 84c448c4cf..b27f8b0de7 100644 --- a/contrib/libs/brotli/common/platform.h +++ b/contrib/libs/brotli/common/platform.h @@ -1,568 +1,568 @@ -/* Copyright 2016 Google Inc. All Rights Reserved. - - Distributed under MIT license. - See file LICENSE for detail or copy at https://opensource.org/licenses/MIT -*/ - -/* Macros for compiler / platform specific features and build options. - - Build options are: - * BROTLI_BUILD_32_BIT disables 64-bit optimizations - * BROTLI_BUILD_64_BIT forces to use 64-bit optimizations - * BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations - * BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations - * BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations - * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned - read and overlapping memcpy; this reduces decompression speed by 5% - * BROTLI_BUILD_NO_RBIT disables "rbit" optimization for ARM CPUs - * BROTLI_DEBUG dumps file name and line number when decoder detects stream - or memory error - * BROTLI_ENABLE_LOG enables asserts and dumps various state information -*/ - -#ifndef BROTLI_COMMON_PLATFORM_H_ -#define BROTLI_COMMON_PLATFORM_H_ - -#include <string.h> /* memcpy */ -#include <stdlib.h> /* malloc, free */ - -#include <brotli/port.h> -#include <brotli/types.h> - -#if defined(OS_LINUX) || defined(OS_CYGWIN) -#include <endian.h> -#elif defined(OS_FREEBSD) -#include <machine/endian.h> -#elif defined(OS_MACOSX) -#include <machine/endian.h> -/* Let's try and follow the Linux convention */ -#define BROTLI_X_BYTE_ORDER BYTE_ORDER -#define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN -#define BROTLI_X_BIG_ENDIAN BIG_ENDIAN -#endif - -#if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG) -#include <assert.h> -#include <stdio.h> -#endif - -/* The following macros were borrowed from https://github.com/nemequ/hedley - * with permission of original author - Evan Nemerson <evan@nemerson.com> */ - -/* >>> >>> >>> hedley macros */ - -/* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable - compilers. - -To apply compiler hint, enclose the branching condition into macros, like this: - - if (BROTLI_PREDICT_TRUE(zero == 0)) { - // main execution path - } else { - // compiler should place this code outside of main execution path - } - -OR: - - if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) { - // compiler should place this code outside of main execution path - } - -*/ -#if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ - BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \ - BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ - BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ - BROTLI_TI_VERSION_CHECK(7, 3, 0) || \ - BROTLI_TINYC_VERSION_CHECK(0, 9, 27) -#define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) -#define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0)) -#else -#define BROTLI_PREDICT_FALSE(x) (x) -#define BROTLI_PREDICT_TRUE(x) (x) -#endif - -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ - !defined(__cplusplus) -#define BROTLI_RESTRICT restrict -#elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \ - BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ - BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ - BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ - BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \ - BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ - BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \ - (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus)) -#define BROTLI_RESTRICT __restrict -#elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus) -#define BROTLI_RESTRICT _Restrict -#else -#define BROTLI_RESTRICT -#endif - -#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ - (defined(__cplusplus) && (__cplusplus >= 199711L)) -#define BROTLI_MAYBE_INLINE inline -#elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \ - BROTLI_ARM_VERSION_CHECK(6, 2, 0) -#define BROTLI_MAYBE_INLINE __inline__ -#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \ - BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0) -#define BROTLI_MAYBE_INLINE __inline -#else -#define BROTLI_MAYBE_INLINE -#endif - -#if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ - BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ - BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ - BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ - BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ - (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) -#define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__)) -#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) -#define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline -#elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus) -#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;") -#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0) -#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced") -#else -#define BROTLI_INLINE BROTLI_MAYBE_INLINE -#endif - -#if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ - BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ - BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ - BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ - BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ - (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) -#define BROTLI_NOINLINE __attribute__((__noinline__)) -#elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0) -#define BROTLI_NOINLINE __declspec(noinline) -#elif BROTLI_PGI_VERSION_CHECK(10, 2, 0) -#define BROTLI_NOINLINE _Pragma("noinline") -#elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus) -#define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;") -#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0) -#define BROTLI_NOINLINE _Pragma("inline=never") -#else -#define BROTLI_NOINLINE -#endif - -/* BROTLI_INTERNAL could be defined to override visibility, e.g. for tests. */ -#if !defined(BROTLI_INTERNAL) -#if defined(_WIN32) || defined(__CYGWIN__) -#define BROTLI_INTERNAL -#elif BROTLI_GNUC_VERSION_CHECK(3, 3, 0) || \ - BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ - BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ - BROTLI_IBM_VERSION_CHECK(13, 1, 0) || \ - BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ - (BROTLI_TI_VERSION_CHECK(7, 3, 0) && \ - defined(__TI_GNU_ATTRIBUTE_SUPPORT__) && defined(__TI_EABI__)) -#define BROTLI_INTERNAL __attribute__ ((visibility ("hidden"))) -#else -#define BROTLI_INTERNAL -#endif -#endif - -/* <<< <<< <<< end of hedley macros. */ - -#if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) -#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused)) -#else -#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE -#endif - -#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) -#define BROTLI_ALIGNED(N) __attribute__((aligned(N))) -#else -#define BROTLI_ALIGNED(N) -#endif - -#if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \ - (defined(M_ARM) && (M_ARM == 7)) -#define BROTLI_TARGET_ARMV7 -#endif /* ARMv7 */ - -#if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \ - defined(__aarch64__) || defined(__ARM64_ARCH_8__) -#define BROTLI_TARGET_ARMV8_ANY - -#if defined(__ARM_32BIT_STATE) -#define BROTLI_TARGET_ARMV8_32 -#elif defined(__ARM_64BIT_STATE) -#define BROTLI_TARGET_ARMV8_64 -#endif - -#endif /* ARMv8 */ - -#if defined(__ARM_NEON__) || defined(__ARM_NEON) -#define BROTLI_TARGET_NEON -#endif - -#if defined(__i386) || defined(_M_IX86) -#define BROTLI_TARGET_X86 -#endif - -#if defined(__x86_64__) || defined(_M_X64) -#define BROTLI_TARGET_X64 -#endif - -#if defined(__PPC64__) -#define BROTLI_TARGET_POWERPC64 -#endif - -#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64 -#define BROTLI_TARGET_RISCV64 -#endif - -#if defined(BROTLI_BUILD_64_BIT) -#define BROTLI_64_BITS 1 -#elif defined(BROTLI_BUILD_32_BIT) -#define BROTLI_64_BITS 0 -#elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \ - defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) -#define BROTLI_64_BITS 1 -#else -#define BROTLI_64_BITS 0 -#endif - -#if (BROTLI_64_BITS) -#define brotli_reg_t uint64_t -#else -#define brotli_reg_t uint32_t -#endif - -#if defined(BROTLI_BUILD_BIG_ENDIAN) -#define BROTLI_BIG_ENDIAN 1 -#elif defined(BROTLI_BUILD_LITTLE_ENDIAN) -#define BROTLI_LITTLE_ENDIAN 1 -#elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL) -/* Just break elif chain. */ -#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) -#define BROTLI_LITTLE_ENDIAN 1 -#elif defined(_WIN32) || defined(BROTLI_TARGET_X64) -/* Win32 & x64 can currently always be assumed to be little endian */ -#define BROTLI_LITTLE_ENDIAN 1 -#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) -#define BROTLI_BIG_ENDIAN 1 -#elif defined(BROTLI_X_BYTE_ORDER) -#if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN -#define BROTLI_LITTLE_ENDIAN 1 -#elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN -#define BROTLI_BIG_ENDIAN 1 -#endif -#endif /* BROTLI_X_BYTE_ORDER */ - -#if !defined(BROTLI_LITTLE_ENDIAN) -#define BROTLI_LITTLE_ENDIAN 0 -#endif - -#if !defined(BROTLI_BIG_ENDIAN) -#define BROTLI_BIG_ENDIAN 0 -#endif - -#if defined(BROTLI_X_BYTE_ORDER) -#undef BROTLI_X_BYTE_ORDER -#undef BROTLI_X_LITTLE_ENDIAN -#undef BROTLI_X_BIG_ENDIAN -#endif - -#if defined(BROTLI_BUILD_PORTABLE) -#define BROTLI_ALIGNED_READ (!!1) -#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \ - defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \ - defined(BROTLI_TARGET_RISCV64) -/* Allow unaligned read only for white-listed CPUs. */ -#define BROTLI_ALIGNED_READ (!!0) -#else -#define BROTLI_ALIGNED_READ (!!1) -#endif - -#if BROTLI_ALIGNED_READ -/* Portable unaligned memory access: read / write values via memcpy. */ -static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) { - uint16_t t; - memcpy(&t, p, sizeof t); - return t; -} -static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) { - uint32_t t; - memcpy(&t, p, sizeof t); - return t; -} -static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { - uint64_t t; - memcpy(&t, p, sizeof t); - return t; -} -static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { - memcpy(p, &v, sizeof v); -} -#else /* BROTLI_ALIGNED_READ */ -/* Unaligned memory access is allowed: just cast pointer to requested type. */ -#if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ - defined(MEMORY_SANITIZER) -/* Consider we have an unaligned load/store of 4 bytes from address 0x...05. - AddressSanitizer will treat it as a 3-byte access to the range 05:07 and - will miss a bug if 08 is the first unaddressable byte. - ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will - miss a race between this access and some other accesses to 08. - MemorySanitizer will correctly propagate the shadow on unaligned stores - and correctly report bugs on unaligned loads, but it may not properly - update and report the origin of the uninitialized memory. - For all three tools, replacing an unaligned access with a tool-specific - callback solves the problem. */ -#if defined(__cplusplus) -extern "C" { -#endif /* __cplusplus */ - uint16_t __sanitizer_unaligned_load16(const void* p); - uint32_t __sanitizer_unaligned_load32(const void* p); - uint64_t __sanitizer_unaligned_load64(const void* p); - void __sanitizer_unaligned_store64(void* p, uint64_t v); -#if defined(__cplusplus) -} /* extern "C" */ -#endif /* __cplusplus */ -#define BrotliUnalignedRead16 __sanitizer_unaligned_load16 -#define BrotliUnalignedRead32 __sanitizer_unaligned_load32 -#define BrotliUnalignedRead64 __sanitizer_unaligned_load64 -#define BrotliUnalignedWrite64 __sanitizer_unaligned_store64 -#else -static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) { - return *(const uint16_t*)p; -} -static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) { - return *(const uint32_t*)p; -} -#if (BROTLI_64_BITS) -static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { - return *(const uint64_t*)p; -} -static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { - *(uint64_t*)p = v; -} -#else /* BROTLI_64_BITS */ -/* Avoid emitting LDRD / STRD, which require properly aligned address. */ -/* If __attribute__(aligned) is available, use that. Otherwise, memcpy. */ - -#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) -typedef BROTLI_ALIGNED(1) uint64_t brotli_unaligned_uint64_t; - -static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { - return (uint64_t) ((brotli_unaligned_uint64_t*) p)[0]; -} -static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { - brotli_unaligned_uint64_t* dwords = (brotli_unaligned_uint64_t*) p; - dwords[0] = (brotli_unaligned_uint64_t) v; -} -#else /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */ -static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { - uint64_t v; - memcpy(&v, p, sizeof(uint64_t)); - return v; -} - -static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { - memcpy(p, &v, sizeof(uint64_t)); -} -#endif /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */ -#endif /* BROTLI_64_BITS */ -#endif /* ASAN / TSAN / MSAN */ -#endif /* BROTLI_ALIGNED_READ */ - -#if BROTLI_LITTLE_ENDIAN -/* Straight endianness. Just read / write values. */ -#define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16 -#define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32 -#define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64 -#define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64 -#elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */ -/* Explain compiler to byte-swap values. */ -#define BROTLI_BSWAP16_(V) ((uint16_t)( \ - (((V) & 0xFFU) << 8) | \ - (((V) >> 8) & 0xFFU))) -static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { - uint16_t value = BrotliUnalignedRead16(p); - return BROTLI_BSWAP16_(value); -} -#define BROTLI_BSWAP32_(V) ( \ - (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \ - (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU)) -static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { - uint32_t value = BrotliUnalignedRead32(p); - return BROTLI_BSWAP32_(value); -} -#define BROTLI_BSWAP64_(V) ( \ - (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \ - (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \ - (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \ - (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU)) -static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { - uint64_t value = BrotliUnalignedRead64(p); - return BROTLI_BSWAP64_(value); -} -static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { - uint64_t value = BROTLI_BSWAP64_(v); - BrotliUnalignedWrite64(p, value); -} -#else /* BROTLI_LITTLE_ENDIAN */ -/* Read / store values byte-wise; hopefully compiler will understand. */ -static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { - const uint8_t* in = (const uint8_t*)p; - return (uint16_t)(in[0] | (in[1] << 8)); -} -static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { - const uint8_t* in = (const uint8_t*)p; - uint32_t value = (uint32_t)(in[0]); - value |= (uint32_t)(in[1]) << 8; - value |= (uint32_t)(in[2]) << 16; - value |= (uint32_t)(in[3]) << 24; - return value; -} -static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { - const uint8_t* in = (const uint8_t*)p; - uint64_t value = (uint64_t)(in[0]); - value |= (uint64_t)(in[1]) << 8; - value |= (uint64_t)(in[2]) << 16; - value |= (uint64_t)(in[3]) << 24; - value |= (uint64_t)(in[4]) << 32; - value |= (uint64_t)(in[5]) << 40; - value |= (uint64_t)(in[6]) << 48; - value |= (uint64_t)(in[7]) << 56; - return value; -} -static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { - uint8_t* out = (uint8_t*)p; - out[0] = (uint8_t)v; - out[1] = (uint8_t)(v >> 8); - out[2] = (uint8_t)(v >> 16); - out[3] = (uint8_t)(v >> 24); - out[4] = (uint8_t)(v >> 32); - out[5] = (uint8_t)(v >> 40); - out[6] = (uint8_t)(v >> 48); - out[7] = (uint8_t)(v >> 56); -} -#endif /* BROTLI_LITTLE_ENDIAN */ - -/* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */ -#if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) -#define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x)) -#else -#define BROTLI_IS_CONSTANT(x) (!!0) -#endif - -#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) -#define BROTLI_HAS_UBFX (!!1) -#else -#define BROTLI_HAS_UBFX (!!0) -#endif - -#if defined(BROTLI_ENABLE_LOG) -#define BROTLI_DCHECK(x) assert(x) -#define BROTLI_LOG(x) printf x -#else -#define BROTLI_DCHECK(x) -#define BROTLI_LOG(x) -#endif - -#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) -static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) { - fprintf(stderr, "%s:%d (%s)\n", f, l, fn); - fflush(stderr); -} -#define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__) -#else -#define BROTLI_DUMP() (void)(0) -#endif - -/* TODO: add appropriate icc/sunpro/arm/ibm/ti checks. */ -#if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \ - !defined(BROTLI_BUILD_NO_RBIT) -#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) -/* TODO: detect ARMv6T2 and enable this code for it. */ -static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) { - brotli_reg_t output; - __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input)); - return output; -} -#define BROTLI_RBIT(x) BrotliRBit(x) -#endif /* armv7 / armv8 */ -#endif /* gcc || clang */ -#if !defined(BROTLI_RBIT) -static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ } -#endif /* BROTLI_RBIT */ - -#define BROTLI_REPEAT(N, X) { \ - if ((N & 1) != 0) {X;} \ - if ((N & 2) != 0) {X; X;} \ - if ((N & 4) != 0) {X; X; X; X;} \ -} - -#define BROTLI_UNUSED(X) (void)(X) - -#define BROTLI_MIN_MAX(T) \ - static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \ - static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; } -BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int) -BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t) -#undef BROTLI_MIN_MAX -#define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B))) -#define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B))) - -#define BROTLI_SWAP(T, A, I, J) { \ - T __brotli_swap_tmp = (A)[(I)]; \ - (A)[(I)] = (A)[(J)]; \ - (A)[(J)] = __brotli_swap_tmp; \ -} - -/* Default brotli_alloc_func */ -static void* BrotliDefaultAllocFunc(void* opaque, size_t size) { - BROTLI_UNUSED(opaque); - return malloc(size); -} - -/* Default brotli_free_func */ -static void BrotliDefaultFreeFunc(void* opaque, void* address) { - BROTLI_UNUSED(opaque); - free(address); -} - -BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) { - BROTLI_UNUSED(&BrotliSuppressUnusedFunctions); - BROTLI_UNUSED(&BrotliUnalignedRead16); - BROTLI_UNUSED(&BrotliUnalignedRead32); - BROTLI_UNUSED(&BrotliUnalignedRead64); - BROTLI_UNUSED(&BrotliUnalignedWrite64); - BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE); - BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE); - BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE); - BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE); - BROTLI_UNUSED(&BrotliRBit); - BROTLI_UNUSED(&brotli_min_double); - BROTLI_UNUSED(&brotli_max_double); - BROTLI_UNUSED(&brotli_min_float); - BROTLI_UNUSED(&brotli_max_float); - BROTLI_UNUSED(&brotli_min_int); - BROTLI_UNUSED(&brotli_max_int); - BROTLI_UNUSED(&brotli_min_size_t); - BROTLI_UNUSED(&brotli_max_size_t); - BROTLI_UNUSED(&brotli_min_uint32_t); - BROTLI_UNUSED(&brotli_max_uint32_t); - BROTLI_UNUSED(&brotli_min_uint8_t); - BROTLI_UNUSED(&brotli_max_uint8_t); - BROTLI_UNUSED(&BrotliDefaultAllocFunc); - BROTLI_UNUSED(&BrotliDefaultFreeFunc); -#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) - BROTLI_UNUSED(&BrotliDump); -#endif -} - -#endif /* BROTLI_COMMON_PLATFORM_H_ */ +/* Copyright 2016 Google Inc. All Rights Reserved. + + Distributed under MIT license. + See file LICENSE for detail or copy at https://opensource.org/licenses/MIT +*/ + +/* Macros for compiler / platform specific features and build options. + + Build options are: + * BROTLI_BUILD_32_BIT disables 64-bit optimizations + * BROTLI_BUILD_64_BIT forces to use 64-bit optimizations + * BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations + * BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations + * BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations + * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned + read and overlapping memcpy; this reduces decompression speed by 5% + * BROTLI_BUILD_NO_RBIT disables "rbit" optimization for ARM CPUs + * BROTLI_DEBUG dumps file name and line number when decoder detects stream + or memory error + * BROTLI_ENABLE_LOG enables asserts and dumps various state information +*/ + +#ifndef BROTLI_COMMON_PLATFORM_H_ +#define BROTLI_COMMON_PLATFORM_H_ + +#include <string.h> /* memcpy */ +#include <stdlib.h> /* malloc, free */ + +#include <brotli/port.h> +#include <brotli/types.h> + +#if defined(OS_LINUX) || defined(OS_CYGWIN) +#include <endian.h> +#elif defined(OS_FREEBSD) +#include <machine/endian.h> +#elif defined(OS_MACOSX) +#include <machine/endian.h> +/* Let's try and follow the Linux convention */ +#define BROTLI_X_BYTE_ORDER BYTE_ORDER +#define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN +#define BROTLI_X_BIG_ENDIAN BIG_ENDIAN +#endif + +#if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG) +#include <assert.h> +#include <stdio.h> +#endif + +/* The following macros were borrowed from https://github.com/nemequ/hedley + * with permission of original author - Evan Nemerson <evan@nemerson.com> */ + +/* >>> >>> >>> hedley macros */ + +/* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable + compilers. + +To apply compiler hint, enclose the branching condition into macros, like this: + + if (BROTLI_PREDICT_TRUE(zero == 0)) { + // main execution path + } else { + // compiler should place this code outside of main execution path + } + +OR: + + if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) { + // compiler should place this code outside of main execution path + } + +*/ +#if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ + BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \ + BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ + BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ + BROTLI_TI_VERSION_CHECK(7, 3, 0) || \ + BROTLI_TINYC_VERSION_CHECK(0, 9, 27) +#define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) +#define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0)) +#else +#define BROTLI_PREDICT_FALSE(x) (x) +#define BROTLI_PREDICT_TRUE(x) (x) +#endif + +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ + !defined(__cplusplus) +#define BROTLI_RESTRICT restrict +#elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \ + BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ + BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ + BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ + BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \ + BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ + BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \ + (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus)) +#define BROTLI_RESTRICT __restrict +#elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus) +#define BROTLI_RESTRICT _Restrict +#else +#define BROTLI_RESTRICT +#endif + +#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ + (defined(__cplusplus) && (__cplusplus >= 199711L)) +#define BROTLI_MAYBE_INLINE inline +#elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \ + BROTLI_ARM_VERSION_CHECK(6, 2, 0) +#define BROTLI_MAYBE_INLINE __inline__ +#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \ + BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0) +#define BROTLI_MAYBE_INLINE __inline +#else +#define BROTLI_MAYBE_INLINE +#endif + +#if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ + BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ + BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ + BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ + BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ + (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) +#define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__)) +#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) +#define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline +#elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus) +#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;") +#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0) +#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced") +#else +#define BROTLI_INLINE BROTLI_MAYBE_INLINE +#endif + +#if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ + BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ + BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ + BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ + BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ + (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) +#define BROTLI_NOINLINE __attribute__((__noinline__)) +#elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0) +#define BROTLI_NOINLINE __declspec(noinline) +#elif BROTLI_PGI_VERSION_CHECK(10, 2, 0) +#define BROTLI_NOINLINE _Pragma("noinline") +#elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus) +#define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;") +#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0) +#define BROTLI_NOINLINE _Pragma("inline=never") +#else +#define BROTLI_NOINLINE +#endif + +/* BROTLI_INTERNAL could be defined to override visibility, e.g. for tests. */ +#if !defined(BROTLI_INTERNAL) +#if defined(_WIN32) || defined(__CYGWIN__) +#define BROTLI_INTERNAL +#elif BROTLI_GNUC_VERSION_CHECK(3, 3, 0) || \ + BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ + BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ + BROTLI_IBM_VERSION_CHECK(13, 1, 0) || \ + BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ + (BROTLI_TI_VERSION_CHECK(7, 3, 0) && \ + defined(__TI_GNU_ATTRIBUTE_SUPPORT__) && defined(__TI_EABI__)) +#define BROTLI_INTERNAL __attribute__ ((visibility ("hidden"))) +#else +#define BROTLI_INTERNAL +#endif +#endif + +/* <<< <<< <<< end of hedley macros. */ + +#if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) +#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused)) +#else +#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE +#endif + +#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) +#define BROTLI_ALIGNED(N) __attribute__((aligned(N))) +#else +#define BROTLI_ALIGNED(N) +#endif + +#if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \ + (defined(M_ARM) && (M_ARM == 7)) +#define BROTLI_TARGET_ARMV7 +#endif /* ARMv7 */ + +#if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \ + defined(__aarch64__) || defined(__ARM64_ARCH_8__) +#define BROTLI_TARGET_ARMV8_ANY + +#if defined(__ARM_32BIT_STATE) +#define BROTLI_TARGET_ARMV8_32 +#elif defined(__ARM_64BIT_STATE) +#define BROTLI_TARGET_ARMV8_64 +#endif + +#endif /* ARMv8 */ + +#if defined(__ARM_NEON__) || defined(__ARM_NEON) +#define BROTLI_TARGET_NEON +#endif + +#if defined(__i386) || defined(_M_IX86) +#define BROTLI_TARGET_X86 +#endif + +#if defined(__x86_64__) || defined(_M_X64) +#define BROTLI_TARGET_X64 +#endif + +#if defined(__PPC64__) +#define BROTLI_TARGET_POWERPC64 +#endif + +#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64 +#define BROTLI_TARGET_RISCV64 +#endif + +#if defined(BROTLI_BUILD_64_BIT) +#define BROTLI_64_BITS 1 +#elif defined(BROTLI_BUILD_32_BIT) +#define BROTLI_64_BITS 0 +#elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \ + defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) +#define BROTLI_64_BITS 1 +#else +#define BROTLI_64_BITS 0 +#endif + +#if (BROTLI_64_BITS) +#define brotli_reg_t uint64_t +#else +#define brotli_reg_t uint32_t +#endif + +#if defined(BROTLI_BUILD_BIG_ENDIAN) +#define BROTLI_BIG_ENDIAN 1 +#elif defined(BROTLI_BUILD_LITTLE_ENDIAN) +#define BROTLI_LITTLE_ENDIAN 1 +#elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL) +/* Just break elif chain. */ +#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#define BROTLI_LITTLE_ENDIAN 1 +#elif defined(_WIN32) || defined(BROTLI_TARGET_X64) +/* Win32 & x64 can currently always be assumed to be little endian */ +#define BROTLI_LITTLE_ENDIAN 1 +#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#define BROTLI_BIG_ENDIAN 1 +#elif defined(BROTLI_X_BYTE_ORDER) +#if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN +#define BROTLI_LITTLE_ENDIAN 1 +#elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN +#define BROTLI_BIG_ENDIAN 1 +#endif +#endif /* BROTLI_X_BYTE_ORDER */ + +#if !defined(BROTLI_LITTLE_ENDIAN) +#define BROTLI_LITTLE_ENDIAN 0 +#endif + +#if !defined(BROTLI_BIG_ENDIAN) +#define BROTLI_BIG_ENDIAN 0 +#endif + +#if defined(BROTLI_X_BYTE_ORDER) +#undef BROTLI_X_BYTE_ORDER +#undef BROTLI_X_LITTLE_ENDIAN +#undef BROTLI_X_BIG_ENDIAN +#endif + +#if defined(BROTLI_BUILD_PORTABLE) +#define BROTLI_ALIGNED_READ (!!1) +#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \ + defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \ + defined(BROTLI_TARGET_RISCV64) +/* Allow unaligned read only for white-listed CPUs. */ +#define BROTLI_ALIGNED_READ (!!0) +#else +#define BROTLI_ALIGNED_READ (!!1) +#endif + +#if BROTLI_ALIGNED_READ +/* Portable unaligned memory access: read / write values via memcpy. */ +static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) { + uint16_t t; + memcpy(&t, p, sizeof t); + return t; +} +static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) { + uint32_t t; + memcpy(&t, p, sizeof t); + return t; +} +static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { + uint64_t t; + memcpy(&t, p, sizeof t); + return t; +} +static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { + memcpy(p, &v, sizeof v); +} +#else /* BROTLI_ALIGNED_READ */ +/* Unaligned memory access is allowed: just cast pointer to requested type. */ +#if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ + defined(MEMORY_SANITIZER) +/* Consider we have an unaligned load/store of 4 bytes from address 0x...05. + AddressSanitizer will treat it as a 3-byte access to the range 05:07 and + will miss a bug if 08 is the first unaddressable byte. + ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will + miss a race between this access and some other accesses to 08. + MemorySanitizer will correctly propagate the shadow on unaligned stores + and correctly report bugs on unaligned loads, but it may not properly + update and report the origin of the uninitialized memory. + For all three tools, replacing an unaligned access with a tool-specific + callback solves the problem. */ +#if defined(__cplusplus) +extern "C" { +#endif /* __cplusplus */ + uint16_t __sanitizer_unaligned_load16(const void* p); + uint32_t __sanitizer_unaligned_load32(const void* p); + uint64_t __sanitizer_unaligned_load64(const void* p); + void __sanitizer_unaligned_store64(void* p, uint64_t v); +#if defined(__cplusplus) +} /* extern "C" */ +#endif /* __cplusplus */ +#define BrotliUnalignedRead16 __sanitizer_unaligned_load16 +#define BrotliUnalignedRead32 __sanitizer_unaligned_load32 +#define BrotliUnalignedRead64 __sanitizer_unaligned_load64 +#define BrotliUnalignedWrite64 __sanitizer_unaligned_store64 +#else +static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) { + return *(const uint16_t*)p; +} +static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) { + return *(const uint32_t*)p; +} +#if (BROTLI_64_BITS) +static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { + return *(const uint64_t*)p; +} +static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { + *(uint64_t*)p = v; +} +#else /* BROTLI_64_BITS */ +/* Avoid emitting LDRD / STRD, which require properly aligned address. */ +/* If __attribute__(aligned) is available, use that. Otherwise, memcpy. */ + +#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) +typedef BROTLI_ALIGNED(1) uint64_t brotli_unaligned_uint64_t; + +static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { + return (uint64_t) ((brotli_unaligned_uint64_t*) p)[0]; +} +static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { + brotli_unaligned_uint64_t* dwords = (brotli_unaligned_uint64_t*) p; + dwords[0] = (brotli_unaligned_uint64_t) v; +} +#else /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */ +static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { + uint64_t v; + memcpy(&v, p, sizeof(uint64_t)); + return v; +} + +static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { + memcpy(p, &v, sizeof(uint64_t)); +} +#endif /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */ +#endif /* BROTLI_64_BITS */ +#endif /* ASAN / TSAN / MSAN */ +#endif /* BROTLI_ALIGNED_READ */ + +#if BROTLI_LITTLE_ENDIAN +/* Straight endianness. Just read / write values. */ +#define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16 +#define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32 +#define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64 +#define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64 +#elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */ +/* Explain compiler to byte-swap values. */ +#define BROTLI_BSWAP16_(V) ((uint16_t)( \ + (((V) & 0xFFU) << 8) | \ + (((V) >> 8) & 0xFFU))) +static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { + uint16_t value = BrotliUnalignedRead16(p); + return BROTLI_BSWAP16_(value); +} +#define BROTLI_BSWAP32_(V) ( \ + (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \ + (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU)) +static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { + uint32_t value = BrotliUnalignedRead32(p); + return BROTLI_BSWAP32_(value); +} +#define BROTLI_BSWAP64_(V) ( \ + (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \ + (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \ + (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \ + (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU)) +static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { + uint64_t value = BrotliUnalignedRead64(p); + return BROTLI_BSWAP64_(value); +} +static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { + uint64_t value = BROTLI_BSWAP64_(v); + BrotliUnalignedWrite64(p, value); +} +#else /* BROTLI_LITTLE_ENDIAN */ +/* Read / store values byte-wise; hopefully compiler will understand. */ +static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { + const uint8_t* in = (const uint8_t*)p; + return (uint16_t)(in[0] | (in[1] << 8)); +} +static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { + const uint8_t* in = (const uint8_t*)p; + uint32_t value = (uint32_t)(in[0]); + value |= (uint32_t)(in[1]) << 8; + value |= (uint32_t)(in[2]) << 16; + value |= (uint32_t)(in[3]) << 24; + return value; +} +static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { + const uint8_t* in = (const uint8_t*)p; + uint64_t value = (uint64_t)(in[0]); + value |= (uint64_t)(in[1]) << 8; + value |= (uint64_t)(in[2]) << 16; + value |= (uint64_t)(in[3]) << 24; + value |= (uint64_t)(in[4]) << 32; + value |= (uint64_t)(in[5]) << 40; + value |= (uint64_t)(in[6]) << 48; + value |= (uint64_t)(in[7]) << 56; + return value; +} +static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { + uint8_t* out = (uint8_t*)p; + out[0] = (uint8_t)v; + out[1] = (uint8_t)(v >> 8); + out[2] = (uint8_t)(v >> 16); + out[3] = (uint8_t)(v >> 24); + out[4] = (uint8_t)(v >> 32); + out[5] = (uint8_t)(v >> 40); + out[6] = (uint8_t)(v >> 48); + out[7] = (uint8_t)(v >> 56); +} +#endif /* BROTLI_LITTLE_ENDIAN */ + +/* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */ +#if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) +#define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x)) +#else +#define BROTLI_IS_CONSTANT(x) (!!0) +#endif + +#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) +#define BROTLI_HAS_UBFX (!!1) +#else +#define BROTLI_HAS_UBFX (!!0) +#endif + +#if defined(BROTLI_ENABLE_LOG) +#define BROTLI_DCHECK(x) assert(x) +#define BROTLI_LOG(x) printf x +#else +#define BROTLI_DCHECK(x) +#define BROTLI_LOG(x) +#endif + +#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) +static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) { + fprintf(stderr, "%s:%d (%s)\n", f, l, fn); + fflush(stderr); +} +#define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__) +#else +#define BROTLI_DUMP() (void)(0) +#endif + +/* TODO: add appropriate icc/sunpro/arm/ibm/ti checks. */ +#if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \ + !defined(BROTLI_BUILD_NO_RBIT) +#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) +/* TODO: detect ARMv6T2 and enable this code for it. */ +static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) { + brotli_reg_t output; + __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input)); + return output; +} +#define BROTLI_RBIT(x) BrotliRBit(x) +#endif /* armv7 / armv8 */ +#endif /* gcc || clang */ +#if !defined(BROTLI_RBIT) +static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ } +#endif /* BROTLI_RBIT */ + +#define BROTLI_REPEAT(N, X) { \ + if ((N & 1) != 0) {X;} \ + if ((N & 2) != 0) {X; X;} \ + if ((N & 4) != 0) {X; X; X; X;} \ +} + +#define BROTLI_UNUSED(X) (void)(X) + +#define BROTLI_MIN_MAX(T) \ + static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \ + static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; } +BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int) +BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t) +#undef BROTLI_MIN_MAX +#define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B))) +#define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B))) + +#define BROTLI_SWAP(T, A, I, J) { \ + T __brotli_swap_tmp = (A)[(I)]; \ + (A)[(I)] = (A)[(J)]; \ + (A)[(J)] = __brotli_swap_tmp; \ +} + +/* Default brotli_alloc_func */ +static void* BrotliDefaultAllocFunc(void* opaque, size_t size) { + BROTLI_UNUSED(opaque); + return malloc(size); +} + +/* Default brotli_free_func */ +static void BrotliDefaultFreeFunc(void* opaque, void* address) { + BROTLI_UNUSED(opaque); + free(address); +} + +BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) { + BROTLI_UNUSED(&BrotliSuppressUnusedFunctions); + BROTLI_UNUSED(&BrotliUnalignedRead16); + BROTLI_UNUSED(&BrotliUnalignedRead32); + BROTLI_UNUSED(&BrotliUnalignedRead64); + BROTLI_UNUSED(&BrotliUnalignedWrite64); + BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE); + BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE); + BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE); + BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE); + BROTLI_UNUSED(&BrotliRBit); + BROTLI_UNUSED(&brotli_min_double); + BROTLI_UNUSED(&brotli_max_double); + BROTLI_UNUSED(&brotli_min_float); + BROTLI_UNUSED(&brotli_max_float); + BROTLI_UNUSED(&brotli_min_int); + BROTLI_UNUSED(&brotli_max_int); + BROTLI_UNUSED(&brotli_min_size_t); + BROTLI_UNUSED(&brotli_max_size_t); + BROTLI_UNUSED(&brotli_min_uint32_t); + BROTLI_UNUSED(&brotli_max_uint32_t); + BROTLI_UNUSED(&brotli_min_uint8_t); + BROTLI_UNUSED(&brotli_max_uint8_t); + BROTLI_UNUSED(&BrotliDefaultAllocFunc); + BROTLI_UNUSED(&BrotliDefaultFreeFunc); +#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) + BROTLI_UNUSED(&BrotliDump); +#endif +} + +#endif /* BROTLI_COMMON_PLATFORM_H_ */ diff --git a/contrib/libs/brotli/common/transform.c b/contrib/libs/brotli/common/transform.c index 426e635fd3..6c51e09373 100644 --- a/contrib/libs/brotli/common/transform.c +++ b/contrib/libs/brotli/common/transform.c @@ -1,235 +1,235 @@ -/* Copyright 2013 Google Inc. All Rights Reserved. - - Distributed under MIT license. - See file LICENSE for detail or copy at https://opensource.org/licenses/MIT -*/ - -#include "./transform.h" - -#if defined(__cplusplus) || defined(c_plusplus) -extern "C" { -#endif - -/* RFC 7932 transforms string data */ -static const char kPrefixSuffix[217] = - "\1 \2, \10 of the \4 of \2s \1.\5 and \4 " -/* 0x _0 _2 __5 _E _3 _6 _8 _E */ - "in \1\"\4 to \2\">\1\n\2. \1]\5 for \3 a \6 " -/* 2x _3_ _5 _A_ _D_ _F _2 _4 _A _E */ - "that \1\'\6 with \6 from \4 by \1(\6. T" -/* 4x _5_ _7 _E _5 _A _C */ - "he \4 on \4 as \4 is \4ing \2\n\t\1:\3ed " -/* 6x _3 _8 _D _2 _7_ _ _A _C */ - "\2=\"\4 at \3ly \1,\2=\'\5.com/\7. This \5" -/* 8x _0 _ _3 _8 _C _E _ _1 _7 _F */ - " not \3er \3al \4ful \4ive \5less \4es" -/* Ax _5 _9 _D _2 _7 _D */ - "t \4ize \2\xc2\xa0\4ous \5 the \2e \0"; -/* Cx _2 _7___ ___ _A _F _5 _8 */ - -static const uint16_t kPrefixSuffixMap[50] = { - 0x00, 0x02, 0x05, 0x0E, 0x13, 0x16, 0x18, 0x1E, 0x23, 0x25, - 0x2A, 0x2D, 0x2F, 0x32, 0x34, 0x3A, 0x3E, 0x45, 0x47, 0x4E, - 0x55, 0x5A, 0x5C, 0x63, 0x68, 0x6D, 0x72, 0x77, 0x7A, 0x7C, - 0x80, 0x83, 0x88, 0x8C, 0x8E, 0x91, 0x97, 0x9F, 0xA5, 0xA9, - 0xAD, 0xB2, 0xB7, 0xBD, 0xC2, 0xC7, 0xCA, 0xCF, 0xD5, 0xD8 -}; - -/* RFC 7932 transforms */ -static const uint8_t kTransformsData[] = { - 49, BROTLI_TRANSFORM_IDENTITY, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 0, - 0, BROTLI_TRANSFORM_IDENTITY, 0, - 49, BROTLI_TRANSFORM_OMIT_FIRST_1, 49, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0, - 49, BROTLI_TRANSFORM_IDENTITY, 47, - 0, BROTLI_TRANSFORM_IDENTITY, 49, - 4, BROTLI_TRANSFORM_IDENTITY, 0, - 49, BROTLI_TRANSFORM_IDENTITY, 3, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 6, - 49, BROTLI_TRANSFORM_OMIT_FIRST_2, 49, - 49, BROTLI_TRANSFORM_OMIT_LAST_1, 49, - 1, BROTLI_TRANSFORM_IDENTITY, 0, - 49, BROTLI_TRANSFORM_IDENTITY, 1, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0, - 49, BROTLI_TRANSFORM_IDENTITY, 7, - 49, BROTLI_TRANSFORM_IDENTITY, 9, - 48, BROTLI_TRANSFORM_IDENTITY, 0, - 49, BROTLI_TRANSFORM_IDENTITY, 8, - 49, BROTLI_TRANSFORM_IDENTITY, 5, - 49, BROTLI_TRANSFORM_IDENTITY, 10, - 49, BROTLI_TRANSFORM_IDENTITY, 11, - 49, BROTLI_TRANSFORM_OMIT_LAST_3, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 13, - 49, BROTLI_TRANSFORM_IDENTITY, 14, - 49, BROTLI_TRANSFORM_OMIT_FIRST_3, 49, - 49, BROTLI_TRANSFORM_OMIT_LAST_2, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 15, - 49, BROTLI_TRANSFORM_IDENTITY, 16, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 12, - 5, BROTLI_TRANSFORM_IDENTITY, 49, - 0, BROTLI_TRANSFORM_IDENTITY, 1, - 49, BROTLI_TRANSFORM_OMIT_FIRST_4, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 18, - 49, BROTLI_TRANSFORM_IDENTITY, 17, - 49, BROTLI_TRANSFORM_IDENTITY, 19, - 49, BROTLI_TRANSFORM_IDENTITY, 20, - 49, BROTLI_TRANSFORM_OMIT_FIRST_5, 49, - 49, BROTLI_TRANSFORM_OMIT_FIRST_6, 49, - 47, BROTLI_TRANSFORM_IDENTITY, 49, - 49, BROTLI_TRANSFORM_OMIT_LAST_4, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 22, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 23, - 49, BROTLI_TRANSFORM_IDENTITY, 24, - 49, BROTLI_TRANSFORM_IDENTITY, 25, - 49, BROTLI_TRANSFORM_OMIT_LAST_7, 49, - 49, BROTLI_TRANSFORM_OMIT_LAST_1, 26, - 49, BROTLI_TRANSFORM_IDENTITY, 27, - 49, BROTLI_TRANSFORM_IDENTITY, 28, - 0, BROTLI_TRANSFORM_IDENTITY, 12, - 49, BROTLI_TRANSFORM_IDENTITY, 29, - 49, BROTLI_TRANSFORM_OMIT_FIRST_9, 49, - 49, BROTLI_TRANSFORM_OMIT_FIRST_7, 49, - 49, BROTLI_TRANSFORM_OMIT_LAST_6, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 21, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1, - 49, BROTLI_TRANSFORM_OMIT_LAST_8, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 31, - 49, BROTLI_TRANSFORM_IDENTITY, 32, - 47, BROTLI_TRANSFORM_IDENTITY, 3, - 49, BROTLI_TRANSFORM_OMIT_LAST_5, 49, - 49, BROTLI_TRANSFORM_OMIT_LAST_9, 49, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 8, - 5, BROTLI_TRANSFORM_IDENTITY, 21, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 0, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 10, - 49, BROTLI_TRANSFORM_IDENTITY, 30, - 0, BROTLI_TRANSFORM_IDENTITY, 5, - 35, BROTLI_TRANSFORM_IDENTITY, 49, - 47, BROTLI_TRANSFORM_IDENTITY, 2, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 17, - 49, BROTLI_TRANSFORM_IDENTITY, 36, - 49, BROTLI_TRANSFORM_IDENTITY, 33, - 5, BROTLI_TRANSFORM_IDENTITY, 0, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 21, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5, - 49, BROTLI_TRANSFORM_IDENTITY, 37, - 0, BROTLI_TRANSFORM_IDENTITY, 30, - 49, BROTLI_TRANSFORM_IDENTITY, 38, - 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 0, - 49, BROTLI_TRANSFORM_IDENTITY, 39, - 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 49, - 49, BROTLI_TRANSFORM_IDENTITY, 34, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 8, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12, - 0, BROTLI_TRANSFORM_IDENTITY, 21, - 49, BROTLI_TRANSFORM_IDENTITY, 40, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12, - 49, BROTLI_TRANSFORM_IDENTITY, 41, - 49, BROTLI_TRANSFORM_IDENTITY, 42, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 17, - 49, BROTLI_TRANSFORM_IDENTITY, 43, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 10, - 0, BROTLI_TRANSFORM_IDENTITY, 34, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33, - 49, BROTLI_TRANSFORM_IDENTITY, 44, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 5, - 45, BROTLI_TRANSFORM_IDENTITY, 49, - 0, BROTLI_TRANSFORM_IDENTITY, 33, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 30, - 49, BROTLI_TRANSFORM_IDENTITY, 46, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 1, - 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33, - 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 30, - 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 1, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 33, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 21, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 12, - 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 5, - 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 34, - 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 12, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30, - 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 34, - 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34, -}; - -static BrotliTransforms kBrotliTransforms = { - sizeof(kPrefixSuffix), - (const uint8_t*)kPrefixSuffix, - kPrefixSuffixMap, - sizeof(kTransformsData) / (3 * sizeof(kTransformsData[0])), - kTransformsData, - {0, 12, 27, 23, 42, 63, 56, 48, 59, 64} -}; - -const BrotliTransforms* BrotliGetTransforms(void) { - return &kBrotliTransforms; -} - -static int ToUpperCase(uint8_t* p) { - if (p[0] < 0xC0) { - if (p[0] >= 'a' && p[0] <= 'z') { - p[0] ^= 32; - } - return 1; - } - /* An overly simplified uppercasing model for UTF-8. */ - if (p[0] < 0xE0) { - p[1] ^= 32; - return 2; - } - /* An arbitrary transform for three byte characters. */ - p[2] ^= 5; - return 3; -} - -int BrotliTransformDictionaryWord(uint8_t* dst, const uint8_t* word, int len, - const BrotliTransforms* transforms, int transform_idx) { - int idx = 0; - const uint8_t* prefix = BROTLI_TRANSFORM_PREFIX(transforms, transform_idx); - uint8_t type = BROTLI_TRANSFORM_TYPE(transforms, transform_idx); - const uint8_t* suffix = BROTLI_TRANSFORM_SUFFIX(transforms, transform_idx); - { - int prefix_len = *prefix++; - while (prefix_len--) { dst[idx++] = *prefix++; } - } - { - const int t = type; - int i = 0; - if (t <= BROTLI_TRANSFORM_OMIT_LAST_9) { - len -= t; - } else if (t >= BROTLI_TRANSFORM_OMIT_FIRST_1 - && t <= BROTLI_TRANSFORM_OMIT_FIRST_9) { - int skip = t - (BROTLI_TRANSFORM_OMIT_FIRST_1 - 1); - word += skip; - len -= skip; - } - while (i < len) { dst[idx++] = word[i++]; } - if (t == BROTLI_TRANSFORM_UPPERCASE_FIRST) { - ToUpperCase(&dst[idx - len]); - } else if (t == BROTLI_TRANSFORM_UPPERCASE_ALL) { - uint8_t* uppercase = &dst[idx - len]; - while (len > 0) { - int step = ToUpperCase(uppercase); - uppercase += step; - len -= step; - } - } - } - { - int suffix_len = *suffix++; - while (suffix_len--) { dst[idx++] = *suffix++; } - return idx; - } -} - -#if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ -#endif +/* Copyright 2013 Google Inc. All Rights Reserved. + + Distributed under MIT license. + See file LICENSE for detail or copy at https://opensource.org/licenses/MIT +*/ + +#include "./transform.h" + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +/* RFC 7932 transforms string data */ +static const char kPrefixSuffix[217] = + "\1 \2, \10 of the \4 of \2s \1.\5 and \4 " +/* 0x _0 _2 __5 _E _3 _6 _8 _E */ + "in \1\"\4 to \2\">\1\n\2. \1]\5 for \3 a \6 " +/* 2x _3_ _5 _A_ _D_ _F _2 _4 _A _E */ + "that \1\'\6 with \6 from \4 by \1(\6. T" +/* 4x _5_ _7 _E _5 _A _C */ + "he \4 on \4 as \4 is \4ing \2\n\t\1:\3ed " +/* 6x _3 _8 _D _2 _7_ _ _A _C */ + "\2=\"\4 at \3ly \1,\2=\'\5.com/\7. This \5" +/* 8x _0 _ _3 _8 _C _E _ _1 _7 _F */ + " not \3er \3al \4ful \4ive \5less \4es" +/* Ax _5 _9 _D _2 _7 _D */ + "t \4ize \2\xc2\xa0\4ous \5 the \2e \0"; +/* Cx _2 _7___ ___ _A _F _5 _8 */ + +static const uint16_t kPrefixSuffixMap[50] = { + 0x00, 0x02, 0x05, 0x0E, 0x13, 0x16, 0x18, 0x1E, 0x23, 0x25, + 0x2A, 0x2D, 0x2F, 0x32, 0x34, 0x3A, 0x3E, 0x45, 0x47, 0x4E, + 0x55, 0x5A, 0x5C, 0x63, 0x68, 0x6D, 0x72, 0x77, 0x7A, 0x7C, + 0x80, 0x83, 0x88, 0x8C, 0x8E, 0x91, 0x97, 0x9F, 0xA5, 0xA9, + 0xAD, 0xB2, 0xB7, 0xBD, 0xC2, 0xC7, 0xCA, 0xCF, 0xD5, 0xD8 +}; + +/* RFC 7932 transforms */ +static const uint8_t kTransformsData[] = { + 49, BROTLI_TRANSFORM_IDENTITY, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 0, + 0, BROTLI_TRANSFORM_IDENTITY, 0, + 49, BROTLI_TRANSFORM_OMIT_FIRST_1, 49, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0, + 49, BROTLI_TRANSFORM_IDENTITY, 47, + 0, BROTLI_TRANSFORM_IDENTITY, 49, + 4, BROTLI_TRANSFORM_IDENTITY, 0, + 49, BROTLI_TRANSFORM_IDENTITY, 3, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 6, + 49, BROTLI_TRANSFORM_OMIT_FIRST_2, 49, + 49, BROTLI_TRANSFORM_OMIT_LAST_1, 49, + 1, BROTLI_TRANSFORM_IDENTITY, 0, + 49, BROTLI_TRANSFORM_IDENTITY, 1, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0, + 49, BROTLI_TRANSFORM_IDENTITY, 7, + 49, BROTLI_TRANSFORM_IDENTITY, 9, + 48, BROTLI_TRANSFORM_IDENTITY, 0, + 49, BROTLI_TRANSFORM_IDENTITY, 8, + 49, BROTLI_TRANSFORM_IDENTITY, 5, + 49, BROTLI_TRANSFORM_IDENTITY, 10, + 49, BROTLI_TRANSFORM_IDENTITY, 11, + 49, BROTLI_TRANSFORM_OMIT_LAST_3, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 13, + 49, BROTLI_TRANSFORM_IDENTITY, 14, + 49, BROTLI_TRANSFORM_OMIT_FIRST_3, 49, + 49, BROTLI_TRANSFORM_OMIT_LAST_2, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 15, + 49, BROTLI_TRANSFORM_IDENTITY, 16, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 12, + 5, BROTLI_TRANSFORM_IDENTITY, 49, + 0, BROTLI_TRANSFORM_IDENTITY, 1, + 49, BROTLI_TRANSFORM_OMIT_FIRST_4, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 18, + 49, BROTLI_TRANSFORM_IDENTITY, 17, + 49, BROTLI_TRANSFORM_IDENTITY, 19, + 49, BROTLI_TRANSFORM_IDENTITY, 20, + 49, BROTLI_TRANSFORM_OMIT_FIRST_5, 49, + 49, BROTLI_TRANSFORM_OMIT_FIRST_6, 49, + 47, BROTLI_TRANSFORM_IDENTITY, 49, + 49, BROTLI_TRANSFORM_OMIT_LAST_4, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 22, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 23, + 49, BROTLI_TRANSFORM_IDENTITY, 24, + 49, BROTLI_TRANSFORM_IDENTITY, 25, + 49, BROTLI_TRANSFORM_OMIT_LAST_7, 49, + 49, BROTLI_TRANSFORM_OMIT_LAST_1, 26, + 49, BROTLI_TRANSFORM_IDENTITY, 27, + 49, BROTLI_TRANSFORM_IDENTITY, 28, + 0, BROTLI_TRANSFORM_IDENTITY, 12, + 49, BROTLI_TRANSFORM_IDENTITY, 29, + 49, BROTLI_TRANSFORM_OMIT_FIRST_9, 49, + 49, BROTLI_TRANSFORM_OMIT_FIRST_7, 49, + 49, BROTLI_TRANSFORM_OMIT_LAST_6, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 21, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1, + 49, BROTLI_TRANSFORM_OMIT_LAST_8, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 31, + 49, BROTLI_TRANSFORM_IDENTITY, 32, + 47, BROTLI_TRANSFORM_IDENTITY, 3, + 49, BROTLI_TRANSFORM_OMIT_LAST_5, 49, + 49, BROTLI_TRANSFORM_OMIT_LAST_9, 49, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 8, + 5, BROTLI_TRANSFORM_IDENTITY, 21, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 0, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 10, + 49, BROTLI_TRANSFORM_IDENTITY, 30, + 0, BROTLI_TRANSFORM_IDENTITY, 5, + 35, BROTLI_TRANSFORM_IDENTITY, 49, + 47, BROTLI_TRANSFORM_IDENTITY, 2, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 17, + 49, BROTLI_TRANSFORM_IDENTITY, 36, + 49, BROTLI_TRANSFORM_IDENTITY, 33, + 5, BROTLI_TRANSFORM_IDENTITY, 0, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 21, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5, + 49, BROTLI_TRANSFORM_IDENTITY, 37, + 0, BROTLI_TRANSFORM_IDENTITY, 30, + 49, BROTLI_TRANSFORM_IDENTITY, 38, + 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 0, + 49, BROTLI_TRANSFORM_IDENTITY, 39, + 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 49, + 49, BROTLI_TRANSFORM_IDENTITY, 34, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 8, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12, + 0, BROTLI_TRANSFORM_IDENTITY, 21, + 49, BROTLI_TRANSFORM_IDENTITY, 40, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12, + 49, BROTLI_TRANSFORM_IDENTITY, 41, + 49, BROTLI_TRANSFORM_IDENTITY, 42, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 17, + 49, BROTLI_TRANSFORM_IDENTITY, 43, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 10, + 0, BROTLI_TRANSFORM_IDENTITY, 34, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33, + 49, BROTLI_TRANSFORM_IDENTITY, 44, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 5, + 45, BROTLI_TRANSFORM_IDENTITY, 49, + 0, BROTLI_TRANSFORM_IDENTITY, 33, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 30, + 49, BROTLI_TRANSFORM_IDENTITY, 46, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 1, + 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33, + 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 30, + 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 1, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 33, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 21, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 12, + 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 5, + 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 34, + 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 12, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30, + 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 34, + 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34, +}; + +static BrotliTransforms kBrotliTransforms = { + sizeof(kPrefixSuffix), + (const uint8_t*)kPrefixSuffix, + kPrefixSuffixMap, + sizeof(kTransformsData) / (3 * sizeof(kTransformsData[0])), + kTransformsData, + {0, 12, 27, 23, 42, 63, 56, 48, 59, 64} +}; + +const BrotliTransforms* BrotliGetTransforms(void) { + return &kBrotliTransforms; +} + +static int ToUpperCase(uint8_t* p) { + if (p[0] < 0xC0) { + if (p[0] >= 'a' && p[0] <= 'z') { + p[0] ^= 32; + } + return 1; + } + /* An overly simplified uppercasing model for UTF-8. */ + if (p[0] < 0xE0) { + p[1] ^= 32; + return 2; + } + /* An arbitrary transform for three byte characters. */ + p[2] ^= 5; + return 3; +} + +int BrotliTransformDictionaryWord(uint8_t* dst, const uint8_t* word, int len, + const BrotliTransforms* transforms, int transform_idx) { + int idx = 0; + const uint8_t* prefix = BROTLI_TRANSFORM_PREFIX(transforms, transform_idx); + uint8_t type = BROTLI_TRANSFORM_TYPE(transforms, transform_idx); + const uint8_t* suffix = BROTLI_TRANSFORM_SUFFIX(transforms, transform_idx); + { + int prefix_len = *prefix++; + while (prefix_len--) { dst[idx++] = *prefix++; } + } + { + const int t = type; + int i = 0; + if (t <= BROTLI_TRANSFORM_OMIT_LAST_9) { + len -= t; + } else if (t >= BROTLI_TRANSFORM_OMIT_FIRST_1 + && t <= BROTLI_TRANSFORM_OMIT_FIRST_9) { + int skip = t - (BROTLI_TRANSFORM_OMIT_FIRST_1 - 1); + word += skip; + len -= skip; + } + while (i < len) { dst[idx++] = word[i++]; } + if (t == BROTLI_TRANSFORM_UPPERCASE_FIRST) { + ToUpperCase(&dst[idx - len]); + } else if (t == BROTLI_TRANSFORM_UPPERCASE_ALL) { + uint8_t* uppercase = &dst[idx - len]; + while (len > 0) { + int step = ToUpperCase(uppercase); + uppercase += step; + len -= step; + } + } + } + { + int suffix_len = *suffix++; + while (suffix_len--) { dst[idx++] = *suffix++; } + return idx; + } +} + +#if defined(__cplusplus) || defined(c_plusplus) +} /* extern "C" */ +#endif diff --git a/contrib/libs/brotli/common/transform.h b/contrib/libs/brotli/common/transform.h index 456c12db9b..545ca63373 100644 --- a/contrib/libs/brotli/common/transform.h +++ b/contrib/libs/brotli/common/transform.h @@ -1,80 +1,80 @@ -/* transforms is a part of ABI, but not API. - - It means that there are some functions that are supposed to be in "common" - library, but header itself is not placed into include/brotli. This way, - aforementioned functions will be available only to brotli internals. - */ - -#ifndef BROTLI_COMMON_TRANSFORM_H_ -#define BROTLI_COMMON_TRANSFORM_H_ - -#include <brotli/port.h> -#include <brotli/types.h> - -#if defined(__cplusplus) || defined(c_plusplus) -extern "C" { -#endif - -enum BrotliWordTransformType { - BROTLI_TRANSFORM_IDENTITY = 0, - BROTLI_TRANSFORM_OMIT_LAST_1 = 1, - BROTLI_TRANSFORM_OMIT_LAST_2 = 2, - BROTLI_TRANSFORM_OMIT_LAST_3 = 3, - BROTLI_TRANSFORM_OMIT_LAST_4 = 4, - BROTLI_TRANSFORM_OMIT_LAST_5 = 5, - BROTLI_TRANSFORM_OMIT_LAST_6 = 6, - BROTLI_TRANSFORM_OMIT_LAST_7 = 7, - BROTLI_TRANSFORM_OMIT_LAST_8 = 8, - BROTLI_TRANSFORM_OMIT_LAST_9 = 9, - BROTLI_TRANSFORM_UPPERCASE_FIRST = 10, - BROTLI_TRANSFORM_UPPERCASE_ALL = 11, - BROTLI_TRANSFORM_OMIT_FIRST_1 = 12, - BROTLI_TRANSFORM_OMIT_FIRST_2 = 13, - BROTLI_TRANSFORM_OMIT_FIRST_3 = 14, - BROTLI_TRANSFORM_OMIT_FIRST_4 = 15, - BROTLI_TRANSFORM_OMIT_FIRST_5 = 16, - BROTLI_TRANSFORM_OMIT_FIRST_6 = 17, - BROTLI_TRANSFORM_OMIT_FIRST_7 = 18, - BROTLI_TRANSFORM_OMIT_FIRST_8 = 19, - BROTLI_TRANSFORM_OMIT_FIRST_9 = 20, - BROTLI_NUM_TRANSFORM_TYPES /* Counts transforms, not a transform itself. */ -}; - -#define BROTLI_TRANSFORMS_MAX_CUT_OFF BROTLI_TRANSFORM_OMIT_LAST_9 - -typedef struct BrotliTransforms { - uint16_t prefix_suffix_size; - /* Last character must be null, so prefix_suffix_size must be at least 1. */ - const uint8_t* prefix_suffix; - const uint16_t* prefix_suffix_map; - uint32_t num_transforms; - /* Each entry is a [prefix_id, transform, suffix_id] triplet. */ - const uint8_t* transforms; - /* Indices of transforms like ["", BROTLI_TRANSFORM_OMIT_LAST_#, ""]. - 0-th element corresponds to ["", BROTLI_TRANSFORM_IDENTITY, ""]. - -1, if cut-off transform does not exist. */ - int16_t cutOffTransforms[BROTLI_TRANSFORMS_MAX_CUT_OFF + 1]; -} BrotliTransforms; - -/* T is BrotliTransforms*; result is uint8_t. */ -#define BROTLI_TRANSFORM_PREFIX_ID(T, I) ((T)->transforms[((I) * 3) + 0]) -#define BROTLI_TRANSFORM_TYPE(T, I) ((T)->transforms[((I) * 3) + 1]) -#define BROTLI_TRANSFORM_SUFFIX_ID(T, I) ((T)->transforms[((I) * 3) + 2]) - -/* T is BrotliTransforms*; result is const uint8_t*. */ -#define BROTLI_TRANSFORM_PREFIX(T, I) (&(T)->prefix_suffix[ \ - (T)->prefix_suffix_map[BROTLI_TRANSFORM_PREFIX_ID(T, I)]]) -#define BROTLI_TRANSFORM_SUFFIX(T, I) (&(T)->prefix_suffix[ \ - (T)->prefix_suffix_map[BROTLI_TRANSFORM_SUFFIX_ID(T, I)]]) - -BROTLI_COMMON_API const BrotliTransforms* BrotliGetTransforms(void); - -BROTLI_COMMON_API int BrotliTransformDictionaryWord( - uint8_t* dst, const uint8_t* word, int len, - const BrotliTransforms* transforms, int transform_idx); - -#if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ -#endif - -#endif /* BROTLI_COMMON_TRANSFORM_H_ */ +/* transforms is a part of ABI, but not API. + + It means that there are some functions that are supposed to be in "common" + library, but header itself is not placed into include/brotli. This way, + aforementioned functions will be available only to brotli internals. + */ + +#ifndef BROTLI_COMMON_TRANSFORM_H_ +#define BROTLI_COMMON_TRANSFORM_H_ + +#include <brotli/port.h> +#include <brotli/types.h> + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +enum BrotliWordTransformType { + BROTLI_TRANSFORM_IDENTITY = 0, + BROTLI_TRANSFORM_OMIT_LAST_1 = 1, + BROTLI_TRANSFORM_OMIT_LAST_2 = 2, + BROTLI_TRANSFORM_OMIT_LAST_3 = 3, + BROTLI_TRANSFORM_OMIT_LAST_4 = 4, + BROTLI_TRANSFORM_OMIT_LAST_5 = 5, + BROTLI_TRANSFORM_OMIT_LAST_6 = 6, + BROTLI_TRANSFORM_OMIT_LAST_7 = 7, + BROTLI_TRANSFORM_OMIT_LAST_8 = 8, + BROTLI_TRANSFORM_OMIT_LAST_9 = 9, + BROTLI_TRANSFORM_UPPERCASE_FIRST = 10, + BROTLI_TRANSFORM_UPPERCASE_ALL = 11, + BROTLI_TRANSFORM_OMIT_FIRST_1 = 12, + BROTLI_TRANSFORM_OMIT_FIRST_2 = 13, + BROTLI_TRANSFORM_OMIT_FIRST_3 = 14, + BROTLI_TRANSFORM_OMIT_FIRST_4 = 15, + BROTLI_TRANSFORM_OMIT_FIRST_5 = 16, + BROTLI_TRANSFORM_OMIT_FIRST_6 = 17, + BROTLI_TRANSFORM_OMIT_FIRST_7 = 18, + BROTLI_TRANSFORM_OMIT_FIRST_8 = 19, + BROTLI_TRANSFORM_OMIT_FIRST_9 = 20, + BROTLI_NUM_TRANSFORM_TYPES /* Counts transforms, not a transform itself. */ +}; + +#define BROTLI_TRANSFORMS_MAX_CUT_OFF BROTLI_TRANSFORM_OMIT_LAST_9 + +typedef struct BrotliTransforms { + uint16_t prefix_suffix_size; + /* Last character must be null, so prefix_suffix_size must be at least 1. */ + const uint8_t* prefix_suffix; + const uint16_t* prefix_suffix_map; + uint32_t num_transforms; + /* Each entry is a [prefix_id, transform, suffix_id] triplet. */ + const uint8_t* transforms; + /* Indices of transforms like ["", BROTLI_TRANSFORM_OMIT_LAST_#, ""]. + 0-th element corresponds to ["", BROTLI_TRANSFORM_IDENTITY, ""]. + -1, if cut-off transform does not exist. */ + int16_t cutOffTransforms[BROTLI_TRANSFORMS_MAX_CUT_OFF + 1]; +} BrotliTransforms; + +/* T is BrotliTransforms*; result is uint8_t. */ +#define BROTLI_TRANSFORM_PREFIX_ID(T, I) ((T)->transforms[((I) * 3) + 0]) +#define BROTLI_TRANSFORM_TYPE(T, I) ((T)->transforms[((I) * 3) + 1]) +#define BROTLI_TRANSFORM_SUFFIX_ID(T, I) ((T)->transforms[((I) * 3) + 2]) + +/* T is BrotliTransforms*; result is const uint8_t*. */ +#define BROTLI_TRANSFORM_PREFIX(T, I) (&(T)->prefix_suffix[ \ + (T)->prefix_suffix_map[BROTLI_TRANSFORM_PREFIX_ID(T, I)]]) +#define BROTLI_TRANSFORM_SUFFIX(T, I) (&(T)->prefix_suffix[ \ + (T)->prefix_suffix_map[BROTLI_TRANSFORM_SUFFIX_ID(T, I)]]) + +BROTLI_COMMON_API const BrotliTransforms* BrotliGetTransforms(void); + +BROTLI_COMMON_API int BrotliTransformDictionaryWord( + uint8_t* dst, const uint8_t* word, int len, + const BrotliTransforms* transforms, int transform_idx); + +#if defined(__cplusplus) || defined(c_plusplus) +} /* extern "C" */ +#endif + +#endif /* BROTLI_COMMON_TRANSFORM_H_ */ diff --git a/contrib/libs/brotli/common/version.h b/contrib/libs/brotli/common/version.h index 0d0d0c7967..d207dcb18f 100644 --- a/contrib/libs/brotli/common/version.h +++ b/contrib/libs/brotli/common/version.h @@ -14,13 +14,13 @@ BrotliEncoderVersion methods. */ /* Semantic version, calculated as (MAJOR << 24) | (MINOR << 12) | PATCH */ -#define BROTLI_VERSION 0x1000007 - -/* This macro is used by build system to produce Libtool-friendly soname. See - https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html - */ - -/* ABI version, calculated as (CURRENT << 24) | (REVISION << 12) | AGE */ -#define BROTLI_ABI_VERSION 0x1007000 - +#define BROTLI_VERSION 0x1000007 + +/* This macro is used by build system to produce Libtool-friendly soname. See + https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html + */ + +/* ABI version, calculated as (CURRENT << 24) | (REVISION << 12) | AGE */ +#define BROTLI_ABI_VERSION 0x1007000 + #endif /* BROTLI_COMMON_VERSION_H_ */ diff --git a/contrib/libs/brotli/common/ya.make b/contrib/libs/brotli/common/ya.make index 6c4157831c..d83bb62379 100644 --- a/contrib/libs/brotli/common/ya.make +++ b/contrib/libs/brotli/common/ya.make @@ -18,7 +18,7 @@ ADDINCL(contrib/libs/brotli/include) SRCS( dictionary.c - transform.c + transform.c ) CFLAGS(-DBROTLI_BUILD_PORTABLE) diff --git a/contrib/libs/brotli/dec/bit_reader.c b/contrib/libs/brotli/dec/bit_reader.c index 722fd906dd..6aa95645d0 100644 --- a/contrib/libs/brotli/dec/bit_reader.c +++ b/contrib/libs/brotli/dec/bit_reader.c @@ -8,7 +8,7 @@ #include "./bit_reader.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) diff --git a/contrib/libs/brotli/dec/bit_reader.h b/contrib/libs/brotli/dec/bit_reader.h index c06e91419f..5a6450307b 100644 --- a/contrib/libs/brotli/dec/bit_reader.h +++ b/contrib/libs/brotli/dec/bit_reader.h @@ -11,16 +11,16 @@ #include <string.h> /* memcpy */ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif -#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1) +#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1) -static const uint32_t kBitMask[33] = { 0x00000000, +static const uint32_t kBitMask[33] = { 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF, @@ -32,24 +32,24 @@ static const uint32_t kBitMask[33] = { 0x00000000, }; static BROTLI_INLINE uint32_t BitMask(uint32_t n) { - if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) { + if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) { /* Masking with this expression turns to a single "Unsigned Bit Field Extract" UBFX instruction on ARM. */ - return ~((0xFFFFFFFFu) << n); + return ~((0xFFFFFFFFu) << n); } else { return kBitMask[n]; } } typedef struct { - brotli_reg_t val_; /* pre-fetched bits */ + brotli_reg_t val_; /* pre-fetched bits */ uint32_t bit_pos_; /* current bit-reading position in val_ */ const uint8_t* next_in; /* the byte we're reading from */ size_t avail_in; } BrotliBitReader; typedef struct { - brotli_reg_t val_; + brotli_reg_t val_; uint32_t bit_pos_; const uint8_t* next_in; size_t avail_in; @@ -58,9 +58,9 @@ typedef struct { /* Initializes the BrotliBitReader fields. */ BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br); -/* Ensures that accumulator is not empty. - May consume up to sizeof(brotli_reg_t) - 1 bytes of input. - Returns BROTLI_FALSE if data is required but there is no input available. +/* Ensures that accumulator is not empty. + May consume up to sizeof(brotli_reg_t) - 1 bytes of input. + Returns BROTLI_FALSE if data is required but there is no input available. For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned reading. */ BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br); @@ -99,27 +99,27 @@ static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount( return TO_BROTLI_BOOL(br->avail_in >= num); } -/* Guarantees that there are at least |n_bits| + 1 bits in accumulator. +/* Guarantees that there are at least |n_bits| + 1 bits in accumulator. Precondition: accumulator contains at least 1 bit. - |n_bits| should be in the range [1..24] for regular build. For portable + |n_bits| should be in the range [1..24] for regular build. For portable non-64-bit little-endian build only 16 bits are safe to request. */ static BROTLI_INLINE void BrotliFillBitWindow( BrotliBitReader* const br, uint32_t n_bits) { #if (BROTLI_64_BITS) - if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) { + if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) { if (br->bit_pos_ >= 56) { br->val_ >>= 56; br->bit_pos_ ^= 56; /* here same as -= 56 because of the if condition */ - br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8; + br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8; br->avail_in -= 7; br->next_in += 7; } - } else if ( - !BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 16)) { + } else if ( + !BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 16)) { if (br->bit_pos_ >= 48) { br->val_ >>= 48; br->bit_pos_ ^= 48; /* here same as -= 48 because of the if condition */ - br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16; + br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16; br->avail_in -= 6; br->next_in += 6; } @@ -127,17 +127,17 @@ static BROTLI_INLINE void BrotliFillBitWindow( if (br->bit_pos_ >= 32) { br->val_ >>= 32; br->bit_pos_ ^= 32; /* here same as -= 32 because of the if condition */ - br->val_ |= ((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32; + br->val_ |= ((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32; br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ; br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ; } } #else - if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) { + if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) { if (br->bit_pos_ >= 24) { br->val_ >>= 24; br->bit_pos_ ^= 24; /* here same as -= 24 because of the if condition */ - br->val_ |= BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8; + br->val_ |= BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8; br->avail_in -= 3; br->next_in += 3; } @@ -145,7 +145,7 @@ static BROTLI_INLINE void BrotliFillBitWindow( if (br->bit_pos_ >= 16) { br->val_ >>= 16; br->bit_pos_ ^= 16; /* here same as -= 16 because of the if condition */ - br->val_ |= ((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16; + br->val_ |= ((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16; br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ; br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ; } @@ -159,8 +159,8 @@ static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) { BrotliFillBitWindow(br, 17); } -/* Tries to pull one byte of input to accumulator. - Returns BROTLI_FALSE if there is no input available. */ +/* Tries to pull one byte of input to accumulator. + Returns BROTLI_FALSE if there is no input available. */ static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) { if (br->avail_in == 0) { return BROTLI_FALSE; @@ -179,8 +179,8 @@ static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) { /* Returns currently available bits. The number of valid bits could be calculated by BrotliGetAvailableBits. */ -static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked( - BrotliBitReader* const br) { +static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked( + BrotliBitReader* const br) { return br->val_ >> br->bit_pos_; } @@ -192,16 +192,16 @@ static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked( return (uint32_t)BrotliGetBitsUnmasked(br); } -/* Returns the specified number of bits from |br| without advancing bit - position. */ +/* Returns the specified number of bits from |br| without advancing bit + position. */ static BROTLI_INLINE uint32_t BrotliGetBits( BrotliBitReader* const br, uint32_t n_bits) { BrotliFillBitWindow(br, n_bits); return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); } -/* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there - is not enough input. */ +/* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there + is not enough input. */ static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits( BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { while (BrotliGetAvailableBits(br) < n_bits) { @@ -213,7 +213,7 @@ static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits( return BROTLI_TRUE; } -/* Advances the bit pos by |n_bits|. */ +/* Advances the bit pos by |n_bits|. */ static BROTLI_INLINE void BrotliDropBits( BrotliBitReader* const br, uint32_t n_bits) { br->bit_pos_ += n_bits; @@ -233,12 +233,12 @@ static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) { } /* Reads the specified number of bits from |br| and advances the bit pos. - Precondition: accumulator MUST contain at least |n_bits|. */ + Precondition: accumulator MUST contain at least |n_bits|. */ static BROTLI_INLINE void BrotliTakeBits( BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); BROTLI_LOG(("[BrotliReadBits] %d %d %d val: %6x\n", - (int)br->avail_in, (int)br->bit_pos_, (int)n_bits, (int)*val)); + (int)br->avail_in, (int)br->bit_pos_, (int)n_bits, (int)*val)); BrotliDropBits(br, n_bits); } @@ -262,8 +262,8 @@ static BROTLI_INLINE uint32_t BrotliReadBits( } } -/* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there - is not enough input. |n_bits| MUST be positive. */ +/* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there + is not enough input. |n_bits| MUST be positive. */ static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits( BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { while (BrotliGetAvailableBits(br) < n_bits) { @@ -287,7 +287,7 @@ static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) { } /* Copies remaining input bytes stored in the bit reader to the output. Value - |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be + |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be warmed up again after this. */ static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest, BrotliBitReader* br, size_t num) { diff --git a/contrib/libs/brotli/dec/decode.c b/contrib/libs/brotli/dec/decode.c index 08bd76ca16..ae4c932068 100644 --- a/contrib/libs/brotli/dec/decode.c +++ b/contrib/libs/brotli/dec/decode.c @@ -10,20 +10,20 @@ #include <string.h> /* memcpy, memset */ #include "../common/constants.h" -#include "../common/context.h" +#include "../common/context.h" #include "../common/dictionary.h" -#include "../common/platform.h" -#include "../common/transform.h" +#include "../common/platform.h" +#include "../common/transform.h" #include "../common/version.h" #include "./bit_reader.h" #include "./huffman.h" #include "./prefix.h" #include "./state.h" -#if defined(BROTLI_TARGET_NEON) -#include <arm_neon.h> -#endif - +#if defined(BROTLI_TARGET_NEON) +#include <arm_neon.h> +#endif + #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif @@ -37,7 +37,7 @@ extern "C" { (unsigned long)(idx), (unsigned long)array_name[idx])) #define HUFFMAN_TABLE_BITS 8U -#define HUFFMAN_TABLE_MASK 0xFF +#define HUFFMAN_TABLE_MASK 0xFF /* We need the slack region for the following reasons: - doing up to two 16-byte copies for fast backward copying @@ -59,16 +59,16 @@ static const uint8_t kCodeLengthPrefixValue[16] = { BROTLI_BOOL BrotliDecoderSetParameter( BrotliDecoderState* state, BrotliDecoderParameter p, uint32_t value) { - if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE; + if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE; switch (p) { case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: state->canny_ringbuffer_allocation = !!value ? 0 : 1; return BROTLI_TRUE; - case BROTLI_DECODER_PARAM_LARGE_WINDOW: - state->large_window = TO_BROTLI_BOOL(!!value); - return BROTLI_TRUE; - + case BROTLI_DECODER_PARAM_LARGE_WINDOW: + state->large_window = TO_BROTLI_BOOL(!!value); + return BROTLI_TRUE; + default: return BROTLI_FALSE; } } @@ -85,15 +85,15 @@ BrotliDecoderState* BrotliDecoderCreateInstance( BROTLI_DUMP(); return 0; } - if (!BrotliDecoderStateInit(state, alloc_func, free_func, opaque)) { - BROTLI_DUMP(); - if (!alloc_func && !free_func) { - free(state); - } else if (alloc_func && free_func) { - free_func(opaque, state); - } - return 0; - } + if (!BrotliDecoderStateInit(state, alloc_func, free_func, opaque)) { + BROTLI_DUMP(); + if (!alloc_func && !free_func) { + free(state); + } else if (alloc_func && free_func) { + free_func(opaque, state); + } + return 0; + } return state; } @@ -109,65 +109,65 @@ void BrotliDecoderDestroyInstance(BrotliDecoderState* state) { } } -/* Saves error code and converts it to BrotliDecoderResult. */ +/* Saves error code and converts it to BrotliDecoderResult. */ static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode( BrotliDecoderState* s, BrotliDecoderErrorCode e) { s->error_code = (int)e; switch (e) { case BROTLI_DECODER_SUCCESS: return BROTLI_DECODER_RESULT_SUCCESS; - + case BROTLI_DECODER_NEEDS_MORE_INPUT: return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; - + case BROTLI_DECODER_NEEDS_MORE_OUTPUT: return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; - + default: return BROTLI_DECODER_RESULT_ERROR; } } -/* Decodes WBITS by reading 1 - 7 bits, or 0x11 for "Large Window Brotli". - Precondition: bit-reader accumulator has at least 8 bits. */ -static BrotliDecoderErrorCode DecodeWindowBits(BrotliDecoderState* s, - BrotliBitReader* br) { +/* Decodes WBITS by reading 1 - 7 bits, or 0x11 for "Large Window Brotli". + Precondition: bit-reader accumulator has at least 8 bits. */ +static BrotliDecoderErrorCode DecodeWindowBits(BrotliDecoderState* s, + BrotliBitReader* br) { uint32_t n; - BROTLI_BOOL large_window = s->large_window; - s->large_window = BROTLI_FALSE; + BROTLI_BOOL large_window = s->large_window; + s->large_window = BROTLI_FALSE; BrotliTakeBits(br, 1, &n); if (n == 0) { - s->window_bits = 16; - return BROTLI_DECODER_SUCCESS; + s->window_bits = 16; + return BROTLI_DECODER_SUCCESS; } BrotliTakeBits(br, 3, &n); if (n != 0) { - s->window_bits = 17 + n; - return BROTLI_DECODER_SUCCESS; + s->window_bits = 17 + n; + return BROTLI_DECODER_SUCCESS; } BrotliTakeBits(br, 3, &n); - if (n == 1) { - if (large_window) { - BrotliTakeBits(br, 1, &n); - if (n == 1) { - return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); - } - s->large_window = BROTLI_TRUE; - return BROTLI_DECODER_SUCCESS; - } else { - return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); - } - } + if (n == 1) { + if (large_window) { + BrotliTakeBits(br, 1, &n); + if (n == 1) { + return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); + } + s->large_window = BROTLI_TRUE; + return BROTLI_DECODER_SUCCESS; + } else { + return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); + } + } if (n != 0) { - s->window_bits = 8 + n; - return BROTLI_DECODER_SUCCESS; + s->window_bits = 8 + n; + return BROTLI_DECODER_SUCCESS; } - s->window_bits = 17; - return BROTLI_DECODER_SUCCESS; + s->window_bits = 17; + return BROTLI_DECODER_SUCCESS; } static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) { -#if defined(BROTLI_TARGET_NEON) +#if defined(BROTLI_TARGET_NEON) vst1q_u8(dst, vld1q_u8(src)); #else uint32_t buffer[4]; @@ -189,7 +189,7 @@ static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8( *value = 0; return BROTLI_DECODER_SUCCESS; } - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_DECODE_UINT8_SHORT: if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) { @@ -203,7 +203,7 @@ static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8( } /* Use output value as a temporary storage. It MUST be persisted. */ *value = bits; - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_DECODE_UINT8_LONG: if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) { @@ -240,7 +240,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( break; } s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY; - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_EMPTY: if (!BrotliSafeReadBits(br, 1, &bits)) { @@ -251,7 +251,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( return BROTLI_DECODER_SUCCESS; } s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_NIBBLES: if (!BrotliSafeReadBits(br, 2, &bits)) { @@ -265,7 +265,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( break; } s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE; - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_SIZE: i = s->loop_counter; @@ -281,7 +281,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( } s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED; - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED: if (!s->is_last_metablock) { @@ -302,7 +302,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED); } s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES; - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_BYTES: if (!BrotliSafeReadBits(br, 2, &bits)) { @@ -314,7 +314,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( } s->size_nibbles = (uint8_t)bits; s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA; - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_METADATA: i = s->loop_counter; @@ -347,17 +347,17 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( static BROTLI_INLINE uint32_t DecodeSymbol(uint32_t bits, const HuffmanCode* table, BrotliBitReader* br) { - BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); - BROTLI_HC_ADJUST_TABLE_INDEX(table, bits & HUFFMAN_TABLE_MASK); - if (BROTLI_HC_FAST_LOAD_BITS(table) > HUFFMAN_TABLE_BITS) { - uint32_t nbits = BROTLI_HC_FAST_LOAD_BITS(table) - HUFFMAN_TABLE_BITS; + BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); + BROTLI_HC_ADJUST_TABLE_INDEX(table, bits & HUFFMAN_TABLE_MASK); + if (BROTLI_HC_FAST_LOAD_BITS(table) > HUFFMAN_TABLE_BITS) { + uint32_t nbits = BROTLI_HC_FAST_LOAD_BITS(table) - HUFFMAN_TABLE_BITS; BrotliDropBits(br, HUFFMAN_TABLE_BITS); - BROTLI_HC_ADJUST_TABLE_INDEX(table, - BROTLI_HC_FAST_LOAD_VALUE(table) + - ((bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits))); + BROTLI_HC_ADJUST_TABLE_INDEX(table, + BROTLI_HC_FAST_LOAD_VALUE(table) + + ((bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits))); } - BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table)); - return BROTLI_HC_FAST_LOAD_VALUE(table); + BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table)); + return BROTLI_HC_FAST_LOAD_VALUE(table); } /* Reads and decodes the next Huffman code from bit-stream. @@ -373,39 +373,39 @@ static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol( const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) { uint32_t val; uint32_t available_bits = BrotliGetAvailableBits(br); - BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); + BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); if (available_bits == 0) { - if (BROTLI_HC_FAST_LOAD_BITS(table) == 0) { - *result = BROTLI_HC_FAST_LOAD_VALUE(table); + if (BROTLI_HC_FAST_LOAD_BITS(table) == 0) { + *result = BROTLI_HC_FAST_LOAD_VALUE(table); return BROTLI_TRUE; } - return BROTLI_FALSE; /* No valid bits at all. */ + return BROTLI_FALSE; /* No valid bits at all. */ } val = (uint32_t)BrotliGetBitsUnmasked(br); - BROTLI_HC_ADJUST_TABLE_INDEX(table, val & HUFFMAN_TABLE_MASK); - if (BROTLI_HC_FAST_LOAD_BITS(table) <= HUFFMAN_TABLE_BITS) { - if (BROTLI_HC_FAST_LOAD_BITS(table) <= available_bits) { - BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table)); - *result = BROTLI_HC_FAST_LOAD_VALUE(table); + BROTLI_HC_ADJUST_TABLE_INDEX(table, val & HUFFMAN_TABLE_MASK); + if (BROTLI_HC_FAST_LOAD_BITS(table) <= HUFFMAN_TABLE_BITS) { + if (BROTLI_HC_FAST_LOAD_BITS(table) <= available_bits) { + BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table)); + *result = BROTLI_HC_FAST_LOAD_VALUE(table); return BROTLI_TRUE; } else { - return BROTLI_FALSE; /* Not enough bits for the first level. */ + return BROTLI_FALSE; /* Not enough bits for the first level. */ } } if (available_bits <= HUFFMAN_TABLE_BITS) { - return BROTLI_FALSE; /* Not enough bits to move to the second level. */ + return BROTLI_FALSE; /* Not enough bits to move to the second level. */ } /* Speculatively drop HUFFMAN_TABLE_BITS. */ - val = (val & BitMask(BROTLI_HC_FAST_LOAD_BITS(table))) >> HUFFMAN_TABLE_BITS; + val = (val & BitMask(BROTLI_HC_FAST_LOAD_BITS(table))) >> HUFFMAN_TABLE_BITS; available_bits -= HUFFMAN_TABLE_BITS; - BROTLI_HC_ADJUST_TABLE_INDEX(table, BROTLI_HC_FAST_LOAD_VALUE(table) + val); - if (available_bits < BROTLI_HC_FAST_LOAD_BITS(table)) { - return BROTLI_FALSE; /* Not enough bits for the second level. */ + BROTLI_HC_ADJUST_TABLE_INDEX(table, BROTLI_HC_FAST_LOAD_VALUE(table) + val); + if (available_bits < BROTLI_HC_FAST_LOAD_BITS(table)) { + return BROTLI_FALSE; /* Not enough bits for the second level. */ } - BrotliDropBits(br, HUFFMAN_TABLE_BITS + BROTLI_HC_FAST_LOAD_BITS(table)); - *result = BROTLI_HC_FAST_LOAD_VALUE(table); + BrotliDropBits(br, HUFFMAN_TABLE_BITS + BROTLI_HC_FAST_LOAD_BITS(table)); + *result = BROTLI_HC_FAST_LOAD_VALUE(table); return BROTLI_TRUE; } @@ -428,10 +428,10 @@ static BROTLI_INLINE void PreloadSymbol(int safe, if (safe) { return; } - BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); - BROTLI_HC_ADJUST_TABLE_INDEX(table, BrotliGetBits(br, HUFFMAN_TABLE_BITS)); - *bits = BROTLI_HC_FAST_LOAD_BITS(table); - *value = BROTLI_HC_FAST_LOAD_VALUE(table); + BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); + BROTLI_HC_ADJUST_TABLE_INDEX(table, BrotliGetBits(br, HUFFMAN_TABLE_BITS)); + *bits = BROTLI_HC_FAST_LOAD_BITS(table); + *value = BROTLI_HC_FAST_LOAD_VALUE(table); } /* Decodes the next Huffman code using data prepared by PreloadSymbol. @@ -445,11 +445,11 @@ static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table, uint32_t val = BrotliGet16BitsUnmasked(br); const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value; uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS)); - BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(ext); + BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(ext); BrotliDropBits(br, HUFFMAN_TABLE_BITS); - BROTLI_HC_ADJUST_TABLE_INDEX(ext, (val >> HUFFMAN_TABLE_BITS) & mask); - BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(ext)); - result = BROTLI_HC_FAST_LOAD_VALUE(ext); + BROTLI_HC_ADJUST_TABLE_INDEX(ext, (val >> HUFFMAN_TABLE_BITS) & mask); + BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(ext)); + result = BROTLI_HC_FAST_LOAD_VALUE(ext); } else { BrotliDropBits(br, *bits); } @@ -467,11 +467,11 @@ static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) { } /* Reads (s->symbol + 1) symbols. - Totally 1..4 symbols are read, 1..11 bits each. - The list of symbols MUST NOT contain duplicates. */ + Totally 1..4 symbols are read, 1..11 bits each. + The list of symbols MUST NOT contain duplicates. */ static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols( - uint32_t alphabet_size, uint32_t max_symbol, BrotliDecoderState* s) { - /* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */ + uint32_t alphabet_size, uint32_t max_symbol, BrotliDecoderState* s) { + /* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */ BrotliBitReader* br = &s->br; uint32_t max_bits = Log2Floor(alphabet_size - 1); uint32_t i = s->sub_loop_counter; @@ -483,7 +483,7 @@ static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols( s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ; return BROTLI_DECODER_NEEDS_MORE_INPUT; } - if (v >= max_symbol) { + if (v >= max_symbol) { return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET); } @@ -509,20 +509,20 @@ static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols( B) remember code length (if it is not 0) C) extend corresponding index-chain D) reduce the Huffman space - E) update the histogram */ + E) update the histogram */ static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len, uint32_t* symbol, uint32_t* repeat, uint32_t* space, uint32_t* prev_code_len, uint16_t* symbol_lists, uint16_t* code_length_histo, int* next_symbol) { *repeat = 0; - if (code_len != 0) { /* code_len == 1..15 */ + if (code_len != 0) { /* code_len == 1..15 */ symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol); next_symbol[code_len] = (int)(*symbol); *prev_code_len = code_len; *space -= 32768U >> code_len; code_length_histo[code_len]++; - BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", - (int)*symbol, (int)code_len)); + BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", + (int)*symbol, (int)code_len)); } (*symbol)++; } @@ -536,7 +536,7 @@ static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len, D) For each symbol do the same operations as in ProcessSingleCodeLength PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or - code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH */ + code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH */ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len, uint32_t repeat_delta, uint32_t alphabet_size, uint32_t* symbol, uint32_t* repeat, uint32_t* space, uint32_t* prev_code_len, @@ -567,7 +567,7 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len, return; } BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n", - (int)*symbol, (int)(*symbol + repeat_delta - 1), (int)*repeat_code_len)); + (int)*symbol, (int)(*symbol + repeat_delta - 1), (int)*repeat_code_len)); if (*repeat_code_len != 0) { unsigned last = *symbol + repeat_delta; int next = next_symbol[*repeat_code_len]; @@ -602,7 +602,7 @@ static BrotliDecoderErrorCode ReadSymbolCodeLengths( while (symbol < alphabet_size && space > 0) { const HuffmanCode* p = s->table; uint32_t code_len; - BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p); + BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p); if (!BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ)) { s->symbol = symbol; s->repeat = repeat; @@ -612,14 +612,14 @@ static BrotliDecoderErrorCode ReadSymbolCodeLengths( return BROTLI_DECODER_NEEDS_MORE_INPUT; } BrotliFillBitWindow16(br); - BROTLI_HC_ADJUST_TABLE_INDEX(p, BrotliGetBitsUnmasked(br) & - BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)); - BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p)); /* Use 1..5 bits. */ - code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */ + BROTLI_HC_ADJUST_TABLE_INDEX(p, BrotliGetBitsUnmasked(br) & + BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)); + BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p)); /* Use 1..5 bits. */ + code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */ if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, &prev_code_len, symbol_lists, code_length_histo, next_symbol); - } else { /* code_len == 16..17, extra_bits == 2..3 */ + } else { /* code_len == 16..17, extra_bits == 2..3 */ uint32_t extra_bits = (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3; uint32_t repeat_delta = @@ -643,34 +643,34 @@ static BrotliDecoderErrorCode SafeReadSymbolCodeLengths( uint32_t code_len; uint32_t available_bits; uint32_t bits = 0; - BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p); + BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p); if (get_byte && !BrotliPullByte(br)) return BROTLI_DECODER_NEEDS_MORE_INPUT; get_byte = BROTLI_FALSE; available_bits = BrotliGetAvailableBits(br); if (available_bits != 0) { bits = (uint32_t)BrotliGetBitsUnmasked(br); } - BROTLI_HC_ADJUST_TABLE_INDEX(p, - bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)); - if (BROTLI_HC_FAST_LOAD_BITS(p) > available_bits) { + BROTLI_HC_ADJUST_TABLE_INDEX(p, + bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)); + if (BROTLI_HC_FAST_LOAD_BITS(p) > available_bits) { get_byte = BROTLI_TRUE; continue; } - code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */ + code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */ if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { - BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p)); + BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p)); ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space, &s->prev_code_len, s->symbol_lists, s->code_length_histo, s->next_symbol); - } else { /* code_len == 16..17, extra_bits == 2..3 */ + } else { /* code_len == 16..17, extra_bits == 2..3 */ uint32_t extra_bits = code_len - 14U; - uint32_t repeat_delta = (bits >> BROTLI_HC_FAST_LOAD_BITS(p)) & - BitMask(extra_bits); - if (available_bits < BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits) { + uint32_t repeat_delta = (bits >> BROTLI_HC_FAST_LOAD_BITS(p)) & + BitMask(extra_bits); + if (available_bits < BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits) { get_byte = BROTLI_TRUE; continue; } - BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits); + BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits); ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, &s->symbol, &s->repeat, &s->space, &s->prev_code_len, &s->repeat_code_len, s->symbol_lists, s->code_length_histo, @@ -715,7 +715,7 @@ static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) { ++num_codes; ++s->code_length_histo[v]; if (space - 1U >= 32U) { - /* space is 0 or wrapped around. */ + /* space is 0 or wrapped around. */ break; } } @@ -730,22 +730,22 @@ static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) { There are 2 scenarios: A) Huffman code contains only few symbols (1..4). Those symbols are read directly; their code lengths are defined by the number of symbols. - For this scenario 4 - 49 bits will be read. + For this scenario 4 - 49 bits will be read. B) 2-phase decoding: B.1) Small Huffman table is decoded; it is specified with code lengths encoded with predefined entropy code. 32 - 74 bits are used. B.2) Decoded table is used to decode code lengths of symbols in resulting - Huffman table. In worst case 3520 bits are read. */ + Huffman table. In worst case 3520 bits are read. */ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size, - uint32_t max_symbol, + uint32_t max_symbol, HuffmanCode* table, uint32_t* opt_table_size, BrotliDecoderState* s) { BrotliBitReader* br = &s->br; /* Unnecessary masking, but might be good for safety. */ - alphabet_size &= 0x7FF; - /* State machine. */ + alphabet_size &= 0x7FF; + /* State machine. */ for (;;) { switch (s->substate_huffman) { case BROTLI_STATE_HUFFMAN_NONE: @@ -758,7 +758,7 @@ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size, 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */ if (s->sub_loop_counter != 1) { s->space = 32; - s->repeat = 0; /* num_codes */ + s->repeat = 0; /* num_codes */ memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) * (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1)); memset(&s->code_length_code_lengths[0], 0, @@ -766,26 +766,26 @@ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size, s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; continue; } - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: /* Read symbols, codes & code lengths directly. */ - if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */ + if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */ s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE; return BROTLI_DECODER_NEEDS_MORE_INPUT; } s->sub_loop_counter = 0; - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { BrotliDecoderErrorCode result = - ReadSimpleHuffmanSymbols(alphabet_size, max_symbol, s); + ReadSimpleHuffmanSymbols(alphabet_size, max_symbol, s); if (result != BROTLI_DECODER_SUCCESS) { return result; } } - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: { uint32_t table_size; if (s->symbol == 3) { @@ -829,20 +829,20 @@ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size, s->space = 32768; s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS; } - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { uint32_t table_size; - BrotliDecoderErrorCode result = ReadSymbolCodeLengths(max_symbol, s); + BrotliDecoderErrorCode result = ReadSymbolCodeLengths(max_symbol, s); if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { - result = SafeReadSymbolCodeLengths(max_symbol, s); + result = SafeReadSymbolCodeLengths(max_symbol, s); } if (result != BROTLI_DECODER_SUCCESS) { return result; } if (s->space != 0) { - BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", (int)s->space)); + BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", (int)s->space)); return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE); } table_size = BrotliBuildHuffmanTable( @@ -867,7 +867,7 @@ static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table, uint32_t code; uint32_t nbits; code = ReadSymbol(table, br); - nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */ + nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */ return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits); } @@ -886,7 +886,7 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength( } { uint32_t bits; - uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */ + uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */ if (!BrotliSafeReadBits(br, nbits, &bits)) { s->block_length_index = index; s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX; @@ -911,7 +911,7 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength( of Y values, and reinitialize only first elements in L. Most of input values are 0 and 1. To reduce number of branches, we replace - inner for loop with do-while. */ + inner for loop with do-while. */ static BROTLI_NOINLINE void InverseMoveToFrontTransform( uint8_t* v, uint32_t v_len, BrotliDecoderState* state) { /* Reinitialize elements that could have been changed. */ @@ -927,7 +927,7 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform( /* Initialize list using 4 consequent values pattern. */ mtf[0] = pattern; do { - pattern += 0x04040404; /* Advance all 4 values by 4. */ + pattern += 0x04040404; /* Advance all 4 values by 4. */ mtf[i] = pattern; i++; } while (i <= upper_bound); @@ -960,8 +960,8 @@ static BrotliDecoderErrorCode HuffmanTreeGroupDecode( while (s->htree_index < group->num_htrees) { uint32_t table_size; BrotliDecoderErrorCode result = - ReadHuffmanCode(group->alphabet_size, group->max_symbol, - s->next, &table_size, s); + ReadHuffmanCode(group->alphabet_size, group->max_symbol, + s->next, &table_size, s); if (result != BROTLI_DECODER_SUCCESS) return result; group->htrees[s->htree_index] = s->next; s->next += table_size; @@ -978,7 +978,7 @@ static BrotliDecoderErrorCode HuffmanTreeGroupDecode( 2) Decode Huffman table using ReadHuffmanCode function. This table will be used for reading context map items. 3) Read context map items; "0" values could be run-length encoded. - 4) Optionally, apply InverseMoveToFront transform to the resulting map. */ + 4) Optionally, apply InverseMoveToFront transform to the resulting map. */ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, uint32_t* num_htrees, uint8_t** context_map_arg, @@ -996,8 +996,8 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, s->context_index = 0; BROTLI_LOG_UINT(context_map_size); BROTLI_LOG_UINT(*num_htrees); - *context_map_arg = - (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)context_map_size); + *context_map_arg = + (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)context_map_size); if (*context_map_arg == 0) { return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP); } @@ -1006,8 +1006,8 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, return BROTLI_DECODER_SUCCESS; } s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX; - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: { uint32_t bits; /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe @@ -1025,18 +1025,18 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, BROTLI_LOG_UINT(s->max_run_length_prefix); s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN; } - /* Fall through. */ - - case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: { - uint32_t alphabet_size = *num_htrees + s->max_run_length_prefix; - result = ReadHuffmanCode(alphabet_size, alphabet_size, + /* Fall through. */ + + case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: { + uint32_t alphabet_size = *num_htrees + s->max_run_length_prefix; + result = ReadHuffmanCode(alphabet_size, alphabet_size, s->context_map_table, NULL, s); if (result != BROTLI_DECODER_SUCCESS) return result; s->code = 0xFFFF; s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE; - } - /* Fall through. */ - + } + /* Fall through. */ + case BROTLI_STATE_CONTEXT_MAP_DECODE: { uint32_t context_index = s->context_index; uint32_t max_run_length_prefix = s->max_run_length_prefix; @@ -1084,8 +1084,8 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, } } } - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: { uint32_t bits; if (!BrotliSafeReadBits(br, 1, &bits)) { @@ -1098,7 +1098,7 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; return BROTLI_DECODER_SUCCESS; } - + default: return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); @@ -1117,11 +1117,11 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( BrotliBitReader* br = &s->br; uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2]; uint32_t block_type; - if (max_block_type <= 1) { - return BROTLI_FALSE; - } + if (max_block_type <= 1) { + return BROTLI_FALSE; + } - /* Read 0..15 + 3..39 bits. */ + /* Read 0..15 + 3..39 bits. */ if (!safe) { block_type = ReadSymbol(type_tree, br); s->block_length[tree_type] = ReadBlockLength(len_tree, br); @@ -1178,8 +1178,8 @@ static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) { trivial = s->trivial_literal_contexts[block_type >> 5]; s->trivial_literal_context = (trivial >> (block_type & 31)) & 1; s->literal_htree = s->literal_hgroup.htrees[s->context_map_slice[0]]; - context_mode = s->context_modes[block_type] & 3; - s->context_lookup = BROTLI_CONTEXT_LUT(context_mode); + context_mode = s->context_modes[block_type] & 3; + s->context_lookup = BROTLI_CONTEXT_LUT(context_mode); } /* Decodes the block type and updates the state for literal context. @@ -1216,7 +1216,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal( static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) { DecodeCommandBlockSwitchInternal(0, s); } - + static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch( BrotliDecoderState* s) { return DecodeCommandBlockSwitchInternal(1, s); @@ -1253,7 +1253,7 @@ static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) { /* Dumps output. Returns BROTLI_DECODER_NEEDS_MORE_OUTPUT only if there is more output to push - and either ring-buffer is as big as window size, or |force| is true. */ + and either ring-buffer is as big as window size, or |force| is true. */ static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer( BrotliDecoderState* s, size_t* available_out, uint8_t** next_out, size_t* total_out, BROTLI_BOOL force) { @@ -1312,7 +1312,7 @@ static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) { this function is called. Last two bytes of ring-buffer are initialized to 0, so context calculation - could be done uniformly for the first two and all other positions. */ + could be done uniformly for the first two and all other positions. */ static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer( BrotliDecoderState* s) { uint8_t* old_ringbuffer = s->ringbuffer; @@ -1320,8 +1320,8 @@ static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer( return BROTLI_TRUE; } - s->ringbuffer = (uint8_t*)BROTLI_DECODER_ALLOC(s, - (size_t)(s->new_ringbuffer_size) + kRingBufferWriteAheadSlack); + s->ringbuffer = (uint8_t*)BROTLI_DECODER_ALLOC(s, + (size_t)(s->new_ringbuffer_size) + kRingBufferWriteAheadSlack); if (s->ringbuffer == 0) { /* Restore previous value. */ s->ringbuffer = old_ringbuffer; @@ -1332,7 +1332,7 @@ static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer( if (!!old_ringbuffer) { memcpy(s->ringbuffer, old_ringbuffer, (size_t)s->pos); - BROTLI_DECODER_FREE(s, old_ringbuffer); + BROTLI_DECODER_FREE(s, old_ringbuffer); } s->ringbuffer_size = s->new_ringbuffer_size; @@ -1373,8 +1373,8 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( } s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE; } - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_UNCOMPRESSED_WRITE: { BrotliDecoderErrorCode result; result = WriteRingBuffer( @@ -1398,7 +1398,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( If we know the data size is small, do not allocate more ring buffer size than needed to reduce memory usage. - When this method is called, metablock size and flags MUST be decoded. */ + When this method is called, metablock size and flags MUST be decoded. */ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize( BrotliDecoderState* s) { int window_size = 1 << s->window_bits; @@ -1429,7 +1429,7 @@ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize( if (!!s->canny_ringbuffer_allocation) { /* Reduce ring buffer size to save memory when server is unscrupulous. In worst case memory usage might be 1.5x bigger for a short period of - ring buffer reallocation. */ + ring buffer reallocation. */ while ((new_ringbuffer_size >> 1) >= min_size) { new_ringbuffer_size >>= 1; } @@ -1449,7 +1449,7 @@ static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) { s->loop_counter = i; return BROTLI_DECODER_NEEDS_MORE_INPUT; } - s->context_modes[i] = (uint8_t)bits; + s->context_modes[i] = (uint8_t)bits; BROTLI_LOG_ARRAY_INDEX(s->context_modes, i); i++; } @@ -1464,12 +1464,12 @@ static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) { s->distance_context = 1; } else { int distance_code = s->distance_code << 1; - /* kDistanceShortCodeIndexOffset has 2-bit values from LSB: - 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */ - const uint32_t kDistanceShortCodeIndexOffset = 0xAAAFFF1B; - /* kDistanceShortCodeValueOffset has 2-bit values from LSB: - -0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */ - const uint32_t kDistanceShortCodeValueOffset = 0xFA5FA500; + /* kDistanceShortCodeIndexOffset has 2-bit values from LSB: + 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */ + const uint32_t kDistanceShortCodeIndexOffset = 0xAAAFFF1B; + /* kDistanceShortCodeValueOffset has 2-bit values from LSB: + -0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */ + const uint32_t kDistanceShortCodeValueOffset = 0xFA5FA500; int v = (s->dist_rb_idx + (int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3; s->distance_code = s->dist_rb[v]; @@ -1479,9 +1479,9 @@ static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) { } else { s->distance_code -= v; if (s->distance_code <= 0) { - /* A huge distance will cause a BROTLI_FAILURE() soon. - This is a little faster than failing here. */ - s->distance_code = 0x7FFFFFFF; + /* A huge distance will cause a BROTLI_FAILURE() soon. + This is a little faster than failing here. */ + s->distance_code = 0x7FFFFFFF; } } } @@ -1497,7 +1497,7 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBits( } } -/* Precondition: s->distance_code < 0. */ +/* Precondition: s->distance_code < 0. */ static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal( int safe, BrotliDecoderState* s, BrotliBitReader* br) { int distval; @@ -1513,10 +1513,10 @@ static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal( } s->distance_code = (int)code; } - /* Convert the distance code to the actual distance by possibly - looking up past distances from the s->ringbuffer. */ + /* Convert the distance code to the actual distance by possibly + looking up past distances from the s->ringbuffer. */ s->distance_context = 0; - if ((s->distance_code & ~0xF) == 0) { + if ((s->distance_code & ~0xF) == 0) { TakeDistanceFromRingBuffer(s); --s->block_length[2]; return BROTLI_TRUE; @@ -1532,14 +1532,14 @@ static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal( s->distance_code = (int)s->num_direct_distance_codes + offset + (int)BrotliReadBits(br, nbits); } else { - /* This branch also works well when s->distance_postfix_bits == 0. */ + /* This branch also works well when s->distance_postfix_bits == 0. */ uint32_t bits; postfix = distval & s->distance_postfix_mask; distval >>= s->distance_postfix_bits; nbits = ((uint32_t)distval >> 1) + 1; if (safe) { if (!SafeReadBits(br, nbits, &bits)) { - s->distance_code = -1; /* Restore precondition. */ + s->distance_code = -1; /* Restore precondition. */ BrotliBitReaderRestoreState(br, &memento); return BROTLI_FALSE; } @@ -1666,7 +1666,7 @@ CommandBegin: if (safe) { s->state = BROTLI_STATE_COMMAND_BEGIN; } - if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */ + if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */ s->state = BROTLI_STATE_COMMAND_BEGIN; result = BROTLI_DECODER_NEEDS_MORE_INPUT; goto saveStateAndReturn; @@ -1675,7 +1675,7 @@ CommandBegin: BROTLI_SAFE(DecodeCommandBlockSwitch(s)); goto CommandBegin; } - /* Read the insert/copy length in the command. */ + /* Read the insert/copy length in the command. */ BROTLI_SAFE(ReadCommand(s, br, &i)); BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n", pos, i, s->copy_length)); @@ -1688,13 +1688,13 @@ CommandInner: if (safe) { s->state = BROTLI_STATE_COMMAND_INNER; } - /* Read the literals in the command. */ + /* Read the literals in the command. */ if (s->trivial_literal_context) { uint32_t bits; uint32_t value; PreloadSymbol(safe, s->literal_htree, br, &bits, &value); do { - if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ + if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ s->state = BROTLI_STATE_COMMAND_INNER; result = BROTLI_DECODER_NEEDS_MORE_INPUT; goto saveStateAndReturn; @@ -1730,7 +1730,7 @@ CommandInner: do { const HuffmanCode* hc; uint8_t context; - if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ + if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ s->state = BROTLI_STATE_COMMAND_INNER; result = BROTLI_DECODER_NEEDS_MORE_INPUT; goto saveStateAndReturn; @@ -1739,7 +1739,7 @@ CommandInner: BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); if (s->trivial_literal_context) goto CommandInner; } - context = BROTLI_CONTEXT(p1, p2, s->context_lookup); + context = BROTLI_CONTEXT(p1, p2, s->context_lookup); BROTLI_LOG_UINT(context); hc = s->literal_hgroup.htrees[s->context_map_slice[context]]; p2 = p1; @@ -1795,44 +1795,44 @@ CommandPostDecodeLiterals: } i = s->copy_length; /* Apply copy of LZ77 back-reference, or static dictionary reference if - the distance is larger than the max LZ77 distance */ + the distance is larger than the max LZ77 distance */ if (s->distance_code > s->max_distance) { - /* The maximum allowed distance is BROTLI_MAX_ALLOWED_DISTANCE = 0x7FFFFFFC. - With this choice, no signed overflow can occur after decoding - a special distance code (e.g., after adding 3 to the last distance). */ - if (s->distance_code > BROTLI_MAX_ALLOWED_DISTANCE) { - BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " - "len: %d bytes left: %d\n", - pos, s->distance_code, i, s->meta_block_remaining_len)); - return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DISTANCE); - } + /* The maximum allowed distance is BROTLI_MAX_ALLOWED_DISTANCE = 0x7FFFFFFC. + With this choice, no signed overflow can occur after decoding + a special distance code (e.g., after adding 3 to the last distance). */ + if (s->distance_code > BROTLI_MAX_ALLOWED_DISTANCE) { + BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " + "len: %d bytes left: %d\n", + pos, s->distance_code, i, s->meta_block_remaining_len)); + return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DISTANCE); + } if (i >= BROTLI_MIN_DICTIONARY_WORD_LENGTH && i <= BROTLI_MAX_DICTIONARY_WORD_LENGTH) { - int address = s->distance_code - s->max_distance - 1; - const BrotliDictionary* words = s->dictionary; - const BrotliTransforms* transforms = s->transforms; + int address = s->distance_code - s->max_distance - 1; + const BrotliDictionary* words = s->dictionary; + const BrotliTransforms* transforms = s->transforms; int offset = (int)s->dictionary->offsets_by_length[i]; uint32_t shift = s->dictionary->size_bits_by_length[i]; - + int mask = (int)BitMask(shift); int word_idx = address & mask; int transform_idx = address >> shift; /* Compensate double distance-ring-buffer roll. */ s->dist_rb_idx += s->distance_context; offset += word_idx * i; - if (BROTLI_PREDICT_FALSE(!words->data)) { + if (BROTLI_PREDICT_FALSE(!words->data)) { return BROTLI_FAILURE(BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET); } - if (transform_idx < (int)transforms->num_transforms) { - const uint8_t* word = &words->data[offset]; + if (transform_idx < (int)transforms->num_transforms) { + const uint8_t* word = &words->data[offset]; int len = i; - if (transform_idx == transforms->cutOffTransforms[0]) { + if (transform_idx == transforms->cutOffTransforms[0]) { memcpy(&s->ringbuffer[pos], word, (size_t)len); BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]\n", len, word)); } else { - len = BrotliTransformDictionaryWord(&s->ringbuffer[pos], word, len, - transforms, transform_idx); + len = BrotliTransformDictionaryWord(&s->ringbuffer[pos], word, len, + transforms, transform_idx); BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]," " transform_idx = %d, transformed: [%.*s]\n", i, word, transform_idx, len, &s->ringbuffer[pos])); @@ -1861,13 +1861,13 @@ CommandPostDecodeLiterals: uint8_t* copy_src = &s->ringbuffer[src_start]; int dst_end = pos + i; int src_end = src_start + i; - /* Update the recent distances cache. */ + /* Update the recent distances cache. */ s->dist_rb[s->dist_rb_idx & 3] = s->distance_code; ++s->dist_rb_idx; s->meta_block_remaining_len -= i; /* There are 32+ bytes of slack in the ring-buffer allocation. Also, we have 16 short codes, that make these 16 bytes irrelevant - in the ring-buffer. Let's copy over them as a first guess. */ + in the ring-buffer. Let's copy over them as a first guess. */ memmove16(copy_dst, copy_src); if (src_end > pos && dst_end > src_start) { /* Regions intersect. */ @@ -1890,7 +1890,7 @@ CommandPostDecodeLiterals: } BROTLI_LOG_UINT(s->meta_block_remaining_len); if (s->meta_block_remaining_len <= 0) { - /* Next metablock, if any. */ + /* Next metablock, if any. */ s->state = BROTLI_STATE_METABLOCK_DONE; goto saveStateAndReturn; } else { @@ -1910,7 +1910,7 @@ CommandPostWrapCopy: } } if (s->meta_block_remaining_len <= 0) { - /* Next metablock, if any. */ + /* Next metablock, if any. */ s->state = BROTLI_STATE_METABLOCK_DONE; goto saveStateAndReturn; } else { @@ -1935,21 +1935,21 @@ static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands( return ProcessCommandsInternal(1, s); } -/* Returns the maximum number of distance symbols which can only represent - distances not exceeding BROTLI_MAX_ALLOWED_DISTANCE. */ -static uint32_t BrotliMaxDistanceSymbol(uint32_t ndirect, uint32_t npostfix) { - static const uint32_t bound[BROTLI_MAX_NPOSTFIX + 1] = {0, 4, 12, 28}; - static const uint32_t diff[BROTLI_MAX_NPOSTFIX + 1] = {73, 126, 228, 424}; - uint32_t postfix = 1U << npostfix; - if (ndirect < bound[npostfix]) { - return ndirect + diff[npostfix] + postfix; - } else if (ndirect > bound[npostfix] + postfix) { - return ndirect + diff[npostfix]; - } else { - return bound[npostfix] + diff[npostfix] + postfix; - } -} - +/* Returns the maximum number of distance symbols which can only represent + distances not exceeding BROTLI_MAX_ALLOWED_DISTANCE. */ +static uint32_t BrotliMaxDistanceSymbol(uint32_t ndirect, uint32_t npostfix) { + static const uint32_t bound[BROTLI_MAX_NPOSTFIX + 1] = {0, 4, 12, 28}; + static const uint32_t diff[BROTLI_MAX_NPOSTFIX + 1] = {73, 126, 228, 424}; + uint32_t postfix = 1U << npostfix; + if (ndirect < bound[npostfix]) { + return ndirect + diff[npostfix] + postfix; + } else if (ndirect > bound[npostfix] + postfix) { + return ndirect + diff[npostfix]; + } else { + return bound[npostfix] + diff[npostfix] + postfix; + } +} + BrotliDecoderResult BrotliDecoderDecompress( size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size, uint8_t* decoded_buffer) { @@ -1960,9 +1960,9 @@ BrotliDecoderResult BrotliDecoderDecompress( const uint8_t* next_in = encoded_buffer; size_t available_out = *decoded_size; uint8_t* next_out = decoded_buffer; - if (!BrotliDecoderStateInit(&s, 0, 0, 0)) { - return BROTLI_DECODER_RESULT_ERROR; - } + if (!BrotliDecoderStateInit(&s, 0, 0, 0)) { + return BROTLI_DECODER_RESULT_ERROR; + } result = BrotliDecoderDecompressStream( &s, &available_in, &next_in, &available_out, &next_out, &total_out); *decoded_size = total_out; @@ -1974,25 +1974,25 @@ BrotliDecoderResult BrotliDecoderDecompress( } /* Invariant: input stream is never overconsumed: - - invalid input implies that the whole stream is invalid -> any amount of + - invalid input implies that the whole stream is invalid -> any amount of input could be read and discarded - - when result is "needs more input", then at least one more byte is REQUIRED + - when result is "needs more input", then at least one more byte is REQUIRED to complete decoding; all input data MUST be consumed by decoder, so client could swap the input buffer - - when result is "needs more output" decoder MUST ensure that it doesn't + - when result is "needs more output" decoder MUST ensure that it doesn't hold more than 7 bits in bit reader; this saves client from swapping input buffer ahead of time - - when result is "success" decoder MUST return all unused data back to input - buffer; this is possible because the invariant is held on enter */ + - when result is "success" decoder MUST return all unused data back to input + buffer; this is possible because the invariant is held on enter */ BrotliDecoderResult BrotliDecoderDecompressStream( BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in, size_t* available_out, uint8_t** next_out, size_t* total_out) { BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; BrotliBitReader* br = &s->br; - /* Ensure that |total_out| is set, even if no data will ever be pushed out. */ - if (total_out) { - *total_out = s->partial_pos_out; - } + /* Ensure that |total_out| is set, even if no data will ever be pushed out. */ + if (total_out) { + *total_out = s->partial_pos_out; + } /* Do not try to process further in a case of unrecoverable error. */ if ((int)s->error_code < 0) { return BROTLI_DECODER_RESULT_ERROR; @@ -2002,7 +2002,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s, BROTLI_FAILURE(BROTLI_DECODER_ERROR_INVALID_ARGUMENTS)); } if (!*available_out) next_out = 0; - if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */ + if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */ br->avail_in = *available_in; br->next_in = *next_in; } else { @@ -2014,10 +2014,10 @@ BrotliDecoderResult BrotliDecoderDecompressStream( } /* State machine */ for (;;) { - if (result != BROTLI_DECODER_SUCCESS) { - /* Error, needs more input/output. */ + if (result != BROTLI_DECODER_SUCCESS) { + /* Error, needs more input/output. */ if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { - if (s->ringbuffer != 0) { /* Pro-actively push output. */ + if (s->ringbuffer != 0) { /* Pro-actively push output. */ BrotliDecoderErrorCode intermediate_result = WriteRingBuffer(s, available_out, next_out, total_out, BROTLI_TRUE); /* WriteRingBuffer checks s->meta_block_remaining_len validity. */ @@ -2026,10 +2026,10 @@ BrotliDecoderResult BrotliDecoderDecompressStream( break; } } - if (s->buffer_length != 0) { /* Used with internal buffer. */ - if (br->avail_in == 0) { - /* Successfully finished read transaction. - Accumulator contains less than 8 bits, because internal buffer + if (s->buffer_length != 0) { /* Used with internal buffer. */ + if (br->avail_in == 0) { + /* Successfully finished read transaction. + Accumulator contains less than 8 bits, because internal buffer is expanded byte-by-byte until it is enough to complete read. */ s->buffer_length = 0; /* Switch to input stream and restart. */ @@ -2049,9 +2049,9 @@ BrotliDecoderResult BrotliDecoderDecompressStream( /* Retry with more data in buffer. */ continue; } - /* Can't finish reading and no more input. */ + /* Can't finish reading and no more input. */ break; - } else { /* Input stream doesn't contain enough input. */ + } else { /* Input stream doesn't contain enough input. */ /* Copy tail to internal buffer and return. */ *next_in = br->next_in; *available_in = br->avail_in; @@ -2070,7 +2070,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( if (s->buffer_length != 0) { /* Just consumed the buffered input and produced some output. Otherwise - it would result in "needs more input". Reset internal buffer. */ + it would result in "needs more input". Reset internal buffer. */ s->buffer_length = 0; } else { /* Using input stream in last iteration. When decoder switches to input @@ -2090,37 +2090,37 @@ BrotliDecoderResult BrotliDecoderDecompressStream( break; } /* Decode window size. */ - result = DecodeWindowBits(s, br); /* Reads 1..8 bits. */ - if (result != BROTLI_DECODER_SUCCESS) { - break; - } - if (s->large_window) { - s->state = BROTLI_STATE_LARGE_WINDOW_BITS; - break; - } - s->state = BROTLI_STATE_INITIALIZE; - break; - - case BROTLI_STATE_LARGE_WINDOW_BITS: - if (!BrotliSafeReadBits(br, 6, &s->window_bits)) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT; - break; - } - if (s->window_bits < BROTLI_LARGE_MIN_WBITS || - s->window_bits > BROTLI_LARGE_MAX_WBITS) { + result = DecodeWindowBits(s, br); /* Reads 1..8 bits. */ + if (result != BROTLI_DECODER_SUCCESS) { + break; + } + if (s->large_window) { + s->state = BROTLI_STATE_LARGE_WINDOW_BITS; + break; + } + s->state = BROTLI_STATE_INITIALIZE; + break; + + case BROTLI_STATE_LARGE_WINDOW_BITS: + if (!BrotliSafeReadBits(br, 6, &s->window_bits)) { + result = BROTLI_DECODER_NEEDS_MORE_INPUT; + break; + } + if (s->window_bits < BROTLI_LARGE_MIN_WBITS || + s->window_bits > BROTLI_LARGE_MAX_WBITS) { result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); break; } - s->state = BROTLI_STATE_INITIALIZE; - /* Fall through. */ - - case BROTLI_STATE_INITIALIZE: - BROTLI_LOG_UINT(s->window_bits); + s->state = BROTLI_STATE_INITIALIZE; + /* Fall through. */ + + case BROTLI_STATE_INITIALIZE: + BROTLI_LOG_UINT(s->window_bits); /* Maximum distance, see section 9.1. of the spec. */ s->max_backward_distance = (1 << s->window_bits) - BROTLI_WINDOW_GAP; /* Allocate memory for both block_type_trees and block_len_trees. */ - s->block_type_trees = (HuffmanCode*)BROTLI_DECODER_ALLOC(s, + s->block_type_trees = (HuffmanCode*)BROTLI_DECODER_ALLOC(s, sizeof(HuffmanCode) * 3 * (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26)); if (s->block_type_trees == 0) { @@ -2131,16 +2131,16 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258; s->state = BROTLI_STATE_METABLOCK_BEGIN; - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_METABLOCK_BEGIN: BrotliDecoderStateMetablockBegin(s); BROTLI_LOG_UINT(s->pos); s->state = BROTLI_STATE_METABLOCK_HEADER; - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_METABLOCK_HEADER: - result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */ + result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */ if (result != BROTLI_DECODER_SUCCESS) { break; } @@ -2170,7 +2170,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->loop_counter = 0; s->state = BROTLI_STATE_HUFFMAN_CODE_0; break; - + case BROTLI_STATE_UNCOMPRESSED: { result = CopyUncompressedBlockToOutput( available_out, next_out, total_out, s); @@ -2180,7 +2180,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->state = BROTLI_STATE_METABLOCK_DONE; break; } - + case BROTLI_STATE_METADATA: for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) { uint32_t bits; @@ -2194,7 +2194,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->state = BROTLI_STATE_METABLOCK_DONE; } break; - + case BROTLI_STATE_HUFFMAN_CODE_0: if (s->loop_counter >= 3) { s->state = BROTLI_STATE_METABLOCK_HEADER_2; @@ -2212,28 +2212,28 @@ BrotliDecoderResult BrotliDecoderDecompressStream( break; } s->state = BROTLI_STATE_HUFFMAN_CODE_1; - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_HUFFMAN_CODE_1: { - uint32_t alphabet_size = s->num_block_types[s->loop_counter] + 2; + uint32_t alphabet_size = s->num_block_types[s->loop_counter] + 2; int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258; - result = ReadHuffmanCode(alphabet_size, alphabet_size, + result = ReadHuffmanCode(alphabet_size, alphabet_size, &s->block_type_trees[tree_offset], NULL, s); if (result != BROTLI_DECODER_SUCCESS) break; s->state = BROTLI_STATE_HUFFMAN_CODE_2; } - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_HUFFMAN_CODE_2: { - uint32_t alphabet_size = BROTLI_NUM_BLOCK_LEN_SYMBOLS; + uint32_t alphabet_size = BROTLI_NUM_BLOCK_LEN_SYMBOLS; int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; - result = ReadHuffmanCode(alphabet_size, alphabet_size, + result = ReadHuffmanCode(alphabet_size, alphabet_size, &s->block_len_trees[tree_offset], NULL, s); if (result != BROTLI_DECODER_SUCCESS) break; s->state = BROTLI_STATE_HUFFMAN_CODE_3; } - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_HUFFMAN_CODE_3: { int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter], @@ -2246,7 +2246,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->state = BROTLI_STATE_HUFFMAN_CODE_0; break; } - + case BROTLI_STATE_METABLOCK_HEADER_2: { uint32_t bits; if (!BrotliSafeReadBits(br, 6, &bits)) { @@ -2261,7 +2261,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( BROTLI_LOG_UINT(s->distance_postfix_bits); s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits); s->context_modes = - (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)s->num_block_types[0]); + (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)s->num_block_types[0]); if (s->context_modes == 0) { result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES); break; @@ -2269,16 +2269,16 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->loop_counter = 0; s->state = BROTLI_STATE_CONTEXT_MODES; } - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_CONTEXT_MODES: result = ReadContextModes(s); if (result != BROTLI_DECODER_SUCCESS) { break; } s->state = BROTLI_STATE_CONTEXT_MAP_1; - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_CONTEXT_MAP_1: result = DecodeContextMap( s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS, @@ -2288,54 +2288,54 @@ BrotliDecoderResult BrotliDecoderDecompressStream( } DetectTrivialLiteralBlockTypes(s); s->state = BROTLI_STATE_CONTEXT_MAP_2; - /* Fall through. */ - - case BROTLI_STATE_CONTEXT_MAP_2: { - uint32_t num_direct_codes = - s->num_direct_distance_codes - BROTLI_NUM_DISTANCE_SHORT_CODES; - uint32_t num_distance_codes = BROTLI_DISTANCE_ALPHABET_SIZE( - s->distance_postfix_bits, num_direct_codes, - (s->large_window ? BROTLI_LARGE_MAX_DISTANCE_BITS : - BROTLI_MAX_DISTANCE_BITS)); - uint32_t max_distance_symbol = (s->large_window ? - BrotliMaxDistanceSymbol( - num_direct_codes, s->distance_postfix_bits) : - num_distance_codes); - BROTLI_BOOL allocation_success = BROTLI_TRUE; - result = DecodeContextMap( - s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS, - &s->num_dist_htrees, &s->dist_context_map, s); - if (result != BROTLI_DECODER_SUCCESS) { - break; - } - allocation_success &= BrotliDecoderHuffmanTreeGroupInit( - s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, - BROTLI_NUM_LITERAL_SYMBOLS, s->num_literal_htrees); - allocation_success &= BrotliDecoderHuffmanTreeGroupInit( - s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, - BROTLI_NUM_COMMAND_SYMBOLS, s->num_block_types[1]); - allocation_success &= BrotliDecoderHuffmanTreeGroupInit( - s, &s->distance_hgroup, num_distance_codes, - max_distance_symbol, s->num_dist_htrees); - if (!allocation_success) { - return SaveErrorCode(s, - BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS)); - } + /* Fall through. */ + + case BROTLI_STATE_CONTEXT_MAP_2: { + uint32_t num_direct_codes = + s->num_direct_distance_codes - BROTLI_NUM_DISTANCE_SHORT_CODES; + uint32_t num_distance_codes = BROTLI_DISTANCE_ALPHABET_SIZE( + s->distance_postfix_bits, num_direct_codes, + (s->large_window ? BROTLI_LARGE_MAX_DISTANCE_BITS : + BROTLI_MAX_DISTANCE_BITS)); + uint32_t max_distance_symbol = (s->large_window ? + BrotliMaxDistanceSymbol( + num_direct_codes, s->distance_postfix_bits) : + num_distance_codes); + BROTLI_BOOL allocation_success = BROTLI_TRUE; + result = DecodeContextMap( + s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS, + &s->num_dist_htrees, &s->dist_context_map, s); + if (result != BROTLI_DECODER_SUCCESS) { + break; + } + allocation_success &= BrotliDecoderHuffmanTreeGroupInit( + s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, + BROTLI_NUM_LITERAL_SYMBOLS, s->num_literal_htrees); + allocation_success &= BrotliDecoderHuffmanTreeGroupInit( + s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, + BROTLI_NUM_COMMAND_SYMBOLS, s->num_block_types[1]); + allocation_success &= BrotliDecoderHuffmanTreeGroupInit( + s, &s->distance_hgroup, num_distance_codes, + max_distance_symbol, s->num_dist_htrees); + if (!allocation_success) { + return SaveErrorCode(s, + BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS)); + } s->loop_counter = 0; s->state = BROTLI_STATE_TREE_GROUP; - } - /* Fall through. */ - - case BROTLI_STATE_TREE_GROUP: { - HuffmanTreeGroup* hgroup = NULL; - switch (s->loop_counter) { - case 0: hgroup = &s->literal_hgroup; break; - case 1: hgroup = &s->insert_copy_hgroup; break; - case 2: hgroup = &s->distance_hgroup; break; - default: return SaveErrorCode(s, BROTLI_FAILURE( - BROTLI_DECODER_ERROR_UNREACHABLE)); - } - result = HuffmanTreeGroupDecode(hgroup, s); + } + /* Fall through. */ + + case BROTLI_STATE_TREE_GROUP: { + HuffmanTreeGroup* hgroup = NULL; + switch (s->loop_counter) { + case 0: hgroup = &s->literal_hgroup; break; + case 1: hgroup = &s->insert_copy_hgroup; break; + case 2: hgroup = &s->distance_hgroup; break; + default: return SaveErrorCode(s, BROTLI_FAILURE( + BROTLI_DECODER_ERROR_UNREACHABLE)); + } + result = HuffmanTreeGroupDecode(hgroup, s); if (result != BROTLI_DECODER_SUCCESS) break; s->loop_counter++; if (s->loop_counter >= 3) { @@ -2349,25 +2349,25 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->state = BROTLI_STATE_COMMAND_BEGIN; } break; - } - + } + case BROTLI_STATE_COMMAND_BEGIN: - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_COMMAND_INNER: - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS: - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_COMMAND_POST_WRAP_COPY: result = ProcessCommands(s); if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { result = SafeProcessCommands(s); } break; - + case BROTLI_STATE_COMMAND_INNER_WRITE: - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_COMMAND_POST_WRITE_1: - /* Fall through. */ + /* Fall through. */ case BROTLI_STATE_COMMAND_POST_WRITE_2: result = WriteRingBuffer( s, available_out, next_out, total_out, BROTLI_FALSE); @@ -2380,7 +2380,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( } if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) { if (s->meta_block_remaining_len == 0) { - /* Next metablock, if any. */ + /* Next metablock, if any. */ s->state = BROTLI_STATE_METABLOCK_DONE; } else { s->state = BROTLI_STATE_COMMAND_BEGIN; @@ -2400,7 +2400,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream( s->state = BROTLI_STATE_COMMAND_INNER; } break; - + case BROTLI_STATE_METABLOCK_DONE: if (s->meta_block_remaining_len < 0) { result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2); @@ -2421,8 +2421,8 @@ BrotliDecoderResult BrotliDecoderDecompressStream( *next_in = br->next_in; } s->state = BROTLI_STATE_DONE; - /* Fall through. */ - + /* Fall through. */ + case BROTLI_STATE_DONE: if (s->ringbuffer != 0) { result = WriteRingBuffer( diff --git a/contrib/libs/brotli/dec/huffman.c b/contrib/libs/brotli/dec/huffman.c index 30c40d33f2..49f66996c8 100644 --- a/contrib/libs/brotli/dec/huffman.c +++ b/contrib/libs/brotli/dec/huffman.c @@ -11,7 +11,7 @@ #include <string.h> /* memcpy, memset */ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -20,9 +20,9 @@ extern "C" { #define BROTLI_REVERSE_BITS_MAX 8 -#if defined(BROTLI_RBIT) +#if defined(BROTLI_RBIT) #define BROTLI_REVERSE_BITS_BASE \ - ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX) + ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX) #else #define BROTLI_REVERSE_BITS_BASE 0 static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = { @@ -62,13 +62,13 @@ static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = { #endif /* BROTLI_RBIT */ #define BROTLI_REVERSE_BITS_LOWEST \ - ((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) + ((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX), where reverse(value, len) is the bit-wise reversal of the len least significant bits of value. */ -static BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) { -#if defined(BROTLI_RBIT) +static BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) { +#if defined(BROTLI_RBIT) return BROTLI_RBIT(num); #else return kReverseBits[num]; @@ -86,9 +86,9 @@ static BROTLI_INLINE void ReplicateValue(HuffmanCode* table, } while (end > 0); } -/* Returns the table width of the next 2nd level table. |count| is the histogram - of bit lengths for the remaining symbols, |len| is the code length of the - next processed symbol. */ +/* Returns the table width of the next 2nd level table. |count| is the histogram + of bit lengths for the remaining symbols, |len| is the code length of the + next processed symbol. */ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count, int len, int root_bits) { int left = 1 << (len - root_bits); @@ -104,12 +104,12 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count, void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, const uint8_t* const code_lengths, uint16_t* count) { - HuffmanCode code; /* current table entry */ - int symbol; /* symbol index in original or sorted table */ - brotli_reg_t key; /* prefix code */ - brotli_reg_t key_step; /* prefix code addend */ - int step; /* step size to replicate values in current table */ - int table_size; /* size of current table */ + HuffmanCode code; /* current table entry */ + int symbol; /* symbol index in original or sorted table */ + brotli_reg_t key; /* prefix code */ + brotli_reg_t key_step; /* prefix code addend */ + int step; /* step size to replicate values in current table */ + int table_size; /* size of current table */ int sorted[BROTLI_CODE_LENGTH_CODES]; /* symbols sorted by code length */ /* offsets in sorted table for each length */ int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1]; @@ -118,7 +118,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= BROTLI_REVERSE_BITS_MAX); - /* Generate offsets into sorted symbol table by code length. */ + /* Generate offsets into sorted symbol table by code length. */ symbol = -1; bits = 1; BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, { @@ -129,7 +129,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, /* Symbols with code length 0 are placed after all other symbols. */ offset[0] = BROTLI_CODE_LENGTH_CODES - 1; - /* Sort symbols by length, by symbol order within each length. */ + /* Sort symbols by length, by symbol order within each length. */ symbol = BROTLI_CODE_LENGTH_CODES; do { BROTLI_REPEAT(6, { @@ -142,14 +142,14 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, /* Special case: all symbols but one have 0 code length. */ if (offset[0] == 0) { - code = ConstructHuffmanCode(0, (uint16_t)sorted[0]); - for (key = 0; key < (brotli_reg_t)table_size; ++key) { + code = ConstructHuffmanCode(0, (uint16_t)sorted[0]); + for (key = 0; key < (brotli_reg_t)table_size; ++key) { table[key] = code; } return; } - /* Fill in table. */ + /* Fill in table. */ key = 0; key_step = BROTLI_REVERSE_BITS_LOWEST; symbol = 0; @@ -157,7 +157,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, step = 2; do { for (bits_count = count[bits]; bits_count != 0; --bits_count) { - code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)sorted[symbol++]); + code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)sorted[symbol++]); ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code); key += key_step; } @@ -170,18 +170,18 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, int root_bits, const uint16_t* const symbol_lists, uint16_t* count) { - HuffmanCode code; /* current table entry */ - HuffmanCode* table; /* next available space in table */ - int len; /* current code length */ - int symbol; /* symbol index in original or sorted table */ - brotli_reg_t key; /* prefix code */ - brotli_reg_t key_step; /* prefix code addend */ - brotli_reg_t sub_key; /* 2nd level table prefix code */ - brotli_reg_t sub_key_step; /* 2nd level table prefix code addend */ - int step; /* step size to replicate values in current table */ - int table_bits; /* key length of current table */ - int table_size; /* size of current table */ - int total_size; /* sum of root table size and 2nd level table sizes */ + HuffmanCode code; /* current table entry */ + HuffmanCode* table; /* next available space in table */ + int len; /* current code length */ + int symbol; /* symbol index in original or sorted table */ + brotli_reg_t key; /* prefix code */ + brotli_reg_t key_step; /* prefix code addend */ + brotli_reg_t sub_key; /* 2nd level table prefix code */ + brotli_reg_t sub_key_step; /* 2nd level table prefix code addend */ + int step; /* step size to replicate values in current table */ + int table_bits; /* key length of current table */ + int table_size; /* size of current table */ + int total_size; /* sum of root table size and 2nd level table sizes */ int max_length = -1; int bits; int bits_count; @@ -198,8 +198,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, table_size = 1 << table_bits; total_size = table_size; - /* Fill in the root table. Reduce the table size to if possible, - and create the repetitions by memcpy. */ + /* Fill in the root table. Reduce the table size to if possible, + and create the repetitions by memcpy. */ if (table_bits > max_length) { table_bits = max_length; table_size = 1 << table_bits; @@ -212,7 +212,7 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, symbol = bits - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); for (bits_count = count[bits]; bits_count != 0; --bits_count) { symbol = symbol_lists[symbol]; - code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)symbol); + code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)symbol); ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code); key += key_step; } @@ -220,14 +220,14 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, key_step >>= 1; } while (++bits <= table_bits); - /* If root_bits != table_bits then replicate to fill the remaining slots. */ + /* If root_bits != table_bits then replicate to fill the remaining slots. */ while (total_size != table_size) { memcpy(&table[table_size], &table[0], (size_t)table_size * sizeof(table[0])); table_size <<= 1; } - /* Fill in 2nd level tables and add pointers to root table. */ + /* Fill in 2nd level tables and add pointers to root table. */ key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1); sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1); sub_key_step = BROTLI_REVERSE_BITS_LOWEST; @@ -241,13 +241,13 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, total_size += table_size; sub_key = BrotliReverseBits(key); key += key_step; - root_table[sub_key] = ConstructHuffmanCode( - (uint8_t)(table_bits + root_bits), - (uint16_t)(((size_t)(table - root_table)) - sub_key)); + root_table[sub_key] = ConstructHuffmanCode( + (uint8_t)(table_bits + root_bits), + (uint16_t)(((size_t)(table - root_table)) - sub_key)); sub_key = 0; } symbol = symbol_lists[symbol]; - code = ConstructHuffmanCode((uint8_t)(len - root_bits), (uint16_t)symbol); + code = ConstructHuffmanCode((uint8_t)(len - root_bits), (uint16_t)symbol); ReplicateValue( &table[BrotliReverseBits(sub_key)], step, table_size, code); sub_key += sub_key_step; @@ -266,27 +266,27 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, const uint32_t goal_size = 1U << root_bits; switch (num_symbols) { case 0: - table[0] = ConstructHuffmanCode(0, val[0]); + table[0] = ConstructHuffmanCode(0, val[0]); break; case 1: if (val[1] > val[0]) { - table[0] = ConstructHuffmanCode(1, val[0]); - table[1] = ConstructHuffmanCode(1, val[1]); + table[0] = ConstructHuffmanCode(1, val[0]); + table[1] = ConstructHuffmanCode(1, val[1]); } else { - table[0] = ConstructHuffmanCode(1, val[1]); - table[1] = ConstructHuffmanCode(1, val[0]); + table[0] = ConstructHuffmanCode(1, val[1]); + table[1] = ConstructHuffmanCode(1, val[0]); } table_size = 2; break; case 2: - table[0] = ConstructHuffmanCode(1, val[0]); - table[2] = ConstructHuffmanCode(1, val[0]); + table[0] = ConstructHuffmanCode(1, val[0]); + table[2] = ConstructHuffmanCode(1, val[0]); if (val[2] > val[1]) { - table[1] = ConstructHuffmanCode(2, val[1]); - table[3] = ConstructHuffmanCode(2, val[2]); + table[1] = ConstructHuffmanCode(2, val[1]); + table[3] = ConstructHuffmanCode(2, val[2]); } else { - table[1] = ConstructHuffmanCode(2, val[2]); - table[3] = ConstructHuffmanCode(2, val[1]); + table[1] = ConstructHuffmanCode(2, val[2]); + table[3] = ConstructHuffmanCode(2, val[1]); } table_size = 4; break; @@ -301,10 +301,10 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, } } } - table[0] = ConstructHuffmanCode(2, val[0]); - table[2] = ConstructHuffmanCode(2, val[1]); - table[1] = ConstructHuffmanCode(2, val[2]); - table[3] = ConstructHuffmanCode(2, val[3]); + table[0] = ConstructHuffmanCode(2, val[0]); + table[2] = ConstructHuffmanCode(2, val[1]); + table[1] = ConstructHuffmanCode(2, val[2]); + table[3] = ConstructHuffmanCode(2, val[3]); table_size = 4; break; } @@ -314,14 +314,14 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, val[3] = val[2]; val[2] = t; } - table[0] = ConstructHuffmanCode(1, val[0]); - table[1] = ConstructHuffmanCode(2, val[1]); - table[2] = ConstructHuffmanCode(1, val[0]); - table[3] = ConstructHuffmanCode(3, val[2]); - table[4] = ConstructHuffmanCode(1, val[0]); - table[5] = ConstructHuffmanCode(2, val[1]); - table[6] = ConstructHuffmanCode(1, val[0]); - table[7] = ConstructHuffmanCode(3, val[3]); + table[0] = ConstructHuffmanCode(1, val[0]); + table[1] = ConstructHuffmanCode(2, val[1]); + table[2] = ConstructHuffmanCode(1, val[0]); + table[3] = ConstructHuffmanCode(3, val[2]); + table[4] = ConstructHuffmanCode(1, val[0]); + table[5] = ConstructHuffmanCode(2, val[1]); + table[6] = ConstructHuffmanCode(1, val[0]); + table[7] = ConstructHuffmanCode(3, val[3]); table_size = 8; break; } diff --git a/contrib/libs/brotli/dec/huffman.h b/contrib/libs/brotli/dec/huffman.h index b9f0716c16..6c99dad441 100644 --- a/contrib/libs/brotli/dec/huffman.h +++ b/contrib/libs/brotli/dec/huffman.h @@ -9,7 +9,7 @@ #ifndef BROTLI_DEC_HUFFMAN_H_ #define BROTLI_DEC_HUFFMAN_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -19,11 +19,11 @@ extern "C" { #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15 /* Maximum possible Huffman table size for an alphabet size of (index * 32), - max code length 15 and root table bits 8. */ + max code length 15 and root table bits 8. */ static const uint16_t kMaxHuffmanTableSize[] = { 256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822, - 854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272, - 1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528}; + 854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272, + 1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528}; /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */ #define BROTLI_HUFFMAN_MAX_SIZE_26 396 /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */ @@ -33,90 +33,90 @@ static const uint16_t kMaxHuffmanTableSize[] = { #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5 -#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \ - BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)) -#define BROTLI_HUFFMAN_CODE_FAST_LOAD -#endif - -#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD) -/* Do not create this struct directly - use the ConstructHuffmanCode - * constructor below! */ +#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \ + BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)) +#define BROTLI_HUFFMAN_CODE_FAST_LOAD +#endif + +#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD) +/* Do not create this struct directly - use the ConstructHuffmanCode + * constructor below! */ typedef struct { uint8_t bits; /* number of bits used for this symbol */ uint16_t value; /* symbol value or table offset */ } HuffmanCode; -static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits, - const uint16_t value) { - HuffmanCode h; - h.bits = bits; - h.value = value; - return h; -} - -/* Please use the following macros to optimize HuffmanCode accesses in hot - * paths. - * - * For example, assuming |table| contains a HuffmanCode pointer: - * - * BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); - * BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table); - * *bits = BROTLI_HC_GET_BITS(table); - * *value = BROTLI_HC_GET_VALUE(table); - * BROTLI_HC_ADJUST_TABLE_INDEX(table, offset); - * *bits2 = BROTLI_HC_GET_BITS(table); - * *value2 = BROTLI_HC_GET_VALUE(table); - * - */ - -#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) -#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V) - -/* These must be given a HuffmanCode pointer! */ -#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits) -#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value) - -#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */ - -typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode; - -static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits, - const uint16_t value) { - return ((value & 0xFFFF) << 16) | (bits & 0xFF); -} - -#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H) -#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H) - -/* These must be given a HuffmanCode pointer! */ -#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF) -#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16) -#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */ - +static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits, + const uint16_t value) { + HuffmanCode h; + h.bits = bits; + h.value = value; + return h; +} + +/* Please use the following macros to optimize HuffmanCode accesses in hot + * paths. + * + * For example, assuming |table| contains a HuffmanCode pointer: + * + * BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); + * BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table); + * *bits = BROTLI_HC_GET_BITS(table); + * *value = BROTLI_HC_GET_VALUE(table); + * BROTLI_HC_ADJUST_TABLE_INDEX(table, offset); + * *bits2 = BROTLI_HC_GET_BITS(table); + * *value2 = BROTLI_HC_GET_VALUE(table); + * + */ + +#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) +#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V) + +/* These must be given a HuffmanCode pointer! */ +#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits) +#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value) + +#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */ + +typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode; + +static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits, + const uint16_t value) { + return ((value & 0xFFFF) << 16) | (bits & 0xFF); +} + +#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H) +#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H) + +/* These must be given a HuffmanCode pointer! */ +#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF) +#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16) +#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */ + /* Builds Huffman lookup table assuming code lengths are in symbol order. */ BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table, const uint8_t* const code_lengths, uint16_t* count); -/* Builds Huffman lookup table assuming code lengths are in symbol order. - Returns size of resulting table. */ +/* Builds Huffman lookup table assuming code lengths are in symbol order. + Returns size of resulting table. */ BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg); -/* Builds a simple Huffman table. The |num_symbols| parameter is to be - interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, - 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2], - 4 means 4 symbols with lengths [1, 2, 3, 3]. */ +/* Builds a simple Huffman table. The |num_symbols| parameter is to be + interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, + 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2], + 4 means 4 symbols with lengths [1, 2, 3, 3]. */ BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, int root_bits, uint16_t* symbols, uint32_t num_symbols); /* Contains a collection of Huffman trees with the same alphabet size. */ -/* max_symbol is needed due to simple codes since log2(alphabet_size) could be - greater than log2(max_symbol). */ +/* max_symbol is needed due to simple codes since log2(alphabet_size) could be + greater than log2(max_symbol). */ typedef struct { HuffmanCode** htrees; HuffmanCode* codes; uint16_t alphabet_size; - uint16_t max_symbol; + uint16_t max_symbol; uint16_t num_htrees; } HuffmanTreeGroup; diff --git a/contrib/libs/brotli/dec/prefix.h b/contrib/libs/brotli/dec/prefix.h index 3ea062d84a..a0331ba0de 100644 --- a/contrib/libs/brotli/dec/prefix.h +++ b/contrib/libs/brotli/dec/prefix.h @@ -5,7 +5,7 @@ */ /* Lookup tables to map prefix codes to value ranges. This is used during - decoding of the block lengths, literal insertion lengths and copy lengths. */ + decoding of the block lengths, literal insertion lengths and copy lengths. */ #ifndef BROTLI_DEC_PREFIX_H_ #define BROTLI_DEC_PREFIX_H_ @@ -13,8 +13,8 @@ #include "../common/constants.h" #include <brotli/types.h> -/* Represents the range of values belonging to a prefix code: - [offset, offset + 2^nbits) */ +/* Represents the range of values belonging to a prefix code: + [offset, offset + 2^nbits) */ struct PrefixCodeRange { uint16_t offset; uint8_t nbits; diff --git a/contrib/libs/brotli/dec/state.c b/contrib/libs/brotli/dec/state.c index e0b37c2dcd..1cbcd9bd69 100644 --- a/contrib/libs/brotli/dec/state.c +++ b/contrib/libs/brotli/dec/state.c @@ -15,11 +15,11 @@ extern "C" { #endif -BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, +BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { if (!alloc_func) { - s->alloc_func = BrotliDefaultAllocFunc; - s->free_func = BrotliDefaultFreeFunc; + s->alloc_func = BrotliDefaultAllocFunc; + s->free_func = BrotliDefaultFreeFunc; s->memory_manager_opaque = 0; } else { s->alloc_func = alloc_func; @@ -31,7 +31,7 @@ BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, BrotliInitBitReader(&s->br); s->state = BROTLI_STATE_UNINITED; - s->large_window = 0; + s->large_window = 0; s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE; s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; @@ -88,18 +88,18 @@ BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1]; s->mtf_upper_bound = 63; - - s->dictionary = BrotliGetDictionary(); - s->transforms = BrotliGetTransforms(); - - return BROTLI_TRUE; + + s->dictionary = BrotliGetDictionary(); + s->transforms = BrotliGetTransforms(); + + return BROTLI_TRUE; } void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { s->meta_block_remaining_len = 0; - s->block_length[0] = 1U << 24; - s->block_length[1] = 1U << 24; - s->block_length[2] = 1U << 24; + s->block_length[0] = 1U << 24; + s->block_length[1] = 1U << 24; + s->block_length[2] = 1U << 24; s->num_block_types[0] = 1; s->num_block_types[1] = 1; s->num_block_types[2] = 1; @@ -116,7 +116,7 @@ void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { s->literal_htree = NULL; s->dist_context_map_slice = NULL; s->dist_htree_index = 0; - s->context_lookup = NULL; + s->context_lookup = NULL; s->literal_hgroup.codes = NULL; s->literal_hgroup.htrees = NULL; s->insert_copy_hgroup.codes = NULL; @@ -126,33 +126,33 @@ void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { } void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) { - BROTLI_DECODER_FREE(s, s->context_modes); - BROTLI_DECODER_FREE(s, s->context_map); - BROTLI_DECODER_FREE(s, s->dist_context_map); - BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees); - BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees); - BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees); + BROTLI_DECODER_FREE(s, s->context_modes); + BROTLI_DECODER_FREE(s, s->context_map); + BROTLI_DECODER_FREE(s, s->dist_context_map); + BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees); + BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees); + BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees); } void BrotliDecoderStateCleanup(BrotliDecoderState* s) { BrotliDecoderStateCleanupAfterMetablock(s); - BROTLI_DECODER_FREE(s, s->ringbuffer); - BROTLI_DECODER_FREE(s, s->block_type_trees); + BROTLI_DECODER_FREE(s, s->ringbuffer); + BROTLI_DECODER_FREE(s, s->block_type_trees); } BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s, - HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t max_symbol, - uint32_t ntrees) { + HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t max_symbol, + uint32_t ntrees) { /* Pack two allocations into one */ const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5]; const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; const size_t htree_size = sizeof(HuffmanCode*) * ntrees; /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */ - HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s, - code_size + htree_size); + HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s, + code_size + htree_size); group->alphabet_size = (uint16_t)alphabet_size; - group->max_symbol = (uint16_t)max_symbol; + group->max_symbol = (uint16_t)max_symbol; group->num_htrees = (uint16_t)ntrees; group->htrees = p; group->codes = (HuffmanCode*)(&p[ntrees]); diff --git a/contrib/libs/brotli/dec/state.h b/contrib/libs/brotli/dec/state.h index d28b63920e..fa0285ef6a 100644 --- a/contrib/libs/brotli/dec/state.h +++ b/contrib/libs/brotli/dec/state.h @@ -11,8 +11,8 @@ #include "../common/constants.h" #include "../common/dictionary.h" -#include "../common/platform.h" -#include "../common/transform.h" +#include "../common/platform.h" +#include "../common/transform.h" #include <brotli/types.h> #include "./bit_reader.h" #include "./huffman.h" @@ -23,8 +23,8 @@ extern "C" { typedef enum { BROTLI_STATE_UNINITED, - BROTLI_STATE_LARGE_WINDOW_BITS, - BROTLI_STATE_INITIALIZE, + BROTLI_STATE_LARGE_WINDOW_BITS, + BROTLI_STATE_INITIALIZE, BROTLI_STATE_METABLOCK_BEGIN, BROTLI_STATE_METABLOCK_HEADER, BROTLI_STATE_METABLOCK_HEADER_2, @@ -129,22 +129,22 @@ struct BrotliDecoderStateStruct { uint8_t* ringbuffer; uint8_t* ringbuffer_end; HuffmanCode* htree_command; - const uint8_t* context_lookup; + const uint8_t* context_lookup; uint8_t* context_map_slice; uint8_t* dist_context_map_slice; - /* This ring buffer holds a few past copy distances that will be used by - some special distance codes. */ + /* This ring buffer holds a few past copy distances that will be used by + some special distance codes. */ HuffmanTreeGroup literal_hgroup; HuffmanTreeGroup insert_copy_hgroup; HuffmanTreeGroup distance_hgroup; HuffmanCode* block_type_trees; HuffmanCode* block_len_trees; /* This is true if the literal context map histogram type always matches the - block type. It is then not needed to keep the context (faster decoding). */ + block type. It is then not needed to keep the context (faster decoding). */ int trivial_literal_context; - /* Distance context is actual after command is decoded and before distance is - computed. After distance computation it is used as a temporary variable. */ + /* Distance context is actual after command is decoded and before distance is + computed. After distance computation it is used as a temporary variable. */ int distance_context; int meta_block_remaining_len; uint32_t block_length_index; @@ -164,11 +164,11 @@ struct BrotliDecoderStateStruct { int copy_length; int distance_code; - /* For partial write operations. */ - size_t rb_roundtrips; /* how many times we went around the ring-buffer */ - size_t partial_pos_out; /* how much output to the user in total */ + /* For partial write operations. */ + size_t rb_roundtrips; /* how many times we went around the ring-buffer */ + size_t partial_pos_out; /* how much output to the user in total */ - /* For ReadHuffmanCode. */ + /* For ReadHuffmanCode. */ uint32_t symbol; uint32_t repeat; uint32_t space; @@ -182,26 +182,26 @@ struct BrotliDecoderStateStruct { /* Tails of symbol chains. */ int next_symbol[32]; uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES]; - /* Population counts for the code lengths. */ + /* Population counts for the code lengths. */ uint16_t code_length_histo[16]; - /* For HuffmanTreeGroupDecode. */ + /* For HuffmanTreeGroupDecode. */ int htree_index; HuffmanCode* next; - /* For DecodeContextMap. */ + /* For DecodeContextMap. */ uint32_t context_index; uint32_t max_run_length_prefix; uint32_t code; HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272]; - /* For InverseMoveToFrontTransform. */ + /* For InverseMoveToFrontTransform. */ uint32_t mtf_upper_bound; uint32_t mtf[64 + 1]; - /* Less used attributes are at the end of this struct. */ - - /* States inside function calls. */ + /* Less used attributes are at the end of this struct. */ + + /* States inside function calls. */ BrotliRunningMetablockHeaderState substate_metablock_header; BrotliRunningTreeGroupState substate_tree_group; BrotliRunningContextMapState substate_context_map; @@ -215,7 +215,7 @@ struct BrotliDecoderStateStruct { unsigned int is_metadata : 1; unsigned int should_wrap_ringbuffer : 1; unsigned int canny_ringbuffer_allocation : 1; - unsigned int large_window : 1; + unsigned int large_window : 1; unsigned int size_nibbles : 8; uint32_t window_bits; @@ -224,9 +224,9 @@ struct BrotliDecoderStateStruct { uint32_t num_literal_htrees; uint8_t* context_map; uint8_t* context_modes; - + const BrotliDictionary* dictionary; - const BrotliTransforms* transforms; + const BrotliTransforms* transforms; uint32_t trivial_literal_contexts[8]; /* 256 bits */ }; @@ -234,23 +234,23 @@ struct BrotliDecoderStateStruct { typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal; #define BrotliDecoderState BrotliDecoderStateInternal -BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, - brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque); +BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, + brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque); BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s); BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s); BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock( BrotliDecoderState* s); BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit( BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size, - uint32_t max_symbol, uint32_t ntrees); - -#define BROTLI_DECODER_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L) - -#define BROTLI_DECODER_FREE(S, X) { \ - S->free_func(S->memory_manager_opaque, X); \ - X = NULL; \ -} - + uint32_t max_symbol, uint32_t ntrees); + +#define BROTLI_DECODER_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L) + +#define BROTLI_DECODER_FREE(S, X) { \ + S->free_func(S->memory_manager_opaque, X); \ + X = NULL; \ +} + #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ #endif diff --git a/contrib/libs/brotli/enc/backward_references.c b/contrib/libs/brotli/enc/backward_references.c index cd023d9b41..7f75b7bfdb 100644 --- a/contrib/libs/brotli/enc/backward_references.c +++ b/contrib/libs/brotli/enc/backward_references.c @@ -10,7 +10,7 @@ #include "../common/constants.h" #include "../common/dictionary.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./command.h" #include "./dictionary_hash.h" @@ -49,7 +49,7 @@ static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance, #define CAT(a, b) a ## b #define FN(X) EXPAND_CAT(X, HASHER()) #define EXPORT_FN(X) EXPAND_CAT(X, EXPAND_CAT(PREFIX(), HASHER())) - + #define PREFIX() N #define HASHER() H2 @@ -97,38 +97,38 @@ static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance, #include "./backward_references_inc.h" #undef HASHER -#define HASHER() H35 -/* NOLINTNEXTLINE(build/include) */ -#include "./backward_references_inc.h" -#undef HASHER - -#define HASHER() H55 -/* NOLINTNEXTLINE(build/include) */ -#include "./backward_references_inc.h" -#undef HASHER - -#define HASHER() H65 -/* NOLINTNEXTLINE(build/include) */ -#include "./backward_references_inc.h" -#undef HASHER - +#define HASHER() H35 +/* NOLINTNEXTLINE(build/include) */ +#include "./backward_references_inc.h" +#undef HASHER + +#define HASHER() H55 +/* NOLINTNEXTLINE(build/include) */ +#include "./backward_references_inc.h" +#undef HASHER + +#define HASHER() H65 +/* NOLINTNEXTLINE(build/include) */ +#include "./backward_references_inc.h" +#undef HASHER + #undef PREFIX - + #undef EXPORT_FN #undef FN #undef CAT #undef EXPAND_CAT -void BrotliCreateBackwardReferences( - size_t num_bytes, size_t position, const uint8_t* ringbuffer, - size_t ringbuffer_mask, const BrotliEncoderParams* params, - HasherHandle hasher, int* dist_cache, size_t* last_insert_len, - Command* commands, size_t* num_commands, size_t* num_literals) { +void BrotliCreateBackwardReferences( + size_t num_bytes, size_t position, const uint8_t* ringbuffer, + size_t ringbuffer_mask, const BrotliEncoderParams* params, + HasherHandle hasher, int* dist_cache, size_t* last_insert_len, + Command* commands, size_t* num_commands, size_t* num_literals) { switch (params->hasher.type) { #define CASE_(N) \ case N: \ - CreateBackwardReferencesNH ## N( \ - num_bytes, position, ringbuffer, \ + CreateBackwardReferencesNH ## N( \ + num_bytes, position, ringbuffer, \ ringbuffer_mask, params, hasher, dist_cache, \ last_insert_len, commands, num_commands, num_literals); \ return; diff --git a/contrib/libs/brotli/enc/backward_references.h b/contrib/libs/brotli/enc/backward_references.h index 3a4146647c..6243d0cef1 100644 --- a/contrib/libs/brotli/enc/backward_references.h +++ b/contrib/libs/brotli/enc/backward_references.h @@ -11,7 +11,7 @@ #include "../common/constants.h" #include "../common/dictionary.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./command.h" #include "./hash.h" @@ -26,10 +26,10 @@ extern "C" { CreateBackwardReferences calls, and must be incremented by the amount written by this call. */ BROTLI_INTERNAL void BrotliCreateBackwardReferences( - size_t num_bytes, size_t position, const uint8_t* ringbuffer, - size_t ringbuffer_mask, const BrotliEncoderParams* params, - HasherHandle hasher, int* dist_cache, size_t* last_insert_len, - Command* commands, size_t* num_commands, size_t* num_literals); + size_t num_bytes, size_t position, const uint8_t* ringbuffer, + size_t ringbuffer_mask, const BrotliEncoderParams* params, + HasherHandle hasher, int* dist_cache, size_t* last_insert_len, + Command* commands, size_t* num_commands, size_t* num_literals); #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ diff --git a/contrib/libs/brotli/enc/backward_references_hq.c b/contrib/libs/brotli/enc/backward_references_hq.c index 96b0e708de..2e75f2227c 100644 --- a/contrib/libs/brotli/enc/backward_references_hq.c +++ b/contrib/libs/brotli/enc/backward_references_hq.c @@ -11,14 +11,14 @@ #include <string.h> /* memcpy, memset */ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./command.h" #include "./fast_log.h" #include "./find_match_length.h" #include "./literal_cost.h" #include "./memory.h" -#include "./params.h" +#include "./params.h" #include "./prefix.h" #include "./quality.h" @@ -26,8 +26,8 @@ extern "C" { #endif -#define BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE 544 - +#define BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE 544 + static const float kInfinity = 1.7e38f; /* ~= 2 ^ 127 */ static const uint32_t kDistanceCacheIndex[] = { @@ -42,41 +42,41 @@ void BrotliInitZopfliNodes(ZopfliNode* array, size_t length) { size_t i; stub.length = 1; stub.distance = 0; - stub.dcode_insert_length = 0; + stub.dcode_insert_length = 0; stub.u.cost = kInfinity; for (i = 0; i < length; ++i) array[i] = stub; } static BROTLI_INLINE uint32_t ZopfliNodeCopyLength(const ZopfliNode* self) { - return self->length & 0x1FFFFFF; + return self->length & 0x1FFFFFF; } static BROTLI_INLINE uint32_t ZopfliNodeLengthCode(const ZopfliNode* self) { - const uint32_t modifier = self->length >> 25; + const uint32_t modifier = self->length >> 25; return ZopfliNodeCopyLength(self) + 9u - modifier; } static BROTLI_INLINE uint32_t ZopfliNodeCopyDistance(const ZopfliNode* self) { - return self->distance; + return self->distance; } static BROTLI_INLINE uint32_t ZopfliNodeDistanceCode(const ZopfliNode* self) { - const uint32_t short_code = self->dcode_insert_length >> 27; + const uint32_t short_code = self->dcode_insert_length >> 27; return short_code == 0 ? ZopfliNodeCopyDistance(self) + BROTLI_NUM_DISTANCE_SHORT_CODES - 1 : short_code - 1; } static BROTLI_INLINE uint32_t ZopfliNodeCommandLength(const ZopfliNode* self) { - return ZopfliNodeCopyLength(self) + (self->dcode_insert_length & 0x7FFFFFF); + return ZopfliNodeCopyLength(self) + (self->dcode_insert_length & 0x7FFFFFF); } /* Histogram based cost model for zopflification. */ typedef struct ZopfliCostModel { /* The insert and copy length symbols. */ float cost_cmd_[BROTLI_NUM_COMMAND_SYMBOLS]; - float* cost_dist_; - uint32_t distance_histogram_size; + float* cost_dist_; + uint32_t distance_histogram_size; /* Cumulative costs of literals per position in the stream. */ float* literal_costs_; float min_cost_cmd_; @@ -84,45 +84,45 @@ typedef struct ZopfliCostModel { } ZopfliCostModel; static void InitZopfliCostModel( - MemoryManager* m, ZopfliCostModel* self, const BrotliDistanceParams* dist, - size_t num_bytes) { - uint32_t distance_histogram_size = dist->alphabet_size; - if (distance_histogram_size > BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE) { - distance_histogram_size = BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE; - } + MemoryManager* m, ZopfliCostModel* self, const BrotliDistanceParams* dist, + size_t num_bytes) { + uint32_t distance_histogram_size = dist->alphabet_size; + if (distance_histogram_size > BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE) { + distance_histogram_size = BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE; + } self->num_bytes_ = num_bytes; self->literal_costs_ = BROTLI_ALLOC(m, float, num_bytes + 2); - self->cost_dist_ = BROTLI_ALLOC(m, float, dist->alphabet_size); - self->distance_histogram_size = distance_histogram_size; + self->cost_dist_ = BROTLI_ALLOC(m, float, dist->alphabet_size); + self->distance_histogram_size = distance_histogram_size; if (BROTLI_IS_OOM(m)) return; } static void CleanupZopfliCostModel(MemoryManager* m, ZopfliCostModel* self) { BROTLI_FREE(m, self->literal_costs_); - BROTLI_FREE(m, self->cost_dist_); + BROTLI_FREE(m, self->cost_dist_); } static void SetCost(const uint32_t* histogram, size_t histogram_size, - BROTLI_BOOL literal_histogram, float* cost) { + BROTLI_BOOL literal_histogram, float* cost) { size_t sum = 0; - size_t missing_symbol_sum; + size_t missing_symbol_sum; float log2sum; - float missing_symbol_cost; + float missing_symbol_cost; size_t i; for (i = 0; i < histogram_size; i++) { sum += histogram[i]; } log2sum = (float)FastLog2(sum); - missing_symbol_sum = sum; - if (!literal_histogram) { - for (i = 0; i < histogram_size; i++) { - if (histogram[i] == 0) missing_symbol_sum++; - } - } - missing_symbol_cost = (float)FastLog2(missing_symbol_sum) + 2; + missing_symbol_sum = sum; + if (!literal_histogram) { + for (i = 0; i < histogram_size; i++) { + if (histogram[i] == 0) missing_symbol_sum++; + } + } + missing_symbol_cost = (float)FastLog2(missing_symbol_sum) + 2; for (i = 0; i < histogram_size; i++) { if (histogram[i] == 0) { - cost[i] = missing_symbol_cost; + cost[i] = missing_symbol_cost; continue; } @@ -143,7 +143,7 @@ static void ZopfliCostModelSetFromCommands(ZopfliCostModel* self, size_t last_insert_len) { uint32_t histogram_literal[BROTLI_NUM_LITERAL_SYMBOLS]; uint32_t histogram_cmd[BROTLI_NUM_COMMAND_SYMBOLS]; - uint32_t histogram_dist[BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE]; + uint32_t histogram_dist[BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE]; float cost_literal[BROTLI_NUM_LITERAL_SYMBOLS]; size_t pos = position - last_insert_len; float min_cost_cmd = kInfinity; @@ -157,7 +157,7 @@ static void ZopfliCostModelSetFromCommands(ZopfliCostModel* self, for (i = 0; i < num_commands; i++) { size_t inslength = commands[i].insert_len_; size_t copylength = CommandCopyLen(&commands[i]); - size_t distcode = commands[i].dist_prefix_ & 0x3FF; + size_t distcode = commands[i].dist_prefix_ & 0x3FF; size_t cmdcode = commands[i].cmd_prefix_; size_t j; @@ -171,12 +171,12 @@ static void ZopfliCostModelSetFromCommands(ZopfliCostModel* self, pos += inslength + copylength; } - SetCost(histogram_literal, BROTLI_NUM_LITERAL_SYMBOLS, BROTLI_TRUE, - cost_literal); - SetCost(histogram_cmd, BROTLI_NUM_COMMAND_SYMBOLS, BROTLI_FALSE, - cost_cmd); - SetCost(histogram_dist, self->distance_histogram_size, BROTLI_FALSE, - self->cost_dist_); + SetCost(histogram_literal, BROTLI_NUM_LITERAL_SYMBOLS, BROTLI_TRUE, + cost_literal); + SetCost(histogram_cmd, BROTLI_NUM_COMMAND_SYMBOLS, BROTLI_FALSE, + cost_cmd); + SetCost(histogram_dist, self->distance_histogram_size, BROTLI_FALSE, + self->cost_dist_); for (i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; ++i) { min_cost_cmd = BROTLI_MIN(float, min_cost_cmd, cost_cmd[i]); @@ -185,14 +185,14 @@ static void ZopfliCostModelSetFromCommands(ZopfliCostModel* self, { float* literal_costs = self->literal_costs_; - float literal_carry = 0.0; + float literal_carry = 0.0; size_t num_bytes = self->num_bytes_; literal_costs[0] = 0.0; for (i = 0; i < num_bytes; ++i) { - literal_carry += + literal_carry += cost_literal[ringbuffer[(position + i) & ringbuffer_mask]]; - literal_costs[i + 1] = literal_costs[i] + literal_carry; - literal_carry -= literal_costs[i + 1] - literal_costs[i]; + literal_costs[i + 1] = literal_costs[i] + literal_carry; + literal_carry -= literal_costs[i + 1] - literal_costs[i]; } } } @@ -202,7 +202,7 @@ static void ZopfliCostModelSetFromLiteralCosts(ZopfliCostModel* self, const uint8_t* ringbuffer, size_t ringbuffer_mask) { float* literal_costs = self->literal_costs_; - float literal_carry = 0.0; + float literal_carry = 0.0; float* cost_dist = self->cost_dist_; float* cost_cmd = self->cost_cmd_; size_t num_bytes = self->num_bytes_; @@ -211,14 +211,14 @@ static void ZopfliCostModelSetFromLiteralCosts(ZopfliCostModel* self, ringbuffer, &literal_costs[1]); literal_costs[0] = 0.0; for (i = 0; i < num_bytes; ++i) { - literal_carry += literal_costs[i + 1]; - literal_costs[i + 1] = literal_costs[i] + literal_carry; - literal_carry -= literal_costs[i + 1] - literal_costs[i]; + literal_carry += literal_costs[i + 1]; + literal_costs[i + 1] = literal_costs[i] + literal_carry; + literal_carry -= literal_costs[i + 1] - literal_costs[i]; } for (i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; ++i) { cost_cmd[i] = (float)FastLog2(11 + (uint32_t)i); } - for (i = 0; i < self->distance_histogram_size; ++i) { + for (i = 0; i < self->distance_histogram_size; ++i) { cost_dist[i] = (float)FastLog2(20 + (uint32_t)i); } self->min_cost_cmd_ = (float)FastLog2(11); @@ -251,10 +251,10 @@ static BROTLI_INLINE void UpdateZopfliNode(ZopfliNode* nodes, size_t pos, size_t start_pos, size_t len, size_t len_code, size_t dist, size_t short_code, float cost) { ZopfliNode* next = &nodes[pos + len]; - next->length = (uint32_t)(len | ((len + 9u - len_code) << 25)); - next->distance = (uint32_t)dist; - next->dcode_insert_length = (uint32_t)( - (short_code << 27) | (pos - start_pos)); + next->length = (uint32_t)(len | ((len + 9u - len_code) << 25)); + next->distance = (uint32_t)dist; + next->dcode_insert_length = (uint32_t)( + (short_code << 27) | (pos - start_pos)); next->u.cost = cost; } @@ -330,21 +330,21 @@ static size_t ComputeMinimumCopyLength(const float start_cost, REQUIRES: nodes[0..pos] satisfies that "ZopfliNode array invariant". */ static uint32_t ComputeDistanceShortcut(const size_t block_start, const size_t pos, - const size_t max_backward_limit, + const size_t max_backward_limit, const size_t gap, const ZopfliNode* nodes) { const size_t clen = ZopfliNodeCopyLength(&nodes[pos]); - const size_t ilen = nodes[pos].dcode_insert_length & 0x7FFFFFF; + const size_t ilen = nodes[pos].dcode_insert_length & 0x7FFFFFF; const size_t dist = ZopfliNodeCopyDistance(&nodes[pos]); /* Since |block_start + pos| is the end position of the command, the copy part starts from |block_start + pos - clen|. Distances that are greater than - this or greater than |max_backward_limit| + |gap| are static dictionary - references, and do not update the last distances. - Also distance code 0 (last distance) does not update the last distances. */ + this or greater than |max_backward_limit| + |gap| are static dictionary + references, and do not update the last distances. + Also distance code 0 (last distance) does not update the last distances. */ if (pos == 0) { return 0; } else if (dist + clen <= block_start + pos + gap && - dist <= max_backward_limit + gap && + dist <= max_backward_limit + gap && ZopfliNodeDistanceCode(&nodes[pos]) > 0) { return (uint32_t)pos; } else { @@ -366,7 +366,7 @@ static void ComputeDistanceCache(const size_t pos, int idx = 0; size_t p = nodes[pos].u.shortcut; while (idx < 4 && p > 0) { - const size_t ilen = nodes[p].dcode_insert_length & 0x7FFFFFF; + const size_t ilen = nodes[p].dcode_insert_length & 0x7FFFFFF; const size_t clen = ZopfliNodeCopyLength(&nodes[p]); const size_t dist = ZopfliNodeCopyDistance(&nodes[p]); dist_cache[idx++] = (int)dist; @@ -454,11 +454,11 @@ static size_t UpdateNodes( break; } if (BROTLI_PREDICT_FALSE(backward > max_distance + gap)) { - /* Word dictionary -> ignore. */ + /* Word dictionary -> ignore. */ continue; } if (backward <= max_distance) { - /* Regular backward reference. */ + /* Regular backward reference. */ if (prev_ix >= cur_ix) { continue; } @@ -515,12 +515,12 @@ static size_t UpdateNodes( uint32_t distnumextra; float dist_cost; size_t max_match_len; - PrefixEncodeCopyDistance( - dist_code, params->dist.num_direct_distance_codes, - params->dist.distance_postfix_bits, &dist_symbol, &distextra); - distnumextra = dist_symbol >> 10; + PrefixEncodeCopyDistance( + dist_code, params->dist.num_direct_distance_codes, + params->dist.distance_postfix_bits, &dist_symbol, &distextra); + distnumextra = dist_symbol >> 10; dist_cost = base_cost + (float)distnumextra + - ZopfliCostModelGetDistanceCost(model, dist_symbol & 0x3FF); + ZopfliCostModelGetDistanceCost(model, dist_symbol & 0x3FF); /* Try all copy lengths up until the maximum copy length corresponding to this distance. If the distance refers to the static dictionary, or @@ -552,8 +552,8 @@ static size_t ComputeShortestPathFromNodes(size_t num_bytes, ZopfliNode* nodes) { size_t index = num_bytes; size_t num_commands = 0; - while ((nodes[index].dcode_insert_length & 0x7FFFFFF) == 0 && - nodes[index].length == 1) --index; + while ((nodes[index].dcode_insert_length & 0x7FFFFFF) == 0 && + nodes[index].length == 1) --index; nodes[index].u.next = BROTLI_UINT32_MAX; while (index != 0) { size_t len = ZopfliNodeCommandLength(&nodes[index]); @@ -566,10 +566,10 @@ static size_t ComputeShortestPathFromNodes(size_t num_bytes, /* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */ void BrotliZopfliCreateCommands(const size_t num_bytes, - const size_t block_start, const ZopfliNode* nodes, int* dist_cache, - size_t* last_insert_len, const BrotliEncoderParams* params, - Command* commands, size_t* num_literals) { - const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); + const size_t block_start, const ZopfliNode* nodes, int* dist_cache, + size_t* last_insert_len, const BrotliEncoderParams* params, + Command* commands, size_t* num_literals) { + const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); size_t pos = 0; uint32_t offset = nodes[0].u.next; size_t i; @@ -577,7 +577,7 @@ void BrotliZopfliCreateCommands(const size_t num_bytes, for (i = 0; offset != BROTLI_UINT32_MAX; i++) { const ZopfliNode* next = &nodes[pos + offset]; size_t copy_length = ZopfliNodeCopyLength(next); - size_t insert_length = next->dcode_insert_length & 0x7FFFFFF; + size_t insert_length = next->dcode_insert_length & 0x7FFFFFF; pos += insert_length; offset = next->u.next; if (i == 0) { @@ -591,7 +591,7 @@ void BrotliZopfliCreateCommands(const size_t num_bytes, BROTLI_MIN(size_t, block_start + pos, max_backward_limit); BROTLI_BOOL is_dictionary = TO_BROTLI_BOOL(distance > max_distance + gap); size_t dist_code = ZopfliNodeDistanceCode(next); - InitCommand(&commands[i], ¶ms->dist, insert_length, + InitCommand(&commands[i], ¶ms->dist, insert_length, copy_length, (int)len_code - (int)copy_length, dist_code); if (!is_dictionary && dist_code > 0) { @@ -608,12 +608,12 @@ void BrotliZopfliCreateCommands(const size_t num_bytes, *last_insert_len += num_bytes - pos; } -static size_t ZopfliIterate(size_t num_bytes, size_t position, - const uint8_t* ringbuffer, size_t ringbuffer_mask, - const BrotliEncoderParams* params, const size_t gap, const int* dist_cache, - const ZopfliCostModel* model, const uint32_t* num_matches, - const BackwardMatch* matches, ZopfliNode* nodes) { - const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); +static size_t ZopfliIterate(size_t num_bytes, size_t position, + const uint8_t* ringbuffer, size_t ringbuffer_mask, + const BrotliEncoderParams* params, const size_t gap, const int* dist_cache, + const ZopfliCostModel* model, const uint32_t* num_matches, + const BackwardMatch* matches, ZopfliNode* nodes) { + const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); const size_t max_zopfli_len = MaxZopfliLen(params); StartPosQueue queue; size_t cur_match_pos = 0; @@ -637,8 +637,8 @@ static size_t ZopfliIterate(size_t num_bytes, size_t position, while (skip) { i++; if (i + 3 >= num_bytes) break; - EvaluateNode(position, i, max_backward_limit, gap, - dist_cache, model, &queue, nodes); + EvaluateNode(position, i, max_backward_limit, gap, + dist_cache, model, &queue, nodes); cur_match_pos += num_matches[i]; skip--; } @@ -648,23 +648,23 @@ static size_t ZopfliIterate(size_t num_bytes, size_t position, } /* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */ -size_t BrotliZopfliComputeShortestPath(MemoryManager* m, size_t num_bytes, - size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask, - const BrotliEncoderParams* params, - const int* dist_cache, HasherHandle hasher, ZopfliNode* nodes) { - const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); +size_t BrotliZopfliComputeShortestPath(MemoryManager* m, size_t num_bytes, + size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask, + const BrotliEncoderParams* params, + const int* dist_cache, HasherHandle hasher, ZopfliNode* nodes) { + const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); const size_t max_zopfli_len = MaxZopfliLen(params); ZopfliCostModel model; StartPosQueue queue; - BackwardMatch matches[2 * (MAX_NUM_MATCHES_H10 + 64)]; + BackwardMatch matches[2 * (MAX_NUM_MATCHES_H10 + 64)]; const size_t store_end = num_bytes >= StoreLookaheadH10() ? position + num_bytes - StoreLookaheadH10() + 1 : position; size_t i; size_t gap = 0; - size_t lz_matches_offset = 0; + size_t lz_matches_offset = 0; nodes[0].length = 0; nodes[0].u.cost = 0; - InitZopfliCostModel(m, &model, ¶ms->dist, num_bytes); + InitZopfliCostModel(m, &model, ¶ms->dist, num_bytes); if (BROTLI_IS_OOM(m)) return 0; ZopfliCostModelSetFromLiteralCosts( &model, position, ringbuffer, ringbuffer_mask); @@ -673,11 +673,11 @@ size_t BrotliZopfliComputeShortestPath(MemoryManager* m, size_t num_bytes, const size_t pos = position + i; const size_t max_distance = BROTLI_MIN(size_t, pos, max_backward_limit); size_t skip; - size_t num_matches; - num_matches = FindAllMatchesH10(hasher, - ¶ms->dictionary, - ringbuffer, ringbuffer_mask, pos, num_bytes - i, max_distance, - gap, params, &matches[lz_matches_offset]); + size_t num_matches; + num_matches = FindAllMatchesH10(hasher, + ¶ms->dictionary, + ringbuffer, ringbuffer_mask, pos, num_bytes - i, max_distance, + gap, params, &matches[lz_matches_offset]); if (num_matches > 0 && BackwardMatchLength(&matches[num_matches - 1]) > max_zopfli_len) { matches[0] = matches[num_matches - 1]; @@ -698,8 +698,8 @@ size_t BrotliZopfliComputeShortestPath(MemoryManager* m, size_t num_bytes, while (skip) { i++; if (i + HashTypeLengthH10() - 1 >= num_bytes) break; - EvaluateNode(position, i, max_backward_limit, gap, - dist_cache, &model, &queue, nodes); + EvaluateNode(position, i, max_backward_limit, gap, + dist_cache, &model, &queue, nodes); skip--; } } @@ -708,29 +708,29 @@ size_t BrotliZopfliComputeShortestPath(MemoryManager* m, size_t num_bytes, return ComputeShortestPathFromNodes(num_bytes, nodes); } -void BrotliCreateZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, +void BrotliCreateZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask, - const BrotliEncoderParams* params, - HasherHandle hasher, int* dist_cache, size_t* last_insert_len, - Command* commands, size_t* num_commands, size_t* num_literals) { + const BrotliEncoderParams* params, + HasherHandle hasher, int* dist_cache, size_t* last_insert_len, + Command* commands, size_t* num_commands, size_t* num_literals) { ZopfliNode* nodes; nodes = BROTLI_ALLOC(m, ZopfliNode, num_bytes + 1); if (BROTLI_IS_OOM(m)) return; BrotliInitZopfliNodes(nodes, num_bytes + 1); - *num_commands += BrotliZopfliComputeShortestPath(m, num_bytes, - position, ringbuffer, ringbuffer_mask, params, + *num_commands += BrotliZopfliComputeShortestPath(m, num_bytes, + position, ringbuffer, ringbuffer_mask, params, dist_cache, hasher, nodes); if (BROTLI_IS_OOM(m)) return; - BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache, - last_insert_len, params, commands, num_literals); + BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache, + last_insert_len, params, commands, num_literals); BROTLI_FREE(m, nodes); } -void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, +void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask, - const BrotliEncoderParams* params, - HasherHandle hasher, int* dist_cache, size_t* last_insert_len, - Command* commands, size_t* num_commands, size_t* num_literals) { + const BrotliEncoderParams* params, + HasherHandle hasher, int* dist_cache, size_t* last_insert_len, + Command* commands, size_t* num_commands, size_t* num_literals) { const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); uint32_t* num_matches = BROTLI_ALLOC(m, uint32_t, num_bytes); size_t matches_size = 4 * num_bytes; @@ -746,7 +746,7 @@ void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, ZopfliNode* nodes; BackwardMatch* matches = BROTLI_ALLOC(m, BackwardMatch, matches_size); size_t gap = 0; - size_t shadow_matches = 0; + size_t shadow_matches = 0; if (BROTLI_IS_OOM(m)) return; for (i = 0; i + HashTypeLengthH10() - 1 < num_bytes; ++i) { const size_t pos = position + i; @@ -757,16 +757,16 @@ void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, size_t j; /* Ensure that we have enough free slots. */ BROTLI_ENSURE_CAPACITY(m, BackwardMatch, matches, matches_size, - cur_match_pos + MAX_NUM_MATCHES_H10 + shadow_matches); + cur_match_pos + MAX_NUM_MATCHES_H10 + shadow_matches); if (BROTLI_IS_OOM(m)) return; - num_found_matches = FindAllMatchesH10(hasher, - ¶ms->dictionary, - ringbuffer, ringbuffer_mask, pos, max_length, - max_distance, gap, params, - &matches[cur_match_pos + shadow_matches]); + num_found_matches = FindAllMatchesH10(hasher, + ¶ms->dictionary, + ringbuffer, ringbuffer_mask, pos, max_length, + max_distance, gap, params, + &matches[cur_match_pos + shadow_matches]); cur_match_end = cur_match_pos + num_found_matches; for (j = cur_match_pos; j + 1 < cur_match_end; ++j) { - BROTLI_DCHECK(BackwardMatchLength(&matches[j]) <= + BROTLI_DCHECK(BackwardMatchLength(&matches[j]) <= BackwardMatchLength(&matches[j + 1])); } num_matches[i] = (uint32_t)num_found_matches; @@ -792,7 +792,7 @@ void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, orig_num_commands = *num_commands; nodes = BROTLI_ALLOC(m, ZopfliNode, num_bytes + 1); if (BROTLI_IS_OOM(m)) return; - InitZopfliCostModel(m, &model, ¶ms->dist, num_bytes); + InitZopfliCostModel(m, &model, ¶ms->dist, num_bytes); if (BROTLI_IS_OOM(m)) return; for (i = 0; i < 2; i++) { BrotliInitZopfliNodes(nodes, num_bytes + 1); @@ -809,10 +809,10 @@ void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, size_t num_bytes, *last_insert_len = orig_last_insert_len; memcpy(dist_cache, orig_dist_cache, 4 * sizeof(dist_cache[0])); *num_commands += ZopfliIterate(num_bytes, position, ringbuffer, - ringbuffer_mask, params, gap, dist_cache, &model, num_matches, matches, - nodes); - BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache, - last_insert_len, params, commands, num_literals); + ringbuffer_mask, params, gap, dist_cache, &model, num_matches, matches, + nodes); + BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache, + last_insert_len, params, commands, num_literals); } CleanupZopfliCostModel(m, &model); BROTLI_FREE(m, nodes); diff --git a/contrib/libs/brotli/enc/backward_references_hq.h b/contrib/libs/brotli/enc/backward_references_hq.h index 1e4275d403..6996bd1c42 100644 --- a/contrib/libs/brotli/enc/backward_references_hq.h +++ b/contrib/libs/brotli/enc/backward_references_hq.h @@ -11,7 +11,7 @@ #include "../common/constants.h" #include "../common/dictionary.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./command.h" #include "./hash.h" @@ -22,27 +22,27 @@ extern "C" { #endif -BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(MemoryManager* m, - size_t num_bytes, size_t position, const uint8_t* ringbuffer, - size_t ringbuffer_mask, const BrotliEncoderParams* params, - HasherHandle hasher, int* dist_cache, size_t* last_insert_len, - Command* commands, size_t* num_commands, size_t* num_literals); +BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(MemoryManager* m, + size_t num_bytes, size_t position, const uint8_t* ringbuffer, + size_t ringbuffer_mask, const BrotliEncoderParams* params, + HasherHandle hasher, int* dist_cache, size_t* last_insert_len, + Command* commands, size_t* num_commands, size_t* num_literals); -BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, - size_t num_bytes, size_t position, const uint8_t* ringbuffer, - size_t ringbuffer_mask, const BrotliEncoderParams* params, - HasherHandle hasher, int* dist_cache, size_t* last_insert_len, - Command* commands, size_t* num_commands, size_t* num_literals); +BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, + size_t num_bytes, size_t position, const uint8_t* ringbuffer, + size_t ringbuffer_mask, const BrotliEncoderParams* params, + HasherHandle hasher, int* dist_cache, size_t* last_insert_len, + Command* commands, size_t* num_commands, size_t* num_literals); typedef struct ZopfliNode { - /* Best length to get up to this byte (not including this byte itself) - highest 7 bit is used to reconstruct the length code. */ + /* Best length to get up to this byte (not including this byte itself) + highest 7 bit is used to reconstruct the length code. */ uint32_t length; - /* Distance associated with the length. */ + /* Distance associated with the length. */ uint32_t distance; - /* Number of literal inserts before this copy; highest 5 bits contain - distance short code + 1 (or zero if no short code). */ - uint32_t dcode_insert_length; + /* Number of literal inserts before this copy; highest 5 bits contain + distance short code + 1 (or zero if no short code). */ + uint32_t dcode_insert_length; /* This union holds information used by dynamic-programming. During forward pass |cost| it used to store the goal function. When node is processed its @@ -75,13 +75,13 @@ BROTLI_INTERNAL void BrotliInitZopfliNodes(ZopfliNode* array, size_t length); (2) nodes[i].command_length() <= i and (3) nodes[i - nodes[i].command_length()].cost < kInfinity */ BROTLI_INTERNAL size_t BrotliZopfliComputeShortestPath( - MemoryManager* m, size_t num_bytes, + MemoryManager* m, size_t num_bytes, size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask, - const BrotliEncoderParams* params, + const BrotliEncoderParams* params, const int* dist_cache, HasherHandle hasher, ZopfliNode* nodes); BROTLI_INTERNAL void BrotliZopfliCreateCommands( - const size_t num_bytes, const size_t block_start, const ZopfliNode* nodes, + const size_t num_bytes, const size_t block_start, const ZopfliNode* nodes, int* dist_cache, size_t* last_insert_len, const BrotliEncoderParams* params, Command* commands, size_t* num_literals); diff --git a/contrib/libs/brotli/enc/backward_references_inc.h b/contrib/libs/brotli/enc/backward_references_inc.h index c18cdb00cb..c48066a121 100644 --- a/contrib/libs/brotli/enc/backward_references_inc.h +++ b/contrib/libs/brotli/enc/backward_references_inc.h @@ -8,11 +8,11 @@ /* template parameters: EXPORT_FN, FN */ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)( - size_t num_bytes, size_t position, + size_t num_bytes, size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask, - const BrotliEncoderParams* params, - HasherHandle hasher, int* dist_cache, size_t* last_insert_len, - Command* commands, size_t* num_commands, size_t* num_literals) { + const BrotliEncoderParams* params, + HasherHandle hasher, int* dist_cache, size_t* last_insert_len, + Command* commands, size_t* num_commands, size_t* num_literals) { /* Set maximum distance, see section 9.1. of the spec. */ const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin); @@ -41,9 +41,9 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)( sr.len_code_delta = 0; sr.distance = 0; sr.score = kMinScore; - FN(FindLongestMatch)(hasher, ¶ms->dictionary, - ringbuffer, ringbuffer_mask, dist_cache, position, max_length, - max_distance, gap, params->dist.max_distance, &sr); + FN(FindLongestMatch)(hasher, ¶ms->dictionary, + ringbuffer, ringbuffer_mask, dist_cache, position, max_length, + max_distance, gap, params->dist.max_distance, &sr); if (sr.score > kMinScore) { /* Found a match. Let's look for something even better ahead. */ int delayed_backward_references_in_row = 0; @@ -57,10 +57,10 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)( sr2.distance = 0; sr2.score = kMinScore; max_distance = BROTLI_MIN(size_t, position + 1, max_backward_limit); - FN(FindLongestMatch)(hasher, - ¶ms->dictionary, - ringbuffer, ringbuffer_mask, dist_cache, position + 1, max_length, - max_distance, gap, params->dist.max_distance, &sr2); + FN(FindLongestMatch)(hasher, + ¶ms->dictionary, + ringbuffer, ringbuffer_mask, dist_cache, position + 1, max_length, + max_distance, gap, params->dist.max_distance, &sr2); if (sr2.score >= sr.score + cost_diff_lazy) { /* Ok, let's just write one byte for now and start a match from the next byte. */ @@ -80,8 +80,8 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)( { /* The first 16 codes are special short-codes, and the minimum offset is 1. */ - size_t distance_code = ComputeDistanceCode( - sr.distance, max_distance + gap, dist_cache); + size_t distance_code = ComputeDistanceCode( + sr.distance, max_distance + gap, dist_cache); if ((sr.distance <= (max_distance + gap)) && distance_code > 0) { dist_cache[3] = dist_cache[2]; dist_cache[2] = dist_cache[1]; @@ -89,8 +89,8 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)( dist_cache[0] = (int)sr.distance; FN(PrepareDistanceCache)(hasher, dist_cache); } - InitCommand(commands++, ¶ms->dist, insert_length, - sr.len, sr.len_code_delta, distance_code); + InitCommand(commands++, ¶ms->dist, insert_length, + sr.len, sr.len_code_delta, distance_code); } *num_literals += insert_length; insert_length = 0; diff --git a/contrib/libs/brotli/enc/bit_cost.c b/contrib/libs/brotli/enc/bit_cost.c index 1f3f7ad5c9..151cab8c4d 100644 --- a/contrib/libs/brotli/enc/bit_cost.c +++ b/contrib/libs/brotli/enc/bit_cost.c @@ -9,7 +9,7 @@ #include "./bit_cost.h" #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./fast_log.h" #include "./histogram.h" diff --git a/contrib/libs/brotli/enc/bit_cost.h b/contrib/libs/brotli/enc/bit_cost.h index 6586469e62..e468a9eaba 100644 --- a/contrib/libs/brotli/enc/bit_cost.h +++ b/contrib/libs/brotli/enc/bit_cost.h @@ -9,7 +9,7 @@ #ifndef BROTLI_ENC_BIT_COST_H_ #define BROTLI_ENC_BIT_COST_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./fast_log.h" #include "./histogram.h" @@ -18,11 +18,11 @@ extern "C" { #endif -static BROTLI_INLINE double ShannonEntropy( - const uint32_t* population, size_t size, size_t* total) { +static BROTLI_INLINE double ShannonEntropy( + const uint32_t* population, size_t size, size_t* total) { size_t sum = 0; double retval = 0; - const uint32_t* population_end = population + size; + const uint32_t* population_end = population + size; size_t p; if (size & 1) { goto odd_number_of_elements_left; @@ -42,7 +42,7 @@ static BROTLI_INLINE double ShannonEntropy( } static BROTLI_INLINE double BitsEntropy( - const uint32_t* population, size_t size) { + const uint32_t* population, size_t size) { size_t sum; double retval = ShannonEntropy(population, size, &sum); if (retval < sum) { diff --git a/contrib/libs/brotli/enc/block_encoder_inc.h b/contrib/libs/brotli/enc/block_encoder_inc.h index 8cbd5eac67..20742c8cce 100644 --- a/contrib/libs/brotli/enc/block_encoder_inc.h +++ b/contrib/libs/brotli/enc/block_encoder_inc.h @@ -13,9 +13,9 @@ stream. */ static void FN(BuildAndStoreEntropyCodes)(MemoryManager* m, BlockEncoder* self, const HistogramType* histograms, const size_t histograms_size, - const size_t alphabet_size, HuffmanTree* tree, - size_t* storage_ix, uint8_t* storage) { - const size_t table_size = histograms_size * self->histogram_length_; + const size_t alphabet_size, HuffmanTree* tree, + size_t* storage_ix, uint8_t* storage) { + const size_t table_size = histograms_size * self->histogram_length_; self->depths_ = BROTLI_ALLOC(m, uint8_t, table_size); self->bits_ = BROTLI_ALLOC(m, uint16_t, table_size); if (BROTLI_IS_OOM(m)) return; @@ -23,10 +23,10 @@ static void FN(BuildAndStoreEntropyCodes)(MemoryManager* m, BlockEncoder* self, { size_t i; for (i = 0; i < histograms_size; ++i) { - size_t ix = i * self->histogram_length_; - BuildAndStoreHuffmanTree(&histograms[i].data_[0], self->histogram_length_, - alphabet_size, tree, &self->depths_[ix], &self->bits_[ix], - storage_ix, storage); + size_t ix = i * self->histogram_length_; + BuildAndStoreHuffmanTree(&histograms[i].data_[0], self->histogram_length_, + alphabet_size, tree, &self->depths_[ix], &self->bits_[ix], + storage_ix, storage); } } } diff --git a/contrib/libs/brotli/enc/block_splitter.c b/contrib/libs/brotli/enc/block_splitter.c index d308eca59d..d20925d384 100644 --- a/contrib/libs/brotli/enc/block_splitter.c +++ b/contrib/libs/brotli/enc/block_splitter.c @@ -10,7 +10,7 @@ #include <string.h> /* memcpy, memset */ -#include "../common/platform.h" +#include "../common/platform.h" #include "./bit_cost.h" #include "./cluster.h" #include "./command.h" @@ -174,7 +174,7 @@ void BrotliSplitBlock(MemoryManager* m, for (i = 0; i < num_commands; ++i) { const Command* cmd = &cmds[i]; if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) { - distance_prefixes[j++] = cmd->dist_prefix_ & 0x3FF; + distance_prefixes[j++] = cmd->dist_prefix_ & 0x3FF; } } /* Create the block split on the array of distance prefixes. */ diff --git a/contrib/libs/brotli/enc/block_splitter.h b/contrib/libs/brotli/enc/block_splitter.h index a5e006c4b3..976283ecf5 100644 --- a/contrib/libs/brotli/enc/block_splitter.h +++ b/contrib/libs/brotli/enc/block_splitter.h @@ -9,7 +9,7 @@ #ifndef BROTLI_ENC_BLOCK_SPLITTER_H_ #define BROTLI_ENC_BLOCK_SPLITTER_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./command.h" #include "./memory.h" diff --git a/contrib/libs/brotli/enc/block_splitter_inc.h b/contrib/libs/brotli/enc/block_splitter_inc.h index 023712b84d..0e24013342 100644 --- a/contrib/libs/brotli/enc/block_splitter_inc.h +++ b/contrib/libs/brotli/enc/block_splitter_inc.h @@ -70,13 +70,13 @@ static size_t FN(FindBlocks)(const DataType* data, const size_t length, double* insert_cost, double* cost, uint8_t* switch_signal, - uint8_t* block_id) { + uint8_t* block_id) { const size_t data_size = FN(HistogramDataSize)(); const size_t bitmaplen = (num_histograms + 7) >> 3; size_t num_blocks = 1; size_t i; size_t j; - BROTLI_DCHECK(num_histograms <= 256); + BROTLI_DCHECK(num_histograms <= 256); if (num_histograms <= 1) { for (i = 0; i < length; ++i) { block_id[i] = 0; @@ -126,7 +126,7 @@ static size_t FN(FindBlocks)(const DataType* data, const size_t length, if (cost[k] >= block_switch_cost) { const uint8_t mask = (uint8_t)(1u << (k & 7)); cost[k] = block_switch_cost; - BROTLI_DCHECK((k >> 3) < bitmaplen); + BROTLI_DCHECK((k >> 3) < bitmaplen); switch_signal[ix + (k >> 3)] |= mask; } } @@ -137,7 +137,7 @@ static size_t FN(FindBlocks)(const DataType* data, const size_t length, uint8_t cur_id = block_id[byte_ix]; while (byte_ix > 0) { const uint8_t mask = (uint8_t)(1u << (cur_id & 7)); - BROTLI_DCHECK(((size_t)cur_id >> 3) < bitmaplen); + BROTLI_DCHECK(((size_t)cur_id >> 3) < bitmaplen); --byte_ix; ix -= bitmaplen; if (switch_signal[ix + (cur_id >> 3)] & mask) { @@ -161,16 +161,16 @@ static size_t FN(RemapBlockIds)(uint8_t* block_ids, const size_t length, new_id[i] = kInvalidId; } for (i = 0; i < length; ++i) { - BROTLI_DCHECK(block_ids[i] < num_histograms); + BROTLI_DCHECK(block_ids[i] < num_histograms); if (new_id[block_ids[i]] == kInvalidId) { new_id[block_ids[i]] = next_id++; } } for (i = 0; i < length; ++i) { block_ids[i] = (uint8_t)new_id[block_ids[i]]; - BROTLI_DCHECK(block_ids[i] < num_histograms); + BROTLI_DCHECK(block_ids[i] < num_histograms); } - BROTLI_DCHECK(next_id <= num_histograms); + BROTLI_DCHECK(next_id <= num_histograms); return next_id; } @@ -226,13 +226,13 @@ static void FN(ClusterBlocks)(MemoryManager* m, { size_t block_idx = 0; for (i = 0; i < length; ++i) { - BROTLI_DCHECK(block_idx < num_blocks); + BROTLI_DCHECK(block_idx < num_blocks); ++block_lengths[block_idx]; if (i + 1 == length || block_ids[i] != block_ids[i + 1]) { ++block_idx; } } - BROTLI_DCHECK(block_idx == num_blocks); + BROTLI_DCHECK(block_idx == num_blocks); } for (i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCH) { @@ -268,8 +268,8 @@ static void FN(ClusterBlocks)(MemoryManager* m, histogram_symbols[i + j] = (uint32_t)num_clusters + remap[symbols[j]]; } num_clusters += num_new_clusters; - BROTLI_DCHECK(num_clusters == cluster_size_size); - BROTLI_DCHECK(num_clusters == all_histograms_size); + BROTLI_DCHECK(num_clusters == cluster_size_size); + BROTLI_DCHECK(num_clusters == all_histograms_size); } BROTLI_FREE(m, histograms); diff --git a/contrib/libs/brotli/enc/brotli_bit_stream.c b/contrib/libs/brotli/enc/brotli_bit_stream.c index aaf2dad7db..9298666943 100644 --- a/contrib/libs/brotli/enc/brotli_bit_stream.c +++ b/contrib/libs/brotli/enc/brotli_bit_stream.c @@ -13,13 +13,13 @@ #include <string.h> /* memcpy, memset */ #include "../common/constants.h" -#include "../common/context.h" -#include "../common/platform.h" +#include "../common/context.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./entropy_encode.h" #include "./entropy_encode_static.h" #include "./fast_log.h" -#include "./histogram.h" +#include "./histogram.h" #include "./memory.h" #include "./write_bits.h" @@ -28,11 +28,11 @@ extern "C" { #endif #define MAX_HUFFMAN_TREE_SIZE (2 * BROTLI_NUM_COMMAND_SYMBOLS + 1) -/* The maximum size of Huffman dictionary for distances assuming that - NPOSTFIX = 0 and NDIRECT = 0. */ -#define MAX_SIMPLE_DISTANCE_ALPHABET_SIZE \ - BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_LARGE_MAX_DISTANCE_BITS) -/* MAX_SIMPLE_DISTANCE_ALPHABET_SIZE == 140 */ +/* The maximum size of Huffman dictionary for distances assuming that + NPOSTFIX = 0 and NDIRECT = 0. */ +#define MAX_SIMPLE_DISTANCE_ALPHABET_SIZE \ + BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_LARGE_MAX_DISTANCE_BITS) +/* MAX_SIMPLE_DISTANCE_ALPHABET_SIZE == 140 */ /* Represents the range of values belonging to a prefix code: [offset, offset + 2^nbits) */ @@ -89,9 +89,9 @@ static void BrotliEncodeMlen(size_t length, uint64_t* bits, size_t* numbits, uint64_t* nibblesbits) { size_t lg = (length == 1) ? 1 : Log2FloorNonZero((uint32_t)(length - 1)) + 1; size_t mnibbles = (lg < 16 ? 16 : (lg + 3)) / 4; - BROTLI_DCHECK(length > 0); - BROTLI_DCHECK(length <= (1 << 24)); - BROTLI_DCHECK(lg <= 24); + BROTLI_DCHECK(length > 0); + BROTLI_DCHECK(length <= (1 << 24)); + BROTLI_DCHECK(lg <= 24); *nibblesbits = mnibbles - 4; *numbits = mnibbles * 4; *bits = length - 1; @@ -258,7 +258,7 @@ static void StoreSimpleHuffmanTree(const uint8_t* depths, size_t symbols[4], size_t num_symbols, size_t max_bits, - size_t* storage_ix, uint8_t* storage) { + size_t* storage_ix, uint8_t* storage) { /* value of 1 indicates a simple Huffman code */ BrotliWriteBits(2, 1, storage_ix, storage); BrotliWriteBits(2, num_symbols - 1, storage_ix, storage); /* NSYM - 1 */ @@ -297,7 +297,7 @@ static void StoreSimpleHuffmanTree(const uint8_t* depths, depths = symbol depths */ void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num, HuffmanTree* tree, - size_t* storage_ix, uint8_t* storage) { + size_t* storage_ix, uint8_t* storage) { /* Write the Huffman tree into the brotli-representation. The command alphabet is the largest, so this allocation will fit all alphabets. */ @@ -311,7 +311,7 @@ void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num, int num_codes = 0; size_t code = 0; - BROTLI_DCHECK(num <= BROTLI_NUM_COMMAND_SYMBOLS); + BROTLI_DCHECK(num <= BROTLI_NUM_COMMAND_SYMBOLS); BrotliWriteHuffmanTree(depths, num, &huffman_tree_size, huffman_tree, huffman_tree_extra_bits); @@ -360,9 +360,9 @@ void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num, /* Builds a Huffman tree from histogram[0:length] into depth[0:length] and bits[0:length] and stores the encoded tree to the bit stream. */ -static void BuildAndStoreHuffmanTree(const uint32_t* histogram, - const size_t histogram_length, - const size_t alphabet_size, +static void BuildAndStoreHuffmanTree(const uint32_t* histogram, + const size_t histogram_length, + const size_t alphabet_size, HuffmanTree* tree, uint8_t* depth, uint16_t* bits, @@ -372,7 +372,7 @@ static void BuildAndStoreHuffmanTree(const uint32_t* histogram, size_t s4[4] = { 0 }; size_t i; size_t max_bits = 0; - for (i = 0; i < histogram_length; i++) { + for (i = 0; i < histogram_length; i++) { if (histogram[i]) { if (count < 4) { s4[count] = i; @@ -384,7 +384,7 @@ static void BuildAndStoreHuffmanTree(const uint32_t* histogram, } { - size_t max_bits_counter = alphabet_size - 1; + size_t max_bits_counter = alphabet_size - 1; while (max_bits_counter) { max_bits_counter >>= 1; ++max_bits; @@ -399,14 +399,14 @@ static void BuildAndStoreHuffmanTree(const uint32_t* histogram, return; } - memset(depth, 0, histogram_length * sizeof(depth[0])); - BrotliCreateHuffmanTree(histogram, histogram_length, 15, tree, depth); - BrotliConvertBitDepthsToSymbols(depth, histogram_length, bits); + memset(depth, 0, histogram_length * sizeof(depth[0])); + BrotliCreateHuffmanTree(histogram, histogram_length, 15, tree, depth); + BrotliConvertBitDepthsToSymbols(depth, histogram_length, bits); if (count <= 4) { StoreSimpleHuffmanTree(depth, s4, count, max_bits, storage_ix, storage); } else { - BrotliStoreHuffmanTree(depth, histogram_length, tree, storage_ix, storage); + BrotliStoreHuffmanTree(depth, histogram_length, tree, storage_ix, storage); } } @@ -620,7 +620,7 @@ static void MoveToFrontTransform(const uint32_t* BROTLI_RESTRICT v_in, for (i = 1; i < v_size; ++i) { if (v_in[i] > max_value) max_value = v_in[i]; } - BROTLI_DCHECK(max_value < 256u); + BROTLI_DCHECK(max_value < 256u); for (i = 0; i <= max_value; ++i) { mtf[i] = (uint8_t)i; } @@ -628,7 +628,7 @@ static void MoveToFrontTransform(const uint32_t* BROTLI_RESTRICT v_in, size_t mtf_size = max_value + 1; for (i = 0; i < v_size; ++i) { size_t index = IndexOf(mtf, mtf_size, (uint8_t)v_in[i]); - BROTLI_DCHECK(index < mtf_size); + BROTLI_DCHECK(index < mtf_size); v_out[i] = (uint32_t)index; MoveToFront(mtf, index); } @@ -660,7 +660,7 @@ static void RunLengthCodeZeros(const size_t in_size, *max_run_length_prefix = max_prefix; *out_size = 0; for (i = 0; i < in_size;) { - BROTLI_DCHECK(*out_size <= i); + BROTLI_DCHECK(*out_size <= i); if (v[i] != 0) { v[*out_size] = v[i] + *max_run_length_prefix; ++i; @@ -730,7 +730,7 @@ static void EncodeContextMap(MemoryManager* m, } } BuildAndStoreHuffmanTree(histogram, num_clusters + max_run_length_prefix, - num_clusters + max_run_length_prefix, + num_clusters + max_run_length_prefix, tree, depths, bits, storage_ix, storage); for (i = 0; i < num_rle_symbols; ++i) { const uint32_t rle_symbol = rle_symbols[i] & kSymbolMask; @@ -790,11 +790,11 @@ static void BuildAndStoreBlockSplitCode(const uint8_t* types, } StoreVarLenUint8(num_types - 1, storage_ix, storage); if (num_types > 1) { /* TODO: else? could StoreBlockSwitch occur? */ - BuildAndStoreHuffmanTree(&type_histo[0], num_types + 2, num_types + 2, tree, + BuildAndStoreHuffmanTree(&type_histo[0], num_types + 2, num_types + 2, tree, &code->type_depths[0], &code->type_bits[0], storage_ix, storage); BuildAndStoreHuffmanTree(&length_histo[0], BROTLI_NUM_BLOCK_LEN_SYMBOLS, - BROTLI_NUM_BLOCK_LEN_SYMBOLS, + BROTLI_NUM_BLOCK_LEN_SYMBOLS, tree, &code->length_depths[0], &code->length_bits[0], storage_ix, storage); StoreBlockSwitch(code, lengths[0], types[0], 1, storage_ix, storage); @@ -825,8 +825,8 @@ static void StoreTrivialContextMap(size_t num_types, for (i = context_bits; i < alphabet_size; ++i) { histogram[i] = 1; } - BuildAndStoreHuffmanTree(histogram, alphabet_size, alphabet_size, - tree, depths, bits, storage_ix, storage); + BuildAndStoreHuffmanTree(histogram, alphabet_size, alphabet_size, + tree, depths, bits, storage_ix, storage); for (i = 0; i < num_types; ++i) { size_t code = (i == 0 ? 0 : i + context_bits - 1); BrotliWriteBits(depths[code], bits[code], storage_ix, storage); @@ -841,7 +841,7 @@ static void StoreTrivialContextMap(size_t num_types, /* Manages the encoding of one block category (literal, command or distance). */ typedef struct BlockEncoder { - size_t histogram_length_; + size_t histogram_length_; size_t num_block_types_; const uint8_t* block_types_; /* Not owned. */ const uint32_t* block_lengths_; /* Not owned. */ @@ -854,10 +854,10 @@ typedef struct BlockEncoder { uint16_t* bits_; } BlockEncoder; -static void InitBlockEncoder(BlockEncoder* self, size_t histogram_length, +static void InitBlockEncoder(BlockEncoder* self, size_t histogram_length, size_t num_block_types, const uint8_t* block_types, const uint32_t* block_lengths, const size_t num_blocks) { - self->histogram_length_ = histogram_length; + self->histogram_length_ = histogram_length; self->num_block_types_ = num_block_types; self->block_types_ = block_types; self->block_lengths_ = block_lengths; @@ -893,7 +893,7 @@ static void StoreSymbol(BlockEncoder* self, size_t symbol, size_t* storage_ix, uint32_t block_len = self->block_lengths_[block_ix]; uint8_t block_type = self->block_types_[block_ix]; self->block_len_ = block_len; - self->entropy_ix_ = block_type * self->histogram_length_; + self->entropy_ix_ = block_type * self->histogram_length_; StoreBlockSwitch(&self->block_split_code_, block_len, block_type, 0, storage_ix, storage); } @@ -922,7 +922,7 @@ static void StoreSymbolWithContext(BlockEncoder* self, size_t symbol, --self->block_len_; { size_t histo_ix = context_map[self->entropy_ix_ + context]; - size_t ix = histo_ix * self->histogram_length_ + symbol; + size_t ix = histo_ix * self->histogram_length_ + symbol; BrotliWriteBits(self->depths_[ix], self->bits_[ix], storage_ix, storage); } } @@ -948,38 +948,38 @@ static void JumpToByteBoundary(size_t* storage_ix, uint8_t* storage) { } void BrotliStoreMetaBlock(MemoryManager* m, - const uint8_t* input, size_t start_pos, size_t length, size_t mask, - uint8_t prev_byte, uint8_t prev_byte2, BROTLI_BOOL is_last, - const BrotliEncoderParams* params, ContextType literal_context_mode, - const Command* commands, size_t n_commands, const MetaBlockSplit* mb, - size_t* storage_ix, uint8_t* storage) { - + const uint8_t* input, size_t start_pos, size_t length, size_t mask, + uint8_t prev_byte, uint8_t prev_byte2, BROTLI_BOOL is_last, + const BrotliEncoderParams* params, ContextType literal_context_mode, + const Command* commands, size_t n_commands, const MetaBlockSplit* mb, + size_t* storage_ix, uint8_t* storage) { + size_t pos = start_pos; size_t i; - uint32_t num_distance_symbols = params->dist.alphabet_size; - uint32_t num_effective_distance_symbols = num_distance_symbols; + uint32_t num_distance_symbols = params->dist.alphabet_size; + uint32_t num_effective_distance_symbols = num_distance_symbols; HuffmanTree* tree; - ContextLut literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode); + ContextLut literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode); BlockEncoder literal_enc; BlockEncoder command_enc; BlockEncoder distance_enc; - const BrotliDistanceParams* dist = ¶ms->dist; - if (params->large_window && - num_effective_distance_symbols > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS) { - num_effective_distance_symbols = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS; - } + const BrotliDistanceParams* dist = ¶ms->dist; + if (params->large_window && + num_effective_distance_symbols > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS) { + num_effective_distance_symbols = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS; + } StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage); tree = BROTLI_ALLOC(m, HuffmanTree, MAX_HUFFMAN_TREE_SIZE); if (BROTLI_IS_OOM(m)) return; - InitBlockEncoder(&literal_enc, BROTLI_NUM_LITERAL_SYMBOLS, - mb->literal_split.num_types, mb->literal_split.types, - mb->literal_split.lengths, mb->literal_split.num_blocks); + InitBlockEncoder(&literal_enc, BROTLI_NUM_LITERAL_SYMBOLS, + mb->literal_split.num_types, mb->literal_split.types, + mb->literal_split.lengths, mb->literal_split.num_blocks); InitBlockEncoder(&command_enc, BROTLI_NUM_COMMAND_SYMBOLS, mb->command_split.num_types, mb->command_split.types, mb->command_split.lengths, mb->command_split.num_blocks); - InitBlockEncoder(&distance_enc, num_effective_distance_symbols, + InitBlockEncoder(&distance_enc, num_effective_distance_symbols, mb->distance_split.num_types, mb->distance_split.types, mb->distance_split.lengths, mb->distance_split.num_blocks); @@ -988,10 +988,10 @@ void BrotliStoreMetaBlock(MemoryManager* m, BuildAndStoreBlockSwitchEntropyCodes( &distance_enc, tree, storage_ix, storage); - BrotliWriteBits(2, dist->distance_postfix_bits, storage_ix, storage); - BrotliWriteBits( - 4, dist->num_direct_distance_codes >> dist->distance_postfix_bits, - storage_ix, storage); + BrotliWriteBits(2, dist->distance_postfix_bits, storage_ix, storage); + BrotliWriteBits( + 4, dist->num_direct_distance_codes >> dist->distance_postfix_bits, + storage_ix, storage); for (i = 0; i < mb->literal_split.num_types; ++i) { BrotliWriteBits(2, literal_context_mode, storage_ix, storage); } @@ -1017,16 +1017,16 @@ void BrotliStoreMetaBlock(MemoryManager* m, } BuildAndStoreEntropyCodesLiteral(m, &literal_enc, mb->literal_histograms, - mb->literal_histograms_size, BROTLI_NUM_LITERAL_SYMBOLS, tree, - storage_ix, storage); + mb->literal_histograms_size, BROTLI_NUM_LITERAL_SYMBOLS, tree, + storage_ix, storage); if (BROTLI_IS_OOM(m)) return; BuildAndStoreEntropyCodesCommand(m, &command_enc, mb->command_histograms, - mb->command_histograms_size, BROTLI_NUM_COMMAND_SYMBOLS, tree, - storage_ix, storage); + mb->command_histograms_size, BROTLI_NUM_COMMAND_SYMBOLS, tree, + storage_ix, storage); if (BROTLI_IS_OOM(m)) return; BuildAndStoreEntropyCodesDistance(m, &distance_enc, mb->distance_histograms, - mb->distance_histograms_size, num_distance_symbols, tree, - storage_ix, storage); + mb->distance_histograms_size, num_distance_symbols, tree, + storage_ix, storage); if (BROTLI_IS_OOM(m)) return; BROTLI_FREE(m, tree); @@ -1044,8 +1044,8 @@ void BrotliStoreMetaBlock(MemoryManager* m, } else { size_t j; for (j = cmd.insert_len_; j != 0; --j) { - size_t context = - BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut); + size_t context = + BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut); uint8_t literal = input[pos & mask]; StoreSymbolWithContext(&literal_enc, literal, context, mb->literal_context_map, storage_ix, storage, @@ -1060,9 +1060,9 @@ void BrotliStoreMetaBlock(MemoryManager* m, prev_byte2 = input[(pos - 2) & mask]; prev_byte = input[(pos - 1) & mask]; if (cmd.cmd_prefix_ >= 128) { - size_t dist_code = cmd.dist_prefix_ & 0x3FF; - uint32_t distnumextra = cmd.dist_prefix_ >> 10; - uint64_t distextra = cmd.dist_extra_; + size_t dist_code = cmd.dist_prefix_ & 0x3FF; + uint32_t distnumextra = cmd.dist_prefix_ >> 10; + uint64_t distextra = cmd.dist_extra_; if (mb->distance_context_map_size == 0) { StoreSymbol(&distance_enc, dist_code, storage_ix, storage); } else { @@ -1086,7 +1086,7 @@ void BrotliStoreMetaBlock(MemoryManager* m, static void BuildHistograms(const uint8_t* input, size_t start_pos, size_t mask, - const Command* commands, + const Command* commands, size_t n_commands, HistogramLiteral* lit_histo, HistogramCommand* cmd_histo, @@ -1103,7 +1103,7 @@ static void BuildHistograms(const uint8_t* input, } pos += CommandCopyLen(&cmd); if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) { - HistogramAddDistance(dist_histo, cmd.dist_prefix_ & 0x3FF); + HistogramAddDistance(dist_histo, cmd.dist_prefix_ & 0x3FF); } } } @@ -1111,7 +1111,7 @@ static void BuildHistograms(const uint8_t* input, static void StoreDataWithHuffmanCodes(const uint8_t* input, size_t start_pos, size_t mask, - const Command* commands, + const Command* commands, size_t n_commands, const uint8_t* lit_depth, const uint16_t* lit_bits, @@ -1138,9 +1138,9 @@ static void StoreDataWithHuffmanCodes(const uint8_t* input, } pos += CommandCopyLen(&cmd); if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) { - const size_t dist_code = cmd.dist_prefix_ & 0x3FF; - const uint32_t distnumextra = cmd.dist_prefix_ >> 10; - const uint32_t distextra = cmd.dist_extra_; + const size_t dist_code = cmd.dist_prefix_ & 0x3FF; + const uint32_t distnumextra = cmd.dist_prefix_ >> 10; + const uint32_t distextra = cmd.dist_extra_; BrotliWriteBits(dist_depth[dist_code], dist_bits[dist_code], storage_ix, storage); BrotliWriteBits(distnumextra, distextra, storage_ix, storage); @@ -1149,10 +1149,10 @@ static void StoreDataWithHuffmanCodes(const uint8_t* input, } void BrotliStoreMetaBlockTrivial(MemoryManager* m, - const uint8_t* input, size_t start_pos, size_t length, size_t mask, - BROTLI_BOOL is_last, const BrotliEncoderParams* params, - const Command* commands, size_t n_commands, - size_t* storage_ix, uint8_t* storage) { + const uint8_t* input, size_t start_pos, size_t length, size_t mask, + BROTLI_BOOL is_last, const BrotliEncoderParams* params, + const Command* commands, size_t n_commands, + size_t* storage_ix, uint8_t* storage) { HistogramLiteral lit_histo; HistogramCommand cmd_histo; HistogramDistance dist_histo; @@ -1160,10 +1160,10 @@ void BrotliStoreMetaBlockTrivial(MemoryManager* m, uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS]; uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS]; uint16_t cmd_bits[BROTLI_NUM_COMMAND_SYMBOLS]; - uint8_t dist_depth[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; - uint16_t dist_bits[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; + uint8_t dist_depth[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; + uint16_t dist_bits[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; HuffmanTree* tree; - uint32_t num_distance_symbols = params->dist.alphabet_size; + uint32_t num_distance_symbols = params->dist.alphabet_size; StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage); @@ -1178,16 +1178,16 @@ void BrotliStoreMetaBlockTrivial(MemoryManager* m, tree = BROTLI_ALLOC(m, HuffmanTree, MAX_HUFFMAN_TREE_SIZE); if (BROTLI_IS_OOM(m)) return; - BuildAndStoreHuffmanTree(lit_histo.data_, BROTLI_NUM_LITERAL_SYMBOLS, - BROTLI_NUM_LITERAL_SYMBOLS, tree, + BuildAndStoreHuffmanTree(lit_histo.data_, BROTLI_NUM_LITERAL_SYMBOLS, + BROTLI_NUM_LITERAL_SYMBOLS, tree, lit_depth, lit_bits, storage_ix, storage); - BuildAndStoreHuffmanTree(cmd_histo.data_, BROTLI_NUM_COMMAND_SYMBOLS, - BROTLI_NUM_COMMAND_SYMBOLS, tree, + BuildAndStoreHuffmanTree(cmd_histo.data_, BROTLI_NUM_COMMAND_SYMBOLS, + BROTLI_NUM_COMMAND_SYMBOLS, tree, cmd_depth, cmd_bits, storage_ix, storage); - BuildAndStoreHuffmanTree(dist_histo.data_, MAX_SIMPLE_DISTANCE_ALPHABET_SIZE, - num_distance_symbols, tree, + BuildAndStoreHuffmanTree(dist_histo.data_, MAX_SIMPLE_DISTANCE_ALPHABET_SIZE, + num_distance_symbols, tree, dist_depth, dist_bits, storage_ix, storage); BROTLI_FREE(m, tree); @@ -1202,14 +1202,14 @@ void BrotliStoreMetaBlockTrivial(MemoryManager* m, } void BrotliStoreMetaBlockFast(MemoryManager* m, - const uint8_t* input, size_t start_pos, size_t length, size_t mask, - BROTLI_BOOL is_last, const BrotliEncoderParams* params, - const Command* commands, size_t n_commands, - size_t* storage_ix, uint8_t* storage) { - uint32_t num_distance_symbols = params->dist.alphabet_size; - uint32_t distance_alphabet_bits = - Log2FloorNonZero(num_distance_symbols - 1) + 1; - + const uint8_t* input, size_t start_pos, size_t length, size_t mask, + BROTLI_BOOL is_last, const BrotliEncoderParams* params, + const Command* commands, size_t n_commands, + size_t* storage_ix, uint8_t* storage) { + uint32_t num_distance_symbols = params->dist.alphabet_size; + uint32_t distance_alphabet_bits = + Log2FloorNonZero(num_distance_symbols - 1) + 1; + StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage); BrotliWriteBits(13, 0, storage_ix, storage); @@ -1253,8 +1253,8 @@ void BrotliStoreMetaBlockFast(MemoryManager* m, uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS]; uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS]; uint16_t cmd_bits[BROTLI_NUM_COMMAND_SYMBOLS]; - uint8_t dist_depth[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; - uint16_t dist_bits[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; + uint8_t dist_depth[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; + uint16_t dist_bits[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]; HistogramClearLiteral(&lit_histo); HistogramClearCommand(&cmd_histo); HistogramClearDistance(&dist_histo); @@ -1275,7 +1275,7 @@ void BrotliStoreMetaBlockFast(MemoryManager* m, BrotliBuildAndStoreHuffmanTreeFast(m, dist_histo.data_, dist_histo.total_count_, /* max_bits = */ - distance_alphabet_bits, + distance_alphabet_bits, dist_depth, dist_bits, storage_ix, storage); if (BROTLI_IS_OOM(m)) return; @@ -1294,11 +1294,11 @@ void BrotliStoreMetaBlockFast(MemoryManager* m, /* This is for storing uncompressed blocks (simple raw storage of bytes-as-bytes). */ void BrotliStoreUncompressedMetaBlock(BROTLI_BOOL is_final_block, - const uint8_t* BROTLI_RESTRICT input, + const uint8_t* BROTLI_RESTRICT input, size_t position, size_t mask, size_t len, - size_t* BROTLI_RESTRICT storage_ix, - uint8_t* BROTLI_RESTRICT storage) { + size_t* BROTLI_RESTRICT storage_ix, + uint8_t* BROTLI_RESTRICT storage) { size_t masked_pos = position & mask; BrotliStoreUncompressedMetaBlockHeader(len, storage_ix, storage); JumpToByteBoundary(storage_ix, storage); diff --git a/contrib/libs/brotli/enc/brotli_bit_stream.h b/contrib/libs/brotli/enc/brotli_bit_stream.h index 2ed703bf79..c86df697ac 100644 --- a/contrib/libs/brotli/enc/brotli_bit_stream.h +++ b/contrib/libs/brotli/enc/brotli_bit_stream.h @@ -16,8 +16,8 @@ #ifndef BROTLI_ENC_BROTLI_BIT_STREAM_H_ #define BROTLI_ENC_BROTLI_BIT_STREAM_H_ -#include "../common/context.h" -#include "../common/platform.h" +#include "../common/context.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./command.h" #include "./entropy_encode.h" @@ -32,7 +32,7 @@ extern "C" { position for the current storage. */ BROTLI_INTERNAL void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num, - HuffmanTree* tree, size_t* storage_ix, uint8_t* storage); + HuffmanTree* tree, size_t* storage_ix, uint8_t* storage); BROTLI_INTERNAL void BrotliBuildAndStoreHuffmanTreeFast( MemoryManager* m, const uint32_t* histogram, const size_t histogram_total, @@ -42,40 +42,40 @@ BROTLI_INTERNAL void BrotliBuildAndStoreHuffmanTreeFast( /* REQUIRES: length > 0 */ /* REQUIRES: length <= (1 << 24) */ BROTLI_INTERNAL void BrotliStoreMetaBlock(MemoryManager* m, - const uint8_t* input, size_t start_pos, size_t length, size_t mask, - uint8_t prev_byte, uint8_t prev_byte2, BROTLI_BOOL is_last, - const BrotliEncoderParams* params, ContextType literal_context_mode, - const Command* commands, size_t n_commands, const MetaBlockSplit* mb, - size_t* storage_ix, uint8_t* storage); + const uint8_t* input, size_t start_pos, size_t length, size_t mask, + uint8_t prev_byte, uint8_t prev_byte2, BROTLI_BOOL is_last, + const BrotliEncoderParams* params, ContextType literal_context_mode, + const Command* commands, size_t n_commands, const MetaBlockSplit* mb, + size_t* storage_ix, uint8_t* storage); /* Stores the meta-block without doing any block splitting, just collects one histogram per block category and uses that for entropy coding. REQUIRES: length > 0 REQUIRES: length <= (1 << 24) */ BROTLI_INTERNAL void BrotliStoreMetaBlockTrivial(MemoryManager* m, - const uint8_t* input, size_t start_pos, size_t length, size_t mask, - BROTLI_BOOL is_last, const BrotliEncoderParams* params, - const Command* commands, size_t n_commands, - size_t* storage_ix, uint8_t* storage); + const uint8_t* input, size_t start_pos, size_t length, size_t mask, + BROTLI_BOOL is_last, const BrotliEncoderParams* params, + const Command* commands, size_t n_commands, + size_t* storage_ix, uint8_t* storage); /* Same as above, but uses static prefix codes for histograms with a only a few symbols, and uses static code length prefix codes for all other histograms. REQUIRES: length > 0 REQUIRES: length <= (1 << 24) */ BROTLI_INTERNAL void BrotliStoreMetaBlockFast(MemoryManager* m, - const uint8_t* input, size_t start_pos, size_t length, size_t mask, - BROTLI_BOOL is_last, const BrotliEncoderParams* params, - const Command* commands, size_t n_commands, - size_t* storage_ix, uint8_t* storage); + const uint8_t* input, size_t start_pos, size_t length, size_t mask, + BROTLI_BOOL is_last, const BrotliEncoderParams* params, + const Command* commands, size_t n_commands, + size_t* storage_ix, uint8_t* storage); /* This is for storing uncompressed blocks (simple raw storage of bytes-as-bytes). REQUIRES: length > 0 REQUIRES: length <= (1 << 24) */ BROTLI_INTERNAL void BrotliStoreUncompressedMetaBlock( - BROTLI_BOOL is_final_block, const uint8_t* BROTLI_RESTRICT input, - size_t position, size_t mask, size_t len, - size_t* BROTLI_RESTRICT storage_ix, uint8_t* BROTLI_RESTRICT storage); + BROTLI_BOOL is_final_block, const uint8_t* BROTLI_RESTRICT input, + size_t position, size_t mask, size_t len, + size_t* BROTLI_RESTRICT storage_ix, uint8_t* BROTLI_RESTRICT storage); #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ diff --git a/contrib/libs/brotli/enc/cluster.c b/contrib/libs/brotli/enc/cluster.c index a20dfd385f..bb60ab410c 100644 --- a/contrib/libs/brotli/enc/cluster.c +++ b/contrib/libs/brotli/enc/cluster.c @@ -8,7 +8,7 @@ #include "./cluster.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./bit_cost.h" /* BrotliPopulationCost */ #include "./fast_log.h" diff --git a/contrib/libs/brotli/enc/cluster.h b/contrib/libs/brotli/enc/cluster.h index bb26124d24..93f80a9803 100644 --- a/contrib/libs/brotli/enc/cluster.h +++ b/contrib/libs/brotli/enc/cluster.h @@ -9,7 +9,7 @@ #ifndef BROTLI_ENC_CLUSTER_H_ #define BROTLI_ENC_CLUSTER_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./histogram.h" #include "./memory.h" diff --git a/contrib/libs/brotli/enc/command.h b/contrib/libs/brotli/enc/command.h index 1aac85689b..d10356a654 100644 --- a/contrib/libs/brotli/enc/command.h +++ b/contrib/libs/brotli/enc/command.h @@ -10,10 +10,10 @@ #define BROTLI_ENC_COMMAND_H_ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./fast_log.h" -#include "./params.h" +#include "./params.h" #include "./prefix.h" #if defined(__cplusplus) || defined(c_plusplus) @@ -62,21 +62,21 @@ static BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) { static BROTLI_INLINE uint16_t CombineLengthCodes( uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) { uint16_t bits64 = - (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3u)); - if (use_last_distance && inscode < 8u && copycode < 16u) { - return (copycode < 8u) ? bits64 : (bits64 | 64u); + (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3u)); + if (use_last_distance && inscode < 8u && copycode < 16u) { + return (copycode < 8u) ? bits64 : (bits64 | 64u); } else { /* Specification: 5 Encoding of ... (last table) */ /* offset = 2 * index, where index is in range [0..8] */ - uint32_t offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u)); + uint32_t offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u)); /* All values in specification are K * 64, where K = [2, 3, 6, 4, 5, 8, 7, 9, 10], i + 1 = [1, 2, 3, 4, 5, 6, 7, 8, 9], K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1, 2] = D. All values in D require only 2 bits to encode. Magic constant is shifted 6 bits left, to avoid final multiplication. */ - offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u); - return (uint16_t)(offset | bits64); + offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u); + return (uint16_t)(offset | bits64); } } @@ -106,61 +106,61 @@ static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) { typedef struct Command { uint32_t insert_len_; - /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */ + /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */ uint32_t copy_len_; - /* Stores distance extra bits. */ + /* Stores distance extra bits. */ uint32_t dist_extra_; uint16_t cmd_prefix_; - /* Stores distance code in low 10 bits - and number of extra bits in high 6 bits. */ + /* Stores distance code in low 10 bits + and number of extra bits in high 6 bits. */ uint16_t dist_prefix_; } Command; /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */ -static BROTLI_INLINE void InitCommand(Command* self, - const BrotliDistanceParams* dist, size_t insertlen, +static BROTLI_INLINE void InitCommand(Command* self, + const BrotliDistanceParams* dist, size_t insertlen, size_t copylen, int copylen_code_delta, size_t distance_code) { /* Don't rely on signed int representation, use honest casts. */ uint32_t delta = (uint8_t)((int8_t)copylen_code_delta); self->insert_len_ = (uint32_t)insertlen; - self->copy_len_ = (uint32_t)(copylen | (delta << 25)); + self->copy_len_ = (uint32_t)(copylen | (delta << 25)); /* The distance prefix and extra bits are stored in this Command as if npostfix and ndirect were 0, they are only recomputed later after the clustering if needed. */ PrefixEncodeCopyDistance( - distance_code, dist->num_direct_distance_codes, - dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_); + distance_code, dist->num_direct_distance_codes, + dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_); GetLengthCode( insertlen, (size_t)((int)copylen + copylen_code_delta), - TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_); + TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_); } static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) { self->insert_len_ = (uint32_t)insertlen; - self->copy_len_ = 4 << 25; + self->copy_len_ = 4 << 25; self->dist_extra_ = 0; self->dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES; GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_); } -static BROTLI_INLINE uint32_t CommandRestoreDistanceCode( - const Command* self, const BrotliDistanceParams* dist) { - if ((self->dist_prefix_ & 0x3FFu) < - BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) { - return self->dist_prefix_ & 0x3FFu; +static BROTLI_INLINE uint32_t CommandRestoreDistanceCode( + const Command* self, const BrotliDistanceParams* dist) { + if ((self->dist_prefix_ & 0x3FFu) < + BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) { + return self->dist_prefix_ & 0x3FFu; } else { - uint32_t dcode = self->dist_prefix_ & 0x3FFu; - uint32_t nbits = self->dist_prefix_ >> 10; - uint32_t extra = self->dist_extra_; - uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U; - uint32_t hcode = (dcode - dist->num_direct_distance_codes - - BROTLI_NUM_DISTANCE_SHORT_CODES) >> - dist->distance_postfix_bits; - uint32_t lcode = (dcode - dist->num_direct_distance_codes - - BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask; - uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U; - return ((offset + extra) << dist->distance_postfix_bits) + lcode + - dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES; + uint32_t dcode = self->dist_prefix_ & 0x3FFu; + uint32_t nbits = self->dist_prefix_ >> 10; + uint32_t extra = self->dist_extra_; + uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U; + uint32_t hcode = (dcode - dist->num_direct_distance_codes - + BROTLI_NUM_DISTANCE_SHORT_CODES) >> + dist->distance_postfix_bits; + uint32_t lcode = (dcode - dist->num_direct_distance_codes - + BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask; + uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U; + return ((offset + extra) << dist->distance_postfix_bits) + lcode + + dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES; } } @@ -174,13 +174,13 @@ static BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) { } static BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) { - return self->copy_len_ & 0x1FFFFFF; + return self->copy_len_ & 0x1FFFFFF; } static BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) { - uint32_t modifier = self->copy_len_ >> 25; - int32_t delta = (int8_t)((uint8_t)(modifier | ((modifier & 0x40) << 1))); - return (uint32_t)((int32_t)(self->copy_len_ & 0x1FFFFFF) + delta); + uint32_t modifier = self->copy_len_ >> 25; + int32_t delta = (int8_t)((uint8_t)(modifier | ((modifier & 0x40) << 1))); + return (uint32_t)((int32_t)(self->copy_len_ & 0x1FFFFFF) + delta); } #if defined(__cplusplus) || defined(c_plusplus) diff --git a/contrib/libs/brotli/enc/compress_fragment.c b/contrib/libs/brotli/enc/compress_fragment.c index 9e50b2098a..0279d9647d 100644 --- a/contrib/libs/brotli/enc/compress_fragment.c +++ b/contrib/libs/brotli/enc/compress_fragment.c @@ -17,7 +17,7 @@ #include <string.h> /* memcmp, memcpy, memset */ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./brotli_bit_stream.h" #include "./entropy_encode.h" @@ -38,7 +38,7 @@ extern "C" { * There is no effort to ensure that it is a prime, the oddity is enough for this use. * The number has been tuned heuristically against compression benchmarks. */ -static const uint32_t kHashMul32 = 0x1E35A7BD; +static const uint32_t kHashMul32 = 0x1E35A7BD; static BROTLI_INLINE uint32_t Hash(const uint8_t* p, size_t shift) { const uint64_t h = (BROTLI_UNALIGNED_LOAD64LE(p) << 24) * kHashMul32; @@ -47,8 +47,8 @@ static BROTLI_INLINE uint32_t Hash(const uint8_t* p, size_t shift) { static BROTLI_INLINE uint32_t HashBytesAtOffset( uint64_t v, int offset, size_t shift) { - BROTLI_DCHECK(offset >= 0); - BROTLI_DCHECK(offset <= 3); + BROTLI_DCHECK(offset >= 0); + BROTLI_DCHECK(offset <= 3); { const uint64_t h = ((v >> (8 * offset)) << 24) * kHashMul32; return (uint32_t)(h >> shift); @@ -57,7 +57,7 @@ static BROTLI_INLINE uint32_t HashBytesAtOffset( static BROTLI_INLINE BROTLI_BOOL IsMatch(const uint8_t* p1, const uint8_t* p2) { return TO_BROTLI_BOOL( - BrotliUnalignedRead32(p1) == BrotliUnalignedRead32(p2) && + BrotliUnalignedRead32(p1) == BrotliUnalignedRead32(p2) && p1[4] == p2[4]); } @@ -202,7 +202,7 @@ static BROTLI_INLINE void EmitInsertLen(size_t insertlen, } else { BrotliWriteBits(depth[61], bits[61], storage_ix, storage); BrotliWriteBits(12, insertlen - 2114, storage_ix, storage); - ++histo[61]; + ++histo[61]; } } @@ -215,11 +215,11 @@ static BROTLI_INLINE void EmitLongInsertLen(size_t insertlen, if (insertlen < 22594) { BrotliWriteBits(depth[62], bits[62], storage_ix, storage); BrotliWriteBits(14, insertlen - 6210, storage_ix, storage); - ++histo[62]; + ++histo[62]; } else { BrotliWriteBits(depth[63], bits[63], storage_ix, storage); BrotliWriteBits(24, insertlen - 22594, storage_ix, storage); - ++histo[63]; + ++histo[63]; } } @@ -251,7 +251,7 @@ static BROTLI_INLINE void EmitCopyLen(size_t copylen, } else { BrotliWriteBits(depth[39], bits[39], storage_ix, storage); BrotliWriteBits(24, copylen - 2118, storage_ix, storage); - ++histo[39]; + ++histo[39]; } } @@ -293,7 +293,7 @@ static BROTLI_INLINE void EmitCopyLenLastDistance(size_t copylen, BrotliWriteBits(depth[39], bits[39], storage_ix, storage); BrotliWriteBits(24, copylen - 2120, storage_ix, storage); BrotliWriteBits(depth[64], bits[64], storage_ix, storage); - ++histo[39]; + ++histo[39]; ++histo[64]; } } @@ -343,7 +343,7 @@ static void BrotliStoreMetaBlockHeader( } static void UpdateBits(size_t n_bits, uint32_t bits, size_t pos, - uint8_t* array) { + uint8_t* array) { while (n_bits > 0) { size_t byte_pos = pos >> 3; size_t n_unchanged_bits = pos & 7; @@ -521,12 +521,12 @@ static BROTLI_INLINE void BrotliCompressFragmentFastImpl( const uint8_t* next_ip = ip; const uint8_t* candidate; - BROTLI_DCHECK(next_emit < ip); + BROTLI_DCHECK(next_emit < ip); trawl: do { uint32_t hash = next_hash; uint32_t bytes_between_hash_lookups = skip++ >> 5; - BROTLI_DCHECK(hash == Hash(next_ip, shift)); + BROTLI_DCHECK(hash == Hash(next_ip, shift)); ip = next_ip; next_ip = ip + bytes_between_hash_lookups; if (BROTLI_PREDICT_FALSE(next_ip > ip_limit)) { @@ -541,8 +541,8 @@ trawl: } } candidate = base_ip + table[hash]; - BROTLI_DCHECK(candidate >= base_ip); - BROTLI_DCHECK(candidate < ip); + BROTLI_DCHECK(candidate >= base_ip); + BROTLI_DCHECK(candidate < ip); table[hash] = (int)(ip - base_ip); } while (BROTLI_PREDICT_TRUE(!IsMatch(ip, candidate))); @@ -565,7 +565,7 @@ trawl: int distance = (int)(base - candidate); /* > 0 */ size_t insert = (size_t)(base - next_emit); ip += matched; - BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); + BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); if (BROTLI_PREDICT_TRUE(insert < 6210)) { EmitInsertLen(insert, cmd_depth, cmd_bits, cmd_histo, storage_ix, storage); @@ -625,7 +625,7 @@ trawl: if (ip - candidate > MAX_DISTANCE) break; ip += matched; last_distance = (int)(base - candidate); /* > 0 */ - BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); + BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); EmitCopyLen(matched, cmd_depth, cmd_bits, cmd_histo, storage_ix, storage); EmitDistance((size_t)last_distance, cmd_depth, cmd_bits, @@ -658,7 +658,7 @@ trawl: } emit_remainder: - BROTLI_DCHECK(next_emit <= ip_end); + BROTLI_DCHECK(next_emit <= ip_end); input += block_size; input_size -= block_size; block_size = BROTLI_MIN(size_t, input_size, kMergeBlockSize); @@ -668,7 +668,7 @@ trawl: if (input_size > 0 && total_block_size + block_size <= (1 << 20) && ShouldMergeBlock(input, block_size, lit_depth)) { - BROTLI_DCHECK(total_block_size > (1 << 16)); + BROTLI_DCHECK(total_block_size > (1 << 16)); /* Update the size of the current meta-block and continue emitting commands. We can do this because the current size and the new size both have 5 nibbles. */ @@ -751,7 +751,7 @@ void BrotliCompressFragmentFast( const size_t table_bits = Log2FloorNonZero(table_size); if (input_size == 0) { - BROTLI_DCHECK(is_last); + BROTLI_DCHECK(is_last); BrotliWriteBits(1, 1, storage_ix, storage); /* islast */ BrotliWriteBits(1, 1, storage_ix, storage); /* isempty */ *storage_ix = (*storage_ix + 7u) & ~7u; @@ -767,7 +767,7 @@ void BrotliCompressFragmentFast( break; FOR_TABLE_BITS_(CASE_) #undef CASE_ - default: BROTLI_DCHECK(0); break; + default: BROTLI_DCHECK(0); break; } /* If output is larger than single uncompressed block, rewrite it. */ diff --git a/contrib/libs/brotli/enc/compress_fragment.h b/contrib/libs/brotli/enc/compress_fragment.h index 80007f5dca..e34fad3e68 100644 --- a/contrib/libs/brotli/enc/compress_fragment.h +++ b/contrib/libs/brotli/enc/compress_fragment.h @@ -12,7 +12,7 @@ #ifndef BROTLI_ENC_COMPRESS_FRAGMENT_H_ #define BROTLI_ENC_COMPRESS_FRAGMENT_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./memory.h" diff --git a/contrib/libs/brotli/enc/compress_fragment_two_pass.c b/contrib/libs/brotli/enc/compress_fragment_two_pass.c index f8a5606384..f1ec2392f8 100644 --- a/contrib/libs/brotli/enc/compress_fragment_two_pass.c +++ b/contrib/libs/brotli/enc/compress_fragment_two_pass.c @@ -15,7 +15,7 @@ #include <string.h> /* memcmp, memcpy, memset */ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./bit_cost.h" #include "./brotli_bit_stream.h" @@ -37,31 +37,31 @@ extern "C" { * There is no effort to ensure that it is a prime, the oddity is enough for this use. * The number has been tuned heuristically against compression benchmarks. */ -static const uint32_t kHashMul32 = 0x1E35A7BD; +static const uint32_t kHashMul32 = 0x1E35A7BD; -static BROTLI_INLINE uint32_t Hash(const uint8_t* p, - size_t shift, size_t length) { - const uint64_t h = - (BROTLI_UNALIGNED_LOAD64LE(p) << ((8 - length) * 8)) * kHashMul32; +static BROTLI_INLINE uint32_t Hash(const uint8_t* p, + size_t shift, size_t length) { + const uint64_t h = + (BROTLI_UNALIGNED_LOAD64LE(p) << ((8 - length) * 8)) * kHashMul32; return (uint32_t)(h >> shift); } -static BROTLI_INLINE uint32_t HashBytesAtOffset(uint64_t v, size_t offset, - size_t shift, size_t length) { - BROTLI_DCHECK(offset <= 8 - length); +static BROTLI_INLINE uint32_t HashBytesAtOffset(uint64_t v, size_t offset, + size_t shift, size_t length) { + BROTLI_DCHECK(offset <= 8 - length); { - const uint64_t h = ((v >> (8 * offset)) << ((8 - length) * 8)) * kHashMul32; + const uint64_t h = ((v >> (8 * offset)) << ((8 - length) * 8)) * kHashMul32; return (uint32_t)(h >> shift); } } -static BROTLI_INLINE BROTLI_BOOL IsMatch(const uint8_t* p1, const uint8_t* p2, - size_t length) { - if (BrotliUnalignedRead32(p1) == BrotliUnalignedRead32(p2)) { - if (length == 4) return BROTLI_TRUE; - return TO_BROTLI_BOOL(p1[4] == p2[4] && p1[5] == p2[5]); - } - return BROTLI_FALSE; +static BROTLI_INLINE BROTLI_BOOL IsMatch(const uint8_t* p1, const uint8_t* p2, + size_t length) { + if (BrotliUnalignedRead32(p1) == BrotliUnalignedRead32(p2)) { + if (length == 4) return BROTLI_TRUE; + return TO_BROTLI_BOOL(p1[4] == p2[4] && p1[5] == p2[5]); + } + return BROTLI_FALSE; } /* Builds a command and distance prefix code (each 64 symbols) into "depth" and @@ -238,8 +238,8 @@ static void BrotliStoreMetaBlockHeader( static BROTLI_INLINE void CreateCommands(const uint8_t* input, size_t block_size, size_t input_size, const uint8_t* base_ip, int* table, - size_t table_bits, size_t min_match, - uint8_t** literals, uint32_t** commands) { + size_t table_bits, size_t min_match, + uint8_t** literals, uint32_t** commands) { /* "ip" is the input pointer. */ const uint8_t* ip = input; const size_t shift = 64u - table_bits; @@ -257,12 +257,12 @@ static BROTLI_INLINE void CreateCommands(const uint8_t* input, sure that all distances are at most window size - 16. For all other blocks, we only need to keep a margin of 5 bytes so that we don't go over the block size with a copy. */ - const size_t len_limit = BROTLI_MIN(size_t, block_size - min_match, + const size_t len_limit = BROTLI_MIN(size_t, block_size - min_match, input_size - kInputMarginBytes); const uint8_t* ip_limit = input + len_limit; uint32_t next_hash; - for (next_hash = Hash(++ip, shift, min_match); ; ) { + for (next_hash = Hash(++ip, shift, min_match); ; ) { /* Step 1: Scan forward in the input looking for a 6-byte-long match. If we get close to exhausting the input then goto emit_remainder. @@ -283,31 +283,31 @@ static BROTLI_INLINE void CreateCommands(const uint8_t* input, const uint8_t* next_ip = ip; const uint8_t* candidate; - BROTLI_DCHECK(next_emit < ip); + BROTLI_DCHECK(next_emit < ip); trawl: do { uint32_t hash = next_hash; uint32_t bytes_between_hash_lookups = skip++ >> 5; ip = next_ip; - BROTLI_DCHECK(hash == Hash(ip, shift, min_match)); + BROTLI_DCHECK(hash == Hash(ip, shift, min_match)); next_ip = ip + bytes_between_hash_lookups; if (BROTLI_PREDICT_FALSE(next_ip > ip_limit)) { goto emit_remainder; } - next_hash = Hash(next_ip, shift, min_match); + next_hash = Hash(next_ip, shift, min_match); candidate = ip - last_distance; - if (IsMatch(ip, candidate, min_match)) { + if (IsMatch(ip, candidate, min_match)) { if (BROTLI_PREDICT_TRUE(candidate < ip)) { table[hash] = (int)(ip - base_ip); break; } } candidate = base_ip + table[hash]; - BROTLI_DCHECK(candidate >= base_ip); - BROTLI_DCHECK(candidate < ip); + BROTLI_DCHECK(candidate >= base_ip); + BROTLI_DCHECK(candidate < ip); table[hash] = (int)(ip - base_ip); - } while (BROTLI_PREDICT_TRUE(!IsMatch(ip, candidate, min_match))); + } while (BROTLI_PREDICT_TRUE(!IsMatch(ip, candidate, min_match))); /* Check copy distance. If candidate is not feasible, continue search. Checking is done outside of hot loop to reduce overhead. */ @@ -322,13 +322,13 @@ trawl: /* We have a 6-byte match at ip, and we need to emit bytes in [next_emit, ip). */ const uint8_t* base = ip; - size_t matched = min_match + FindMatchLengthWithLimit( - candidate + min_match, ip + min_match, - (size_t)(ip_end - ip) - min_match); + size_t matched = min_match + FindMatchLengthWithLimit( + candidate + min_match, ip + min_match, + (size_t)(ip_end - ip) - min_match); int distance = (int)(base - candidate); /* > 0 */ int insert = (int)(base - next_emit); ip += matched; - BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); + BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); EmitInsertLen((uint32_t)insert, commands); memcpy(*literals, next_emit, (size_t)insert); *literals += insert; @@ -349,50 +349,50 @@ trawl: /* We could immediately start working at ip now, but to improve compression we first update "table" with the hashes of some positions within the last copy. */ - uint64_t input_bytes; + uint64_t input_bytes; uint32_t cur_hash; - uint32_t prev_hash; - if (min_match == 4) { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3); - cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match); - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 3); - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 2); - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 1); - } else { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 5); - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 5); - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 4); - prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 3); - input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 2); - cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 2); - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 1); - } + uint32_t prev_hash; + if (min_match == 4) { + input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3); + cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match); + prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 3); + prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 2); + prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 1); + } else { + input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 5); + prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 5); + prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 4); + prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 3); + input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 2); + cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); + prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 2); + prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 1); + } candidate = base_ip + table[cur_hash]; table[cur_hash] = (int)(ip - base_ip); } } - while (ip - candidate <= MAX_DISTANCE && - IsMatch(ip, candidate, min_match)) { + while (ip - candidate <= MAX_DISTANCE && + IsMatch(ip, candidate, min_match)) { /* We have a 6-byte match at ip, and no need to emit any literal bytes prior to ip. */ const uint8_t* base = ip; - size_t matched = min_match + FindMatchLengthWithLimit( - candidate + min_match, ip + min_match, - (size_t)(ip_end - ip) - min_match); + size_t matched = min_match + FindMatchLengthWithLimit( + candidate + min_match, ip + min_match, + (size_t)(ip_end - ip) - min_match); ip += matched; last_distance = (int)(base - candidate); /* > 0 */ - BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); + BROTLI_DCHECK(0 == memcmp(base, candidate, matched)); EmitCopyLen(matched, commands); EmitDistance((uint32_t)last_distance, commands); @@ -404,45 +404,45 @@ trawl: /* We could immediately start working at ip now, but to improve compression we first update "table" with the hashes of some positions within the last copy. */ - uint64_t input_bytes; + uint64_t input_bytes; uint32_t cur_hash; - uint32_t prev_hash; - if (min_match == 4) { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3); - cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match); - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 3); - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 2); - prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 1); - } else { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 5); - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 5); - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 4); - prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 3); - input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 2); - cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 2); - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); - table[prev_hash] = (int)(ip - base_ip - 1); - } + uint32_t prev_hash; + if (min_match == 4) { + input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3); + cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match); + prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 3); + prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 2); + prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 1); + } else { + input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 5); + prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 5); + prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 4); + prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 3); + input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 2); + cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match); + prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 2); + prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match); + table[prev_hash] = (int)(ip - base_ip - 1); + } candidate = base_ip + table[cur_hash]; table[cur_hash] = (int)(ip - base_ip); } } - next_hash = Hash(++ip, shift, min_match); + next_hash = Hash(++ip, shift, min_match); } } emit_remainder: - BROTLI_DCHECK(next_emit <= ip_end); + BROTLI_DCHECK(next_emit <= ip_end); /* Emit the remaining bytes as literals. */ if (next_emit < ip_end) { const uint32_t insert = (uint32_t)(ip_end - next_emit); @@ -488,7 +488,7 @@ static void StoreCommands(MemoryManager* m, for (i = 0; i < num_commands; ++i) { const uint32_t code = commands[i] & 0xFF; - BROTLI_DCHECK(code < 128); + BROTLI_DCHECK(code < 128); ++cmd_histo[code]; } cmd_histo[1] += 1; @@ -502,7 +502,7 @@ static void StoreCommands(MemoryManager* m, const uint32_t cmd = commands[i]; const uint32_t code = cmd & 0xFF; const uint32_t extra = cmd >> 8; - BROTLI_DCHECK(code < 128); + BROTLI_DCHECK(code < 128); BrotliWriteBits(cmd_depths[code], cmd_bits[code], storage_ix, storage); BrotliWriteBits(kNumExtraBits[code], extra, storage_ix, storage); if (code < 24) { @@ -557,8 +557,8 @@ static void EmitUncompressedMetaBlock(const uint8_t* input, size_t input_size, static BROTLI_INLINE void BrotliCompressFragmentTwoPassImpl( MemoryManager* m, const uint8_t* input, size_t input_size, BROTLI_BOOL is_last, uint32_t* command_buf, uint8_t* literal_buf, - int* table, size_t table_bits, size_t min_match, - size_t* storage_ix, uint8_t* storage) { + int* table, size_t table_bits, size_t min_match, + size_t* storage_ix, uint8_t* storage) { /* Save the start of the first block for position and distance computations. */ const uint8_t* base_ip = input; @@ -570,8 +570,8 @@ static BROTLI_INLINE void BrotliCompressFragmentTwoPassImpl( uint32_t* commands = command_buf; uint8_t* literals = literal_buf; size_t num_literals; - CreateCommands(input, block_size, input_size, base_ip, table, - table_bits, min_match, &literals, &commands); + CreateCommands(input, block_size, input_size, base_ip, table, + table_bits, min_match, &literals, &commands); num_literals = (size_t)(literals - literal_buf); if (ShouldCompress(input, block_size, num_literals)) { const size_t num_commands = (size_t)(commands - command_buf); @@ -600,9 +600,9 @@ static BROTLI_NOINLINE void BrotliCompressFragmentTwoPassImpl ## B( \ MemoryManager* m, const uint8_t* input, size_t input_size, \ BROTLI_BOOL is_last, uint32_t* command_buf, uint8_t* literal_buf, \ int* table, size_t* storage_ix, uint8_t* storage) { \ - size_t min_match = (B <= 15) ? 4 : 6; \ + size_t min_match = (B <= 15) ? 4 : 6; \ BrotliCompressFragmentTwoPassImpl(m, input, input_size, is_last, command_buf,\ - literal_buf, table, B, min_match, storage_ix, storage); \ + literal_buf, table, B, min_match, storage_ix, storage); \ } FOR_TABLE_BITS_(BAKE_METHOD_PARAM_) #undef BAKE_METHOD_PARAM_ @@ -622,7 +622,7 @@ void BrotliCompressFragmentTwoPass( break; FOR_TABLE_BITS_(CASE_) #undef CASE_ - default: BROTLI_DCHECK(0); break; + default: BROTLI_DCHECK(0); break; } /* If output is larger than single uncompressed block, rewrite it. */ diff --git a/contrib/libs/brotli/enc/compress_fragment_two_pass.h b/contrib/libs/brotli/enc/compress_fragment_two_pass.h index 928677df42..6103656f05 100644 --- a/contrib/libs/brotli/enc/compress_fragment_two_pass.h +++ b/contrib/libs/brotli/enc/compress_fragment_two_pass.h @@ -13,7 +13,7 @@ #ifndef BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_ #define BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./memory.h" diff --git a/contrib/libs/brotli/enc/dictionary_hash.c b/contrib/libs/brotli/enc/dictionary_hash.c index 3677d7ddb4..eeba48af1e 100644 --- a/contrib/libs/brotli/enc/dictionary_hash.c +++ b/contrib/libs/brotli/enc/dictionary_hash.c @@ -6,7 +6,7 @@ /* Hash table on the 4-byte prefixes of static dictionary words. */ -#include "../common/platform.h" +#include "../common/platform.h" #include "./dictionary_hash.h" #if defined(__cplusplus) || defined(c_plusplus) diff --git a/contrib/libs/brotli/enc/encode.c b/contrib/libs/brotli/enc/encode.c index 141e70aa2a..e7a6708c8b 100644 --- a/contrib/libs/brotli/enc/encode.c +++ b/contrib/libs/brotli/enc/encode.c @@ -11,9 +11,9 @@ #include <stdlib.h> /* free, malloc */ #include <string.h> /* memcpy, memset */ -#include "../common/constants.h" -#include "../common/context.h" -#include "../common/platform.h" +#include "../common/constants.h" +#include "../common/context.h" +#include "../common/platform.h" #include "../common/version.h" #include "./backward_references.h" #include "./backward_references_hq.h" @@ -21,7 +21,7 @@ #include "./brotli_bit_stream.h" #include "./compress_fragment.h" #include "./compress_fragment_two_pass.h" -#include "./encoder_dict.h" +#include "./encoder_dict.h" #include "./entropy_encode.h" #include "./fast_log.h" #include "./hash.h" @@ -71,8 +71,8 @@ typedef struct BrotliEncoderStateStruct { uint64_t last_processed_pos_; int dist_cache_[BROTLI_NUM_DISTANCE_SHORT_CODES]; int saved_dist_cache_[4]; - uint16_t last_bytes_; - uint8_t last_bytes_bits_; + uint16_t last_bytes_; + uint8_t last_bytes_bits_; uint8_t prev_byte_; uint8_t prev_byte2_; size_t storage_size_; @@ -162,18 +162,18 @@ BROTLI_BOOL BrotliEncoderSetParameter( state->params.size_hint = value; return BROTLI_TRUE; - case BROTLI_PARAM_LARGE_WINDOW: - state->params.large_window = TO_BROTLI_BOOL(!!value); - return BROTLI_TRUE; - - case BROTLI_PARAM_NPOSTFIX: - state->params.dist.distance_postfix_bits = value; - return BROTLI_TRUE; - - case BROTLI_PARAM_NDIRECT: - state->params.dist.num_direct_distance_codes = value; - return BROTLI_TRUE; - + case BROTLI_PARAM_LARGE_WINDOW: + state->params.large_window = TO_BROTLI_BOOL(!!value); + return BROTLI_TRUE; + + case BROTLI_PARAM_NPOSTFIX: + state->params.dist.distance_postfix_bits = value; + return BROTLI_TRUE; + + case BROTLI_PARAM_NDIRECT: + state->params.dist.num_direct_distance_codes = value; + return BROTLI_TRUE; + default: return BROTLI_FALSE; } } @@ -219,7 +219,7 @@ static int* GetHashTable(BrotliEncoderState* s, int quality, const size_t max_table_size = MaxHashTableSize(quality); size_t htsize = HashTableSize(max_table_size, input_size); int* table; - BROTLI_DCHECK(max_table_size >= 256); + BROTLI_DCHECK(max_table_size >= 256); if (quality == FAST_ONE_PASS_COMPRESSION_QUALITY) { /* Only odd shifts are supported by fast-one-pass. */ if ((htsize & 0xAAAAA) == 0) { @@ -244,25 +244,25 @@ static int* GetHashTable(BrotliEncoderState* s, int quality, return table; } -static void EncodeWindowBits(int lgwin, BROTLI_BOOL large_window, - uint16_t* last_bytes, uint8_t* last_bytes_bits) { - if (large_window) { - *last_bytes = (uint16_t)(((lgwin & 0x3F) << 8) | 0x11); - *last_bytes_bits = 14; +static void EncodeWindowBits(int lgwin, BROTLI_BOOL large_window, + uint16_t* last_bytes, uint8_t* last_bytes_bits) { + if (large_window) { + *last_bytes = (uint16_t)(((lgwin & 0x3F) << 8) | 0x11); + *last_bytes_bits = 14; } else { - if (lgwin == 16) { - *last_bytes = 0; - *last_bytes_bits = 1; - } else if (lgwin == 17) { - *last_bytes = 1; - *last_bytes_bits = 7; - } else if (lgwin > 17) { - *last_bytes = (uint16_t)(((lgwin - 17) << 1) | 0x01); - *last_bytes_bits = 4; - } else { - *last_bytes = (uint16_t)(((lgwin - 8) << 4) | 0x01); - *last_bytes_bits = 7; - } + if (lgwin == 16) { + *last_bytes = 0; + *last_bytes_bits = 1; + } else if (lgwin == 17) { + *last_bytes = 1; + *last_bytes_bits = 7; + } else if (lgwin > 17) { + *last_bytes = (uint16_t)(((lgwin - 17) << 1) | 0x01); + *last_bytes_bits = 4; + } else { + *last_bytes = (uint16_t)(((lgwin - 8) << 4) | 0x01); + *last_bytes_bits = 7; + } } } @@ -355,7 +355,7 @@ static void ChooseContextMap(int quality, } total = monogram_histo[0] + monogram_histo[1] + monogram_histo[2]; - BROTLI_DCHECK(total != 0); + BROTLI_DCHECK(total != 0); entropy[0] = 1.0 / (double)total; entropy[1] *= entropy[0]; entropy[2] *= entropy[0]; @@ -418,7 +418,7 @@ static BROTLI_BOOL ShouldUseComplexStaticContextMap(const uint8_t* input, double entropy[3]; size_t dummy; size_t i; - ContextLut utf8_lut = BROTLI_CONTEXT_LUT(CONTEXT_UTF8); + ContextLut utf8_lut = BROTLI_CONTEXT_LUT(CONTEXT_UTF8); for (; start_pos + 64 <= end_pos; start_pos += 4096) { const size_t stride_end_pos = start_pos + 64; uint8_t prev2 = input[start_pos & mask]; @@ -429,7 +429,7 @@ static BROTLI_BOOL ShouldUseComplexStaticContextMap(const uint8_t* input, for (pos = start_pos + 2; pos < stride_end_pos; ++pos) { const uint8_t literal = input[pos & mask]; const uint8_t context = (uint8_t)kStaticContextMapComplexUTF8[ - BROTLI_CONTEXT(prev1, prev2, utf8_lut)]; + BROTLI_CONTEXT(prev1, prev2, utf8_lut)]; ++total; ++combined_histo[literal >> 3]; ++context_histo[context][literal >> 3]; @@ -496,8 +496,8 @@ static void DecideOverLiteralContextModeling(const uint8_t* input, static BROTLI_BOOL ShouldCompress( const uint8_t* data, const size_t mask, const uint64_t last_flush_pos, const size_t bytes, const size_t num_literals, const size_t num_commands) { - /* TODO: find more precise minimal block overhead. */ - if (bytes <= 2) return BROTLI_FALSE; + /* TODO: find more precise minimal block overhead. */ + if (bytes <= 2) return BROTLI_FALSE; if (num_commands < (bytes >> 8) + 2) { if (num_literals > 0.99 * (double)bytes) { uint32_t literal_histo[256] = { 0 }; @@ -520,26 +520,26 @@ static BROTLI_BOOL ShouldCompress( return BROTLI_TRUE; } -/* Chooses the literal context mode for a metablock */ -static ContextType ChooseContextMode(const BrotliEncoderParams* params, - const uint8_t* data, const size_t pos, const size_t mask, - const size_t length) { - /* We only do the computation for the option of something else than - CONTEXT_UTF8 for the highest qualities */ - if (params->quality >= MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING && - !BrotliIsMostlyUTF8(data, pos, mask, length, kMinUTF8Ratio)) { - return CONTEXT_SIGNED; - } - return CONTEXT_UTF8; -} - +/* Chooses the literal context mode for a metablock */ +static ContextType ChooseContextMode(const BrotliEncoderParams* params, + const uint8_t* data, const size_t pos, const size_t mask, + const size_t length) { + /* We only do the computation for the option of something else than + CONTEXT_UTF8 for the highest qualities */ + if (params->quality >= MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING && + !BrotliIsMostlyUTF8(data, pos, mask, length, kMinUTF8Ratio)) { + return CONTEXT_SIGNED; + } + return CONTEXT_UTF8; +} + static void WriteMetaBlockInternal(MemoryManager* m, const uint8_t* data, const size_t mask, const uint64_t last_flush_pos, const size_t bytes, const BROTLI_BOOL is_last, - ContextType literal_context_mode, + ContextType literal_context_mode, const BrotliEncoderParams* params, const uint8_t prev_byte, const uint8_t prev_byte2, @@ -551,10 +551,10 @@ static void WriteMetaBlockInternal(MemoryManager* m, size_t* storage_ix, uint8_t* storage) { const uint32_t wrapped_last_flush_pos = WrapPosition(last_flush_pos); - uint16_t last_bytes; - uint8_t last_bytes_bits; - ContextLut literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode); - BrotliEncoderParams block_params = *params; + uint16_t last_bytes; + uint8_t last_bytes_bits; + ContextLut literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode); + BrotliEncoderParams block_params = *params; if (bytes == 0) { /* Write the ISLAST and ISEMPTY bits. */ @@ -574,18 +574,18 @@ static void WriteMetaBlockInternal(MemoryManager* m, return; } - BROTLI_DCHECK(*storage_ix <= 14); - last_bytes = (uint16_t)((storage[1] << 8) | storage[0]); - last_bytes_bits = (uint8_t)(*storage_ix); + BROTLI_DCHECK(*storage_ix <= 14); + last_bytes = (uint16_t)((storage[1] << 8) | storage[0]); + last_bytes_bits = (uint8_t)(*storage_ix); if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) { BrotliStoreMetaBlockFast(m, data, wrapped_last_flush_pos, - bytes, mask, is_last, params, + bytes, mask, is_last, params, commands, num_commands, storage_ix, storage); if (BROTLI_IS_OOM(m)) return; } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) { BrotliStoreMetaBlockTrivial(m, data, wrapped_last_flush_pos, - bytes, mask, is_last, params, + bytes, mask, is_last, params, commands, num_commands, storage_ix, storage); if (BROTLI_IS_OOM(m)) return; @@ -602,11 +602,11 @@ static void WriteMetaBlockInternal(MemoryManager* m, &literal_context_map); } BrotliBuildMetaBlockGreedy(m, data, wrapped_last_flush_pos, mask, - prev_byte, prev_byte2, literal_context_lut, num_literal_contexts, + prev_byte, prev_byte2, literal_context_lut, num_literal_contexts, literal_context_map, commands, num_commands, &mb); if (BROTLI_IS_OOM(m)) return; } else { - BrotliBuildMetaBlock(m, data, wrapped_last_flush_pos, mask, &block_params, + BrotliBuildMetaBlock(m, data, wrapped_last_flush_pos, mask, &block_params, prev_byte, prev_byte2, commands, num_commands, literal_context_mode, @@ -614,19 +614,19 @@ static void WriteMetaBlockInternal(MemoryManager* m, if (BROTLI_IS_OOM(m)) return; } if (params->quality >= MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS) { - /* The number of distance symbols effectively used for distance - histograms. It might be less than distance alphabet size - for "Large Window Brotli" (32-bit). */ - uint32_t num_effective_dist_codes = block_params.dist.alphabet_size; - if (num_effective_dist_codes > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS) { - num_effective_dist_codes = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS; - } - BrotliOptimizeHistograms(num_effective_dist_codes, &mb); + /* The number of distance symbols effectively used for distance + histograms. It might be less than distance alphabet size + for "Large Window Brotli" (32-bit). */ + uint32_t num_effective_dist_codes = block_params.dist.alphabet_size; + if (num_effective_dist_codes > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS) { + num_effective_dist_codes = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS; + } + BrotliOptimizeHistograms(num_effective_dist_codes, &mb); } BrotliStoreMetaBlock(m, data, wrapped_last_flush_pos, bytes, mask, prev_byte, prev_byte2, is_last, - &block_params, + &block_params, literal_context_mode, commands, num_commands, &mb, @@ -637,52 +637,52 @@ static void WriteMetaBlockInternal(MemoryManager* m, if (bytes + 4 < (*storage_ix >> 3)) { /* Restore the distance cache and last byte. */ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0])); - storage[0] = (uint8_t)last_bytes; - storage[1] = (uint8_t)(last_bytes >> 8); - *storage_ix = last_bytes_bits; + storage[0] = (uint8_t)last_bytes; + storage[1] = (uint8_t)(last_bytes >> 8); + *storage_ix = last_bytes_bits; BrotliStoreUncompressedMetaBlock(is_last, data, wrapped_last_flush_pos, mask, bytes, storage_ix, storage); } } -static void ChooseDistanceParams(BrotliEncoderParams* params) { - uint32_t distance_postfix_bits = 0; - uint32_t num_direct_distance_codes = 0; - - if (params->quality >= MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS) { - uint32_t ndirect_msb; - if (params->mode == BROTLI_MODE_FONT) { - distance_postfix_bits = 1; - num_direct_distance_codes = 12; - } else { - distance_postfix_bits = params->dist.distance_postfix_bits; - num_direct_distance_codes = params->dist.num_direct_distance_codes; - } - ndirect_msb = (num_direct_distance_codes >> distance_postfix_bits) & 0x0F; - if (distance_postfix_bits > BROTLI_MAX_NPOSTFIX || - num_direct_distance_codes > BROTLI_MAX_NDIRECT || - (ndirect_msb << distance_postfix_bits) != num_direct_distance_codes) { - distance_postfix_bits = 0; - num_direct_distance_codes = 0; - } - } - - BrotliInitDistanceParams( - params, distance_postfix_bits, num_direct_distance_codes); -} - +static void ChooseDistanceParams(BrotliEncoderParams* params) { + uint32_t distance_postfix_bits = 0; + uint32_t num_direct_distance_codes = 0; + + if (params->quality >= MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS) { + uint32_t ndirect_msb; + if (params->mode == BROTLI_MODE_FONT) { + distance_postfix_bits = 1; + num_direct_distance_codes = 12; + } else { + distance_postfix_bits = params->dist.distance_postfix_bits; + num_direct_distance_codes = params->dist.num_direct_distance_codes; + } + ndirect_msb = (num_direct_distance_codes >> distance_postfix_bits) & 0x0F; + if (distance_postfix_bits > BROTLI_MAX_NPOSTFIX || + num_direct_distance_codes > BROTLI_MAX_NDIRECT || + (ndirect_msb << distance_postfix_bits) != num_direct_distance_codes) { + distance_postfix_bits = 0; + num_direct_distance_codes = 0; + } + } + + BrotliInitDistanceParams( + params, distance_postfix_bits, num_direct_distance_codes); +} + static BROTLI_BOOL EnsureInitialized(BrotliEncoderState* s) { if (BROTLI_IS_OOM(&s->memory_manager_)) return BROTLI_FALSE; if (s->is_initialized_) return BROTLI_TRUE; - s->last_bytes_bits_ = 0; - s->last_bytes_ = 0; - s->remaining_metadata_bytes_ = BROTLI_UINT32_MAX; - + s->last_bytes_bits_ = 0; + s->last_bytes_ = 0; + s->remaining_metadata_bytes_ = BROTLI_UINT32_MAX; + SanitizeParams(&s->params); s->params.lgblock = ComputeLgBlock(&s->params); - ChooseDistanceParams(&s->params); + ChooseDistanceParams(&s->params); RingBufferSetup(&s->params, &s->ringbuffer_); @@ -693,8 +693,8 @@ static BROTLI_BOOL EnsureInitialized(BrotliEncoderState* s) { s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) { lgwin = BROTLI_MAX(int, lgwin, 18); } - EncodeWindowBits(lgwin, s->params.large_window, - &s->last_bytes_, &s->last_bytes_bits_); + EncodeWindowBits(lgwin, s->params.large_window, + &s->last_bytes_, &s->last_bytes_bits_); } if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) { @@ -708,18 +708,18 @@ static BROTLI_BOOL EnsureInitialized(BrotliEncoderState* s) { static void BrotliEncoderInitParams(BrotliEncoderParams* params) { params->mode = BROTLI_DEFAULT_MODE; - params->large_window = BROTLI_FALSE; + params->large_window = BROTLI_FALSE; params->quality = BROTLI_DEFAULT_QUALITY; params->lgwin = BROTLI_DEFAULT_WINDOW; params->lgblock = 0; params->size_hint = 0; params->disable_literal_context_modeling = BROTLI_FALSE; - BrotliInitEncoderDictionary(¶ms->dictionary); - params->dist.distance_postfix_bits = 0; - params->dist.num_direct_distance_codes = 0; - params->dist.alphabet_size = - BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_MAX_DISTANCE_BITS); - params->dist.max_distance = BROTLI_MAX_DISTANCE; + BrotliInitEncoderDictionary(¶ms->dictionary); + params->dist.distance_postfix_bits = 0; + params->dist.num_direct_distance_codes = 0; + params->dist.alphabet_size = + BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_MAX_DISTANCE_BITS); + params->dist.max_distance = BROTLI_MAX_DISTANCE; } static void BrotliEncoderInitState(BrotliEncoderState* s) { @@ -762,8 +762,8 @@ static void BrotliEncoderInitState(BrotliEncoderState* s) { memcpy(s->saved_dist_cache_, s->dist_cache_, sizeof(s->saved_dist_cache_)); } -BrotliEncoderState* BrotliEncoderCreateInstance( - brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { +BrotliEncoderState* BrotliEncoderCreateInstance( + brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { BrotliEncoderState* state = 0; if (!alloc_func && !free_func) { state = (BrotliEncoderState*)malloc(sizeof(BrotliEncoderState)); @@ -879,39 +879,39 @@ static BROTLI_BOOL UpdateLastProcessedPos(BrotliEncoderState* s) { return TO_BROTLI_BOOL(wrapped_input_pos < wrapped_last_processed_pos); } -static void ExtendLastCommand(BrotliEncoderState* s, uint32_t* bytes, - uint32_t* wrapped_last_processed_pos) { - Command* last_command = &s->commands_[s->num_commands_ - 1]; - const uint8_t* data = s->ringbuffer_.buffer_; - const uint32_t mask = s->ringbuffer_.mask_; - uint64_t max_backward_distance = - (((uint64_t)1) << s->params.lgwin) - BROTLI_WINDOW_GAP; - uint64_t last_copy_len = last_command->copy_len_ & 0x1FFFFFF; - uint64_t last_processed_pos = s->last_processed_pos_ - last_copy_len; - uint64_t max_distance = last_processed_pos < max_backward_distance ? - last_processed_pos : max_backward_distance; - uint64_t cmd_dist = (uint64_t)s->dist_cache_[0]; - uint32_t distance_code = CommandRestoreDistanceCode(last_command, - &s->params.dist); - if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES || - distance_code - (BROTLI_NUM_DISTANCE_SHORT_CODES - 1) == cmd_dist) { - if (cmd_dist <= max_distance) { - while (*bytes != 0 && data[*wrapped_last_processed_pos & mask] == - data[(*wrapped_last_processed_pos - cmd_dist) & mask]) { - last_command->copy_len_++; - (*bytes)--; - (*wrapped_last_processed_pos)++; - } - } - /* The copy length is at most the metablock size, and thus expressible. */ - GetLengthCode(last_command->insert_len_, - (size_t)((int)(last_command->copy_len_ & 0x1FFFFFF) + - (int)(last_command->copy_len_ >> 25)), - TO_BROTLI_BOOL((last_command->dist_prefix_ & 0x3FF) == 0), - &last_command->cmd_prefix_); - } -} - +static void ExtendLastCommand(BrotliEncoderState* s, uint32_t* bytes, + uint32_t* wrapped_last_processed_pos) { + Command* last_command = &s->commands_[s->num_commands_ - 1]; + const uint8_t* data = s->ringbuffer_.buffer_; + const uint32_t mask = s->ringbuffer_.mask_; + uint64_t max_backward_distance = + (((uint64_t)1) << s->params.lgwin) - BROTLI_WINDOW_GAP; + uint64_t last_copy_len = last_command->copy_len_ & 0x1FFFFFF; + uint64_t last_processed_pos = s->last_processed_pos_ - last_copy_len; + uint64_t max_distance = last_processed_pos < max_backward_distance ? + last_processed_pos : max_backward_distance; + uint64_t cmd_dist = (uint64_t)s->dist_cache_[0]; + uint32_t distance_code = CommandRestoreDistanceCode(last_command, + &s->params.dist); + if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES || + distance_code - (BROTLI_NUM_DISTANCE_SHORT_CODES - 1) == cmd_dist) { + if (cmd_dist <= max_distance) { + while (*bytes != 0 && data[*wrapped_last_processed_pos & mask] == + data[(*wrapped_last_processed_pos - cmd_dist) & mask]) { + last_command->copy_len_++; + (*bytes)--; + (*wrapped_last_processed_pos)++; + } + } + /* The copy length is at most the metablock size, and thus expressible. */ + GetLengthCode(last_command->insert_len_, + (size_t)((int)(last_command->copy_len_ & 0x1FFFFFF) + + (int)(last_command->copy_len_ >> 25)), + TO_BROTLI_BOOL((last_command->dist_prefix_ & 0x3FF) == 0), + &last_command->cmd_prefix_); + } +} + /* Processes the accumulated input data and sets |*out_size| to the length of the new output meta-block, or to zero if no new output meta-block has been @@ -928,12 +928,12 @@ static BROTLI_BOOL EncodeData( BrotliEncoderState* s, const BROTLI_BOOL is_last, const BROTLI_BOOL force_flush, size_t* out_size, uint8_t** output) { const uint64_t delta = UnprocessedInputSize(s); - uint32_t bytes = (uint32_t)delta; - uint32_t wrapped_last_processed_pos = WrapPosition(s->last_processed_pos_); + uint32_t bytes = (uint32_t)delta; + uint32_t wrapped_last_processed_pos = WrapPosition(s->last_processed_pos_); uint8_t* data; uint32_t mask; MemoryManager* m = &s->memory_manager_; - ContextType literal_context_mode; + ContextType literal_context_mode; data = s->ringbuffer_.buffer_; mask = s->ringbuffer_.mask_; @@ -957,7 +957,7 @@ static BROTLI_BOOL EncodeData( if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY || s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) { uint8_t* storage; - size_t storage_ix = s->last_bytes_bits_; + size_t storage_ix = s->last_bytes_bits_; size_t table_size; int* table; @@ -967,10 +967,10 @@ static BROTLI_BOOL EncodeData( *out_size = 0; return BROTLI_TRUE; } - storage = GetBrotliStorage(s, 2 * bytes + 503); + storage = GetBrotliStorage(s, 2 * bytes + 503); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; - storage[0] = (uint8_t)s->last_bytes_; - storage[1] = (uint8_t)(s->last_bytes_ >> 8); + storage[0] = (uint8_t)s->last_bytes_; + storage[1] = (uint8_t)(s->last_bytes_ >> 8); table = GetHashTable(s, s->params.quality, bytes, &table_size); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) { @@ -991,8 +991,8 @@ static BROTLI_BOOL EncodeData( &storage_ix, storage); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; } - s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]); - s->last_bytes_bits_ = storage_ix & 7u; + s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]); + s->last_bytes_bits_ = storage_ix & 7u; UpdateLastProcessedPos(s); *output = &storage[0]; *out_size = storage_ix >> 3; @@ -1020,36 +1020,36 @@ static BROTLI_BOOL EncodeData( InitOrStitchToPreviousBlock(m, &s->hasher_, data, mask, &s->params, wrapped_last_processed_pos, bytes, is_last); - - literal_context_mode = ChooseContextMode( - &s->params, data, WrapPosition(s->last_flush_pos_), - mask, (size_t)(s->input_pos_ - s->last_flush_pos_)); - + + literal_context_mode = ChooseContextMode( + &s->params, data, WrapPosition(s->last_flush_pos_), + mask, (size_t)(s->input_pos_ - s->last_flush_pos_)); + if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; - if (s->num_commands_ && s->last_insert_len_ == 0) { - ExtendLastCommand(s, &bytes, &wrapped_last_processed_pos); - } - + if (s->num_commands_ && s->last_insert_len_ == 0) { + ExtendLastCommand(s, &bytes, &wrapped_last_processed_pos); + } + if (s->params.quality == ZOPFLIFICATION_QUALITY) { - BROTLI_DCHECK(s->params.hasher.type == 10); - BrotliCreateZopfliBackwardReferences(m, bytes, wrapped_last_processed_pos, - data, mask, &s->params, s->hasher_, s->dist_cache_, - &s->last_insert_len_, &s->commands_[s->num_commands_], - &s->num_commands_, &s->num_literals_); + BROTLI_DCHECK(s->params.hasher.type == 10); + BrotliCreateZopfliBackwardReferences(m, bytes, wrapped_last_processed_pos, + data, mask, &s->params, s->hasher_, s->dist_cache_, + &s->last_insert_len_, &s->commands_[s->num_commands_], + &s->num_commands_, &s->num_literals_); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; } else if (s->params.quality == HQ_ZOPFLIFICATION_QUALITY) { - BROTLI_DCHECK(s->params.hasher.type == 10); - BrotliCreateHqZopfliBackwardReferences(m, bytes, wrapped_last_processed_pos, - data, mask, &s->params, s->hasher_, s->dist_cache_, - &s->last_insert_len_, &s->commands_[s->num_commands_], - &s->num_commands_, &s->num_literals_); + BROTLI_DCHECK(s->params.hasher.type == 10); + BrotliCreateHqZopfliBackwardReferences(m, bytes, wrapped_last_processed_pos, + data, mask, &s->params, s->hasher_, s->dist_cache_, + &s->last_insert_len_, &s->commands_[s->num_commands_], + &s->num_commands_, &s->num_literals_); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; } else { - BrotliCreateBackwardReferences(bytes, wrapped_last_processed_pos, - data, mask, &s->params, s->hasher_, s->dist_cache_, - &s->last_insert_len_, &s->commands_[s->num_commands_], - &s->num_commands_, &s->num_literals_); + BrotliCreateBackwardReferences(bytes, wrapped_last_processed_pos, + data, mask, &s->params, s->hasher_, s->dist_cache_, + &s->last_insert_len_, &s->commands_[s->num_commands_], + &s->num_commands_, &s->num_literals_); } { @@ -1092,25 +1092,25 @@ static BROTLI_BOOL EncodeData( *out_size = 0; return BROTLI_TRUE; } - BROTLI_DCHECK(s->input_pos_ >= s->last_flush_pos_); - BROTLI_DCHECK(s->input_pos_ > s->last_flush_pos_ || is_last); - BROTLI_DCHECK(s->input_pos_ - s->last_flush_pos_ <= 1u << 24); + BROTLI_DCHECK(s->input_pos_ >= s->last_flush_pos_); + BROTLI_DCHECK(s->input_pos_ > s->last_flush_pos_ || is_last); + BROTLI_DCHECK(s->input_pos_ - s->last_flush_pos_ <= 1u << 24); { const uint32_t metablock_size = (uint32_t)(s->input_pos_ - s->last_flush_pos_); - uint8_t* storage = GetBrotliStorage(s, 2 * metablock_size + 503); - size_t storage_ix = s->last_bytes_bits_; + uint8_t* storage = GetBrotliStorage(s, 2 * metablock_size + 503); + size_t storage_ix = s->last_bytes_bits_; if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; - storage[0] = (uint8_t)s->last_bytes_; - storage[1] = (uint8_t)(s->last_bytes_ >> 8); + storage[0] = (uint8_t)s->last_bytes_; + storage[1] = (uint8_t)(s->last_bytes_ >> 8); WriteMetaBlockInternal( m, data, mask, s->last_flush_pos_, metablock_size, is_last, - literal_context_mode, &s->params, s->prev_byte_, s->prev_byte2_, + literal_context_mode, &s->params, s->prev_byte_, s->prev_byte2_, s->num_literals_, s->num_commands_, s->commands_, s->saved_dist_cache_, s->dist_cache_, &storage_ix, storage); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; - s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]); - s->last_bytes_bits_ = storage_ix & 7u; + s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]); + s->last_bytes_bits_ = storage_ix & 7u; s->last_flush_pos_ = s->input_pos_; if (UpdateLastProcessedPos(s)) { HasherReset(s->hasher_); @@ -1139,11 +1139,11 @@ static BROTLI_BOOL EncodeData( static size_t WriteMetadataHeader( BrotliEncoderState* s, const size_t block_size, uint8_t* header) { size_t storage_ix; - storage_ix = s->last_bytes_bits_; - header[0] = (uint8_t)s->last_bytes_; - header[1] = (uint8_t)(s->last_bytes_ >> 8); - s->last_bytes_ = 0; - s->last_bytes_bits_ = 0; + storage_ix = s->last_bytes_bits_; + header[0] = (uint8_t)s->last_bytes_; + header[1] = (uint8_t)(s->last_bytes_ >> 8); + s->last_bytes_ = 0; + s->last_bytes_bits_ = 0; BrotliWriteBits(1, 0, &storage_ix, header); BrotliWriteBits(2, 3, &storage_ix, header); @@ -1172,12 +1172,12 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( BROTLI_BOOL ok = BROTLI_TRUE; const size_t max_out_size = *encoded_size; size_t total_out_size = 0; - uint16_t last_bytes; - uint8_t last_bytes_bits; + uint16_t last_bytes; + uint8_t last_bytes_bits; HasherHandle hasher = NULL; - const size_t hasher_eff_size = BROTLI_MIN(size_t, - input_size, BROTLI_MAX_BACKWARD_LIMIT(lgwin) + BROTLI_WINDOW_GAP); + const size_t hasher_eff_size = BROTLI_MIN(size_t, + input_size, BROTLI_MAX_BACKWARD_LIMIT(lgwin) + BROTLI_WINDOW_GAP); BrotliEncoderParams params; @@ -1193,18 +1193,18 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( BrotliEncoderInitParams(¶ms); params.quality = 10; params.lgwin = lgwin; - if (lgwin > BROTLI_MAX_WINDOW_BITS) { - params.large_window = BROTLI_TRUE; - } + if (lgwin > BROTLI_MAX_WINDOW_BITS) { + params.large_window = BROTLI_TRUE; + } SanitizeParams(¶ms); params.lgblock = ComputeLgBlock(¶ms); - ChooseDistanceParams(¶ms); + ChooseDistanceParams(¶ms); max_block_size = (size_t)1 << params.lgblock; BrotliInitMemoryManager(m, 0, 0, 0); - BROTLI_DCHECK(input_size <= mask + 1); - EncodeWindowBits(lgwin, params.large_window, &last_bytes, &last_bytes_bits); + BROTLI_DCHECK(input_size <= mask + 1); + EncodeWindowBits(lgwin, params.large_window, &last_bytes, &last_bytes_bits); InitOrStitchToPreviousBlock(m, &hasher, input_buffer, mask, ¶ms, 0, hasher_eff_size, BROTLI_TRUE); if (BROTLI_IS_OOM(m)) goto oom; @@ -1224,9 +1224,9 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( uint8_t* storage; size_t storage_ix; - ContextType literal_context_mode = ChooseContextMode(¶ms, - input_buffer, metablock_start, mask, metablock_end - metablock_start); - + ContextType literal_context_mode = ChooseContextMode(¶ms, + input_buffer, metablock_start, mask, metablock_end - metablock_start); + size_t block_start; for (block_start = metablock_start; block_start < metablock_end; ) { size_t block_size = @@ -1238,9 +1238,9 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( BrotliInitZopfliNodes(nodes, block_size + 1); StitchToPreviousBlockH10(hasher, block_size, block_start, input_buffer, mask); - path_size = BrotliZopfliComputeShortestPath(m, block_size, block_start, - input_buffer, mask, ¶ms, dist_cache, hasher, - nodes); + path_size = BrotliZopfliComputeShortestPath(m, block_size, block_start, + input_buffer, mask, ¶ms, dist_cache, hasher, + nodes); if (BROTLI_IS_OOM(m)) goto oom; /* We allocate a command buffer in the first iteration of this loop that will be likely big enough for the whole metablock, so that for most @@ -1262,8 +1262,8 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( } commands = new_commands; } - BrotliZopfliCreateCommands(block_size, block_start, &nodes[0], dist_cache, - &last_insert_len, ¶ms, &commands[num_commands], &num_literals); + BrotliZopfliCreateCommands(block_size, block_start, &nodes[0], dist_cache, + &last_insert_len, ¶ms, &commands[num_commands], &num_literals); num_commands += path_size; block_start += block_size; metablock_size += block_size; @@ -1281,14 +1281,14 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( is_last = TO_BROTLI_BOOL(metablock_start + metablock_size == input_size); storage = NULL; - storage_ix = last_bytes_bits; + storage_ix = last_bytes_bits; if (metablock_size == 0) { /* Write the ISLAST and ISEMPTY bits. */ storage = BROTLI_ALLOC(m, uint8_t, 16); if (BROTLI_IS_OOM(m)) goto oom; - storage[0] = (uint8_t)last_bytes; - storage[1] = (uint8_t)(last_bytes >> 8); + storage[0] = (uint8_t)last_bytes; + storage[1] = (uint8_t)(last_bytes >> 8); BrotliWriteBits(2, 3, &storage_ix, storage); storage_ix = (storage_ix + 7u) & ~7u; } else if (!ShouldCompress(input_buffer, mask, metablock_start, @@ -1298,40 +1298,40 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0])); storage = BROTLI_ALLOC(m, uint8_t, metablock_size + 16); if (BROTLI_IS_OOM(m)) goto oom; - storage[0] = (uint8_t)last_bytes; - storage[1] = (uint8_t)(last_bytes >> 8); + storage[0] = (uint8_t)last_bytes; + storage[1] = (uint8_t)(last_bytes >> 8); BrotliStoreUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage); } else { MetaBlockSplit mb; - BrotliEncoderParams block_params = params; + BrotliEncoderParams block_params = params; InitMetaBlockSplit(&mb); - BrotliBuildMetaBlock(m, input_buffer, metablock_start, mask, - &block_params, + BrotliBuildMetaBlock(m, input_buffer, metablock_start, mask, + &block_params, prev_byte, prev_byte2, commands, num_commands, literal_context_mode, &mb); if (BROTLI_IS_OOM(m)) goto oom; - { - /* The number of distance symbols effectively used for distance - histograms. It might be less than distance alphabet size - for "Large Window Brotli" (32-bit). */ - uint32_t num_effective_dist_codes = block_params.dist.alphabet_size; - if (num_effective_dist_codes > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS) { - num_effective_dist_codes = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS; - } - BrotliOptimizeHistograms(num_effective_dist_codes, &mb); - } - storage = BROTLI_ALLOC(m, uint8_t, 2 * metablock_size + 503); + { + /* The number of distance symbols effectively used for distance + histograms. It might be less than distance alphabet size + for "Large Window Brotli" (32-bit). */ + uint32_t num_effective_dist_codes = block_params.dist.alphabet_size; + if (num_effective_dist_codes > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS) { + num_effective_dist_codes = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS; + } + BrotliOptimizeHistograms(num_effective_dist_codes, &mb); + } + storage = BROTLI_ALLOC(m, uint8_t, 2 * metablock_size + 503); if (BROTLI_IS_OOM(m)) goto oom; - storage[0] = (uint8_t)last_bytes; - storage[1] = (uint8_t)(last_bytes >> 8); + storage[0] = (uint8_t)last_bytes; + storage[1] = (uint8_t)(last_bytes >> 8); BrotliStoreMetaBlock(m, input_buffer, metablock_start, metablock_size, mask, prev_byte, prev_byte2, is_last, - &block_params, + &block_params, literal_context_mode, commands, num_commands, &mb, @@ -1340,22 +1340,22 @@ static BROTLI_BOOL BrotliCompressBufferQuality10( if (metablock_size + 4 < (storage_ix >> 3)) { /* Restore the distance cache and last byte. */ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0])); - storage[0] = (uint8_t)last_bytes; - storage[1] = (uint8_t)(last_bytes >> 8); - storage_ix = last_bytes_bits; + storage[0] = (uint8_t)last_bytes; + storage[1] = (uint8_t)(last_bytes >> 8); + storage_ix = last_bytes_bits; BrotliStoreUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage); } DestroyMetaBlockSplit(m, &mb); } - last_bytes = (uint16_t)(storage[storage_ix >> 3]); - last_bytes_bits = storage_ix & 7u; + last_bytes = (uint16_t)(storage[storage_ix >> 3]); + last_bytes_bits = storage_ix & 7u; metablock_start += metablock_size; - if (metablock_start < input_size) { - prev_byte = input_buffer[metablock_start - 1]; - prev_byte2 = input_buffer[metablock_start - 2]; - } + if (metablock_start < input_size) { + prev_byte = input_buffer[metablock_start - 1]; + prev_byte2 = input_buffer[metablock_start - 2]; + } /* Save the state of the distance cache in case we need to restore it for emitting an uncompressed block. */ memcpy(saved_dist_cache, dist_cache, 4 * sizeof(dist_cache[0])); @@ -1385,10 +1385,10 @@ oom: size_t BrotliEncoderMaxCompressedSize(size_t input_size) { /* [window bits / empty metadata] + N * [uncompressed] + [last empty] */ - size_t num_large_blocks = input_size >> 14; - size_t overhead = 2 + (4 * num_large_blocks) + 3 + 1; + size_t num_large_blocks = input_size >> 14; + size_t overhead = 2 + (4 * num_large_blocks) + 3 + 1; size_t result = input_size + overhead; - if (input_size == 0) return 2; + if (input_size == 0) return 2; return (result < input_size) ? 0 : result; } @@ -1449,7 +1449,7 @@ BROTLI_BOOL BrotliEncoderCompress( } if (quality == 10) { /* TODO: Implement this direct path for all quality levels. */ - const int lg_win = BROTLI_MIN(int, BROTLI_LARGE_MAX_WINDOW_BITS, + const int lg_win = BROTLI_MIN(int, BROTLI_LARGE_MAX_WINDOW_BITS, BROTLI_MAX(int, 16, lgwin)); int ok = BrotliCompressBufferQuality10(lg_win, input_size, input_buffer, encoded_size, encoded_buffer); @@ -1473,9 +1473,9 @@ BROTLI_BOOL BrotliEncoderCompress( BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, (uint32_t)lgwin); BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, (uint32_t)mode); BrotliEncoderSetParameter(s, BROTLI_PARAM_SIZE_HINT, (uint32_t)input_size); - if (lgwin > BROTLI_MAX_WINDOW_BITS) { - BrotliEncoderSetParameter(s, BROTLI_PARAM_LARGE_WINDOW, BROTLI_TRUE); - } + if (lgwin > BROTLI_MAX_WINDOW_BITS) { + BrotliEncoderSetParameter(s, BROTLI_PARAM_LARGE_WINDOW, BROTLI_TRUE); + } result = BrotliEncoderCompressStream(s, BROTLI_OPERATION_FINISH, &available_in, &next_in, &available_out, &next_out, &total_out); if (!BrotliEncoderIsFinished(s)) result = 0; @@ -1498,11 +1498,11 @@ fallback: } static void InjectBytePaddingBlock(BrotliEncoderState* s) { - uint32_t seal = s->last_bytes_; - size_t seal_bits = s->last_bytes_bits_; + uint32_t seal = s->last_bytes_; + size_t seal_bits = s->last_bytes_bits_; uint8_t* destination; - s->last_bytes_ = 0; - s->last_bytes_bits_ = 0; + s->last_bytes_ = 0; + s->last_bytes_bits_ = 0; /* is_last = 0, data_nibbles = 11, reserved = 0, meta_nibbles = 00 */ seal |= 0x6u << seal_bits; seal_bits += 6; @@ -1516,7 +1516,7 @@ static void InjectBytePaddingBlock(BrotliEncoderState* s) { } destination[0] = (uint8_t)seal; if (seal_bits > 8) destination[1] = (uint8_t)(seal >> 8); - if (seal_bits > 16) destination[2] = (uint8_t)(seal >> 16); + if (seal_bits > 16) destination[2] = (uint8_t)(seal >> 16); s->available_out_ += (seal_bits + 7) >> 3; } @@ -1525,7 +1525,7 @@ static void InjectBytePaddingBlock(BrotliEncoderState* s) { static BROTLI_BOOL InjectFlushOrPushOutput(BrotliEncoderState* s, size_t* available_out, uint8_t** next_out, size_t* total_out) { if (s->stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED && - s->last_bytes_bits_ != 0) { + s->last_bytes_bits_ != 0) { InjectBytePaddingBlock(s); return BROTLI_TRUE; } @@ -1606,10 +1606,10 @@ static BROTLI_BOOL BrotliEncoderCompressStreamFast( (*available_in == block_size) && (op == BROTLI_OPERATION_FINISH); BROTLI_BOOL force_flush = (*available_in == block_size) && (op == BROTLI_OPERATION_FLUSH); - size_t max_out_size = 2 * block_size + 503; + size_t max_out_size = 2 * block_size + 503; BROTLI_BOOL inplace = BROTLI_TRUE; uint8_t* storage = NULL; - size_t storage_ix = s->last_bytes_bits_; + size_t storage_ix = s->last_bytes_bits_; size_t table_size; int* table; @@ -1624,8 +1624,8 @@ static BROTLI_BOOL BrotliEncoderCompressStreamFast( storage = GetBrotliStorage(s, max_out_size); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; } - storage[0] = (uint8_t)s->last_bytes_; - storage[1] = (uint8_t)(s->last_bytes_ >> 8); + storage[0] = (uint8_t)s->last_bytes_; + storage[1] = (uint8_t)(s->last_bytes_ >> 8); table = GetHashTable(s, s->params.quality, block_size, &table_size); if (BROTLI_IS_OOM(m)) return BROTLI_FALSE; @@ -1644,8 +1644,8 @@ static BROTLI_BOOL BrotliEncoderCompressStreamFast( *available_in -= block_size; if (inplace) { size_t out_bytes = storage_ix >> 3; - BROTLI_DCHECK(out_bytes <= *available_out); - BROTLI_DCHECK((storage_ix & 7) == 0 || out_bytes < *available_out); + BROTLI_DCHECK(out_bytes <= *available_out); + BROTLI_DCHECK((storage_ix & 7) == 0 || out_bytes < *available_out); *next_out += out_bytes; *available_out -= out_bytes; s->total_out_ += out_bytes; @@ -1655,8 +1655,8 @@ static BROTLI_BOOL BrotliEncoderCompressStreamFast( s->next_out_ = storage; s->available_out_ = out_bytes; } - s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]); - s->last_bytes_bits_ = storage_ix & 7u; + s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]); + s->last_bytes_bits_ = storage_ix & 7u; if (force_flush) s->stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED; if (is_last) s->stream_state_ = BROTLI_STREAM_FINISHED; diff --git a/contrib/libs/brotli/enc/encoder_dict.c b/contrib/libs/brotli/enc/encoder_dict.c index 8b2f6ad4a4..20d8da1965 100644 --- a/contrib/libs/brotli/enc/encoder_dict.c +++ b/contrib/libs/brotli/enc/encoder_dict.c @@ -1,32 +1,32 @@ -/* Copyright 2017 Google Inc. All Rights Reserved. - - Distributed under MIT license. - See file LICENSE for detail or copy at https://opensource.org/licenses/MIT -*/ - -#include "./encoder_dict.h" - -#include "../common/dictionary.h" -#include "../common/transform.h" -#include "./dictionary_hash.h" -#include "./hash.h" - -#if defined(__cplusplus) || defined(c_plusplus) -extern "C" { -#endif - -void BrotliInitEncoderDictionary(BrotliEncoderDictionary* dict) { - dict->words = BrotliGetDictionary(); - - dict->hash_table = kStaticDictionaryHash; - dict->buckets = kStaticDictionaryBuckets; - dict->dict_words = kStaticDictionaryWords; - - dict->cutoffTransformsCount = kCutoffTransformsCount; - dict->cutoffTransforms = kCutoffTransforms; - -} - -#if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ -#endif +/* Copyright 2017 Google Inc. All Rights Reserved. + + Distributed under MIT license. + See file LICENSE for detail or copy at https://opensource.org/licenses/MIT +*/ + +#include "./encoder_dict.h" + +#include "../common/dictionary.h" +#include "../common/transform.h" +#include "./dictionary_hash.h" +#include "./hash.h" + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +void BrotliInitEncoderDictionary(BrotliEncoderDictionary* dict) { + dict->words = BrotliGetDictionary(); + + dict->hash_table = kStaticDictionaryHash; + dict->buckets = kStaticDictionaryBuckets; + dict->dict_words = kStaticDictionaryWords; + + dict->cutoffTransformsCount = kCutoffTransformsCount; + dict->cutoffTransforms = kCutoffTransforms; + +} + +#if defined(__cplusplus) || defined(c_plusplus) +} /* extern "C" */ +#endif diff --git a/contrib/libs/brotli/enc/encoder_dict.h b/contrib/libs/brotli/enc/encoder_dict.h index 3cb6b0ac15..d3cd3bb2e1 100644 --- a/contrib/libs/brotli/enc/encoder_dict.h +++ b/contrib/libs/brotli/enc/encoder_dict.h @@ -1,41 +1,41 @@ -/* Copyright 2017 Google Inc. All Rights Reserved. - - Distributed under MIT license. - See file LICENSE for detail or copy at https://opensource.org/licenses/MIT -*/ - -#ifndef BROTLI_ENC_ENCODER_DICT_H_ -#define BROTLI_ENC_ENCODER_DICT_H_ - -#include "../common/dictionary.h" -#include "../common/platform.h" -#include <brotli/types.h> -#include "./static_dict_lut.h" - -#if defined(__cplusplus) || defined(c_plusplus) -extern "C" { -#endif - -/* Dictionary data (words and transforms) for 1 possible context */ -typedef struct BrotliEncoderDictionary { - const BrotliDictionary* words; - - /* cut off for fast encoder */ - uint32_t cutoffTransformsCount; - uint64_t cutoffTransforms; - - /* from dictionary_hash.h, for fast encoder */ - const uint16_t* hash_table; - - /* from static_dict_lut.h, for slow encoder */ - const uint16_t* buckets; - const DictWord* dict_words; -} BrotliEncoderDictionary; - -BROTLI_INTERNAL void BrotliInitEncoderDictionary(BrotliEncoderDictionary* dict); - -#if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ -#endif - -#endif /* BROTLI_ENC_ENCODER_DICT_H_ */ +/* Copyright 2017 Google Inc. All Rights Reserved. + + Distributed under MIT license. + See file LICENSE for detail or copy at https://opensource.org/licenses/MIT +*/ + +#ifndef BROTLI_ENC_ENCODER_DICT_H_ +#define BROTLI_ENC_ENCODER_DICT_H_ + +#include "../common/dictionary.h" +#include "../common/platform.h" +#include <brotli/types.h> +#include "./static_dict_lut.h" + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +/* Dictionary data (words and transforms) for 1 possible context */ +typedef struct BrotliEncoderDictionary { + const BrotliDictionary* words; + + /* cut off for fast encoder */ + uint32_t cutoffTransformsCount; + uint64_t cutoffTransforms; + + /* from dictionary_hash.h, for fast encoder */ + const uint16_t* hash_table; + + /* from static_dict_lut.h, for slow encoder */ + const uint16_t* buckets; + const DictWord* dict_words; +} BrotliEncoderDictionary; + +BROTLI_INTERNAL void BrotliInitEncoderDictionary(BrotliEncoderDictionary* dict); + +#if defined(__cplusplus) || defined(c_plusplus) +} /* extern "C" */ +#endif + +#endif /* BROTLI_ENC_ENCODER_DICT_H_ */ diff --git a/contrib/libs/brotli/enc/entropy_encode.c b/contrib/libs/brotli/enc/entropy_encode.c index 97f9dfb82a..223ef662ac 100644 --- a/contrib/libs/brotli/enc/entropy_encode.c +++ b/contrib/libs/brotli/enc/entropy_encode.c @@ -11,7 +11,7 @@ #include <string.h> /* memset */ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -23,7 +23,7 @@ BROTLI_BOOL BrotliSetDepth( int stack[16]; int level = 0; int p = p0; - BROTLI_DCHECK(max_depth <= 15); + BROTLI_DCHECK(max_depth <= 15); stack[0] = -1; while (BROTLI_TRUE) { if (pool[p].index_left_ >= 0) { @@ -66,11 +66,11 @@ static BROTLI_INLINE BROTLI_BOOL SortHuffmanTree( we are not planning to use this with extremely long blocks. See http://en.wikipedia.org/wiki/Huffman_coding */ -void BrotliCreateHuffmanTree(const uint32_t* data, +void BrotliCreateHuffmanTree(const uint32_t* data, const size_t length, const int tree_limit, HuffmanTree* tree, - uint8_t* depth) { + uint8_t* depth) { uint32_t count_limit; HuffmanTree sentinel; InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1); @@ -165,7 +165,7 @@ static void BrotliWriteHuffmanTreeRepetitions( size_t* tree_size, uint8_t* tree, uint8_t* extra_bits_data) { - BROTLI_DCHECK(repetitions > 0); + BROTLI_DCHECK(repetitions > 0); if (previous_value != value) { tree[*tree_size] = value; extra_bits_data[*tree_size] = 0; @@ -371,8 +371,8 @@ void BrotliOptimizeHuffmanCountsForRle(size_t length, uint32_t* counts, } static void DecideOverRleUse(const uint8_t* depth, const size_t length, - BROTLI_BOOL* use_rle_for_non_zero, - BROTLI_BOOL* use_rle_for_zero) { + BROTLI_BOOL* use_rle_for_non_zero, + BROTLI_BOOL* use_rle_for_zero) { size_t total_reps_zero = 0; size_t total_reps_non_zero = 0; size_t count_reps_zero = 1; @@ -454,26 +454,26 @@ void BrotliWriteHuffmanTree(const uint8_t* depth, static uint16_t BrotliReverseBits(size_t num_bits, uint16_t bits) { static const size_t kLut[16] = { /* Pre-reversed 4-bit values. */ - 0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E, - 0x01, 0x09, 0x05, 0x0D, 0x03, 0x0B, 0x07, 0x0F + 0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E, + 0x01, 0x09, 0x05, 0x0D, 0x03, 0x0B, 0x07, 0x0F }; - size_t retval = kLut[bits & 0x0F]; + size_t retval = kLut[bits & 0x0F]; size_t i; for (i = 4; i < num_bits; i += 4) { retval <<= 4; bits = (uint16_t)(bits >> 4); - retval |= kLut[bits & 0x0F]; + retval |= kLut[bits & 0x0F]; } - retval >>= ((0 - num_bits) & 0x03); + retval >>= ((0 - num_bits) & 0x03); return (uint16_t)retval; } /* 0..15 are values for bits */ #define MAX_HUFFMAN_BITS 16 -void BrotliConvertBitDepthsToSymbols(const uint8_t* depth, +void BrotliConvertBitDepthsToSymbols(const uint8_t* depth, size_t len, - uint16_t* bits) { + uint16_t* bits) { /* In Brotli, all bit depths are [1..15] 0 bit depth means that the symbol does not exist. */ uint16_t bl_count[MAX_HUFFMAN_BITS] = { 0 }; diff --git a/contrib/libs/brotli/enc/entropy_encode.h b/contrib/libs/brotli/enc/entropy_encode.h index f23d9c379d..837a4079f4 100644 --- a/contrib/libs/brotli/enc/entropy_encode.h +++ b/contrib/libs/brotli/enc/entropy_encode.h @@ -9,7 +9,7 @@ #ifndef BROTLI_ENC_ENTROPY_ENCODE_H_ #define BROTLI_ENC_ENTROPY_ENCODE_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -46,11 +46,11 @@ BROTLI_INTERNAL BROTLI_BOOL BrotliSetDepth( be at least 2 * length + 1 long. See http://en.wikipedia.org/wiki/Huffman_coding */ -BROTLI_INTERNAL void BrotliCreateHuffmanTree(const uint32_t* data, +BROTLI_INTERNAL void BrotliCreateHuffmanTree(const uint32_t* data, const size_t length, const int tree_limit, HuffmanTree* tree, - uint8_t* depth); + uint8_t* depth); /* Change the population counts in a way that the consequent Huffman tree compression, especially its RLE-part will be more @@ -72,9 +72,9 @@ BROTLI_INTERNAL void BrotliWriteHuffmanTree(const uint8_t* depth, uint8_t* extra_bits_data); /* Get the actual bit values for a tree of bit depths. */ -BROTLI_INTERNAL void BrotliConvertBitDepthsToSymbols(const uint8_t* depth, +BROTLI_INTERNAL void BrotliConvertBitDepthsToSymbols(const uint8_t* depth, size_t len, - uint16_t* bits); + uint16_t* bits); /* Input size optimized Shell sort. */ typedef BROTLI_BOOL (*HuffmanTreeComparator)( diff --git a/contrib/libs/brotli/enc/entropy_encode_static.h b/contrib/libs/brotli/enc/entropy_encode_static.h index 62b99a954c..33ba2bce3e 100644 --- a/contrib/libs/brotli/enc/entropy_encode_static.h +++ b/contrib/libs/brotli/enc/entropy_encode_static.h @@ -10,7 +10,7 @@ #define BROTLI_ENC_ENTROPY_ENCODE_STATIC_H_ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./write_bits.h" @@ -83,7 +83,7 @@ static const uint32_t kCodeLengthBits[18] = { static BROTLI_INLINE void StoreStaticCodeLengthCode( size_t* storage_ix, uint8_t* storage) { BrotliWriteBits( - 40, BROTLI_MAKE_UINT64_T(0x0000FFu, 0x55555554u), storage_ix, storage); + 40, BROTLI_MAKE_UINT64_T(0x0000FFu, 0x55555554u), storage_ix, storage); } static const uint64_t kZeroRepsBits[BROTLI_NUM_COMMAND_SYMBOLS] = { @@ -529,7 +529,7 @@ static const uint16_t kStaticDistanceCodeBits[64] = { static BROTLI_INLINE void StoreStaticDistanceHuffmanTree( size_t* storage_ix, uint8_t* storage) { - BrotliWriteBits(28, 0x0369DC03u, storage_ix, storage); + BrotliWriteBits(28, 0x0369DC03u, storage_ix, storage); } #if defined(__cplusplus) || defined(c_plusplus) diff --git a/contrib/libs/brotli/enc/fast_log.h b/contrib/libs/brotli/enc/fast_log.h index cade1235ad..d994a62018 100644 --- a/contrib/libs/brotli/enc/fast_log.h +++ b/contrib/libs/brotli/enc/fast_log.h @@ -11,7 +11,7 @@ #include <math.h> -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -19,9 +19,9 @@ extern "C" { #endif static BROTLI_INLINE uint32_t Log2FloorNonZero(size_t n) { - /* TODO: generalize and move to platform.h */ -#if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) + /* TODO: generalize and move to platform.h */ +#if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) return 31u ^ (uint32_t)__builtin_clz((uint32_t)n); #else uint32_t result = 0; diff --git a/contrib/libs/brotli/enc/find_match_length.h b/contrib/libs/brotli/enc/find_match_length.h index bc428cffda..f7cf218c25 100644 --- a/contrib/libs/brotli/enc/find_match_length.h +++ b/contrib/libs/brotli/enc/find_match_length.h @@ -9,7 +9,7 @@ #ifndef BROTLI_ENC_FIND_MATCH_LENGTH_H_ #define BROTLI_ENC_FIND_MATCH_LENGTH_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -60,8 +60,8 @@ static BROTLI_INLINE size_t FindMatchLengthWithLimit(const uint8_t* s1, the first non-matching bit and use that to calculate the total length of the match. */ while (s2_ptr <= s2_limit - 4 && - BrotliUnalignedRead32(s2_ptr) == - BrotliUnalignedRead32(s1 + matched)) { + BrotliUnalignedRead32(s2_ptr) == + BrotliUnalignedRead32(s1 + matched)) { s2_ptr += 4; matched += 4; } diff --git a/contrib/libs/brotli/enc/hash.h b/contrib/libs/brotli/enc/hash.h index 8c5a7bb5ad..6e1c4b7df3 100644 --- a/contrib/libs/brotli/enc/hash.h +++ b/contrib/libs/brotli/enc/hash.h @@ -14,9 +14,9 @@ #include "../common/constants.h" #include "../common/dictionary.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> -#include "./encoder_dict.h" +#include "./encoder_dict.h" #include "./fast_log.h" #include "./find_match_length.h" #include "./memory.h" @@ -36,10 +36,10 @@ extern "C" { * * HasherCommon structure * * private structured hasher data, depending on hasher type * * private dynamic hasher data, depending on hasher type and parameters - * - * Using "define" instead of "typedef", because on MSVC __restrict does not work - * on typedef pointer types. */ -#define HasherHandle uint8_t* + * + * Using "define" instead of "typedef", because on MSVC __restrict does not work + * on typedef pointer types. */ +#define HasherHandle uint8_t* typedef struct { BrotliHasherParams params; @@ -76,13 +76,13 @@ typedef struct HasherSearchResult { * There is no effort to ensure that it is a prime, the oddity is enough for this use. * The number has been tuned heuristically against compression benchmarks. */ -static const uint32_t kHashMul32 = 0x1E35A7BD; -static const uint64_t kHashMul64 = BROTLI_MAKE_UINT64_T(0x1E35A7BD, 0x1E35A7BD); +static const uint32_t kHashMul32 = 0x1E35A7BD; +static const uint64_t kHashMul64 = BROTLI_MAKE_UINT64_T(0x1E35A7BD, 0x1E35A7BD); static const uint64_t kHashMul64Long = - BROTLI_MAKE_UINT64_T(0x1FE35A7Bu, 0xD3579BD3u); + BROTLI_MAKE_UINT64_T(0x1FE35A7Bu, 0xD3579BD3u); static BROTLI_INLINE uint32_t Hash14(const uint8_t* data) { - uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; + uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ return h >> (32 - 14); @@ -149,35 +149,35 @@ static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance( } static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem( - const BrotliEncoderDictionary* dictionary, size_t item, - const uint8_t* data, size_t max_length, size_t max_backward, - size_t max_distance, HasherSearchResult* out) { + const BrotliEncoderDictionary* dictionary, size_t item, + const uint8_t* data, size_t max_length, size_t max_backward, + size_t max_distance, HasherSearchResult* out) { size_t len; - size_t word_idx; + size_t word_idx; size_t offset; size_t matchlen; size_t backward; score_t score; len = item & 0x1F; - word_idx = item >> 5; - offset = dictionary->words->offsets_by_length[len] + len * word_idx; + word_idx = item >> 5; + offset = dictionary->words->offsets_by_length[len] + len * word_idx; if (len > max_length) { return BROTLI_FALSE; } matchlen = - FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len); - if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) { + FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len); + if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) { return BROTLI_FALSE; } { size_t cut = len - matchlen; - size_t transform_id = (cut << 2) + - (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F); - backward = max_backward + 1 + word_idx + - (transform_id << dictionary->words->size_bits_by_length[len]); + size_t transform_id = (cut << 2) + + (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F); + backward = max_backward + 1 + word_idx + + (transform_id << dictionary->words->size_bits_by_length[len]); } - if (backward > max_distance) { + if (backward > max_distance) { return BROTLI_FALSE; } score = BackwardReferenceScore(matchlen, backward); @@ -192,10 +192,10 @@ static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem( } static BROTLI_INLINE void SearchInStaticDictionary( - const BrotliEncoderDictionary* dictionary, + const BrotliEncoderDictionary* dictionary, HasherHandle handle, const uint8_t* data, size_t max_length, - size_t max_backward, size_t max_distance, - HasherSearchResult* out, BROTLI_BOOL shallow) { + size_t max_backward, size_t max_distance, + HasherSearchResult* out, BROTLI_BOOL shallow) { size_t key; size_t i; HasherCommon* self = GetHasherCommon(handle); @@ -204,12 +204,12 @@ static BROTLI_INLINE void SearchInStaticDictionary( } key = Hash14(data) << 1; for (i = 0; i < (shallow ? 1u : 2u); ++i, ++key) { - size_t item = dictionary->hash_table[key]; + size_t item = dictionary->hash_table[key]; self->dict_num_lookups++; if (item != 0) { BROTLI_BOOL item_matches = TestStaticDictionaryItem( - dictionary, item, data, - max_length, max_backward, max_distance, out); + dictionary, item, data, + max_length, max_backward, max_distance, out); if (item_matches) { self->dict_num_matches++; } @@ -344,57 +344,57 @@ static BROTLI_INLINE size_t BackwardMatchLengthCode(const BackwardMatch* self) { #undef BUCKET_BITS #undef HASHER -/* fast large window hashers */ - -#define HASHER() HROLLING_FAST -#define CHUNKLEN 32 -#define JUMP 4 -#define NUMBUCKETS 16777216 -#define MASK ((NUMBUCKETS * 64) - 1) -#include "./hash_rolling_inc.h" /* NOLINT(build/include) */ -#undef JUMP -#undef HASHER - - -#define HASHER() HROLLING -#define JUMP 1 -#include "./hash_rolling_inc.h" /* NOLINT(build/include) */ -#undef MASK -#undef NUMBUCKETS -#undef JUMP -#undef CHUNKLEN -#undef HASHER - -#define HASHER() H35 -#define HASHER_A H3 -#define HASHER_B HROLLING_FAST -#include "./hash_composite_inc.h" /* NOLINT(build/include) */ -#undef HASHER_A -#undef HASHER_B -#undef HASHER - -#define HASHER() H55 -#define HASHER_A H54 -#define HASHER_B HROLLING_FAST -#include "./hash_composite_inc.h" /* NOLINT(build/include) */ -#undef HASHER_A -#undef HASHER_B -#undef HASHER - -#define HASHER() H65 -#define HASHER_A H6 -#define HASHER_B HROLLING -#include "./hash_composite_inc.h" /* NOLINT(build/include) */ -#undef HASHER_A -#undef HASHER_B -#undef HASHER - +/* fast large window hashers */ + +#define HASHER() HROLLING_FAST +#define CHUNKLEN 32 +#define JUMP 4 +#define NUMBUCKETS 16777216 +#define MASK ((NUMBUCKETS * 64) - 1) +#include "./hash_rolling_inc.h" /* NOLINT(build/include) */ +#undef JUMP +#undef HASHER + + +#define HASHER() HROLLING +#define JUMP 1 +#include "./hash_rolling_inc.h" /* NOLINT(build/include) */ +#undef MASK +#undef NUMBUCKETS +#undef JUMP +#undef CHUNKLEN +#undef HASHER + +#define HASHER() H35 +#define HASHER_A H3 +#define HASHER_B HROLLING_FAST +#include "./hash_composite_inc.h" /* NOLINT(build/include) */ +#undef HASHER_A +#undef HASHER_B +#undef HASHER + +#define HASHER() H55 +#define HASHER_A H54 +#define HASHER_B HROLLING_FAST +#include "./hash_composite_inc.h" /* NOLINT(build/include) */ +#undef HASHER_A +#undef HASHER_B +#undef HASHER + +#define HASHER() H65 +#define HASHER_A H6 +#define HASHER_B HROLLING +#include "./hash_composite_inc.h" /* NOLINT(build/include) */ +#undef HASHER_A +#undef HASHER_B +#undef HASHER + #undef FN #undef CAT #undef EXPAND_CAT -#define FOR_GENERIC_HASHERS(H) H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54)\ - H(35) H(55) H(65) +#define FOR_GENERIC_HASHERS(H) H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54)\ + H(35) H(55) H(65) #define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10) static BROTLI_INLINE void DestroyHasher( diff --git a/contrib/libs/brotli/enc/hash_composite_inc.h b/contrib/libs/brotli/enc/hash_composite_inc.h index b266aa2f8d..8bb927e70b 100644 --- a/contrib/libs/brotli/enc/hash_composite_inc.h +++ b/contrib/libs/brotli/enc/hash_composite_inc.h @@ -1,136 +1,136 @@ -/* NOLINT(build/header_guard) */ -/* Copyright 2018 Google Inc. All Rights Reserved. - - Distributed under MIT license. - See file LICENSE for detail or copy at https://opensource.org/licenses/MIT -*/ - -/* template parameters: FN, HASHER_A, HASHER_B */ - -/* Composite hasher: This hasher allows to combine two other hashers, HASHER_A - and HASHER_B. */ - -#define HashComposite HASHER() - -#define FN_A(X) EXPAND_CAT(X, HASHER_A) -#define FN_B(X) EXPAND_CAT(X, HASHER_B) - -static BROTLI_INLINE size_t FN(HashTypeLength)(void) { - size_t a = FN_A(HashTypeLength)(); - size_t b = FN_B(HashTypeLength)(); - return a > b ? a : b; -} - -static BROTLI_INLINE size_t FN(StoreLookahead)(void) { - size_t a = FN_A(StoreLookahead)(); - size_t b = FN_B(StoreLookahead)(); - return a > b ? a : b; -} - -typedef struct HashComposite { - HasherHandle ha; - HasherHandle hb; - const BrotliEncoderParams* params; -} HashComposite; - -static BROTLI_INLINE HashComposite* FN(Self)(HasherHandle handle) { - return (HashComposite*)&(GetHasherCommon(handle)[1]); -} - -static void FN(Initialize)( - HasherHandle handle, const BrotliEncoderParams* params) { - HashComposite* self = FN(Self)(handle); - self->ha = 0; - self->hb = 0; - self->params = params; - /* TODO: Initialize of the hashers is defered to Prepare (and params - remembered here) because we don't get the one_shot and input_size params - here that are needed to know the memory size of them. Instead provide - those params to all hashers FN(Initialize) */ -} - -static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot, - size_t input_size, const uint8_t* data) { - HashComposite* self = FN(Self)(handle); - if (!self->ha) { - HasherCommon* common_a; - HasherCommon* common_b; - - self->ha = handle + sizeof(HasherCommon) + sizeof(HashComposite); - common_a = (HasherCommon*)self->ha; - common_a->params = self->params->hasher; - common_a->is_prepared_ = BROTLI_FALSE; - common_a->dict_num_lookups = 0; - common_a->dict_num_matches = 0; - FN_A(Initialize)(self->ha, self->params); - - self->hb = self->ha + sizeof(HasherCommon) + FN_A(HashMemAllocInBytes)( - self->params, one_shot, input_size); - common_b = (HasherCommon*)self->hb; - common_b->params = self->params->hasher; - common_b->is_prepared_ = BROTLI_FALSE; - common_b->dict_num_lookups = 0; - common_b->dict_num_matches = 0; - FN_B(Initialize)(self->hb, self->params); - } - FN_A(Prepare)(self->ha, one_shot, input_size, data); - FN_B(Prepare)(self->hb, one_shot, input_size, data); -} - -static BROTLI_INLINE size_t FN(HashMemAllocInBytes)( - const BrotliEncoderParams* params, BROTLI_BOOL one_shot, - size_t input_size) { - return sizeof(HashComposite) + 2 * sizeof(HasherCommon) + - FN_A(HashMemAllocInBytes)(params, one_shot, input_size) + - FN_B(HashMemAllocInBytes)(params, one_shot, input_size); -} - -static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle, - const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) { - HashComposite* self = FN(Self)(handle); - FN_A(Store)(self->ha, data, mask, ix); - FN_B(Store)(self->hb, data, mask, ix); -} - -static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix_start, - const size_t ix_end) { - HashComposite* self = FN(Self)(handle); - FN_A(StoreRange)(self->ha, data, mask, ix_start, ix_end); - FN_B(StoreRange)(self->hb, data, mask, ix_start, ix_end); -} - -static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle, - size_t num_bytes, size_t position, const uint8_t* ringbuffer, - size_t ring_buffer_mask) { - HashComposite* self = FN(Self)(handle); - FN_A(StitchToPreviousBlock)(self->ha, num_bytes, position, ringbuffer, - ring_buffer_mask); - FN_B(StitchToPreviousBlock)(self->hb, num_bytes, position, ringbuffer, - ring_buffer_mask); -} - -static BROTLI_INLINE void FN(PrepareDistanceCache)( - HasherHandle handle, int* BROTLI_RESTRICT distance_cache) { - HashComposite* self = FN(Self)(handle); - FN_A(PrepareDistanceCache)(self->ha, distance_cache); - FN_B(PrepareDistanceCache)(self->hb, distance_cache); -} - -static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, - const BrotliEncoderDictionary* dictionary, - const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, - const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, - const size_t max_length, const size_t max_backward, - const size_t gap, const size_t max_distance, - HasherSearchResult* BROTLI_RESTRICT out) { - HashComposite* self = FN(Self)(handle); - FN_A(FindLongestMatch)(self->ha, dictionary, data, ring_buffer_mask, - distance_cache, cur_ix, max_length, max_backward, gap, - max_distance, out); - FN_B(FindLongestMatch)(self->hb, dictionary, data, ring_buffer_mask, - distance_cache, cur_ix, max_length, max_backward, gap, - max_distance, out); -} - -#undef HashComposite +/* NOLINT(build/header_guard) */ +/* Copyright 2018 Google Inc. All Rights Reserved. + + Distributed under MIT license. + See file LICENSE for detail or copy at https://opensource.org/licenses/MIT +*/ + +/* template parameters: FN, HASHER_A, HASHER_B */ + +/* Composite hasher: This hasher allows to combine two other hashers, HASHER_A + and HASHER_B. */ + +#define HashComposite HASHER() + +#define FN_A(X) EXPAND_CAT(X, HASHER_A) +#define FN_B(X) EXPAND_CAT(X, HASHER_B) + +static BROTLI_INLINE size_t FN(HashTypeLength)(void) { + size_t a = FN_A(HashTypeLength)(); + size_t b = FN_B(HashTypeLength)(); + return a > b ? a : b; +} + +static BROTLI_INLINE size_t FN(StoreLookahead)(void) { + size_t a = FN_A(StoreLookahead)(); + size_t b = FN_B(StoreLookahead)(); + return a > b ? a : b; +} + +typedef struct HashComposite { + HasherHandle ha; + HasherHandle hb; + const BrotliEncoderParams* params; +} HashComposite; + +static BROTLI_INLINE HashComposite* FN(Self)(HasherHandle handle) { + return (HashComposite*)&(GetHasherCommon(handle)[1]); +} + +static void FN(Initialize)( + HasherHandle handle, const BrotliEncoderParams* params) { + HashComposite* self = FN(Self)(handle); + self->ha = 0; + self->hb = 0; + self->params = params; + /* TODO: Initialize of the hashers is defered to Prepare (and params + remembered here) because we don't get the one_shot and input_size params + here that are needed to know the memory size of them. Instead provide + those params to all hashers FN(Initialize) */ +} + +static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot, + size_t input_size, const uint8_t* data) { + HashComposite* self = FN(Self)(handle); + if (!self->ha) { + HasherCommon* common_a; + HasherCommon* common_b; + + self->ha = handle + sizeof(HasherCommon) + sizeof(HashComposite); + common_a = (HasherCommon*)self->ha; + common_a->params = self->params->hasher; + common_a->is_prepared_ = BROTLI_FALSE; + common_a->dict_num_lookups = 0; + common_a->dict_num_matches = 0; + FN_A(Initialize)(self->ha, self->params); + + self->hb = self->ha + sizeof(HasherCommon) + FN_A(HashMemAllocInBytes)( + self->params, one_shot, input_size); + common_b = (HasherCommon*)self->hb; + common_b->params = self->params->hasher; + common_b->is_prepared_ = BROTLI_FALSE; + common_b->dict_num_lookups = 0; + common_b->dict_num_matches = 0; + FN_B(Initialize)(self->hb, self->params); + } + FN_A(Prepare)(self->ha, one_shot, input_size, data); + FN_B(Prepare)(self->hb, one_shot, input_size, data); +} + +static BROTLI_INLINE size_t FN(HashMemAllocInBytes)( + const BrotliEncoderParams* params, BROTLI_BOOL one_shot, + size_t input_size) { + return sizeof(HashComposite) + 2 * sizeof(HasherCommon) + + FN_A(HashMemAllocInBytes)(params, one_shot, input_size) + + FN_B(HashMemAllocInBytes)(params, one_shot, input_size); +} + +static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle, + const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) { + HashComposite* self = FN(Self)(handle); + FN_A(Store)(self->ha, data, mask, ix); + FN_B(Store)(self->hb, data, mask, ix); +} + +static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, + const uint8_t* data, const size_t mask, const size_t ix_start, + const size_t ix_end) { + HashComposite* self = FN(Self)(handle); + FN_A(StoreRange)(self->ha, data, mask, ix_start, ix_end); + FN_B(StoreRange)(self->hb, data, mask, ix_start, ix_end); +} + +static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle, + size_t num_bytes, size_t position, const uint8_t* ringbuffer, + size_t ring_buffer_mask) { + HashComposite* self = FN(Self)(handle); + FN_A(StitchToPreviousBlock)(self->ha, num_bytes, position, ringbuffer, + ring_buffer_mask); + FN_B(StitchToPreviousBlock)(self->hb, num_bytes, position, ringbuffer, + ring_buffer_mask); +} + +static BROTLI_INLINE void FN(PrepareDistanceCache)( + HasherHandle handle, int* BROTLI_RESTRICT distance_cache) { + HashComposite* self = FN(Self)(handle); + FN_A(PrepareDistanceCache)(self->ha, distance_cache); + FN_B(PrepareDistanceCache)(self->hb, distance_cache); +} + +static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, + const BrotliEncoderDictionary* dictionary, + const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, + const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, + const size_t max_length, const size_t max_backward, + const size_t gap, const size_t max_distance, + HasherSearchResult* BROTLI_RESTRICT out) { + HashComposite* self = FN(Self)(handle); + FN_A(FindLongestMatch)(self->ha, dictionary, data, ring_buffer_mask, + distance_cache, cur_ix, max_length, max_backward, gap, + max_distance, out); + FN_B(FindLongestMatch)(self->hb, dictionary, data, ring_buffer_mask, + distance_cache, cur_ix, max_length, max_backward, gap, + max_distance, out); +} + +#undef HashComposite diff --git a/contrib/libs/brotli/enc/hash_forgetful_chain_inc.h b/contrib/libs/brotli/enc/hash_forgetful_chain_inc.h index 41cb3ff03a..201334624a 100644 --- a/contrib/libs/brotli/enc/hash_forgetful_chain_inc.h +++ b/contrib/libs/brotli/enc/hash_forgetful_chain_inc.h @@ -28,8 +28,8 @@ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; } static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; } /* HashBytes is the function that chooses the bucket to place the address in.*/ -static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* data) { - const uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; +static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* data) { + const uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ return h >> (32 - BUCKET_BITS); @@ -115,7 +115,7 @@ static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle, } static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix_start, + const uint8_t* data, const size_t mask, const size_t ix_start, const size_t ix_end) { size_t i; for (i = ix_start; i < ix_end; ++i) { @@ -154,12 +154,12 @@ static BROTLI_INLINE void FN(PrepareDistanceCache)( Writes the best match into |out|. |out|->score is updated only if a better match is found. */ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, - const BrotliEncoderDictionary* dictionary, + const BrotliEncoderDictionary* dictionary, const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, const size_t max_length, const size_t max_backward, - const size_t gap, const size_t max_distance, - HasherSearchResult* BROTLI_RESTRICT out) { + const size_t gap, const size_t max_distance, + HasherSearchResult* BROTLI_RESTRICT out) { HashForgetfulChain* self = FN(Self)(handle); const size_t cur_ix_masked = cur_ix & ring_buffer_mask; /* Don't accept a short copy from far away. */ @@ -241,9 +241,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, FN(Store)(handle, data, ring_buffer_mask, cur_ix); } if (out->score == min_score) { - SearchInStaticDictionary(dictionary, - handle, &data[cur_ix_masked], max_length, max_backward + gap, - max_distance, out, BROTLI_FALSE); + SearchInStaticDictionary(dictionary, + handle, &data[cur_ix_masked], max_length, max_backward + gap, + max_distance, out, BROTLI_FALSE); } } diff --git a/contrib/libs/brotli/enc/hash_longest_match64_inc.h b/contrib/libs/brotli/enc/hash_longest_match64_inc.h index cb953a644f..48e57684bb 100644 --- a/contrib/libs/brotli/enc/hash_longest_match64_inc.h +++ b/contrib/libs/brotli/enc/hash_longest_match64_inc.h @@ -20,7 +20,7 @@ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; } static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; } /* HashBytes is the function that chooses the bucket to place the address in. */ -static BROTLI_INLINE uint32_t FN(HashBytes)(const uint8_t* data, +static BROTLI_INLINE uint32_t FN(HashBytes)(const uint8_t* data, const uint64_t mask, const int shift) { const uint64_t h = (BROTLI_UNALIGNED_LOAD64LE(data) & mask) * kHashMul64Long; @@ -105,7 +105,7 @@ static BROTLI_INLINE size_t FN(HashMemAllocInBytes)( /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and store the value of ix at that position. */ -static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data, +static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data, const size_t mask, const size_t ix) { HashLongestMatch* self = FN(Self)(handle); uint16_t* num = FN(Num)(self); @@ -119,7 +119,7 @@ static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data, } static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix_start, + const uint8_t* data, const size_t mask, const size_t ix_start, const size_t ix_end) { size_t i; for (i = ix_start; i < ix_end; ++i) { @@ -158,11 +158,11 @@ static BROTLI_INLINE void FN(PrepareDistanceCache)( Writes the best match into |out|. |out|->score is updated only if a better match is found. */ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, - const BrotliEncoderDictionary* dictionary, + const BrotliEncoderDictionary* dictionary, const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, - const size_t max_length, const size_t max_backward, - const size_t gap, const size_t max_distance, + const size_t max_length, const size_t max_backward, + const size_t gap, const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) { HasherCommon* common = GetHasherCommon(handle); HashLongestMatch* self = FN(Self)(handle); @@ -258,9 +258,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, ++num[key]; } if (min_score == out->score) { - SearchInStaticDictionary(dictionary, - handle, &data[cur_ix_masked], max_length, max_backward + gap, - max_distance, out, BROTLI_FALSE); + SearchInStaticDictionary(dictionary, + handle, &data[cur_ix_masked], max_length, max_backward + gap, + max_distance, out, BROTLI_FALSE); } } diff --git a/contrib/libs/brotli/enc/hash_longest_match_inc.h b/contrib/libs/brotli/enc/hash_longest_match_inc.h index 457f5a9ed2..6168b06bf6 100644 --- a/contrib/libs/brotli/enc/hash_longest_match_inc.h +++ b/contrib/libs/brotli/enc/hash_longest_match_inc.h @@ -20,8 +20,8 @@ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; } static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; } /* HashBytes is the function that chooses the bucket to place the address in. */ -static uint32_t FN(HashBytes)(const uint8_t* data, const int shift) { - uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; +static uint32_t FN(HashBytes)(const uint8_t* data, const int shift) { + uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ return (uint32_t)(h >> shift); @@ -112,7 +112,7 @@ static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data, } static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix_start, + const uint8_t* data, const size_t mask, const size_t ix_start, const size_t ix_end) { size_t i; for (i = ix_start; i < ix_end; ++i) { @@ -151,11 +151,11 @@ static BROTLI_INLINE void FN(PrepareDistanceCache)( Writes the best match into |out|. |out|->score is updated only if a better match is found. */ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, - const BrotliEncoderDictionary* dictionary, + const BrotliEncoderDictionary* dictionary, const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, - const size_t max_length, const size_t max_backward, - const size_t gap, const size_t max_distance, + const size_t max_length, const size_t max_backward, + const size_t gap, const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) { HasherCommon* common = GetHasherCommon(handle); HashLongestMatch* self = FN(Self)(handle); @@ -250,9 +250,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, ++num[key]; } if (min_score == out->score) { - SearchInStaticDictionary(dictionary, - handle, &data[cur_ix_masked], max_length, max_backward + gap, - max_distance, out, BROTLI_FALSE); + SearchInStaticDictionary(dictionary, + handle, &data[cur_ix_masked], max_length, max_backward + gap, + max_distance, out, BROTLI_FALSE); } } diff --git a/contrib/libs/brotli/enc/hash_longest_match_quickly_inc.h b/contrib/libs/brotli/enc/hash_longest_match_quickly_inc.h index a7b9639feb..5a6b7c4a59 100644 --- a/contrib/libs/brotli/enc/hash_longest_match_quickly_inc.h +++ b/contrib/libs/brotli/enc/hash_longest_match_quickly_inc.h @@ -81,7 +81,7 @@ static BROTLI_INLINE size_t FN(HashMemAllocInBytes)( Compute a hash from these, and store the value somewhere within [ix .. ix+3]. */ static BROTLI_INLINE void FN(Store)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix) { + const uint8_t* data, const size_t mask, const size_t ix) { const uint32_t key = FN(HashBytes)(&data[ix & mask]); /* Wiggle the value with the bucket sweep range. */ const uint32_t off = (ix >> 3) % BUCKET_SWEEP; @@ -89,7 +89,7 @@ static BROTLI_INLINE void FN(Store)(HasherHandle handle, } static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix_start, + const uint8_t* data, const size_t mask, const size_t ix_start, const size_t ix_end) { size_t i; for (i = ix_start; i < ix_end; ++i) { @@ -125,12 +125,12 @@ static BROTLI_INLINE void FN(PrepareDistanceCache)( Writes the best match into |out|. |out|->score is updated only if a better match is found. */ static BROTLI_INLINE void FN(FindLongestMatch)( - HasherHandle handle, const BrotliEncoderDictionary* dictionary, - const uint8_t* BROTLI_RESTRICT data, + HasherHandle handle, const BrotliEncoderDictionary* dictionary, + const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, const size_t max_length, const size_t max_backward, - const size_t gap, const size_t max_distance, - HasherSearchResult* BROTLI_RESTRICT out) { + const size_t gap, const size_t max_distance, + HasherSearchResult* BROTLI_RESTRICT out) { HashLongestMatchQuickly* self = FN(Self)(handle); const size_t best_len_in = out->len; const size_t cur_ix_masked = cur_ix & ring_buffer_mask; @@ -192,7 +192,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( } } } else { - uint32_t* bucket = self->buckets_ + key; + uint32_t* bucket = self->buckets_ + key; int i; prev_ix = *bucket++; for (i = 0; i < BUCKET_SWEEP; ++i, prev_ix = *bucket++) { @@ -222,9 +222,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)( } } if (USE_DICTIONARY && min_score == out->score) { - SearchInStaticDictionary(dictionary, - handle, &data[cur_ix_masked], max_length, max_backward + gap, - max_distance, out, BROTLI_TRUE); + SearchInStaticDictionary(dictionary, + handle, &data[cur_ix_masked], max_length, max_backward + gap, + max_distance, out, BROTLI_TRUE); } self->buckets_[key + ((cur_ix >> 3) % BUCKET_SWEEP)] = (uint32_t)cur_ix; } diff --git a/contrib/libs/brotli/enc/hash_rolling_inc.h b/contrib/libs/brotli/enc/hash_rolling_inc.h index 17f8a408e2..78946dd533 100644 --- a/contrib/libs/brotli/enc/hash_rolling_inc.h +++ b/contrib/libs/brotli/enc/hash_rolling_inc.h @@ -1,216 +1,216 @@ -/* NOLINT(build/header_guard) */ -/* Copyright 2018 Google Inc. All Rights Reserved. - - Distributed under MIT license. - See file LICENSE for detail or copy at https://opensource.org/licenses/MIT -*/ - -/* template parameters: FN, JUMP, NUMBUCKETS, MASK, CHUNKLEN */ -/* NUMBUCKETS / (MASK + 1) = probability of storing and using hash code. */ -/* JUMP = skip bytes for speedup */ - -/* Rolling hash for long distance long string matches. Stores one position - per bucket, bucket key is computed over a long region. */ - -#define HashRolling HASHER() - -static const uint32_t FN(kRollingHashMul32) = 69069; -static const uint32_t FN(kInvalidPos) = 0xffffffff; - -/* This hasher uses a longer forward length, but returning a higher value here - will hurt compression by the main hasher when combined with a composite - hasher. The hasher tests for forward itself instead. */ -static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; } -static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; } - -/* Computes a code from a single byte. A lookup table of 256 values could be - used, but simply adding 1 works about as good. */ -static uint32_t FN(HashByte)(uint8_t byte) { - return (uint32_t)byte + 1u; -} - -static uint32_t FN(HashRollingFunctionInitial)(uint32_t state, uint8_t add, - uint32_t factor) { - return (uint32_t)(factor * state + FN(HashByte)(add)); -} - -static uint32_t FN(HashRollingFunction)(uint32_t state, uint8_t add, - uint8_t rem, uint32_t factor, - uint32_t factor_remove) { - return (uint32_t)(factor * state + - FN(HashByte)(add) - factor_remove * FN(HashByte)(rem)); -} - -typedef struct HashRolling { - uint32_t state; - uint32_t* table; - size_t next_ix; - - uint32_t chunk_len; - uint32_t factor; - uint32_t factor_remove; -} HashRolling; - -static BROTLI_INLINE HashRolling* FN(Self)(HasherHandle handle) { - return (HashRolling*)&(GetHasherCommon(handle)[1]); -} - -static void FN(Initialize)( - HasherHandle handle, const BrotliEncoderParams* params) { - HashRolling* self = FN(Self)(handle); - size_t i; - self->state = 0; - self->next_ix = 0; - - self->factor = FN(kRollingHashMul32); - - /* Compute the factor of the oldest byte to remove: factor**steps modulo - 0xffffffff (the multiplications rely on 32-bit overflow) */ - self->factor_remove = 1; - for (i = 0; i < CHUNKLEN; i += JUMP) { - self->factor_remove *= self->factor; - } - - self->table = (uint32_t*)((HasherHandle)self + sizeof(HashRolling)); - for (i = 0; i < NUMBUCKETS; i++) { - self->table[i] = FN(kInvalidPos); - } - - BROTLI_UNUSED(params); -} - -static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot, - size_t input_size, const uint8_t* data) { - HashRolling* self = FN(Self)(handle); - size_t i; - /* Too small size, cannot use this hasher. */ - if (input_size < CHUNKLEN) return; - self->state = 0; - for (i = 0; i < CHUNKLEN; i += JUMP) { - self->state = FN(HashRollingFunctionInitial)( - self->state, data[i], self->factor); - } - BROTLI_UNUSED(one_shot); -} - -static BROTLI_INLINE size_t FN(HashMemAllocInBytes)( - const BrotliEncoderParams* params, BROTLI_BOOL one_shot, - size_t input_size) { - return sizeof(HashRolling) + NUMBUCKETS * sizeof(uint32_t); - BROTLI_UNUSED(params); - BROTLI_UNUSED(one_shot); - BROTLI_UNUSED(input_size); -} - -static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle, - const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) { - BROTLI_UNUSED(handle); - BROTLI_UNUSED(data); - BROTLI_UNUSED(mask); - BROTLI_UNUSED(ix); -} - -static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix_start, - const size_t ix_end) { - BROTLI_UNUSED(handle); - BROTLI_UNUSED(data); - BROTLI_UNUSED(mask); - BROTLI_UNUSED(ix_start); - BROTLI_UNUSED(ix_end); -} - -static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle, - size_t num_bytes, size_t position, const uint8_t* ringbuffer, - size_t ring_buffer_mask) { - /* In this case we must re-initialize the hasher from scratch from the - current position. */ - HashRolling* self = FN(Self)(handle); - size_t position_masked; - size_t available = num_bytes; - if ((position & (JUMP - 1)) != 0) { - size_t diff = JUMP - (position & (JUMP - 1)); - available = (diff > available) ? 0 : (available - diff); - position += diff; - } - position_masked = position & ring_buffer_mask; - /* wrapping around ringbuffer not handled. */ - if (available > ring_buffer_mask - position_masked) { - available = ring_buffer_mask - position_masked; - } - - FN(Prepare)(handle, BROTLI_FALSE, available, - ringbuffer + (position & ring_buffer_mask)); - self->next_ix = position; - BROTLI_UNUSED(num_bytes); -} - -static BROTLI_INLINE void FN(PrepareDistanceCache)( - HasherHandle handle, int* BROTLI_RESTRICT distance_cache) { - BROTLI_UNUSED(handle); - BROTLI_UNUSED(distance_cache); -} - -static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, - const BrotliEncoderDictionary* dictionary, - const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, - const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, - const size_t max_length, const size_t max_backward, - const size_t gap, const size_t max_distance, - HasherSearchResult* BROTLI_RESTRICT out) { - HashRolling* self = FN(Self)(handle); - const size_t cur_ix_masked = cur_ix & ring_buffer_mask; - size_t pos = self->next_ix; - - if ((cur_ix & (JUMP - 1)) != 0) return; - - /* Not enough lookahead */ - if (max_length < CHUNKLEN) return; - - for (pos = self->next_ix; pos <= cur_ix; pos += JUMP) { - uint32_t code = self->state & MASK; - - uint8_t rem = data[pos & ring_buffer_mask]; - uint8_t add = data[(pos + CHUNKLEN) & ring_buffer_mask]; - size_t found_ix = FN(kInvalidPos); - - self->state = FN(HashRollingFunction)( - self->state, add, rem, self->factor, self->factor_remove); - - if (code < NUMBUCKETS) { - found_ix = self->table[code]; - self->table[code] = (uint32_t)pos; - if (pos == cur_ix && found_ix != FN(kInvalidPos)) { - /* The cast to 32-bit makes backward distances up to 4GB work even - if cur_ix is above 4GB, despite using 32-bit values in the table. */ - size_t backward = (uint32_t)(cur_ix - found_ix); - if (backward <= max_backward) { - const size_t found_ix_masked = found_ix & ring_buffer_mask; - const size_t len = FindMatchLengthWithLimit(&data[found_ix_masked], - &data[cur_ix_masked], - max_length); - if (len >= 4 && len > out->len) { - score_t score = BackwardReferenceScore(len, backward); - if (score > out->score) { - out->len = len; - out->distance = backward; - out->score = score; - out->len_code_delta = 0; - } - } - } - } - } - } - - self->next_ix = cur_ix + JUMP; - - /* NOTE: this hasher does not search in the dictionary. It is used as - backup-hasher, the main hasher already searches in it. */ - BROTLI_UNUSED(dictionary); - BROTLI_UNUSED(distance_cache); - BROTLI_UNUSED(gap); - BROTLI_UNUSED(max_distance); -} - -#undef HashRolling +/* NOLINT(build/header_guard) */ +/* Copyright 2018 Google Inc. All Rights Reserved. + + Distributed under MIT license. + See file LICENSE for detail or copy at https://opensource.org/licenses/MIT +*/ + +/* template parameters: FN, JUMP, NUMBUCKETS, MASK, CHUNKLEN */ +/* NUMBUCKETS / (MASK + 1) = probability of storing and using hash code. */ +/* JUMP = skip bytes for speedup */ + +/* Rolling hash for long distance long string matches. Stores one position + per bucket, bucket key is computed over a long region. */ + +#define HashRolling HASHER() + +static const uint32_t FN(kRollingHashMul32) = 69069; +static const uint32_t FN(kInvalidPos) = 0xffffffff; + +/* This hasher uses a longer forward length, but returning a higher value here + will hurt compression by the main hasher when combined with a composite + hasher. The hasher tests for forward itself instead. */ +static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; } +static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; } + +/* Computes a code from a single byte. A lookup table of 256 values could be + used, but simply adding 1 works about as good. */ +static uint32_t FN(HashByte)(uint8_t byte) { + return (uint32_t)byte + 1u; +} + +static uint32_t FN(HashRollingFunctionInitial)(uint32_t state, uint8_t add, + uint32_t factor) { + return (uint32_t)(factor * state + FN(HashByte)(add)); +} + +static uint32_t FN(HashRollingFunction)(uint32_t state, uint8_t add, + uint8_t rem, uint32_t factor, + uint32_t factor_remove) { + return (uint32_t)(factor * state + + FN(HashByte)(add) - factor_remove * FN(HashByte)(rem)); +} + +typedef struct HashRolling { + uint32_t state; + uint32_t* table; + size_t next_ix; + + uint32_t chunk_len; + uint32_t factor; + uint32_t factor_remove; +} HashRolling; + +static BROTLI_INLINE HashRolling* FN(Self)(HasherHandle handle) { + return (HashRolling*)&(GetHasherCommon(handle)[1]); +} + +static void FN(Initialize)( + HasherHandle handle, const BrotliEncoderParams* params) { + HashRolling* self = FN(Self)(handle); + size_t i; + self->state = 0; + self->next_ix = 0; + + self->factor = FN(kRollingHashMul32); + + /* Compute the factor of the oldest byte to remove: factor**steps modulo + 0xffffffff (the multiplications rely on 32-bit overflow) */ + self->factor_remove = 1; + for (i = 0; i < CHUNKLEN; i += JUMP) { + self->factor_remove *= self->factor; + } + + self->table = (uint32_t*)((HasherHandle)self + sizeof(HashRolling)); + for (i = 0; i < NUMBUCKETS; i++) { + self->table[i] = FN(kInvalidPos); + } + + BROTLI_UNUSED(params); +} + +static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot, + size_t input_size, const uint8_t* data) { + HashRolling* self = FN(Self)(handle); + size_t i; + /* Too small size, cannot use this hasher. */ + if (input_size < CHUNKLEN) return; + self->state = 0; + for (i = 0; i < CHUNKLEN; i += JUMP) { + self->state = FN(HashRollingFunctionInitial)( + self->state, data[i], self->factor); + } + BROTLI_UNUSED(one_shot); +} + +static BROTLI_INLINE size_t FN(HashMemAllocInBytes)( + const BrotliEncoderParams* params, BROTLI_BOOL one_shot, + size_t input_size) { + return sizeof(HashRolling) + NUMBUCKETS * sizeof(uint32_t); + BROTLI_UNUSED(params); + BROTLI_UNUSED(one_shot); + BROTLI_UNUSED(input_size); +} + +static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle, + const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) { + BROTLI_UNUSED(handle); + BROTLI_UNUSED(data); + BROTLI_UNUSED(mask); + BROTLI_UNUSED(ix); +} + +static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, + const uint8_t* data, const size_t mask, const size_t ix_start, + const size_t ix_end) { + BROTLI_UNUSED(handle); + BROTLI_UNUSED(data); + BROTLI_UNUSED(mask); + BROTLI_UNUSED(ix_start); + BROTLI_UNUSED(ix_end); +} + +static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle, + size_t num_bytes, size_t position, const uint8_t* ringbuffer, + size_t ring_buffer_mask) { + /* In this case we must re-initialize the hasher from scratch from the + current position. */ + HashRolling* self = FN(Self)(handle); + size_t position_masked; + size_t available = num_bytes; + if ((position & (JUMP - 1)) != 0) { + size_t diff = JUMP - (position & (JUMP - 1)); + available = (diff > available) ? 0 : (available - diff); + position += diff; + } + position_masked = position & ring_buffer_mask; + /* wrapping around ringbuffer not handled. */ + if (available > ring_buffer_mask - position_masked) { + available = ring_buffer_mask - position_masked; + } + + FN(Prepare)(handle, BROTLI_FALSE, available, + ringbuffer + (position & ring_buffer_mask)); + self->next_ix = position; + BROTLI_UNUSED(num_bytes); +} + +static BROTLI_INLINE void FN(PrepareDistanceCache)( + HasherHandle handle, int* BROTLI_RESTRICT distance_cache) { + BROTLI_UNUSED(handle); + BROTLI_UNUSED(distance_cache); +} + +static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle, + const BrotliEncoderDictionary* dictionary, + const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, + const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, + const size_t max_length, const size_t max_backward, + const size_t gap, const size_t max_distance, + HasherSearchResult* BROTLI_RESTRICT out) { + HashRolling* self = FN(Self)(handle); + const size_t cur_ix_masked = cur_ix & ring_buffer_mask; + size_t pos = self->next_ix; + + if ((cur_ix & (JUMP - 1)) != 0) return; + + /* Not enough lookahead */ + if (max_length < CHUNKLEN) return; + + for (pos = self->next_ix; pos <= cur_ix; pos += JUMP) { + uint32_t code = self->state & MASK; + + uint8_t rem = data[pos & ring_buffer_mask]; + uint8_t add = data[(pos + CHUNKLEN) & ring_buffer_mask]; + size_t found_ix = FN(kInvalidPos); + + self->state = FN(HashRollingFunction)( + self->state, add, rem, self->factor, self->factor_remove); + + if (code < NUMBUCKETS) { + found_ix = self->table[code]; + self->table[code] = (uint32_t)pos; + if (pos == cur_ix && found_ix != FN(kInvalidPos)) { + /* The cast to 32-bit makes backward distances up to 4GB work even + if cur_ix is above 4GB, despite using 32-bit values in the table. */ + size_t backward = (uint32_t)(cur_ix - found_ix); + if (backward <= max_backward) { + const size_t found_ix_masked = found_ix & ring_buffer_mask; + const size_t len = FindMatchLengthWithLimit(&data[found_ix_masked], + &data[cur_ix_masked], + max_length); + if (len >= 4 && len > out->len) { + score_t score = BackwardReferenceScore(len, backward); + if (score > out->score) { + out->len = len; + out->distance = backward; + out->score = score; + out->len_code_delta = 0; + } + } + } + } + } + } + + self->next_ix = cur_ix + JUMP; + + /* NOTE: this hasher does not search in the dictionary. It is used as + backup-hasher, the main hasher already searches in it. */ + BROTLI_UNUSED(dictionary); + BROTLI_UNUSED(distance_cache); + BROTLI_UNUSED(gap); + BROTLI_UNUSED(max_distance); +} + +#undef HashRolling diff --git a/contrib/libs/brotli/enc/hash_to_binary_tree_inc.h b/contrib/libs/brotli/enc/hash_to_binary_tree_inc.h index 7fb0356f55..45801b1972 100644 --- a/contrib/libs/brotli/enc/hash_to_binary_tree_inc.h +++ b/contrib/libs/brotli/enc/hash_to_binary_tree_inc.h @@ -24,8 +24,8 @@ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return MAX_TREE_COMP_LENGTH; } -static uint32_t FN(HashBytes)(const uint8_t* data) { - uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; +static uint32_t FN(HashBytes)(const uint8_t* data) { + uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ return h >> (32 - BUCKET_BITS); @@ -154,13 +154,13 @@ static BROTLI_INLINE BackwardMatch* FN(StoreAndFindMatches)( { const size_t cur_len = BROTLI_MIN(size_t, best_len_left, best_len_right); size_t len; - BROTLI_DCHECK(cur_len <= MAX_TREE_COMP_LENGTH); + BROTLI_DCHECK(cur_len <= MAX_TREE_COMP_LENGTH); len = cur_len + FindMatchLengthWithLimit(&data[cur_ix_masked + cur_len], &data[prev_ix_masked + cur_len], max_length - cur_len); - BROTLI_DCHECK( - 0 == memcmp(&data[cur_ix_masked], &data[prev_ix_masked], len)); + BROTLI_DCHECK( + 0 == memcmp(&data[cur_ix_masked], &data[prev_ix_masked], len)); if (matches && len > *best_len) { *best_len = len; InitBackwardMatch(matches++, backward, len); @@ -200,11 +200,11 @@ static BROTLI_INLINE BackwardMatch* FN(StoreAndFindMatches)( sorted by strictly increasing length and (non-strictly) increasing distance. */ static BROTLI_INLINE size_t FN(FindAllMatches)(HasherHandle handle, - const BrotliEncoderDictionary* dictionary, const uint8_t* data, + const BrotliEncoderDictionary* dictionary, const uint8_t* data, const size_t ring_buffer_mask, const size_t cur_ix, - const size_t max_length, const size_t max_backward, - const size_t gap, const BrotliEncoderParams* params, - BackwardMatch* matches) { + const size_t max_length, const size_t max_backward, + const size_t gap, const BrotliEncoderParams* params, + BackwardMatch* matches) { BackwardMatch* const orig_matches = matches; const size_t cur_ix_masked = cur_ix & ring_buffer_mask; size_t best_len = 1; @@ -253,7 +253,7 @@ static BROTLI_INLINE size_t FN(FindAllMatches)(HasherHandle handle, uint32_t dict_id = dict_matches[l]; if (dict_id < kInvalidMatch) { size_t distance = max_backward + gap + (dict_id >> 5) + 1; - if (distance <= params->dist.max_distance) { + if (distance <= params->dist.max_distance) { InitDictionaryBackwardMatch(matches++, distance, l, dict_id & 31); } } @@ -266,7 +266,7 @@ static BROTLI_INLINE size_t FN(FindAllMatches)(HasherHandle handle, /* Stores the hash of the next 4 bytes and re-roots the binary tree at the current sequence, without returning any matches. REQUIRES: ix + MAX_TREE_COMP_LENGTH <= end-of-current-block */ -static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data, +static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data, const size_t mask, const size_t ix) { HashToBinaryTree* self = FN(Self)(handle); /* Maximum distance is window size - 16, see section 9.1. of the spec. */ @@ -276,7 +276,7 @@ static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data, } static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle, - const uint8_t* data, const size_t mask, const size_t ix_start, + const uint8_t* data, const size_t mask, const size_t ix_start, const size_t ix_end) { size_t i = ix_start; size_t j = ix_start; diff --git a/contrib/libs/brotli/enc/histogram.c b/contrib/libs/brotli/enc/histogram.c index 6da2ff6bb4..a026ad4e12 100644 --- a/contrib/libs/brotli/enc/histogram.c +++ b/contrib/libs/brotli/enc/histogram.c @@ -8,7 +8,7 @@ #include "./histogram.h" -#include "../common/context.h" +#include "../common/context.h" #include "./block_splitter.h" #include "./command.h" @@ -63,16 +63,16 @@ void BrotliBuildHistogramsWithContext( BlockSplitIteratorNext(&insert_and_copy_it); HistogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_], cmd->cmd_prefix_); - /* TODO: unwrap iterator blocks. */ + /* TODO: unwrap iterator blocks. */ for (j = cmd->insert_len_; j != 0; --j) { size_t context; BlockSplitIteratorNext(&literal_it); - context = literal_it.type_; - if (context_modes) { - ContextLut lut = BROTLI_CONTEXT_LUT(context_modes[context]); - context = (context << BROTLI_LITERAL_CONTEXT_BITS) + - BROTLI_CONTEXT(prev_byte, prev_byte2, lut); - } + context = literal_it.type_; + if (context_modes) { + ContextLut lut = BROTLI_CONTEXT_LUT(context_modes[context]); + context = (context << BROTLI_LITERAL_CONTEXT_BITS) + + BROTLI_CONTEXT(prev_byte, prev_byte2, lut); + } HistogramAddLiteral(&literal_histograms[context], ringbuffer[pos & mask]); prev_byte2 = prev_byte; @@ -89,7 +89,7 @@ void BrotliBuildHistogramsWithContext( context = (dist_it.type_ << BROTLI_DISTANCE_CONTEXT_BITS) + CommandDistanceContext(cmd); HistogramAddDistance(©_dist_histograms[context], - cmd->dist_prefix_ & 0x3FF); + cmd->dist_prefix_ & 0x3FF); } } } diff --git a/contrib/libs/brotli/enc/histogram.h b/contrib/libs/brotli/enc/histogram.h index 42af3c3f9d..f976c66039 100644 --- a/contrib/libs/brotli/enc/histogram.h +++ b/contrib/libs/brotli/enc/histogram.h @@ -12,8 +12,8 @@ #include <string.h> /* memset */ #include "../common/constants.h" -#include "../common/context.h" -#include "../common/platform.h" +#include "../common/context.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./block_splitter.h" #include "./command.h" @@ -22,9 +22,9 @@ extern "C" { #endif -/* The distance symbols effectively used by "Large Window Brotli" (32-bit). */ -#define BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS 544 - +/* The distance symbols effectively used by "Large Window Brotli" (32-bit). */ +#define BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS 544 + #define FN(X) X ## Literal #define DATA_SIZE BROTLI_NUM_LITERAL_SYMBOLS #define DataType uint8_t @@ -41,7 +41,7 @@ extern "C" { #undef FN #define FN(X) X ## Distance -#define DATA_SIZE BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS +#define DATA_SIZE BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS #include "./histogram_inc.h" /* NOLINT(build/include) */ #undef DataType #undef DATA_SIZE diff --git a/contrib/libs/brotli/enc/histogram_inc.h b/contrib/libs/brotli/enc/histogram_inc.h index 50eaf7468d..5f818ba934 100644 --- a/contrib/libs/brotli/enc/histogram_inc.h +++ b/contrib/libs/brotli/enc/histogram_inc.h @@ -33,7 +33,7 @@ static BROTLI_INLINE void FN(HistogramAdd)(FN(Histogram)* self, size_t val) { } static BROTLI_INLINE void FN(HistogramAddVector)(FN(Histogram)* self, - const DataType* p, size_t n) { + const DataType* p, size_t n) { self->total_count_ += n; n += 1; while (--n) ++self->data_[*p++]; diff --git a/contrib/libs/brotli/enc/literal_cost.c b/contrib/libs/brotli/enc/literal_cost.c index c231100e34..670614c38d 100644 --- a/contrib/libs/brotli/enc/literal_cost.c +++ b/contrib/libs/brotli/enc/literal_cost.c @@ -9,7 +9,7 @@ #include "./literal_cost.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./fast_log.h" #include "./utf8_util.h" @@ -25,7 +25,7 @@ static size_t UTF8Position(size_t last, size_t c, size_t clamp) { return BROTLI_MIN(size_t, 1, clamp); } else { /* Let's decide over the last byte if this ends the sequence. */ - if (last < 0xE0) { + if (last < 0xE0) { return 0; /* Completed two or three byte coding. */ } else { /* Next one is the 'Byte 3' of utf-8 encoding. */ return BROTLI_MIN(size_t, 2, clamp); @@ -34,7 +34,7 @@ static size_t UTF8Position(size_t last, size_t c, size_t clamp) { } static size_t DecideMultiByteStatsLevel(size_t pos, size_t len, size_t mask, - const uint8_t* data) { + const uint8_t* data) { size_t counts[3] = { 0 }; size_t max_utf8 = 1; /* should be 2, but 1 compresses better. */ size_t last_c = 0; @@ -54,7 +54,7 @@ static size_t DecideMultiByteStatsLevel(size_t pos, size_t len, size_t mask, } static void EstimateBitCostsForLiteralsUTF8(size_t pos, size_t len, size_t mask, - const uint8_t* data, float* cost) { + const uint8_t* data, float* cost) { /* max_utf8 is 0 (normal ASCII single byte modeling), 1 (for 2-byte UTF-8 modeling), or 2 (for 3-byte UTF-8 modeling). */ const size_t max_utf8 = DecideMultiByteStatsLevel(pos, len, mask, data); @@ -125,7 +125,7 @@ static void EstimateBitCostsForLiteralsUTF8(size_t pos, size_t len, size_t mask, } void BrotliEstimateBitCostsForLiterals(size_t pos, size_t len, size_t mask, - const uint8_t* data, float* cost) { + const uint8_t* data, float* cost) { if (BrotliIsMostlyUTF8(data, pos, mask, len, kMinUTF8Ratio)) { EstimateBitCostsForLiteralsUTF8(pos, len, mask, data, cost); return; diff --git a/contrib/libs/brotli/enc/literal_cost.h b/contrib/libs/brotli/enc/literal_cost.h index 8f53f39d3f..00fc2bfd47 100644 --- a/contrib/libs/brotli/enc/literal_cost.h +++ b/contrib/libs/brotli/enc/literal_cost.h @@ -10,7 +10,7 @@ #ifndef BROTLI_ENC_LITERAL_COST_H_ #define BROTLI_ENC_LITERAL_COST_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -21,7 +21,7 @@ extern "C" { ring-buffer (data, mask) will take entropy coded and writes these estimates to the cost[0..len) array. */ BROTLI_INTERNAL void BrotliEstimateBitCostsForLiterals( - size_t pos, size_t len, size_t mask, const uint8_t* data, float* cost); + size_t pos, size_t len, size_t mask, const uint8_t* data, float* cost); #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ diff --git a/contrib/libs/brotli/enc/memory.c b/contrib/libs/brotli/enc/memory.c index f6ed7e3cb7..14cc86b556 100644 --- a/contrib/libs/brotli/enc/memory.c +++ b/contrib/libs/brotli/enc/memory.c @@ -12,7 +12,7 @@ #include <stdlib.h> /* exit, free, malloc */ #include <string.h> /* memcpy */ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -31,8 +31,8 @@ void BrotliInitMemoryManager( MemoryManager* m, brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { if (!alloc_func) { - m->alloc_func = BrotliDefaultAllocFunc; - m->free_func = BrotliDefaultFreeFunc; + m->alloc_func = BrotliDefaultAllocFunc; + m->free_func = BrotliDefaultFreeFunc; m->opaque = 0; } else { m->alloc_func = alloc_func; @@ -121,11 +121,11 @@ static void CollectGarbagePointers(MemoryManager* m) { m->pointers + NEW_FREED_OFFSET, m->new_freed); m->perm_allocated -= annihilated; m->new_freed -= annihilated; - BROTLI_DCHECK(m->new_freed == 0); + BROTLI_DCHECK(m->new_freed == 0); } if (m->new_allocated != 0) { - BROTLI_DCHECK(m->perm_allocated + m->new_allocated <= MAX_PERM_ALLOCATED); + BROTLI_DCHECK(m->perm_allocated + m->new_allocated <= MAX_PERM_ALLOCATED); memcpy(m->pointers + PERM_ALLOCATED_OFFSET + m->perm_allocated, m->pointers + NEW_ALLOCATED_OFFSET, sizeof(void*) * m->new_allocated); diff --git a/contrib/libs/brotli/enc/memory.h b/contrib/libs/brotli/enc/memory.h index ab928d019b..d72a663355 100644 --- a/contrib/libs/brotli/enc/memory.h +++ b/contrib/libs/brotli/enc/memory.h @@ -9,9 +9,9 @@ #ifndef BROTLI_ENC_MEMORY_H_ #define BROTLI_ENC_MEMORY_H_ -#include <string.h> /* memcpy */ - -#include "../common/platform.h" +#include <string.h> /* memcpy */ + +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -58,43 +58,43 @@ BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p); BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m); -/* -Dynamically grows array capacity to at least the requested size -M: MemoryManager -T: data type -A: array -C: capacity -R: requested size -*/ -#define BROTLI_ENSURE_CAPACITY(M, T, A, C, R) { \ - if (C < (R)) { \ - size_t _new_size = (C == 0) ? (R) : C; \ - T* new_array; \ - while (_new_size < (R)) _new_size *= 2; \ - new_array = BROTLI_ALLOC((M), T, _new_size); \ - if (!BROTLI_IS_OOM(M) && C != 0) \ - memcpy(new_array, A, C * sizeof(T)); \ - BROTLI_FREE((M), A); \ - A = new_array; \ - C = _new_size; \ - } \ -} - -/* -Appends value and dynamically grows array capacity when needed -M: MemoryManager -T: data type -A: array -C: array capacity -S: array size -V: value to append -*/ -#define BROTLI_ENSURE_CAPACITY_APPEND(M, T, A, C, S, V) { \ - (S)++; \ - BROTLI_ENSURE_CAPACITY(M, T, A, C, S); \ - A[(S) - 1] = (V); \ -} - +/* +Dynamically grows array capacity to at least the requested size +M: MemoryManager +T: data type +A: array +C: capacity +R: requested size +*/ +#define BROTLI_ENSURE_CAPACITY(M, T, A, C, R) { \ + if (C < (R)) { \ + size_t _new_size = (C == 0) ? (R) : C; \ + T* new_array; \ + while (_new_size < (R)) _new_size *= 2; \ + new_array = BROTLI_ALLOC((M), T, _new_size); \ + if (!BROTLI_IS_OOM(M) && C != 0) \ + memcpy(new_array, A, C * sizeof(T)); \ + BROTLI_FREE((M), A); \ + A = new_array; \ + C = _new_size; \ + } \ +} + +/* +Appends value and dynamically grows array capacity when needed +M: MemoryManager +T: data type +A: array +C: array capacity +S: array size +V: value to append +*/ +#define BROTLI_ENSURE_CAPACITY_APPEND(M, T, A, C, S, V) { \ + (S)++; \ + BROTLI_ENSURE_CAPACITY(M, T, A, C, S); \ + A[(S) - 1] = (V); \ +} + #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ #endif diff --git a/contrib/libs/brotli/enc/metablock.c b/contrib/libs/brotli/enc/metablock.c index 4e80044f31..1d2d5d2d21 100644 --- a/contrib/libs/brotli/enc/metablock.c +++ b/contrib/libs/brotli/enc/metablock.c @@ -10,8 +10,8 @@ #include "./metablock.h" #include "../common/constants.h" -#include "../common/context.h" -#include "../common/platform.h" +#include "../common/context.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./bit_cost.h" #include "./block_splitter.h" @@ -25,116 +25,116 @@ extern "C" { #endif -void BrotliInitDistanceParams(BrotliEncoderParams* params, - uint32_t npostfix, uint32_t ndirect) { - BrotliDistanceParams* dist_params = ¶ms->dist; - uint32_t alphabet_size, max_distance; - - dist_params->distance_postfix_bits = npostfix; - dist_params->num_direct_distance_codes = ndirect; - - alphabet_size = BROTLI_DISTANCE_ALPHABET_SIZE( - npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS); - max_distance = ndirect + (1U << (BROTLI_MAX_DISTANCE_BITS + npostfix + 2)) - - (1U << (npostfix + 2)); - - if (params->large_window) { - static const uint32_t bound[BROTLI_MAX_NPOSTFIX + 1] = {0, 4, 12, 28}; - uint32_t postfix = 1U << npostfix; - alphabet_size = BROTLI_DISTANCE_ALPHABET_SIZE( - npostfix, ndirect, BROTLI_LARGE_MAX_DISTANCE_BITS); - /* The maximum distance is set so that no distance symbol used can encode - a distance larger than BROTLI_MAX_ALLOWED_DISTANCE with all - its extra bits set. */ - if (ndirect < bound[npostfix]) { - max_distance = BROTLI_MAX_ALLOWED_DISTANCE - (bound[npostfix] - ndirect); - } else if (ndirect >= bound[npostfix] + postfix) { - max_distance = (3U << 29) - 4 + (ndirect - bound[npostfix]); - } else { - max_distance = BROTLI_MAX_ALLOWED_DISTANCE; - } - } - - dist_params->alphabet_size = alphabet_size; - dist_params->max_distance = max_distance; -} - -static void RecomputeDistancePrefixes(Command* cmds, - size_t num_commands, - const BrotliDistanceParams* orig_params, - const BrotliDistanceParams* new_params) { - size_t i; - - if (orig_params->distance_postfix_bits == new_params->distance_postfix_bits && - orig_params->num_direct_distance_codes == - new_params->num_direct_distance_codes) { - return; - } - - for (i = 0; i < num_commands; ++i) { - Command* cmd = &cmds[i]; - if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) { - PrefixEncodeCopyDistance(CommandRestoreDistanceCode(cmd, orig_params), - new_params->num_direct_distance_codes, - new_params->distance_postfix_bits, - &cmd->dist_prefix_, - &cmd->dist_extra_); - } - } -} - -static BROTLI_BOOL ComputeDistanceCost(const Command* cmds, - size_t num_commands, - const BrotliDistanceParams* orig_params, - const BrotliDistanceParams* new_params, - double* cost) { - size_t i; - BROTLI_BOOL equal_params = BROTLI_FALSE; - uint16_t dist_prefix; - uint32_t dist_extra; - double extra_bits = 0.0; - HistogramDistance histo; - HistogramClearDistance(&histo); - - if (orig_params->distance_postfix_bits == new_params->distance_postfix_bits && - orig_params->num_direct_distance_codes == - new_params->num_direct_distance_codes) { - equal_params = BROTLI_TRUE; - } - - for (i = 0; i < num_commands; i++) { - const Command* cmd = &cmds[i]; - if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) { - if (equal_params) { - dist_prefix = cmd->dist_prefix_; - } else { - uint32_t distance = CommandRestoreDistanceCode(cmd, orig_params); - if (distance > new_params->max_distance) { - return BROTLI_FALSE; - } - PrefixEncodeCopyDistance(distance, - new_params->num_direct_distance_codes, - new_params->distance_postfix_bits, - &dist_prefix, - &dist_extra); - } - HistogramAddDistance(&histo, dist_prefix & 0x3FF); - extra_bits += dist_prefix >> 10; - } - } - - *cost = BrotliPopulationCostDistance(&histo) + extra_bits; - return BROTLI_TRUE; -} - +void BrotliInitDistanceParams(BrotliEncoderParams* params, + uint32_t npostfix, uint32_t ndirect) { + BrotliDistanceParams* dist_params = ¶ms->dist; + uint32_t alphabet_size, max_distance; + + dist_params->distance_postfix_bits = npostfix; + dist_params->num_direct_distance_codes = ndirect; + + alphabet_size = BROTLI_DISTANCE_ALPHABET_SIZE( + npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS); + max_distance = ndirect + (1U << (BROTLI_MAX_DISTANCE_BITS + npostfix + 2)) - + (1U << (npostfix + 2)); + + if (params->large_window) { + static const uint32_t bound[BROTLI_MAX_NPOSTFIX + 1] = {0, 4, 12, 28}; + uint32_t postfix = 1U << npostfix; + alphabet_size = BROTLI_DISTANCE_ALPHABET_SIZE( + npostfix, ndirect, BROTLI_LARGE_MAX_DISTANCE_BITS); + /* The maximum distance is set so that no distance symbol used can encode + a distance larger than BROTLI_MAX_ALLOWED_DISTANCE with all + its extra bits set. */ + if (ndirect < bound[npostfix]) { + max_distance = BROTLI_MAX_ALLOWED_DISTANCE - (bound[npostfix] - ndirect); + } else if (ndirect >= bound[npostfix] + postfix) { + max_distance = (3U << 29) - 4 + (ndirect - bound[npostfix]); + } else { + max_distance = BROTLI_MAX_ALLOWED_DISTANCE; + } + } + + dist_params->alphabet_size = alphabet_size; + dist_params->max_distance = max_distance; +} + +static void RecomputeDistancePrefixes(Command* cmds, + size_t num_commands, + const BrotliDistanceParams* orig_params, + const BrotliDistanceParams* new_params) { + size_t i; + + if (orig_params->distance_postfix_bits == new_params->distance_postfix_bits && + orig_params->num_direct_distance_codes == + new_params->num_direct_distance_codes) { + return; + } + + for (i = 0; i < num_commands; ++i) { + Command* cmd = &cmds[i]; + if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) { + PrefixEncodeCopyDistance(CommandRestoreDistanceCode(cmd, orig_params), + new_params->num_direct_distance_codes, + new_params->distance_postfix_bits, + &cmd->dist_prefix_, + &cmd->dist_extra_); + } + } +} + +static BROTLI_BOOL ComputeDistanceCost(const Command* cmds, + size_t num_commands, + const BrotliDistanceParams* orig_params, + const BrotliDistanceParams* new_params, + double* cost) { + size_t i; + BROTLI_BOOL equal_params = BROTLI_FALSE; + uint16_t dist_prefix; + uint32_t dist_extra; + double extra_bits = 0.0; + HistogramDistance histo; + HistogramClearDistance(&histo); + + if (orig_params->distance_postfix_bits == new_params->distance_postfix_bits && + orig_params->num_direct_distance_codes == + new_params->num_direct_distance_codes) { + equal_params = BROTLI_TRUE; + } + + for (i = 0; i < num_commands; i++) { + const Command* cmd = &cmds[i]; + if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) { + if (equal_params) { + dist_prefix = cmd->dist_prefix_; + } else { + uint32_t distance = CommandRestoreDistanceCode(cmd, orig_params); + if (distance > new_params->max_distance) { + return BROTLI_FALSE; + } + PrefixEncodeCopyDistance(distance, + new_params->num_direct_distance_codes, + new_params->distance_postfix_bits, + &dist_prefix, + &dist_extra); + } + HistogramAddDistance(&histo, dist_prefix & 0x3FF); + extra_bits += dist_prefix >> 10; + } + } + + *cost = BrotliPopulationCostDistance(&histo) + extra_bits; + return BROTLI_TRUE; +} + void BrotliBuildMetaBlock(MemoryManager* m, const uint8_t* ringbuffer, const size_t pos, const size_t mask, - BrotliEncoderParams* params, + BrotliEncoderParams* params, uint8_t prev_byte, uint8_t prev_byte2, - Command* cmds, + Command* cmds, size_t num_commands, ContextType literal_context_mode, MetaBlockSplit* mb) { @@ -147,48 +147,48 @@ void BrotliBuildMetaBlock(MemoryManager* m, size_t distance_histograms_size; size_t i; size_t literal_context_multiplier = 1; - uint32_t npostfix; - uint32_t ndirect_msb = 0; - BROTLI_BOOL check_orig = BROTLI_TRUE; - double best_dist_cost = 1e99; - BrotliEncoderParams orig_params = *params; - BrotliEncoderParams new_params = *params; - - for (npostfix = 0; npostfix <= BROTLI_MAX_NPOSTFIX; npostfix++) { - for (; ndirect_msb < 16; ndirect_msb++) { - uint32_t ndirect = ndirect_msb << npostfix; - BROTLI_BOOL skip; - double dist_cost; - BrotliInitDistanceParams(&new_params, npostfix, ndirect); - if (npostfix == orig_params.dist.distance_postfix_bits && - ndirect == orig_params.dist.num_direct_distance_codes) { - check_orig = BROTLI_FALSE; - } - skip = !ComputeDistanceCost( - cmds, num_commands, - &orig_params.dist, &new_params.dist, &dist_cost); - if (skip || (dist_cost > best_dist_cost)) { - break; - } - best_dist_cost = dist_cost; - params->dist = new_params.dist; - } - if (ndirect_msb > 0) ndirect_msb--; - ndirect_msb /= 2; - } - if (check_orig) { - double dist_cost; - ComputeDistanceCost(cmds, num_commands, - &orig_params.dist, &orig_params.dist, &dist_cost); - if (dist_cost < best_dist_cost) { - /* NB: currently unused; uncomment when more param tuning is added. */ - /* best_dist_cost = dist_cost; */ - params->dist = orig_params.dist; - } - } - RecomputeDistancePrefixes(cmds, num_commands, - &orig_params.dist, ¶ms->dist); - + uint32_t npostfix; + uint32_t ndirect_msb = 0; + BROTLI_BOOL check_orig = BROTLI_TRUE; + double best_dist_cost = 1e99; + BrotliEncoderParams orig_params = *params; + BrotliEncoderParams new_params = *params; + + for (npostfix = 0; npostfix <= BROTLI_MAX_NPOSTFIX; npostfix++) { + for (; ndirect_msb < 16; ndirect_msb++) { + uint32_t ndirect = ndirect_msb << npostfix; + BROTLI_BOOL skip; + double dist_cost; + BrotliInitDistanceParams(&new_params, npostfix, ndirect); + if (npostfix == orig_params.dist.distance_postfix_bits && + ndirect == orig_params.dist.num_direct_distance_codes) { + check_orig = BROTLI_FALSE; + } + skip = !ComputeDistanceCost( + cmds, num_commands, + &orig_params.dist, &new_params.dist, &dist_cost); + if (skip || (dist_cost > best_dist_cost)) { + break; + } + best_dist_cost = dist_cost; + params->dist = new_params.dist; + } + if (ndirect_msb > 0) ndirect_msb--; + ndirect_msb /= 2; + } + if (check_orig) { + double dist_cost; + ComputeDistanceCost(cmds, num_commands, + &orig_params.dist, &orig_params.dist, &dist_cost); + if (dist_cost < best_dist_cost) { + /* NB: currently unused; uncomment when more param tuning is added. */ + /* best_dist_cost = dist_cost; */ + params->dist = orig_params.dist; + } + } + RecomputeDistancePrefixes(cmds, num_commands, + &orig_params.dist, ¶ms->dist); + BrotliSplitBlock(m, cmds, num_commands, ringbuffer, pos, mask, params, &mb->literal_split, @@ -220,7 +220,7 @@ void BrotliBuildMetaBlock(MemoryManager* m, if (BROTLI_IS_OOM(m)) return; ClearHistogramsDistance(distance_histograms, distance_histograms_size); - BROTLI_DCHECK(mb->command_histograms == 0); + BROTLI_DCHECK(mb->command_histograms == 0); mb->command_histograms_size = mb->command_split.num_types; mb->command_histograms = BROTLI_ALLOC(m, HistogramCommand, mb->command_histograms_size); @@ -233,14 +233,14 @@ void BrotliBuildMetaBlock(MemoryManager* m, literal_histograms, mb->command_histograms, distance_histograms); BROTLI_FREE(m, literal_context_modes); - BROTLI_DCHECK(mb->literal_context_map == 0); + BROTLI_DCHECK(mb->literal_context_map == 0); mb->literal_context_map_size = mb->literal_split.num_types << BROTLI_LITERAL_CONTEXT_BITS; mb->literal_context_map = BROTLI_ALLOC(m, uint32_t, mb->literal_context_map_size); if (BROTLI_IS_OOM(m)) return; - BROTLI_DCHECK(mb->literal_histograms == 0); + BROTLI_DCHECK(mb->literal_histograms == 0); mb->literal_histograms_size = mb->literal_context_map_size; mb->literal_histograms = BROTLI_ALLOC(m, HistogramLiteral, mb->literal_histograms_size); @@ -264,14 +264,14 @@ void BrotliBuildMetaBlock(MemoryManager* m, } } - BROTLI_DCHECK(mb->distance_context_map == 0); + BROTLI_DCHECK(mb->distance_context_map == 0); mb->distance_context_map_size = mb->distance_split.num_types << BROTLI_DISTANCE_CONTEXT_BITS; mb->distance_context_map = BROTLI_ALLOC(m, uint32_t, mb->distance_context_map_size); if (BROTLI_IS_OOM(m)) return; - BROTLI_DCHECK(mb->distance_histograms == 0); + BROTLI_DCHECK(mb->distance_histograms == 0); mb->distance_histograms_size = mb->distance_context_map_size; mb->distance_histograms = BROTLI_ALLOC(m, HistogramDistance, mb->distance_histograms_size); @@ -343,7 +343,7 @@ static void InitContextBlockSplitter( size_t* histograms_size) { size_t max_num_blocks = num_symbols / min_block_size + 1; size_t max_num_types; - BROTLI_DCHECK(num_contexts <= BROTLI_MAX_STATIC_CONTEXTS); + BROTLI_DCHECK(num_contexts <= BROTLI_MAX_STATIC_CONTEXTS); self->alphabet_size_ = alphabet_size; self->num_contexts_ = num_contexts; @@ -369,7 +369,7 @@ static void InitContextBlockSplitter( if (BROTLI_IS_OOM(m)) return; split->num_blocks = max_num_blocks; if (BROTLI_IS_OOM(m)) return; - BROTLI_DCHECK(*histograms == 0); + BROTLI_DCHECK(*histograms == 0); *histograms_size = max_num_types * num_contexts; *histograms = BROTLI_ALLOC(m, HistogramLiteral, *histograms_size); self->histograms_ = *histograms; @@ -522,7 +522,7 @@ static void MapStaticContexts(MemoryManager* m, const uint32_t* static_context_map, MetaBlockSplit* mb) { size_t i; - BROTLI_DCHECK(mb->literal_context_map == 0); + BROTLI_DCHECK(mb->literal_context_map == 0); mb->literal_context_map_size = mb->literal_split.num_types << BROTLI_LITERAL_CONTEXT_BITS; mb->literal_context_map = @@ -541,9 +541,9 @@ static void MapStaticContexts(MemoryManager* m, static BROTLI_INLINE void BrotliBuildMetaBlockGreedyInternal( MemoryManager* m, const uint8_t* ringbuffer, size_t pos, size_t mask, - uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut, + uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut, const size_t num_contexts, const uint32_t* static_context_map, - const Command* commands, size_t n_commands, MetaBlockSplit* mb) { + const Command* commands, size_t n_commands, MetaBlockSplit* mb) { union { BlockSplitterLiteral plain; ContextBlockSplitter ctx; @@ -584,8 +584,8 @@ static BROTLI_INLINE void BrotliBuildMetaBlockGreedyInternal( if (num_contexts == 1) { BlockSplitterAddSymbolLiteral(&lit_blocks.plain, literal); } else { - size_t context = - BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut); + size_t context = + BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut); ContextBlockSplitterAddSymbol(&lit_blocks.ctx, m, literal, static_context_map[context]); if (BROTLI_IS_OOM(m)) return; @@ -599,7 +599,7 @@ static BROTLI_INLINE void BrotliBuildMetaBlockGreedyInternal( prev_byte2 = ringbuffer[(pos - 2) & mask]; prev_byte = ringbuffer[(pos - 1) & mask]; if (cmd.cmd_prefix_ >= 128) { - BlockSplitterAddSymbolDistance(&dist_blocks, cmd.dist_prefix_ & 0x3FF); + BlockSplitterAddSymbolDistance(&dist_blocks, cmd.dist_prefix_ & 0x3FF); } } } @@ -626,7 +626,7 @@ void BrotliBuildMetaBlockGreedy(MemoryManager* m, size_t mask, uint8_t prev_byte, uint8_t prev_byte2, - ContextLut literal_context_lut, + ContextLut literal_context_lut, size_t num_contexts, const uint32_t* static_context_map, const Command* commands, @@ -634,15 +634,15 @@ void BrotliBuildMetaBlockGreedy(MemoryManager* m, MetaBlockSplit* mb) { if (num_contexts == 1) { BrotliBuildMetaBlockGreedyInternal(m, ringbuffer, pos, mask, prev_byte, - prev_byte2, literal_context_lut, 1, NULL, commands, n_commands, mb); + prev_byte2, literal_context_lut, 1, NULL, commands, n_commands, mb); } else { BrotliBuildMetaBlockGreedyInternal(m, ringbuffer, pos, mask, prev_byte, - prev_byte2, literal_context_lut, num_contexts, static_context_map, + prev_byte2, literal_context_lut, num_contexts, static_context_map, commands, n_commands, mb); } } -void BrotliOptimizeHistograms(uint32_t num_distance_codes, +void BrotliOptimizeHistograms(uint32_t num_distance_codes, MetaBlockSplit* mb) { uint8_t good_for_rle[BROTLI_NUM_COMMAND_SYMBOLS]; size_t i; diff --git a/contrib/libs/brotli/enc/metablock.h b/contrib/libs/brotli/enc/metablock.h index 334a79a443..5718662df6 100644 --- a/contrib/libs/brotli/enc/metablock.h +++ b/contrib/libs/brotli/enc/metablock.h @@ -10,8 +10,8 @@ #ifndef BROTLI_ENC_METABLOCK_H_ #define BROTLI_ENC_METABLOCK_H_ -#include "../common/context.h" -#include "../common/platform.h" +#include "../common/context.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./block_splitter.h" #include "./command.h" @@ -67,18 +67,18 @@ static BROTLI_INLINE void DestroyMetaBlockSplit( BROTLI_FREE(m, mb->distance_histograms); } -/* Uses the slow shortest-path block splitter and does context clustering. - The distance parameters are dynamically selected based on the commands - which get recomputed under the new distance parameters. The new distance - parameters are stored into *params. */ +/* Uses the slow shortest-path block splitter and does context clustering. + The distance parameters are dynamically selected based on the commands + which get recomputed under the new distance parameters. The new distance + parameters are stored into *params. */ BROTLI_INTERNAL void BrotliBuildMetaBlock(MemoryManager* m, const uint8_t* ringbuffer, const size_t pos, const size_t mask, - BrotliEncoderParams* params, + BrotliEncoderParams* params, uint8_t prev_byte, uint8_t prev_byte2, - Command* cmds, + Command* cmds, size_t num_commands, ContextType literal_context_mode, MetaBlockSplit* mb); @@ -88,16 +88,16 @@ BROTLI_INTERNAL void BrotliBuildMetaBlock(MemoryManager* m, is the same for all block types. */ BROTLI_INTERNAL void BrotliBuildMetaBlockGreedy( MemoryManager* m, const uint8_t* ringbuffer, size_t pos, size_t mask, - uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut, + uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut, size_t num_contexts, const uint32_t* static_context_map, const Command* commands, size_t n_commands, MetaBlockSplit* mb); -BROTLI_INTERNAL void BrotliOptimizeHistograms(uint32_t num_distance_codes, +BROTLI_INTERNAL void BrotliOptimizeHistograms(uint32_t num_distance_codes, MetaBlockSplit* mb); -BROTLI_INTERNAL void BrotliInitDistanceParams(BrotliEncoderParams* params, - uint32_t npostfix, uint32_t ndirect); - +BROTLI_INTERNAL void BrotliInitDistanceParams(BrotliEncoderParams* params, + uint32_t npostfix, uint32_t ndirect); + #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ #endif diff --git a/contrib/libs/brotli/enc/metablock_inc.h b/contrib/libs/brotli/enc/metablock_inc.h index dcc9d3c4a6..cb1887d895 100644 --- a/contrib/libs/brotli/enc/metablock_inc.h +++ b/contrib/libs/brotli/enc/metablock_inc.h @@ -67,7 +67,7 @@ static void FN(InitBlockSplitter)( split->lengths, split->lengths_alloc_size, max_num_blocks); if (BROTLI_IS_OOM(m)) return; self->split_->num_blocks = max_num_blocks; - BROTLI_DCHECK(*histograms == 0); + BROTLI_DCHECK(*histograms == 0); *histograms_size = max_num_types; *histograms = BROTLI_ALLOC(m, HistogramType, *histograms_size); self->histograms_ = *histograms; diff --git a/contrib/libs/brotli/enc/params.h b/contrib/libs/brotli/enc/params.h index 6ecf1d3f99..a46f2b5823 100644 --- a/contrib/libs/brotli/enc/params.h +++ b/contrib/libs/brotli/enc/params.h @@ -1,44 +1,44 @@ -/* Copyright 2017 Google Inc. All Rights Reserved. - - Distributed under MIT license. - See file LICENSE for detail or copy at https://opensource.org/licenses/MIT -*/ - -/* Parameters for the Brotli encoder with chosen quality levels. */ - -#ifndef BROTLI_ENC_PARAMS_H_ -#define BROTLI_ENC_PARAMS_H_ - -#include <brotli/encode.h> -#include "./encoder_dict.h" - -typedef struct BrotliHasherParams { - int type; - int bucket_bits; - int block_bits; - int hash_len; - int num_last_distances_to_check; -} BrotliHasherParams; - -typedef struct BrotliDistanceParams { - uint32_t distance_postfix_bits; - uint32_t num_direct_distance_codes; - uint32_t alphabet_size; - size_t max_distance; -} BrotliDistanceParams; - -/* Encoding parameters */ -typedef struct BrotliEncoderParams { - BrotliEncoderMode mode; - int quality; - int lgwin; - int lgblock; - size_t size_hint; - BROTLI_BOOL disable_literal_context_modeling; - BROTLI_BOOL large_window; - BrotliHasherParams hasher; - BrotliDistanceParams dist; - BrotliEncoderDictionary dictionary; -} BrotliEncoderParams; - -#endif /* BROTLI_ENC_PARAMS_H_ */ +/* Copyright 2017 Google Inc. All Rights Reserved. + + Distributed under MIT license. + See file LICENSE for detail or copy at https://opensource.org/licenses/MIT +*/ + +/* Parameters for the Brotli encoder with chosen quality levels. */ + +#ifndef BROTLI_ENC_PARAMS_H_ +#define BROTLI_ENC_PARAMS_H_ + +#include <brotli/encode.h> +#include "./encoder_dict.h" + +typedef struct BrotliHasherParams { + int type; + int bucket_bits; + int block_bits; + int hash_len; + int num_last_distances_to_check; +} BrotliHasherParams; + +typedef struct BrotliDistanceParams { + uint32_t distance_postfix_bits; + uint32_t num_direct_distance_codes; + uint32_t alphabet_size; + size_t max_distance; +} BrotliDistanceParams; + +/* Encoding parameters */ +typedef struct BrotliEncoderParams { + BrotliEncoderMode mode; + int quality; + int lgwin; + int lgblock; + size_t size_hint; + BROTLI_BOOL disable_literal_context_modeling; + BROTLI_BOOL large_window; + BrotliHasherParams hasher; + BrotliDistanceParams dist; + BrotliEncoderDictionary dictionary; +} BrotliEncoderParams; + +#endif /* BROTLI_ENC_PARAMS_H_ */ diff --git a/contrib/libs/brotli/enc/prefix.h b/contrib/libs/brotli/enc/prefix.h index fd359a478d..f7d3e104a2 100644 --- a/contrib/libs/brotli/enc/prefix.h +++ b/contrib/libs/brotli/enc/prefix.h @@ -11,7 +11,7 @@ #define BROTLI_ENC_PREFIX_H_ #include "../common/constants.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./fast_log.h" @@ -39,10 +39,10 @@ static BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code, size_t prefix = (dist >> bucket) & 1; size_t offset = (2 + prefix) << bucket; size_t nbits = bucket - postfix_bits; - *code = (uint16_t)((nbits << 10) | + *code = (uint16_t)((nbits << 10) | (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes + ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix)); - *extra_bits = (uint32_t)((dist - offset) >> postfix_bits); + *extra_bits = (uint32_t)((dist - offset) >> postfix_bits); } } diff --git a/contrib/libs/brotli/enc/quality.h b/contrib/libs/brotli/enc/quality.h index 5f4d034503..b543752ef4 100644 --- a/contrib/libs/brotli/enc/quality.h +++ b/contrib/libs/brotli/enc/quality.h @@ -10,9 +10,9 @@ #ifndef BROTLI_ENC_QUALITY_H_ #define BROTLI_ENC_QUALITY_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/encode.h> -#include "./params.h" +#include "./params.h" #define FAST_ONE_PASS_COMPRESSION_QUALITY 0 #define FAST_TWO_PASS_COMPRESSION_QUALITY 1 @@ -21,7 +21,7 @@ #define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2 #define MIN_QUALITY_FOR_BLOCK_SPLIT 4 -#define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4 +#define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4 #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4 #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5 #define MIN_QUALITY_FOR_CONTEXT_MODELING 5 @@ -30,7 +30,7 @@ /* For quality below MIN_QUALITY_FOR_BLOCK_SPLIT there is no block splitting, so we buffer at most this much literals and commands. */ -#define MAX_NUM_DELAYED_SYMBOLS 0x2FFF +#define MAX_NUM_DELAYED_SYMBOLS 0x2FFF /* Returns hash-table size for quality levels 0 and 1. */ static BROTLI_INLINE size_t MaxHashTableSize(int quality) { @@ -59,15 +59,15 @@ static BROTLI_INLINE size_t MaxZopfliCandidates( static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) { params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY, BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality)); - if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) { - params->large_window = BROTLI_FALSE; - } + if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) { + params->large_window = BROTLI_FALSE; + } if (params->lgwin < BROTLI_MIN_WINDOW_BITS) { params->lgwin = BROTLI_MIN_WINDOW_BITS; - } else { - int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS : - BROTLI_MAX_WINDOW_BITS; - if (params->lgwin > max_lgwin) params->lgwin = max_lgwin; + } else { + int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS : + BROTLI_MAX_WINDOW_BITS; + if (params->lgwin > max_lgwin) params->lgwin = max_lgwin; } } @@ -142,24 +142,24 @@ static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params, hparams->num_last_distances_to_check = params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; } - - if (params->lgwin > 24) { - /* Different hashers for large window brotli: not for qualities <= 2, - these are too fast for large window. Not for qualities >= 10: their - hasher already works well with large window. So the changes are: - H3 --> H35: for quality 3. - H54 --> H55: for quality 4 with size hint > 1MB - H6 --> H65: for qualities 5, 6, 7, 8, 9. */ - if (hparams->type == 3) { - hparams->type = 35; - } - if (hparams->type == 54) { - hparams->type = 55; - } - if (hparams->type == 6) { - hparams->type = 65; - } - } + + if (params->lgwin > 24) { + /* Different hashers for large window brotli: not for qualities <= 2, + these are too fast for large window. Not for qualities >= 10: their + hasher already works well with large window. So the changes are: + H3 --> H35: for quality 3. + H54 --> H55: for quality 4 with size hint > 1MB + H6 --> H65: for qualities 5, 6, 7, 8, 9. */ + if (hparams->type == 3) { + hparams->type = 35; + } + if (hparams->type == 54) { + hparams->type = 55; + } + if (hparams->type == 6) { + hparams->type = 65; + } + } } #endif /* BROTLI_ENC_QUALITY_H_ */ diff --git a/contrib/libs/brotli/enc/ringbuffer.h b/contrib/libs/brotli/enc/ringbuffer.h index 86079a89d3..9787ef81cf 100644 --- a/contrib/libs/brotli/enc/ringbuffer.h +++ b/contrib/libs/brotli/enc/ringbuffer.h @@ -11,7 +11,7 @@ #include <string.h> /* memcpy */ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #include "./memory.h" #include "./quality.h" @@ -41,9 +41,9 @@ typedef struct RingBuffer { uint32_t pos_; /* The actual ring buffer containing the copy of the last two bytes, the data, and the copy of the beginning as a tail. */ - uint8_t* data_; + uint8_t* data_; /* The start of the ring-buffer. */ - uint8_t* buffer_; + uint8_t* buffer_; } RingBuffer; static BROTLI_INLINE void RingBufferInit(RingBuffer* rb) { @@ -91,7 +91,7 @@ static BROTLI_INLINE void RingBufferInitBuffer( } static BROTLI_INLINE void RingBufferWriteTail( - const uint8_t* bytes, size_t n, RingBuffer* rb) { + const uint8_t* bytes, size_t n, RingBuffer* rb) { const size_t masked_pos = rb->pos_ & rb->mask_; if (BROTLI_PREDICT_FALSE(masked_pos < rb->tail_size_)) { /* Just fill the tail buffer with the beginning data. */ @@ -103,7 +103,7 @@ static BROTLI_INLINE void RingBufferWriteTail( /* Push bytes into the ring buffer. */ static BROTLI_INLINE void RingBufferWrite( - MemoryManager* m, const uint8_t* bytes, size_t n, RingBuffer* rb) { + MemoryManager* m, const uint8_t* bytes, size_t n, RingBuffer* rb) { if (rb->pos_ == 0 && n < rb->tail_size_) { /* Special case for the first write: to process the first block, we don't need to allocate the whole ring-buffer and we don't need the tail @@ -144,16 +144,16 @@ static BROTLI_INLINE void RingBufferWrite( n - (rb->size_ - masked_pos)); } } - { - BROTLI_BOOL not_first_lap = (rb->pos_ & (1u << 31)) != 0; - uint32_t rb_pos_mask = (1u << 31) - 1; - rb->buffer_[-2] = rb->buffer_[rb->size_ - 2]; - rb->buffer_[-1] = rb->buffer_[rb->size_ - 1]; - rb->pos_ = (rb->pos_ & rb_pos_mask) + (uint32_t)(n & rb_pos_mask); - if (not_first_lap) { - /* Wrap, but preserve not-a-first-lap feature. */ - rb->pos_ |= 1u << 31; - } + { + BROTLI_BOOL not_first_lap = (rb->pos_ & (1u << 31)) != 0; + uint32_t rb_pos_mask = (1u << 31) - 1; + rb->buffer_[-2] = rb->buffer_[rb->size_ - 2]; + rb->buffer_[-1] = rb->buffer_[rb->size_ - 1]; + rb->pos_ = (rb->pos_ & rb_pos_mask) + (uint32_t)(n & rb_pos_mask); + if (not_first_lap) { + /* Wrap, but preserve not-a-first-lap feature. */ + rb->pos_ |= 1u << 31; + } } } diff --git a/contrib/libs/brotli/enc/static_dict.c b/contrib/libs/brotli/enc/static_dict.c index 7299ab7203..8f8ac35495 100644 --- a/contrib/libs/brotli/enc/static_dict.c +++ b/contrib/libs/brotli/enc/static_dict.c @@ -7,17 +7,17 @@ #include "./static_dict.h" #include "../common/dictionary.h" -#include "../common/platform.h" -#include "../common/transform.h" -#include "./encoder_dict.h" +#include "../common/platform.h" +#include "../common/transform.h" +#include "./encoder_dict.h" #include "./find_match_length.h" #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif -static BROTLI_INLINE uint32_t Hash(const uint8_t* data) { - uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kDictHashMul32; +static BROTLI_INLINE uint32_t Hash(const uint8_t* data) { + uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kDictHashMul32; /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ return h >> (32 - kDictNumBits); @@ -75,33 +75,33 @@ static BROTLI_INLINE BROTLI_BOOL IsMatch(const BrotliDictionary* dictionary, } BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( - const BrotliEncoderDictionary* dictionary, const uint8_t* data, - size_t min_length, size_t max_length, uint32_t* matches) { + const BrotliEncoderDictionary* dictionary, const uint8_t* data, + size_t min_length, size_t max_length, uint32_t* matches) { BROTLI_BOOL has_found_match = BROTLI_FALSE; { - size_t offset = dictionary->buckets[Hash(data)]; + size_t offset = dictionary->buckets[Hash(data)]; BROTLI_BOOL end = !offset; while (!end) { - DictWord w = dictionary->dict_words[offset++]; + DictWord w = dictionary->dict_words[offset++]; const size_t l = w.len & 0x1F; - const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; + const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; const size_t id = w.idx; end = !!(w.len & 0x80); w.len = (uint8_t)l; if (w.transform == 0) { const size_t matchlen = - DictMatchLength(dictionary->words, data, id, l, max_length); + DictMatchLength(dictionary->words, data, id, l, max_length); const uint8_t* s; size_t minlen; size_t maxlen; size_t len; - /* Transform "" + BROTLI_TRANSFORM_IDENTITY + "" */ + /* Transform "" + BROTLI_TRANSFORM_IDENTITY + "" */ if (matchlen == l) { AddMatch(id, l, l, matches); has_found_match = BROTLI_TRUE; } - /* Transforms "" + BROTLI_TRANSFORM_OMIT_LAST_1 + "" and - "" + BROTLI_TRANSFORM_OMIT_LAST_1 + "ing " */ + /* Transforms "" + BROTLI_TRANSFORM_OMIT_LAST_1 + "" and + "" + BROTLI_TRANSFORM_OMIT_LAST_1 + "ing " */ if (matchlen >= l - 1) { AddMatch(id + 12 * n, l - 1, l, matches); if (l + 2 < max_length && @@ -111,22 +111,22 @@ BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( } has_found_match = BROTLI_TRUE; } - /* Transform "" + BROTLI_TRANSFORM_OMIT_LAST_# + "" (# = 2 .. 9) */ + /* Transform "" + BROTLI_TRANSFORM_OMIT_LAST_# + "" (# = 2 .. 9) */ minlen = min_length; if (l > 9) minlen = BROTLI_MAX(size_t, minlen, l - 9); maxlen = BROTLI_MIN(size_t, matchlen, l - 2); for (len = minlen; len <= maxlen; ++len) { - size_t cut = l - len; - size_t transform_id = (cut << 2) + - (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F); - AddMatch(id + transform_id * n, len, l, matches); + size_t cut = l - len; + size_t transform_id = (cut << 2) + + (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F); + AddMatch(id + transform_id * n, len, l, matches); has_found_match = BROTLI_TRUE; } if (matchlen < l || l + 6 >= max_length) { continue; } s = &data[l]; - /* Transforms "" + BROTLI_TRANSFORM_IDENTITY + <suffix> */ + /* Transforms "" + BROTLI_TRANSFORM_IDENTITY + <suffix> */ if (s[0] == ' ') { AddMatch(id + n, l + 1, l, matches); if (s[1] == 'a') { @@ -273,13 +273,13 @@ BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( } } } else { - /* Set is_all_caps=0 for BROTLI_TRANSFORM_UPPERCASE_FIRST and - is_all_caps=1 otherwise (BROTLI_TRANSFORM_UPPERCASE_ALL) - transform. */ + /* Set is_all_caps=0 for BROTLI_TRANSFORM_UPPERCASE_FIRST and + is_all_caps=1 otherwise (BROTLI_TRANSFORM_UPPERCASE_ALL) + transform. */ const BROTLI_BOOL is_all_caps = - TO_BROTLI_BOOL(w.transform != BROTLI_TRANSFORM_UPPERCASE_FIRST); + TO_BROTLI_BOOL(w.transform != BROTLI_TRANSFORM_UPPERCASE_FIRST); const uint8_t* s; - if (!IsMatch(dictionary->words, w, data, max_length)) { + if (!IsMatch(dictionary->words, w, data, max_length)) { continue; } /* Transform "" + kUppercase{First,All} + "" */ @@ -324,29 +324,29 @@ BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( /* Transforms with prefixes " " and "." */ if (max_length >= 5 && (data[0] == ' ' || data[0] == '.')) { BROTLI_BOOL is_space = TO_BROTLI_BOOL(data[0] == ' '); - size_t offset = dictionary->buckets[Hash(&data[1])]; + size_t offset = dictionary->buckets[Hash(&data[1])]; BROTLI_BOOL end = !offset; while (!end) { - DictWord w = dictionary->dict_words[offset++]; + DictWord w = dictionary->dict_words[offset++]; const size_t l = w.len & 0x1F; - const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; + const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; const size_t id = w.idx; end = !!(w.len & 0x80); w.len = (uint8_t)l; if (w.transform == 0) { const uint8_t* s; - if (!IsMatch(dictionary->words, w, &data[1], max_length - 1)) { + if (!IsMatch(dictionary->words, w, &data[1], max_length - 1)) { continue; } - /* Transforms " " + BROTLI_TRANSFORM_IDENTITY + "" and - "." + BROTLI_TRANSFORM_IDENTITY + "" */ + /* Transforms " " + BROTLI_TRANSFORM_IDENTITY + "" and + "." + BROTLI_TRANSFORM_IDENTITY + "" */ AddMatch(id + (is_space ? 6 : 32) * n, l + 1, l, matches); has_found_match = BROTLI_TRUE; if (l + 2 >= max_length) { continue; } - /* Transforms " " + BROTLI_TRANSFORM_IDENTITY + <suffix> and - "." + BROTLI_TRANSFORM_IDENTITY + <suffix> + /* Transforms " " + BROTLI_TRANSFORM_IDENTITY + <suffix> and + "." + BROTLI_TRANSFORM_IDENTITY + <suffix> */ s = &data[l + 1]; if (s[0] == ' ') { @@ -373,13 +373,13 @@ BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( } } } else if (is_space) { - /* Set is_all_caps=0 for BROTLI_TRANSFORM_UPPERCASE_FIRST and - is_all_caps=1 otherwise (BROTLI_TRANSFORM_UPPERCASE_ALL) - transform. */ + /* Set is_all_caps=0 for BROTLI_TRANSFORM_UPPERCASE_FIRST and + is_all_caps=1 otherwise (BROTLI_TRANSFORM_UPPERCASE_ALL) + transform. */ const BROTLI_BOOL is_all_caps = - TO_BROTLI_BOOL(w.transform != BROTLI_TRANSFORM_UPPERCASE_FIRST); + TO_BROTLI_BOOL(w.transform != BROTLI_TRANSFORM_UPPERCASE_FIRST); const uint8_t* s; - if (!IsMatch(dictionary->words, w, &data[1], max_length - 1)) { + if (!IsMatch(dictionary->words, w, &data[1], max_length - 1)) { continue; } /* Transforms " " + kUppercase{First,All} + "" */ @@ -415,22 +415,22 @@ BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( } } if (max_length >= 6) { - /* Transforms with prefixes "e ", "s ", ", " and "\xC2\xA0" */ + /* Transforms with prefixes "e ", "s ", ", " and "\xC2\xA0" */ if ((data[1] == ' ' && (data[0] == 'e' || data[0] == 's' || data[0] == ',')) || - (data[0] == 0xC2 && data[1] == 0xA0)) { - size_t offset = dictionary->buckets[Hash(&data[2])]; + (data[0] == 0xC2 && data[1] == 0xA0)) { + size_t offset = dictionary->buckets[Hash(&data[2])]; BROTLI_BOOL end = !offset; while (!end) { - DictWord w = dictionary->dict_words[offset++]; + DictWord w = dictionary->dict_words[offset++]; const size_t l = w.len & 0x1F; - const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; + const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; const size_t id = w.idx; end = !!(w.len & 0x80); w.len = (uint8_t)l; if (w.transform == 0 && - IsMatch(dictionary->words, w, &data[2], max_length - 2)) { - if (data[0] == 0xC2) { + IsMatch(dictionary->words, w, &data[2], max_length - 2)) { + if (data[0] == 0xC2) { AddMatch(id + 102 * n, l + 2, l, matches); has_found_match = BROTLI_TRUE; } else if (l + 2 < max_length && data[l + 2] == ' ') { @@ -448,17 +448,17 @@ BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( data[3] == 'e' && data[4] == ' ') || (data[0] == '.' && data[1] == 'c' && data[2] == 'o' && data[3] == 'm' && data[4] == '/')) { - size_t offset = dictionary->buckets[Hash(&data[5])]; + size_t offset = dictionary->buckets[Hash(&data[5])]; BROTLI_BOOL end = !offset; while (!end) { - DictWord w = dictionary->dict_words[offset++]; + DictWord w = dictionary->dict_words[offset++]; const size_t l = w.len & 0x1F; - const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; + const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l]; const size_t id = w.idx; end = !!(w.len & 0x80); w.len = (uint8_t)l; if (w.transform == 0 && - IsMatch(dictionary->words, w, &data[5], max_length - 5)) { + IsMatch(dictionary->words, w, &data[5], max_length - 5)) { AddMatch(id + (data[0] == ' ' ? 41 : 72) * n, l + 5, l, matches); has_found_match = BROTLI_TRUE; if (l + 5 < max_length) { diff --git a/contrib/libs/brotli/enc/static_dict.h b/contrib/libs/brotli/enc/static_dict.h index 6b5d4eb0c9..28f90f6d9b 100644 --- a/contrib/libs/brotli/enc/static_dict.h +++ b/contrib/libs/brotli/enc/static_dict.h @@ -10,16 +10,16 @@ #define BROTLI_ENC_STATIC_DICT_H_ #include "../common/dictionary.h" -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> -#include "./encoder_dict.h" +#include "./encoder_dict.h" #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif #define BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN 37 -static const uint32_t kInvalidMatch = 0xFFFFFFF; +static const uint32_t kInvalidMatch = 0xFFFFFFF; /* Matches data against static dictionary words, and for each length l, for which a match is found, updates matches[l] to be the minimum possible @@ -29,7 +29,7 @@ static const uint32_t kInvalidMatch = 0xFFFFFFF; matches array is at least BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN + 1 long all elements are initialized to kInvalidMatch */ BROTLI_INTERNAL BROTLI_BOOL BrotliFindAllStaticDictionaryMatches( - const BrotliEncoderDictionary* dictionary, + const BrotliEncoderDictionary* dictionary, const uint8_t* data, size_t min_length, size_t max_length, uint32_t* matches); diff --git a/contrib/libs/brotli/enc/static_dict_lut.h b/contrib/libs/brotli/enc/static_dict_lut.h index e299cda6d8..113f2811e9 100644 --- a/contrib/libs/brotli/enc/static_dict_lut.h +++ b/contrib/libs/brotli/enc/static_dict_lut.h @@ -23,7 +23,7 @@ typedef struct DictWord { } DictWord; static const int kDictNumBits = 15; -static const uint32_t kDictHashMul32 = 0x1E35A7BD; +static const uint32_t kDictHashMul32 = 0x1E35A7BD; static const uint16_t kStaticDictionaryBuckets[32768] = { 1,0,0,0,0,0,0,0,0,3,6,0,0,0,0,0,20,0,0,0,21,0,22,0,0,0,0,0,0,0,0,23,0,0,25,0,29, diff --git a/contrib/libs/brotli/enc/utf8_util.c b/contrib/libs/brotli/enc/utf8_util.c index 04a7805161..35600b9bee 100644 --- a/contrib/libs/brotli/enc/utf8_util.c +++ b/contrib/libs/brotli/enc/utf8_util.c @@ -25,37 +25,37 @@ static size_t BrotliParseAsUTF8( } /* 2-byte UTF8 */ if (size > 1u && - (input[0] & 0xE0) == 0xC0 && - (input[1] & 0xC0) == 0x80) { - *symbol = (((input[0] & 0x1F) << 6) | - (input[1] & 0x3F)); - if (*symbol > 0x7F) { + (input[0] & 0xE0) == 0xC0 && + (input[1] & 0xC0) == 0x80) { + *symbol = (((input[0] & 0x1F) << 6) | + (input[1] & 0x3F)); + if (*symbol > 0x7F) { return 2; } } /* 3-byte UFT8 */ if (size > 2u && - (input[0] & 0xF0) == 0xE0 && - (input[1] & 0xC0) == 0x80 && - (input[2] & 0xC0) == 0x80) { - *symbol = (((input[0] & 0x0F) << 12) | - ((input[1] & 0x3F) << 6) | - (input[2] & 0x3F)); - if (*symbol > 0x7FF) { + (input[0] & 0xF0) == 0xE0 && + (input[1] & 0xC0) == 0x80 && + (input[2] & 0xC0) == 0x80) { + *symbol = (((input[0] & 0x0F) << 12) | + ((input[1] & 0x3F) << 6) | + (input[2] & 0x3F)); + if (*symbol > 0x7FF) { return 3; } } /* 4-byte UFT8 */ if (size > 3u && - (input[0] & 0xF8) == 0xF0 && - (input[1] & 0xC0) == 0x80 && - (input[2] & 0xC0) == 0x80 && - (input[3] & 0xC0) == 0x80) { + (input[0] & 0xF8) == 0xF0 && + (input[1] & 0xC0) == 0x80 && + (input[2] & 0xC0) == 0x80 && + (input[3] & 0xC0) == 0x80) { *symbol = (((input[0] & 0x07) << 18) | - ((input[1] & 0x3F) << 12) | - ((input[2] & 0x3F) << 6) | - (input[3] & 0x3F)); - if (*symbol > 0xFFFF && *symbol <= 0x10FFFF) { + ((input[1] & 0x3F) << 12) | + ((input[2] & 0x3F) << 6) | + (input[3] & 0x3F)); + if (*symbol > 0xFFFF && *symbol <= 0x10FFFF) { return 4; } } diff --git a/contrib/libs/brotli/enc/utf8_util.h b/contrib/libs/brotli/enc/utf8_util.h index 8fda80c220..7eb75e86cb 100644 --- a/contrib/libs/brotli/enc/utf8_util.h +++ b/contrib/libs/brotli/enc/utf8_util.h @@ -9,7 +9,7 @@ #ifndef BROTLI_ENC_UTF8_UTIL_H_ #define BROTLI_ENC_UTF8_UTIL_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) diff --git a/contrib/libs/brotli/enc/write_bits.h b/contrib/libs/brotli/enc/write_bits.h index 36515a6893..2ebf20a7f0 100644 --- a/contrib/libs/brotli/enc/write_bits.h +++ b/contrib/libs/brotli/enc/write_bits.h @@ -9,7 +9,7 @@ #ifndef BROTLI_ENC_WRITE_BITS_H_ #define BROTLI_ENC_WRITE_BITS_H_ -#include "../common/platform.h" +#include "../common/platform.h" #include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) @@ -35,27 +35,27 @@ extern "C" { and locate the rest in BYTE+1, BYTE+2, etc. */ static BROTLI_INLINE void BrotliWriteBits(size_t n_bits, uint64_t bits, - size_t* BROTLI_RESTRICT pos, - uint8_t* BROTLI_RESTRICT array) { -#if defined(BROTLI_LITTLE_ENDIAN) + size_t* BROTLI_RESTRICT pos, + uint8_t* BROTLI_RESTRICT array) { +#if defined(BROTLI_LITTLE_ENDIAN) /* This branch of the code can write up to 56 bits at a time, 7 bits are lost by being perhaps already in *p and at least 1 bit is needed to initialize the bit-stream ahead (i.e. if 7 bits are in *p and we write 57 bits, then the next write will access a byte that was never initialized). */ - uint8_t* p = &array[*pos >> 3]; - uint64_t v = (uint64_t)(*p); /* Zero-extend 8 to 64 bits. */ - BROTLI_LOG(("WriteBits %2d 0x%08x%08x %10d\n", (int)n_bits, - (uint32_t)(bits >> 32), (uint32_t)(bits & 0xFFFFFFFF), - (int)*pos)); - BROTLI_DCHECK((bits >> n_bits) == 0); - BROTLI_DCHECK(n_bits <= 56); + uint8_t* p = &array[*pos >> 3]; + uint64_t v = (uint64_t)(*p); /* Zero-extend 8 to 64 bits. */ + BROTLI_LOG(("WriteBits %2d 0x%08x%08x %10d\n", (int)n_bits, + (uint32_t)(bits >> 32), (uint32_t)(bits & 0xFFFFFFFF), + (int)*pos)); + BROTLI_DCHECK((bits >> n_bits) == 0); + BROTLI_DCHECK(n_bits <= 56); v |= bits << (*pos & 7); BROTLI_UNALIGNED_STORE64LE(p, v); /* Set some bits. */ *pos += n_bits; #else - /* implicit & 0xFF is assumed for uint8_t arithmetics */ - uint8_t* array_pos = &array[*pos >> 3]; + /* implicit & 0xFF is assumed for uint8_t arithmetics */ + uint8_t* array_pos = &array[*pos >> 3]; const size_t bits_reserved_in_first_byte = (*pos & 7); size_t bits_left_to_write; bits <<= bits_reserved_in_first_byte; @@ -72,9 +72,9 @@ static BROTLI_INLINE void BrotliWriteBits(size_t n_bits, } static BROTLI_INLINE void BrotliWriteBitsPrepareStorage( - size_t pos, uint8_t* array) { - BROTLI_LOG(("WriteBitsPrepareStorage %10d\n", (int)pos)); - BROTLI_DCHECK((pos & 7) == 0); + size_t pos, uint8_t* array) { + BROTLI_LOG(("WriteBitsPrepareStorage %10d\n", (int)pos)); + BROTLI_DCHECK((pos & 7) == 0); array[pos >> 3] = 0; } diff --git a/contrib/libs/brotli/enc/ya.make b/contrib/libs/brotli/enc/ya.make index 67da82ec4d..d10a599f57 100644 --- a/contrib/libs/brotli/enc/ya.make +++ b/contrib/libs/brotli/enc/ya.make @@ -32,7 +32,7 @@ SRCS( compress_fragment_two_pass.c dictionary_hash.c encode.c - encoder_dict.c + encoder_dict.c entropy_encode.c histogram.c literal_cost.c diff --git a/contrib/libs/brotli/include/brotli/decode.h b/contrib/libs/brotli/include/brotli/decode.h index 0f5c8f9d11..d354994563 100644 --- a/contrib/libs/brotli/include/brotli/decode.h +++ b/contrib/libs/brotli/include/brotli/decode.h @@ -34,11 +34,11 @@ typedef struct BrotliDecoderStateStruct BrotliDecoderState; typedef enum { /** Decoding error, e.g. corrupted input or memory allocation problem. */ BROTLI_DECODER_RESULT_ERROR = 0, - /** Decoding successfully completed. */ + /** Decoding successfully completed. */ BROTLI_DECODER_RESULT_SUCCESS = 1, - /** Partially done; should be called again with more input. */ + /** Partially done; should be called again with more input. */ BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT = 2, - /** Partially done; should be called again with more output. */ + /** Partially done; should be called again with more output. */ BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT = 3 } BrotliDecoderResult; @@ -83,9 +83,9 @@ typedef enum { BROTLI_ERROR_CODE(_ERROR_FORMAT_, WINDOW_BITS, -13) SEPARATOR \ BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_1, -14) SEPARATOR \ BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_2, -15) SEPARATOR \ - BROTLI_ERROR_CODE(_ERROR_FORMAT_, DISTANCE, -16) SEPARATOR \ + BROTLI_ERROR_CODE(_ERROR_FORMAT_, DISTANCE, -16) SEPARATOR \ \ - /* -17..-18 codes are reserved */ \ + /* -17..-18 codes are reserved */ \ \ BROTLI_ERROR_CODE(_ERROR_, DICTIONARY_NOT_SET, -19) SEPARATOR \ BROTLI_ERROR_CODE(_ERROR_, INVALID_ARGUMENTS, -20) SEPARATOR \ @@ -135,11 +135,11 @@ typedef enum BrotliDecoderParameter { * Ring buffer is allocated according to window size, despite the real size of * the content. */ - BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION = 0, - /** - * Flag that determines if "Large Window Brotli" is used. - */ - BROTLI_DECODER_PARAM_LARGE_WINDOW = 1 + BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION = 0, + /** + * Flag that determines if "Large Window Brotli" is used. + */ + BROTLI_DECODER_PARAM_LARGE_WINDOW = 1 } BrotliDecoderParameter; /** @@ -163,11 +163,11 @@ BROTLI_DEC_API BROTLI_BOOL BrotliDecoderSetParameter( * * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the * case they are both zero, default memory allocators are used. @p opaque is - * passed to @p alloc_func and @p free_func when they are called. @p free_func - * has to return without doing anything when asked to free a NULL pointer. + * passed to @p alloc_func and @p free_func when they are called. @p free_func + * has to return without doing anything when asked to free a NULL pointer. * * @param alloc_func custom memory allocation function - * @param free_func custom memory free function + * @param free_func custom memory free function * @param opaque custom memory manager handle * @returns @c 0 if instance can not be allocated or initialized * @returns pointer to initialized ::BrotliDecoderState otherwise diff --git a/contrib/libs/brotli/include/brotli/encode.h b/contrib/libs/brotli/include/brotli/encode.h index 0ced7e55be..e7714b3cff 100644 --- a/contrib/libs/brotli/include/brotli/encode.h +++ b/contrib/libs/brotli/include/brotli/encode.h @@ -27,11 +27,11 @@ extern "C" { * @note equal to @c BROTLI_MAX_DISTANCE_BITS constant. */ #define BROTLI_MAX_WINDOW_BITS 24 -/** - * Maximal value for ::BROTLI_PARAM_LGWIN parameter - * in "Large Window Brotli" (32-bit). - */ -#define BROTLI_LARGE_MAX_WINDOW_BITS 30 +/** + * Maximal value for ::BROTLI_PARAM_LGWIN parameter + * in "Large Window Brotli" (32-bit). + */ +#define BROTLI_LARGE_MAX_WINDOW_BITS 30 /** Minimal value for ::BROTLI_PARAM_LGBLOCK parameter. */ #define BROTLI_MIN_INPUT_BLOCK_BITS 16 /** Maximal value for ::BROTLI_PARAM_LGBLOCK parameter. */ @@ -181,27 +181,27 @@ typedef enum BrotliEncoderParameter { * * The default value is 0, which means that the total input size is unknown. */ - BROTLI_PARAM_SIZE_HINT = 5, - /** - * Flag that determines if "Large Window Brotli" is used. - */ - BROTLI_PARAM_LARGE_WINDOW = 6, - /** - * Recommended number of postfix bits (NPOSTFIX). - * - * Encoder may change this value. - * - * Range is from 0 to ::BROTLI_MAX_NPOSTFIX. - */ - BROTLI_PARAM_NPOSTFIX = 7, - /** - * Recommended number of direct distance codes (NDIRECT). - * - * Encoder may change this value. - * - * Range is from 0 to (15 << NPOSTFIX) in steps of (1 << NPOSTFIX). - */ - BROTLI_PARAM_NDIRECT = 8 + BROTLI_PARAM_SIZE_HINT = 5, + /** + * Flag that determines if "Large Window Brotli" is used. + */ + BROTLI_PARAM_LARGE_WINDOW = 6, + /** + * Recommended number of postfix bits (NPOSTFIX). + * + * Encoder may change this value. + * + * Range is from 0 to ::BROTLI_MAX_NPOSTFIX. + */ + BROTLI_PARAM_NPOSTFIX = 7, + /** + * Recommended number of direct distance codes (NDIRECT). + * + * Encoder may change this value. + * + * Range is from 0 to (15 << NPOSTFIX) in steps of (1 << NPOSTFIX). + */ + BROTLI_PARAM_NDIRECT = 8 } BrotliEncoderParameter; /** @@ -234,11 +234,11 @@ BROTLI_ENC_API BROTLI_BOOL BrotliEncoderSetParameter( * * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the * case they are both zero, default memory allocators are used. @p opaque is - * passed to @p alloc_func and @p free_func when they are called. @p free_func - * has to return without doing anything when asked to free a NULL pointer. + * passed to @p alloc_func and @p free_func when they are called. @p free_func + * has to return without doing anything when asked to free a NULL pointer. * * @param alloc_func custom memory allocation function - * @param free_func custom memory free function + * @param free_func custom memory free function * @param opaque custom memory manager handle * @returns @c 0 if instance can not be allocated or initialized * @returns pointer to initialized ::BrotliEncoderState otherwise @@ -256,9 +256,9 @@ BROTLI_ENC_API void BrotliEncoderDestroyInstance(BrotliEncoderState* state); /** * Calculates the output size bound for the given @p input_size. * - * @warning Result is only valid if quality is at least @c 2 and, in - * case ::BrotliEncoderCompressStream was used, no flushes - * (::BROTLI_OPERATION_FLUSH) were performed. + * @warning Result is only valid if quality is at least @c 2 and, in + * case ::BrotliEncoderCompressStream was used, no flushes + * (::BROTLI_OPERATION_FLUSH) were performed. * * @param input_size size of projected input * @returns @c 0 if result does not fit @c size_t @@ -308,7 +308,7 @@ BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompress( * that amount. * * @p total_out, if it is not a null-pointer, will be set to the number - * of bytes compressed since the last @p state initialization. + * of bytes compressed since the last @p state initialization. * * * diff --git a/contrib/libs/brotli/include/brotli/port.h b/contrib/libs/brotli/include/brotli/port.h index 20dc2314d8..4950dbd351 100644 --- a/contrib/libs/brotli/include/brotli/port.h +++ b/contrib/libs/brotli/include/brotli/port.h @@ -4,247 +4,247 @@ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -/* Macros for compiler / platform specific API declarations. */ +/* Macros for compiler / platform specific API declarations. */ #ifndef BROTLI_COMMON_PORT_H_ #define BROTLI_COMMON_PORT_H_ -/* The following macros were borrowed from https://github.com/nemequ/hedley - * with permission of original author - Evan Nemerson <evan@nemerson.com> */ - -/* >>> >>> >>> hedley macros */ - -#define BROTLI_MAKE_VERSION(major, minor, revision) \ - (((major) * 1000000) + ((minor) * 1000) + (revision)) - -#if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__) -#define BROTLI_GNUC_VERSION \ - BROTLI_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) -#elif defined(__GNUC__) -#define BROTLI_GNUC_VERSION BROTLI_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, 0) -#endif - -#if defined(BROTLI_GNUC_VERSION) -#define BROTLI_GNUC_VERSION_CHECK(major, minor, patch) \ - (BROTLI_GNUC_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) -#else -#define BROTLI_GNUC_VERSION_CHECK(major, minor, patch) (0) -#endif - -#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) -#define BROTLI_MSVC_VERSION \ - BROTLI_MAKE_VERSION((_MSC_FULL_VER / 10000000), \ - (_MSC_FULL_VER % 10000000) / 100000, \ - (_MSC_FULL_VER % 100000) / 100) -#elif defined(_MSC_FULL_VER) -#define BROTLI_MSVC_VERSION \ - BROTLI_MAKE_VERSION((_MSC_FULL_VER / 1000000), \ - (_MSC_FULL_VER % 1000000) / 10000, \ - (_MSC_FULL_VER % 10000) / 10) -#elif defined(_MSC_VER) -#define BROTLI_MSVC_VERSION \ - BROTLI_MAKE_VERSION(_MSC_VER / 100, _MSC_VER % 100, 0) -#endif - -#if !defined(_MSC_VER) -#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) (0) -#elif defined(_MSC_VER) && (_MSC_VER >= 1400) -#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) \ - (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch))) -#elif defined(_MSC_VER) && (_MSC_VER >= 1200) -#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) \ - (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch))) -#else -#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) \ - (_MSC_VER >= ((major * 100) + (minor))) -#endif - -#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) -#define BROTLI_INTEL_VERSION \ - BROTLI_MAKE_VERSION(__INTEL_COMPILER / 100, \ - __INTEL_COMPILER % 100, \ - __INTEL_COMPILER_UPDATE) -#elif defined(__INTEL_COMPILER) -#define BROTLI_INTEL_VERSION \ - BROTLI_MAKE_VERSION(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0) -#endif - -#if defined(BROTLI_INTEL_VERSION) -#define BROTLI_INTEL_VERSION_CHECK(major, minor, patch) \ - (BROTLI_INTEL_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) -#else -#define BROTLI_INTEL_VERSION_CHECK(major, minor, patch) (0) -#endif - -#if defined(__PGI) && \ - defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__) -#define BROTLI_PGI_VERSION \ - BROTLI_MAKE_VERSION(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) -#endif - -#if defined(BROTLI_PGI_VERSION) -#define BROTLI_PGI_VERSION_CHECK(major, minor, patch) \ - (BROTLI_PGI_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) -#else -#define BROTLI_PGI_VERSION_CHECK(major, minor, patch) (0) +/* The following macros were borrowed from https://github.com/nemequ/hedley + * with permission of original author - Evan Nemerson <evan@nemerson.com> */ + +/* >>> >>> >>> hedley macros */ + +#define BROTLI_MAKE_VERSION(major, minor, revision) \ + (((major) * 1000000) + ((minor) * 1000) + (revision)) + +#if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__) +#define BROTLI_GNUC_VERSION \ + BROTLI_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) +#elif defined(__GNUC__) +#define BROTLI_GNUC_VERSION BROTLI_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, 0) #endif -#if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000) -#define BROTLI_SUNPRO_VERSION \ - BROTLI_MAKE_VERSION( \ - (((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), \ - (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), \ - (__SUNPRO_C & 0xf) * 10) -#elif defined(__SUNPRO_C) -#define BROTLI_SUNPRO_VERSION \ - BROTLI_MAKE_VERSION((__SUNPRO_C >> 8) & 0xf, \ - (__SUNPRO_C >> 4) & 0xf, \ - (__SUNPRO_C) & 0xf) -#elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000) -#define BROTLI_SUNPRO_VERSION \ - BROTLI_MAKE_VERSION( \ - (((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), \ - (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), \ - (__SUNPRO_CC & 0xf) * 10) -#elif defined(__SUNPRO_CC) -#define BROTLI_SUNPRO_VERSION \ - BROTLI_MAKE_VERSION((__SUNPRO_CC >> 8) & 0xf, \ - (__SUNPRO_CC >> 4) & 0xf, \ - (__SUNPRO_CC) & 0xf) +#if defined(BROTLI_GNUC_VERSION) +#define BROTLI_GNUC_VERSION_CHECK(major, minor, patch) \ + (BROTLI_GNUC_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#else +#define BROTLI_GNUC_VERSION_CHECK(major, minor, patch) (0) #endif -#if defined(BROTLI_SUNPRO_VERSION) -#define BROTLI_SUNPRO_VERSION_CHECK(major, minor, patch) \ - (BROTLI_SUNPRO_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) -#else -#define BROTLI_SUNPRO_VERSION_CHECK(major, minor, patch) (0) -#endif - -#if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION) -#define BROTLI_ARM_VERSION \ - BROTLI_MAKE_VERSION((__ARMCOMPILER_VERSION / 1000000), \ - (__ARMCOMPILER_VERSION % 1000000) / 10000, \ - (__ARMCOMPILER_VERSION % 10000) / 100) -#elif defined(__CC_ARM) && defined(__ARMCC_VERSION) -#define BROTLI_ARM_VERSION \ - BROTLI_MAKE_VERSION((__ARMCC_VERSION / 1000000), \ - (__ARMCC_VERSION % 1000000) / 10000, \ - (__ARMCC_VERSION % 10000) / 100) +#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) +#define BROTLI_MSVC_VERSION \ + BROTLI_MAKE_VERSION((_MSC_FULL_VER / 10000000), \ + (_MSC_FULL_VER % 10000000) / 100000, \ + (_MSC_FULL_VER % 100000) / 100) +#elif defined(_MSC_FULL_VER) +#define BROTLI_MSVC_VERSION \ + BROTLI_MAKE_VERSION((_MSC_FULL_VER / 1000000), \ + (_MSC_FULL_VER % 1000000) / 10000, \ + (_MSC_FULL_VER % 10000) / 10) +#elif defined(_MSC_VER) +#define BROTLI_MSVC_VERSION \ + BROTLI_MAKE_VERSION(_MSC_VER / 100, _MSC_VER % 100, 0) #endif -#if defined(BROTLI_ARM_VERSION) -#define BROTLI_ARM_VERSION_CHECK(major, minor, patch) \ - (BROTLI_ARM_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#if !defined(_MSC_VER) +#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) (0) +#elif defined(_MSC_VER) && (_MSC_VER >= 1400) +#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) \ + (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch))) +#elif defined(_MSC_VER) && (_MSC_VER >= 1200) +#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) \ + (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch))) #else -#define BROTLI_ARM_VERSION_CHECK(major, minor, patch) (0) +#define BROTLI_MSVC_VERSION_CHECK(major, minor, patch) \ + (_MSC_VER >= ((major * 100) + (minor))) #endif -#if defined(__ibmxl__) -#define BROTLI_IBM_VERSION \ - BROTLI_MAKE_VERSION(__ibmxl_version__, \ - __ibmxl_release__, \ - __ibmxl_modification__) -#elif defined(__xlC__) && defined(__xlC_ver__) -#define BROTLI_IBM_VERSION \ - BROTLI_MAKE_VERSION(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff) -#elif defined(__xlC__) -#define BROTLI_IBM_VERSION BROTLI_MAKE_VERSION(__xlC__ >> 8, __xlC__ & 0xff, 0) -#endif - -#if defined(BROTLI_IBM_VERSION) -#define BROTLI_IBM_VERSION_CHECK(major, minor, patch) \ - (BROTLI_IBM_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) +#define BROTLI_INTEL_VERSION \ + BROTLI_MAKE_VERSION(__INTEL_COMPILER / 100, \ + __INTEL_COMPILER % 100, \ + __INTEL_COMPILER_UPDATE) +#elif defined(__INTEL_COMPILER) +#define BROTLI_INTEL_VERSION \ + BROTLI_MAKE_VERSION(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0) +#endif + +#if defined(BROTLI_INTEL_VERSION) +#define BROTLI_INTEL_VERSION_CHECK(major, minor, patch) \ + (BROTLI_INTEL_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) #else -#define BROTLI_IBM_VERSION_CHECK(major, minor, patch) (0) +#define BROTLI_INTEL_VERSION_CHECK(major, minor, patch) (0) #endif -#if defined(__TI_COMPILER_VERSION__) -#define BROTLI_TI_VERSION \ - BROTLI_MAKE_VERSION((__TI_COMPILER_VERSION__ / 1000000), \ - (__TI_COMPILER_VERSION__ % 1000000) / 1000, \ - (__TI_COMPILER_VERSION__ % 1000)) -#endif - -#if defined(BROTLI_TI_VERSION) -#define BROTLI_TI_VERSION_CHECK(major, minor, patch) \ - (BROTLI_TI_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#if defined(__PGI) && \ + defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__) +#define BROTLI_PGI_VERSION \ + BROTLI_MAKE_VERSION(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) +#endif + +#if defined(BROTLI_PGI_VERSION) +#define BROTLI_PGI_VERSION_CHECK(major, minor, patch) \ + (BROTLI_PGI_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) #else -#define BROTLI_TI_VERSION_CHECK(major, minor, patch) (0) +#define BROTLI_PGI_VERSION_CHECK(major, minor, patch) (0) #endif -#if defined(__IAR_SYSTEMS_ICC__) -#if __VER__ > 1000 -#define BROTLI_IAR_VERSION \ - BROTLI_MAKE_VERSION((__VER__ / 1000000), \ - (__VER__ / 1000) % 1000, \ - (__VER__ % 1000)) +#if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000) +#define BROTLI_SUNPRO_VERSION \ + BROTLI_MAKE_VERSION( \ + (((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), \ + (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), \ + (__SUNPRO_C & 0xf) * 10) +#elif defined(__SUNPRO_C) +#define BROTLI_SUNPRO_VERSION \ + BROTLI_MAKE_VERSION((__SUNPRO_C >> 8) & 0xf, \ + (__SUNPRO_C >> 4) & 0xf, \ + (__SUNPRO_C) & 0xf) +#elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000) +#define BROTLI_SUNPRO_VERSION \ + BROTLI_MAKE_VERSION( \ + (((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), \ + (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), \ + (__SUNPRO_CC & 0xf) * 10) +#elif defined(__SUNPRO_CC) +#define BROTLI_SUNPRO_VERSION \ + BROTLI_MAKE_VERSION((__SUNPRO_CC >> 8) & 0xf, \ + (__SUNPRO_CC >> 4) & 0xf, \ + (__SUNPRO_CC) & 0xf) +#endif + +#if defined(BROTLI_SUNPRO_VERSION) +#define BROTLI_SUNPRO_VERSION_CHECK(major, minor, patch) \ + (BROTLI_SUNPRO_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#else +#define BROTLI_SUNPRO_VERSION_CHECK(major, minor, patch) (0) +#endif + +#if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION) +#define BROTLI_ARM_VERSION \ + BROTLI_MAKE_VERSION((__ARMCOMPILER_VERSION / 1000000), \ + (__ARMCOMPILER_VERSION % 1000000) / 10000, \ + (__ARMCOMPILER_VERSION % 10000) / 100) +#elif defined(__CC_ARM) && defined(__ARMCC_VERSION) +#define BROTLI_ARM_VERSION \ + BROTLI_MAKE_VERSION((__ARMCC_VERSION / 1000000), \ + (__ARMCC_VERSION % 1000000) / 10000, \ + (__ARMCC_VERSION % 10000) / 100) +#endif + +#if defined(BROTLI_ARM_VERSION) +#define BROTLI_ARM_VERSION_CHECK(major, minor, patch) \ + (BROTLI_ARM_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#else +#define BROTLI_ARM_VERSION_CHECK(major, minor, patch) (0) +#endif + +#if defined(__ibmxl__) +#define BROTLI_IBM_VERSION \ + BROTLI_MAKE_VERSION(__ibmxl_version__, \ + __ibmxl_release__, \ + __ibmxl_modification__) +#elif defined(__xlC__) && defined(__xlC_ver__) +#define BROTLI_IBM_VERSION \ + BROTLI_MAKE_VERSION(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff) +#elif defined(__xlC__) +#define BROTLI_IBM_VERSION BROTLI_MAKE_VERSION(__xlC__ >> 8, __xlC__ & 0xff, 0) +#endif + +#if defined(BROTLI_IBM_VERSION) +#define BROTLI_IBM_VERSION_CHECK(major, minor, patch) \ + (BROTLI_IBM_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) #else -#define BROTLI_IAR_VERSION BROTLI_MAKE_VERSION(VER / 100, __VER__ % 100, 0) -#endif +#define BROTLI_IBM_VERSION_CHECK(major, minor, patch) (0) #endif -#if defined(BROTLI_IAR_VERSION) -#define BROTLI_IAR_VERSION_CHECK(major, minor, patch) \ - (BROTLI_IAR_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#if defined(__TI_COMPILER_VERSION__) +#define BROTLI_TI_VERSION \ + BROTLI_MAKE_VERSION((__TI_COMPILER_VERSION__ / 1000000), \ + (__TI_COMPILER_VERSION__ % 1000000) / 1000, \ + (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(BROTLI_TI_VERSION) +#define BROTLI_TI_VERSION_CHECK(major, minor, patch) \ + (BROTLI_TI_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) #else -#define BROTLI_IAR_VERSION_CHECK(major, minor, patch) (0) -#endif - -#if defined(__TINYC__) -#define BROTLI_TINYC_VERSION \ - BROTLI_MAKE_VERSION(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100) -#endif - -#if defined(BROTLI_TINYC_VERSION) -#define BROTLI_TINYC_VERSION_CHECK(major, minor, patch) \ - (BROTLI_TINYC_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) -#else -#define BROTLI_TINYC_VERSION_CHECK(major, minor, patch) (0) -#endif - -#if defined(__has_attribute) -#define BROTLI_GNUC_HAS_ATTRIBUTE(attribute, major, minor, patch) \ - __has_attribute(attribute) -#else -#define BROTLI_GNUC_HAS_ATTRIBUTE(attribute, major, minor, patch) \ - BROTLI_GNUC_VERSION_CHECK(major, minor, patch) -#endif - -#if defined(__has_builtin) -#define BROTLI_GNUC_HAS_BUILTIN(builtin, major, minor, patch) \ - __has_builtin(builtin) -#else -#define BROTLI_GNUC_HAS_BUILTIN(builtin, major, minor, patch) \ - BROTLI_GNUC_VERSION_CHECK(major, minor, patch) +#define BROTLI_TI_VERSION_CHECK(major, minor, patch) (0) #endif +#if defined(__IAR_SYSTEMS_ICC__) +#if __VER__ > 1000 +#define BROTLI_IAR_VERSION \ + BROTLI_MAKE_VERSION((__VER__ / 1000000), \ + (__VER__ / 1000) % 1000, \ + (__VER__ % 1000)) +#else +#define BROTLI_IAR_VERSION BROTLI_MAKE_VERSION(VER / 100, __VER__ % 100, 0) +#endif +#endif + +#if defined(BROTLI_IAR_VERSION) +#define BROTLI_IAR_VERSION_CHECK(major, minor, patch) \ + (BROTLI_IAR_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#else +#define BROTLI_IAR_VERSION_CHECK(major, minor, patch) (0) +#endif + +#if defined(__TINYC__) +#define BROTLI_TINYC_VERSION \ + BROTLI_MAKE_VERSION(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100) +#endif + +#if defined(BROTLI_TINYC_VERSION) +#define BROTLI_TINYC_VERSION_CHECK(major, minor, patch) \ + (BROTLI_TINYC_VERSION >= BROTLI_MAKE_VERSION(major, minor, patch)) +#else +#define BROTLI_TINYC_VERSION_CHECK(major, minor, patch) (0) +#endif + +#if defined(__has_attribute) +#define BROTLI_GNUC_HAS_ATTRIBUTE(attribute, major, minor, patch) \ + __has_attribute(attribute) +#else +#define BROTLI_GNUC_HAS_ATTRIBUTE(attribute, major, minor, patch) \ + BROTLI_GNUC_VERSION_CHECK(major, minor, patch) +#endif + +#if defined(__has_builtin) +#define BROTLI_GNUC_HAS_BUILTIN(builtin, major, minor, patch) \ + __has_builtin(builtin) +#else +#define BROTLI_GNUC_HAS_BUILTIN(builtin, major, minor, patch) \ + BROTLI_GNUC_VERSION_CHECK(major, minor, patch) +#endif + #if defined(_WIN32) || defined(__CYGWIN__) -#define BROTLI_PUBLIC -#elif BROTLI_GNUC_VERSION_CHECK(3, 3, 0) || \ - BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ - BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ - BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ - BROTLI_IBM_VERSION_CHECK(13, 1, 0) || \ - BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ - (BROTLI_TI_VERSION_CHECK(7, 3, 0) && \ - defined(__TI_GNU_ATTRIBUTE_SUPPORT__) && defined(__TI_EABI__)) -#define BROTLI_PUBLIC __attribute__ ((visibility ("default"))) +#define BROTLI_PUBLIC +#elif BROTLI_GNUC_VERSION_CHECK(3, 3, 0) || \ + BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ + BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ + BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ + BROTLI_IBM_VERSION_CHECK(13, 1, 0) || \ + BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ + (BROTLI_TI_VERSION_CHECK(7, 3, 0) && \ + defined(__TI_GNU_ATTRIBUTE_SUPPORT__) && defined(__TI_EABI__)) +#define BROTLI_PUBLIC __attribute__ ((visibility ("default"))) #else -#define BROTLI_PUBLIC +#define BROTLI_PUBLIC #endif -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ - !defined(__STDC_NO_VLA__) && !defined(__cplusplus) && \ - !defined(__PGI) && !defined(__PGIC__) && !defined(__TINYC__) -#define BROTLI_ARRAY_PARAM(name) (name) -#else -#define BROTLI_ARRAY_PARAM(name) +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ + !defined(__STDC_NO_VLA__) && !defined(__cplusplus) && \ + !defined(__PGI) && !defined(__PGIC__) && !defined(__TINYC__) +#define BROTLI_ARRAY_PARAM(name) (name) +#else +#define BROTLI_ARRAY_PARAM(name) #endif -/* <<< <<< <<< end of hedley macros. */ - -#if defined(BROTLI_SHARED_COMPILATION) -#if defined(_WIN32) +/* <<< <<< <<< end of hedley macros. */ + +#if defined(BROTLI_SHARED_COMPILATION) +#if defined(_WIN32) #if defined(BROTLICOMMON_SHARED_COMPILATION) #define BROTLI_COMMON_API __declspec(dllexport) #else @@ -260,12 +260,12 @@ #else #define BROTLI_ENC_API __declspec(dllimport) #endif /* BROTLIENC_SHARED_COMPILATION */ -#else /* _WIN32 */ -#define BROTLI_COMMON_API BROTLI_PUBLIC -#define BROTLI_DEC_API BROTLI_PUBLIC -#define BROTLI_ENC_API BROTLI_PUBLIC -#endif /* _WIN32 */ -#else /* BROTLI_SHARED_COMPILATION */ +#else /* _WIN32 */ +#define BROTLI_COMMON_API BROTLI_PUBLIC +#define BROTLI_DEC_API BROTLI_PUBLIC +#define BROTLI_ENC_API BROTLI_PUBLIC +#endif /* _WIN32 */ +#else /* BROTLI_SHARED_COMPILATION */ #define BROTLI_COMMON_API #define BROTLI_DEC_API #define BROTLI_ENC_API |