Пример #1
0
void Packet::setPacket(ENetPacket* packet)
{
	MemoryStream pms;
	pms.setRawBuffer(packet->data);

	uint64 id;

	if( !DecodeVariableInteger(&pms, id) )
		return;

	this->id = (PacketId) id;
	this->packet = packet;

	pms.readBuffer(&flags, sizeof(PacketFlags));

	uint8* in = packet->data + pms.position;
	size_t inSize = packet->dataLength - (size_t) pms.position;

	if( GetBitFlag(flags, PacketFlags::Compressed) )
	{
		size_t bufSize = GetCompressionBufferSize(packet->dataLength);
		ms.resize(bufSize);

		int32 length = fastlz_decompress(in, inSize, ms.data.Buffer(), ms.data.Size());
		ms.resize(length);
	}
	else
	{
		ms.resize(inSize);
		ms.write(in, inSize);
	}

	freePacket = true;
}
Пример #2
0
void Message::setPacket(ENetPacket* packet)
{
	MemoryStream pms;
	StreamMemoryInit(&pms);
	StreamMemorySetRawBuffer(&pms, packet->data);

	uint64 id;

	if( !DecodeVariableInteger(&pms, id) )
		return;

	this->id = (MessageId) id;
	this->packet = packet;

	StreamReadBuffer(&pms, &flags, sizeof(MessageFlags::Enum));

	uint8* in = packet->data + pms.position;
	size_t inSize = packet->dataLength - (size_t) pms.position;

	if( GetBitFlag(flags, MessageFlags::Compressed) )
	{
		size_t bufSize = GetCompressionBufferSize(packet->dataLength);
		StreamResize(ms, bufSize);

		int32 length = fastlz_decompress(in, inSize, &ms->data[0], ms->data.size());
		StreamResize(ms, length);
	}
	else
	{
		StreamResize(ms, inSize);
		StreamWrite(ms, in, inSize);
	}

	freePacket = true;
}
Пример #3
0
// our input method (STL string)
void CFastLZIO::Read(string& s, FILE* stm) {

	// read the buffer length
	unsigned int bufferLen;
	fread((char*)&bufferLen, SIZEOF_INT, 1, stm);

	// handle an empty packet
	if(bufferLen == 0) {
		s.clear();
		return;
	}

	// calculate the number of blocks
	const unsigned short numBlocksRead = (unsigned short)ceil(bufferLen / (double)FASTLZ_IO_OUTPUT_BUFFER_LEN);

	// resize the string
	s.resize(bufferLen);

	// read each block
	char* pBuffer = (char*)s.data();
	int numCompressedBytes;

	for(unsigned int i = 0; i < numBlocksRead; ++i) {
		fread((char*)&numCompressedBytes, SIZEOF_INT, 1, stm);
		fread(mBuffer, numCompressedBytes, 1, stm);
		int numUncompressedBytes = fastlz_decompress(mBuffer, numCompressedBytes, (void*)pBuffer, FASTLZ_IO_BUFFER_LEN);
		pBuffer += numUncompressedBytes;
	}
}
Пример #4
0
static SquashStatus
squash_fastlz_decompress_buffer (SquashCodec* codec,
                                 size_t* decompressed_size,
                                 uint8_t decompressed[HEDLEY_ARRAY_PARAM(*decompressed_size)],
                                 size_t compressed_size,
                                 const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)],
                                 SquashOptions* options) {
#if INT_MAX < SIZE_MAX
  if (HEDLEY_UNLIKELY(INT_MAX < compressed_size) ||
      HEDLEY_UNLIKELY(INT_MAX < *decompressed_size))
    return squash_error (SQUASH_RANGE);
#endif

  int fastlz_e = fastlz_decompress ((const void*) compressed,
                                    (int) compressed_size,
                                    (void*) decompressed,
                                    (int) *decompressed_size);

  if (HEDLEY_UNLIKELY(fastlz_e < 0)) {
    return squash_error (SQUASH_FAILED);
  } else if (HEDLEY_UNLIKELY(fastlz_e == 0)) {
    return SQUASH_BUFFER_FULL;
  } else {
#if SIZE_MAX < INT_MAX
    if (HEDLEY_UNLIKELY(SIZE_MAX < fastlz_e))
      return squash_error (SQUASH_RANGE);
#endif
    *decompressed_size = (size_t) fastlz_e;
    return SQUASH_OK;
  }
}
Пример #5
0
void Message::uncompress() {
	if (!isCompressed())
		return;

#ifdef BUILD_WITH_COMPRESSION_MINIZ
	int cmp_status;
	mz_ulong actualSize = strTo<size_t>(_meta["um.compressed"]);
	uint8_t *pUncmp;

	pUncmp = (mz_uint8 *)malloc((size_t)actualSize);
	cmp_status = mz_uncompress(pUncmp, &actualSize, (const unsigned char *)_data.get(), _size);

	_size = actualSize;
	_data = SharedPtr<char>((char*)pUncmp);
	_meta.erase("um.compressed");

#elif defined(BUILD_WITH_COMPRESSION_FASTLZ)

	int actualSize = strTo<size_t>(_meta["um.compressed"]);
	void* uncompressed = malloc((size_t)actualSize);

	// returns the size of the decompressed block.
	actualSize = fastlz_decompress(_data.get(), _size, uncompressed, actualSize);

	// If error occurs, e.g. the compressed data is corrupted or the output buffer is not large enough, then 0

	_size = actualSize;
	_data = SharedPtr<char>((char*)uncompressed);
	_meta.erase("um.compressed");

#endif

}
Пример #6
0
int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode){

	switch(p_mode) {
		case MODE_FASTLZ: {

			int ret_size=0;

			if (p_dst_max_size<16) {
				uint8_t dst[16];
				ret_size = fastlz_decompress(p_src,p_src_size,dst,16);
				copymem(p_dst,dst,p_dst_max_size);
			} else {
				ret_size = fastlz_decompress(p_src,p_src_size,p_dst,p_dst_max_size);
			}
			return ret_size;
		} break;
		case MODE_DEFLATE: {

			z_stream strm;
			strm.zalloc = zipio_alloc;
			strm.zfree = zipio_free;
			strm.opaque = Z_NULL;
			strm.avail_in= 0;
			strm.next_in=Z_NULL;
			int err = inflateInit(&strm);
			ERR_FAIL_COND_V(err!=Z_OK,-1);

			strm.avail_in=p_src_size;
			strm.avail_out=p_dst_max_size;
			strm.next_in=(Bytef*)p_src;
			strm.next_out=p_dst;

			err = inflate(&strm,Z_FINISH);
			int total = strm.total_out;
			inflateEnd(&strm);
			ERR_FAIL_COND_V(err!=Z_STREAM_END,-1);
			return total;
		} break;
	}

	ERR_FAIL_V(-1);
}
Пример #7
0
size_t fa_decompress_block(fa_compression_t compression, void* out, size_t outSize, const void* in, size_t inSize)
{
	switch (compression)
	{
		default: return 0;

		case FA_COMPRESSION_FASTLZ:
		{
			return fastlz_decompress(in, inSize, out, outSize);
		}
		break;
#if defined(FA_ZLIB_ENABLE)
		case FA_COMPRESSION_DEFLATE:
		{
			uLongf destLen = outSize;

			if (uncompress(out, &destLen, in, inSize) != Z_OK)
			{
				return 0;
			}

			return destLen;
		}
		break;
#endif

#if defined(FA_LZMA_ENABLE) 
		case FA_COMPRESSION_LZMA2:
		{
			size_t inPos = 0, outPos = 0;
			lzma_filter filters[2];
			lzma_options_lzma options;

			if (lzma_lzma_preset(&options, 6))
			{
				return 0;
			}

			filters[0].id = LZMA_FILTER_LZMA2;
			filters[0].options = &options;
			filters[1].id = LZMA_VLI_UNKNOWN;

			lzma_ret ret = lzma_raw_buffer_decode(filters, NULL, in, &inPos, inSize, out, &outPos, outSize);
			if (ret != LZMA_OK)
			{
				return 0;
			}

			return outPos; 
		}
		break;
#endif
	}
}
Пример #8
0
        static bool decompress( T2 &buffer_out, const T1 &buffer_in )
        {
            static const bool verbose = false;

            size_t decompressed_size = fastlz_decompress( &buffer_in.at(0), buffer_in.size(), &buffer_out.at(0), buffer_out.size() );

            bool ret = ( decompressed_size == buffer_out.size() );

            if( verbose )
            {
//                std::cout << moon9::echo( ret, decompressed_size, buffer_out.size() );
            }

            return ret;
        }
