aboutsummaryrefslogtreecommitdiff
path: root/src/bzip2.cpp
blob: 6bb1429ab6fe8601e992e3cc3106478adfaa3c8b (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "bu/bzip2.h"
#include "bu/exceptions.h"

using namespace Bu;

Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) :
	Bu::Filter( rNext ),
	nCompression( nCompression )
{
	start();
}

Bu::BZip2::~BZip2()
{
	stop();
}

void Bu::BZip2::start()
{
	bzState.state = NULL;
	bzState.bzalloc = NULL;
	bzState.bzfree = NULL;
	bzState.opaque = NULL;

	nBufSize = 50000;
	pBuf = new char[nBufSize];
}

size_t Bu::BZip2::stop()
{
	if( bzState.state )
	{
		if( bReading )
		{
			BZ2_bzDecompressEnd( &bzState );
			return 0;
		}
		else
		{
			size_t sTotal = 0;
			for(;;)
			{
				bzState.next_in = NULL;
				bzState.avail_in = 0;
				bzState.avail_out = nBufSize;
				bzState.next_out = pBuf;
				int res = BZ2_bzCompress( &bzState, BZ_FINISH );
				if( bzState.avail_out < nBufSize )
				{
					sTotal += rNext.write( pBuf, nBufSize-bzState.avail_out );
				}
				if( res == BZ_STREAM_END )
					break;
			}
			BZ2_bzCompressEnd( &bzState );
			return sTotal;
		}
	}
	return 0;
}

void Bu::BZip2::bzError( int code )
{
	switch( code )
	{
		case BZ_OK:
			printf("<BZ_OK>\n"); return;
		case BZ_RUN_OK:
			printf("<BZ_RUN_OK>\n"); return;
		case BZ_FLUSH_OK:
			printf("<BZ_FLUSH_OK>\n"); return;
		case BZ_FINISH_OK:
			printf("<BZ_FINISH_OK>\n"); return;
			return;

		case BZ_CONFIG_ERROR:
			throw ExceptionBase("BZip2: Library configured improperly, reinstall.");

		case BZ_SEQUENCE_ERROR:
			throw ExceptionBase("BZip2: Functions were called in an invalid sequence.");

		case BZ_PARAM_ERROR:
			throw ExceptionBase("BZip2: Invalid parameter was passed into a function.");

		case BZ_MEM_ERROR:
			throw ExceptionBase("BZip2: Couldn't allocate sufficient memory.");

		case BZ_DATA_ERROR:
			throw ExceptionBase("BZip2: Data was corrupted before decompression.");

		case BZ_DATA_ERROR_MAGIC:
			throw ExceptionBase("BZip2: Stream does not appear to be bzip2 data.");

		case BZ_IO_ERROR:
			throw ExceptionBase("BZip2: File couldn't be read from / written to.");

		case BZ_UNEXPECTED_EOF:
			throw ExceptionBase("BZip2: End of file encountered before end of stream.");

		case BZ_OUTBUFF_FULL:
			throw ExceptionBase("BZip2: Buffer not large enough to accomidate data.");

		default:
			throw ExceptionBase("BZip2: Unknown error encountered.");
	
	}
}

size_t Bu::BZip2::read( void *pData, size_t nBytes )
{
	if( !bzState.state )
	{
		bReading = true;
		BZ2_bzDecompressInit( &bzState, 0, 0 );
		bzState.next_in = pBuf;
		bzState.avail_in = 0;
	}
	if( bReading == false )
		throw ExceptionBase("This bzip2 filter is in writing mode, you can't read.");

	int nRead = 0;
	int nReadTotal = bzState.total_out_lo32;
	for(;;)
	{
		bzState.next_out = (char *)pData;
		bzState.avail_out = nBytes;
		printf(" (pre) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out );
		int ret = BZ2_bzDecompress( &bzState );
		printf("(post) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out );
	
		nReadTotal += nRead-bzState.avail_out;

		if( ret == BZ_STREAM_END )
		{
			printf("<BZ_STREAM_END>\n");
			if( bzState.avail_in > 0 )
			{
				if( rNext.canSeek() )
				{
					rNext.seek( -bzState.avail_in );
				}
			}
			return nBytes-bzState.avail_out;
		}
		bzError( ret );

		if( bzState.avail_out )
		{
			printf("Still more to fill, in: %db, out: %db\n", bzState.avail_in, bzState.avail_out );

			if( bzState.avail_in == 0 )
			{
				nRead = rNext.read( pBuf, nBufSize );
				bzState.next_in = pBuf;
				bzState.avail_in = nRead;
			}
		}
		else
		{
			return nBytes-bzState.avail_out;
		}
	}
	return 0;
}

size_t Bu::BZip2::write( const void *pData, size_t nBytes )
{
	if( !bzState.state )
	{
		bReading = false;
		BZ2_bzCompressInit( &bzState, nCompression, 0, 30 );
	}
	if( bReading == true )
		throw ExceptionBase("This bzip2 filter is in reading mode, you can't write.");

	size_t sTotalOut = 0;
	bzState.next_in = (char *)pData;
	bzState.avail_in = nBytes;
	for(;;)
	{
		bzState.avail_out = nBufSize;
		bzState.next_out = pBuf;

		bzError( BZ2_bzCompress( &bzState, BZ_RUN ) );

		if( bzState.avail_out < nBufSize )
		{
			sTotalOut += rNext.write( pBuf, nBufSize-bzState.avail_out );
		}
		if( bzState.avail_in == 0 )
			break;
	}

	return sTotalOut;
}