Exemplo n.º 1
0
static void
run (int format, gboolean alpha)
{
  guint8 data[2000] = { 0, };
  guint8 compressed[2000] = { 0, };
  gsize len; 
  gulong clen;
  int ret;
  char *s;

  switch (format) {
    case 3:
      len = create_palletized (data, alpha);
      g_print ("%u\n", len);
      break;
    default:
      g_assert_not_reached ();
      return;
  }
  
  if (alpha) {
    clen = sizeof (compressed);
    ret = compress2 (compressed, &clen, data, len, 9);
  } else {
    clen = sizeof (compressed) - 1;
    ret = compress2 (compressed + 1, &clen, data + 1, len - 1, 9);
    compressed[0] = data[0];
  }
  g_assert (ret == Z_OK);

  s = g_base64_encode (compressed, clen + 1);
  g_print ("%s\n\n", s);
  g_free (s);
}
Exemplo n.º 2
0
static TACommandVerdict compress2_cmd(TAThread thread,TAInputStream stream)
{
    Bytef * dest, *source;
    uLongf destLen, sourceLen;
    int res, level;

    dest = readPointer(&stream);
    destLen = readULong(&stream);
    source = readPointer(&stream);
    sourceLen = readULong(&stream);
    level = readInt(&stream);

    ta_debug_printf("level==%d\n", level);
    ta_debug_printf("source==%s\n", source);

    START_TARGET_OPERATION(thread);

    if(level==-1)
        res = compress2(dest, &destLen, source, sourceLen,
                                                Z_DEFAULT_COMPRESSION);
    else
        res = compress2(dest, &destLen, source, sourceLen, level);

    END_TARGET_OPERATION(thread);

    ta_debug_printf("dest==%s\n", dest);

    writePointer(thread, dest);
    writeULong(thread, destLen);
    writeInt(thread, res);

    sendResponse(thread);

    return taDefaultVerdict;
}
Exemplo n.º 3
0
bool compress(uint8 *dst, size_t &dst_size, const uint8 *src, size_t src_size,
              level l)
{
    return Z_OK == compress2(reinterpret_cast<Bytef*>(dst), reinterpret_cast<uLongf*>(&dst_size),
                             reinterpret_cast<const Bytef*>(src), static_cast<uLong>(src_size),
                             static_cast<int>(l));
}
Exemplo n.º 4
0
//Deryabin Andrew: vst chunks support
template <typename T> void
rdwr_writeRaw(T fd, std::vector<char> rawdata, const char *file, int line)
{
    unsigned long complen = compressBound(rawdata.size());
    char *compressed = new char [complen];
    if(!compressed)
    {
        fprintf(stderr, "Failed to allocate %lu bytes of memory at %s:%d\n", complen, file, line);
        throw RemotePluginClosedException();
    }

    std::vector<char>::pointer ptr = &rawdata [0];

    if(compress2((Bytef *)compressed, &complen, (Bytef *)ptr, rawdata.size(), 9) != Z_OK)
    {
        delete compressed;
        fprintf(stderr, "Failed to compress source buffer at %s:%d\n", file, line);
        throw RemotePluginClosedException();
    }

    fprintf(stderr, "compressed source buffer. size=%lu bytes\n", complen);

    int len = complen;
    rdwr_tryWrite(fd, &len, sizeof(int), file, line);
    len = rawdata.size();
    rdwr_tryWrite(fd, &len, sizeof(int), file, line);    
    rdwr_tryWrite(fd, compressed, complen, file, line);

    delete [] compressed;
}
Exemplo n.º 5
0
/*
 * Function:    compress_buffer
 * Purpose:     Compress the buffer.
 * Returns:     Z_OK            - success
 *              Z_MEM_ERROR     - not enough memory
 *              Z_BUF_ERROR     - not enough room in the output buffer
 *              Z_STREAM_ERROR  - level parameter is invalid
 * Programmer:  Bill Wendling, 05. June 2002
 * Modifications:
 */
