int lz4_decompress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen, int level, uchar_t chdr, void *data) { int rv; struct lz4_params *lzdat = (struct lz4_params *)data; int _dstlen = *dstlen; if (lzdat->level == 1 || lzdat->level == 3) { rv = LZ4_uncompress((const char *)src, (char *)dst, _dstlen); if (rv != srclen) { return (-1); } } else if (lzdat->level == 2) { int sz1; sz1 = ntohl(*((int *)src)); rv = LZ4_uncompress((const char *)src + sizeof (int), (char *)dst, sz1); if (rv != srclen - sizeof (int)) { return (-1); } memcpy(src, dst, sz1); rv = LZ4_uncompress((const char *)src, (char *)dst, _dstlen); if (rv != sz1) { return (-1); } } return (0); }
void reconstruct_table_pass(ubyte *table, char color, int k, ubyte *v) { int i; FILE *F; char name[64]; sprintf(name, "%s.%c.%d", tablename, color, k); if (!(F = fopen(name, "rb"))) { printf("Could not open %s for writing.\n", name); exit(-1); } ubyte *ptr = table; long64 total = size; while (total > 0) { int chunk = COPYSIZE; if (total < chunk) chunk = total; total -= chunk; uint32 lz4size; fread(&lz4size, 1, 4, F); fread(lz4_buf, 1, lz4size, F); LZ4_uncompress(lz4_buf, (char *)copybuf, chunk); for (i = 0; i < chunk; i++) ptr[i] |= v[copybuf[i]]; ptr += chunk; } fclose(F); unlink(name); }
void load_table(ubyte *table, char color) { FILE *F; char name[64]; sprintf(name, "%s.%c", tablename, color); if (!(F = fopen(name, "rb"))) { printf("Could not open %s for writing.\n", name); exit(-1); } ubyte *ptr = table; long64 total = size; while (total > 0) { int chunk = COPYSIZE; if (total < chunk) chunk = total; total -= chunk; uint32 lz4size; fread(&lz4size, 1, 4, F); fread(lz4_buf, 1, lz4size, F); LZ4_uncompress(lz4_buf, (char *)ptr, chunk); ptr += chunk; } fclose(F); unlink(name); }
Termsave * termpty_save_extract(Termsave *ts) { if (!ts) return NULL; #if 0 if (ts->z) //TODO: unused { Termsavecomp *tsc = (Termsavecomp *)ts; Termsave *ts2; char *buf; int bytes; ts2 = _ts_new(sizeof(Termsave) + ((tsc->wout - 1) * sizeof(Termcell))); if (!ts2) return NULL; ts2->w = tsc->wout; buf = ((char *)tsc) + sizeof(Termsavecomp); bytes = LZ4_uncompress(buf, (char *)(&(ts2->cells[0])), tsc->wout * sizeof(Termcell)); if (bytes < 0) { memset(&(ts2->cells[0]), 0, tsc->wout * sizeof(Termcell)); // ERR("Decompress problem in row at byte %i", -bytes); } return ts2; } #endif return ts; }
void lz4Decompress(std::shared_ptr<uint8_t> src, std::shared_ptr<uint8_t>& dst, size_t uncompressedBytes) { if (uncompressedBytes > size_t(std::numeric_limits<int>::max())) throw std::runtime_error("Expected output data too big for LZ4 (max ~1.9GB)"); int const outputSize = static_cast<int>(uncompressedBytes); int readBytes = LZ4_uncompress((const char*)src.get(), (char*)dst.get(), outputSize); if (readBytes < 0) throw std::runtime_error(std::string("LZ4_uncompress failed: faulty input " "byte at position ") + SysTools::ToString(-readBytes)); }
static bool decompress( T2 &buffer_out, const T1 &buffer_in ) { static const bool verbose = false; size_t bytes_read = LZ4_uncompress( &buffer_in.at(0), &buffer_out.at(0), buffer_out.size() ); bool ret = ( bytes_read == buffer_in.size() ); if( verbose ) { // std::cout << moon9::echo( ret, decompressed_size, buffer_out.size() ); } return ret; }
static ZEND_FUNCTION(horde_lz4_uncompress) { zval *data; int data_len = 0; int header_offset = (sizeof(headerid) + sizeof(int)); int output_len; char *output, *p; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &data) == FAILURE) { RETURN_FALSE; } if (Z_TYPE_P(data) != IS_STRING) { zend_error(E_WARNING, "horde_lz4_uncompress: compressed data must be a string."); RETURN_FALSE; } p = Z_STRVAL_P(data); /* Check for header information. */ if (p[0] == headerid) { memcpy(&data_len, p + sizeof(headerid), sizeof(int)); } /* Header information not found. */ if (data_len <= 0) { RETURN_FALSE; } output = (char *)emalloc(data_len + 1); if (!output) { RETURN_FALSE; } output_len = LZ4_uncompress(p + header_offset, output, data_len); if (output_len <= 0) { RETVAL_FALSE; } else { RETVAL_STRINGL(output, data_len, 1); } efree(output); }
output_t& decompress(const compressed_data& in, output_t& out) { uint16_t byte_size = in.unpacked_len; // Make room for the unpacked data out.resize(byte_size / sizeof(typename output_t::value_type)); std::fill(out.begin(), out.end(), 0); if (byte_size > 0) { auto out_ptr = reinterpret_cast<char*>(&*out.begin()); int output_length = LZ4_uncompress(in.ptr(), out_ptr, byte_size); if (output_length < 0) throw std::runtime_error("lz4 decompression failed"); assert((size_t)output_length == in.size()); } return out; }
/* * Class: net_jpountz_lz4_LZ4 * Method: LZ4_decompress * Signature: ([BI[BII)I */ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1decompress (JNIEnv *env, jclass cls, jbyteArray src, jint srcOff, jbyteArray dest, jint destOff, jint destLen) { char* in = (char*) (*env)->GetPrimitiveArrayCritical(env, src, 0); if (in == NULL) { throw_OOM(env); return 0; } char* out = (char*) (*env)->GetPrimitiveArrayCritical(env, dest, 0); if (out == NULL) { throw_OOM(env); return 0; } jint compressed = LZ4_uncompress(in + srcOff, out + destOff, destLen); (*env)->ReleasePrimitiveArrayCritical(env, src, in, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, out, 0); return compressed; }
int decode_file(char* input_filename, char* output_filename) { U64 filesize = 0; char* in_buff; char* out_buff; char stdinout[] = "std"; FILE* finput; if (!strcmp (input_filename, stdinout)) { fprintf(stderr, "Using stdin for input\n"); finput = stdin; } else { finput = fopen( input_filename, "rb" ); } FILE* foutput; if (!strcmp (output_filename, stdinout)) { fprintf(stderr, "Using stdout for output\n"); foutput = stdout; } else { foutput = fopen( output_filename, "wb" ); } size_t uselessRet; int sinkint; U32 nextSize; if (finput==0 ) { printf("Pb opening %s\n", input_filename); return 4; } if (foutput==0) { printf("Pb opening %s\n", output_filename); return 5; } // Allocate Memory in_buff = malloc(OUT_CHUNKSIZE); out_buff = malloc(CHUNKSIZE); // Check Archive Header uselessRet = fread(out_buff, 1, ARCHIVE_MAGICNUMBER_SIZE, finput); if (*(U32*)out_buff != ARCHIVE_MAGICNUMBER) { printf("Wrong file : cannot be decoded\n"); return 6; } uselessRet = fread(in_buff, 1, 4, finput); nextSize = *(U32*)in_buff; // Main Loop while (1) { // Read Block uselessRet = fread(in_buff, 1, nextSize, finput); // Check Next Block uselessRet = (U32) fread(&nextSize, 1, 4, finput); if( uselessRet==0 ) break; // Decode Block sinkint = LZ4_uncompress(in_buff, out_buff, CHUNKSIZE); filesize += CHUNKSIZE; // Write Block fwrite(out_buff, 1, CHUNKSIZE, foutput); } // Last Block uselessRet = fread(in_buff, 1, nextSize, finput); sinkint = LZ4_uncompress_unknownOutputSize(in_buff, out_buff, nextSize, CHUNKSIZE); filesize += sinkint; fwrite(out_buff, 1, sinkint, foutput); // Status fprintf(stderr, "Successfully decoded %llu bytes \n", (unsigned long long)filesize); fclose(finput); fclose(foutput); return 0; }
static size_t lz4_filter(unsigned int flags, size_t cd_nelmts, const unsigned int cd_values[], size_t nbytes, size_t *buf_size, void **buf) { void * outBuf = NULL; size_t ret_value; if (flags & H5Z_FLAG_REVERSE) { const char* rpos = (char*)*buf; /* pointer to current read position */ const uint64_t * const i64Buf = (uint64_t *) rpos; const uint64_t origSize = (uint64_t)(be64toht(*i64Buf));/* is saved in be format */ rpos += 8; /* advance the pointer */ uint32_t *i32Buf = (uint32_t*)rpos; uint32_t blockSize = (uint32_t)(be32toht(*i32Buf)); rpos += 4; if(blockSize>origSize) blockSize = origSize; if (NULL==(outBuf = malloc(origSize))) { printf("cannot malloc\n"); goto error; } char *roBuf = (char*)outBuf; /* pointer to current write position */ uint64_t decompSize = 0; /// start with the first block /// while(decompSize < origSize) { if(origSize-decompSize < blockSize) /* the last block can be smaller than blockSize. */ blockSize = origSize-decompSize; i32Buf = (uint32_t*)rpos; uint32_t compressedBlockSize = be32toht(*i32Buf); /// is saved in be format rpos += 4; if(compressedBlockSize == blockSize) /* there was no compression */ { memcpy(roBuf, rpos, blockSize); } else /* do the decompression */ { int compressedBytes = LZ4_uncompress(rpos, roBuf, blockSize); if(compressedBytes != compressedBlockSize) { printf("decompressed size not the same: %d, != %d\n", compressedBytes, compressedBlockSize); goto error; } } rpos += compressedBlockSize; /* advance the read pointer to the next block */ roBuf += blockSize; /* advance the write pointer */ decompSize += blockSize; } free(*buf); *buf = outBuf; outBuf = NULL; ret_value = (size_t)origSize; // should always work, as orig_size cannot be > 2GB (sizeof(size_t) < 4GB) } else /* forward filter */ { if (nbytes > INT32_MAX) { /* can only compress chunks up to 2GB */ goto error; } size_t blockSize; if(cd_nelmts > 0 && cd_values[0] > 0) { blockSize = cd_values[0]; } else { blockSize = DEFAULT_BLOCK_SIZE; } if(blockSize > nbytes) { blockSize = nbytes; } size_t nBlocks = (nbytes-1)/blockSize +1 ; if (NULL==(outBuf = malloc(LZ4_COMPRESSBOUND(nbytes) + 4+8 + nBlocks*4))) { goto error; } char *rpos = (char*)*buf; /* pointer to current read position */ char *roBuf = (char*)outBuf; /* pointer to current write position */ /* header */ uint64_t * i64Buf = (uint64_t *) (roBuf); i64Buf[0] = htobe64t((uint64_t)nbytes); /* Store decompressed size in be format */ roBuf += 8; uint32_t *i32Buf = (uint32_t *) (roBuf); i32Buf[0] = htobe32t((uint32_t)blockSize); /* Store the block size in be format */ roBuf += 4; size_t outSize = 12; /* size of the output buffer. Header size (12 bytes) is included */ for(size_t block = 0; block < nBlocks; ++block) { size_t origWritten = block*blockSize; if(nbytes - origWritten < blockSize) /* the last block may be < blockSize */ blockSize = nbytes - origWritten; uint32_t compBlockSize = LZ4_compress(rpos, roBuf+4, blockSize); /// reserve space for compBlockSize if(!compBlockSize) goto error; if(compBlockSize >= blockSize) /* compression did not save any space, do a memcpy instead */ { compBlockSize = blockSize; memcpy(roBuf+4, rpos, blockSize); } i32Buf = (uint32_t *) (roBuf); i32Buf[0] = htobe32t((uint32_t)compBlockSize); /* write blocksize */ roBuf += 4; rpos += blockSize; /* advance read pointer */ roBuf += compBlockSize; /* advance write pointer */ outSize += compBlockSize + 4; } free(*buf); *buf = outBuf; *buf_size = outSize; outBuf = NULL; ret_value = outSize; } done: if(outBuf) free(outBuf); return ret_value; error: if(outBuf) free(outBuf); outBuf = NULL; return 0; }
extern __declspec(dllexport) int dll_LZ4_uncompress (const char* source, char* dest, int osize) { return LZ4_uncompress (source, dest, osize); }
bool Decompress( void * to, size_t to_size ) { return LZ4_uncompress( (const char *)buffer, (char*)to, to_size) >= 0; }