Пример #9
0
PHP_COUCHBASE_LOCAL
int php_couchbase_decompress_fastlz(php_couchbase_decomp *info)
{
	if (info->expanded_len == 0 || info->expanded_len > CBCOMPHDR_SANITY_MAX) {
		return 0;
	}

	info->expanded = emalloc(info->expanded_len);
	if (!info->expanded) {
		return 0;
	}

	info->expanded_len = fastlz_decompress(info->compressed,
	                                       info->compressed_len, info->expanded, info->expanded_len);

	return info->expanded_len;
}
Пример #10
0
// our input method
void CFastLZIO::Read(char* &buffer, unsigned int& bufferLen, FILE* stm) {

	// read the buffer length
	unsigned int newBufferLen;
	fread((char*)&newBufferLen, SIZEOF_INT, 1, stm);

	// allocate memory if necessary
	if(newBufferLen > bufferLen) {
		try {
			bufferLen = newBufferLen;
			if(buffer) delete [] buffer;
			buffer = new char[bufferLen + 1];
		} catch(bad_alloc) {
			printf("ERROR: Unable to initialize the uncompressed FastLZ buffer (Read).\n");
			exit(1);
		}
	}

	// calculate the number of blocks
	const unsigned short numBlocksRead = (unsigned short)ceil(bufferLen / (double)FASTLZ_IO_OUTPUT_BUFFER_LEN);

	// read each block
	char* pBuffer = buffer;
	int numCompressedBytes;

	for(unsigned int i = 0; i < numBlocksRead; ++i) {

		// read the block length
		fread((char*)&numCompressedBytes, SIZEOF_INT, 1, stm);

		// read the compressed block
		fread(mBuffer, numCompressedBytes, 1, stm);

		// uncompress the block
		int numUncompressedBytes = fastlz_decompress(mBuffer, numCompressedBytes, (void*)pBuffer, FASTLZ_IO_BUFFER_LEN);
		pBuffer += numUncompressedBytes;
	}

	// add the null termination
	*pBuffer = 0;
}
Пример #11
0
static gfximage_t readImage(reader_t*r, state_t*state)
{
    gfximage_t img;
    img.width = reader_readU16(r);
    img.height = reader_readU16(r);
    uLongf size = img.width*img.height*sizeof(gfxcolor_t);
    img.data = malloc(size);
#ifdef COMPRESS_IMAGES
    uLongf compressdata_size = reader_readU32(r);
    void*compressdata = malloc(compressdata_size);
# ifdef FILTER_IMAGES
    unsigned char*filter = malloc(img.height);
    r->read(r, filter, img.height);
# endif
    r->read(r, compressdata, compressdata_size);
   
# ifdef HAVE_FASTLZ
    fastlz_decompress(compressdata, compressdata_size, (void*)img.data, size);
# else
    uncompress((void*)img.data, &size, compressdata, compressdata_size);
# endif
    free(compressdata);

# ifdef FILTER_IMAGES
    int y;
    unsigned char*line = malloc(img.width*sizeof(gfxcolor_t));
    for(y=0;y<img.height;y++) {
	png_inverse_filter_32(filter[y], (void*)&img.data[y*img.width], 
			      y?(void*)&img.data[(y-1)*img.width]:(void*)0, 
			      line, img.width);
	memcpy(&img.data[y*img.width], line, img.width*sizeof(gfxcolor_t));
    }
    free(line);
    free(filter);
# endif

#else
    r->read(r, img.data, size);
#endif
    return img;
}
Пример #12
0
static SquashStatus
squash_fastlz_decompress_buffer (SquashCodec* codec,
                                 size_t* decompressed_length,
                                 uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_length)],
                                 size_t compressed_length,
                                 const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_length)],
                                 SquashOptions* options) {
  int fastlz_e = fastlz_decompress ((const void*) compressed,
                                    (int) compressed_length,
                                    (void*) decompressed,
                                    (int) *decompressed_length);

  if (fastlz_e < 0) {
    return squash_error (SQUASH_FAILED);
  } else if (fastlz_e == 0) {
    return SQUASH_BUFFER_FULL;
  } else {
    *decompressed_length = (size_t) fastlz_e;
    return SQUASH_OK;
  }
}
Пример #13
0
	// reads a new compressed partition (returns false if EOF occurs)
	bool CAlignmentReader::ReadPartition(void) {

		// read the uncompressed partition entry size
		unsigned int uncompressedSize = 0;
		fread((char*)&uncompressedSize, SIZEOF_INT, 1, mInStream);

		if(feof(mInStream)) return false;

		// read the compressed partition entry size
		int compressedSize = 0;
		fread((char*)&compressedSize, SIZEOF_INT, 1, mInStream);

		// read the partition member size
		mPartitionMembers = 0;
		fread((char*)&mPartitionSize, SIZEOF_SHORT, 1, mInStream);

		// check the compression buffer size
		CMemoryUtilities::CheckBufferSize(mCompressionBuffer, mCompressionBufferLen, compressedSize);
		CMemoryUtilities::CheckBufferSize(mBuffer, mBufferLen, uncompressedSize);

		// read and uncompress the partition
		int numBytesRead = (int)fread(mCompressionBuffer, 1, compressedSize, mInStream);

		if(numBytesRead != compressedSize) {
			cout << "ERROR: Tried to read " << compressedSize << " bytes, but received only " << numBytesRead << " bytes (" << mInputFilename << ") [read the partition: LoadNextRead]" << endl;
			exit(1);
		}

		int result = fastlz_decompress(mCompressionBuffer, compressedSize, mBuffer, mBufferLen);

		if(result == 0) {
			cout << "ERROR: Unable to properly uncompress the current data partition." << endl;
			exit(1);
		}

		// set the buffer pointer
		mBufferPtr = mBuffer;

		return true;
	}