static void
compress_buffer(Bytef *dest, uLongf *destLen, const Bytef *source,
                uLong sourceLen)
{
    int rc = compress2(dest, destLen, source, sourceLen, compress_level);

    if (rc != Z_OK) {
        /* compress2 failed - cleanup and tell why */
        cleanup();

        switch (rc) {
        case Z_MEM_ERROR:
            error("not enough memory");
            break;
        case Z_BUF_ERROR:
            error("not enough room in the output buffer");
            break;
        case Z_STREAM_ERROR:
            error("level parameter (%d) is invalid", compress_level);
            break;
        default:
            error("unknown compression error");
            break;
        }
    }
}
Exemplo n.º 6
0
static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
	if(level == 0) {
		memcpy(dest, source, len);
		return len;
	} else if(level == 10) {
#ifdef HAVE_LZO
		lzo_uint lzolen = MAXSIZE;
		lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
		return lzolen;
#else
		return -1;
#endif
	} else if(level < 10) {
#ifdef HAVE_ZLIB
		unsigned long destlen = MAXSIZE;
		if(compress2(dest, &destlen, source, len, level) == Z_OK)
			return destlen;
		else
#endif
			return -1;
	} else {
#ifdef HAVE_LZO
		lzo_uint lzolen = MAXSIZE;
		lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
		return lzolen;
#else
		return -1;
#endif
	}
	
	return -1;
}
Exemplo n.º 7
0
Arquivo: stream.c Projeto: epa/lrzip
static void gzip_compress_buf(struct stream *s, int *c_type, i64 *c_len)
{
	uchar *c_buf;
	unsigned long dlen = s->buflen;

	c_buf = malloc(dlen);
	if (!c_buf)
		return;

	if (compress2(c_buf, &dlen, s->buf, s->buflen, control.compression_level) != Z_OK) {
		free(c_buf);
		return;
	}

	if ((i64)dlen >= *c_len) {
		/* Incompressible, leave as CTYPE_NONE */
		free(c_buf);
		return;
	}

	*c_len = dlen;
	free(s->buf);
	s->buf = c_buf;
	*c_type = CTYPE_GZIP;
}
Exemplo n.º 8
0
static void writeDat( std::ofstream & os, const std::vector<char> & data ) {
	std::vector<char> cdata( data.size() );
	uLongf len = data.size();
	compress2( (Bytef*)cdata.data(), &len, (const Bytef*)data.data(), data.size(), 9 );
	cdata.resize( len );
	writeChunk( os, "IDAT", cdata );
}
Exemplo n.º 9
0
void
TestCompress(CuTest * tc)
{
    char input[] =
        {
 "This is a test input string.  I like to test inputThis is a test input string.  I like to test input."
};

    uLongf output_len = 2000;
    void *output = malloc(output_len);
    uLongf input_len = (uLongf) strlen(input);

  /**
   * Attempt the compression.
   */
    int ret =
        compress2(output, &output_len, (void *)input, input_len,
                  Z_BEST_SPEED);

  /**
   * Assert it 1.  Worked.
   */
    CuAssertTrue(tc, ret == Z_OK);

  /**
   * The output size is smaller than the input size.
   */
    CuAssertTrue(tc, output_len < input_len);

    free(output);
}
Exemplo n.º 10
0
/**
 * Attempt to compress and decompress.
 */
