-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuCE.c
135 lines (120 loc) · 2.4 KB
/
GuCE.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include <assert.h>
#define CHUNK 1024
int deflatenbt(unsigned char * source, long src_len, FILE * dest, int level)
{
int ret, flush;
unsigned have;
z_stream strm;
unsigned char out[CHUNK];
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, level);
if (ret != Z_OK)
return ret;
strm.avail_in = src_len;
strm.next_in = source;
do
{
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR);
have = CHUNK - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == 0);
assert(ret == Z_STREAM_END);
(void)deflateEnd(&strm);
return Z_OK;
}
unsigned char * inflatenbt(FILE * source, long * rsize)
{
int ret;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
unsigned char * inflated = NULL;
long size = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, 16 + MAX_WBITS);
if (ret != Z_OK)
return NULL;
do
{
strm.avail_in = fread(in, 1, CHUNK, source);
if (ferror(source))
{
(void)inflateEnd(&strm);
return NULL;
}
if (strm.avail_in == 0)
break;
strm.next_in = in;
do
{
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR);
printf("ret: %i", ret);
switch (ret)
{
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return NULL;
}
have = CHUNK - strm.avail_out;
inflated = realloc(inflated, have + size);
memcpy(inflated + size, out, have);
size += have;
} while (strm.avail_out == 0);
} while (ret != Z_STREAM_END);
(void)inflateEnd(&strm);
if(Z_STREAM_END)
{
*rsize = size;
return inflated;
}
else
return NULL;
}
/*int main(int argc, char ** argv)
{
FILE * fp;
fp = fopen("level.dat", "rb");
if(fp == NULL)
{
printf("Error reading file!\n");
return 1;
}
long size;
unsigned char * buffer = inflatenbt(fp, &size);
if(buffer == NULL)
{
printf("ERROR!\n");
fclose(fp);
return 1;
}
printf("Size %i\n", (int)size);
int i;
for(i = 0; i < size; i++)
{
printf("%c", buffer[i]);
}
printf("\n");
fclose(fp);
}*/