Пример #14
0
int hcMiscSeqGetFile( u8* buffer, int bufsize, char* filename, u8* destbuffer )
{
  int o;
  int l;
  u8* h;
  int lz;
  int r;

  hcMiscSeqFindFile( buffer, bufsize, filename, &o, &l );
  h = (u8*)(buffer+o-128);

  lz = h[4];

  if( lz == 1 )
  {
    r = fastlz_decompress(buffer+o, l, destbuffer, 5000000);
    l = r;
  } else {
    memcpy( destbuffer, buffer+o, l );
  }

  return l;
}
Пример #15
0
int64_t lzbench_fastlz_decompress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t, size_t, char*)
{
	return fastlz_decompress(inbuf, insize, outbuf, outsize);
}
Пример #16
0
	int MPK_Decompress(byte * uncomp, int max_len, const byte * comp, int c_len)
	{
		return fastlz_decompress((const char *)comp, c_len, uncomp, max_len);
	}
 virtual dtStatus decompress(const unsigned char* compressed, const int compressedSize,
                             unsigned char* buffer, const int maxBufferSize, int* bufferSize)
 {
     *bufferSize = fastlz_decompress(compressed, compressedSize, buffer, maxBufferSize);
     return *bufferSize < 0 ? DT_FAILURE : DT_SUCCESS;
 }