void
TestDecompress(CuTest * tc)
{
    char input[] =
        {
 "This is a test input string.  I like to test inputThis is a test input string.  I like to test input."
};
    uLongf input_len = (uLongf) strlen(input);

    uLongf compressed_len = 2000;
    void *compressed = malloc(compressed_len);

    uLongf decompressed_len = 2000;
    void *decompressed = malloc(decompressed_len);


  /**
   * Attempt the compression.
   */
    int ret =
        compress2(compressed, &compressed_len, (void *)input, input_len,
                  Z_BEST_SPEED);

  /**
   * Assert it 1.  Worked.
   */
    CuAssertTrue(tc, ret == Z_OK);

  /**
   * The output size is smaller than the input size.
   */
    CuAssertTrue(tc, compressed_len < input_len);


  /**
   * OK now we decompress.
   */
    ret =
        uncompress(decompressed, &decompressed_len, (void *)compressed,
                   compressed_len);

  /**
   * Assert: 1. it worked.
   */
    CuAssertTrue(tc, ret == Z_OK);

  /**
   * 2. Output is longer than the input.
   */
    CuAssertTrue(tc, compressed_len < decompressed_len);

  /**
   * 3. Ouptut is equal to original starting point.
   */
    CuAssertTrue(tc, decompressed_len == input_len);
    CuAssertTrue(tc, (strcmp(decompressed, input) == 0));

    free(compressed);
    free(decompressed);
}
Exemplo n.º 11
0
  void ZlibCompressor::Compress(std::string& compressed,
                                const void* uncompressed,
                                size_t uncompressedSize)
  {
    if (uncompressedSize == 0)
    {
      compressed.clear();
      return;
    }

    uLongf compressedSize = compressBound(uncompressedSize) + 1024 /* security margin */;
    if (compressedSize == 0)
    {
      compressedSize = 1;
    }

    uint8_t* target;
    if (HasPrefixWithUncompressedSize())
    {
      compressed.resize(compressedSize + sizeof(uint64_t));
      target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
    }
    else
    {
      compressed.resize(compressedSize);
      target = reinterpret_cast<uint8_t*>(&compressed[0]);
    }

    int error = compress2(target,
                          &compressedSize,
                          const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), 
                          uncompressedSize,
                          GetCompressionLevel());

    if (error != Z_OK)
    {
      compressed.clear();

      switch (error)
      {
      case Z_MEM_ERROR:
        throw OrthancException(ErrorCode_NotEnoughMemory);

      default:
        throw OrthancException(ErrorCode_InternalError);
      }  
    }

    // The compression was successful
    if (HasPrefixWithUncompressedSize())
    {
      uint64_t s = static_cast<uint64_t>(uncompressedSize);
      memcpy(&compressed[0], &s, sizeof(uint64_t));
      compressed.resize(compressedSize + sizeof(uint64_t));
    }
    else
    {
      compressed.resize(compressedSize);
    }
  }
