Xindong Xu | b84a662 | 2023-04-04 08:56:00 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL 2.0+ OR BSD-3-Clause */ |
| 2 | /* LZ4 Kernel Interface |
| 3 | * |
| 4 | * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com> |
| 5 | * Copyright (C) 2016, Sven Schmidt <4sschmid@informatik.uni-hamburg.de> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This file is based on the original header file |
| 12 | * for LZ4 - Fast LZ compression algorithm. |
| 13 | * |
| 14 | * LZ4 - Fast LZ compression algorithm |
| 15 | * Copyright (C) 2011-2016, Yann Collet. |
| 16 | * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions are |
| 19 | * met: |
| 20 | * * Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * * Redistributions in binary form must reproduce the above |
| 23 | * copyright notice, this list of conditions and the following disclaimer |
| 24 | * in the documentation and/or other materials provided with the |
| 25 | * distribution. |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 29 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 30 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 31 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 32 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | * You can contact the author at : |
| 38 | * - LZ4 homepage : http://www.lz4.org |
| 39 | * - LZ4 source repository : https://github.com/lz4/lz4 |
| 40 | */ |
| 41 | |
| 42 | #ifndef __LZ4_H__ |
| 43 | #define __LZ4_H__ |
| 44 | |
| 45 | #include <linux/types.h> |
| 46 | #include <linux/string.h> /* memset, memcpy */ |
| 47 | |
| 48 | /*-************************************************************************ |
| 49 | * CONSTANTS |
| 50 | **************************************************************************/ |
| 51 | /* |
| 52 | * LZ4_MEMORY_USAGE : |
| 53 | * Memory usage formula : N->2^N Bytes |
| 54 | * (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) |
| 55 | * Increasing memory usage improves compression ratio |
| 56 | * Reduced memory usage can improve speed, due to cache effect |
| 57 | * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache |
| 58 | */ |
| 59 | #define LZ4_MEMORY_USAGE 14 |
| 60 | |
| 61 | #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ |
| 62 | #define LZ4_COMPRESSBOUND(isize) (\ |
| 63 | (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \ |
| 64 | ? 0 \ |
| 65 | : (isize) + ((isize) / 255) + 16) |
| 66 | |
| 67 | #define LZ4_ACCELERATION_DEFAULT 1 |
| 68 | #define LZ4_HASHLOG (LZ4_MEMORY_USAGE - 2) |
| 69 | #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) |
| 70 | #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) |
| 71 | |
| 72 | #define LZ4HC_MIN_CLEVEL 3 |
| 73 | #define LZ4HC_DEFAULT_CLEVEL 9 |
| 74 | #define LZ4HC_MAX_CLEVEL 16 |
| 75 | |
| 76 | #define LZ4HC_DICTIONARY_LOGSIZE 16 |
| 77 | #define LZ4HC_MAXD (1 << LZ4HC_DICTIONARY_LOGSIZE) |
| 78 | #define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) |
| 79 | #define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE - 1) |
| 80 | #define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) |
| 81 | #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) |
| 82 | |
| 83 | /*-************************************************************************ |
| 84 | * STREAMING CONSTANTS AND STRUCTURES |
| 85 | **************************************************************************/ |
| 86 | #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE - 3)) + 4) |
| 87 | #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) |
| 88 | |
| 89 | #define LZ4_STREAMHCSIZE 262192 |
| 90 | #define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t)) |
| 91 | |
| 92 | #define LZ4_STREAMDECODESIZE_U64 4 |
| 93 | #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \ |
| 94 | sizeof(unsigned long long)) |
| 95 | |
| 96 | /* |
| 97 | * LZ4_stream_t - information structure to track an LZ4 stream. |
| 98 | */ |
| 99 | typedef struct { |
| 100 | uint32_t hashTable[LZ4_HASH_SIZE_U32]; |
| 101 | uint32_t currentOffset; |
| 102 | uint32_t initCheck; |
| 103 | const uint8_t *dictionary; |
| 104 | uint8_t *bufferStart; |
| 105 | uint32_t dictSize; |
| 106 | } LZ4_stream_t_internal; |
| 107 | typedef union { |
| 108 | unsigned long long table[LZ4_STREAMSIZE_U64]; |
| 109 | LZ4_stream_t_internal internal_donotuse; |
| 110 | } LZ4_stream_t; |
| 111 | |
| 112 | /* |
| 113 | * LZ4_streamHC_t - information structure to track an LZ4HC stream. |
| 114 | */ |
| 115 | typedef struct { |
| 116 | unsigned int hashTable[LZ4HC_HASHTABLESIZE]; |
| 117 | unsigned short chainTable[LZ4HC_MAXD]; |
| 118 | /* next block to continue on current prefix */ |
| 119 | const unsigned char *end; |
| 120 | /* All index relative to this position */ |
| 121 | const unsigned char *base; |
| 122 | /* alternate base for extDict */ |
| 123 | const unsigned char *dictBase; |
| 124 | /* below that point, need extDict */ |
| 125 | unsigned int dictLimit; |
| 126 | /* below that point, no more dict */ |
| 127 | unsigned int lowLimit; |
| 128 | /* index from which to continue dict update */ |
| 129 | unsigned int nextToUpdate; |
| 130 | unsigned int compressionLevel; |
| 131 | } LZ4HC_CCtx_internal; |
| 132 | typedef union { |
| 133 | size_t table[LZ4_STREAMHCSIZE_SIZET]; |
| 134 | LZ4HC_CCtx_internal internal_donotuse; |
| 135 | } LZ4_streamHC_t; |
| 136 | |
| 137 | /* |
| 138 | * LZ4_streamDecode_t - information structure to track an |
| 139 | * LZ4 stream during decompression. |
| 140 | * |
| 141 | * init this structure using LZ4_setStreamDecode (or memset()) before first use |
| 142 | */ |
| 143 | typedef struct { |
| 144 | const uint8_t *externalDict; |
| 145 | size_t extDictSize; |
| 146 | const uint8_t *prefixEnd; |
| 147 | size_t prefixSize; |
| 148 | } LZ4_streamDecode_t_internal; |
| 149 | typedef union { |
| 150 | unsigned long long table[LZ4_STREAMDECODESIZE_U64]; |
| 151 | LZ4_streamDecode_t_internal internal_donotuse; |
| 152 | } LZ4_streamDecode_t; |
| 153 | |
| 154 | /*-************************************************************************ |
| 155 | * SIZE OF STATE |
| 156 | **************************************************************************/ |
| 157 | #define LZ4_MEM_COMPRESS LZ4_STREAMSIZE |
| 158 | #define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE |
| 159 | |
| 160 | /*-************************************************************************ |
| 161 | * Compression Functions |
| 162 | **************************************************************************/ |
| 163 | |
| 164 | /** |
| 165 | * LZ4_compressBound() - Max. output size in worst case szenarios |
| 166 | * @isize: Size of the input data |
| 167 | * |
| 168 | * Return: Max. size LZ4 may output in a "worst case" scenario |
| 169 | * (data not compressible) |
| 170 | */ |
| 171 | static inline int LZ4_compressBound(size_t isize) |
| 172 | { |
| 173 | return LZ4_COMPRESSBOUND(isize); |
| 174 | } |
| 175 | |
| 176 | #endif |