Esempio n. 1
0
int main(int argc, char *argv[])
{
    lzo_bytep block;
    lzo_uint block_len;
    lzo_uint32 adler, crc;

    if (argc < 0 && argv == NULL)   /* avoid warning about unused args */
        return 0;

    if (lzo_init() != LZO_E_OK)
    {
        printf("lzo_init() failed !!!\n");
        return 4;
    }

/* prepare the block */
    block_len = 128 * 1024L;
    block = (lzo_bytep) lzo_malloc(block_len);
    if (block == NULL)
    {
        printf("out of memory\n");
        return 3;
    }
    lzo_memset(block, 0, block_len);

/* adler32 checksum */
    adler = lzo_adler32(0, NULL, 0);
    adler = lzo_adler32(adler, block, block_len);
    if (adler != 0x001e0001UL)
    {
        printf("adler32 checksum error !!! (0x%08lx)\n", (long) adler);
        return 2;
    }

/* crc32 checksum */
    crc = lzo_crc32(0, NULL, 0);
    crc = lzo_crc32(crc, block, block_len);
    if (crc != 0x7ee8cdcdUL)
    {
        printf("crc32 checksum error !!! (0x%08lx)\n", (long) crc);
        return 1;
    }

    lzo_free(block);
    printf("Checksum test passed.\n");
    return 0;
}
Esempio n. 2
0
static int lzo_wwrite_block(const char *buffer, off_t len, struct buffer_t *outbuf)
{
	char b2[MAX_BUFFER_SIZE];
	int err;
	lzo_uint dst_len;
	char scratch[LZO1X_1_MEM_COMPRESS];

	outbuf->offset=0;

	memset(scratch,0,sizeof(scratch));
	err=lzo1x_1_compress((void*)buffer, len, 
			(void*)b2, &dst_len, 
			scratch);

	switch(err) {
		case LZO_E_OK:
			break;
		case LZO_E_ERROR:
			return -EINVAL; /* WTF? */
		case LZO_E_OUT_OF_MEMORY:
			return -ENOMEM; /* Uh oh */
		case LZO_E_NOT_COMPRESSIBLE:
			return -EINVAL; /* Claimed not to be used, dunno what we'll do */
		case LZO_E_INPUT_OVERRUN:
			return -EINVAL;  /* Can't happen on compress? */
		case LZO_E_OUTPUT_OVERRUN:
			return -ENOMEM;
		case LZO_E_LOOKBEHIND_OVERRUN:
			return -EINVAL;
		case LZO_E_EOF_NOT_FOUND:
			return -ENOENT; /* Can't happen on compress? */
		case LZO_E_INPUT_NOT_CONSUMED:
			return -EINVAL;
		case LZO_E_NOT_YET_IMPLEMENTED:
			return -ENOSYS;
		default:
			fprintf(stderr,"Unknown lzo error %d\n",err);
			return -EINVAL;
	}

	write32(outbuf, len); /* Original length */
	write32(outbuf, min((uint32_t)len,(uint32_t)dst_len));
	/* CRC32 of the uncompressed buffer */
#if 0
	write32(outbuf, lzo_crc32(CRC32_INIT_VALUE, (void*)buffer, len));
#endif
	write32(outbuf, 
		lzo_adler32(ADLER32_INIT_VALUE, (const void*)buffer, len));
	write_buf(outbuf, b2, dst_len);

	/* Return the number of bytes compressed */
	return len;
}
Esempio n. 3
0
static
int do_decompress ( const compress_t *c, lzo_decompress_t d,
                    const lzo_byte *src, lzo_uint  src_len,
                          lzo_byte *dst, lzo_uintp dst_len )
{
	int r = -100;

   	if (c && d && WRK_LEN >= c->mem_decompress)
	{
		unsigned char random_byte = (unsigned char) src_len;
		init_mem_checker(wrkmem,_wrkmem,c->mem_decompress,random_byte);

		if (opt_dict && c->decompress_dict_safe)
			r = c->decompress_dict_safe(src,src_len,dst,dst_len,wrkmem,dict,dict_len);
		else
			r = d(src,src_len,dst,dst_len,wrkmem);

		if (check_mem(wrkmem,_wrkmem,c->mem_decompress,random_byte) != 0)
			printf("WARNING: wrkmem overwrite error (decompress) !!!\n");
	}

	if (r == 0 && opt_compute_adler32)
	{
		lzo_uint32 adler;
		adler = lzo_adler32(0, NULL, 0);
		adler = lzo_adler32(adler, dst, *dst_len);
		adler_out = adler;
	}
	if (r == 0 && opt_compute_crc32)
	{
		lzo_uint32 crc;
		crc = lzo_crc32(0, NULL, 0);
		crc = lzo_crc32(crc, dst, *dst_len);
		crc_out = crc;
	}

	return r;
}
Esempio n. 4
0
iow_t *lzo_wopen(iow_t *child, int compress_level)
{
	const int opt_filter = 0;
	int flags;
	iow_t *iow;
	struct buffer_t buffer;
	buffer.offset=0;
	int i;

	if (!child)
		return NULL;

	if (lzo_init() != LZO_E_OK) {
		/* Fail */
		return NULL;
	}

        /* Compress level is useless for LZO, but getting UNUSED into here
         * is more trouble than it is worth so this check will at least
         * stop us from getting warnings about it.
         */
        if (compress_level < 0)
                return NULL;

	iow = malloc(sizeof(iow_t));
	iow->source = &lzo_wsource;
	iow->data = malloc(sizeof(struct lzow_t));

	DATA(iow)->child = child;
	DATA(iow)->err = ERR_OK;

	flags = 0;
	flags |= F_OS_UNIX & F_OS_MASK;	/* Operating System */
	flags |= F_CS_NATIVE & F_CS_MASK;	/* Character Set */
	flags |= F_ADLER32_D; /* We adler32 the uncompressed data */
	/* flags |= F_STDIN; */
	/* flags |= F_STDOUT */
	/* flags |= F_MULTIPART; */
	/* flags |= F_H_CRC32; */

	write_buf(&buffer, lzop_magic, sizeof(lzop_magic));
	write16(&buffer, 0x1010 &0xFFFF); /* version: pretend to be LZOP version 0x1010 from lzop's version.h */
	write16(&buffer, lzo_version() & 0xFFFF); /* libversion */
	write16(&buffer, opt_filter ? 0x0950 : 0x0940); /* version needed to extract */
	write8(&buffer, M_LZO1X_1);	/* method */
	write8(&buffer, 5); /* level */
	write32(&buffer, flags); /* flags */
	/* if (flags & F_H_FILTER) 
		write32(iow, opt_filter); 
	*/ 
	write32(&buffer, 0x600); /* mode: We assume traces may be sensitive */
	write32(&buffer, time(NULL)); /* mtime */
	write32(&buffer, 0); /* GMTdiff */

	/* Length, filename */
	write8(&buffer, strlen("compresseddata"));
	write_buf(&buffer, "compresseddata",strlen("compresseddata"));

	if (flags & F_H_CRC32) {
		write32(&buffer, 
			lzo_crc32(CRC32_INIT_VALUE, 
				(const void*)buffer.buffer+sizeof(lzop_magic), 
				buffer.offset-sizeof(lzop_magic)));
	}
	else {
		uint32_t chksum=lzo_adler32(
			ADLER32_INIT_VALUE, 
			(const void *)buffer.buffer+sizeof(lzop_magic), 
			buffer.offset-sizeof(lzop_magic));
		write32(&buffer, chksum);
	}

	wandio_wwrite(DATA(iow)->child,
		buffer.buffer,
		buffer.offset);

	/* Set up the thread pool -- one thread per core */
	DATA(iow)->threads = min((uint32_t)sysconf(_SC_NPROCESSORS_ONLN),
			use_threads);
	DATA(iow)->thread = malloc(
			sizeof(struct lzothread_t) * DATA(iow)->threads);
	DATA(iow)->next_thread = 0;
	for(i=0; i<DATA(iow)->threads; ++i) {
		pthread_cond_init(&DATA(iow)->thread[i].in_ready, NULL);
		pthread_cond_init(&DATA(iow)->thread[i].out_ready, NULL);
		pthread_mutex_init(&DATA(iow)->thread[i].mutex, NULL);
		DATA(iow)->thread[i].closing = false;
		DATA(iow)->thread[i].num = i;
		DATA(iow)->thread[i].state = EMPTY;
		DATA(iow)->thread[i].inbuf.offset = 0;

		pthread_create(&DATA(iow)->thread[i].thread, 
				NULL,
				lzo_compress_thread,
				(void*)&DATA(iow)->thread[i]);
	}

	return iow;
}
Esempio n. 5
0
static
int do_file ( int method, const char *file_name,
              int c_loops, int d_loops,
              lzo_uint32 *p_adler, lzo_uint32 *p_crc )
{
	int r;
	const compress_t *c;
	lzo_decompress_t decompress;
	lzo_uint l;
	lzo_uint32 adler, crc;
	char method_name[32];
	const char *n;
	const int t_loops = 1;
	lzo_byte *saved_dict = dict;
	lzo_uint saved_dict_len = dict_len;

	adler_in = adler_out = 0;
	crc_in = crc_out = 0;
	if (p_adler)
		*p_adler = 0;
	if (p_crc)
		*p_crc = 0;

	c = info(method,NULL);
	if (c == NULL || c->name == NULL || c->compress == NULL)
		return EXIT_INTERNAL;
	decompress = get_decomp_info(c,&n);
	if (!decompress || n == NULL || WRK_LEN < c->mem_decompress)
		return EXIT_INTERNAL;
	strcpy(method_name,c->name);
	strcat(method_name,n);

	if (c_loops < 1)  c_loops = 1;
	if (d_loops < 1)  d_loops = 1;

	fflush(stdout); fflush(stderr);

	/* read the whole file */
	r = load_file(file_name,&l);
	if (r != 0)
		return r;

	/* compute some checksums */
	adler = lzo_adler32(0, NULL, 0);
	adler = lzo_adler32(adler, data, l);
	if (p_adler)
		*p_adler = adler;
	crc = lzo_crc32(0, NULL, 0);
	crc = lzo_crc32(crc, data, l);
	if (p_crc)
		*p_crc = crc;

#if 0 && defined(ALG_ZLIB)
	{
		uLong x;
		x = adler32(0, Z_NULL, 0);
		x = adler32(x, data, l);
		if (x != adler)
			return EXIT_LZO_ERROR;
		x = crc32(0, Z_NULL, 0);
		x = crc32(x, data, l);
		if (x != crc)
			return EXIT_LZO_ERROR;
	}
#endif

	if (opt_dict && dict)
		if (opt_max_dict_len <= 0 && dict_len > l)
		{
			if (opt_max_dict_len == -1)
				dict += dict_len - l;		/* use end of dictionary */
			dict_len = l;
		}

	if (opt_verbose >= 2)
	{
		printf("File %s: %lu bytes   (0x%08lx, 0x%08lx)\n",
		        file_name, (long) l, (long) adler, (long) crc);
		printf("  compressing %lu bytes (%d/%d/%d loops, %lu block-size)\n",
		        (long) l, t_loops, c_loops, d_loops, (long) opt_block_size);
		printf("  %s\n", method_name);
	}

	r = process_file(c, decompress, method_name, file_name, l,
	                 t_loops, c_loops, d_loops);

	dict = saved_dict;
	dict_len = saved_dict_len;

	return r;
}
Esempio n. 6
0
static
int do_compress ( const compress_t *c,
                  const lzo_byte *src, lzo_uint  src_len,
                        lzo_byte *dst, lzo_uintp dst_len )
{
	int r = -100;

	if (c && c->compress && WRK_LEN >= c->mem_compress)
	{
#if defined(__LZO_CHECKER)
		/* malloc a block of the exact size to detect any overrun */
		lzo_byte *w = wrkmem;
		if (c->mem_compress > 0)
		{
			wrkmem = malloc(c->mem_compress);
			/* must initialize memory - fill in garbage */
			{
				lzo_uint32 i;
				unsigned char random_byte = (unsigned char) src_len;
				random_byte |= 1;
				for (i = 0; i < c->mem_compress; i++, random_byte += 2)
					wrkmem[i] = random_byte;
			}
		}
		else
			wrkmem = NULL;
#else
		unsigned char random_byte = (unsigned char) src_len;
		init_mem_checker(wrkmem,_wrkmem,c->mem_compress,random_byte);
#endif

		if (opt_dict && c->compress_dict)
			r = c->compress_dict(src,src_len,dst,dst_len,wrkmem,dict,dict_len);
		else
			r = c->compress(src,src_len,dst,dst_len,wrkmem);

#if defined(__LZO_CHECKER)
		if (wrkmem)
			free(wrkmem);
		wrkmem = w;
#else
		if (check_mem(wrkmem,_wrkmem,c->mem_compress,random_byte) != 0)
			printf("WARNING: wrkmem overwrite error (compress) !!!\n");
#endif
	}

	if (r == 0 && opt_compute_adler32)
	{
		lzo_uint32 adler;
		adler = lzo_adler32(0, NULL, 0);
		adler = lzo_adler32(adler, src, src_len);
		adler_in = adler;
	}
	if (r == 0 && opt_compute_crc32)
	{
		lzo_uint32 crc;
		crc = lzo_crc32(0, NULL, 0);
		crc = lzo_crc32(crc, src, src_len);
		crc_in = crc;
	}

	return r;
}
Esempio n. 7
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;
}
Esempio n. 8
0
//---------------------------------------------------------------------------
uint32_t crc32(uint32_t c,const void * buf,uintptr_t len)
{
  return lzo_crc32(c,(const lzo_bytep) buf,(lzo_uint) len);
}
Esempio n. 9
0
//---------------------------------------------------------------------------
LZO1X & LZO1X::decompress(AutoPtrBuffer & buf)
{
  union {
    int32_t * i32;
    uint8_t * i8;
  };
  i8 = buf;
  int32_t a = (int32_t) (i32[0] - sizeof(int32_t) * 2 - (crc_ != CRCNone) * sizeof(uint32_t));
  if( crc_ != CRCNone && a > 0 ){
    lzo_uint32 checksum = 0;
    if( crc_ == CRC32 ){
      checksum = lzo_crc32(0,NULL,0);
      checksum = lzo_crc32(checksum,i8 + sizeof(int32_t) * 2,a);
    }
    else if( crc_ == ADLER32 ){
      checksum = lzo_adler32(0,NULL,0);
      checksum = lzo_adler32(checksum,i8 + sizeof(int32_t) * 2,a);
    }
    if( *(uint32_t *) (i8 + sizeof(int32_t) * 2 + a) != checksum ) a = -a;
  }
  if( a <= 0 || i32[1] <= 0 )
    newObjectV1C2<Exception>(EINVAL,__PRETTY_FUNCTION__)->throwSP();
  rBufSize(i32[1]);
  lzo_uint srcLen = a, dst_len = i32[1];
#if HAVE_LZO1X_DECOMPRESS_ASM_FAST_SAFE
  int r = lzo1x_decompress_asm_fast_safe(
    (const lzo_bytep) (i8 + sizeof(int32_t) * 2),
    srcLen,
    (lzo_bytep) rBuf_.ptr(),
    &dst_len,
    NULL
  );
#elif HAVE_LZO1X_DECOMPRESS_ASM_SAFE
  int r = lzo1x_decompress_asm_safe(
    (const lzo_bytep) (i8 + sizeof(int32_t) * 2),
    srcLen,
    (lzo_bytep) rBuf_.ptr(),
    &dst_len,
    NULL
  );
#elif HAVE_LZO1X_DECOMPRESS_SAFE
  int r = lzo1x_decompress_safe(
    (const lzo_bytep) (i8 + sizeof(int32_t) * 2),
    srcLen,
    (lzo_bytep) rBuf_.ptr(),
    &dst_len,
    NULL
  );
#elif HAVE_LZO1X_DECOMPRESS_ASM_FAST
  int r = lzo1x_decompress_asm_fast(
    (const lzo_bytep) (i8 + sizeof(int32_t) * 2),
    srcLen,
    (lzo_bytep) rBuf_.ptr(),
    &dst_len,
    NULL
  );
#elif HAVE_LZO1X_DECOMPRESS_ASM
  int r = lzo1x_decompress_asm(
    (const lzo_bytep) (i8 + sizeof(int32_t) * 2),
    srcLen,
    (lzo_bytep) rBuf_.ptr(),
    &dst_len,
    NULL
  );
//#elif HAVE_LZO1X_DECOMPRESS
#else
  int r = lzo1x_decompress(
    (const lzo_bytep) (i8 + sizeof(int32_t) * 2),
    srcLen,
    (lzo_bytep) rBuf_.ptr(),
    &dst_len,
    NULL
  );
//#else
//#error broken lzo library
#endif
  assert( r == LZO_E_OK && dst_len == rBufSize_ );
  if( r != LZO_E_OK || dst_len != rBufSize_ )
    newObjectV1C2<Exception>(EINVAL,__PRETTY_FUNCTION__)->throwSP();
  return *this;
}
Esempio n. 10
0
lzo_bool lzo_decompress(file_t *fip, file_t *fop,
                        const header_t *h, lzo_bool skip)
{
    int r;
    lzo_uint src_len, dst_len;
    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_bool ok = 1;
    lzo_bool use_seek;
    memblock_t * const block = &block2;
    lzo_byte * b1;
    lzo_byte * const b2 = block->mem;

    use_seek = skip || opt_cmd == CMD_LIST || opt_cmd == CMD_LS ||
               opt_cmd == CMD_INFO;

    for (;;)
    {
        lzo_byte *dst;

        /* read uncompressed block size */
        read32(fip,&dst_len);

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

        /* error if split file */
        if (dst_len == 0xffffffffL)
        {
            /* should not happen - not yet implemented */
            error(fip,"this file is a split " PACKAGE " file");
            ok = 0;
            break;
        }

        if (dst_len > MAX_BLOCK_SIZE)
        {
            error(fip, PACKAGE " file corrupted");
            ok = 0;
            break;
        }

        /* read compressed block size */
        read32(fip,&src_len);
        if (src_len <= 0 || src_len > dst_len)
        {
            error(fip, PACKAGE " file corrupted");
            ok = 0;
            break;
        }

        if (dst_len > BLOCK_SIZE)
        {
            fatal(fip,"block size too small -- recompile " PACKAGE);
            ok = 0;
            break;
        }
        if (dst_len > block_size)
        {
            /* should not happen - not yet implemented */
            fatal(fip,"block size too small -- use option `--blocksize'");
            ok = 0;
            break;
        }
        assert(block->size >= src_len);

        /* read checksum of uncompressed block */
        if (h->flags & F_ADLER32_D)
            read32(fip,&d_adler32);
        if (h->flags & F_CRC32_D)
            read32(fip,&d_crc32);

        /* read checksum of compressed block */
        if (h->flags & F_ADLER32_C)
        {
            if (src_len < dst_len)
                read32(fip,&c_adler32);
            else
            {
                assert(h->flags & F_ADLER32_D);
                c_adler32 = d_adler32;
            }
        }
        if (h->flags & F_CRC32_C)
        {
            if (src_len < dst_len)
                read32(fip,&c_crc32);
            else
            {
                assert(h->flags & F_CRC32_D);
                c_crc32 = d_crc32;
            }
        }

        /* read the block */
        b1 = block->mem + block->size - src_len;
        if (use_seek && fip->fd != STDIN_FILENO)
        {
            if (lseek(fip->fd, src_len, SEEK_CUR) == -1)
                read_error(fip);
        }
        else
        {
            if (read_buf(fip, b1, src_len) != (lzo_int) src_len)
                read_error(fip);
        }

        fip->bytes_processed += src_len;
        if (use_seek)
        {
            fop->bytes_processed += dst_len;
            continue;
        }
        assert(block->size >= MAX_COMPRESSED_SIZE(dst_len));

        /* verify checksum of compressed block */
        if (opt_checksum && (h->flags & F_ADLER32_C))
        {
            lzo_uint32 c;
            c = lzo_adler32(ADLER32_INIT_VALUE,b1,src_len);
            if (c != c_adler32)
            {
                error(fip,"Checksum error (" PACKAGE " file corrupted)");
                ok = 0;
                break;
            }
        }
        if (opt_checksum && (h->flags & F_CRC32_C))
        {
            lzo_uint32 c;
            c = lzo_crc32(CRC32_INIT_VALUE,b1,src_len);
            if (c != c_crc32)
            {
                error(fip,"Checksum error (" PACKAGE " file corrupted)");
                ok = 0;
                break;
            }
        }

        if (src_len < dst_len)
        {
            lzo_uint32 d = dst_len;

            /* decompress */
            if (opt_decompress_safe)
                r = lzo1x_decompress_safe(b1,src_len,b2,&d,NULL);
            else
                r = lzo1x_decompress(b1,src_len,b2,&d,NULL);

            if (r != LZO_E_OK || dst_len != d)
            {
                error(fip,"Compressed data violation");
#if 0
                fprintf(stderr,"%d %ld %ld\n", r, (long)dst_len, (long)d);
#endif
                ok = 0;
                break;
            }
            dst = b2;
        }
        else
        {
            assert(dst_len == src_len);
            dst = b1;
        }

        x_filter(dst,dst_len,h);

        /* verify checksum of uncompressed block */
        if (opt_checksum && (h->flags & F_ADLER32_D))
        {
            lzo_uint32 c;
            c = lzo_adler32(ADLER32_INIT_VALUE,dst,dst_len);
            if (c != d_adler32)
            {
                error(fip,"Checksum error");
                ok = 0;
                break;
            }
        }
        if (opt_checksum && (h->flags & F_CRC32_D))
        {
            lzo_uint32 c;
            c = lzo_crc32(CRC32_INIT_VALUE,dst,dst_len);
            if (c != d_crc32)
            {
                error(fip,"Checksum error");
                ok = 0;
                break;
            }
        }

        /* write uncompressed block data */
        write_buf(fop,dst,dst_len);
        fop->bytes_processed += dst_len;
    }

    return ok;
}
Esempio n. 11
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;
}