Exemplo n.º 12
0
int CDataFileWriter::AddData(int Size, void *pData)
{
	if(!m_File) return 0;

	dbg_assert(m_NumDatas < 1024, "too much data");

	CDataInfo *pInfo = &m_pDatas[m_NumDatas];
	unsigned long s = compressBound(Size);
	void *pCompData = mem_alloc(s, 1); // temporary buffer that we use during compression

	int Result = compress2((Bytef*)pCompData, &s, (Bytef*)pData, Size, CompressionLevel); // ignore_convention
	if(Result != Z_OK)
	{
		dbg_msg("datafile", "compression error %d", Result);
		dbg_assert(0, "zlib error");
	}

	pInfo->m_UncompressedSize = Size;
	pInfo->m_CompressedSize = (int)s;
	pInfo->m_pCompressedData = mem_alloc(pInfo->m_CompressedSize, 1);
	mem_copy(pInfo->m_pCompressedData, pCompData, pInfo->m_CompressedSize);
	mem_free(pCompData);

	m_NumDatas++;
	return m_NumDatas-1;
}
Exemplo n.º 13
0
int segment_compress(int *source, int size, char *compress_source, long int *compress_size)
{	
	int err;
	
	uLong sourceLen = (uLongf)size;
	uLongf destLen = (uLongf)(*compress_size);
	Bytef *byte_source;
	Bytef *byte_dest;
	
	byte_source = (Bytef *)source;
	byte_dest = (Bytef *)compress_source;
	
	err = compress2(byte_dest, &destLen, byte_source, sourceLen, COMPRESS_LEVEL);
	
	if(err != Z_OK) {
		printf("ERROR: Error compress!\n");
		if(err == Z_BUF_ERROR)
			printf("ERROR: The buffer was not large enough to hold the uncompressed data.\n");
		if(err == Z_MEM_ERROR)
			printf("ERROR: Insufficient memory.\n");
		if(err == Z_STREAM_ERROR)
			printf("ERROR: The compressed data (referenced by source) was corrupted.\n");
	}

	*compress_size = (long int)destLen;
	
	return 0;
}
Exemplo n.º 14
0
Arquivo: main.cpp Projeto: CCJY/coliru
std::vector<uint8_t> compress(const std::vector<uint8_t> & input, int level)
{
    uLongf destlen = input.size();
    std::vector<uint8_t> output;
    output.resize(2 * input.size());

    Bytef* dest = reinterpret_cast<Bytef*>(&output.front());

    uLongf srclen = input.size();
    const Bytef* src = input.data();

    int ret = ::compress2(dest, &destlen, src, srclen, level);

    // realloc a larger buffer if the current one is too small
    while (ret == Z_BUF_ERROR)
    {
        output.resize(2 * output.size());
        dest = reinterpret_cast<Bytef*>(&output.front());
        destlen = output.size();
        dest = reinterpret_cast<Bytef*>(&output.front());
        ret = compress2(dest, &destlen, src, srclen, level);
    }

    output.resize(destlen);
    output.shrink_to_fit();
    return output;
}
Exemplo n.º 15
0
	/// Compress data by Zlib
	bool nekoByteBuffer::Compress(int32 level)
	{
		nekoAssert(level >= 0 && level <= 9, "압축 레벨은 0과 9 사이의 정수 입니다.");

		// No data
		if(!length)
			return false;

		// Compress it.
		int32 destLen;
		char8 *destData = nekoNew char8[(destLen = (int32)((length + 12) * 1.1)) + sizeof(int32)];
		if(compress2((Bytef *)(destData + sizeof(int32)), (uLongf *)&destLen, (Bytef *)buf, (uLongf)length, level) != Z_OK)
		{
			delete []destData;
			return false;
		}

		SafeDeleteArray(buf);

		// 앞부분에 원래 길이 넣어두기
		memcpy(destData, &length, sizeof(int32));
		buf = destData;
		length = destLen + sizeof(int32);
		writePos = length;
		return true;
	}
Exemplo n.º 16
0
/**
 * Try to compress the given block of data.
 *
 * @param data block to compress; if compression
 *        resulted in a smaller block, the first
 *        bytes of data are updated to the compressed
 *        data
 * @param oldSize number of bytes in data
 * @param result set to the compressed data
 * @param newSize set to size of result
 * @return GNUNET_YES if compression reduce the size,
 *         GNUNET_NO if compression did not help
 */
static int
try_compression (const char *data, size_t oldSize, char **result,
                 size_t * newSize)
{
  char *tmp;
  uLongf dlen;

#ifdef compressBound
  dlen = compressBound (oldSize);
#else
  dlen = oldSize + (oldSize / 100) + 20;
  /* documentation says 100.1% oldSize + 12 bytes, but we
   * should be able to overshoot by more to be safe */
#endif
  tmp = GNUNET_malloc (dlen);
  if (Z_OK ==
      compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9))
  {
    if (dlen < oldSize)
    {
      *result = tmp;
      *newSize = dlen;
      return GNUNET_YES;
    }
  }
  GNUNET_free (tmp);
  return GNUNET_NO;
}
Exemplo n.º 17
0
unsigned int MkZipCompressor::Compress
(const unsigned char* srcArray, unsigned int srcSize, MkByteArray& destBuffer, int level)
{
	if ((srcArray == 0) || (srcSize == 0))
		return 0;

	uLongf lfSrcSize = static_cast<uLongf>(srcSize);
	uLongf bonusSize = lfSrcSize / 100;
	if (bonusSize < 1024) bonusSize = 1024;
	uLongf destSize = lfSrcSize + bonusSize;  // 압축후 용량이 올라가는 경우도 있으므로 1% 혹은 1kbyte 여유 확보

	unsigned char* destArray = new unsigned char[destSize];
	int rlt = compress2(destArray, &destSize, srcArray, lfSrcSize, level);
	unsigned int newSize;
	if (rlt == Z_OK) // Z_MEM_ERROR, Z_BUF_ERROR
	{
		newSize = static_cast<unsigned int>(destSize);
		destBuffer.Clear();
		destBuffer.Fill(newSize);
		destBuffer.Overwrite(0, MkByteArrayDescriptor(destArray, newSize));
	}
	else
	{
		newSize = 0;
	}
	delete [] destArray;
	return newSize;
}
Exemplo n.º 18
0
/**
 * Compress two strings and return the length of the compressed data
 * @param x String x
 * @param y String y
 * @return length of the compressed data.
 */
