Exemple #1
0
static void read_dict(const char *file_name)
{
	FILE *f;

	dict_len = 0;
	f = fopen(file_name,"rb");
	if (f)
	{
		dict_len = lzo_fread(f,dict,DICT_LEN);
		fclose(f);
		dict_adler32 = lzo_adler32(0,NULL,0);
		dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
	}
}
Exemple #2
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;
}
Exemple #3
0
gboolean
lzo_init_compress_checksum(gulong* checksum)
{
	lzo_uint32 checksum32 = 0;
	checksum32 = lzo_adler32(0, NULL, 0);
	*checksum = checksum32;
	return TRUE;
}
Exemple #4
0
static void init_default_dict(void)
{
	lzo_uint i, j, d, dd;

	dict_len = DICT_LEN;
	lzo_memset(dict,0,dict_len);

	/* this default dictionary does not provide good contexts... */
	dd = dict_len;
	if (dd >= 16 * 256)
	{
		dd -= 16 * 256;
		for (i = 0, d = dd; i < 256; i++)
			for (j = 0; j < 16; j++)
				dict[d++] = (unsigned char) i;
	}

	dict_adler32 = lzo_adler32(0,NULL,0);
	dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
}
Exemple #5
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;
}
Exemple #6
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;
}
Exemple #7
0
static VALUE lzoruby_adler32(VALUE self, VALUE v_adler, VALUE v_buf) {
  lzo_uint32 adler;
  lzo_bytep buf;
  lzo_uint len;

  Check_Type(v_adler, T_FIXNUM);
  Check_Type(v_buf, T_STRING);
  adler = FIX2LONG(v_adler);
  buf = RSTRING_PTR(v_buf);
  len = RSTRING_LEN(v_buf);

  adler = lzo_adler32(adler, buf, len);

  return LONG2FIX(adler);
}
Exemple #8
0
int
lzo_write_compress_header(FILE *file, lzo_uint32 blocksize, gulong *checksum, lzo_uint32 *compressed_size)
{
	lzo_uint32 flags = LZO_FLAG_CHECKSUM;       /* do compute a checksum */
	char method = LZO_METHOD;
	char level = LZO_LEVEL;
	gsize written = 0;

	GByteArray *headers = NULL;
	headers = g_byte_array_new();	

#define HEADER_APPEND(V, S) g_byte_array_append(headers,(guint8*)V, S);

	headers = HEADER_APPEND(&lzo_magic, sizeof(lzo_magic)); /* char[7] */
	headers = HEADER_APPEND(&flags, sizeof(flags)); /* guint32 */
	headers = HEADER_APPEND(&method, sizeof(method)); /* char */
	headers = HEADER_APPEND(&level, sizeof(level)); /* char */
	headers = HEADER_APPEND(&blocksize, sizeof(blocksize)); /* guint32 */

	written = fwrite(headers->data, headers->len, 1, file);

	if (written != 1) {
		ERROR("Failed to write headers\n");
		if(headers)
			g_byte_array_free(headers, TRUE);
		return 1;
	}

	*compressed_size = *compressed_size + headers->len;
	lzo_uint32 cheksum32 = 0;
	cheksum32 = lzo_adler32(0, NULL, 0);
	*checksum = cheksum32;
	if(headers)
		g_byte_array_free(headers, TRUE);
	return 0;
}
Exemple #9
0
static int
_fill_decompressed_buffer(struct compressed_chunk_s * chunk, gsize to_skip)
{
	DEBUG("_fill_decompressed_buffer: START\n");
	gsize nb_read;
	gsize in_len;
	gulong out_len;
	gsize total_skipped = 0;
	int r;

	if(chunk->buf)
		g_free(chunk->buf);

	chunk->buf = NULL;
	chunk->buf_len = 0;
	chunk->buf_offset = 0;
	chunk->data_len = 0;

	while(1) {
		/* read uncompressed size */
		nb_read = 0;
		nb_read = fread(&out_len, sizeof(out_len), 1, chunk->fd);
		if (nb_read != 1) {
			DEBUG("Failed to read compressed chunk size");
			return -1;
		}
		/* exit if last block (EOF marker) */
		if(out_len == 0) {
			return 0;
		}

		/* read compressed size */
		in_len = 0;
		nb_read = 0;
		nb_read = fread(&in_len, sizeof(in_len), 1, chunk->fd);

		if (nb_read != 1) {
			return -1;
		}
		/* check if we are in good block */
		if(to_skip < total_skipped + out_len) {
			/* data in this block */
			chunk->data_len = out_len;
			chunk->buf_offset = to_skip - total_skipped;
			break;
		} else {
			/* don't need to uncompress this block, go to the next */
			total_skipped += out_len;
			if(fseek(chunk->fd, in_len, SEEK_CUR)) {
				/* fseek issue */
				return -1;
			}
		}
	}

	/* Consider the "to_skip" bytes already read */
	chunk->read += to_skip;

	DEBUG("_fill_decompressed_buffer: current block compressed size (read from file): %"G_GSIZE_FORMAT, in_len);
	DEBUG("_fill_decompressed_buffer: block_size = %u", (uint)chunk->block_size);

	/* sanity check of the size values */
	if (in_len > chunk->block_size || chunk->data_len > chunk->block_size ||
			in_len == 0 || in_len > chunk->data_len){
		DEBUG("_fill_decompressed_buffer: block size error - data corrupted");
		r = -1;
		goto err;
	}
	DEBUG("_fill_decompressed_buffer: ok, data not corrupted, it's time to work");

	/* Manage the case of uncompressed data */
	if (in_len == chunk->data_len) {
		chunk->buf_len = chunk->data_len;
		chunk->buf = g_malloc0(chunk->buf_len);
		nb_read = 0;
		nb_read = fread(chunk->buf, chunk->buf_len, 1, chunk->fd);
		if (nb_read != 1) {
			DEBUG("Failed to read uncompressed block");
			r = -1;
			goto err;
		}
	}
	else { /* in_len < chunk->data_len */
		lzo_bytep in;
		lzo_uint new_len;

		chunk->buf_len = chunk->data_len;
		chunk->buf = g_malloc0(chunk->buf_len);

		DEBUG("_fill_uncompressed_buffer: before lzo1x_decompress_safe"
				" (input=%u max_out=%u expected=%u)\n",
				(uint)in_len, (uint)chunk->buf_len, (uint)chunk->data_len);

		in = g_malloc0(in_len);
		nb_read = 0;
		nb_read = fread(in, in_len, 1, chunk->fd);
		if (nb_read != 1) {
			g_free(in);
			DEBUG("Failed to read compressed block");
			r = -1;
			goto err;
		}

		/* decompress - use safe decompressor as data might be corrupted
		 * during a file transfer */
		new_len = chunk->buf_len;
		r = lzo1x_decompress_safe(in, in_len, chunk->buf, &new_len, NULL);
		g_free(in);

		if (r != LZO_E_OK || new_len != chunk->data_len) {
			DEBUG("_fill_uncompressed_buffer: compressed data violation"
					" (input=%u max_out=%u expected=%u got=%u)\n",
					(uint)in_len, (uint)chunk->buf_len, (uint)chunk->data_len, (uint)new_len);
			r = -1;
			goto err;
		}

		DEBUG("_fill_uncompressed_buffer: afetr lzo1x_decompress_safe"
				" (input=%u max_out=%u expected=%u got=%u)\n",
				(uint)in_len, (uint)chunk->buf_len, (uint)chunk->data_len, (uint)new_len);
	}

	/* update checksum */
	if (chunk->flags & LZO_FLAG_CHECKSUM) {
		DEBUG("data used for checksum : %u\n", (uint)chunk->data_len);
		lzo_uint32 checksum32 = 0;
		checksum32 = chunk->checksum;
		checksum32 = lzo_adler32(checksum32, chunk->buf, chunk->data_len);
		chunk->checksum = checksum32;
		DEBUG("_fill_decompressed_buffer: checksum updated\n");
	}

	r = 1;
err:
	return r;
}
Exemple #10
0
int
lzo_compressed_chunk_init(struct compressed_chunk_s *chunk, const gchar *path)
{
	int r = 0;	
	gsize nb_read;
	guint8 headers[HEADER_SIZE];
	GError * error = NULL;
	struct chunk_textinfo_s cti;
	DEBUG("compressed_chunk_init: START\n");

	if(!chunk)
		ERROR("Invalid parameter : %p\n", chunk);

	memset(headers, 0, sizeof(headers));
	memset(&cti, 0, sizeof(cti));
	
	/* Get chunk uncompressed size in his attr */
	if (!get_chunk_info_in_attr(path, &error, &cti)){
		DEBUG("Failed to get chunk info in attr : %s\n", error->message);
		g_clear_error(&error);
		return 1;
	}

	chunk->uncompressed_size = g_strdup(cti.size);
	DEBUG("size get in attr = %s", chunk->uncompressed_size);

	/* Read magic header & flags */
	/* place block at top of buffer */
	/*
 	 * Step 1: check magic header, read flags & block size, init checksum
 	*/

	chunk->fd = fopen(path, "r");

	if (!chunk->fd) {
		r = 1;
		goto err;
	}
	
	DEBUG("compressed_chunk_init: compressed chunk open");

	/* compile for read all header info in one call */
	nb_read = 0;
	nb_read = fread(headers, sizeof(headers), 1, chunk->fd);
	if (nb_read != 1) {
		DEBUG("Failed to read headers from chunk file");
		r = 2;
		goto err;
	}

	do { /* extract all headers */
#define GETNEXTPTR(Res,Ptr,Type) do { Res = *((Type *)Ptr); Ptr = Ptr + sizeof(Type); } while (0)
		guint32 bsize32 = 0;

		char *ptr = (char*)headers + sizeof(lzo_magic);
		GETNEXTPTR(chunk->flags, ptr, lzo_uint32);
		GETNEXTPTR(chunk->method, ptr, char);
		GETNEXTPTR(chunk->level, ptr, char);
		GETNEXTPTR(bsize32, ptr, lzo_uint32);
		chunk->block_size = bsize32;
	} while (0);

	if (memcmp(headers, lzo_magic, sizeof(lzo_magic)) != 0) {
		r = 4;
		goto err;
	}
    	if (chunk->method != 1) {
        	r = 5;
        	goto err;
    	}
    	if (chunk->block_size < 1024 || chunk->block_size > 8*1024*1024L){
        	r = 6;
        	goto err;
    	}

	DEBUG("ck.block_size : %"G_GUINT32_FORMAT"\n", chunk->block_size);

	chunk->checksum = lzo_adler32(0, NULL, 0);

	DEBUG("chunk->uncompressed_size = %s\n", chunk->uncompressed_size);

	r=0;

err:
	if(error)
		g_clear_error(&error);
	chunk_textinfo_free_content(&cti);
	
	return r;
}
Exemple #11
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;
}
Exemple #12
0
int main(int argc, char *argv[])
{
	int r;
	int lazy;
	const int max_try_lazy = 5;
	const lzo_uint big = 65536L;	/* can result in very slow compression */
	const lzo_uint32 flags = 0x1;

	lzo_byte *in;
	lzo_uint in_len;

	lzo_byte *out;
	lzo_uint out_len = 0;

	lzo_byte *wrkmem;
	lzo_uint wrk_len;

	lzo_uint best_len;
	int best_compress = -1;
	int best_lazy = -1;

	lzo_uint orig_len;
	lzo_uint32 uncompressed_checksum;
	lzo_uint32 compressed_checksum;

	FILE *f;
	const char *progname = NULL;
	const char *in_name = NULL;
	const char *out_name = NULL;
	long l;


#if defined(__EMX__)
	_response(&argc,&argv);
	_wildcard(&argc,&argv);
#endif

	printf("\nLZO real-time data compression library (v%s, %s).\n",
	        lzo_version_string(), lzo_version_date());
	printf("Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer\n\n");

	progname = argv[0];
	if (argc < 2 || argc > 3)
	{
		printf("usage: %s file [output-file]\n", progname);
		exit(1);
	}
 	in_name = argv[1];
 	out_name = (argc > 2) ? argv[2] : NULL;

/*
 * Step 1: initialize the LZO library
 */
	if (lzo_init() != LZO_E_OK)
	{
		printf("lzo_init() failed !!!\n");
		exit(1);
	}

/*
 * Step 2: allocate the work-memory
 */
 	wrk_len = 1;
#ifdef USE_LZO1X
 	if (wrk_len < LZO1X_999_MEM_COMPRESS)
	 	wrk_len = LZO1X_999_MEM_COMPRESS;
#endif
#ifdef USE_LZO1Y
 	if (wrk_len < LZO1Y_999_MEM_COMPRESS)
	 	wrk_len = LZO1Y_999_MEM_COMPRESS;
#endif
	wrkmem = (lzo_bytep) lzo_malloc(wrk_len);
	if (wrkmem == NULL)
	{
		printf("%s: out of memory\n", progname);
		exit(1);
	}

/*
 * Step 3: open the input file
 */
 	f = fopen(in_name,"rb");
	if (f == NULL)
	{
		printf("%s: cannot open file %s\n", progname, in_name);
		exit(1);
	}
	fseek(f,0,SEEK_END);
	l = ftell(f);
	fseek(f,0,SEEK_SET);
	if (l <= 0)
	{
		printf("%s: %s: empty file\n", progname, in_name);
		fclose(f);
		exit(1);
	}
	in_len = (lzo_uint) l;
	best_len = in_len;

/*
 * Step 4: allocate compression buffers and read the file
 */
	in = (lzo_bytep) lzo_malloc(in_len);
	out = (lzo_bytep) lzo_malloc(in_len + in_len / 64 + 16 + 3);
	if (in == NULL || out == NULL)
	{
		printf("%s: out of memory\n", progname);
		exit(1);
	}
	in_len = lzo_fread(f,in,in_len);
	printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
	fclose(f);

/*
 * Step 5: compute a checksum of the uncompressed data
 */
 	uncompressed_checksum = lzo_adler32(0,NULL,0);
 	uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);

