18#include <FreeNOS/User.h>
25 : m_inputData(static_cast<const
u8 *>(data))
28 , m_blockChecksums(false)
29 , m_contentChecksum(false)
31 , m_blockMaximumSize(0)
69 ERROR(
"inter-dependent blocks not supported");
86 ERROR(
"content size must not be zero");
117 ERROR(
"invalid maximum block size value: " << (bd >> 4));
131 const Size size)
const
135 u8 *output =
static_cast<u8 *
>(buffer);
138 while (copied < size && input < inputEnd)
142 const u32 blockSize = blockSizeByte & ~(1 << 31);
143 const bool isCompressed = blockSizeByte & (1 << 31) ?
false :
true;
152 input +=
sizeof(
u32);
157 uncompSize =
decompress(input, blockSize, output, size - copied);
163 uncompSize = blockSize;
167 copied += uncompSize;
168 output += uncompSize;
172 input +=
sizeof(
u32);
181 Size &byteCount)
const
190 for (byteCount = 1; ; byteCount++)
192 const u8 byte = *next++;
205 const Size inputSize,
207 const Size outputSize)
const
209 const u8 *inputEnd = input + inputSize;
210 Size outputOffset = 0;
213 while (input < inputEnd && outputOffset < outputSize)
215 u32 literalBytes = 0;
219 const u8 token = *input;
224 input += literalBytes;
225 DEBUG(
"token = " << token <<
" literalsCount = " << literalsCount <<
" literalBytes = " << literalBytes);
228 if (literalsCount > 0)
231 input += literalsCount;
232 outputOffset += literalsCount;
236 if (input >= inputEnd)
243 assert(off <= outputOffset);
244 const u32 matchOffset = outputOffset - off;
245 input +=
sizeof(
u16);
252 DEBUG(
"matchOffset = " << matchOffset <<
" matchCount = " << matchCount);
254 outputOffset += matchCount;
Result initialize()
Initialize the decompressor.
u64 m_contentSize
Content size as specified in the frame header.
const Size m_inputSize
Total size in bytes of the compressed input data.
const u32 integerDecode(const u32 initial, const u8 *next, Size &byteCount) const
Decode input data as integer (little-endian, 32-bit unsigned)
Size m_blockMaximumSize
Maximum block size in bytes of the uncompressed content.
u64 getUncompressedSize() const
Get size of the uncompressed data.
const u8 * m_inputData
Compressed input data.
Size m_frameDescSize
Size of the frame descriptor in bytes.
bool m_blockChecksums
True if blocks have checksums enabled.
static const u32 EndMark
EndMark marks the end of the data stream.
bool m_contentChecksum
True if the input data buffer contains content checksum.
Lz4Decompressor(const void *data, const Size size)
Constructor function.
const u32 decompress(const u8 *input, const Size inputSize, u8 *output, const Size outputSize) const
Decompress a block of compressed data.
static const u32 FrameMagic
Magic number value marks the start of the frame header.
Result read(void *buffer, const Size size) const
Reads compressed data.
static const u8 FrameVersion
Current supported version of the LZ4 algorithm.
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
#define assert(exp)
Insert program diagnostics.
unsigned int u32
Unsigned 32-bit number.
#define MegaByte(v)
Convert megabytes to bytes.
#define ERROR(msg)
Output an error message.
const u16 readLe16(const void *data)
Read 16-bit little endian integer.
const u64 readLe64(const void *data)
Read 64-bit little endian integer.
unsigned short u16
Unsigned 16-bit number.
#define KiloByte(v)
Convert kilobytes to bytes.
unsigned int Size
Any sane size indicator cannot go negative.
unsigned long long u64
Unsigned 64-bit number.
const u32 readLe32(const void *data)
Read 32-bit little endian integer.
#define DEBUG(msg)
Output a debug message to standard output.
unsigned char u8
Unsigned 8-bit number.