diff options
Diffstat (limited to '')
-rw-r--r-- | src/bzip2.cpp | 123 |
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 | |||
4 | using namespace Bu; | ||
5 | |||
6 | Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : | ||
7 | Bu::Filter( rNext ), | ||
8 | nCompression( nCompression ) | ||
9 | { | ||
10 | start(); | ||
11 | } | ||
12 | |||
13 | Bu::BZip2::~BZip2() | ||
14 | { | ||
15 | printf("-> Bu::BZip2::~BZip2()\n"); | ||
16 | stop(); | ||
17 | } | ||
18 | |||
19 | void 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 | |||
32 | void 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 | |||
61 | void 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 | |||
79 | size_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 | |||
94 | size_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 | |||