/*
 * Step 6a: compress from `in' to `out' with LZO1X-999
 */
#ifdef USE_LZO1X
 	for (lazy = 0; lazy <= max_try_lazy; lazy++)
	{
		r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
		                                NULL, 0, 0,
		                                lazy, big, big, big, big, flags);
		if (r != LZO_E_OK)
		{
			/* this should NEVER happen */
			printf("internal error - compression failed: %d\n", r);
			exit(1);
		}
		printf("LZO1X-999: lazy =%2d: %8lu -> %8lu\n",
				lazy, (long) in_len, (long) out_len);
		if (out_len < best_len)
		{
			best_len = out_len;
			best_lazy = lazy;
			best_compress = 1;		/* LZO1X-999 */
		}
	}
#endif /* USE_LZO1X */

/*
 * Step 6b: compress from `in' to `out' with LZO1Y-999
 */
#ifdef USE_LZO1Y
 	for (lazy = 0; lazy <= max_try_lazy; lazy++)
	{
		r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
		                                NULL, 0, 0,
		                                lazy, big, big, big, big, flags);
		if (r != LZO_E_OK)
		{
			/* this should NEVER happen */
			printf("internal error - compression failed: %d\n", r);
			exit(1);
		}
		printf("LZO1Y-999: lazy =%2d: %8lu -> %8lu\n",
				lazy, (long) in_len, (long) out_len);
		if (out_len < best_len)
		{
			best_len = out_len;
			best_lazy = lazy;
			best_compress = 2;		/* LZO1Y-999 */
		}
	}