static float compress_str2(hstring_t x, hstring_t y)
{
    unsigned long tmp, width;
    unsigned char *src, *dst;

    assert(x.type == y.type);

    width = x.type == TYPE_SYM ? sizeof(sym_t) : sizeof(char);
    tmp = compressBound((x.len + y.len) * width);

    dst = malloc(tmp);
    src = malloc(tmp);
    if (!src || !dst) {
        error("Failed to allocate memory for compression");
        return -1;
    }

    /* Concatenate sequences y and x */
    memcpy(src, y.str.s, y.len * width);
    memcpy(src + y.len * width, x.str.s, x.len * width);

    compress2(dst, &tmp, src, (x.len + y.len) * width, level);

    free(dst);
    free(src);
    return (float) tmp;
}
Exemplo n.º 19
0
/*
 * Add an arbitrary TLV whose data is to be compressed with zlib.
 */
DWORD packet_add_tlv_raw_compressed(Packet *packet, TlvType type, LPVOID buf, DWORD length)
{
	DWORD result            = ERROR_SUCCESS;
	DWORD headerLength      = sizeof( TlvHeader );
	PUCHAR newPayload       = NULL;
	BYTE * compressed_buf   = NULL;
	DWORD realLength        = 0;
	DWORD newPayloadLength  = 0;
	DWORD compressed_length = (DWORD)( 1.01 * ( length + 12 ) + 1 );

	do
	{
		compressed_buf = (BYTE *)malloc( compressed_length );
		if( !compressed_buf )
		{
			result = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}

		if( compress2( compressed_buf, &compressed_length, buf, length, Z_BEST_COMPRESSION ) != Z_OK )
		{
			result = ERROR_UNSUPPORTED_COMPRESSION;
			break;
		}

		realLength       = compressed_length + headerLength;
		newPayloadLength = packet->payloadLength + realLength;
		
		// Allocate/Reallocate the packet's payload
		if( packet->payload )
			newPayload = (PUCHAR)realloc(packet->payload, newPayloadLength);
		else
			newPayload = (PUCHAR)malloc(newPayloadLength);
	
		if( !newPayload )
		{
			result = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}

		// Populate the new TLV
		((LPDWORD)(newPayload + packet->payloadLength))[0] = htonl(realLength);
		((LPDWORD)(newPayload + packet->payloadLength))[1] = htonl((DWORD)type);

		memcpy(newPayload + packet->payloadLength + headerLength, compressed_buf, compressed_length );

		// Update the header length and payload length
		packet->header.length = htonl(ntohl(packet->header.length) + realLength);
		packet->payload       = newPayload;
		packet->payloadLength = newPayloadLength;

		result = ERROR_SUCCESS;

	} while( 0 );

	if( compressed_buf )
		free( compressed_buf );

	return result;
}
Exemplo n.º 20
0
// -----------------------------------------------------------------------------------
// Compress a binary dump file (beginning at offset head_size)
void CompressBinaryDump(const char* file, unsigned int head_size)
{
	// for simplicity ... copy the file into memory again and compress it there
	FILE* p = fopen(file,"r");
	fseek(p,0,SEEK_END);
	const uint32_t size = ftell(p);
	fseek(p,0,SEEK_SET);

	if (size<head_size) {
		fclose(p);
		return;
	}

	uint8_t* data = new uint8_t[size];
	fread(data,1,size,p);

	uLongf out_size = (uLongf)((size-head_size) * 1.001 + 12.);
	uint8_t* out = new uint8_t[out_size];

	compress2(out,&out_size,data+head_size,size-head_size,9);
	fclose(p);
	p = fopen(file,"w");

	fwrite(data,head_size,1,p);
	fwrite(&out_size,4,1,p); // write size of uncompressed data
	fwrite(out,out_size,1,p);

	fclose(p);
	delete[] data;
	delete[] out;
}
Exemplo n.º 21
0
// Quality comes in 0-100, while the zlib param needs to be 0-9 
CPLErr CompressZLIB(buf_mgr &dst, buf_mgr &src, const ILImage &img)
{
    if (Z_OK==compress2((Bytef *)dst.buffer,(uLongf *)&dst.size,
        (Bytef *)src.buffer,src.size,img.quality/10)) return CE_None;
    CPLError(CE_Failure,CPLE_AppDefined,"MRF: Error during zlib compression");
    return CE_Failure;
}
Exemplo n.º 22
0
/**
Compresses a source buffer into a target buffer, using the ZLib library. 
On success, the target buffer contains a GZIP compatible layout.
Upon entry, target_size is the total size of the destination buffer, 
which must be at least 0.1% larger than source_size plus 24 bytes. 

@param target Destination buffer
@param target_size Size of the destination buffer, in bytes
@param source Source buffer
@param source_size Size of the source buffer, in bytes
@return Returns the actual size of the compressed buffer, returns 0 if an error occured
@see FreeImage_ZLibCompress
*/
DWORD DLL_CALLCONV 
FreeImage_ZLibGZip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size) {
	uLongf dest_len = (uLongf)target_size - 12;
	DWORD crc = crc32(0L, NULL, 0);

    // set up header (stolen from zlib/gzio.c)
    sprintf((char *)target, "%c%c%c%c%c%c%c%c", 0x1f, 0x8b,
         Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/);
    int zerr = compress2(target + 8, &dest_len, source, source_size, Z_BEST_COMPRESSION);
	switch(zerr) {
		case Z_MEM_ERROR:	// not enough memory
		case Z_BUF_ERROR:	// not enough room in the output buffer
			FreeImage_OutputMessageProc(FIF_UNKNOWN, "Zlib error : %s", zError(zerr));
			return 0;
        case Z_OK: {
            // patch header, setup crc and length (stolen from mod_trace_output)
            BYTE *p = target + 8; *p++ = 2; *p = OS_CODE; // xflags, os_code
 	        crc = crc32(crc, source, source_size);
	        memcpy(target + 4 + dest_len, &crc, 4);
	        memcpy(target + 8 + dest_len, &source_size, 4);
            return dest_len + 12;
        }
	}
	return 0;
}
Exemplo n.º 23
0
int   XComDoc_Compress(_xcd_int8* dest_buf,_xcd_int8* src_buf, int src_len,int compressRate)
{
	if(compressRate == XCOMDOC_COMPRESS_ENCRYPT)
		return XComDoc_Encrypot(dest_buf,src_buf,src_len);

    if(compressRate <= 10)
    {
#ifndef _WIN32_WCE
        uLong dest_len =  compressBound(src_len);
#else
        uLong dest_len =  2 * src_len + 12;
#endif
        compress2((Byte *)dest_buf,&dest_len,(Byte *)src_buf,src_len,compressRate - 1);
        return dest_len;
    }    
    else//大于10。用LZMA压缩
    {
        size_t dest_len = 2 * src_len + 12;
#ifdef _USE_LZMA_        
        struct _outProp
        {
            char _v;
            int  _disSize;
        }outProps;
        CompressLzma(dest_buf , &dest_len , src_buf ,src_len , (unsigned char*)&outProps , compressRate - 6 );
#else
        XEVOL_LOG(eXL_ERROR_FALT , "lzma not compiled, uncompressed failed\n");
        assert(0);
#endif
        return (int)dest_len;
    }
	
}
Exemplo n.º 24
0
void compressUtil(unsigned long originalDataLen) {
	//get compress buffer bound
	int rv;
	int compressBufBound = compressBound(originalDataLen);
	compressedBuf = (unsigned char*) malloc(sizeof(unsigned char)*compressBufBound);
	unsigned long compressedDataLen = compressBufBound;
	//compress
	rv = compress2(compressedBuf, &compressedDataLen, dataBuf, originalDataLen, 6);
	if (Z_OK != rv) {
		LOGE(1, "compression error");
		free(compressedBuf);
		return;
	}
	LOGI(1, "upper bound:%d; input: %d; compressed: %d",
			compressBufBound, originalDataLen, compressedDataLen);
	//decompress and verify result
	unsigned long decompressedDataLen = S_BUF_SIZE;
	rv = uncompress(decompressedBuf, &decompressedDataLen, compressedBuf, compressedDataLen);
	if (Z_OK != rv) {
		LOGE(1, "decompression error");
		free(compressedBuf);
		return;
	}
	LOGI(1, "decompressed: %d", decompressedDataLen);
	if (0 == memcmp(dataBuf, decompressedBuf, originalDataLen)) {
		LOGI(1, "decompressed data same as original data");
	} else {
		LOGI(1, "decompressed data different from original data");
	}
	//free resource
	free(compressedBuf);
}
Exemplo n.º 25
0
double compute_archive_ratio(t_idx& idx)
{
    size_t bits_encoding = idx.factor_text.size();
    size_t bits_blockmap = idx.block_map.size_in_bytes() * 8;
    size_t bits_compressed_dict = 0;
    {
        const uint8_t* dict = (const uint8_t*) idx.dict.data();
        size_t dict_size = idx.dict.size();
        std::vector<uint8_t> dict_buf(dict_size*2);
        uint8_t* out_buf = dict_buf.data();
        uint64_t out_len = dict_buf.size();
        int c*k = compress2(out_buf,&out_len,dict,dict_size,9);
        if(c*k != Z_OK) {
            if(c*k == Z_MEM_ERROR) LOG(FATAL) << "error compressing dictionary: Z_MEM_ERROR";
            if(c*k == Z_BUF_ERROR) LOG(FATAL) << "error compressing dictionary: Z_BUF_ERROR";
            if(c*k == Z_STREAM_ERROR) LOG(FATAL) << "error compressing dictionary: Z_STREAM_ERROR";
            LOG(FATAL) << "error compressing ditionary: UNKNOWN ERROR ("<<c*k<<")";
        }
        bits_compressed_dict = out_len * 8;
    }
    size_t encoding_bits_total = bits_encoding + bits_compressed_dict + bits_blockmap;
    size_t text_size_bits = idx.size() * 8;
    
    double archive_ratio = 100.0 * double(encoding_bits_total) / double(text_size_bits);
    return archive_ratio;
}
Exemplo n.º 26
0
// compress current chunk and write it to file,
// also update directory map
void write_current_chunk(void) {
    // uncompressed chunk size
    unsigned long cs = thePandalog->chunk.buf_p - thePandalog->chunk.buf;
    unsigned long ccs = thePandalog->chunk.zsize;
    int ret;
    // loop allows compress2 to fail and resize output buffer as needed
    // not sure why compress2 needs output buf to be bigger than input
    // even though ultimately it is smaller.  scratch space?
    // 10 is just a random guess.  shouldn't need more than 1 re-try
    uint32_t i;
    for (i=0; i<10; i++) {
        ret = compress2(thePandalog->chunk.zbuf, &ccs, thePandalog->chunk.buf, cs, Z_BEST_COMPRESSION);
        if (ret == Z_OK) break;
        // bigger output buffer needed to perform compression?
        thePandalog->chunk.zsize *= 2;
        thePandalog->chunk.zbuf = (unsigned char *) realloc(thePandalog->chunk.zbuf, thePandalog->chunk.zsize);
        assert (thePandalog->chunk.zbuf != NULL);
    }
    // ccs is final compressed chunk size
    assert (ret == Z_OK);
    assert(ccs > 0);
    assert(cs >= ccs);
    printf ("writing chunk %d of pandalog %d / %d = %.2f compression\n",
            (int) thePandalog->chunk_num, (int) cs, (int) ccs, ((float) cs) / ((float) ccs));
    fwrite(thePandalog->chunk.zbuf, 1, ccs, thePandalog->file);
    add_dir_entry(thePandalog->chunk_num);
    // reset start instr / pos
    thePandalog->chunk.start_instr = rr_get_guest_instr_count();
    thePandalog->chunk.start_pos = ftell(thePandalog->file);
    // rewind chunk buf and inc chunk #
    thePandalog->chunk.buf_p = thePandalog->chunk.buf;
    thePandalog->chunk_num ++;
    thePandalog->chunk.ind_entry = 0;
}
/**
 * Compute a type map message for this peer.
 *
 * @return this peers current type map message.
 */
