45 #define DEBUG(...) fprintf(stderr, __VA_ARGS__) 62 return ((buffer[0] << 0) | (buffer[1] << 8) |
63 (buffer[2] << 16) | (buffer[3] << 24));
67 buffer[0] = val & 0xFF;
68 buffer[1] = (val >> 8) & 0xFF;
69 buffer[2] = (val >> 16) & 0xFF;
70 buffer[3] = (val >> 24) & 0xFF;
73 int min(
int a,
int b) {
117 char *dest = (
char*) dest_val;
119 memcpy(dest + *offset, str->
input_next, to_copy);
133 *(out+4) = ((
unsigned)version &
k2Bits) << 6;
144 DEBUG(
"writeHeader() Put 7 bytes in output\n");
176 DEBUG(
"bufferToOutput() Not enough space left in output\n");
180 DEBUG(
"bufferToOutput() Flushing %i bytes, %i left in output\n",
184 uint32_t comp_size = LZ4_compress_limitedOutput(state->
buffer,
187 (
int) uncomp_size - 1);
190 DEBUG(
"bufferToOutput() Compressed to %i bytes\n", comp_size);
192 wrote = 4 + comp_size;
196 DEBUG(
"bufferToOutput() Can't compress, copying input\n");
200 wrote = 4 + uncomp_size;
207 DEBUG(
"bufferToOutput() Ate %i from buffer, wrote %i to output (%i)\n",
230 DEBUG(
"inputToBuffer() Wrote % 5i bytes to buffer (size=% 5i)\n",
251 if (state->
buffer != NULL) {
279 if (!(4 <= block_size_id && block_size_id <= 7)) {
287 if (state->
buffer == NULL) {
296 if (state->
buffer != NULL) {
308 return (1 << (8 + (2 * block_id)));
313 if (ret < 0) {
return ret; }
328 if (ret < 0) {
return ret; }
333 int read = 0, wrote = 0;
336 if (read < 0) {
return read; }
341 if (wrote < 0) {
return wrote; }
343 }
while (read > 0 || wrote > 0);
380 unsigned char *header = (
unsigned char*) state->
header;
386 int version = (header[4] >> 6) &
k2Bits;
387 int block_independence_flag = (header[4] >> 5) &
k1Bits;
388 int block_checksum_flag = (header[4] >> 4) &
k1Bits;
389 int stream_size_flag = (header[4] >> 3) &
k1Bits;
390 int stream_checksum_flag = (header[4] >> 2) &
k1Bits;
391 int reserved1 = (header[4] >> 1) &
k1Bits;
392 int preset_dictionary_flag = (header[4] >> 0) &
k1Bits;
394 int reserved2 = (header[5] >> 7) &
k1Bits;
395 int block_max_id = (header[5] >> 4) &
k3Bits;
396 int reserved3 = (header[5] >> 0) &
k4Bits;
402 if (reserved1 != 0 || reserved2 != 0 || reserved3 != 0) {
405 if (!(4 <= block_max_id && block_max_id <= 7)) {
410 if (stream_size_flag != 0) {
413 if (preset_dictionary_flag != 0) {
416 if (block_independence_flag != 1) {
419 if (block_checksum_flag != 0) {
422 if (stream_checksum_flag != 1) {
426 int header_checksum = (
XXH32(header + 4, 2, 0) >> 8) &
k8Bits;
427 int stored_header_checksum = (header[6] >> 0) &
k8Bits;
428 if (header_checksum != stored_header_checksum) {
449 DEBUG(
"readBlockSize() Block size = %i uncompressed = %i\n",
472 DEBUG(
"readBlock() Read %i bytes from input (block = %i/%i)\n",
501 if (decomp_size < 0) {
559 else if (ret < 0) {
return ret; }
562 if (ret < 0) {
return ret; }
574 char *output,
unsigned int *output_size,
608 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
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)