Пример #18
0
void Message::uncompress() {
	if (!isCompressed())
		return;

	std::string errorStr = "Unsupported compression";
	size_t actualSize;
	std::string comprType;

	std::string comprIdent = _meta["um.compressed"];
	size_t colon = comprIdent.find_first_of(':');
	if (colon == std::string::npos) {
		errorStr = "No colon found in um.compressed meta field";
		goto DECOMPRESS_ERROR;
	}

	actualSize = strTo<size_t>(comprIdent.substr(0, colon));
	comprType = comprIdent.substr(colon + 1);

	_meta["um.compressRatio"] = toStr((double)_size / (double)actualSize);
//    std::cout << _size << " vs " << actualSize << std::endl;

	if (false) {}
#ifdef BUILD_WITH_COMPRESSION_MINIZ
	else if (comprType == "miniz") {
		int cmp_status;
		uint8_t *pUncmp;

		pUncmp = (mz_uint8 *)malloc((size_t)actualSize);
		cmp_status = mz_uncompress(pUncmp, (mz_ulong*)&actualSize, (const unsigned char *)_data.get(), _size);

		if (cmp_status != MZ_OK) {
			errorStr = mz_error(cmp_status);
			goto DECOMPRESS_ERROR;
		}

		_size = actualSize;
		_data = SharedPtr<char>((char*)pUncmp);
		_meta.erase("um.compressed");
		return;
	}
#endif
#ifdef BUILD_WITH_COMPRESSION_FASTLZ
	else if (comprType == "fastlz") {
		void* uncompressed = malloc((size_t)actualSize);

		// returns the size of the decompressed block.
		actualSize = fastlz_decompress(_data.get(), _size, uncompressed, actualSize);

		// If error occurs, e.g. the compressed data is corrupted or the output buffer is not large enough, then 0
		if (actualSize == 0) {
			errorStr = "fastlz_decompress returned 0";
			goto DECOMPRESS_ERROR;
		}

		_size = actualSize;
		_data = SharedPtr<char>((char*)uncompressed);
		_meta.erase("um.compressed");
		return;
	}

#endif
#ifdef BUILD_WITH_COMPRESSION_LZ4
	else if (comprType == "lz4") {
#ifdef LZ4_FRAME
		LZ4F_errorCode_t n;
		LZ4F_decompressionContext_t ctx;

		void* uncompressed = malloc((size_t)actualSize);

		n = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
		if (LZ4F_isError(n)) {
			errorStr = LZ4F_getErrorName(n);
			goto DECOMPRESS_ERROR;
		}

		n = LZ4F_decompress(ctx, uncompressed, &actualSize, _data.get(), &_size, NULL);
		if (LZ4F_isError(n)) {
			errorStr = LZ4F_getErrorName(n);
			goto DECOMPRESS_ERROR;
		}

		_size = actualSize;
		_data = SharedPtr<char>((char*)uncompressed);
		_meta.erase("um.compressed");

		LZ4F_freeDecompressionContext(ctx);
#else
		char* uncompressed = (char*)malloc((size_t)actualSize);
		int n = LZ4_decompress_fast(_data.get(), uncompressed, actualSize);
		if (n < 0) {
			errorStr = "Decompression failed";
			goto DECOMPRESS_ERROR;
		}

		_size = actualSize;
		_data = SharedPtr<char>((char*)uncompressed);
		_meta.erase("um.compressed");

#endif
		return;
	}
#endif

DECOMPRESS_ERROR:
	UM_LOG_WARN("Could not decompress message: %s", errorStr.c_str());

}
bool CompressionSuite::Decompressor::Decompress(char* pOutputBuffer, const int outputBufferSize, CompressionStats* pStats)
{
    assert(mHeader.Algorithm != ALG_Invalid);

    int uncompressedDataSize = mHeader.UncompressedDataSize;
    if (pStats)
    {
        pStats->CompressedDataSize = mCompressedDataSize;
        pStats->UncompressedDataSize = uncompressedDataSize;
    }

    if (uncompressedDataSize > outputBufferSize)
        return false;

    double elapsedTime = 0.0;
    bool result = false;
    if (mHeader.Algorithm == ALG_DOBOZ)
    {
        doboz::Decompressor decompressor;
        Timer timer;

        timer.delta();
        result = (decompressor.decompress(mpInputBuffer, mCompressedDataSize, pOutputBuffer, uncompressedDataSize) == doboz::RESULT_OK);
        elapsedTime = timer.delta();
    }
    else if (mHeader.Algorithm == ALG_YAPPY)
    {
        Timer timer;
        timer.delta();

        unsigned char* pSrcData = (unsigned char*)mpInputBuffer;
        unsigned char* pSrcDataEnd = (unsigned char*)mpInputBuffer + mCompressedDataSize;
        unsigned char* pDstData = (unsigned char*)pOutputBuffer;

        while (pSrcData < pSrcDataEnd)
        {
            unsigned short compressedBlockSize = *reinterpret_cast<unsigned short*>(pSrcData);
            pSrcData += sizeof(unsigned short);

            unsigned char* pSrcBlockEnd = pSrcData + compressedBlockSize;
            unsigned char* pDstBlockEnd = Yappy_UnCompress(pSrcData, pSrcBlockEnd, pDstData);

            pDstData = pDstBlockEnd;
            pSrcData = pSrcBlockEnd;
        };

        elapsedTime = timer.delta();

        result = ((pDstData == ((unsigned char*)pOutputBuffer + uncompressedDataSize)) &&
                  (pSrcData == pSrcDataEnd));
    }
    else if (mHeader.Algorithm == ALG_QUICKLZ)
    {
        qlz_state_decompress qlzState;
        memset(&qlzState, 0, sizeof(qlzState));
        Timer timer;

        timer.delta();
        unsigned int outSize = qlz_decompress(mpInputBuffer, pOutputBuffer, &qlzState);
        elapsedTime = timer.delta();

        result = (outSize == uncompressedDataSize);
    }
    else if (mHeader.Algorithm == ALG_FASTLZ)
    {
        Timer timer;

        timer.delta();
        int outSize = fastlz_decompress(mpInputBuffer, mCompressedDataSize, pOutputBuffer, uncompressedDataSize);
        elapsedTime = timer.delta();

        result = (outSize == uncompressedDataSize);
    }
    else if (mHeader.Algorithm == ALG_LZF)
    {
        Timer timer;

        timer.delta();
        int outSize = lzf_decompress(mpInputBuffer, mCompressedDataSize, pOutputBuffer, uncompressedDataSize);
        elapsedTime = timer.delta();

        result = (outSize == uncompressedDataSize);
    }
    else if (mHeader.Algorithm == ALG_SNAPPY)
    {
        Timer timer;

        timer.delta();
        result = snappy::RawUncompress(mpInputBuffer, mCompressedDataSize, pOutputBuffer);
        elapsedTime = timer.delta();
    }
    else if (mHeader.Algorithm == ALG_LZ4)
    {
        Timer timer;

        timer.delta();
        int outSize = LZ4_decode(mpInputBuffer, pOutputBuffer, mCompressedDataSize);
        elapsedTime = timer.delta();

        result = (outSize == uncompressedDataSize);
    }

    if (pStats)
        pStats->ElapsedTime = elapsedTime;

    FreeCompressedData();

    return result;
}
Пример #20
0
bool
rtems_rtl_obj_comp_read (rtems_rtl_obj_comp_t* comp,
                         void*                 buffer,
                         size_t                length)
{
  uint8_t* bin = buffer;

  if (!comp->cache)
  {
    rtems_rtl_set_error (EINVAL, "not open");
    return false;
  }

  if (comp->fd != comp->cache->fd)
  {
    comp->level = 0;
  }

  while (length)
  {
    size_t buffer_level;

    buffer_level = length > comp->level ? comp->level : length;

    if (buffer_level)
    {
      memcpy (bin, comp->buffer, buffer_level);

      if ((comp->level - buffer_level) != 0)
      {
        memmove (comp->buffer,
                 comp->buffer + buffer_level,
                 comp->level - buffer_level);
      }

      bin += buffer_level;
      length -= buffer_level;
      comp->level -= buffer_level;
      comp->read += buffer_level;
    }

    if (length)
    {
      uint8_t* input = NULL;
      uint16_t block_size;
      size_t   in_length = sizeof (block_size);
      int      decompressed;

      if (!rtems_rtl_obj_cache_read (comp->cache, comp->fd, comp->offset,
                                     (void**) &input, &in_length))
        return false;

      block_size = (input[0] << 8) | input[1];

      comp->offset += sizeof (block_size);

      in_length = block_size;

      if (!rtems_rtl_obj_cache_read (comp->cache, comp->fd, comp->offset,
                                     (void**) &input, &in_length))
        return false;

      if (in_length != block_size)
      {
        rtems_rtl_set_error (EIO, "compressed read failed: bs=%u in=%u",
                             block_size, in_length);
        return false;
      }

      switch (comp->compression)
      {
        case RTEMS_RTL_COMP_NONE:
          memcpy (comp->buffer, input, in_length);
          decompressed = in_length;
          break;

        case RTEMS_RTL_COMP_LZ77:
          decompressed = fastlz_decompress (input, in_length,
                                            comp->buffer, comp->size);
          if (decompressed == 0)
          {
            rtems_rtl_set_error (EBADF, "decompression failed");
            return false;
          }
          break;

        default:
          rtems_rtl_set_error (EINVAL, "bad compression type");
          return false;
      }

      comp->offset += block_size;

      comp->level = decompressed;
    }
  }

  return true;
}
Пример #21
0
	// loads the next read from the read archive
	bool CReadReader::LoadNextRead(Mosaik::Read& mr) {

		if(!mIsOpen) {
			cout << "ERROR: An attempt was made to get reads from a read archive that hasn't been opened yet." << endl;
			exit(1);
		}

		// check if there are any more reads
		if(mCurrentRead >= mNumReads) return false;

		// ==================
		// read the partition
		// ==================

		if(mPartitionMembers == mPartitionSize) {

			// read the uncompressed partition entry size
			unsigned int uncompressedSize = 0;
			fread((char*)&uncompressedSize, SIZEOF_INT, 1, mInStream);

			if(feof(mInStream)) return false;

			// read the compressed partition entry size
			int compressedSize = 0;
			fread((char*)&compressedSize, SIZEOF_INT, 1, mInStream);

			// read the partition member size
			mPartitionMembers = 0;
			fread((char*)&mPartitionSize, SIZEOF_SHORT, 1, mInStream);

			// check the compression buffer size
			CMemoryUtilities::CheckBufferSize(mCompressionBuffer, mCompressionBufferLen, compressedSize);
			CMemoryUtilities::CheckBufferSize(mBuffer, mBufferLen, uncompressedSize);

			// read and uncompress the partition
			int numBytesRead = (int)fread(mCompressionBuffer, 1, compressedSize, mInStream);

			if(numBytesRead != compressedSize) {
				cout << "ERROR: Tried to read " << compressedSize << " bytes, but received only " << numBytesRead << " bytes." << endl;
				exit(1);
			}

			int result = fastlz_decompress(mCompressionBuffer, compressedSize, mBuffer, mBufferLen);

			if(result == 0) {
				cout << "ERROR: Unable to properly uncompress the current data partition." << endl;
				exit(1);
			}

			// set the buffer pointer
			mBufferPtr = mBuffer;
		}

		// retrieve the read type
		const bool isPairedEnd = (*mBufferPtr == 0 ? false : true);
		mBufferPtr++;

		// retrieve the read name
		unsigned char readNameLen = *mBufferPtr;
		mBufferPtr++;

		mr.Name.Copy((const char*)mBufferPtr, readNameLen);
		mBufferPtr += readNameLen;

		// set the read group code
		mr.ReadGroupCode = mReadGroup.ReadGroupCode;

		// ===============
		// retrieve mate 1
		// ===============

		// retrieve the read length
		unsigned short numMate1Bases = 0;
		memcpy((char*)&numMate1Bases, mBufferPtr, SIZEOF_SHORT);
		mBufferPtr += SIZEOF_SHORT;

		// retrieve the bases
		mr.Mate1.Bases.Copy((const char*)mBufferPtr, numMate1Bases);
		mBufferPtr += numMate1Bases;

		if(mIsSOLiD) {
			memcpy(mr.Mate1.SolidPrefixTransition, mBufferPtr, SOLID_PREFIX_LENGTH);
			mBufferPtr += SOLID_PREFIX_LENGTH;
		}

		// retrieve the qualities
		mr.Mate1.Qualities.Copy((const char*)mBufferPtr, numMate1Bases);
		mBufferPtr += numMate1Bases;

		// ===============
		// retrieve mate 2
		// ===============

		if(isPairedEnd) {

			// retrieve the read length
			unsigned short numMate2Bases = 0;
			memcpy((char*)&numMate2Bases, mBufferPtr, SIZEOF_SHORT);
			mBufferPtr += SIZEOF_SHORT;

			// retrieve the bases
			mr.Mate2.Bases.Copy((const char*)mBufferPtr, numMate2Bases);
			mBufferPtr += numMate2Bases;

			if(mIsSOLiD) {
				memcpy(mr.Mate2.SolidPrefixTransition, mBufferPtr, SOLID_PREFIX_LENGTH);
				mBufferPtr += SOLID_PREFIX_LENGTH;
			}

			// retrieve the qualities
			mr.Mate2.Qualities.Copy((const char*)mBufferPtr, numMate2Bases);
			mBufferPtr += numMate2Bases;

		} else {

			// reset mate2
			mr.Mate2.Bases.SetLength(0);
			mr.Mate2.Qualities.SetLength(0);
		}

		// increment the read counter
		mCurrentRead++;
		mPartitionMembers++;

		return true;
	}
