summaryrefslogtreecommitdiff
path: root/src/bzip2.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-06-07 19:56:20 +0000
committerMike Buland <eichlan@xagasoft.com>2007-06-07 19:56:20 +0000
commit9e8a4944e50fab432012878c66e1bdac20649f76 (patch)
tree412b0d49ae4086bb330045f9956513397b866807 /src/bzip2.cpp
parentc2e3879b965d297604804f03271ac71c8c5c81f3 (diff)
downloadlibbu++-9e8a4944e50fab432012878c66e1bdac20649f76.tar.gz
libbu++-9e8a4944e50fab432012878c66e1bdac20649f76.tar.bz2
libbu++-9e8a4944e50fab432012878c66e1bdac20649f76.tar.xz
libbu++-9e8a4944e50fab432012878c66e1bdac20649f76.zip
The Stream Filter archetecture is finished, it's actually much cooler than I
had anticipated, and much cleaner. I'll have to add some documentation to it, because it's not really obvious how any of it fits together from the outset, although I have to say that the bzip2 test program is the easiest general bzip2 compression program I've ever made...it just goes :) Decompression in Bu::BZip2 isn't finished yet, but that's ok, it's coming soon.
Diffstat (limited to 'src/bzip2.cpp')
-rw-r--r--src/bzip2.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/bzip2.cpp b/src/bzip2.cpp
new file mode 100644
index 0000000..b8c0f74
--- /dev/null
+++ b/src/bzip2.cpp
@@ -0,0 +1,123 @@
1#include "bu/bzip2.h"
2#include "bu/exceptions.h"
3
4using namespace Bu;
5
6Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) :
7 Bu::Filter( rNext ),
8 nCompression( nCompression )
9{
10 start();
11}
12
13Bu::BZip2::~BZip2()
14{
15 printf("-> Bu::BZip2::~BZip2()\n");
16 stop();
17}
18
19void Bu::BZip2::start()
20{
21 printf("-> Bu::BZip2::start()\n");
22 printf("Hey, it's starting...\n");
23 bzState.state = NULL;
24 bzState.bzalloc = NULL;
25 bzState.bzfree = NULL;
26 bzState.opaque = NULL;
27
28 nBufSize = 50000;
29 pBuf = new char[nBufSize];
30}
31
32void Bu::BZip2::stop()
33{
34 printf("-> Bu::BZip2::stop()\n");
35 if( bzState.state )
36 {
37 if( bReading )
38 {
39 }
40 else
41 {
42 for(;;)
43 {
44 bzState.next_in = NULL;
45 bzState.avail_in = 0;
46 bzState.avail_out = nBufSize;
47 bzState.next_out = pBuf;
48 int res = BZ2_bzCompress( &bzState, BZ_FINISH );
49 if( bzState.avail_out < nBufSize )
50 {
51 rNext.write( pBuf, nBufSize-bzState.avail_out );
52 }
53 if( res == BZ_STREAM_END )
54 break;
55 }
56 BZ2_bzCompressEnd( &bzState );
57 }
58 }
59}
60
61void Bu::BZip2::bzError( int code )
62{
63 switch( code )
64 {
65 case BZ_OK:
66 return;
67
68 case BZ_CONFIG_ERROR:
69 throw ExceptionBase("The bzip2 library has been miscompiled.");
70
71 case BZ_PARAM_ERROR:
72 throw ExceptionBase("bzip2 parameter error.");
73
74 case BZ_MEM_ERROR:
75 throw ExceptionBase("Not enough memory available for bzip2.");
76 }
77}
78
79size_t Bu::BZip2::read( void *pData, size_t nBytes )
80{
81 if( !bzState.state )
82 {
83 bReading = true;
84 }
85 if( bReading == false )
86 throw ExceptionBase("This bzip2 filter is in writing mode, you can't read.");
87 //bzState.next_in = pData;
88 //bzState.avail_in = nSizeIn;
89
90 //printf("%db at [%08X] (%db)\n", bzState.avail_in, (uint32_t)bzState.next_in, bzState.total_in_lo32 );
91 return 0;
92}
93
94size_t Bu::BZip2::write( const void *pData, size_t nBytes )
95{
96 if( !bzState.state )
97 {
98 bReading = false;
99 BZ2_bzCompressInit( &bzState, nCompression, 0, 30 );
100 }
101 if( bReading == true )
102 throw ExceptionBase("This bzip2 filter is in reading mode, you can't write.");
103
104 bzState.next_in = (char *)pData;
105 bzState.avail_in = nBytes;
106 for(;;)
107 {
108 bzState.avail_out = nBufSize;
109 bzState.next_out = pBuf;
110
111 BZ2_bzCompress( &bzState, BZ_RUN );
112
113 if( bzState.avail_out < nBufSize )
114 {
115 rNext.write( pBuf, nBufSize-bzState.avail_out );
116 }
117 if( bzState.avail_in == 0 )
118 break;
119 }
120
121 return 0;
122}
123