#endif /* USE_LZO1Y */

/*
 * Step 7: check if compressible
 */
 	if (best_len >= in_len)
	{
		printf("This file contains incompressible data.\n");
		return 0;
	}

/*
 * Step 8: compress data again using the best compressor found
 */
	if (best_compress == 1)
		r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
		                                NULL, 0, 0,
		                                best_lazy, big, big, big, big, flags);
	else if (best_compress == 2)
		r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
		                                NULL, 0, 0,
		                                best_lazy, big, big, big, big, flags);
	else
		r = -100;
	assert(r == LZO_E_OK);
	assert(out_len == best_len);

/*
 * Step 9: optimize compressed data (compressed data is in `out' buffer)
 */
#if 1
	/* Optimization does not require any data in the buffer that will
	 * hold the uncompressed data. To prove this, we clear the buffer.
	 */
	lzo_memset(in,0,in_len);
#endif

 	orig_len = in_len;
	if (best_compress == 1)
		r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
	else if (best_compress == 2)
		r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
	else
		r = -100;
	if (r != LZO_E_OK || orig_len != in_len)
	{
		/* this should NEVER happen */
		printf("internal error - optimization failed: %d\n", r);
		exit(1);
	}

/*
 * Step 10: compute a checksum of the compressed data
 */
 	compressed_checksum = lzo_adler32(0,NULL,0);
 	compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);

/*
 * Step 11: write compressed data to a file
 */
	printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
	        progname, in_name, (long) in_len, (long) out_len,
	        (long) uncompressed_checksum, (long) compressed_checksum);

	if (out_name && out_name[0])
	{
		printf("%s: writing to file %s\n", progname, out_name);
		f = fopen(out_name,"wb");
		if (f == NULL)
		{
			printf("%s: cannot open output file %s\n", progname, out_name);
			exit(1);
		}
		if (lzo_fwrite(f,out,out_len) != out_len || fclose(f) != 0)
		{
			printf("%s: write error !!\n", progname);
			exit(1);
		}
	}

/*
 * Step 12: verify decompression
 */
#ifdef PARANOID
 	orig_len = in_len;
	if (best_compress == 1)
		r = lzo1x_decompress(out,out_len,in,&orig_len,NULL);
	else if (best_compress == 2)
		r = lzo1y_decompress(out,out_len,in,&orig_len,NULL);
	else
		r = -100;
	if (r != LZO_E_OK || orig_len != in_len)
	{
		/* this should NEVER happen */
		printf("internal error - decompression failed: %d\n", r);
		exit(1);
	}
	if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
	{
		/* this should NEVER happen */
		printf("internal error - decompression data error\n");
		exit(1);
	}
	/* Now you could also verify decompression under similar conditions as in
	 * your application, e.g. overlapping assembler decompression etc.
	 */