Пример #22
0
static int lua_f_fastlz_decompress ( lua_State *L )
{
    if ( !lua_isstring ( L, 1 ) ) {
        lua_pushnil ( L );
        return 1;
    }

    size_t vlen = 0;
    const char *value = lua_tolstring ( L, 1, &vlen );
    printf("=========================\n");
    printf("vlen %d\n", vlen);
        
    if ( vlen < 1 ) {
        lua_pushnil ( L );
        return 1;
    }

    unsigned int value_len = 0;
//    value_len = *value;
    memcpy ( &value_len, value, sizeof ( unsigned int ) );
    
    printf("value_len %d\n", value_len);

//    if (ntohl ( value_len ) > 0 && vlen < 1000) {
//	value_len = ntohl ( value_len );
//    }
    printf("value_len %d\n", value_len);
    if ( value_len > 1024 * 1024 + 20 ) {
        lua_pushnil ( L );
        lua_pushstring ( L, "not enough memory! too big value" );
        return 2;
    }

    char *dst = ( char * ) &temp_buf;

    if ( value_len + 20 > 4096 ) {
        dst = ( unsigned char * ) large_malloc ( value_len + 20 );
    }

    if ( dst == NULL ) {
        lua_pushnil ( L );
        lua_pushstring ( L, "not enough memory! cant malloc" );
        return 2;
    }

    int dlen = fastlz_decompress ( value + sizeof ( unsigned int ),
                                   vlen - sizeof ( unsigned int ), dst, value_len + 20 );

    if ( dst ) {
        lua_pushlstring ( L, dst, value_len );

        if ( dst != ( char * ) &temp_buf ) {
            free ( dst );
        }

        return 1;

    } else {
        lua_pushnil ( L );
        return 1;
    }
}
Пример #23
0
int unpack(void *input, size_t input_size, void** output)
{
  int chunk_id;
  int chunk_options;
  int pos = 0;
  unsigned long chunk_size;
  unsigned long chunk_checksum;
  unsigned long chunk_extra;
  unsigned char buffer[BLOCK_SIZE];
  unsigned long checksum;

  unsigned long decompressed_size;
  unsigned long total_extracted;

  unsigned char* compressed_buffer;
  unsigned char* decompressed_buffer;
  unsigned long compressed_bufsize;
  unsigned long decompressed_bufsize;

  unsigned char* b_input = (unsigned char*) input;
  size_t output_size = 0;

  /* sanity check */
  if (input_size < 8) {
    php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Data too short");
    return -1;
  }
  input_size = input_size + sizeof(sixpack_magic);
  /* not a 6pack archive? */
  if(!detect_magic(input, input_size))
  {
    php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Data not in 6pack format");
    return -1;
  }

  /* position of first chunk */
  pos = 8;

  /* initialize */
  total_extracted = 0;
  decompressed_size = 0;
  compressed_buffer = 0;
  decompressed_buffer = 0;
  compressed_bufsize = 0;
  decompressed_bufsize = 0;

  /* main loop */
  for(;;)
  {
    /* end of file? */
    if(pos > input_size)
      break;

    read_chunk_header(input, input_size, &pos, &chunk_id, &chunk_options,
        &chunk_size, &chunk_checksum, &chunk_extra);

    if((chunk_id == 1) && (chunk_size > 10) && (chunk_size < BLOCK_SIZE))
    {
      /* file entry */
      int c;
      for (c=0;c<chunk_size;c++)
      {
        if (pos < input_size) {
          buffer[c] = b_input[pos];
          pos++;
        } else {
          break;
        }
      }
      checksum = update_adler32(1L, buffer, chunk_size);
      if(checksum != chunk_checksum)
      {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Checksum mismatch. Got %08lX, expected %08lX", checksum, chunk_checksum);
        return (-1);
      }

      decompressed_size = readU32(buffer);
      total_extracted = 0;
    }

    if((chunk_id == 17) && decompressed_size)
    {
      unsigned long remaining;

      /* uncompressed */
      switch(chunk_options)
      {
        /* stored, simply copy to output */
        case 0:
          /* read one block at at time, write and update checksum */
          total_extracted += chunk_size;
          remaining = chunk_size;
          checksum = 1L;
          for(;;)
          {
            unsigned long r = (BLOCK_SIZE < remaining) ? BLOCK_SIZE: remaining;
            int c;

           for (c=0;c<r;c++)
           {
            if (pos<input_size) {
              buffer[c] = b_input[pos];
              pos++;
            } else{
              break;
            }
           }
           size_t bytes_read = c;
            if(bytes_read == 0)
              break;
            output_size = mem_append(output, output_size, buffer, bytes_read);
            checksum = update_adler32(checksum, buffer, bytes_read);
            remaining -= bytes_read;
          }

          /* verify everything is written correctly */
          if(checksum != chunk_checksum)
          {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Checksum mismatch. Got %08lX, expected %08lX", checksum, chunk_checksum);
            return (-1);
          }
          break;

        /* compressed using FastLZ */
        case 1:
          /* enlarge input buffer if necessary */
          if(chunk_size > compressed_bufsize)
          {
            compressed_bufsize = chunk_size;
            efree(compressed_buffer);
            compressed_buffer = (unsigned char*)emalloc(compressed_bufsize);
          }

          /* enlarge output buffer if necessary */
          if(chunk_extra > decompressed_bufsize)
          {
            decompressed_bufsize = chunk_extra;
            efree(decompressed_buffer);
            decompressed_buffer = (unsigned char*)emalloc(decompressed_bufsize);
          }

          /* read and check checksum */
           int c;
           for (c=0;c<chunk_size;c++)
           {
            if (pos<input_size) {
              compressed_buffer[c] = b_input[pos];
              pos++;
            } else{
              break;
            }
           }
          checksum = update_adler32(1L, compressed_buffer, chunk_size);
          total_extracted += chunk_extra;

          /* verify that the chunk data is correct */
          if(checksum != chunk_checksum)
          {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Checksum mismatch. Got %08lX, expected %08lX", checksum, chunk_checksum);
            return (-1);
          }
          else
          {
            /* decompress and verify */
            remaining = fastlz_decompress(compressed_buffer, chunk_size, decompressed_buffer, chunk_extra);
            if(remaining != chunk_extra)
            {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Decompression failed. Skipped");
            }
            else
              output_size = mem_append(output, output_size, decompressed_buffer, chunk_extra);
          }
          break;

        default:
          php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown compression method (%d)\n", chunk_options);
          break;
      }
    }

    /* position of next chunk */
    //pos = pos + 16 + chunk_size;
    //fseek(in, pos + 16 + chunk_size, SEEK_SET);
  }

  /* free allocated stuff */
  efree(compressed_buffer);
  efree(decompressed_buffer);

  /* so far so good */
  return total_extracted;
}
Пример #24
0
dtStatus cocos2d::FastLZCompressor::decompress(const unsigned char* compressed, const int compressedSize
                                             , unsigned char* buffer, const int maxBufferSize, int* bufferSize)
{
    *bufferSize = fastlz_decompress(compressed, compressedSize, buffer, maxBufferSize);
    return *bufferSize < 0 ? DT_FAILURE : DT_SUCCESS;
}