Ejemplo n.º 1
0
// our output method
void CFastLZIO::Write(const char* buffer, const unsigned int bufferLen, FILE* stm) {

	// write the buffer length
	fwrite((char*)&bufferLen, SIZEOF_INT, 1, stm);

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

	// write each block
	const char* pBuffer    = buffer;
	unsigned int bytesLeft = bufferLen;

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

		// compress the block
		unsigned int numUncompressedBytes = (bytesLeft > FASTLZ_IO_OUTPUT_BUFFER_LEN ? FASTLZ_IO_OUTPUT_BUFFER_LEN : bytesLeft);
		int numCompressedBytes = fastlz_compress_level(FASTLZ_BETTER_COMPRESSION, pBuffer, numUncompressedBytes, mBuffer);
		pBuffer   += numUncompressedBytes;
		bytesLeft -= numUncompressedBytes;

		// write the block length
		fwrite((char*)&numCompressedBytes, SIZEOF_INT, 1, stm);
		//printf("uncompressed bytes: %u, compressed bytes: %u\n", numUncompressedBytes, numCompressedBytes);

		// write the compressed block
		fwrite(mBuffer, numCompressedBytes, 1, stm);
	}
}
Ejemplo n.º 2
0
/* compression backend for FastLZ (level adjustment) */
static int fastlz_backend_compress(int level, const void* input, int length,
                                   void* output) {
  /* Level 1 is the fastest compression and generally useful for short data.
     Level 2 is slightly slower but it gives better compression ratio. */
  const int l = level <= Z_BEST_SPEED ? 1 : 2;
  return fastlz_compress_level(l, input, length, output);
}
Ejemplo n.º 3
0
void Message::prepare()
{
	if( !packet ) createPacket();

	size_t totalSize = (size_t) ms->position;

	size_t bufSize = GetCompressionBufferSize(totalSize);
	enet_packet_resize(packet, bufSize);

	MemoryStream pms;
	StreamMemoryInit(&pms);
	StreamMemorySetRawBuffer(&pms, packet->data);

	EncodeVariableInteger(&pms, id);
	StreamWrite(&pms, &flags, sizeof(flags));
	
	if( GetBitFlag(flags, MessageFlags::Compressed) )
	{
		uint8* in = &ms->data[0];
		uint8* out = pms.buffer + pms.position;
		int32 length = fastlz_compress_level(1, in, totalSize, out);
		pms.position += length;
	}
	else
	{
		enet_packet_resize(packet, totalSize + pms.position);
		StreamWrite(&pms, &ms->data[0], totalSize);
	}

	packet->dataLength = (size_t) pms.position;
}
Ejemplo n.º 4
0
/* {{{ fastlz helper functions */
PHP_FASTLZ_API int fastlz_xcompress(char *value, int value_len, char** cvalue, long compression_level TSRMLS_DC)
{
	uint32_t compressed_len;
	char *compressed;
	const uint32_t size_header = htonl(value_len);

	assert(value && cvalue);

	compressed_len = sizeof(uint32_t) + (uint32_t)((value_len * 1.05) + 2); /* add 1 rather then one, to account for the fact cast may round down */

	compressed = emalloc(compressed_len);

	memcpy(compressed, &size_header, sizeof(uint32_t));
	compressed_len = fastlz_compress_level(compression_level, value, value_len, (compressed + sizeof(uint32_t)));
	if (compressed_len > 0) {
		compressed_len += sizeof(uint32_t);
		compressed[compressed_len] = 0;
		(*cvalue) = compressed;
		return compressed_len;
	} else {
		efree(compressed);
	}

	return 0;
}
Ejemplo n.º 5
0
void Packet::prepare()
{
	if( !packet ) createPacket();

	size_t totalSize = (size_t) ms.getPosition();

	size_t bufSize = GetCompressionBufferSize(totalSize);
	enet_packet_resize(packet, bufSize);

	MemoryStream pms;
	pms.setRawBuffer(packet->data);

	EncodeVariableInteger(&pms, id);
	pms.write((uint8*) &flags, sizeof(flags));
	
	if( GetBitFlag(flags, PacketFlags::Compressed) )
	{
		uint8* in = ms.data.Buffer();
		uint8* out = pms.buffer + pms.position;
		int32 length = fastlz_compress_level(1, in, totalSize, out);
		pms.position += length;
	}
	else
	{
		enet_packet_resize(packet, totalSize + pms.position);
		pms.write(ms.data.Buffer(), totalSize);
	}

	packet->dataLength = (size_t) pms.position;
}
Ejemplo n.º 6
0
static SquashStatus
squash_fastlz_compress_buffer (SquashCodec* codec,
                               size_t* compressed_size,
                               uint8_t compressed[HEDLEY_ARRAY_PARAM(*compressed_size)],
                               size_t uncompressed_size,
                               const uint8_t uncompressed[HEDLEY_ARRAY_PARAM(uncompressed_size)],
                               SquashOptions* options) {
#if INT_MAX < SIZE_MAX
  if (HEDLEY_UNLIKELY(INT_MAX < uncompressed_size) ||
      HEDLEY_UNLIKELY(INT_MAX < *compressed_size))
    return squash_error (SQUASH_RANGE);
#endif

  const int fastlz_e =
    fastlz_compress_level (squash_options_get_int_at (options, codec, SQUASH_FASTLZ_OPT_LEVEL),
                           (const void*) uncompressed,
                           (int) uncompressed_size,
                           (void*) compressed);

#if SIZE_MAX < INT_MAX
  if (HEDLEY_UNLIKELY(SIZE_MAX < fastlz_e))
    return squash_error (SQUASH_RANGE);
#endif

  *compressed_size = (size_t) fastlz_e;

  return HEDLEY_UNLIKELY(fastlz_e == 0) ? squash_error (SQUASH_FAILED) : SQUASH_OK;
}
Ejemplo n.º 7
0
        static bool compress( T2 &buffer_out, const T1 &buffer_in, bool highest_compression = true )
        {
            static const bool verbose = false;

            bool ret = false;

            if( buffer_in.size() >= 70 )
            {
                buffer_out.resize( std::max<size_t>( size_t(buffer_in.size() * 1.05), 70 ) );

                size_t compressed_size = fastlz_compress_level( highest_compression ? 2 : 1, &buffer_in.at(0), buffer_in.size(), &buffer_out.at(0) );

                ret = ( compressed_size > 0 && compressed_size < buffer_in.size() );

                if( ret )
                {
                    buffer_out.resize( compressed_size );
                }

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

            return ret;
        }
Ejemplo n.º 8
0
static void dumpImage(writer_t*w, state_t*state, gfximage_t*img)
{
    int oldpos = w->pos;
    writer_writeU16(w, img->width);
    writer_writeU16(w, img->height);
#ifdef COMPRESS_IMAGES
    //35.3% images (2027305 bytes) (with filter, Z_BEST_COMPRESSION)
    //39.9% images (2458904 bytes) (with filter, Z_BEST_SPEED)
    //45.2% images (3055340 bytes) (without filter)
    //45.9% images (3149247 bytes) (without filter, 5)
    //48.0% images (3480495 bytes) (with filter, fastlz)
    //48.0% images (3488650 bytes) (without filter, Z_BEST_SPEED)
    //55.3% images (4665889 bytes) (without filter, fastlz level 2)
    //55.6% images (4726334 bytes) (without filter, fastlz level 1)
    //83.0% images (18091804 bytes) (no compression)

    gfxcolor_t*image;
#ifdef FILTER_IMAGES
    unsigned char*filter = malloc(img->height);
    int y;
    image = malloc(img->width*img->height*sizeof(gfxcolor_t));
    for(y=0;y<img->height;y++) {
	filter[y] = png_apply_filter_32(
		(void*)&image[y*img->width], 
		(void*)&img->data[y*img->width], img->width, y);
    }
#else
    image = img->data;
#endif
    int size = img->width*img->height;
    uLongf compressdata_size = compressBound(size*sizeof(gfxcolor_t));
    void*compressdata = malloc(compressdata_size);

#ifdef HAVE_FASTLZ
    compressdata_size = fastlz_compress_level(2, (void*)image, size*sizeof(gfxcolor_t), compressdata);
#else
    compress2(compressdata, &compressdata_size, (void*)image, sizeof(gfxcolor_t)*size, Z_BEST_SPEED);
#endif

    writer_writeU32(w, compressdata_size);
#ifdef FILTER_IMAGES
    w->write(w, filter, img->height);
    free(filter);
    free(image);
#endif
    w->write(w, compressdata, compressdata_size);
    free(compressdata);
#else
    w->write(w, img->data, img->width*img->height*sizeof(gfxcolor_t));
#endif
#ifdef STATS
    state->size_images += w->pos - oldpos;
#endif
}
Ejemplo n.º 9
0
static SquashStatus
squash_fastlz_compress_buffer (SquashCodec* codec,
                               size_t* compressed_length,
                               uint8_t compressed[SQUASH_ARRAY_PARAM(*compressed_length)],
                               size_t uncompressed_length,
                               const uint8_t uncompressed[SQUASH_ARRAY_PARAM(uncompressed_length)],
                               SquashOptions* options) {
  *compressed_length = fastlz_compress_level (squash_codec_get_option_int_index (codec, options, SQUASH_FASTLZ_OPT_LEVEL),
                                              (const void*) uncompressed,
                                              (int) uncompressed_length,
                                              (void*) compressed);

  return (*compressed_length == 0) ? squash_error (SQUASH_FAILED) : SQUASH_OK;
}
Ejemplo n.º 10
0
// write partition to disk
void CReadWriter::WritePartition(void) {

    // check the compression buffer size
    unsigned int requestedSize = (unsigned int)(mBufferPosition * 1.05);
    CMemoryUtilities::CheckBufferSize(mCompressionBuffer, mCompressionBufferLen, requestedSize);

    // compress the partition
    int compressedSize = fastlz_compress_level(FASTLZ_BETTER_COMPRESSION, mBuffer, mBufferPosition, mCompressionBuffer);

    // write the uncompressed partition entry size
    fwrite((char*)&mBufferPosition, SIZEOF_INT, 1, mOutStream);

    // write the compressed partition entry size
    fwrite((char*)&compressedSize, SIZEOF_INT, 1, mOutStream);

    // write the partition member size
    fwrite((char*)&mPartitionMembers, SIZEOF_SHORT, 1, mOutStream);

    // write the partition
    fwrite(mCompressionBuffer, compressedSize, 1, mOutStream);

    mPartitionMembers = 0;
    mBufferPosition   = 0;
}
Ejemplo n.º 11
0
int64_t lzbench_fastlz_compress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t level, size_t, char*)
{
	return fastlz_compress_level(level, inbuf, insize, outbuf);
}
Ejemplo n.º 12
0
size_t fa_compress_block(fa_compression_t compression, void* out, size_t outSize, const void* in, size_t inSize)
{
	switch (compression)
	{
		default: return inSize;

		case FA_COMPRESSION_FASTLZ:
		{
			if (inSize < 16)
			{
				return inSize;
			}

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

			if (outSize < compressBound(inSize))
			{
				return inSize;
			}

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

			return destLen;
		}
		break;
#endif

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

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

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

			lzma_ret ret = lzma_raw_buffer_encode(filters, NULL, in, inSize, out, &outPos, outSize);
			if (ret != LZMA_OK)
			{
				return inSize;
			}
			return outPos;
		}
		break;
#endif
	}
}