#endif

	lzo_free(in);
	lzo_free(out);
	lzo_free(wrkmem);

	return 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;
}
//---------------------------------------------------------------------------
uint32_t adler32(uint32_t adler,const void * buf, uintptr_t len)
{
  return lzo_adler32(adler,(const lzo_bytep) buf,(lzo_uint) len);
}
Exemple #15
0
void* lzowrite_init(const char* filename) {
	//Prepare the buffers
	struct lzowrite_buffer* buffer = malloc(sizeof(struct lzowrite_buffer));
	buffer->output = fopen(filename, "w");
	buffer->length = 0;

	//Allocate workmemory
	HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
	buffer->workmemory = wrkmem;

	//Write LZO fileformat
	unsigned char magic[LZOWRITE_LZO_MAGIC_LEN] = LZOWRITE_LZO_MAGIC;
	fwrite(magic, sizeof(unsigned char), LZOWRITE_LZO_MAGIC_LEN, buffer->output);

	//Init header
	struct lzowrite_file_header* fheader = malloc(sizeof(struct lzowrite_file_header));
	fheader->version = LZOWRITE_LZO_VERSION;
	fheader->library_version = lzo_version();
	fheader->needed_version = LZOWRITE_LZO_VERSION_NEEDED_TO_EXTRACT;
	fheader->compression_method = LZOWRITE_LZO_METHOD;
	fheader->compression_level = LZOWRITE_LZO_COMPRESSION_LEVEL;
	fheader->compression_flags = LZOWRITE_LZO_FLAGS;
	fheader->mode = LZOWRITE_LZO_MODE;
	fheader->file_name_length = 0;
	fheader->file_header_checksum = 1;
	fheader->file_mtime_high = 0;
	fheader->file_mtime_low = 0;

	fwrite(&fheader->version, sizeof(uint16_t), 1, buffer->output);
	fwrite(&fheader->library_version, sizeof(uint16_t), 1, buffer->output);
	fwrite(&fheader->needed_version, sizeof(uint16_t), 1, buffer->output);
	fwrite(&fheader->compression_method, sizeof(uint8_t), 1, buffer->output);
	fwrite(&fheader->compression_level, sizeof(uint8_t), 1, buffer->output);
	fwrite(&fheader->compression_flags, sizeof(uint32_t), 1, buffer->output);
	fwrite(&fheader->mode, sizeof(uint32_t), 1, buffer->output);
	fwrite(&fheader->file_mtime_low, sizeof(uint32_t), 1, buffer->output);
	fwrite(&fheader->file_mtime_high, sizeof(uint32_t), 1, buffer->output);
	fwrite(&fheader->file_name_length, sizeof(uint8_t), 1, buffer->output);

	//Calculate checksum
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->version, 2);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->library_version, 2);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->needed_version, 2);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->compression_method, 1);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->compression_level, 1);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->compression_flags, 4);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->mode, 4);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->file_mtime_low, 4);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->file_mtime_high, 4);
	fheader->file_header_checksum = lzo_adler32(fheader->file_header_checksum, (lzo_bytep)&fheader->file_name_length, 1);


	fwrite(&((uint8_t*)(&fheader->file_header_checksum))[3], sizeof(uint8_t), 1, buffer->output);
	fwrite(&((uint8_t*)(&fheader->file_header_checksum))[2], sizeof(uint8_t), 1, buffer->output);
	fwrite(&((uint8_t*)(&fheader->file_header_checksum))[1], sizeof(uint8_t), 1, buffer->output);
	fwrite(&((uint8_t*)(&fheader->file_header_checksum))[0], sizeof(uint8_t), 1, buffer->output);

	free(fheader);

	return buffer;
}
Exemple #16
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;
}
Exemple #17
0
int __lzo_cdecl_main main(int argc, char *argv[])
{
    int r;

    lzo_bytep in;
    lzo_uint in_len;

    lzo_bytep out;
    lzo_uint out_bufsize;
    lzo_uint out_len = 0;

    lzo_voidp wrkmem;
    lzo_uint wrkmem_size;

    lzo_uint best_len;
    int best_compress = -1;

    lzo_uint orig_len;
    lzo_uint32_t uncompressed_checksum;
    lzo_uint32_t compressed_checksum;

    FILE *fp;
    const char *in_name = NULL;
    const char *out_name = NULL;
    long l;


    lzo_wildargv(&argc, &argv);

    printf("\nLZO real-time data compression library (v%s, %s).\n",
           lzo_version_string(), lzo_version_date());
    printf("Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");

    progname = argv[0];
    if (argc < 2 || argc > 3)
    {
        printf("usage: %s file [output-file]\n", progname);
        exit(1);
    }
    in_name = argv[1];
    if (argc > 2) out_name = argv[2];

/*
 * Step 1: initialize the LZO library
 */
    if (lzo_init() != LZO_E_OK)
    {
        printf("internal error - lzo_init() failed !!!\n");
        printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
        exit(1);
    }

/*
 * Step 2: allocate the work-memory
 */
    wrkmem_size = 1;
#ifdef USE_LZO1X
    wrkmem_size = (LZO1X_999_MEM_COMPRESS > wrkmem_size) ? LZO1X_999_MEM_COMPRESS : wrkmem_size;
#endif
#ifdef USE_LZO1Y
    wrkmem_size = (LZO1Y_999_MEM_COMPRESS > wrkmem_size) ? LZO1Y_999_MEM_COMPRESS : wrkmem_size;
#endif
    wrkmem = (lzo_voidp) xmalloc(wrkmem_size);
    if (wrkmem == NULL)
    {
        printf("%s: out of memory\n", progname);
        exit(1);
    }

/*
 * Step 3: open the input file
 */
    fp = fopen(in_name,"rb");
    if (fp == NULL)
    {
        printf("%s: cannot open file %s\n", progname, in_name);
        exit(1);
    }
    fseek(fp, 0, SEEK_END);
    l = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    if (l <= 0)
    {
        printf("%s: %s: empty file\n", progname, in_name);
        fclose(fp); fp = NULL;
        exit(1);
    }
    in_len = (lzo_uint) l;
    out_bufsize = in_len + in_len / 16 + 64 + 3;
    best_len = in_len;

/*
 * Step 4: allocate compression buffers and read the file
 */
    in = (lzo_bytep) xmalloc(in_len);
    out = (lzo_bytep) xmalloc(out_bufsize);
    if (in == NULL || out == NULL)
    {
        printf("%s: out of memory\n", progname);
        exit(1);
    }
    in_len = (lzo_uint) lzo_fread(fp, in, in_len);
    printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
    fclose(fp); fp = NULL;

/*
 * Step 5: compute a checksum of the uncompressed data
 */
    uncompressed_checksum = lzo_adler32(0,NULL,0);
    uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);

/*
 * Step 6a: compress from 'in' to 'out' with LZO1X-999
 */
#ifdef USE_LZO1X
        out_len = out_bufsize;
        r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
        if (r != LZO_E_OK)
        {
            /* this should NEVER happen */
            printf("internal error - compression failed: %d\n", r);
            exit(1);
        }
        printf("LZO1X-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
        if (out_len < best_len)
        {
            best_len = out_len;
            best_compress = 1;      /* LZO1X-999 */
        }
#endif /* USE_LZO1X */

/*
 * Step 6b: compress from 'in' to 'out' with LZO1Y-999
 */
#ifdef USE_LZO1Y
        out_len = out_bufsize;
        r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
        if (r != LZO_E_OK)
        {
            /* this should NEVER happen */
            printf("internal error - compression failed: %d\n", r);
            exit(1);
        }
        printf("LZO1Y-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
        if (out_len < best_len)
        {
            best_len = out_len;
            best_compress = 2;      /* LZO1Y-999 */
        }
#endif /* USE_LZO1Y */

/*
 * Step 7: check if compressible
 */
    if (best_len >= in_len)
    {
        printf("This file contains incompressible data.\n");
        return 0;
    }

/*
 * Step 8: compress data again using the best compressor found
 */
    out_len = out_bufsize;
    if (best_compress == 1)
        r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
    else if (best_compress == 2)
        r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
    else
        r = -100;
    assert(r == LZO_E_OK);
    assert(out_len == best_len);

/*
 * Step 9: optimize compressed data (compressed data is in 'out' buffer)
 */
#if 1
    /* Optimization does not require any data in the buffer that will
     * hold the uncompressed data. To prove this, we clear the buffer.
     */
    lzo_memset(in,0,in_len);
#endif

    orig_len = in_len;
    r = -100;
#ifdef USE_LZO1X
    if (best_compress == 1)
        r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
#endif
#ifdef USE_LZO1Y
    if (best_compress == 2)
        r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
#endif
    if (r != LZO_E_OK || orig_len != in_len)
    {
        /* this should NEVER happen */
        printf("internal error - optimization failed: %d\n", r);
        exit(1);
    }

/*
 * Step 10: compute a checksum of the compressed data
 */
    compressed_checksum = lzo_adler32(0,NULL,0);
    compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);

/*
 * Step 11: write compressed data to a file
 */
    printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
            progname, in_name, (long) in_len, (long) out_len,
            (long) uncompressed_checksum, (long) compressed_checksum);

    if (out_name && out_name[0])
    {
        printf("%s: writing to file %s\n", progname, out_name);
        fp = fopen(out_name,"wb");
        if (fp == NULL)
        {
            printf("%s: cannot open output file %s\n", progname, out_name);
            exit(1);
        }
        if (lzo_fwrite(fp, out, out_len) != out_len || fclose(fp) != 0)
        {
            printf("%s: write error !!\n", progname);
            exit(1);
        }
    }

