File: zfile.cpp

package info (click to toggle)
amoeba 1.1-19
  • links: PTS
  • area: contrib
  • in suites: lenny
  • size: 816 kB
  • ctags: 966
  • sloc: cpp: 8,286; makefile: 173
file content (55 lines) | stat: -rw-r--r-- 994 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <zlib.h>

#include "demolib_prefs.h"
#include "file.h"
#include "zfile.h"
#include "../exception.h"

#if DEMOLIB_DATA_ZLIB

ZFile::ZFile(File *basefile)
{
	z_stream zs;

	char *file_data = basefile->get_data();

	/* read the file length first */
	this->length = *((unsigned int *)file_data);
	this->data = (char *)(malloc(this->length));

	if (this->data == NULL) {
		throw new FatalException("Out of memory!");
	}

        memset(&zs, 0, sizeof(zs));
	zs.next_in = (Bytef *)(file_data + 4);
	zs.avail_in = basefile->data_length() - 4;
	zs.next_out = (Bytef *)(this->data);
	zs.avail_out = this->length;
	
	if (inflateInit(&zs) != Z_OK ||
	    inflate(&zs, Z_FINISH) != Z_STREAM_END) {
		throw new FatalException("zlib", zs.msg);
	}
	inflateEnd(&zs);
}

ZFile::~ZFile()
{
	free(this->data);
}

char *ZFile::get_data()
{
	return this->data;
}

int ZFile::data_length()
{
	return this->length;
}
#endif /* DEMOLIB_DATA_ZLIB */