Commit 4e1a33b1 authored by Sven Schmidt's avatar Sven Schmidt Committed by Linus Torvalds

lib: update LZ4 compressor module

Patch series "Update LZ4 compressor module", v7.

This patchset updates the LZ4 compression module to a version based on
LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast
which provides an "acceleration" parameter as a tradeoff between high
compression ratio and high compression speed.

We want to use LZ4 fast in order to support compression in lustre and
(mostly, based on that) investigate data reduction techniques in behalf
of storage systems.

Also, it will be useful for other users of LZ4 compression, as with LZ4
fast it is possible to enable applications to use fast and/or high
compression depending on the usecase.  For instance, ZRAM is offering a
LZ4 backend and could benefit from an updated LZ4 in the kernel.

LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4 Source version: 1.7.3

Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|----------
Compressor      | Compression  | Decompression  | Ratio
----------------|--------------|----------------|----------
memcpy          |  4200 MB/s   |  4200 MB/s     | 1.000
LZ4 fast 50     |  1080 MB/s   |  2650 MB/s     | 1.375
LZ4 fast 17     |   680 MB/s   |  2220 MB/s     | 1.607
LZ4 fast 5      |   475 MB/s   |  1920 MB/s     | 1.886
LZ4 default     |   385 MB/s   |  1850 MB/s     | 2.101

