Ejemplo n.º 1
0
int Compression::compress(void *input, int nByte)
{
	void *output = getBuf();
	int compressingMethod = gConfig->networkCompression;
	
	uLongf CompBuffSize = 0;
	if(compressingMethod == 1)
		CompBuffSize = (uLongf)(nByte + (nByte * 0.1) + 12);
	else
		CompBuffSize = (uLongf)(nByte + (nByte/1024 * 16 ) + 16);

	unsigned char * workingMemory = NULL;
	if(compressingMethod == 2)
		workingMemory = (unsigned char*)malloc(LZO1B_MEM_COMPRESS);
	else if(compressingMethod == 3)
		workingMemory = (unsigned char*)malloc(LZO1X_1_15_MEM_COMPRESS);
	int ret = 0;
	if(nByte > 4) {
		int compressLevel = 1;
		if(compressingMethod == 1)
			ret = compress2((Bytef *) output, &CompBuffSize, (Bytef *) input, nByte, compressLevel);
		else if(compressingMethod == 2)
			ret = lzo1b_compress((Bytef *) input, nByte, (Bytef *) output, &CompBuffSize, workingMemory, compressLevel);
		else if(compressingMethod == 3)
			ret = lzo1x_1_15_compress((Bytef *) input, nByte, (Bytef *) output, &CompBuffSize, workingMemory);

		if(compressingMethod == 1) {
			if(ret != Z_OK) {
				if(ret == Z_MEM_ERROR){
					LOG("ERROR compressing: memory error\n");
				}else if(ret == Z_BUF_ERROR){
					LOG("ERROR compressing: buffer error\n");					
				}else if(ret == Z_STREAM_ERROR){
					LOG("ERROR compressing: compressLevel not (1-9), %d\n", compressLevel);
				}
			}
		}
	}
	else {
		memcpy(output, input, nByte);
		CompBuffSize = nByte;
	}
	if(compressingMethod == 2 || compressingMethod == 3)
		free(workingMemory);
	return CompBuffSize;
}
Ejemplo n.º 2
0
int64_t lzbench_lzo1x_compress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t level, size_t, char* workmem)
{
	lzo_uint lzo_complen = 0;
	int res;

    if (!workmem)
        return 0;

	switch (level)
	{
		default:
		case 1: res = lzo1x_1_compress((uint8_t*)inbuf, insize, (uint8_t*)outbuf, &lzo_complen, (void*)workmem); break;
		case 11: res = lzo1x_1_11_compress((uint8_t*)inbuf, insize, (uint8_t*)outbuf, &lzo_complen, (void*)workmem); break;
		case 12: res = lzo1x_1_12_compress((uint8_t*)inbuf, insize, (uint8_t*)outbuf, &lzo_complen, (void*)workmem); break;
		case 15: res = lzo1x_1_15_compress((uint8_t*)inbuf, insize, (uint8_t*)outbuf, &lzo_complen, (void*)workmem); break;
		case 999: res = lzo1x_999_compress((uint8_t*)inbuf, insize, (uint8_t*)outbuf, &lzo_complen, (void*)workmem); break;
    }
    
	if (res != LZO_E_OK) return 0;
		
	return lzo_complen; 
}
Ejemplo n.º 3
0
//---------------------------------------------------------------------------
LZO1X & LZO1X::compress(AutoPtrBuffer & buf,uint8_t * & p,int32_t & len)
{
  int r = 0;
  lzo_uint dst_len = 0;
  if( level_ > 0 ){
    buf.realloc(wBufPos_ + wBufPos_ / 16 + 64 + 3 + sizeof(int32_t) * 2 + (crc_ != CRCNone) * sizeof(uint32_t));
    switch( method_ ){
      case LZO1X_1    :
        r = lzo1x_1_compress(
          (const lzo_bytep) (wBuf_.ptr() + sizeof(int32_t)),
          wBufPos_,
          (lzo_bytep) (buf.ptr() + sizeof(int32_t) * 2),
          &dst_len,
          (lzo_voidp) wWrkMem_.ptr()
        );
        break;
      case LZO1X_1_11 :
        r = lzo1x_1_11_compress(
          (const lzo_bytep) (wBuf_.ptr() + sizeof(int32_t)),
          wBufPos_,
          (lzo_bytep) (buf.ptr() + sizeof(int32_t) * 2),
          &dst_len,
          (lzo_voidp) wWrkMem_.ptr()
        );
        break;
      case LZO1X_1_12 :
        r = lzo1x_1_11_compress(
          (const lzo_bytep) (wBuf_.ptr() + sizeof(int32_t)),
          wBufPos_,
          (lzo_bytep) (buf.ptr() + sizeof(int32_t) * 2),
          &dst_len,
          (lzo_voidp) wWrkMem_.ptr()
        );
        break;
      case LZO1X_1_15 :
        r = lzo1x_1_15_compress(
          (const lzo_bytep) (wBuf_.ptr() + sizeof(int32_t)),
          wBufPos_,
          (lzo_bytep) (buf.ptr() + sizeof(int32_t) * 2),
          &dst_len,
          (lzo_voidp) wWrkMem_.ptr()
        );
        break;
      case LZO1X_999  :
        r = lzo1x_999_compress_level(
          (const lzo_bytep) (wBuf_.ptr() + sizeof(int32_t)),
          wBufPos_,
          (lzo_bytep) (buf.ptr() + sizeof(int32_t) * 2),
          &dst_len,
          (lzo_voidp) wWrkMem_.ptr(),
          NULL,
          0,
          NULL,
          level_
        );
        break;
      default : assert( 0 );
    }
    assert( r == LZO_E_OK );
    if( r != LZO_E_OK )
      newObjectV1C2<Exception>(EINVAL,__PRETTY_FUNCTION__)->throwSP();
    if( dst_len >= wBufPos_ ) goto l1;
    if( optimize_ ){
      lzo_uint orig_len = wBufPos_;
      r = lzo1x_optimize(
        (lzo_bytep) (buf.ptr() + sizeof(int32_t) * 2),
        dst_len,
        (lzo_bytep) (wBuf_.ptr() + sizeof(int32_t)),
        &orig_len,
        NULL
      );
      assert( r == LZO_E_OK );
    }
    if( crc_ != CRCNone ){
      lzo_uint32 checksum = 0;
      if( crc_ == CRC32 ){
        checksum = lzo_crc32(0,NULL,0);
        checksum = lzo_crc32(checksum,buf.ptr() + sizeof(int32_t) * 2,dst_len);
      }
      else if( crc_ == ADLER32 ){
        checksum = lzo_adler32(0,NULL,0);
        checksum = lzo_adler32(checksum,buf.ptr() + sizeof(int32_t) * 2,dst_len);
      }
      *(uint32_t *)(buf.ptr() + sizeof(int32_t) * 2 + dst_len) = checksum;
    }
    dst_len += sizeof(int32_t) * 2 + (crc_ != CRCNone) * sizeof(uint32_t);
    ((int32_t *) buf.ptr())[0] = (int32_t) dst_len;
    ((int32_t *) buf.ptr())[1] = wBufPos_;
    p = buf;
    len = (int32_t) dst_len;
  }
  else {
l1: ((int32_t *) wBuf_.ptr())[0] = -int32_t(wBufPos_);
    p = wBuf_;
    len = (int32_t) (wBufPos_ + sizeof(int32_t));
  }
  return *this;
}
Ejemplo n.º 4
0
lzo_bool lzo_compress(file_t *fip, file_t *fop, const header_t *h)
{
    int r = LZO_E_OK;
    lzo_byte * const b1 = block1.mem;
    lzo_byte * const b2 = block2.mem;
    lzo_uint32 src_len = 0, dst_len = 0;
    lzo_uint32 c_adler32 = ADLER32_INIT_VALUE, d_adler32 = ADLER32_INIT_VALUE;
    lzo_uint32 c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
    lzo_int l;
    lzo_bool ok = 1;

    for (;;)
    {
        /* read a block */
        l = read_buf(fip, b1, block_size);
        src_len = (l > 0 ? l : 0);

        /* write uncompressed block size */
        write32(fop,src_len);

        /* exit if last block */
        if (src_len == 0)
            break;

        /* compute checksum of uncompressed block */
        if (h->flags & F_ADLER32_D)
            d_adler32 = lzo_adler32(ADLER32_INIT_VALUE,b1,src_len);
        if (h->flags & F_CRC32_D)
            d_crc32 = lzo_crc32(CRC32_INIT_VALUE,b1,src_len);

        x_filter(b1,src_len,h);

        /* compress */
        if (h->method == M_LZO1X_1)
            r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrkmem.mem);
#if defined(USE_LZO1X_1_15)
        else if (h->method == M_LZO1X_1_15)
            r = lzo1x_1_15_compress(b1, src_len,
                                    b2, &dst_len, wrkmem.mem);
#endif
#if defined(USE_LZO1X_999)
        else if (h->method == M_LZO1X_999)
            r = lzo1x_999_compress_level(b1, src_len,
                                         b2, &dst_len, wrkmem.mem,
                                         NULL, 0, 0, h->level);
#endif
        else
            fatal(fip,"Internal error");

#if 0
        fprintf(stderr, "%ld %ld %ld\n", (long)src_len, (long)dst_len, (long)block2.size);
#endif
        assert(dst_len <= block2.size);
        if (r != LZO_E_OK)
            fatal(fip,"Internal error - compression failed");

        /* optimize */
        if (opt_optimize && dst_len < src_len)
        {
            lzo_uint32 new_len = src_len;
            r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL);
            if (r != LZO_E_OK || new_len != src_len)
                fatal(fip,"Internal error - optimization failed");
        }

        /* write compressed block size */
        if (dst_len < src_len)
            write32(fop,dst_len);
        else
            write32(fop,src_len);

        /* write checksum of uncompressed block */
        if (h->flags & F_ADLER32_D)
            write32(fop,d_adler32);
        if (h->flags & F_CRC32_D)
            write32(fop,d_crc32);

        /* write checksum of compressed block */
        if (dst_len < src_len && (h->flags & F_ADLER32_C))
        {
            c_adler32 = lzo_adler32(ADLER32_INIT_VALUE,b2,dst_len);
            write32(fop,c_adler32);
        }
        if (dst_len < src_len && (h->flags & F_CRC32_C))
        {
            c_crc32 = lzo_crc32(CRC32_INIT_VALUE,b2,dst_len);
            write32(fop,c_crc32);
        }

        /* write compressed block data */
        if (dst_len < src_len)
            write_buf(fop,b2,dst_len);
        else
            write_buf(fop,b1,src_len);
    }

    return ok;
}