Panchajanya1999 6dba37fe76
include/linux: lz4: Increase LZ4 memory usage to 64KB
64KB perfectly fits into Kryo 460 Silver L1 cache. Improves
compression ratio too.

Change-Id: I579a91c7bdb4ee4ac73bd86871b7f6a683b856f8
Signed-off-by: Panchajanya1999 <panchajanya@azure-dev.live>
Signed-off-by: Cyber Knight <cyberknight755@gmail.com>
Signed-off-by: azrim <mirzaspc@gmail.com>
2022-06-10 15:40:50 +07:00

257 lines
10 KiB
C

/* LZ4 Kernel Interface
*
* Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
* Copyright (C) 2016, Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This file is based on the original header file
* for LZ4 - Fast LZ compression algorithm.
*
* LZ4 - Fast LZ compression algorithm
* Copyright (C) 2011-2016, Yann Collet.
* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* You can contact the author at :
* - LZ4 homepage : http://www.lz4.org
* - LZ4 source repository : https://github.com/lz4/lz4
*/
#ifndef __LZ4_H__
#define __LZ4_H__
#include <linux/types.h>
#include <linux/string.h> /* memset, memcpy */
/*-************************************************************************
* CONSTANTS
**************************************************************************/
/*
* LZ4_MEMORY_USAGE :
* Memory usage formula : N->2^N Bytes
* (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
* Increasing memory usage improves compression ratio
* Reduced memory usage can improve speed, due to cache effect
* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
*/
#define LZ4_MEMORY_USAGE 16
#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
#define LZ4_COMPRESSBOUND(isize) (\
(unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \
? 0 \
: (isize) + ((isize)/255) + 16)
#define LZ4_ACCELERATION_DEFAULT 1
#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG)
#define LZ4HC_MIN_CLEVEL 3
#define LZ4HC_DEFAULT_CLEVEL 9
#define LZ4HC_MAX_CLEVEL 16
#define LZ4HC_DICTIONARY_LOGSIZE 16
#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE)
#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1)
#define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE - 1)
#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG)
#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
/*-************************************************************************
* STREAMING CONSTANTS AND STRUCTURES
**************************************************************************/
#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE - 3)) + 4)
#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
#define LZ4_STREAMHCSIZE 262192
#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t))
#define LZ4_STREAMDECODESIZE_U64 4
#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \
sizeof(unsigned long long))
/*
* LZ4_stream_t - information structure to track an LZ4 stream.
*/
typedef struct {
uint32_t hashTable[LZ4_HASH_SIZE_U32];
uint32_t currentOffset;
uint32_t initCheck;
const uint8_t *dictionary;
uint8_t *bufferStart;
uint32_t dictSize;
} LZ4_stream_t_internal;
typedef union {
unsigned long long table[LZ4_STREAMSIZE_U64];
LZ4_stream_t_internal internal_donotuse;
} LZ4_stream_t;
/*
* LZ4_streamHC_t - information structure to track an LZ4HC stream.
*/
typedef struct {
unsigned int hashTable[LZ4HC_HASHTABLESIZE];
unsigned short chainTable[LZ4HC_MAXD];
/* next block to continue on current prefix */
const unsigned char *end;
/* All index relative to this position */
const unsigned char *base;
/* alternate base for extDict */
const unsigned char *dictBase;
/* below that point, need extDict */
unsigned int dictLimit;
/* below that point, no more dict */
unsigned int lowLimit;
/* index from which to continue dict update */
unsigned int nextToUpdate;
unsigned int compressionLevel;
} LZ4HC_CCtx_internal;
typedef union {
size_t table[LZ4_STREAMHCSIZE_SIZET];
LZ4HC_CCtx_internal internal_donotuse;
} LZ4_streamHC_t;
/*-************************************************************************
* SIZE OF STATE
**************************************************************************/
#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE
#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE
/*-************************************************************************
* Compression Functions
**************************************************************************/
/**
* LZ4_compressBound() - Max. output size in worst case szenarios
* @isize: Size of the input data
*
* Return: Max. size LZ4 may output in a "worst case" szenario
* (data not compressible)
*/
static inline int LZ4_compressBound(size_t isize)
{
return LZ4_COMPRESSBOUND(isize);
}
/**
* LZ4_compress_default() - Compress data from source to dest
* @source: source address of the original data
* @dest: output buffer address of the compressed data
* @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
* @maxOutputSize: full or partial size of buffer 'dest'
* which must be already allocated
* @wrkmem: address of the working memory.
* This requires 'workmem' of LZ4_MEM_COMPRESS.
*
* Compresses 'sourceSize' bytes from buffer 'source'
* into already allocated 'dest' buffer of size 'maxOutputSize'.
* Compression is guaranteed to succeed if
* 'maxOutputSize' >= LZ4_compressBound(inputSize).
* It also runs faster, so it's a recommended setting.
* If the function cannot compress 'source' into a more limited 'dest' budget,
* compression stops *immediately*, and the function result is zero.
* As a consequence, 'dest' content is not valid.
*
* Return: Number of bytes written into buffer 'dest'
* (necessarily <= maxOutputSize) or 0 if compression fails
*/
int LZ4_compress_default(const char *source, char *dest, int inputSize,
int maxOutputSize, void *wrkmem);
/*-************************************************************************
* Decompression Functions
**************************************************************************/
/**
* LZ4_decompress_fast() - Decompresses data from 'source' into 'dest'
* @source: source address of the compressed data
* @dest: output buffer address of the uncompressed data
* which must be already allocated with 'originalSize' bytes
* @originalSize: is the original and therefore uncompressed size
*
* Decompresses data from 'source' into 'dest'.
* This function fully respect memory boundaries for properly formed
* compressed data.
* It is a bit faster than LZ4_decompress_safe().
* However, it does not provide any protection against intentionally
* modified data stream (malicious input).
* Use this function in trusted environment only
* (data to decode comes from a trusted source).
*
* Return: number of bytes read from the source buffer
* or a negative result if decompression fails.
*/
int LZ4_decompress_fast(const char *source, char *dest, int originalSize);
/**
* LZ4_decompress_safe() - Decompression protected against buffer overflow
* @source: source address of the compressed data
* @dest: output buffer address of the uncompressed data
* which must be already allocated
* @compressedSize: is the precise full size of the compressed block
* @maxDecompressedSize: is the size of 'dest' buffer
*
* Decompresses data from 'source' into 'dest'.
* If the source stream is detected malformed, the function will
* stop decoding and return a negative result.
* This function is protected against buffer overflow exploits,
* including malicious data packets. It never writes outside output buffer,
* nor reads outside input buffer.
*
* Return: number of bytes decompressed into destination buffer
* (necessarily <= maxDecompressedSize)
* or a negative result in case of error
*/
int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,
int maxDecompressedSize);
/*-************************************************************************
* LZ4 HC Compression
**************************************************************************/
/**
* LZ4_compress_HC() - Compress data from `src` into `dst`, using HC algorithm
* @src: source address of the original data
* @dst: output buffer address of the compressed data
* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
* @dstCapacity: full or partial size of buffer 'dst',
* which must be already allocated
* @compressionLevel: Recommended values are between 4 and 9, although any
* value between 1 and LZ4HC_MAX_CLEVEL will work.
* Values >LZ4HC_MAX_CLEVEL behave the same as 16.
* @wrkmem: address of the working memory.
* This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS.
*
* Compress data from 'src' into 'dst', using the more powerful
* but slower "HC" algorithm. Compression is guaranteed to succeed if
* `dstCapacity >= LZ4_compressBound(srcSize)
*
* Return : the number of bytes written into 'dst' or 0 if compression fails.
*/
int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity,
int compressionLevel, void *wrkmem);
#endif