[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html

[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5] lib/decompress_unlz4: Change module to work with new LZ4 module version
[PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version
[PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
[PATCH 5/5] lib/lz4: Remove back-compat wrappers

This patch (of 5):

Update the LZ4 kernel module to LZ4 v1.7.3 by Yann Collet.  The kernel
module is inspired by the previous work by Chanho Min.  The updated LZ4
module will not break existing code since the patchset contains
appropriate changes.

API changes:

New method LZ4_compress_fast which differs from the variant available in
kernel by the new acceleration parameter, allowing to trade compression
ratio for more compression speed and vice versa.

LZ4_decompress_fast is the respective decompression method, featuring a
very fast decoder (multiple GB/s per core), able to reach RAM speed in
multi-core systems.  The decompressor allows to decompress data
compressed with LZ4 fast as well as the LZ4 HC (high compression)
algorithm.

Also the useful functions LZ4_decompress_safe_partial and
LZ4_compress_destsize were added.  The latter reverses the logic by
trying to compress as much data as possible from source to dest while
the former aims to decompress partial blocks of data.

A bunch of streaming functions were also added which allow
compressig/decompressing data in multiple steps (so called "streaming
mode").

The methods lz4_compress and lz4_decompress_unknownoutputsize are now
known as LZ4_compress_default respectivley LZ4_decompress_safe.  The old
methods will be removed since there's no callers left in the code.

[arnd@arndb.de: fix KERNEL_LZ4 support]
  Link: http://lkml.kernel.org/r/20170208211946.2839649-1-arnd@arndb.de
[akpm@linux-foundation.org: simplify]
[akpm@linux-foundation.org: fix the simplification]
[4sschmid@informatik.uni-hamburg.de: fix performance regressions]
  Link: http://lkml.kernel.org/r/1486898178-17125-2-git-send-email-4sschmid@informatik.uni-hamburg.de
[4sschmid@informatik.uni-hamburg.de: v8]
  Link: http://lkml.kernel.org/r/1487182598-15351-2-git-send-email-4sschmid@informatik.uni-hamburg.de
Link: http://lkml.kernel.org/r/1486321748-19085-2-git-send-email-4sschmid@informatik.uni-hamburg.deSigned-off-by: default avatarSven Schmidt <4sschmid@informatik.uni-hamburg.de>
Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
Cc: Bongkyu Kim <bongkyu.kim@lge.com>
Cc: Rui Salvaterra <rsalvaterra@gmail.com>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: David S. Miller <davem@davemloft.net>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Tony Luck <tony.luck@intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 8893f519
#ifndef __LZ4_H__ /* LZ4 Kernel Interface
#define __LZ4_H__
/*
* LZ4 Kernel Interface
* *
* Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com> * 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 * 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 * it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. * 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
*/ */
#define LZ4_MEM_COMPRESS (16384)
#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *)))
#ifndef __LZ4_H__
#define __LZ4_H__
#include <linux/types.h>
#include <linux/string.h> /* memset, memcpy */
/*-************************************************************************
* CONSTANTS
**************************************************************************/
/* /*
* lz4_compressbound() * LZ4_MEMORY_USAGE :
* Provides the maximum size that LZ4 may output in a "worst case" scenario * Memory usage formula : N->2^N Bytes
* (input data not compressible) * (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
*/ */
static inline size_t lz4_compressbound(size_t isize) #define LZ4_MEMORY_USAGE 14
#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;
/*
* LZ4_streamDecode_t - information structure to track an
* LZ4 stream during decompression.
*
* init this structure using LZ4_setStreamDecode (or memset()) before first use
*/
typedef struct {
const uint8_t *externalDict;
size_t extDictSize;
const uint8_t *prefixEnd;
size_t prefixSize;
} LZ4_streamDecode_t_internal;
typedef union {
unsigned long long table[LZ4_STREAMDECODESIZE_U64];
LZ4_streamDecode_t_internal internal_donotuse;
} LZ4_streamDecode_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 isize + (isize / 255) + 16; return LZ4_COMPRESSBOUND(isize);
} }
/* /**
* lz4_compress() * lz4_compressbound() - For backwards compatibility; see LZ4_compressBound
* src : source address of the original data * @isize: Size of the input data
* src_len : size of the original data *
* dst : output buffer address of the compressed data * Return: Max. size LZ4 may output in a "worst case" szenario
* This requires 'dst' of size LZ4_COMPRESSBOUND. * (data not compressible)
* dst_len : is the output size, which is returned after compress done */
* workmem : address of the working memory. static inline int lz4_compressbound(size_t isize)
* This requires 'workmem' of size LZ4_MEM_COMPRESS. {
* return : Success if return 0 return LZ4_COMPRESSBOUND(isize);
* Error if return (< 0) }
* note : Destination buffer and workmem must be already allocated with
* the defined size. /**
*/ * LZ4_compress_default() - Compress data from source to dest
int lz4_compress(const unsigned char *src, size_t src_len, * @source: source address of the original data
unsigned char *dst, size_t *dst_len, void *wrkmem); * @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'
* lz4hc_compress() * which must be already allocated
* src : source address of the original data * @wrkmem: address of the working memory.
* src_len : size of the original data * This requires 'workmem' of LZ4_MEM_COMPRESS.
* dst : output buffer address of the compressed data *
* This requires 'dst' of size LZ4_COMPRESSBOUND. * Compresses 'sourceSize' bytes from buffer 'source'
* dst_len : is the output size, which is returned after compress done * into already allocated 'dest' buffer of size 'maxOutputSize'.
* workmem : address of the working memory. * Compression is guaranteed to succeed if
* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. * 'maxOutputSize' >= LZ4_compressBound(inputSize).
* return : Success if return 0 * It also runs faster, so it's a recommended setting.
* Error if return (< 0) * If the function cannot compress 'source' into a more limited 'dest' budget,
* note : Destination buffer and workmem must be already allocated with * compression stops *immediately*, and the function result is zero.
* the defined size. * As a consequence, 'dest' content is not valid.
*/ *
int lz4hc_compress(const unsigned char *src, size_t src_len, * Return: Number of bytes written into buffer 'dest'
unsigned char *dst, size_t *dst_len, void *wrkmem); * (necessarily <= maxOutputSize) or 0 if compression fails
*/
int LZ4_compress_default(const char *source, char *dest, int inputSize,
int maxOutputSize, void *wrkmem);
/**
* LZ4_compress_fast() - As LZ4_compress_default providing an acceleration param
* @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
* @acceleration: acceleration factor
* @wrkmem: address of the working memory.
* This requires 'workmem' of LZ4_MEM_COMPRESS.
*
* Same as LZ4_compress_default(), but allows to select an "acceleration"
* factor. The larger the acceleration value, the faster the algorithm,
* but also the lesser the compression. It's a trade-off. It can be fine tuned,
* with each successive value providing roughly +~3% to speed.
* An acceleration value of "1" is the same as regular LZ4_compress_default()
* Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT, which is 1.
*
* Return: Number of bytes written into buffer 'dest'
* (necessarily <= maxOutputSize) or 0 if compression fails
*/
int LZ4_compress_fast(const char *source, char *dest, int inputSize,
int maxOutputSize, int acceleration, void *wrkmem);
/**
* LZ4_compress_destSize() - Compress as much data as possible
* from source to dest
* @source: source address of the original data
* @dest: output buffer address of the compressed data
* @sourceSizePtr: will be modified to indicate how many bytes where read
* from 'source' to fill 'dest'. New value is necessarily <= old value.
* @targetDestSize: Size of buffer 'dest' which must be already allocated
* @wrkmem: address of the working memory.
* This requires 'workmem' of LZ4_MEM_COMPRESS.
*
* Reverse the logic, by compressing as much data as possible
* from 'source' buffer into already allocated buffer 'dest'
* of size 'targetDestSize'.
* This function either compresses the entire 'source' content into 'dest'
* if it's large enough, or fill 'dest' buffer completely with as much data as
* possible from 'source'.
*
* Return: Number of bytes written into 'dest' (necessarily <= targetDestSize)
* or 0 if compression fails
*/
int LZ4_compress_destSize(const char *source, char *dest, int *sourceSizePtr,
int targetDestSize, void *wrkmem);
/* /*
* lz4_decompress() * lz4_compress() - For backward compatibility, see LZ4_compress_default
* src : source address of the compressed data * @src: source address of the original data
* src_len : is the input size, whcih is returned after decompress done * @src_len: size of the original data
* dest : output buffer address of the decompressed data * @dst: output buffer address of the compressed data. This requires 'dst'
* actual_dest_len: is the size of uncompressed data, supposing it's known * of size LZ4_COMPRESSBOUND
* return : Success if return 0 * @dst_len: is the output size, which is returned after compress done
* Error if return (< 0) * @workmem: address of the working memory.
* note : Destination buffer must be already allocated. *
* slightly faster than lz4_decompress_unknownoutputsize() * Return: Success if return 0, Error if return < 0
*/ */
int lz4_decompress(const unsigned char *src, size_t *src_len, int lz4_compress(const unsigned char *src, size_t src_len, unsigned char *dst,
unsigned char *dest, size_t actual_dest_len); size_t *dst_len, 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 fom '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_decompress_safe_partial() - Decompress a block of size 'compressedSize'
* at position 'source' into buffer 'dest'
* @source: source address of the compressed data
* @dest: output buffer address of the decompressed data which must be
* already allocated
* @compressedSize: is the precise full size of the compressed block.
* @targetOutputSize: the decompression operation will try
* to stop as soon as 'targetOutputSize' has been reached
* @maxDecompressedSize: is the size of destination buffer
*
* This function decompresses a compressed block of size 'compressedSize'
* at position 'source' into destination buffer 'dest'
* of size 'maxDecompressedSize'.
* The function tries to stop decompressing operation as soon as
* 'targetOutputSize' has been reached, reducing decompression time.
* This function never writes outside of output buffer,
* and never reads outside of input buffer.
* It is therefore protected against malicious data packets.
*
* Return: the number of bytes decoded in the destination buffer
* (necessarily <= maxDecompressedSize)
* or a negative result in case of error
*
*/
int LZ4_decompress_safe_partial(const char *source, char *dest,
int compressedSize, int targetOutputSize, int maxDecompressedSize);
/* /*
* lz4_decompress_unknownoutputsize() * lz4_decompress_unknownoutputsize() - For backwards compatibility,
* src : source address of the compressed data * see LZ4_decompress_safe
* src_len : is the input size, therefore the compressed size * @src: source address of the compressed data
* dest : output buffer address of the decompressed data * @src_len: is the input size, therefore the compressed size
* dest_len: is the max size of the destination buffer, which is * @dest: output buffer address of the decompressed data
* returned with actual size of decompressed data after * which must be already allocated
* decompress done * @dest_len: is the max size of the destination buffer, which is
* return : Success if return 0 * returned with actual size of decompressed data after decompress done
* Error if return (< 0) *
* note : Destination buffer must be already allocated. * Return: Success if return 0, Error if return (< 0)
*/ */
int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len, int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
unsigned char *dest, size_t *dest_len); unsigned char *dest, size_t *dest_len);
/**
* lz4_decompress() - For backwards cocmpatibility, see LZ4_decompress_fast
* @src: source address of the compressed data
* @src_len: is the input size, which is returned after decompress done
* @dest: output buffer address of the decompressed data,
* which must be already allocated
* @actual_dest_len: is the size of uncompressed data, supposing it's known
*
* Return: Success if return 0, Error if return (< 0)
*/
int lz4_decompress(const unsigned char *src, size_t *src_len,
unsigned char *dest, size_t actual_dest_len);
/*-************************************************************************
* 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);
/**
* lz4hc_compress() - For backwards compatibility, see LZ4_compress_HC
* @src: source address of the original data
* @src_len: size of the original data
* @dst: output buffer address of the compressed data. This requires 'dst'
* of size LZ4_COMPRESSBOUND.
* @dst_len: is the output size, which is returned after compress done
* @wrkmem: address of the working memory.
* This requires 'workmem' of size LZ4HC_MEM_COMPRESS.
*
* Return : Success if return 0, Error if return (< 0)
*/
int lz4hc_compress(const unsigned char *src, size_t src_len, unsigned char *dst,
size_t *dst_len, void *wrkmem);
/**
* LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure
* @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure
* @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.
*
* An LZ4_streamHC_t structure can be allocated once
* and re-used multiple times.
* Use this function to init an allocated `LZ4_streamHC_t` structure
* and start a new compression.
*/
void LZ4_resetStreamHC(LZ4_streamHC_t *streamHCPtr, int compressionLevel);
/**
* LZ4_loadDictHC() - Load a static dictionary into LZ4_streamHC
* @streamHCPtr: pointer to the LZ4HC_stream_t
* @dictionary: dictionary to load
* @dictSize: size of dictionary
*
* Use this function to load a static dictionary into LZ4HC_stream.
* Any previous data will be forgotten, only 'dictionary'
* will remain in memory.
* Loading a size of 0 is allowed.
*
* Return : dictionary size, in bytes (necessarily <= 64 KB)
*/
int LZ4_loadDictHC(LZ4_streamHC_t *streamHCPtr, const char *dictionary,
int dictSize);
/**
* LZ4_compress_HC_continue() - Compress 'src' using data from previously
* compressed blocks as a dictionary using the HC algorithm
* @streamHCPtr: Pointer to the previous 'LZ4_streamHC_t' structure
* @src: source address of the original data
* @dst: output buffer address of the compressed data,
* which must be already allocated
* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
* @maxDstSize: full or partial size of buffer 'dest'
* which must be already allocated
*
* These functions compress data in successive blocks of any size, using
* previous blocks as dictionary. One key assumption is that previous
* blocks (up to 64 KB) remain read-accessible while
* compressing next blocks. There is an exception for ring buffers,
* which can be smaller than 64 KB.
* Ring buffers scenario is automatically detected and handled by
* LZ4_compress_HC_continue().
* Before starting compression, state must be properly initialized,
* using LZ4_resetStreamHC().
* A first "fictional block" can then be designated as
* initial dictionary, using LZ4_loadDictHC() (Optional).
* Then, use LZ4_compress_HC_continue()
* to compress each successive block. Previous memory blocks
* (including initial dictionary when present) must remain accessible
* and unmodified during compression.
* 'dst' buffer should be sized to handle worst case scenarios, using
* LZ4_compressBound(), to ensure operation success.
* If, for any reason, previous data blocks can't be preserved unmodified
* in memory during next compression block,
* you must save it to a safer memory space, using LZ4_saveDictHC().
* Return value of LZ4_saveDictHC() is the size of dictionary
* effectively saved into 'safeBuffer'.
*
* Return: Number of bytes written into buffer 'dst' or 0 if compression fails
*/
int LZ4_compress_HC_continue(LZ4_streamHC_t *streamHCPtr, const char *src,
char *dst, int srcSize, int maxDstSize);
/**
* LZ4_saveDictHC() - Save static dictionary from LZ4HC_stream
* @streamHCPtr: pointer to the 'LZ4HC_stream_t' structure
* @safeBuffer: buffer to save dictionary to, must be already allocated
* @maxDictSize: size of 'safeBuffer'
*
* If previously compressed data block is not guaranteed
* to remain available at its memory location,
* save it into a safer place (char *safeBuffer).
* Note : you don't need to call LZ4_loadDictHC() afterwards,
* dictionary is immediately usable, you can therefore call
* LZ4_compress_HC_continue().
*
* Return : saved dictionary size in bytes (necessarily <= maxDictSize),
* or 0 if error.
*/
int LZ4_saveDictHC(LZ4_streamHC_t *streamHCPtr, char *safeBuffer,
int maxDictSize);
/*-*********************************************
* Streaming Compression Functions
***********************************************/
/**
* LZ4_resetStream() - Init an allocated 'LZ4_stream_t' structure
* @LZ4_stream: pointer to the 'LZ4_stream_t' structure
*
* An LZ4_stream_t structure can be allocated once
* and re-used multiple times.
* Use this function to init an allocated `LZ4_stream_t` structure
* and start a new compression.
*/
void LZ4_resetStream(LZ4_stream_t *LZ4_stream);
/**
* LZ4_loadDict() - Load a static dictionary into LZ4_stream
* @streamPtr: pointer to the LZ4_stream_t
* @dictionary: dictionary to load
* @dictSize: size of dictionary
*
* Use this function to load a static dictionary into LZ4_stream.
* Any previous data will be forgotten, only 'dictionary'
* will remain in memory.
* Loading a size of 0 is allowed.
*
* Return : dictionary size, in bytes (necessarily <= 64 KB)
*/
int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary,
int dictSize);
/**
* LZ4_saveDict() - Save static dictionary from LZ4_stream
* @streamPtr: pointer to the 'LZ4_stream_t' structure
* @safeBuffer: buffer to save dictionary to, must be already allocated
* @dictSize: size of 'safeBuffer'
*
* If previously compressed data block is not guaranteed
* to remain available at its memory location,
* save it into a safer place (char *safeBuffer).
* Note : you don't need to call LZ4_loadDict() afterwards,
* dictionary is immediately usable, you can therefore call
* LZ4_compress_fast_continue().
*
* Return : saved dictionary size in bytes (necessarily <= dictSize),
* or 0 if error.
*/
int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int dictSize);
/**
* LZ4_compress_fast_continue() - Compress 'src' using data from previously
* compressed blocks as a dictionary
* @streamPtr: Pointer to the previous 'LZ4_stream_t' structure
* @src: source address of the original data
* @dst: output buffer address of the compressed data,
* which must be already allocated
* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
* @maxDstSize: full or partial size of buffer 'dest'
* which must be already allocated
* @acceleration: acceleration factor
*
* Compress buffer content 'src', using data from previously compressed blocks
* as dictionary to improve compression ratio.
* Important : Previous data blocks are assumed to still
* be present and unmodified !
* If maxDstSize >= LZ4_compressBound(srcSize),
* compression is guaranteed to succeed, and runs faster.
*
* Return: Number of bytes written into buffer 'dst' or 0 if compression fails
*/
int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src,
char *dst, int srcSize, int maxDstSize, int acceleration);
/**
* LZ4_setStreamDecode() - Instruct where to find dictionary
* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
* @dictionary: dictionary to use
* @dictSize: size of dictionary
*
* Use this function to instruct where to find the dictionary.
* Setting a size of 0 is allowed (same effect as reset).
*
* Return: 1 if OK, 0 if error
*/
int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
const char *dictionary, int dictSize);
/**
* LZ4_decompress_fast_continue() - Decompress blocks in streaming mode
* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
* @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
*
* These decoding function allows decompression of multiple blocks
* in "streaming" mode.
* Previously decoded blocks *must* remain available at the memory position
* where they were decoded (up to 64 KB)
* In the case of a ring buffers, decoding buffer must be either :
* - Exactly same size as encoding buffer, with same update rule
* (block boundaries at same positions) In which case,
* the decoding & encoding ring buffer can have any size,
* including very small ones ( < 64 KB).
* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
* maxBlockSize is implementation dependent.
* It's the maximum size you intend to compress into a single block.
* In which case, encoding and decoding buffers do not need
* to be synchronized, and encoding ring buffer can have any size,
* including small ones ( < 64 KB).
* - _At least_ 64 KB + 8 bytes + maxBlockSize.
* In which case, encoding and decoding buffers do not need to be
* synchronized, and encoding ring buffer can have any size,
* including larger than decoding buffer. W
* Whenever these conditions are not possible, save the last 64KB of decoded
* data into a safe buffer, and indicate where it is saved
* using LZ4_setStreamDecode()
*
* Return: number of bytes decompressed into destination buffer
* (necessarily <= maxDecompressedSize)
* or a negative result in case of error
*/
int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
const char *source, char *dest, int compressedSize,
int maxDecompressedSize);
/**
* LZ4_decompress_fast_continue() - Decompress blocks in streaming mode
* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
* @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
*
* These decoding function allows decompression of multiple blocks
* in "streaming" mode.
* Previously decoded blocks *must* remain available at the memory position
* where they were decoded (up to 64 KB)
* In the case of a ring buffers, decoding buffer must be either :
* - Exactly same size as encoding buffer, with same update rule
* (block boundaries at same positions) In which case,
* the decoding & encoding ring buffer can have any size,
* including very small ones ( < 64 KB).
* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
* maxBlockSize is implementation dependent.
* It's the maximum size you intend to compress into a single block.
* In which case, encoding and decoding buffers do not need
* to be synchronized, and encoding ring buffer can have any size,
* including small ones ( < 64 KB).
* - _At least_ 64 KB + 8 bytes + maxBlockSize.
* In which case, encoding and decoding buffers do not need to be
* synchronized, and encoding ring buffer can have any size,
* including larger than decoding buffer. W
* Whenever these conditions are not possible, save the last 64KB of decoded
* data into a safe buffer, and indicate where it is saved
* using LZ4_setStreamDecode()
*
* Return: number of bytes decompressed into destination buffer
* (necessarily <= maxDecompressedSize)
* or a negative result in case of error
*/
int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
const char *source, char *dest, int originalSize);
/**
* LZ4_decompress_safe_usingDict() - Same as LZ4_setStreamDecode()
* followed by LZ4_decompress_safe_continue()
* @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
* @dictStart: pointer to the start of the dictionary in memory
* @dictSize: size of dictionary
*
* These decoding function works the same as
* a combination of LZ4_setStreamDecode() followed by
* LZ4_decompress_safe_continue()
* It is stand-alone, and don'tn eed a LZ4_streamDecode_t structure.
*
* Return: number of bytes decompressed into destination buffer
* (necessarily <= maxDecompressedSize)
* or a negative result in case of error
*/
int LZ4_decompress_safe_usingDict(const char *source, char *dest,
int compressedSize, int maxDecompressedSize, const char *dictStart,
int dictSize);
/**
* LZ4_decompress_fast_usingDict() - Same as LZ4_setStreamDecode()
* followed by LZ4_decompress_fast_continue()
* @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
* @dictStart: pointer to the start of the dictionary in memory
* @dictSize: size of dictionary
*
* These decoding function works the same as
* a combination of LZ4_setStreamDecode() followed by
* LZ4_decompress_safe_continue()
* It is stand-alone, and don'tn eed a LZ4_streamDecode_t structure.
*
* Return: number of bytes decompressed into destination buffer
* (necessarily <= maxDecompressedSize)
* or a negative result in case of error
*/
int LZ4_decompress_fast_usingDict(const char *source, char *dest,
int originalSize, const char *dictStart, int dictSize);
#endif #endif
ccflags-y += -O3
obj-$(CONFIG_LZ4_COMPRESS) += lz4_compress.o obj-$(CONFIG_LZ4_COMPRESS) += lz4_compress.o
obj-$(CONFIG_LZ4HC_COMPRESS) += lz4hc_compress.o obj-$(CONFIG_LZ4HC_COMPRESS) += lz4hc_compress.o
obj-$(CONFIG_LZ4_DECOMPRESS) += lz4_decompress.o obj-$(CONFIG_LZ4_DECOMPRESS) += lz4_decompress.o
/* /*
* LZ4 - Fast LZ compression algorithm * LZ4 - Fast LZ compression algorithm
* Copyright (C) 2011-2012, Yann Collet. * Copyright (C) 2011 - 2016, Yann Collet.
* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* * * Redistributions of source code must retain the above copyright
* * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer * copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the * in the documentation and/or other materials provided with the
* distribution. * distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -25,417 +22,939 @@ ...@@ -25,417 +22,939 @@
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at : * You can contact the author at :
* - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html * - LZ4 homepage : http://www.lz4.org
* - LZ4 source repository : http://code.google.com/p/lz4/ * - LZ4 source repository : https://github.com/lz4/lz4
* *
* Changed for kernel use by: * Changed for kernel usage by:
* Chanho Min <chanho.min@lge.com> * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
*/ */
/*-************************************
* Dependencies
**************************************/
#include <linux/lz4.h>
#include "lz4defs.h"
#include <linux/module.h> #include <linux/module.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/lz4.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
#include "lz4defs.h"
/* static const int LZ4_minLength = (MFLIMIT + 1);
* LZ4_compressCtx : static const int LZ4_64Klimit = ((64 * KB) + (MFLIMIT - 1));
* -----------------
* Compress 'isize' bytes from 'source' into an output buffer 'dest' of /*-******************************
* maximum size 'maxOutputSize'. * If it cannot achieve it, compression * Compression functions
* will stop, and result of the function will be zero. ********************************/
* return : the number of bytes written in buffer 'dest', or 0 if the static FORCE_INLINE U32 LZ4_hash4(
* compression fails U32 sequence,
*/ tableType_t const tableType)
static inline int lz4_compressctx(void *ctx,
const char *source,
char *dest,
int isize,
int maxoutputsize)
{ {
HTYPE *hashtable = (HTYPE *)ctx; if (tableType == byU16)
const u8 *ip = (u8 *)source; return ((sequence * 2654435761U)
#if LZ4_ARCH64 >> ((MINMATCH * 8) - (LZ4_HASHLOG + 1)));
const BYTE * const base = ip; else
return ((sequence * 2654435761U)
>> ((MINMATCH * 8) - LZ4_HASHLOG));
}
static FORCE_INLINE U32 LZ4_hash5(
U64 sequence,
tableType_t const tableType)
{
const U32 hashLog = (tableType == byU16)
? LZ4_HASHLOG + 1
: LZ4_HASHLOG;
#if LZ4_LITTLE_ENDIAN
static const U64 prime5bytes = 889523592379ULL;
return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
#else #else
const int base = 0; static const U64 prime8bytes = 11400714785074694791ULL;
return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
#endif #endif
const u8 *anchor = ip; }
const u8 *const iend = ip + isize;
const u8 *const mflimit = iend - MFLIMIT; static FORCE_INLINE U32 LZ4_hashPosition(
#define MATCHLIMIT (iend - LASTLITERALS) const void *p,
tableType_t const tableType)
u8 *op = (u8 *) dest; {
u8 *const oend = op + maxoutputsize; #if LZ4_ARCH64
int length; if (tableType == byU32)
const int skipstrength = SKIPSTRENGTH; return LZ4_hash5(LZ4_read_ARCH(p), tableType);
u32 forwardh; #endif
int lastrun;
return LZ4_hash4(LZ4_read32(p), tableType);
/* Init */ }
if (isize < MINLENGTH)
goto _last_literals; static void LZ4_putPositionOnHash(
const BYTE *p,
U32 h,
void *tableBase,
tableType_t const tableType,
const BYTE *srcBase)
{
switch (tableType) {
case byPtr:
{
const BYTE **hashTable = (const BYTE **)tableBase;
hashTable[h] = p;
return;
}
case byU32:
{
U32 *hashTable = (U32 *) tableBase;
hashTable[h] = (U32)(p - srcBase);
return;
}
case byU16:
{
U16 *hashTable = (U16 *) tableBase;
hashTable[h] = (U16)(p - srcBase);
return;
}
}
}
static FORCE_INLINE void LZ4_putPosition(
const BYTE *p,
void *tableBase,
tableType_t tableType,
const BYTE *srcBase)
{
U32 const h = LZ4_hashPosition(p, tableType);
LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
}
static const BYTE *LZ4_getPositionOnHash(
U32 h,
void *tableBase,
tableType_t tableType,
const BYTE *srcBase)
{
if (tableType == byPtr) {
const BYTE **hashTable = (const BYTE **) tableBase;
return hashTable[h];
}
if (tableType == byU32) {
const U32 * const hashTable = (U32 *) tableBase;
return hashTable[h] + srcBase;
}
{
/* default, to ensure a return */
const U16 * const hashTable = (U16 *) tableBase;
return hashTable[h] + srcBase;
}
}
static FORCE_INLINE const BYTE *LZ4_getPosition(
const BYTE *p,
void *tableBase,
tableType_t tableType,
const BYTE *srcBase)
{
U32 const h = LZ4_hashPosition(p, tableType);
return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
}
memset((void *)hashtable, 0, LZ4_MEM_COMPRESS); /*
* LZ4_compress_generic() :
* inlined, to ensure branches are decided at compilation time
*/
static FORCE_INLINE int LZ4_compress_generic(
LZ4_stream_t_internal * const dictPtr,
const char * const source,
char * const dest,
const int inputSize,
const int maxOutputSize,
const limitedOutput_directive outputLimited,
const tableType_t tableType,
const dict_directive dict,
const dictIssue_directive dictIssue,
const U32 acceleration)
{
const BYTE *ip = (const BYTE *) source;
const BYTE *base;
const BYTE *lowLimit;
const BYTE * const lowRefLimit = ip - dictPtr->dictSize;
const BYTE * const dictionary = dictPtr->dictionary;
const BYTE * const dictEnd = dictionary + dictPtr->dictSize;
const size_t dictDelta = dictEnd - (const BYTE *)source;
const BYTE *anchor = (const BYTE *) source;
const BYTE * const iend = ip + inputSize;
const BYTE * const mflimit = iend - MFLIMIT;
const BYTE * const matchlimit = iend - LASTLITERALS;
BYTE *op = (BYTE *) dest;
BYTE * const olimit = op + maxOutputSize;
U32 forwardH;
size_t refDelta = 0;
/* Init conditions */
if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) {
/* Unsupported inputSize, too large (or negative) */
return 0;
}
switch (dict) {
case noDict:
default:
base = (const BYTE *)source;
lowLimit = (const BYTE *)source;
break;
case withPrefix64k:
base = (const BYTE *)source - dictPtr->currentOffset;
lowLimit = (const BYTE *)source - dictPtr->dictSize;
break;
case usingExtDict:
base = (const BYTE *)source - dictPtr->currentOffset;
lowLimit = (const BYTE *)source;
break;
}
if ((tableType == byU16)
&& (inputSize >= LZ4_64Klimit)) {
/* Size too large (not within 64K limit) */
return 0;
}
if (inputSize < LZ4_minLength) {
/* Input too small, no compression (all literals) */
goto _last_literals;
}
/* First Byte */ /* First Byte */
hashtable[LZ4_HASH_VALUE(ip)] = ip - base; LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
ip++; ip++;
forwardh = LZ4_HASH_VALUE(ip); forwardH = LZ4_hashPosition(ip, tableType);
/* Main Loop */ /* Main Loop */
for (;;) { for ( ; ; ) {
int findmatchattempts = (1U << skipstrength) + 3; const BYTE *match;
const u8 *forwardip = ip; BYTE *token;
const u8 *ref;
u8 *token;
/* Find a match */ /* Find a match */
do { {
u32 h = forwardh; const BYTE *forwardIp = ip;
int step = findmatchattempts++ >> skipstrength; unsigned int step = 1;
ip = forwardip; unsigned int searchMatchNb = acceleration << LZ4_SKIPTRIGGER;
forwardip = ip + step;
do {
if (unlikely(forwardip > mflimit)) U32 const h = forwardH;
goto _last_literals;
ip = forwardIp;
forwardh = LZ4_HASH_VALUE(forwardip); forwardIp += step;
ref = base + hashtable[h]; step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
hashtable[h] = ip - base;
} while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip))); if (unlikely(forwardIp > mflimit))
goto _last_literals;
match = LZ4_getPositionOnHash(h,
dictPtr->hashTable,
tableType, base);
if (dict == usingExtDict) {
if (match < (const BYTE *)source) {
refDelta = dictDelta;
lowLimit = dictionary;
} else {
refDelta = 0;
lowLimit = (const BYTE *)source;
} }
forwardH = LZ4_hashPosition(forwardIp,
tableType);
LZ4_putPositionOnHash(ip, h, dictPtr->hashTable,
tableType, base);
} while (((dictIssue == dictSmall)
? (match < lowRefLimit)
: 0)
|| ((tableType == byU16)
? 0
: (match + MAX_DISTANCE < ip))
|| (LZ4_read32(match + refDelta)
!= LZ4_read32(ip)));
}
/* Catch up */ /* Catch up */
while ((ip > anchor) && (ref > (u8 *)source) && while (((ip > anchor) & (match + refDelta > lowLimit))
unlikely(ip[-1] == ref[-1])) { && (unlikely(ip[-1] == match[refDelta - 1]))) {
ip--; ip--;
ref--; match--;
} }
/* Encode Literal length */ /* Encode Literals */
length = (int)(ip - anchor); {
token = op++; unsigned const int litLength = (unsigned int)(ip - anchor);
/* check output limit */
if (unlikely(op + length + (2 + 1 + LASTLITERALS) +
(length >> 8) > oend))
return 0;
if (length >= (int)RUN_MASK) { token = op++;
int len;
*token = (RUN_MASK << ML_BITS); if ((outputLimited) &&
len = length - RUN_MASK; /* Check output buffer overflow */
for (; len > 254 ; len -= 255) (unlikely(op + litLength +
*op++ = 255; (2 + 1 + LASTLITERALS) +
*op++ = (u8)len; (litLength / 255) > olimit)))
} else return 0;
*token = (length << ML_BITS);
if (litLength >= RUN_MASK) {
int len = (int)litLength - RUN_MASK;
*token = (RUN_MASK << ML_BITS);
for (; len >= 255; len -= 255)
*op++ = 255;
*op++ = (BYTE)len;
} else
*token = (BYTE)(litLength << ML_BITS);
/* Copy Literals */
LZ4_wildCopy(op, anchor, op + litLength);
op += litLength;
}
/* Copy Literals */
LZ4_BLINDCOPY(anchor, op, length);
_next_match: _next_match:
/* Encode Offset */ /* Encode Offset */
LZ4_WRITE_LITTLEENDIAN_16(op, (u16)(ip - ref)); LZ4_writeLE16(op, (U16)(ip - match));
op += 2;
/* Start Counting */
ip += MINMATCH;
/* MinMatch verified */
ref += MINMATCH;
anchor = ip;
while (likely(ip < MATCHLIMIT - (STEPSIZE - 1))) {
#if LZ4_ARCH64
u64 diff = A64(ref) ^ A64(ip);
#else
u32 diff = A32(ref) ^ A32(ip);
#endif
if (!diff) {
ip += STEPSIZE;
ref += STEPSIZE;
continue;
}
ip += LZ4_NBCOMMONBYTES(diff);
goto _endcount;
}
#if LZ4_ARCH64
if ((ip < (MATCHLIMIT - 3)) && (A32(ref) == A32(ip))) {
ip += 4;
ref += 4;
}
#endif
if ((ip < (MATCHLIMIT - 1)) && (A16(ref) == A16(ip))) {
ip += 2;
ref += 2;
}
if ((ip < MATCHLIMIT) && (*ref == *ip))
ip++;
_endcount:
/* Encode MatchLength */ /* Encode MatchLength */
length = (int)(ip - anchor); {
/* Check output limit */ unsigned int matchCode;
if (unlikely(op + (1 + LASTLITERALS) + (length >> 8) > oend))
return 0; if ((dict == usingExtDict)
if (length >= (int)ML_MASK) { && (lowLimit == dictionary)) {
*token += ML_MASK; const BYTE *limit;
length -= ML_MASK;
for (; length > 509 ; length -= 510) { match += refDelta;
*op++ = 255; limit = ip + (dictEnd - match);
*op++ = 255;
} if (limit > matchlimit)
if (length > 254) { limit = matchlimit;
length -= 255;
*op++ = 255; matchCode = LZ4_count(ip + MINMATCH,
match + MINMATCH, limit);
ip += MINMATCH + matchCode;
if (ip == limit) {
unsigned const int more = LZ4_count(ip,
(const BYTE *)source,
matchlimit);
matchCode += more;
ip += more;
}
} else {
matchCode = LZ4_count(ip + MINMATCH,
match + MINMATCH, matchlimit);
ip += MINMATCH + matchCode;
} }
*op++ = (u8)length;
} else if (outputLimited &&
*token += length; /* Check output buffer overflow */
(unlikely(op +
(1 + LASTLITERALS) +
(matchCode >> 8) > olimit)))
return 0;
if (matchCode >= ML_MASK) {
*token += ML_MASK;
matchCode -= ML_MASK;
LZ4_write32(op, 0xFFFFFFFF);
while (matchCode >= 4 * 255) {
op += 4;
LZ4_write32(op, 0xFFFFFFFF);
matchCode -= 4 * 255;
}
op += matchCode / 255;
*op++ = (BYTE)(matchCode % 255);
} else
*token += (BYTE)(matchCode);
}
anchor = ip;
/* Test end of chunk */ /* Test end of chunk */
if (ip > mflimit) { if (ip > mflimit)
anchor = ip;
break; break;
}
/* Fill table */ /* Fill table */
hashtable[LZ4_HASH_VALUE(ip-2)] = ip - 2 - base; LZ4_putPosition(ip - 2, dictPtr->hashTable, tableType, base);
/* Test next position */ /* Test next position */
ref = base + hashtable[LZ4_HASH_VALUE(ip)]; match = LZ4_getPosition(ip, dictPtr->hashTable,
hashtable[LZ4_HASH_VALUE(ip)] = ip - base; tableType, base);
if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) {
if (dict == usingExtDict) {
if (match < (const BYTE *)source) {
refDelta = dictDelta;
lowLimit = dictionary;
} else {
refDelta = 0;
lowLimit = (const BYTE *)source;
}
}
LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
if (((dictIssue == dictSmall) ? (match >= lowRefLimit) : 1)
&& (match + MAX_DISTANCE >= ip)
&& (LZ4_read32(match + refDelta) == LZ4_read32(ip))) {
token = op++; token = op++;
*token = 0; *token = 0;
goto _next_match; goto _next_match;
} }
/* Prepare next loop */ /* Prepare next loop */
anchor = ip++; forwardH = LZ4_hashPosition(++ip, tableType);
forwardh = LZ4_HASH_VALUE(ip);
} }
_last_literals: _last_literals:
/* Encode Last Literals */ /* Encode Last Literals */
lastrun = (int)(iend - anchor); {
if (((char *)op - dest) + lastrun + 1 size_t const lastRun = (size_t)(iend - anchor);
+ ((lastrun + 255 - RUN_MASK) / 255) > (u32)maxoutputsize)
return 0;
if (lastrun >= (int)RUN_MASK) { if ((outputLimited) &&
*op++ = (RUN_MASK << ML_BITS); /* Check output buffer overflow */
lastrun -= RUN_MASK; ((op - (BYTE *)dest) + lastRun + 1 +
for (; lastrun > 254 ; lastrun -= 255) ((lastRun + 255 - RUN_MASK) / 255) > (U32)maxOutputSize))
*op++ = 255; return 0;
*op++ = (u8)lastrun;
} else if (lastRun >= RUN_MASK) {
*op++ = (lastrun << ML_BITS); size_t accumulator = lastRun - RUN_MASK;
memcpy(op, anchor, iend - anchor); *op++ = RUN_MASK << ML_BITS;
op += iend - anchor; for (; accumulator >= 255; accumulator -= 255)
*op++ = 255;
*op++ = (BYTE) accumulator;
} else {
*op++ = (BYTE)(lastRun << ML_BITS);
}
memcpy(op, anchor, lastRun);
op += lastRun;
}
/* End */ /* End */
return (int)(((char *)op) - dest); return (int) (((char *)op) - dest);
} }
static inline int lz4_compress64kctx(void *ctx, static int LZ4_compress_fast_extState(
const char *source, void *state,
char *dest, const char *source,
int isize, char *dest,
int maxoutputsize) int inputSize,
int maxOutputSize,
int acceleration)
{ {
u16 *hashtable = (u16 *)ctx; LZ4_stream_t_internal *ctx = &((LZ4_stream_t *)state)->internal_donotuse;
const u8 *ip = (u8 *) source; #if LZ4_ARCH64
const u8 *anchor = ip; const tableType_t tableType = byU32;
const u8 *const base = ip; #else
const u8 *const iend = ip + isize; const tableType_t tableType = byPtr;
const u8 *const mflimit = iend - MFLIMIT; #endif
#define MATCHLIMIT (iend - LASTLITERALS)
u8 *op = (u8 *) dest;
u8 *const oend = op + maxoutputsize;
int len, length;
const int skipstrength = SKIPSTRENGTH;
u32 forwardh;
int lastrun;
/* Init */
if (isize < MINLENGTH)
goto _last_literals;
memset((void *)hashtable, 0, LZ4_MEM_COMPRESS); LZ4_resetStream((LZ4_stream_t *)state);
if (acceleration < 1)
acceleration = LZ4_ACCELERATION_DEFAULT;
if (maxOutputSize >= LZ4_COMPRESSBOUND(inputSize)) {
if (inputSize < LZ4_64Klimit)
return LZ4_compress_generic(ctx, source,
dest, inputSize, 0,
noLimit, byU16, noDict,
noDictIssue, acceleration);
else
return LZ4_compress_generic(ctx, source,
dest, inputSize, 0,
noLimit, tableType, noDict,
noDictIssue, acceleration);
} else {
if (inputSize < LZ4_64Klimit)
return LZ4_compress_generic(ctx, source,
dest, inputSize,
maxOutputSize, limitedOutput, byU16, noDict,
noDictIssue, acceleration);
else
return LZ4_compress_generic(ctx, source,
dest, inputSize,
maxOutputSize, limitedOutput, tableType, noDict,
noDictIssue, acceleration);
}
}
int LZ4_compress_fast(const char *source, char *dest, int inputSize,
int maxOutputSize, int acceleration, void *wrkmem)
{
return LZ4_compress_fast_extState(wrkmem, source, dest, inputSize,
maxOutputSize, acceleration);
}
EXPORT_SYMBOL(LZ4_compress_fast);
int LZ4_compress_default(const char *source, char *dest, int inputSize,
int maxOutputSize, void *wrkmem)
{
return LZ4_compress_fast(source, dest, inputSize,
maxOutputSize, LZ4_ACCELERATION_DEFAULT, wrkmem);
}
EXPORT_SYMBOL(LZ4_compress_default);
/*-******************************
* *_destSize() variant
********************************/
static int LZ4_compress_destSize_generic(
LZ4_stream_t_internal * const ctx,
const char * const src,
char * const dst,
int * const srcSizePtr,
const int targetDstSize,
const tableType_t tableType)
{
const BYTE *ip = (const BYTE *) src;
const BYTE *base = (const BYTE *) src;
const BYTE *lowLimit = (const BYTE *) src;
const BYTE *anchor = ip;
const BYTE * const iend = ip + *srcSizePtr;
const BYTE * const mflimit = iend - MFLIMIT;
const BYTE * const matchlimit = iend - LASTLITERALS;
BYTE *op = (BYTE *) dst;
BYTE * const oend = op + targetDstSize;
BYTE * const oMaxLit = op + targetDstSize - 2 /* offset */
- 8 /* because 8 + MINMATCH == MFLIMIT */ - 1 /* token */;
BYTE * const oMaxMatch = op + targetDstSize
- (LASTLITERALS + 1 /* token */);
BYTE * const oMaxSeq = oMaxLit - 1 /* token */;
U32 forwardH;
/* Init conditions */
/* Impossible to store anything */
if (targetDstSize < 1)
return 0;
/* Unsupported input size, too large (or negative) */
if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE)
return 0;
/* Size too large (not within 64K limit) */
if ((tableType == byU16) && (*srcSizePtr >= LZ4_64Klimit))
return 0;
/* Input too small, no compression (all literals) */
if (*srcSizePtr < LZ4_minLength)
goto _last_literals;
/* First Byte */ /* First Byte */
ip++; *srcSizePtr = 0;
forwardh = LZ4_HASH64K_VALUE(ip); LZ4_putPosition(ip, ctx->hashTable, tableType, base);
ip++; forwardH = LZ4_hashPosition(ip, tableType);
/* Main Loop */ /* Main Loop */
for (;;) { for ( ; ; ) {
int findmatchattempts = (1U << skipstrength) + 3; const BYTE *match;
const u8 *forwardip = ip; BYTE *token;
const u8 *ref;
u8 *token;
/* Find a match */ /* Find a match */
do { {
u32 h = forwardh; const BYTE *forwardIp = ip;
int step = findmatchattempts++ >> skipstrength; unsigned int step = 1;
ip = forwardip; unsigned int searchMatchNb = 1 << LZ4_SKIPTRIGGER;
forwardip = ip + step;
do {
if (forwardip > mflimit) U32 h = forwardH;
goto _last_literals;
ip = forwardIp;
forwardh = LZ4_HASH64K_VALUE(forwardip); forwardIp += step;
ref = base + hashtable[h]; step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
hashtable[h] = (u16)(ip - base);
} while (A32(ref) != A32(ip)); if (unlikely(forwardIp > mflimit))
goto _last_literals;
match = LZ4_getPositionOnHash(h, ctx->hashTable,
tableType, base);
forwardH = LZ4_hashPosition(forwardIp,
tableType);
LZ4_putPositionOnHash(ip, h,
ctx->hashTable, tableType,
base);
} while (((tableType == byU16)
? 0
: (match + MAX_DISTANCE < ip))
|| (LZ4_read32(match) != LZ4_read32(ip)));
}
/* Catch up */ /* Catch up */
while ((ip > anchor) && (ref > (u8 *)source) while ((ip > anchor)
&& (ip[-1] == ref[-1])) { && (match > lowLimit)
&& (unlikely(ip[-1] == match[-1]))) {
ip--; ip--;
ref--; match--;
} }
/* Encode Literal length */ /* Encode Literal length */
length = (int)(ip - anchor); {
token = op++; unsigned int litLength = (unsigned int)(ip - anchor);
/* Check output limit */
if (unlikely(op + length + (2 + 1 + LASTLITERALS)
+ (length >> 8) > oend))
return 0;
if (length >= (int)RUN_MASK) {
*token = (RUN_MASK << ML_BITS);
len = length - RUN_MASK;
for (; len > 254 ; len -= 255)
*op++ = 255;
*op++ = (u8)len;
} else
*token = (length << ML_BITS);
/* Copy Literals */ token = op++;
LZ4_BLINDCOPY(anchor, op, length); if (op + ((litLength + 240) / 255)
+ litLength > oMaxLit) {
/* Not enough space for a last match */
op--;
goto _last_literals;
}
if (litLength >= RUN_MASK) {
unsigned int len = litLength - RUN_MASK;
*token = (RUN_MASK<<ML_BITS);
for (; len >= 255; len -= 255)
*op++ = 255;
*op++ = (BYTE)len;
} else
*token = (BYTE)(litLength << ML_BITS);
/* Copy Literals */
LZ4_wildCopy(op, anchor, op + litLength);
op += litLength;
}
_next_match: _next_match:
/* Encode Offset */ /* Encode Offset */
LZ4_WRITE_LITTLEENDIAN_16(op, (u16)(ip - ref)); LZ4_writeLE16(op, (U16)(ip - match)); op += 2;
/* Start Counting */ /* Encode MatchLength */
ip += MINMATCH; {
/* MinMatch verified */ size_t matchLength = LZ4_count(ip + MINMATCH,
ref += MINMATCH; match + MINMATCH, matchlimit);
anchor = ip;
while (ip < MATCHLIMIT - (STEPSIZE - 1)) { if (op + ((matchLength + 240)/255) > oMaxMatch) {
#if LZ4_ARCH64 /* Match description too long : reduce it */
u64 diff = A64(ref) ^ A64(ip); matchLength = (15 - 1) + (oMaxMatch - op) * 255;
#else
u32 diff = A32(ref) ^ A32(ip);
#endif
if (!diff) {
ip += STEPSIZE;
ref += STEPSIZE;
continue;
} }
ip += LZ4_NBCOMMONBYTES(diff); ip += MINMATCH + matchLength;
goto _endcount;
} if (matchLength >= ML_MASK) {
#if LZ4_ARCH64 *token += ML_MASK;
if ((ip < (MATCHLIMIT - 3)) && (A32(ref) == A32(ip))) { matchLength -= ML_MASK;
ip += 4; while (matchLength >= 255) {
ref += 4; matchLength -= 255;
} *op++ = 255;
#endif }
if ((ip < (MATCHLIMIT - 1)) && (A16(ref) == A16(ip))) { *op++ = (BYTE)matchLength;
ip += 2; } else
ref += 2; *token += (BYTE)(matchLength);
} }
if ((ip < MATCHLIMIT) && (*ref == *ip))
ip++;
_endcount:
/* Encode MatchLength */ anchor = ip;
len = (int)(ip - anchor);
/* Check output limit */
if (unlikely(op + (1 + LASTLITERALS) + (len >> 8) > oend))
return 0;
if (len >= (int)ML_MASK) {
*token += ML_MASK;
len -= ML_MASK;
for (; len > 509 ; len -= 510) {
*op++ = 255;
*op++ = 255;
}
if (len > 254) {
len -= 255;
*op++ = 255;
}
*op++ = (u8)len;
} else
*token += len;
/* Test end of chunk */ /* Test end of block */
if (ip > mflimit) { if (ip > mflimit)
anchor = ip; break;
if (op > oMaxSeq)
break; break;
}
/* Fill table */ /* Fill table */
hashtable[LZ4_HASH64K_VALUE(ip-2)] = (u16)(ip - 2 - base); LZ4_putPosition(ip - 2, ctx->hashTable, tableType, base);
/* Test next position */ /* Test next position */
ref = base + hashtable[LZ4_HASH64K_VALUE(ip)]; match = LZ4_getPosition(ip, ctx->hashTable, tableType, base);
hashtable[LZ4_HASH64K_VALUE(ip)] = (u16)(ip - base); LZ4_putPosition(ip, ctx->hashTable, tableType, base);
if (A32(ref) == A32(ip)) {
token = op++; if ((match + MAX_DISTANCE >= ip)
*token = 0; && (LZ4_read32(match) == LZ4_read32(ip))) {
token = op++; *token = 0;
goto _next_match; goto _next_match;
} }
/* Prepare next loop */ /* Prepare next loop */
anchor = ip++; forwardH = LZ4_hashPosition(++ip, tableType);
forwardh = LZ4_HASH64K_VALUE(ip);
} }
_last_literals: _last_literals:
/* Encode Last Literals */ /* Encode Last Literals */
lastrun = (int)(iend - anchor); {
if (op + lastrun + 1 + (lastrun - RUN_MASK + 255) / 255 > oend) size_t lastRunSize = (size_t)(iend - anchor);
return 0;
if (lastrun >= (int)RUN_MASK) { if (op + 1 /* token */
*op++ = (RUN_MASK << ML_BITS); + ((lastRunSize + 240) / 255) /* litLength */
lastrun -= RUN_MASK; + lastRunSize /* literals */ > oend) {
for (; lastrun > 254 ; lastrun -= 255) /* adapt lastRunSize to fill 'dst' */
*op++ = 255; lastRunSize = (oend - op) - 1;
*op++ = (u8)lastrun; lastRunSize -= (lastRunSize + 240) / 255;
} else }
*op++ = (lastrun << ML_BITS); ip = anchor + lastRunSize;
memcpy(op, anchor, iend - anchor);
op += iend - anchor; if (lastRunSize >= RUN_MASK) {
size_t accumulator = lastRunSize - RUN_MASK;
*op++ = RUN_MASK << ML_BITS;
for (; accumulator >= 255; accumulator -= 255)
*op++ = 255;
*op++ = (BYTE) accumulator;
} else {
*op++ = (BYTE)(lastRunSize<<ML_BITS);
}
memcpy(op, anchor, lastRunSize);
op += lastRunSize;
}
/* End */ /* End */
return (int)(((char *)op) - dest); *srcSizePtr = (int) (((const char *)ip) - src);
return (int) (((char *)op) - dst);
} }
int lz4_compress(const unsigned char *src, size_t src_len, static int LZ4_compress_destSize_extState(
unsigned char *dst, size_t *dst_len, void *wrkmem) LZ4_stream_t *state,
const char *src,
char *dst,
int *srcSizePtr,
int targetDstSize)
{ {
int ret = -1; #if LZ4_ARCH64
int out_len = 0; const tableType_t tableType = byU32;
#else
const tableType_t tableType = byPtr;
#endif
if (src_len < LZ4_64KLIMIT) LZ4_resetStream(state);
out_len = lz4_compress64kctx(wrkmem, src, dst, src_len,
lz4_compressbound(src_len)); if (targetDstSize >= LZ4_COMPRESSBOUND(*srcSizePtr)) {
else /* compression success is guaranteed */
out_len = lz4_compressctx(wrkmem, src, dst, src_len, return LZ4_compress_fast_extState(
lz4_compressbound(src_len)); state, src, dst, *srcSizePtr,
targetDstSize, 1);
} else {
if (*srcSizePtr < LZ4_64Klimit)
return LZ4_compress_destSize_generic(
&state->internal_donotuse,
src, dst, srcSizePtr,
targetDstSize, byU16);
else
return LZ4_compress_destSize_generic(
&state->internal_donotuse,
src, dst, srcSizePtr,
targetDstSize, tableType);
}
}
int LZ4_compress_destSize(
const char *src,
char *dst,
int *srcSizePtr,
int targetDstSize,
void *wrkmem)
{
return LZ4_compress_destSize_extState(wrkmem, src, dst, srcSizePtr,
targetDstSize);
}
EXPORT_SYMBOL(LZ4_compress_destSize);
/*-******************************
* Streaming functions
********************************/
void LZ4_resetStream(LZ4_stream_t *LZ4_stream)
{
memset(LZ4_stream, 0, sizeof(LZ4_stream_t));
}
int LZ4_loadDict(LZ4_stream_t *LZ4_dict,
const char *dictionary, int dictSize)
{
LZ4_stream_t_internal *dict = &LZ4_dict->internal_donotuse;
const BYTE *p = (const BYTE *)dictionary;
const BYTE * const dictEnd = p + dictSize;
const BYTE *base;
if ((dict->initCheck)
|| (dict->currentOffset > 1 * GB)) {
/* Uninitialized structure, or reuse overflow */
LZ4_resetStream(LZ4_dict);
}
if (dictSize < (int)HASH_UNIT) {
dict->dictionary = NULL;
dict->dictSize = 0;
return 0;
}
if ((dictEnd - p) > 64 * KB)
p = dictEnd - 64 * KB;
dict->currentOffset += 64 * KB;
base = p - dict->currentOffset;
dict->dictionary = p;
dict->dictSize = (U32)(dictEnd - p);
dict->currentOffset += dict->dictSize;
while (p <= dictEnd - HASH_UNIT) {
LZ4_putPosition(p, dict->hashTable, byU32, base);
p += 3;
}
return dict->dictSize;
}
EXPORT_SYMBOL(LZ4_loadDict);
static void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict,
const BYTE *src)
{
if ((LZ4_dict->currentOffset > 0x80000000) ||
((uptrval)LZ4_dict->currentOffset > (uptrval)src)) {
/* address space overflow */
/* rescale hash table */
U32 const delta = LZ4_dict->currentOffset - 64 * KB;
const BYTE *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
int i;
for (i = 0; i < LZ4_HASH_SIZE_U32; i++) {
if (LZ4_dict->hashTable[i] < delta)
LZ4_dict->hashTable[i] = 0;
else
LZ4_dict->hashTable[i] -= delta;
}
LZ4_dict->currentOffset = 64 * KB;
if (LZ4_dict->dictSize > 64 * KB)
LZ4_dict->dictSize = 64 * KB;
LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
}
}
int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize)
{
LZ4_stream_t_internal * const dict = &LZ4_dict->internal_donotuse;
const BYTE * const previousDictEnd = dict->dictionary + dict->dictSize;
if ((U32)dictSize > 64 * KB) {
/* useless to define a dictionary > 64 * KB */
dictSize = 64 * KB;
}
if ((U32)dictSize > dict->dictSize)
dictSize = dict->dictSize;
memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
dict->dictionary = (const BYTE *)safeBuffer;
dict->dictSize = (U32)dictSize;
return dictSize;
}
EXPORT_SYMBOL(LZ4_saveDict);
int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source,
char *dest, int inputSize, int maxOutputSize, int acceleration)
{
LZ4_stream_t_internal *streamPtr = &LZ4_stream->internal_donotuse;
const BYTE * const dictEnd = streamPtr->dictionary
+ streamPtr->dictSize;
const BYTE *smallest = (const BYTE *) source;
if (streamPtr->initCheck) {
/* Uninitialized structure detected */
return 0;
}
if ((streamPtr->dictSize > 0) && (smallest > dictEnd))
smallest = dictEnd;
LZ4_renormDictT(streamPtr, smallest);
if (acceleration < 1)
acceleration = LZ4_ACCELERATION_DEFAULT;
if (out_len < 0) /* Check overlapping input/dictionary space */
goto exit; {
const BYTE *sourceEnd = (const BYTE *) source + inputSize;
if ((sourceEnd > streamPtr->dictionary)
&& (sourceEnd < dictEnd)) {
streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
if (streamPtr->dictSize > 64 * KB)
streamPtr->dictSize = 64 * KB;
if (streamPtr->dictSize < 4)
streamPtr->dictSize = 0;
streamPtr->dictionary = dictEnd - streamPtr->dictSize;
}
}
*dst_len = out_len; /* prefix mode : source data follows dictionary */
if (dictEnd == (const BYTE *)source) {
int result;
if ((streamPtr->dictSize < 64 * KB) &&
(streamPtr->dictSize < streamPtr->currentOffset)) {
result = LZ4_compress_generic(
streamPtr, source, dest, inputSize,
maxOutputSize, limitedOutput, byU32,
withPrefix64k, dictSmall, acceleration);
} else {
result = LZ4_compress_generic(
streamPtr, source, dest, inputSize,
maxOutputSize, limitedOutput, byU32,
withPrefix64k, noDictIssue, acceleration);
}
streamPtr->dictSize += (U32)inputSize;
streamPtr->currentOffset += (U32)inputSize;
return result;
}
return 0; /* external dictionary mode */
exit: {
return ret; int result;
if ((streamPtr->dictSize < 64 * KB) &&
(streamPtr->dictSize < streamPtr->currentOffset)) {
result = LZ4_compress_generic(
streamPtr, source, dest, inputSize,
maxOutputSize, limitedOutput, byU32,
usingExtDict, dictSmall, acceleration);
} else {
result = LZ4_compress_generic(
streamPtr, source, dest, inputSize,
maxOutputSize, limitedOutput, byU32,
usingExtDict, noDictIssue, acceleration);
}
streamPtr->dictionary = (const BYTE *)source;
streamPtr->dictSize = (U32)inputSize;
streamPtr->currentOffset += (U32)inputSize;
return result;
}
}
EXPORT_SYMBOL(LZ4_compress_fast_continue);
/*-******************************
* For backwards compatibility
********************************/
int lz4_compress(const unsigned char *src, size_t src_len, unsigned char *dst,
size_t *dst_len, void *wrkmem) {
*dst_len = LZ4_compress_default(src, dst, src_len,
*dst_len, wrkmem);
/*
* Prior lz4_compress will return -1 in case of error
* and 0 on success
* while new LZ4_compress_fast/default
* returns 0 in case of error
* and the output length on success
*/
if (!*dst_len)
return -1;
else
return 0;
} }
EXPORT_SYMBOL(lz4_compress); EXPORT_SYMBOL(lz4_compress);
......
/* /*
* LZ4 Decompressor for Linux kernel
*
* Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
*
* Based on LZ4 implementation by Yann Collet.
*
* LZ4 - Fast LZ compression algorithm * LZ4 - Fast LZ compression algorithm
* Copyright (C) 2011-2012, Yann Collet. * Copyright (C) 2011 - 2016, Yann Collet.
* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
*
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* * * Redistributions of source code must retain the above copyright
* * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer * copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the * in the documentation and/or other materials provided with the
* distribution. * distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -31,313 +22,529 @@ ...@@ -31,313 +22,529 @@
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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
* *
* You can contact the author at : * Changed for kernel usage by:
* - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
* - LZ4 source repository : http://code.google.com/p/lz4/
*/ */
#ifndef STATIC /*-************************************
* Dependencies
**************************************/
#include <linux/lz4.h>
#include "lz4defs.h"
#include <linux/init.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#endif
#include <linux/lz4.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
#include "lz4defs.h" /*-*****************************
* Decompression functions
static const int dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0}; *******************************/
#if LZ4_ARCH64 /* LZ4_decompress_generic() :
static const int dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3}; * This generic decompression function cover all use cases.
#endif * It shall be instantiated several times, using different sets of directives
* Note that it is important this generic function is really inlined,
static int lz4_uncompress(const char *source, char *dest, int osize) * in order to remove useless branches during compilation optimization.
*/
static FORCE_INLINE int LZ4_decompress_generic(
const char * const source,
char * const dest,
int inputSize,
/*
* If endOnInput == endOnInputSize,
* this value is the max size of Output Buffer.
*/
int outputSize,
/* endOnOutputSize, endOnInputSize */
int endOnInput,
/* full, partial */
int partialDecoding,
/* only used if partialDecoding == partial */
int targetOutputSize,
/* noDict, withPrefix64k, usingExtDict */
int dict,
/* == dest when no prefix */
const BYTE * const lowPrefix,
/* only if dict == usingExtDict */
const BYTE * const dictStart,
/* note : = 0 if noDict */
const size_t dictSize
)
{ {
/* Local Variables */
const BYTE *ip = (const BYTE *) source; const BYTE *ip = (const BYTE *) source;
const BYTE *ref; const BYTE * const iend = ip + inputSize;
BYTE *op = (BYTE *) dest; BYTE *op = (BYTE *) dest;
BYTE * const oend = op + osize; BYTE * const oend = op + outputSize;
BYTE *cpy; BYTE *cpy;
unsigned token; BYTE *oexit = op + targetOutputSize;
size_t length; const BYTE * const lowLimit = lowPrefix - dictSize;
const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
const unsigned int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 };
const int dec64table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
const int safeDecode = (endOnInput == endOnInputSize);
const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
/* Special cases */
/* targetOutputSize too high => decode everything */
if ((partialDecoding) && (oexit > oend - MFLIMIT))
oexit = oend - MFLIMIT;
/* Empty output buffer */
if ((endOnInput) && (unlikely(outputSize == 0)))
return ((inputSize == 1) && (*ip == 0)) ? 0 : -1;
if ((!endOnInput) && (unlikely(outputSize == 0)))
return (*ip == 0 ? 1 : -1);
/* Main Loop : decode sequences */
while (1) { while (1) {
size_t length;
const BYTE *match;
size_t offset;
/* get literal length */
unsigned int const token = *ip++;
length = token>>ML_BITS;
/* get runlength */
token = *ip++;
length = (token >> ML_BITS);
if (length == RUN_MASK) { if (length == RUN_MASK) {
size_t len; unsigned int s;
len = *ip++; do {
for (; len == 255; length += 255) s = *ip++;
len = *ip++; length += s;
if (unlikely(length > (size_t)(length + len))) } while (likely(endOnInput
? ip < iend - RUN_MASK
: 1) & (s == 255));
if ((safeDecode)
&& unlikely(
(size_t)(op + length) < (size_t)(op))) {
/* overflow detection */
goto _output_error; goto _output_error;
length += len; }
if ((safeDecode)
&& unlikely(
(size_t)(ip + length) < (size_t)(ip))) {
/* overflow detection */
goto _output_error;
}
} }
/* copy literals */ /* copy literals */
cpy = op + length; cpy = op + length;
if (unlikely(cpy > oend - COPYLENGTH)) { if (((endOnInput) && ((cpy > (partialDecoding ? oexit : oend - MFLIMIT))
/* || (ip + length > iend - (2 + 1 + LASTLITERALS))))
* Error: not enough place for another match || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
* (min 4) + 5 literals if (partialDecoding) {
*/ if (cpy > oend) {
if (cpy != oend) /*
goto _output_error; * Error :
* write attempt beyond end of output buffer
*/
goto _output_error;
}
if ((endOnInput)
&& (ip + length > iend)) {
/*
* Error :
* read attempt beyond
* end of input buffer
*/
goto _output_error;
}
} else {
if ((!endOnInput)
&& (cpy != oend)) {
/*
* Error :
* block decoding must
* stop exactly there
*/
goto _output_error;
}
if ((endOnInput)
&& ((ip + length != iend)
|| (cpy > oend))) {
/*
* Error :
* input must be consumed
*/
goto _output_error;
}
}
memcpy(op, ip, length); memcpy(op, ip, length);
ip += length; ip += length;
break; /* EOF */ op += length;
/* Necessarily EOF, due to parsing restrictions */
break;
} }
LZ4_WILDCOPY(ip, op, cpy);
ip -= (op - cpy); LZ4_wildCopy(op, ip, cpy);
ip += length;
op = cpy; op = cpy;
/* get offset */ /* get offset */
LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip); offset = LZ4_readLE16(ip);
ip += 2; ip += 2;
match = op - offset;
/* Error: offset create reference outside destination buffer */ if ((checkOffset) && (unlikely(match < lowLimit))) {
if (unlikely(ref < (BYTE *const) dest)) /* Error : offset outside buffers */
goto _output_error; goto _output_error;
}
/* costs ~1%; silence an msan warning when offset == 0 */
LZ4_write32(op, (U32)offset);
/* get matchlength */ /* get matchlength */
length = token & ML_MASK; length = token & ML_MASK;
if (length == ML_MASK) { if (length == ML_MASK) {
for (; *ip == 255; length += 255) unsigned int s;
ip++;
if (unlikely(length > (size_t)(length + *ip))) do {
s = *ip++;
if ((endOnInput) && (ip > iend - LASTLITERALS))
goto _output_error;
length += s;
} while (s == 255);
if ((safeDecode)
&& unlikely(
(size_t)(op + length) < (size_t)op)) {
/* overflow detection */
goto _output_error; goto _output_error;
length += *ip++; }
} }
/* copy repeated sequence */ length += MINMATCH;
if (unlikely((op - ref) < STEPSIZE)) {
#if LZ4_ARCH64 /* check external dictionary */
int dec64 = dec64table[op - ref]; if ((dict == usingExtDict) && (match < lowPrefix)) {
#else if (unlikely(op + length > oend - LASTLITERALS)) {
const int dec64 = 0; /* doesn't respect parsing restriction */
#endif goto _output_error;
op[0] = ref[0]; }
op[1] = ref[1];
op[2] = ref[2]; if (length <= (size_t)(lowPrefix - match)) {
op[3] = ref[3]; /*
op += 4; * match can be copied as a single segment
ref += 4; * from external dictionary
ref -= dec32table[op-ref]; */
PUT4(ref, op); memmove(op, dictEnd - (lowPrefix - match),
op += STEPSIZE - 4; length);
ref -= dec64; op += length;
} else {
/*
* match encompass external
* dictionary and current block
*/
size_t const copySize = (size_t)(lowPrefix - match);
size_t const restSize = length - copySize;
memcpy(op, dictEnd - copySize, copySize);
op += copySize;
if (restSize > (size_t)(op - lowPrefix)) {
/* overlap copy */
BYTE * const endOfMatch = op + restSize;
const BYTE *copyFrom = lowPrefix;
while (op < endOfMatch)
*op++ = *copyFrom++;
} else {
memcpy(op, lowPrefix, restSize);
op += restSize;
}
}
continue;
}
/* copy match within block */
cpy = op + length;
if (unlikely(offset < 8)) {
const int dec64 = dec64table[offset];
op[0] = match[0];
op[1] = match[1];
op[2] = match[2];
op[3] = match[3];
match += dec32table[offset];
memcpy(op + 4, match, 4);
match -= dec64;
} else { } else {
LZ4_COPYSTEP(ref, op); LZ4_copy8(op, match);
match += 8;
} }
cpy = op + length - (STEPSIZE - 4);
if (cpy > (oend - COPYLENGTH)) {
/* Error: request to write beyond destination buffer */ op += 8;
if (cpy > oend)
goto _output_error; if (unlikely(cpy > oend - 12)) {
#if LZ4_ARCH64 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
if ((ref + COPYLENGTH) > oend)
#else if (cpy > oend - LASTLITERALS) {
if ((ref + COPYLENGTH) > oend || /*
(op + COPYLENGTH) > oend) * Error : last LASTLITERALS bytes
#endif * must be literals (uncompressed)
*/
goto _output_error; goto _output_error;
LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH)); }
if (op < oCopyLimit) {
LZ4_wildCopy(op, match, oCopyLimit);
match += oCopyLimit - op;
op = oCopyLimit;
}
while (op < cpy) while (op < cpy)
*op++ = *ref++; *op++ = *match++;
op = cpy; } else {
/* LZ4_copy8(op, match);
* Check EOF (should never happen, since last 5 bytes
* are supposed to be literals) if (length > 16)
*/ LZ4_wildCopy(op + 8, match + 8, cpy);
if (op == oend)
goto _output_error;
continue;
} }
LZ4_SECURECOPY(ref, op, cpy);
op = cpy; /* correction */ op = cpy; /* correction */
} }
/* end of decoding */ /* end of decoding */
return (int) (((char *)ip) - source); if (endOnInput) {
/* Nb of output bytes decoded */
return (int) (((char *)op) - dest);
} else {
/* Nb of input bytes read */
return (int) (((const char *)ip) - source);
}
/* write overflow error detected */ /* Overflow error detected */
_output_error: _output_error:
return -1; return -1;
} }
static int lz4_uncompress_unknownoutputsize(const char *source, char *dest, int LZ4_decompress_safe(const char *source, char *dest,
int isize, size_t maxoutputsize) int compressedSize, int maxDecompressedSize)
{ {
const BYTE *ip = (const BYTE *) source; return LZ4_decompress_generic(source, dest, compressedSize,
const BYTE *const iend = ip + isize; maxDecompressedSize, endOnInputSize, full, 0,
const BYTE *ref; noDict, (BYTE *)dest, NULL, 0);
}
BYTE *op = (BYTE *) dest; int LZ4_decompress_safe_partial(const char *source, char *dest,
BYTE * const oend = op + maxoutputsize; int compressedSize, int targetOutputSize, int maxDecompressedSize)
BYTE *cpy; {
return LZ4_decompress_generic(source, dest, compressedSize,
maxDecompressedSize, endOnInputSize, partial,
targetOutputSize, noDict, (BYTE *)dest, NULL, 0);
}
/* Main Loop */ int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
while (ip < iend) { {
return LZ4_decompress_generic(source, dest, 0, originalSize,
endOnOutputSize, full, 0, withPrefix64k,
(BYTE *)(dest - 64 * KB), NULL, 64 * KB);
}
unsigned token; int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
size_t length; const char *dictionary, int dictSize)
{
LZ4_streamDecode_t_internal *lz4sd = (LZ4_streamDecode_t_internal *) LZ4_streamDecode;
/* get runlength */ lz4sd->prefixSize = (size_t) dictSize;
token = *ip++; lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
length = (token >> ML_BITS); lz4sd->externalDict = NULL;
if (length == RUN_MASK) { lz4sd->extDictSize = 0;
int s = 255; return 1;
while ((ip < iend) && (s == 255)) { }
s = *ip++;
if (unlikely(length > (size_t)(length + s)))
goto _output_error;
length += s;
}
}
/* copy literals */
cpy = op + length;
if ((cpy > oend - COPYLENGTH) ||
(ip + length > iend - COPYLENGTH)) {
if (cpy > oend)
goto _output_error;/* writes beyond buffer */
if (ip + length != iend)
goto _output_error;/*
* Error: LZ4 format requires
* to consume all input
* at this stage
*/
memcpy(op, ip, length);
op += length;
break;/* Necessarily EOF, due to parsing restrictions */
}
LZ4_WILDCOPY(ip, op, cpy);
ip -= (op - cpy);
op = cpy;
/* get offset */ /*
LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip); * *_continue() :
ip += 2; * These decoding functions allow decompression of multiple blocks
if (ref < (BYTE * const) dest) * in "streaming" mode.
goto _output_error; * Previously decoded blocks must still be available at the memory
/* * position where they were decoded.
* Error : offset creates reference * If it's not possible, save the relevant part of
* outside of destination buffer * decoded data into a safe buffer,
*/ * and indicate where it stands using LZ4_setStreamDecode()
*/
int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
const char *source, char *dest, int compressedSize, int maxOutputSize)
{
LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
int result;
if (lz4sd->prefixEnd == (BYTE *)dest) {
result = LZ4_decompress_generic(source, dest,
compressedSize,
maxOutputSize,
endOnInputSize, full, 0,
usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize,
lz4sd->externalDict,
lz4sd->extDictSize);
if (result <= 0)
return result;
lz4sd->prefixSize += result;
lz4sd->prefixEnd += result;
} else {
lz4sd->extDictSize = lz4sd->prefixSize;
lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
result = LZ4_decompress_generic(source, dest,
compressedSize, maxOutputSize,
endOnInputSize, full, 0,
usingExtDict, (BYTE *)dest,
lz4sd->externalDict, lz4sd->extDictSize);
if (result <= 0)
return result;
lz4sd->prefixSize = result;
lz4sd->prefixEnd = (BYTE *)dest + result;
}
/* get matchlength */ return result;
length = (token & ML_MASK); }
if (length == ML_MASK) {
while (ip < iend) {
int s = *ip++;
if (unlikely(length > (size_t)(length + s)))
goto _output_error;
length += s;
if (s == 255)
continue;
break;
}
}
/* copy repeated sequence */ int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
if (unlikely((op - ref) < STEPSIZE)) { const char *source, char *dest, int originalSize)
#if LZ4_ARCH64 {
int dec64 = dec64table[op - ref]; LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
#else int result;
const int dec64 = 0;
#endif if (lz4sd->prefixEnd == (BYTE *)dest) {
op[0] = ref[0]; result = LZ4_decompress_generic(source, dest, 0, originalSize,
op[1] = ref[1]; endOnOutputSize, full, 0,
op[2] = ref[2]; usingExtDict,
op[3] = ref[3]; lz4sd->prefixEnd - lz4sd->prefixSize,
op += 4; lz4sd->externalDict, lz4sd->extDictSize);
ref += 4;
ref -= dec32table[op - ref]; if (result <= 0)
PUT4(ref, op); return result;
op += STEPSIZE - 4;
ref -= dec64; lz4sd->prefixSize += originalSize;
} else { lz4sd->prefixEnd += originalSize;
LZ4_COPYSTEP(ref, op); } else {
} lz4sd->extDictSize = lz4sd->prefixSize;
cpy = op + length - (STEPSIZE-4); lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
if (cpy > oend - COPYLENGTH) { result = LZ4_decompress_generic(source, dest, 0, originalSize,
if (cpy > oend) endOnOutputSize, full, 0,
goto _output_error; /* write outside of buf */ usingExtDict, (BYTE *)dest,
#if LZ4_ARCH64 lz4sd->externalDict, lz4sd->extDictSize);
if ((ref + COPYLENGTH) > oend) if (result <= 0)
#else return result;
if ((ref + COPYLENGTH) > oend || lz4sd->prefixSize = originalSize;
(op + COPYLENGTH) > oend) lz4sd->prefixEnd = (BYTE *)dest + originalSize;
#endif
goto _output_error;
LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
while (op < cpy)
*op++ = *ref++;
op = cpy;
/*
* Check EOF (should never happen, since last 5 bytes
* are supposed to be literals)
*/
if (op == oend)
goto _output_error;
continue;
}
LZ4_SECURECOPY(ref, op, cpy);
op = cpy; /* correction */
} }
/* end of decoding */
return (int) (((char *) op) - dest);
/* write overflow error detected */ return result;
_output_error:
return -1;
} }
int lz4_decompress(const unsigned char *src, size_t *src_len, /*
unsigned char *dest, size_t actual_dest_len) * Advanced decoding functions :
* *_usingDict() :
* These decoding functions work the same as "_continue" ones,
* the dictionary must be explicitly provided within parameters
*/
static FORCE_INLINE int LZ4_decompress_usingDict_generic(const char *source,
char *dest, int compressedSize, int maxOutputSize, int safe,
const char *dictStart, int dictSize)
{ {
int ret = -1; if (dictSize == 0)
int input_len = 0; return LZ4_decompress_generic(source, dest,
compressedSize, maxOutputSize, safe, full, 0,
input_len = lz4_uncompress(src, dest, actual_dest_len); noDict, (BYTE *)dest, NULL, 0);
if (input_len < 0) if (dictStart + dictSize == dest) {
goto exit_0; if (dictSize >= (int)(64 * KB - 1))
*src_len = input_len; return LZ4_decompress_generic(source, dest,
compressedSize, maxOutputSize, safe, full, 0,
withPrefix64k, (BYTE *)dest - 64 * KB, NULL, 0);
return LZ4_decompress_generic(source, dest, compressedSize,
maxOutputSize, safe, full, 0, noDict,
(BYTE *)dest - dictSize, NULL, 0);
}
return LZ4_decompress_generic(source, dest, compressedSize,
maxOutputSize, safe, full, 0, usingExtDict,
(BYTE *)dest, (const BYTE *)dictStart, dictSize);
}
return 0; int LZ4_decompress_safe_usingDict(const char *source, char *dest,
exit_0: int compressedSize, int maxOutputSize,
return ret; const char *dictStart, int dictSize)
{
return LZ4_decompress_usingDict_generic(source, dest,
compressedSize, maxOutputSize, 1, dictStart, dictSize);
} }
#ifndef STATIC
EXPORT_SYMBOL(lz4_decompress);
#endif
int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len, int LZ4_decompress_fast_usingDict(const char *source, char *dest,
unsigned char *dest, size_t *dest_len) int originalSize, const char *dictStart, int dictSize)
{ {
int ret = -1; return LZ4_decompress_usingDict_generic(source, dest, 0,
int out_len = 0; originalSize, 0, dictStart, dictSize);
out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
*dest_len);
if (out_len < 0)
goto exit_0;
*dest_len = out_len;
return 0;
exit_0:
return ret;
} }
/*-******************************
* For backwards compatibility
********************************/
int lz4_decompress_unknownoutputsize(const unsigned char *src,
size_t src_len, unsigned char *dest, size_t *dest_len) {
*dest_len = LZ4_decompress_safe(src, dest,
src_len, *dest_len);
/*
* Prior lz4_decompress_unknownoutputsize will return
* 0 for success and a negative result for error
* new LZ4_decompress_safe returns
* - the length of data read on success
* - and also a negative result on error
* meaning when result > 0, we just return 0 here
*/
if (src_len > 0)
return 0;
else
return -1;
}
int lz4_decompress(const unsigned char *src, size_t *src_len,
unsigned char *dest, size_t actual_dest_len) {
*src_len = LZ4_decompress_fast(src, dest, actual_dest_len);
/*
* Prior lz4_decompress will return
* 0 for success and a negative result for error
* new LZ4_decompress_fast returns
* - the length of data read on success
* - and also a negative result on error
* meaning when result > 0, we just return 0 here
*/
if (*src_len > 0)
return 0;
else
return -1;
}
#ifndef STATIC #ifndef STATIC
EXPORT_SYMBOL(LZ4_decompress_safe);
EXPORT_SYMBOL(LZ4_decompress_safe_partial);
EXPORT_SYMBOL(LZ4_decompress_fast);
EXPORT_SYMBOL(LZ4_setStreamDecode);
EXPORT_SYMBOL(LZ4_decompress_safe_continue);
EXPORT_SYMBOL(LZ4_decompress_fast_continue);
EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
EXPORT_SYMBOL(lz4_decompress_unknownoutputsize); EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
EXPORT_SYMBOL(lz4_decompress);
MODULE_LICENSE("Dual BSD/GPL"); MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("LZ4 Decompressor"); MODULE_DESCRIPTION("LZ4 decompressor");
#endif #endif
#ifndef __LZ4DEFS_H__
#define __LZ4DEFS_H__
/* /*
* lz4defs.h -- architecture specific defines * lz4defs.h -- common and architecture specific defines for the kernel usage
*
* Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com> * 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
* *
* This program is free software; you can redistribute it and/or modify * Changed for kernel usage by:
* it under the terms of the GNU General Public License version 2 as * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
* published by the Free Software Foundation.
*/ */
/* #include <asm/unaligned.h>
* Detects 64 bits mode #include <linux/string.h> /* memset, memcpy */
*/
#define FORCE_INLINE __always_inline
/*-************************************
* Basic Types
**************************************/
#include <linux/types.h>
typedef uint8_t BYTE;
typedef uint16_t U16;
typedef uint32_t U32;
typedef int32_t S32;
typedef uint64_t U64;
typedef uintptr_t uptrval;
/*-************************************
* Architecture specifics
**************************************/
#if defined(CONFIG_64BIT) #if defined(CONFIG_64BIT)
#define LZ4_ARCH64 1 #define LZ4_ARCH64 1
#else #else
#define LZ4_ARCH64 0 #define LZ4_ARCH64 0
#endif #endif
/* #if defined(__LITTLE_ENDIAN)
* Architecture-specific macros #define LZ4_LITTLE_ENDIAN 1
*/ #else
#define BYTE u8 #define LZ4_LITTLE_ENDIAN 0
typedef struct _U16_S { u16 v; } U16_S;
typedef struct _U32_S { u32 v; } U32_S;
typedef struct _U64_S { u64 v; } U64_S;
#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
#define A16(x) (((U16_S *)(x))->v)
#define A32(x) (((U32_S *)(x))->v)
#define A64(x) (((U64_S *)(x))->v)
#define PUT4(s, d) (A32(d) = A32(s))
#define PUT8(s, d) (A64(d) = A64(s))
#define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
(d = s - A16(p))
#define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
do { \
A16(p) = v; \
p += 2; \
} while (0)
#else /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
#define A64(x) get_unaligned((u64 *)&(((U16_S *)(x))->v))
#define A32(x) get_unaligned((u32 *)&(((U16_S *)(x))->v))
#define A16(x) get_unaligned((u16 *)&(((U16_S *)(x))->v))
#define PUT4(s, d) \
put_unaligned(get_unaligned((const u32 *) s), (u32 *) d)
#define PUT8(s, d) \
put_unaligned(get_unaligned((const u64 *) s), (u64 *) d)
#define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
(d = s - get_unaligned_le16(p))
#define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
do { \
put_unaligned_le16(v, (u16 *)(p)); \
p += 2; \
} while (0)
#endif #endif
#define COPYLENGTH 8 /*-************************************
#define ML_BITS 4 * Constants
#define ML_MASK ((1U << ML_BITS) - 1) **************************************/
#define MINMATCH 4
#define WILDCOPYLENGTH 8
#define LASTLITERALS 5
#define MFLIMIT (WILDCOPYLENGTH + MINMATCH)
/* Increase this value ==> compression run slower on incompressible data */
#define LZ4_SKIPTRIGGER 6
#define HASH_UNIT sizeof(size_t)
#define KB (1 << 10)
#define MB (1 << 20)
#define GB (1U << 30)
#define MAXD_LOG 16
#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
#define STEPSIZE sizeof(size_t)
#define ML_BITS 4
#define ML_MASK ((1U << ML_BITS) - 1)
#define RUN_BITS (8 - ML_BITS) #define RUN_BITS (8 - ML_BITS)
#define RUN_MASK ((1U << RUN_BITS) - 1) #define RUN_MASK ((1U << RUN_BITS) - 1)
#define MEMORY_USAGE 14
#define MINMATCH 4 /*-************************************
#define SKIPSTRENGTH 6 * Reading and writing into memory
#define LASTLITERALS 5 **************************************/
#define MFLIMIT (COPYLENGTH + MINMATCH) static FORCE_INLINE U16 LZ4_read16(const void *ptr)
#define MINLENGTH (MFLIMIT + 1) {
#define MAXD_LOG 16 return get_unaligned((const U16 *)ptr);
#define MAXD (1 << MAXD_LOG) }
#define MAXD_MASK (u32)(MAXD - 1)
#define MAX_DISTANCE (MAXD - 1) static FORCE_INLINE U32 LZ4_read32(const void *ptr)
#define HASH_LOG (MAXD_LOG - 1) {
#define HASHTABLESIZE (1 << HASH_LOG) return get_unaligned((const U32 *)ptr);
#define MAX_NB_ATTEMPTS 256 }
#define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH)
#define LZ4_64KLIMIT ((1<<16) + (MFLIMIT - 1)) static FORCE_INLINE size_t LZ4_read_ARCH(const void *ptr)
#define HASHLOG64K ((MEMORY_USAGE - 2) + 1) {
#define HASH64KTABLESIZE (1U << HASHLOG64K) return get_unaligned((const size_t *)ptr);
#define LZ4_HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \ }
((MINMATCH * 8) - (MEMORY_USAGE-2)))
#define LZ4_HASH64K_VALUE(p) (((A32(p)) * 2654435761U) >> \ static FORCE_INLINE void LZ4_write16(void *memPtr, U16 value)
((MINMATCH * 8) - HASHLOG64K)) {
#define HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \ put_unaligned(value, (U16 *)memPtr);
((MINMATCH * 8) - HASH_LOG)) }
#if LZ4_ARCH64/* 64-bit */ static FORCE_INLINE void LZ4_write32(void *memPtr, U32 value)
#define STEPSIZE 8 {
put_unaligned(value, (U32 *)memPtr);
#define LZ4_COPYSTEP(s, d) \ }
do { \
PUT8(s, d); \ static FORCE_INLINE U16 LZ4_readLE16(const void *memPtr)
d += 8; \ {
s += 8; \ return get_unaligned_le16(memPtr);
} while (0) }
#define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d) static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
{
#define LZ4_SECURECOPY(s, d, e) \ return put_unaligned_le16(value, memPtr);
do { \ }
if (d < e) { \
LZ4_WILDCOPY(s, d, e); \ static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
} \ {
} while (0) #if LZ4_ARCH64
#define HTYPE u32 U64 a = get_unaligned((const U64 *)src);
#ifdef __BIG_ENDIAN put_unaligned(a, (U64 *)dst);
#define LZ4_NBCOMMONBYTES(val) (__builtin_clzll(val) >> 3) #else
U32 a = get_unaligned((const U32 *)src);
U32 b = get_unaligned((const U32 *)src + 1);
put_unaligned(a, (U32 *)dst);
put_unaligned(b, (U32 *)dst + 1);
#endif
}
/*
* customized variant of memcpy,
* which can overwrite up to 7 bytes beyond dstEnd
*/
static FORCE_INLINE void LZ4_wildCopy(void *dstPtr,
const void *srcPtr, void *dstEnd)
{
BYTE *d = (BYTE *)dstPtr;
const BYTE *s = (const BYTE *)srcPtr;
BYTE *const e = (BYTE *)dstEnd;
do {
LZ4_copy8(d, s);
d += 8;
s += 8;
} while (d < e);
}
static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val)
{
#if LZ4_LITTLE_ENDIAN
return __ffs(val) >> 3;
#else #else
#define LZ4_NBCOMMONBYTES(val) (__builtin_ctzll(val) >> 3) return (BITS_PER_LONG - 1 - __fls(val)) >> 3;
#endif
}
static FORCE_INLINE unsigned int LZ4_count(
const BYTE *pIn,
const BYTE *pMatch,
const BYTE *pInLimit)
{
const BYTE *const pStart = pIn;
while (likely(pIn < pInLimit - (STEPSIZE - 1))) {
size_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
if (!diff) {
pIn += STEPSIZE;
pMatch += STEPSIZE;
continue;
}
pIn += LZ4_NbCommonBytes(diff);
return (unsigned int)(pIn - pStart);
}
#if LZ4_ARCH64
if ((pIn < (pInLimit - 3))
&& (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
pIn += 4;
pMatch += 4;
}
#endif #endif
#else /* 32-bit */ if ((pIn < (pInLimit - 1))
#define STEPSIZE 4 && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
pIn += 2;
pMatch += 2;
}
#define LZ4_COPYSTEP(s, d) \ if ((pIn < pInLimit) && (*pMatch == *pIn))
do { \ pIn++;
PUT4(s, d); \
d += 4; \
s += 4; \
} while (0)
#define LZ4_COPYPACKET(s, d) \ return (unsigned int)(pIn - pStart);
do { \ }
LZ4_COPYSTEP(s, d); \
LZ4_COPYSTEP(s, d); \
} while (0)
#define LZ4_SECURECOPY LZ4_WILDCOPY typedef enum { noLimit = 0, limitedOutput = 1 } limitedOutput_directive;
#define HTYPE const u8* typedef enum { byPtr, byU32, byU16 } tableType_t;
#ifdef __BIG_ENDIAN typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
#define LZ4_NBCOMMONBYTES(val) (__builtin_clz(val) >> 3) typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
#else
#define LZ4_NBCOMMONBYTES(val) (__builtin_ctz(val) >> 3)
#endif
#endif typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
typedef enum { full = 0, partial = 1 } earlyEnd_directive;
#define LZ4_WILDCOPY(s, d, e) \ #endif
do { \
LZ4_COPYPACKET(s, d); \
} while (d < e)
#define LZ4_BLINDCOPY(s, d, l) \
do { \
u8 *e = (d) + l; \
LZ4_WILDCOPY(s, d, e); \
d = e; \
} while (0)
/* /*
* LZ4 HC - High Compression Mode of LZ4 * LZ4 HC - High Compression Mode of LZ4
* Copyright (C) 2011-2012, Yann Collet. * Copyright (C) 2011-2015, Yann Collet.
* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
* *
* BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* * * Redistributions of source code must retain the above copyright
* * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer * copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the * in the documentation and/or other materials provided with the
* distribution. * distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -25,323 +23,361 @@ ...@@ -25,323 +23,361 @@
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at : * You can contact the author at :
* - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html * - LZ4 homepage : http://www.lz4.org
* - LZ4 source repository : http://code.google.com/p/lz4/ * - LZ4 source repository : https://github.com/lz4/lz4
* *
* Changed for kernel use by: * Changed for kernel usage by:
* Chanho Min <chanho.min@lge.com> * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
*/ */
#include <linux/module.h> /*-************************************
#include <linux/kernel.h> * Dependencies
**************************************/
#include <linux/lz4.h> #include <linux/lz4.h>
#include <asm/unaligned.h>
#include "lz4defs.h" #include "lz4defs.h"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h> /* memset */
/* *************************************
* Local Constants and types
***************************************/
struct lz4hc_data { #define OPTIMAL_ML (int)((ML_MASK - 1) + MINMATCH)
const u8 *base;
HTYPE hashtable[HASHTABLESIZE];
u16 chaintable[MAXD];
const u8 *nexttoupdate;
} __attribute__((__packed__));
static inline int lz4hc_init(struct lz4hc_data *hc4, const u8 *base) #define HASH_FUNCTION(i) (((i) * 2654435761U) \
>> ((MINMATCH*8) - LZ4HC_HASH_LOG))
#define DELTANEXTU16(p) chainTable[(U16)(p)] /* faster */
static U32 LZ4HC_hashPtr(const void *ptr)
{
return HASH_FUNCTION(LZ4_read32(ptr));
}
/**************************************
* HC Compression
**************************************/
static void LZ4HC_init(LZ4HC_CCtx_internal *hc4, const BYTE *start)
{ {
memset((void *)hc4->hashtable, 0, sizeof(hc4->hashtable)); memset((void *)hc4->hashTable, 0, sizeof(hc4->hashTable));
memset(hc4->chaintable, 0xFF, sizeof(hc4->chaintable)); memset(hc4->chainTable, 0xFF, sizeof(hc4->chainTable));
hc4->nextToUpdate = 64 * KB;
#if LZ4_ARCH64 hc4->base = start - 64 * KB;
hc4->nexttoupdate = base + 1; hc4->end = start;
#else hc4->dictBase = start - 64 * KB;
hc4->nexttoupdate = base; hc4->dictLimit = 64 * KB;
#endif hc4->lowLimit = 64 * KB;
hc4->base = base;
return 1;
} }
/* Update chains up to ip (excluded) */ /* Update chains up to ip (excluded) */
static inline void lz4hc_insert(struct lz4hc_data *hc4, const u8 *ip) static FORCE_INLINE void LZ4HC_Insert(LZ4HC_CCtx_internal *hc4,
const BYTE *ip)
{ {
u16 *chaintable = hc4->chaintable; U16 * const chainTable = hc4->chainTable;
HTYPE *hashtable = hc4->hashtable; U32 * const hashTable = hc4->hashTable;
#if LZ4_ARCH64
const BYTE * const base = hc4->base; const BYTE * const base = hc4->base;
#else U32 const target = (U32)(ip - base);
const int base = 0; U32 idx = hc4->nextToUpdate;
#endif
while (idx < target) {
U32 const h = LZ4HC_hashPtr(base + idx);
size_t delta = idx - hashTable[h];
while (hc4->nexttoupdate < ip) {
const u8 *p = hc4->nexttoupdate;
size_t delta = p - (hashtable[HASH_VALUE(p)] + base);
if (delta > MAX_DISTANCE) if (delta > MAX_DISTANCE)
delta = MAX_DISTANCE; delta = MAX_DISTANCE;
chaintable[(size_t)(p) & MAXD_MASK] = (u16)delta;
hashtable[HASH_VALUE(p)] = (p) - base;
hc4->nexttoupdate++;
}
}
static inline size_t lz4hc_commonlength(const u8 *p1, const u8 *p2, DELTANEXTU16(idx) = (U16)delta;
const u8 *const matchlimit)
{
const u8 *p1t = p1;
while (p1t < matchlimit - (STEPSIZE - 1)) {
#if LZ4_ARCH64
u64 diff = A64(p2) ^ A64(p1t);
#else
u32 diff = A32(p2) ^ A32(p1t);
#endif
if (!diff) {
p1t += STEPSIZE;
p2 += STEPSIZE;
continue;
}
p1t += LZ4_NBCOMMONBYTES(diff);
return p1t - p1;
}
#if LZ4_ARCH64
if ((p1t < (matchlimit-3)) && (A32(p2) == A32(p1t))) {
p1t += 4;
p2 += 4;
}
#endif
if ((p1t < (matchlimit - 1)) && (A16(p2) == A16(p1t))) { hashTable[h] = idx;
p1t += 2; idx++;
p2 += 2;
} }
if ((p1t < matchlimit) && (*p2 == *p1t))
p1t++; hc4->nextToUpdate = target;
return p1t - p1;
} }
static inline int lz4hc_insertandfindbestmatch(struct lz4hc_data *hc4, static FORCE_INLINE int LZ4HC_InsertAndFindBestMatch(
const u8 *ip, const u8 *const matchlimit, const u8 **matchpos) LZ4HC_CCtx_internal *hc4, /* Index table will be updated */
const BYTE *ip,
const BYTE * const iLimit,
const BYTE **matchpos,
const int maxNbAttempts)
{ {
u16 *const chaintable = hc4->chaintable; U16 * const chainTable = hc4->chainTable;
HTYPE *const hashtable = hc4->hashtable; U32 * const HashTable = hc4->hashTable;
const u8 *ref;
#if LZ4_ARCH64
const BYTE * const base = hc4->base; const BYTE * const base = hc4->base;
#else const BYTE * const dictBase = hc4->dictBase;
const int base = 0; const U32 dictLimit = hc4->dictLimit;
#endif const U32 lowLimit = (hc4->lowLimit + 64 * KB > (U32)(ip - base))
int nbattempts = MAX_NB_ATTEMPTS; ? hc4->lowLimit
size_t repl = 0, ml = 0; : (U32)(ip - base) - (64 * KB - 1);
u16 delta; U32 matchIndex;
int nbAttempts = maxNbAttempts;
size_t ml = 0;
/* HC4 match finder */ /* HC4 match finder */
lz4hc_insert(hc4, ip); LZ4HC_Insert(hc4, ip);
ref = hashtable[HASH_VALUE(ip)] + base; matchIndex = HashTable[LZ4HC_hashPtr(ip)];
/* potential repetition */ while ((matchIndex >= lowLimit)
if (ref >= ip-4) { && (nbAttempts)) {
/* confirmed */ nbAttempts--;
if (A32(ref) == A32(ip)) { if (matchIndex >= dictLimit) {
delta = (u16)(ip-ref); const BYTE * const match = base + matchIndex;
repl = ml = lz4hc_commonlength(ip + MINMATCH,
ref + MINMATCH, matchlimit) + MINMATCH; if (*(match + ml) == *(ip + ml)
*matchpos = ref; && (LZ4_read32(match) == LZ4_read32(ip))) {
} size_t const mlt = LZ4_count(ip + MINMATCH,
ref -= (size_t)chaintable[(size_t)(ref) & MAXD_MASK]; match + MINMATCH, iLimit) + MINMATCH;
}
while ((ref >= ip - MAX_DISTANCE) && nbattempts) {
nbattempts--;
if (*(ref + ml) == *(ip + ml)) {
if (A32(ref) == A32(ip)) {
size_t mlt =
lz4hc_commonlength(ip + MINMATCH,
ref + MINMATCH, matchlimit) + MINMATCH;
if (mlt > ml) { if (mlt > ml) {
ml = mlt; ml = mlt;
*matchpos = ref; *matchpos = match;
}
}
} else {
const BYTE * const match = dictBase + matchIndex;
if (LZ4_read32(match) == LZ4_read32(ip)) {
size_t mlt;
const BYTE *vLimit = ip
+ (dictLimit - matchIndex);
if (vLimit > iLimit)
vLimit = iLimit;
mlt = LZ4_count(ip + MINMATCH,
match + MINMATCH, vLimit) + MINMATCH;
if ((ip + mlt == vLimit)
&& (vLimit < iLimit))
mlt += LZ4_count(ip + mlt,
base + dictLimit,
iLimit);
if (mlt > ml) {
/* virtual matchpos */
ml = mlt;
*matchpos = base + matchIndex;
} }
} }
} }
ref -= (size_t)chaintable[(size_t)(ref) & MAXD_MASK]; matchIndex -= DELTANEXTU16(matchIndex);
}
/* Complete table */
if (repl) {
const BYTE *ptr = ip;
const BYTE *end;
end = ip + repl - (MINMATCH-1);
/* Pre-Load */
while (ptr < end - delta) {
chaintable[(size_t)(ptr) & MAXD_MASK] = delta;
ptr++;
}
do {
chaintable[(size_t)(ptr) & MAXD_MASK] = delta;
/* Head of chain */
hashtable[HASH_VALUE(ptr)] = (ptr) - base;
ptr++;
} while (ptr < end);
hc4->nexttoupdate = end;
} }
return (int)ml; return (int)ml;
} }
static inline int lz4hc_insertandgetwidermatch(struct lz4hc_data *hc4, static FORCE_INLINE int LZ4HC_InsertAndGetWiderMatch(
const u8 *ip, const u8 *startlimit, const u8 *matchlimit, int longest, LZ4HC_CCtx_internal *hc4,
const u8 **matchpos, const u8 **startpos) const BYTE * const ip,
const BYTE * const iLowLimit,
const BYTE * const iHighLimit,
int longest,
const BYTE **matchpos,
const BYTE **startpos,
const int maxNbAttempts)
{ {
u16 *const chaintable = hc4->chaintable; U16 * const chainTable = hc4->chainTable;
HTYPE *const hashtable = hc4->hashtable; U32 * const HashTable = hc4->hashTable;
#if LZ4_ARCH64
const BYTE * const base = hc4->base; const BYTE * const base = hc4->base;
#else const U32 dictLimit = hc4->dictLimit;
const int base = 0; const BYTE * const lowPrefixPtr = base + dictLimit;
#endif const U32 lowLimit = (hc4->lowLimit + 64 * KB > (U32)(ip - base))
const u8 *ref; ? hc4->lowLimit
int nbattempts = MAX_NB_ATTEMPTS; : (U32)(ip - base) - (64 * KB - 1);
int delta = (int)(ip - startlimit); const BYTE * const dictBase = hc4->dictBase;
U32 matchIndex;
int nbAttempts = maxNbAttempts;
int delta = (int)(ip - iLowLimit);
/* First Match */ /* First Match */
lz4hc_insert(hc4, ip); LZ4HC_Insert(hc4, ip);
ref = hashtable[HASH_VALUE(ip)] + base; matchIndex = HashTable[LZ4HC_hashPtr(ip)];
while ((ref >= ip - MAX_DISTANCE) && (ref >= hc4->base) while ((matchIndex >= lowLimit)
&& (nbattempts)) { && (nbAttempts)) {
nbattempts--; nbAttempts--;
if (*(startlimit + longest) == *(ref - delta + longest)) { if (matchIndex >= dictLimit) {
if (A32(ref) == A32(ip)) { const BYTE *matchPtr = base + matchIndex;
const u8 *reft = ref + MINMATCH;
const u8 *ipt = ip + MINMATCH; if (*(iLowLimit + longest)
const u8 *startt = ip; == *(matchPtr - delta + longest)) {
if (LZ4_read32(matchPtr) == LZ4_read32(ip)) {
while (ipt < matchlimit-(STEPSIZE - 1)) { int mlt = MINMATCH + LZ4_count(
#if LZ4_ARCH64 ip + MINMATCH,
u64 diff = A64(reft) ^ A64(ipt); matchPtr + MINMATCH,
#else iHighLimit);
u32 diff = A32(reft) ^ A32(ipt); int back = 0;
#endif
while ((ip + back > iLowLimit)
if (!diff) { && (matchPtr + back > lowPrefixPtr)
ipt += STEPSIZE; && (ip[back - 1] == matchPtr[back - 1]))
reft += STEPSIZE; back--;
continue;
mlt -= back;
if (mlt > longest) {
longest = (int)mlt;
*matchpos = matchPtr + back;
*startpos = ip + back;
} }
ipt += LZ4_NBCOMMONBYTES(diff);
goto _endcount;
}
#if LZ4_ARCH64
if ((ipt < (matchlimit - 3))
&& (A32(reft) == A32(ipt))) {
ipt += 4;
reft += 4;
} }
ipt += 2; }
#endif } else {
if ((ipt < (matchlimit - 1)) const BYTE * const matchPtr = dictBase + matchIndex;
&& (A16(reft) == A16(ipt))) {
reft += 2; if (LZ4_read32(matchPtr) == LZ4_read32(ip)) {
} size_t mlt;
if ((ipt < matchlimit) && (*reft == *ipt)) int back = 0;
ipt++; const BYTE *vLimit = ip + (dictLimit - matchIndex);
_endcount:
reft = ref; if (vLimit > iHighLimit)
vLimit = iHighLimit;
while ((startt > startlimit)
&& (reft > hc4->base) mlt = LZ4_count(ip + MINMATCH,
&& (startt[-1] == reft[-1])) { matchPtr + MINMATCH, vLimit) + MINMATCH;
startt--;
reft--; if ((ip + mlt == vLimit) && (vLimit < iHighLimit))
} mlt += LZ4_count(ip + mlt, base + dictLimit,
iHighLimit);
if ((ipt - startt) > longest) { while ((ip + back > iLowLimit)
longest = (int)(ipt - startt); && (matchIndex + back > lowLimit)
*matchpos = reft; && (ip[back - 1] == matchPtr[back - 1]))
*startpos = startt; back--;
mlt -= back;
if ((int)mlt > longest) {
longest = (int)mlt;
*matchpos = base + matchIndex + back;
*startpos = ip + back;
} }
} }
} }
ref -= (size_t)chaintable[(size_t)(ref) & MAXD_MASK];
matchIndex -= DELTANEXTU16(matchIndex);
} }
return longest; return longest;
} }
static inline int lz4_encodesequence(const u8 **ip, u8 **op, const u8 **anchor, static FORCE_INLINE int LZ4HC_encodeSequence(
int ml, const u8 *ref) const BYTE **ip,
BYTE **op,
const BYTE **anchor,
int matchLength,
const BYTE * const match,
limitedOutput_directive limitedOutputBuffer,
BYTE *oend)
{ {
int length, len; int length;
u8 *token; BYTE *token;
/* Encode Literal length */ /* Encode Literal length */
length = (int)(*ip - *anchor); length = (int)(*ip - *anchor);
token = (*op)++; token = (*op)++;
if ((limitedOutputBuffer)
&& ((*op + (length>>8)
+ length + (2 + 1 + LASTLITERALS)) > oend)) {
/* Check output limit */
return 1;
}
if (length >= (int)RUN_MASK) { if (length >= (int)RUN_MASK) {
*token = (RUN_MASK << ML_BITS); int len;
*token = (RUN_MASK<<ML_BITS);
len = length - RUN_MASK; len = length - RUN_MASK;
for (; len > 254 ; len -= 255) for (; len > 254 ; len -= 255)
*(*op)++ = 255; *(*op)++ = 255;
*(*op)++ = (u8)len; *(*op)++ = (BYTE)len;
} else } else
*token = (length << ML_BITS); *token = (BYTE)(length<<ML_BITS);
/* Copy Literals */ /* Copy Literals */
LZ4_BLINDCOPY(*anchor, *op, length); LZ4_wildCopy(*op, *anchor, (*op) + length);
*op += length;
/* Encode Offset */ /* Encode Offset */
LZ4_WRITE_LITTLEENDIAN_16(*op, (u16)(*ip - ref)); LZ4_writeLE16(*op, (U16)(*ip - match));
*op += 2;
/* Encode MatchLength */ /* Encode MatchLength */
len = (int)(ml - MINMATCH); length = (int)(matchLength - MINMATCH);
if (len >= (int)ML_MASK) {
if ((limitedOutputBuffer)
&& (*op + (length>>8)
+ (1 + LASTLITERALS) > oend)) {
/* Check output limit */
return 1;
}
if (length >= (int)ML_MASK) {
*token += ML_MASK; *token += ML_MASK;
len -= ML_MASK; length -= ML_MASK;
for (; len > 509 ; len -= 510) {
for (; length > 509 ; length -= 510) {
*(*op)++ = 255; *(*op)++ = 255;
*(*op)++ = 255; *(*op)++ = 255;
} }
if (len > 254) {
len -= 255; if (length > 254) {
length -= 255;
*(*op)++ = 255; *(*op)++ = 255;
} }
*(*op)++ = (u8)len;
*(*op)++ = (BYTE)length;
} else } else
*token += len; *token += (BYTE)(length);
/* Prepare next loop */ /* Prepare next loop */
*ip += ml; *ip += matchLength;
*anchor = *ip; *anchor = *ip;
return 0; return 0;
} }
static int lz4_compresshcctx(struct lz4hc_data *ctx, static int LZ4HC_compress_generic(
const char *source, LZ4HC_CCtx_internal *const ctx,
char *dest, const char * const source,
int isize) char * const dest,
int const inputSize,
int const maxOutputSize,
int compressionLevel,
limitedOutput_directive limit
)
{ {
const u8 *ip = (const u8 *)source; const BYTE *ip = (const BYTE *) source;
const u8 *anchor = ip; const BYTE *anchor = ip;
const u8 *const iend = ip + isize; const BYTE * const iend = ip + inputSize;
const u8 *const mflimit = iend - MFLIMIT; const BYTE * const mflimit = iend - MFLIMIT;
const u8 *const matchlimit = (iend - LASTLITERALS); const BYTE * const matchlimit = (iend - LASTLITERALS);
u8 *op = (u8 *)dest; BYTE *op = (BYTE *) dest;
BYTE * const oend = op + maxOutputSize;
unsigned int maxNbAttempts;
int ml, ml2, ml3, ml0; int ml, ml2, ml3, ml0;
const u8 *ref = NULL; const BYTE *ref = NULL;
const u8 *start2 = NULL; const BYTE *start2 = NULL;
const u8 *ref2 = NULL; const BYTE *ref2 = NULL;
const u8 *start3 = NULL; const BYTE *start3 = NULL;
const u8 *ref3 = NULL; const BYTE *ref3 = NULL;
const u8 *start0; const BYTE *start0;
const u8 *ref0; const BYTE *ref0;
int lastrun;
/* init */
if (compressionLevel > LZ4HC_MAX_CLEVEL)
compressionLevel = LZ4HC_MAX_CLEVEL;
if (compressionLevel < 1)
compressionLevel = LZ4HC_DEFAULT_CLEVEL;
maxNbAttempts = 1 << (compressionLevel - 1);
ctx->end += inputSize;
ip++; ip++;
/* Main Loop */ /* Main Loop */
while (ip < mflimit) { while (ip < mflimit) {
ml = lz4hc_insertandfindbestmatch(ctx, ip, matchlimit, (&ref)); ml = LZ4HC_InsertAndFindBestMatch(ctx, ip,
matchlimit, (&ref), maxNbAttempts);
if (!ml) { if (!ml) {
ip++; ip++;
continue; continue;
...@@ -351,51 +387,59 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx, ...@@ -351,51 +387,59 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx,
start0 = ip; start0 = ip;
ref0 = ref; ref0 = ref;
ml0 = ml; ml0 = ml;
_search2:
if (ip+ml < mflimit) _Search2:
ml2 = lz4hc_insertandgetwidermatch(ctx, ip + ml - 2, if (ip + ml < mflimit)
ip + 1, matchlimit, ml, &ref2, &start2); ml2 = LZ4HC_InsertAndGetWiderMatch(ctx,
ip + ml - 2, ip + 0,
matchlimit, ml, &ref2,
&start2, maxNbAttempts);
else else
ml2 = ml; ml2 = ml;
/* No better match */
if (ml2 == ml) { if (ml2 == ml) {
lz4_encodesequence(&ip, &op, &anchor, ml, ref); /* No better match */
if (LZ4HC_encodeSequence(&ip, &op,
&anchor, ml, ref, limit, oend))
return 0;
continue; continue;
} }
if (start0 < ip) { if (start0 < ip) {
/* empirical */
if (start2 < ip + ml0) { if (start2 < ip + ml0) {
/* empirical */
ip = start0; ip = start0;
ref = ref0; ref = ref0;
ml = ml0; ml = ml0;
} }
} }
/*
* Here, start0==ip /* Here, start0 == ip */
* First Match too small : removed
*/
if ((start2 - ip) < 3) { if ((start2 - ip) < 3) {
/* First Match too small : removed */
ml = ml2; ml = ml2;
ip = start2; ip = start2;
ref = ref2; ref = ref2;
goto _search2; goto _Search2;
} }
_search3: _Search3:
/* /*
* Currently we have : * Currently we have :
* ml2 > ml1, and * ml2 > ml1, and
* ip1+3 <= ip2 (usually < ip1+ml1) * ip1 + 3 <= ip2 (usually < ip1 + ml1)
*/ */
if ((start2 - ip) < OPTIMAL_ML) { if ((start2 - ip) < OPTIMAL_ML) {
int correction; int correction;
int new_ml = ml; int new_ml = ml;
if (new_ml > OPTIMAL_ML) if (new_ml > OPTIMAL_ML)
new_ml = OPTIMAL_ML; new_ml = OPTIMAL_ML;
if (ip + new_ml > start2 + ml2 - MINMATCH) if (ip + new_ml > start2 + ml2 - MINMATCH)
new_ml = (int)(start2 - ip) + ml2 - MINMATCH; new_ml = (int)(start2 - ip) + ml2 - MINMATCH;
correction = new_ml - (int)(start2 - ip); correction = new_ml - (int)(start2 - ip);
if (correction > 0) { if (correction > 0) {
start2 += correction; start2 += correction;
ref2 += correction; ref2 += correction;
...@@ -403,39 +447,44 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx, ...@@ -403,39 +447,44 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx,
} }
} }
/* /*
* Now, we have start2 = ip+new_ml, * Now, we have start2 = ip + new_ml,
* with new_ml=min(ml, OPTIMAL_ML=18) * with new_ml = min(ml, OPTIMAL_ML = 18)
*/ */
if (start2 + ml2 < mflimit) if (start2 + ml2 < mflimit)
ml3 = lz4hc_insertandgetwidermatch(ctx, ml3 = LZ4HC_InsertAndGetWiderMatch(ctx,
start2 + ml2 - 3, start2, matchlimit, start2 + ml2 - 3, start2,
ml2, &ref3, &start3); matchlimit, ml2, &ref3, &start3,
maxNbAttempts);
else else
ml3 = ml2; ml3 = ml2;
/* No better match : 2 sequences to encode */
if (ml3 == ml2) { if (ml3 == ml2) {
/* No better match : 2 sequences to encode */
/* ip & ref are known; Now for ml */ /* ip & ref are known; Now for ml */
if (start2 < ip+ml) if (start2 < ip + ml)
ml = (int)(start2 - ip); ml = (int)(start2 - ip);
/* Now, encode 2 sequences */ /* Now, encode 2 sequences */
lz4_encodesequence(&ip, &op, &anchor, ml, ref); if (LZ4HC_encodeSequence(&ip, &op, &anchor,
ml, ref, limit, oend))
return 0;
ip = start2; ip = start2;
lz4_encodesequence(&ip, &op, &anchor, ml2, ref2); if (LZ4HC_encodeSequence(&ip, &op, &anchor,
ml2, ref2, limit, oend))
return 0;
continue; continue;
} }
/* Not enough space for match 2 : remove it */
if (start3 < ip + ml + 3) { if (start3 < ip + ml + 3) {
/* /* Not enough space for match 2 : remove it */
* can write Seq1 immediately ==> Seq2 is removed,
* so Seq3 becomes Seq1
*/
if (start3 >= (ip + ml)) { if (start3 >= (ip + ml)) {
/* can write Seq1 immediately
* ==> Seq2 is removed,
* so Seq3 becomes Seq1
*/
if (start2 < ip + ml) { if (start2 < ip + ml) {
int correction = int correction = (int)(ip + ml - start2);
(int)(ip + ml - start2);
start2 += correction; start2 += correction;
ref2 += correction; ref2 += correction;
ml2 -= correction; ml2 -= correction;
...@@ -446,35 +495,38 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx, ...@@ -446,35 +495,38 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx,
} }
} }
lz4_encodesequence(&ip, &op, &anchor, ml, ref); if (LZ4HC_encodeSequence(&ip, &op, &anchor,
ip = start3; ml, ref, limit, oend))
return 0;
ip = start3;
ref = ref3; ref = ref3;
ml = ml3; ml = ml3;
start0 = start2; start0 = start2;
ref0 = ref2; ref0 = ref2;
ml0 = ml2; ml0 = ml2;
goto _search2; goto _Search2;
} }
start2 = start3; start2 = start3;
ref2 = ref3; ref2 = ref3;
ml2 = ml3; ml2 = ml3;
goto _search3; goto _Search3;
} }
/* /*
* OK, now we have 3 ascending matches; let's write at least * OK, now we have 3 ascending matches;
* the first one ip & ref are known; Now for ml * let's write at least the first one
*/ * ip & ref are known; Now for ml
*/
if (start2 < ip + ml) { if (start2 < ip + ml) {
if ((start2 - ip) < (int)ML_MASK) { if ((start2 - ip) < (int)ML_MASK) {
int correction; int correction;
if (ml > OPTIMAL_ML) if (ml > OPTIMAL_ML)
ml = OPTIMAL_ML; ml = OPTIMAL_ML;
if (ip + ml > start2 + ml2 - MINMATCH) if (ip + ml > start2 + ml2 - MINMATCH)
ml = (int)(start2 - ip) + ml2 ml = (int)(start2 - ip) + ml2 - MINMATCH;
- MINMATCH;
correction = ml - (int)(start2 - ip); correction = ml - (int)(start2 - ip);
if (correction > 0) { if (correction > 0) {
start2 += correction; start2 += correction;
...@@ -484,7 +536,9 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx, ...@@ -484,7 +536,9 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx,
} else } else
ml = (int)(start2 - ip); ml = (int)(start2 - ip);
} }
lz4_encodesequence(&ip, &op, &anchor, ml, ref); if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml,
ref, limit, oend))
return 0;
ip = start2; ip = start2;
ref = ref2; ref = ref2;
...@@ -494,46 +548,245 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx, ...@@ -494,46 +548,245 @@ static int lz4_compresshcctx(struct lz4hc_data *ctx,
ref2 = ref3; ref2 = ref3;
ml2 = ml3; ml2 = ml3;
goto _search3; goto _Search3;
} }
/* Encode Last Literals */ /* Encode Last Literals */
lastrun = (int)(iend - anchor); {
if (lastrun >= (int)RUN_MASK) { int lastRun = (int)(iend - anchor);
*op++ = (RUN_MASK << ML_BITS);
lastrun -= RUN_MASK; if ((limit)
for (; lastrun > 254 ; lastrun -= 255) && (((char *)op - dest) + lastRun + 1
*op++ = 255; + ((lastRun + 255 - RUN_MASK)/255)
*op++ = (u8) lastrun; > (U32)maxOutputSize)) {
} else /* Check output limit */
*op++ = (lastrun << ML_BITS); return 0;
memcpy(op, anchor, iend - anchor); }
op += iend - anchor; if (lastRun >= (int)RUN_MASK) {
*op++ = (RUN_MASK<<ML_BITS);
lastRun -= RUN_MASK;
for (; lastRun > 254 ; lastRun -= 255)
*op++ = 255;
*op++ = (BYTE) lastRun;
} else
*op++ = (BYTE)(lastRun<<ML_BITS);
memcpy(op, anchor, iend - anchor);
op += iend - anchor;
}
/* End */ /* End */
return (int) (((char *)op) - dest); return (int) (((char *)op) - dest);
} }
int lz4hc_compress(const unsigned char *src, size_t src_len, static int LZ4_compress_HC_extStateHC(
unsigned char *dst, size_t *dst_len, void *wrkmem) void *state,
const char *src,
char *dst,
int srcSize,
int maxDstSize,
int compressionLevel)
{ {
int ret = -1; LZ4HC_CCtx_internal *ctx = &((LZ4_streamHC_t *)state)->internal_donotuse;
int out_len = 0;
struct lz4hc_data *hc4 = (struct lz4hc_data *)wrkmem; if (((size_t)(state)&(sizeof(void *) - 1)) != 0) {
lz4hc_init(hc4, (const u8 *)src); /* Error : state is not aligned
out_len = lz4_compresshcctx((struct lz4hc_data *)hc4, (const u8 *)src, * for pointers (32 or 64 bits)
(char *)dst, (int)src_len); */
return 0;
}
if (out_len < 0) LZ4HC_init(ctx, (const BYTE *)src);
goto exit;
*dst_len = out_len; if (maxDstSize < LZ4_compressBound(srcSize))
return 0; return LZ4HC_compress_generic(ctx, src, dst,
srcSize, maxDstSize, compressionLevel, limitedOutput);
else
return LZ4HC_compress_generic(ctx, src, dst,
srcSize, maxDstSize, compressionLevel, noLimit);
}
int LZ4_compress_HC(const char *src, char *dst, int srcSize,
int maxDstSize, int compressionLevel, void *wrkmem)
{
return LZ4_compress_HC_extStateHC(wrkmem, src, dst,
srcSize, maxDstSize, compressionLevel);
}
EXPORT_SYMBOL(LZ4_compress_HC);
/**************************************
* Streaming Functions
**************************************/
void LZ4_resetStreamHC(LZ4_streamHC_t *LZ4_streamHCPtr, int compressionLevel)
{
LZ4_streamHCPtr->internal_donotuse.base = NULL;
LZ4_streamHCPtr->internal_donotuse.compressionLevel = (unsigned int)compressionLevel;
}
int LZ4_loadDictHC(LZ4_streamHC_t *LZ4_streamHCPtr,
const char *dictionary,
int dictSize)
{
LZ4HC_CCtx_internal *ctxPtr = &LZ4_streamHCPtr->internal_donotuse;
if (dictSize > 64 * KB) {
dictionary += dictSize - 64 * KB;
dictSize = 64 * KB;
}
LZ4HC_init(ctxPtr, (const BYTE *)dictionary);
if (dictSize >= 4)
LZ4HC_Insert(ctxPtr, (const BYTE *)dictionary + (dictSize - 3));
ctxPtr->end = (const BYTE *)dictionary + dictSize;
return dictSize;
}
EXPORT_SYMBOL(LZ4_loadDictHC);
/* compression */
static void LZ4HC_setExternalDict(
LZ4HC_CCtx_internal *ctxPtr,
const BYTE *newBlock)
{
if (ctxPtr->end >= ctxPtr->base + 4) {
/* Referencing remaining dictionary content */
LZ4HC_Insert(ctxPtr, ctxPtr->end - 3);
}
/*
* Only one memory segment for extDict,
* so any previous extDict is lost at this stage
*/
ctxPtr->lowLimit = ctxPtr->dictLimit;
ctxPtr->dictLimit = (U32)(ctxPtr->end - ctxPtr->base);
ctxPtr->dictBase = ctxPtr->base;
ctxPtr->base = newBlock - ctxPtr->dictLimit;
ctxPtr->end = newBlock;
/* match referencing will resume from there */
ctxPtr->nextToUpdate = ctxPtr->dictLimit;
}
EXPORT_SYMBOL(LZ4HC_setExternalDict);
static int LZ4_compressHC_continue_generic(
LZ4_streamHC_t *LZ4_streamHCPtr,
const char *source,
char *dest,
int inputSize,
int maxOutputSize,
limitedOutput_directive limit)
{
LZ4HC_CCtx_internal *ctxPtr = &LZ4_streamHCPtr->internal_donotuse;
/* auto - init if forgotten */
if (ctxPtr->base == NULL)
LZ4HC_init(ctxPtr, (const BYTE *) source);
/* Check overflow */
if ((size_t)(ctxPtr->end - ctxPtr->base) > 2 * GB) {
size_t dictSize = (size_t)(ctxPtr->end - ctxPtr->base)
- ctxPtr->dictLimit;
if (dictSize > 64 * KB)
dictSize = 64 * KB;
LZ4_loadDictHC(LZ4_streamHCPtr,
(const char *)(ctxPtr->end) - dictSize, (int)dictSize);
}
/* Check if blocks follow each other */
if ((const BYTE *)source != ctxPtr->end)
LZ4HC_setExternalDict(ctxPtr, (const BYTE *)source);
/* Check overlapping input/dictionary space */
{
const BYTE *sourceEnd = (const BYTE *) source + inputSize;
const BYTE * const dictBegin = ctxPtr->dictBase + ctxPtr->lowLimit;
const BYTE * const dictEnd = ctxPtr->dictBase + ctxPtr->dictLimit;
if ((sourceEnd > dictBegin)
&& ((const BYTE *)source < dictEnd)) {
if (sourceEnd > dictEnd)
sourceEnd = dictEnd;
ctxPtr->lowLimit = (U32)(sourceEnd - ctxPtr->dictBase);
if (ctxPtr->dictLimit - ctxPtr->lowLimit < 4)
ctxPtr->lowLimit = ctxPtr->dictLimit;
}
}
return LZ4HC_compress_generic(ctxPtr, source, dest,
inputSize, maxOutputSize, ctxPtr->compressionLevel, limit);
}
int LZ4_compress_HC_continue(
LZ4_streamHC_t *LZ4_streamHCPtr,
const char *source,
char *dest,
int inputSize,
int maxOutputSize)
{
if (maxOutputSize < LZ4_compressBound(inputSize))
return LZ4_compressHC_continue_generic(LZ4_streamHCPtr,
source, dest, inputSize, maxOutputSize, limitedOutput);
else
return LZ4_compressHC_continue_generic(LZ4_streamHCPtr,
source, dest, inputSize, maxOutputSize, noLimit);
}
EXPORT_SYMBOL(LZ4_compress_HC_continue);
/* dictionary saving */
int LZ4_saveDictHC(
LZ4_streamHC_t *LZ4_streamHCPtr,
char *safeBuffer,
int dictSize)
{
LZ4HC_CCtx_internal *const streamPtr = &LZ4_streamHCPtr->internal_donotuse;
int const prefixSize = (int)(streamPtr->end
- (streamPtr->base + streamPtr->dictLimit));
if (dictSize > 64 * KB)
dictSize = 64 * KB;
if (dictSize < 4)
dictSize = 0;
if (dictSize > prefixSize)
dictSize = prefixSize;
memmove(safeBuffer, streamPtr->end - dictSize, dictSize);
exit: {
return ret; U32 const endIndex = (U32)(streamPtr->end - streamPtr->base);
streamPtr->end = (const BYTE *)safeBuffer + dictSize;
streamPtr->base = streamPtr->end - endIndex;
streamPtr->dictLimit = endIndex - dictSize;
streamPtr->lowLimit = endIndex - dictSize;
if (streamPtr->nextToUpdate < streamPtr->dictLimit)
streamPtr->nextToUpdate = streamPtr->dictLimit;
}
return dictSize;
}
EXPORT_SYMBOL(LZ4_saveDictHC);
/*-******************************
* For backwards compatibility
********************************/
int lz4hc_compress(const unsigned char *src, size_t src_len,
unsigned char *dst, size_t *dst_len, void *wrkmem)
{
*dst_len = LZ4_compress_HC(src, dst, src_len,
*dst_len, LZ4HC_DEFAULT_CLEVEL, wrkmem);
/*
* Prior lz4hc_compress will return -1 in case of error
* and 0 on success
* while new LZ4_compress_HC
* returns 0 in case of error
* and the output length on success
*/
if (!*dst_len)
return -1;
else
return 0;
} }
EXPORT_SYMBOL(lz4hc_compress); EXPORT_SYMBOL(lz4hc_compress);
MODULE_LICENSE("Dual BSD/GPL"); MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("LZ4HC compressor"); MODULE_DESCRIPTION("LZ4 HC compressor");
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment