view src/win32/7zip/7z/CPP/7zip/Compress/BZip2Decoder.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // Compress/BZip2Decoder.h
3 #ifndef __COMPRESS_BZIP2_DECODER_H
4 #define __COMPRESS_BZIP2_DECODER_H
6 #include "../../Common/MyCom.h"
8 #ifdef COMPRESS_BZIP2_MT
9 #include "../../Windows/Synchronization.h"
10 #include "../../Windows/Thread.h"
11 #endif
13 #include "../ICoder.h"
15 #include "../Common/InBuffer.h"
16 #include "../Common/OutBuffer.h"
18 #include "BitmDecoder.h"
19 #include "BZip2Const.h"
20 #include "BZip2Crc.h"
21 #include "HuffmanDecoder.h"
23 namespace NCompress {
24 namespace NBZip2 {
26 typedef NCompress::NHuffman::CDecoder<kMaxHuffmanLen, kMaxAlphaSize> CHuffmanDecoder;
28 class CDecoder;
30 struct CState
31 {
32 UInt32 *Counters;
34 #ifdef COMPRESS_BZIP2_MT
36 CDecoder *Decoder;
37 NWindows::CThread Thread;
38 bool m_OptimizeNumTables;
40 NWindows::NSynchronization::CAutoResetEvent StreamWasFinishedEvent;
41 NWindows::NSynchronization::CAutoResetEvent WaitingWasStartedEvent;
43 // it's not member of this thread. We just need one event per thread
44 NWindows::NSynchronization::CAutoResetEvent CanWriteEvent;
46 Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size.
48 HRESULT Create();
49 void FinishStream();
50 void ThreadFunc();
52 #endif
54 CState(): Counters(0) {}
55 ~CState() { Free(); }
56 bool Alloc();
57 void Free();
58 };
60 class CDecoder :
61 public ICompressCoder,
62 #ifdef COMPRESS_BZIP2_MT
63 public ICompressSetCoderMt,
64 #endif
65 public ICompressGetInStreamProcessedSize,
66 public CMyUnknownImp
67 {
68 public:
69 COutBuffer m_OutStream;
70 Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size.
71 NBitm::CDecoder<CInBuffer> m_InStream;
72 Byte m_Selectors[kNumSelectorsMax];
73 CHuffmanDecoder m_HuffmanDecoders[kNumTablesMax];
74 private:
76 UInt32 m_NumThreadsPrev;
78 UInt32 ReadBits(int numBits);
79 Byte ReadByte();
80 bool ReadBit();
81 UInt32 ReadCrc();
82 HRESULT PrepareBlock(CState &state);
83 HRESULT DecodeFile(bool &isBZ, ICompressProgressInfo *progress);
84 HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
85 const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
86 class CDecoderFlusher
87 {
88 CDecoder *_decoder;
89 public:
90 bool NeedFlush;
91 CDecoderFlusher(CDecoder *decoder): _decoder(decoder), NeedFlush(true) {}
92 ~CDecoderFlusher()
93 {
94 if (NeedFlush)
95 _decoder->Flush();
96 _decoder->ReleaseStreams();
97 }
98 };
100 public:
101 CBZip2CombinedCrc CombinedCrc;
103 #ifdef COMPRESS_BZIP2_MT
104 ICompressProgressInfo *Progress;
105 CState *m_States;
107 NWindows::NSynchronization::CManualResetEvent CanProcessEvent;
108 NWindows::NSynchronization::CCriticalSection CS;
109 UInt32 NumThreads;
110 bool MtMode;
111 UInt32 NextBlockIndex;
112 bool CloseThreads;
113 bool StreamWasFinished1;
114 bool StreamWasFinished2;
115 NWindows::NSynchronization::CManualResetEvent CanStartWaitingEvent;
117 HRESULT Result1;
118 HRESULT Result2;
120 UInt32 BlockSizeMax;
121 CDecoder();
122 ~CDecoder();
123 HRESULT Create();
124 void Free();
126 #else
127 CState m_States[1];
128 #endif
130 HRESULT ReadSignatures(bool &wasFinished, UInt32 &crc);
133 HRESULT Flush() { return m_OutStream.Flush(); }
134 void ReleaseStreams()
135 {
136 m_InStream.ReleaseStream();
137 m_OutStream.ReleaseStream();
138 }
140 #ifdef COMPRESS_BZIP2_MT
141 MY_UNKNOWN_IMP2(ICompressSetCoderMt, ICompressGetInStreamProcessedSize)
142 #else
143 MY_UNKNOWN_IMP1(ICompressGetInStreamProcessedSize)
144 #endif
147 STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
148 const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
150 STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);
152 #ifdef COMPRESS_BZIP2_MT
153 STDMETHOD(SetNumberOfThreads)(UInt32 numThreads);
154 #endif
155 };
157 }}
159 #endif