int
zlib_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;
	struct compressed_chunk_s ck;

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

	ck.uncompressed_size = g_strdup(cti.chunk_size);
	DEBUG("size get in attr = %s", ck.uncompressed_size); 

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

	ck.fd = fopen(path, "r");

	if (!ck.fd) {
		DEBUG("Failed to open chunk file");
		r = 1;
		goto err;
	}
	
	TRACE("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, ck.fd);
	if (nb_read != 1) {
		DEBUG("Failed to read compressed chunk headers");
		r = 2;
		goto err;
	}

	do { /* extract all headers */
		#define GETNEXTPTR(Res,Ptr,Type) do { Res = *((Type *)Ptr); Ptr = ((char*)Ptr) + sizeof(Type); } while (0)
		char *ptr = ((char*)headers + sizeof(magic));
		GETNEXTPTR(ck.block_size, ptr, guint32);
	} while (0);

	if (memcmp(headers, magic, sizeof(magic)) != 0) {
		r = 4;
		goto err;
	}
    	if (ck.block_size < 1024 || ck.block_size > 8*1024*1024L){
        	r = 6;
        	goto err;
    	}

	TRACE("ck.block_size : %d", ck.block_size);

	ck.checksum = adler32(0,NULL,0);
	memcpy(chunk, &ck, sizeof(ck));
	TRACE("chunk->uncompressed_size = %s", chunk->uncompressed_size);

	r=0;

err:
	if (error)
		g_clear_error(&error);
	chunk_textinfo_free_content(&cti);
	return r;
}
Example #2
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;
}