struct GNUNET_MessageHeader *
GSC_TYPEMAP_compute_type_map_message ()
{
  char *tmp;
  uLongf dlen;
  struct GNUNET_MessageHeader *hdr;

#ifdef compressBound
  dlen = compressBound (sizeof (my_type_map));
#else
  dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
  /* documentation says 100.1% oldSize + 12 bytes, but we
   * should be able to overshoot by more to be safe */
#endif
  hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
  tmp = (char *) &hdr[1];
  if ((Z_OK !=
       compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
                  sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
  {
    dlen = sizeof (my_type_map);
    memcpy (tmp, &my_type_map, sizeof (my_type_map));
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
  }
  else
  {
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
  }
  hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
  return hdr;
}
Exemplo n.º 28
0
/*
http://www.zlib.net/manual.html#compress2
int compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level);
Compresses the source buffer into the destination buffer. The level parameter has the same meaning
as in deflateInit. sourceLen is the byte length of the source buffer. Upon entry, destLen is the
total size of the destination buffer, which must be at least 0.1% larger than sourceLen plus 12 bytes.
Upon exit, destLen is the actual size of the compressed buffer.

compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there
was not enough room in the output buffer, Z_STREAM_ERROR if the level parameter is invalid.
*/
static int Netchan_ZLibCompressChunk( const uint8_t *source, unsigned long sourceLen, uint8_t *dest, unsigned long destLen,
									 int level, int wbits )
{
	int result, zlerror;

	zlerror = compress2( dest, &destLen, source, sourceLen, level );
	switch( zlerror )
	{
	case Z_OK:
		result = destLen; // returns the new length into destLen
		break;
	case Z_MEM_ERROR:
		Com_DPrintf( "ZLib data error! Z_MEM_ERROR on compress.\n" );
		result = -1;
		break;
	case Z_BUF_ERROR:
		Com_DPrintf( "ZLib data error! Z_BUF_ERROR on compress.\n" );
		result = -1;
		break;
	case Z_STREAM_ERROR:
		Com_DPrintf( "ZLib data error! Z_STREAM_ERROR on compress.\n" );
		result = -1;
		break;
	default:
		Com_DPrintf( "ZLib data error! Error code %i on compress.\n", zlerror );
		result = -1;
		break;
	}

	return result;
}
Exemplo n.º 29
0
int64_t lzbench_zlib_compress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t level, size_t, char*)
{
	uLongf zcomplen = insize;
	int err = compress2((uint8_t*)outbuf, &zcomplen, (uint8_t*)inbuf, insize, level);
	if (err != Z_OK)
		return 0;
	return zcomplen;
}
size_t zlib2_compress_entire(const size_t _input_buffer_size, char *_input_buffer,
                            const size_t _output_buffer_size, char *_output_buffer,
                            const int level)
{
    size_t compressed_output_size = _output_buffer_size;
    compress2 ((Bytef*) _output_buffer, &compressed_output_size, (Bytef*) _input_buffer, (uLong) _input_buffer_size, level);
    return compressed_output_size;
}