/*
 * Step 12: verify decompression
 */
#ifdef PARANOID
    lzo_memset(in,0,in_len);    /* paranoia - clear output buffer */
    orig_len = in_len;
    r = -100;
#ifdef USE_LZO1X
    if (best_compress == 1)
        r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL);
#endif
#ifdef USE_LZO1Y
    if (best_compress == 2)
        r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL);
#endif
    if (r != LZO_E_OK || orig_len != in_len)
    {
        /* this should NEVER happen */
        printf("internal error - decompression failed: %d\n", r);
        exit(1);
    }
    if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
    {
        /* this should NEVER happen */
        printf("internal error - decompression data error\n");
        exit(1);
    }
    /* Now you could also verify decompression under similar conditions as in
     * your application, e.g. overlapping assembler decompression etc.
     */
#endif

    lzo_free(in);
    lzo_free(out);
    lzo_free(wrkmem);

    return 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;
}
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;
}
Exemple #20
0
uint32_t LzopStreamReader::ReadNextBlock(void)
{
	uint32_t			uncompressed;			// Length of uncompressed data
	uint32_t			compressed;				// Length of compressed data
	uint32_t			adler_checksum_d = 0;	// Decompressed data ADLER32
	uint32_t			crc32_checksum_d = 0;	// Decompressed data CRC32
	uint32_t			adler_checksum_c = 0;	// Compressed data ADLER32
	uint32_t			crc32_checksum_c = 0;	// Compressed data CRC32

	// No more data
	if(m_lzoremain == 0) return 0;

	// Get the amount of uncompressed data in the next block
	m_lzopos = ReadBE32(m_lzopos, &m_lzoremain, &uncompressed);
	if(uncompressed == 0) {

		// Force data pointer to the end of the provided length
		// to prevent any more data from being read
		m_lzopos += m_lzoremain;
		m_lzoremain = 0;
		return 0;
	}

	// Read the length of the compressed data
	m_lzopos = ReadBE32(m_lzopos, &m_lzoremain, &compressed);

	// Read checksums
	if(m_lzoflags & F_ADLER32_D) m_lzopos = ReadBE32(m_lzopos, &m_lzoremain, &adler_checksum_d);
	if(m_lzoflags & F_CRC32_D) m_lzopos = ReadBE32(m_lzopos, &m_lzoremain, &crc32_checksum_d);
	if(m_lzoflags & F_ADLER32_C) m_lzopos = ReadBE32(m_lzopos, &m_lzoremain, &adler_checksum_c);
	if(m_lzoflags & F_CRC32_C) m_lzopos = ReadBE32(m_lzopos, &m_lzoremain, &crc32_checksum_c);

	// Sanity checks
	if(uncompressed > MAX_BLOCK_SIZE) throw Exception(E_DECOMPRESS_CORRUPT, COMPRESSION_METHOD);
	if(compressed > m_lzoremain) throw Exception(E_DECOMPRESS_TRUNCATED, COMPRESSION_METHOD);

	// Reallocate the block buffer if the current one is not large enough
	if(uncompressed > m_blocklen) {

		if(m_block) { delete[] m_block; m_block = NULL; }
		m_block = new uint8_t[uncompressed];
		if(!m_block) throw Exception(E_OUTOFMEMORY);
		m_blocklen = uncompressed;
	}

	// Special Case: uncompressed == compressed -> copy the data
	if(uncompressed == compressed)
		memcpy(m_block, reinterpret_cast<const void*>(m_lzopos), uncompressed);

	// Not the same, decompress the next block of data into the block buffer
	else {

		lzo_uint out = uncompressed;
		lzo1x_decompress(reinterpret_cast<const lzo_bytep>(m_lzopos), static_cast<lzo_uint>(compressed), m_block, &out, LZO1X_MEM_DECOMPRESS);
		if(out != uncompressed) throw Exception(E_DECOMPRESS_TRUNCATED, COMPRESSION_METHOD);
	}

#ifdef _DEBUG
	// Validate the checksum of the decompressed block data (adler32 only - minilzo doesn't have crc32)
	if(m_lzoflags & F_ADLER32_D) {

		uint32_t adler = lzo_adler32(ADLER32_INIT_VALUE, m_block, uncompressed);
		if(adler != adler_checksum_d) throw Exception(E_DECOMPRESS_CORRUPT, COMPRESSION_METHOD);
	}
#endif

	// Move the LZO stream pointer to the next block of data
	m_lzopos += compressed;
	m_lzoremain -= compressed;

	// Reset the block current pointer and the number of remaining bytes
	m_blockcurrent = m_block;
	m_blockremain = uncompressed;

	return uncompressed;
}
Exemple #21
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;
}
Exemple #22
0
int main(int argc, char *argv[]) {
  lb_idtype lb_id;
  lzo_uint  lb_overhead;
  lzo_uint  lb_offset;
  lzo_bytep lb_data;
  lzo_bytep lb_temp;

  /* start up the stuff */
#ifdef _WIN32
  char selfname[MAX_PATH]; /* Flawfinder: ignore */
  if (GetModuleFileName(NULL,selfname,sizeof(selfname))==0)
    lb_error("cannot locate this executable");
  argv[0] = selfname;
#endif /* not _WIN32 */
  lb_progname = lb_fnname(argv[0]);
  const char *lb_path = lb_tmppath();
  if (lb_path == NULL) lb_cannot("find","tmp dir",NULL);

  if (lzo_init() != LZO_E_OK)
    lb_error("internal LZO error");
  if ((L = luaL_newstate()) == NULL) lb_error("not enough memory");
  luaL_openlibs(L);
  LBCONF_USERFUNC_INIT(L);

  /* open and load */
  FILE *lb_file = fopen(argv[0],"rb"); /* Flawfinder: ignore */
  if (lb_file == NULL)
    lb_cannot("open",argv[0],NULL);
  if (fseek(lb_file,-sizeof(lb_id),SEEK_END)!=0)
    lb_cannot("seek",argv[0],NULL);
  if (fread(&lb_id,sizeof(lb_id),1,lb_file)!=1)
    lb_cannot("read",argv[0],NULL);
  if (memcmp(lb_id.id,"LB02",4)!=0)
    lb_error("missing overlay");
  if (fseek(lb_file,-(sizeof(lb_id)+lb_id.nlen),SEEK_END)!=0)
    lb_cannot("seek",argv[0],NULL);
  if (lb_id.dlen != 0) {
    lb_overhead = lb_id.dlen / 16 + 64 + 3;
    lb_offset = lb_id.dlen + lb_overhead - lb_id.nlen;
    lb_temp = (lzo_bytep)alloca(lb_id.dlen + lb_overhead);
    lb_data = lb_temp + lb_offset;
  } else
    lb_data = (lzo_bytep)alloca(lb_id.nlen);
  if (fread(lb_data,lb_id.nlen,1,lb_file)!=1)
    lb_cannot("read",argv[0],NULL);
  fclose(lb_file);

  /* checksum */
  if (lzo_adler32(0,(lzo_bytep)lb_data,lb_id.nlen)!=lb_id.adler32)
    lb_error("bad checksum");

  /* decompress */
  if (lb_id.dlen != 0) {
    lzo_uint new_len;
    int r = lzo1x_decompress(lb_data,lb_id.nlen,lb_temp,&new_len,NULL);
    if (r != LZO_E_OK || new_len != lb_id.dlen)
      lb_error("overlapping decompression failed");
    lb_data = lb_temp;
  } else 
    lb_id.dlen = lb_id.nlen;

  /* set parameters (arg = argv) */
  lua_newtable(L);
  { int i;
    for (i=0;i <= argc; i++) {
      lua_pushstring(L,argv[i]);
      lua_rawseti(L,-2,i);
    }
  }
  lua_setglobal(L,"arg");

  /* setup the clean up */
  lua_newtable(L);
  lua_setfield(L,LUA_REGISTRYINDEX,"_LBCM");
  lua_getfield(L,LUA_REGISTRYINDEX,"_LBCM");   /* _LBCM is at index 1   */
  atexit(lb_quit);

  /* link, load and run loop */
  lua_getfield(L,LUA_REGISTRYINDEX,"_LOADED"); /* _LOADED is at index 2 */
  char *buf = lb_data;
  uint32_t ptr = 0;
  do {
    char  _type = *buf++;
    char  _nlen = *buf++;
    char *_name = alloca((size_t)_nlen+1);
    memcpy(_name,buf,_nlen); /* Flawfinder: ignore */
    _name[_nlen] = '\0';
    buf += _nlen;
    uint32_t _size;
    memcpy(&_size,buf,4); /* Flawfinder: ignore */
    buf += 4;
    switch (_type) {
      case LB_LUAMAIN: /* run */
        if (luaL_loadbuffer(L,buf,_size,_name))
          lb_error(lua_tostring(L,-1));
        int i = 1;
        for(;i <= argc;i++)
          lua_pushstring(L,argv[i]);
        if (lua_pcall(L,i-1,0,0))
          lb_error(lua_tostring(L,-1));
        return 0;
      case LB_LMODULE: /* load */
      case LB_CMODULE: /* link and load */
        lua_getfield(L,2,_name); /* _LOADED[name] */
        if (lua_toboolean(L,-1)) { /* is it there? */
          if (lua_touserdata(L,-1) == sentinel) { /* check loops */
            lua_pushfstring(L,"loop or previous error loading module '%s'",_name);
            lb_error(lua_tostring(L,-1));
          }
          lua_pop(L,2);
          break; /* package is already loaded */
        } else lua_pop(L,1);
        /* else must load it */
        if (_type == LB_LMODULE) {
          if (luaL_loadbuffer(L,buf,_size,_name))
            lb_error(lua_tostring(L,-1));
        } else {
          lua_pushfstring(L,"%s/%s%s",lb_path,_name,LB_DLEXT);
          lb_libcreate(lua_tostring(L,-1),buf,_size);
          void *_lib = ll_load(L,lua_tostring(L,-1));
          if (_lib == NULL) lb_cannot("load library",_name,lua_tostring(L,-1));
          lua_CFunction _fun = ll_sym(L,_lib,mkfuncname(L,_name));
          if (_fun == NULL) lb_cannot("find function",lua_tostring(L,-2),lua_tostring(L,-1));
          lua_pop(L,1); /* pop value from mkfuncname() */
          lua_pushlightuserdata(L,_lib);
          lua_settable(L,1); /* _LBCM[libname] = _lib */
          lua_pushcfunction(L,_fun);
        }
        lua_pushlightuserdata(L,sentinel);
        lua_setfield(L,2,_name); /* _LOADED[name] = sentinel */
        lua_pushstring(L,_name); /* pass name as argument to module */
        if (lua_pcall(L,1,1,0)) /* run loaded module */
          lb_error(lua_tostring(L,-1)); 
        if (!lua_isnil(L,-1)) /* non-nil return ? */
          lua_setfield(L,2,_name); /* _LOADED[name] = returned value */
        lua_getfield(L,2,_name);
        if (lua_touserdata(L,-1)==sentinel){ /* module did not set a value? */
          lua_pushboolean(L,1); /* use true as result */
          lua_setfield(L,2,_name); /* _LOADED[name] = true */
        }
        break;
      case LB_LIBRARY: /* link */
        lua_pushfstring(L,"%s/%s%s",lb_path,_name,LB_DLEXT);
        lb_libcreate(lua_tostring(L,-1),buf,_size);
        void *_lib = ll_load(L,lua_tostring(L,-1));
        if (_lib == NULL) lb_cannot("load library",_name,lua_tostring(L,-1));
        lua_pushlightuserdata(L,_lib);
        lua_settable(L,1); /* _LBCM[libname] = _lib */
        break;
    }
    buf += _size;
    ptr += 2 + _nlen + 4 + _size;
  } while (ptr < lb_id.dlen);
  return 0;
}
Exemple #23
0
int
lzo_compress_chunk_part(const void *buf, gsize bufsize, GByteArray *result, gulong* checksum)
{
	lzo_bytep out = NULL;
	lzo_uint out_len = 0;
	gsize out_max;
	lzo_bytep wrkmem = NULL;
	lzo_uint wrk_len = 0;
	int r = 0;
	
	/* Sanity check */
	if(!result) {
		ERROR("Invalid parameter : %p", result);
		return 1;
	}

	out_max = get_working_buffer_size(bufsize);
	out = g_malloc0(out_max);
	lzo_uint tmp = bufsize;
	lzo_uint32 checksum32 = 0;
	checksum32 = *checksum;
	checksum32 = lzo_adler32(checksum32, buf, tmp);
	*checksum = checksum32;

	wrk_len = LZO1X_1_MEM_COMPRESS;
	wrkmem = (lzo_bytep) g_malloc0(wrk_len);
	if (buf == NULL || out == NULL || wrkmem == NULL){
		DEBUG("out of memory\n");
		r = 1;
		goto err;
	}

	/* compress block */
	r = lzo1x_1_compress(buf, bufsize, out, &out_len, wrkmem);
	if (r != LZO_E_OK || out_len > out_max){
		/* this should NEVER happen */
		DEBUG("internal error - compression failed\n");
		r = 2;
		goto err;
	}

#define DATA_APPEND(D, S) g_byte_array_append(result, (guint8*)D, S);

	/* write uncompressed block size */
	result = DATA_APPEND(&bufsize, sizeof(lzo_uint));

	if (out_len < bufsize) {
		/* write compressed block */
		result = DATA_APPEND(&out_len, sizeof(lzo_uint));
		result = DATA_APPEND(out, out_len);
		out = NULL;
	}
	else {
		/* not compressible - write uncompressed block */
		result = DATA_APPEND(&bufsize, sizeof(lzo_uint));
		result = DATA_APPEND(buf, bufsize);
	}

	r = 0;

err:
	g_free(wrkmem);
	if (out)
		g_free(out);
	return r; 
}
Exemple #24
0
unsigned
bmz_checksum(const void *in, size_t in_len) {
  return lzo_adler32(1, (Byte *)in, in_len);
}
Exemple #25
0
unsigned
bmz_update_checksum(unsigned s, const void *in, size_t in_len) {
  return lzo_adler32(s, (Byte *)in, in_len);
}
Exemple #26
0
size_t lzo_deflate (unsigned flags, size_t cd_nelmts,
                    const unsigned cd_values[], size_t nbytes,
                    size_t *buf_size, void **buf)
{
  size_t ret_value = 0;
#ifdef HAVE_LZO_LIB
  void *outbuf = NULL, *wrkmem = NULL;
  int status;
  size_t  nalloc = *buf_size;
  lzo_uint out_len = (lzo_uint) nalloc;
  /* max_len_buffer will keep the likely output buffer size
     after processing the first chunk */
  static unsigned int max_len_buffer = 0;
  /* int complevel = 1; */
#if (defined CHECKSUM || defined DEBUG)
  int object_version = 10;      /* Default version 1.0 */
  int object_type = Table;      /* Default object type */
#endif
#ifdef CHECKSUM
  lzo_uint32 checksum;
#endif

  /* Check arguments */
  /* For Table versions < 20, there were no parameters */
  if (cd_nelmts==1 ) {
    /* complevel = cd_values[0]; */ /* This do nothing right now */
  }
  else if (cd_nelmts==2 ) {
    /* complevel = cd_values[0]; */ /* This do nothing right now */
#if (defined CHECKSUM || defined DEBUG)
    object_version = cd_values[1]; /* The table VERSION attribute */
#endif
  }
  else if (cd_nelmts==3 ) {
    /* complevel = cd_values[0]; */ /* This do nothing right now */
#if (defined CHECKSUM || defined DEBUG)
    object_version = cd_values[1]; /* The table VERSION attribute */
    object_type = cd_values[2]; /* A tag for identifying the object
                                   (see tables.h) */
#endif
  }

#ifdef DEBUG
  printf("Object type: %d. ", object_type);
  printf("object_version:%d\n", object_version);
#endif

  if (flags & H5Z_FLAG_REVERSE) {
    /* Input */

/*     printf("Decompressing chunk with LZO\n"); */
#ifdef CHECKSUM
    if ((object_type == Table && object_version >= 20) ||
        object_type != Table) {
      nbytes -= 4;      /* Point to uncompressed buffer length */
      memcpy(&nalloc, ((unsigned char *)(*buf)+nbytes), 4);
      out_len = nalloc;
      nbytes -= 4;      /* Point to the checksum */
#ifdef DEBUG
      printf("Compressed bytes: %d. Uncompressed bytes: %d\n", nbytes, nalloc);
#endif
    }
#endif

    /* Only allocate the bytes for the outbuf */
    if (max_len_buffer == 0) {
      if (NULL==(outbuf = (void *)malloc(nalloc)))
        fprintf(stderr, "Memory allocation failed for lzo uncompression.\n");
    }
    else {
      if (NULL==(outbuf = (void *)malloc(max_len_buffer)))
        fprintf(stderr, "Memory allocation failed for lzo uncompression.\n");
      out_len = max_len_buffer;
      nalloc =  max_len_buffer;
    }

    while(1) {

#ifdef DEBUG
      printf("nbytes -->%d\n", nbytes);
      printf("nalloc -->%d\n", nalloc);
      printf("max_len_buffer -->%d\n", max_len_buffer);
#endif /* DEBUG */

      /* The assembler version is a 10% slower than the C version with
         gcc 3.2.2 and gcc 3.3.3 */
/*       status = lzo1x_decompress_asm_safe(*buf, (lzo_uint)nbytes, outbuf, */
/*                                          &out_len, NULL); */
      /* The safe and unsafe versions have the same speed more or less */
      status = lzo1x_decompress_safe(*buf, (lzo_uint)nbytes, outbuf,
                                     &out_len, NULL);

      if (status == LZO_E_OK) {
#ifdef DEBUG
        printf("decompressed %lu bytes back into %lu bytes\n",
               (long) nbytes, (long) out_len);
#endif
        max_len_buffer = out_len;
        break; /* done */
      }
      else if (status == LZO_E_OUTPUT_OVERRUN) {
        nalloc *= 2;
        out_len = (lzo_uint) nalloc;
        if (NULL==(outbuf = realloc(outbuf, nalloc))) {
          fprintf(stderr, "Memory allocation failed for lzo uncompression\n");
        }
      }
      else {
        /* this should NEVER happen */
        fprintf(stderr, "internal error - decompression failed: %d\n", status);
        ret_value = 0; /* fail */
        goto done;
      }
    }

#ifdef CHECKSUM
    if ((object_type == Table && object_version >= 20) ||
        object_type != Table) {
#ifdef DEBUG
      printf("Checksum uncompressing...");
#endif
      /* Compute the checksum */
      checksum=lzo_adler32(lzo_adler32(0,NULL,0), outbuf, out_len);

      /* Compare */
      if (memcmp(&checksum, (unsigned char*)(*buf)+nbytes, 4)) {
        ret_value = 0; /*fail*/
        fprintf(stderr,"Checksum failed!.\n");
        goto done;
      }
    }
#endif /* CHECKSUM */

    free(*buf);
    *buf = outbuf;
    outbuf = NULL;
    *buf_size = nalloc;
    ret_value = out_len;

  } else {
    /*
     * Output; compress but fail if the result would be larger than the
     * input.  The library doesn't provide in-place compression, so we
     * must allocate a separate buffer for the result.
     */
    lzo_byte *z_src = (lzo_byte*)(*buf);
    lzo_byte *z_dst;         /*destination buffer            */
    lzo_uint z_src_nbytes = (lzo_uint)(nbytes);
    /* The next was the original computation for worst-case expansion */
    /* I don't know why the difference with LZO1*. Perhaps some wrong docs in
       LZO package? */
/*     lzo_uint z_dst_nbytes = (lzo_uint)(nbytes + (nbytes / 64) + 16 + 3); */
    /* The next is for LZO1* algorithms */
/*     lzo_uint z_dst_nbytes = (lzo_uint)(nbytes + (nbytes / 16) + 64 + 3); */
    /* The next is for LZO2* algorithms. This will be the default */
    lzo_uint z_dst_nbytes = (lzo_uint)(nbytes + (nbytes / 8) + 128 + 3);

#ifdef CHECKSUM
    if ((object_type == Table && object_version >= 20) ||
        object_type != Table) {
      z_dst_nbytes += 4+4;      /* Checksum + buffer size */
    }
#endif

    if (NULL==(z_dst=outbuf=(void *)malloc(z_dst_nbytes))) {
      fprintf(stderr, "Unable to allocate lzo destination buffer.\n");
      ret_value = 0; /* fail */
      goto done;
    }

    /* Compress this buffer */
    wrkmem = malloc(LZO1X_1_MEM_COMPRESS);
    if (wrkmem == NULL) {
      fprintf(stderr, "Memory allocation failed for lzo compression\n");
      ret_value = 0;
      goto done;
    }

    status = lzo1x_1_compress (z_src, z_src_nbytes, z_dst, &z_dst_nbytes,
                               wrkmem);

    free(wrkmem);
    wrkmem = NULL;

#ifdef CHECKSUM
    if ((object_type == Table && object_version >= 20) ||
        object_type != Table) {
#ifdef DEBUG
      printf("Checksum compressing ...");
      printf("src_nbytes: %d, dst_nbytes: %d\n", z_src_nbytes, z_dst_nbytes);
#endif
      /* Append checksum of *uncompressed* data at the end */
      checksum = lzo_adler32(lzo_adler32(0,NULL,0), *buf, nbytes);
      memcpy((unsigned char*)(z_dst)+z_dst_nbytes, &checksum, 4);
      memcpy((unsigned char*)(z_dst)+z_dst_nbytes+4, &nbytes, 4);
      z_dst_nbytes += (lzo_uint)4+4;
      nbytes += 4+4;
    }
#endif

    if (z_dst_nbytes >= nbytes) {
#ifdef DEBUG
      printf("The compressed buffer takes more space than uncompressed!.\n");
#endif
      ret_value = 0; /* fail */
      goto done;
    } else if (LZO_E_OK != status) {
      fprintf(stderr,"lzo library error in compression\n");
      ret_value = 0; /* fail */
      goto done;
    } else {
      free(*buf);
      *buf = outbuf;
      outbuf = NULL;
      *buf_size = z_dst_nbytes;
      ret_value = z_dst_nbytes;
    }
  }

done:
  if(outbuf)
    free(outbuf);

#endif  /* HAVE_LZO_LIB */

  return ret_value;
}
//---------------------------------------------------------------------------
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;
}
Exemple #28
0
int __lzo_cdecl_main main(int argc, char *argv[])
{
    int i = 1;
    int r;
    const char *dict_name;
    FILE *f;
    time_t t_total;
    int level = 7;

    lzo_wildargv(&argc, &argv);

    printf("\nLZO real-time data compression library (v%s, %s).\n",
           lzo_version_string(), lzo_version_date());
    printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");

    progname = argv[0];

    if (i < argc && argv[i][0] == '-' && isdigit(argv[i][1]))
        level = atoi(&argv[i++][1]);

    if (i + 1 >= argc || level < 1 || level > 9)
    {
        printf("usage: %s [-level] [ dictionary-file | -n ]  file...\n", progname);
        exit(1);
    }
    printf("Compression level is LZO1X-999/%d\n", level);

/*
 * Step 1: initialize the LZO library
 */
    if (lzo_init() != LZO_E_OK)
    {
        printf("internal error - lzo_init() failed !!!\n");
        printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n");
        exit(1);
    }

/*
 * Step 2: prepare the dictionary
 */
    dict = (lzo_bytep) lzo_malloc(DICT_LEN);
    if (dict == NULL)
    {
        printf("%s: out of memory\n", progname);
        exit(1);
    }
    dict_name = argv[i++];
    if (strcmp(dict_name,"-n") == 0)
    {
        dict_name = "empty";
        dict_len = 0;
    }
    else
    {
        f = fopen(dict_name,"rb");
        if (!f)
        {
            printf("%s: cannot open dictionary file %s\n", progname, dict_name);
            exit(1);
        }
        dict_len = (lzo_uint) lzo_fread(f,dict,DICT_LEN);
        fclose(f);
    }

    dict_adler32 = lzo_adler32(0,NULL,0);
    dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
    printf("Using dictionary '%s', %ld bytes, ID 0x%08lx.\n",
            dict_name, (long) dict_len, (long) dict_adler32);

/*
 * Step 3: process files
 */
    t_total = time(NULL);
    for (r = 0; r == 0 && i < argc; i++)
        r = do_file(argv[i], level);
    t_total = time(NULL) - t_total;

    lzo_free(dict);

    if (total_n > 1)
        print_file("***TOTALS***",total_d_len,total_c_len);

    printf("Dictionary compression test %s, execution time %lu seconds.\n",
            r == 0 ? "passed" : "FAILED", (unsigned long) t_total);
    return r;
}
static unsigned long __cdecl _calculateChecksum( unsigned long c, const void *data, size_t dataSize )
{
    // TODO: fix checksum verification for R* XBOX IMG files.
    return lzo_adler32( c, (const unsigned char*)data, dataSize );
}