Пример #1
0
int
zlib_compress_chunk_part(const void *buf, gsize bufsize, GByteArray *result, gulong* checksum)
{
	guint8* out = NULL;
	gulong bufsize_ulong = bufsize;
	gulong out_max;
	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);
	*checksum = adler32(*checksum, buf, bufsize_ulong);
		
	if (buf == NULL || out == NULL){
		r = 1;
		goto err;
	}

	/* compress block */
	r = compress(out, &out_max, buf, bufsize_ulong);
	if (r != Z_OK){
		/* this should NEVER happen */
		ERROR("internal error - compression failed");
		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_ulong, sizeof(gulong));

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

	r = 0;

err:
	if (out)
		g_free(out);
	return r; 
}
Пример #2
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; 
}