Zlib and vectors

Hey
I have zlib library and want to deflate and inflate data at vector with buffer help.
I have found some code and modified it a bit, can anyone check and correct it if it's wrong? It looks like it's working, but maybe it causing some bugs I can't see?




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
deflate
) {
	char p1 = GetInt();

	if (numBoards)
	{
		vector <char> buffer;
		
		const size_t BUFSIZE = 128 * 1024;
		unsigned char temp_buffer[BUFSIZE];
		
		z_stream strm;
		strm.zalloc = 0;
		strm.zfree = 0;
		strm.next_in = reinterpret_cast<unsigned char *>(d_vData.data());
		strm.avail_in = d_vData.size();
		strm.next_out = temp_buffer;
		strm.avail_out = BUFSIZE;
		
		deflateInit(&strm, p1);
		
		while (strm.avail_in)
		{
			int res = deflate(&strm, Z_NO_FLUSH);
			assert(res == Z_OK);
			if (!strm.avail_out)
			{
				buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
				strm.next_out = temp_buffer;
				strm.avail_out = BUFSIZE;
			}
		}
		
		int deflate_res = Z_OK;
		//while (deflate_res == Z_OK)
		{
			deflate_res = deflate(&strm, Z_FINISH);
		}
		
		assert(deflate_res == Z_STREAM_END);
		buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out); // rest
		deflateEnd(&strm);
		
		d_vData.swap(buffer);
		buffer.clear();
	}
}

inflate
) {
	if (numBoards)
	{
		vector <char> buffer;
		
		const size_t BUFSIZE = 128 * 1024;
		unsigned char temp_buffer[BUFSIZE];
		
		z_stream strm;
		strm.zalloc = 0;
		strm.zfree = 0;
		strm.next_in = reinterpret_cast<unsigned char *>(d_vData.data());
		strm.avail_in = d_vData.size();
		strm.next_out = temp_buffer;
		strm.avail_out = BUFSIZE;
		
		inflateInit(&strm);
		
		while (strm.avail_in)
		{
			int res = inflate(&strm, Z_NO_FLUSH);
			assert(res == Z_OK);
			if (!strm.avail_out)
			{
				buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
				strm.next_out = temp_buffer;
				strm.avail_out = BUFSIZE;
			}
			else
				break;
		}
		
		int inflate_res = Z_OK;
		//while (inflate_res == Z_OK)
		{
			inflate_res = inflate(&strm, Z_FINISH);
		}
		
		assert(inflate_res == Z_STREAM_END);
		buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out);
		inflateEnd(&strm);
		
		d_vData.swap(buffer);
		buffer.clear();
	}
}
Last edited on
Topic archived. No new replies allowed.