45 #define DEBUG(...) fprintf(stderr, __VA_ARGS__) 53 #define LZ4_VERSION (LZ4_VERSION_MAJOR * 100 * 100 + LZ4_VERSION_MINOR * 100 + LZ4_VERSION_RELEASE) 54 #if LZ4_VERSION >= 10701 55 #define LZ4_COMPRESS_DEFAULT LZ4_compress_default 57 #define LZ4_COMPRESS_DEFAULT LZ4_compress_limitedOutput 72 return ((buffer[0] << 0) | (buffer[1] << 8) |
73 (buffer[2] << 16) | (buffer[3] << 24));
77 buffer[0] = val & 0xFF;
78 buffer[1] = (val >> 8) & 0xFF;
79 buffer[2] = (val >> 16) & 0xFF;
80 buffer[3] = (val >> 24) & 0xFF;
83 int min(
int a,
int b) {
127 char *dest = (
char*) dest_val;
129 memcpy(dest + *offset, str->
input_next, to_copy);
143 *(out+4) = ((
unsigned)version &
k2Bits) << 6;
154 DEBUG(
"writeHeader() Put 7 bytes in output\n");
186 DEBUG(
"bufferToOutput() Not enough space left in output\n");
190 DEBUG(
"bufferToOutput() Flushing %i bytes, %i left in output\n",
197 (
int) uncomp_size - 1);
200 DEBUG(
"bufferToOutput() Compressed to %i bytes\n", comp_size);
202 wrote = 4 + comp_size;
206 DEBUG(
"bufferToOutput() Can't compress, copying input\n");
210 wrote = 4 + uncomp_size;
217 DEBUG(
"bufferToOutput() Ate %i from buffer, wrote %i to output (%i)\n",
240 DEBUG(
"inputToBuffer() Wrote % 5i bytes to buffer (size=% 5i)\n",
281 if (!(4 <= block_size_id && block_size_id <= 7)) {
289 if (state->
buffer == NULL) {
298 if (state->
buffer != NULL) {
310 return (1 << (8 + (2 * block_id)));
315 if (ret < 0) {
return ret; }
330 if (ret < 0) {
return ret; }
335 int read = 0, wrote = 0;
338 if (read < 0) {
return read; }
343 if (wrote < 0) {
return wrote; }
345 }
while (read > 0 || wrote > 0);
382 unsigned char *header = (
unsigned char*) state->
header;
388 int version = (header[4] >> 6) &
k2Bits;
389 int block_independence_flag = (header[4] >> 5) &
k1Bits;
390 int block_checksum_flag = (header[4] >> 4) &
k1Bits;
391 int stream_size_flag = (header[4] >> 3) &
k1Bits;
392 int stream_checksum_flag = (header[4] >> 2) &
k1Bits;
393 int reserved1 = (header[4] >> 1) &
k1Bits;
394 int preset_dictionary_flag = (header[4] >> 0) &
k1Bits;
396 int reserved2 = (header[5] >> 7) &
k1Bits;
397 int block_max_id = (header[5] >> 4) &
k3Bits;
398 int reserved3 = (header[5] >> 0) &
k4Bits;
404 if (reserved1 != 0 || reserved2 != 0 || reserved3 != 0) {
407 if (!(4 <= block_max_id && block_max_id <= 7)) {
412 if (stream_size_flag != 0) {
415 if (preset_dictionary_flag != 0) {
418 if (block_independence_flag != 1) {
421 if (block_checksum_flag != 0) {
424 if (stream_checksum_flag != 1) {
428 int header_checksum = (
XXH32(header + 4, 2, 0) >> 8) &
k8Bits;
429 int stored_header_checksum = (header[6] >> 0) &
k8Bits;
430 if (header_checksum != stored_header_checksum) {
451 DEBUG(
"readBlockSize() Block size = %i uncompressed = %i\n",
474 DEBUG(
"readBlock() Read %i bytes from input (block = %i/%i)\n",
503 if (decomp_size < 0) {
561 else if (ret < 0) {
return ret; }
564 if (ret < 0) {
return ret; }
576 char *output,
unsigned int *output_size,
610 char *output,
unsigned int *output_size) {
const int ROSLZ4_MEMORY_ERROR
U32 XXH32(const void *input, int len, U32 seed)
U32 XXH32_digest(void *state_in)
int writeEOS(roslz4_stream *str)
void advanceOutput(roslz4_stream *str, int nbytes)
void * XXH32_init(U32 seed)
void advanceInput(roslz4_stream *str, int nbytes)
int writeHeader(roslz4_stream *str)
uint32_t readUInt32(unsigned char *buffer)
const int ROSLZ4_OUTPUT_SMALL
void roslz4_decompressEnd(roslz4_stream *str)
int block_independence_flag
void streamStateFree(roslz4_stream *str)
int roslz4_compress(roslz4_stream *str, int action)
void roslz4_compressEnd(roslz4_stream *str)
int processHeader(roslz4_stream *str)
const int ROSLZ4_STREAM_END
int roslz4_buffToBuffCompress(char *input, unsigned int input_size, char *output, unsigned int *output_size, int block_size_id)
int readBlockSize(roslz4_stream *str)
int streamStateAlloc(roslz4_stream *str)
void fillUInt32(roslz4_stream *str, uint32_t *dest_val, int *offset)
const int ROSLZ4_PARAM_ERROR
int readBlock(roslz4_stream *str)
int roslz4_decompressStart(roslz4_stream *str)
int bufferToOutput(roslz4_stream *str)
void writeUInt32(unsigned char *buffer, uint32_t val)
const int ROSLZ4_DATA_ERROR
#define LZ4_COMPRESS_DEFAULT
XXH_errorcode XXH32_update(void *state_in, const void *input, int len)
const uint32_t kEndOfStream
int decompressBlock(roslz4_stream *str)
const uint32_t kMagicNumber
int readChecksum(roslz4_stream *str)
int streamResizeBuffer(roslz4_stream *str, int block_size_id)
int inputToBuffer(roslz4_stream *str)
int roslz4_decompress(roslz4_stream *str)
int roslz4_compressStart(roslz4_stream *str, int block_size_id)
int roslz4_blockSizeFromIndex(int block_id)
int roslz4_buffToBuffDecompress(char *input, unsigned int input_size, char *output, unsigned int *output_size)