~ubuntu-branches/ubuntu/precise/p7zip/precise-updates

« back to all changes in this revision

Viewing changes to CPP/7zip/Compress/BZip2Decoder.h

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-02-14 20:12:27 UTC
  • mfrom: (1.1.11 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090214201227-go63qxm9ozfdma60
Tags: 4.65~dfsg.1-1
* New upstream release.
* Remove wx2.8 Build-Depends added by mistakes (7zG is not yet
  intended to be built).
* Use dh_clean without -k.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Compress/BZip2Decoder.h
 
2
 
 
3
#ifndef __COMPRESS_BZIP2_DECODER_H
 
4
#define __COMPRESS_BZIP2_DECODER_H
 
5
 
 
6
#include "../../Common/MyCom.h"
 
7
 
 
8
#ifdef COMPRESS_BZIP2_MT
 
9
#include "../../Windows/Synchronization.h"
 
10
#include "../../Windows/Thread.h"
 
11
#endif
 
12
 
 
13
#include "../ICoder.h"
 
14
 
 
15
#include "../Common/InBuffer.h"
 
16
#include "../Common/OutBuffer.h"
 
17
 
 
18
#include "BitmDecoder.h"
 
19
#include "BZip2Const.h"
 
20
#include "BZip2Crc.h"
 
21
#include "HuffmanDecoder.h"
 
22
 
 
23
namespace NCompress {
 
24
namespace NBZip2 {
 
25
 
 
26
typedef NCompress::NHuffman::CDecoder<kMaxHuffmanLen, kMaxAlphaSize> CHuffmanDecoder;
 
27
 
 
28
class CDecoder;
 
29
 
 
30
struct CState
 
31
{
 
32
  UInt32 *Counters;
 
33
 
 
34
  #ifdef COMPRESS_BZIP2_MT
 
35
 
 
36
  CDecoder *Decoder;
 
37
  NWindows::CThread Thread;
 
38
  bool m_OptimizeNumTables;
 
39
 
 
40
  NWindows::NSynchronization::CAutoResetEvent StreamWasFinishedEvent;
 
41
  NWindows::NSynchronization::CAutoResetEvent WaitingWasStartedEvent;
 
42
 
 
43
  // it's not member of this thread. We just need one event per thread
 
44
  NWindows::NSynchronization::CAutoResetEvent CanWriteEvent;
 
45
 
 
46
  Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size.
 
47
 
 
48
  HRESULT Create();
 
49
  void FinishStream();
 
50
  void ThreadFunc();
 
51
 
 
52
  #endif
 
53
 
 
54
  CState(): Counters(0) {}
 
55
  ~CState() { Free(); }
 
56
  bool Alloc();
 
57
  void Free();
 
58
};
 
59
 
 
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:
 
75
 
 
76
  UInt32 m_NumThreadsPrev;
 
77
 
 
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
  };
 
99
 
 
100
public:
 
101
  CBZip2CombinedCrc CombinedCrc;
 
102
 
 
103
  #ifdef COMPRESS_BZIP2_MT
 
104
  ICompressProgressInfo *Progress;
 
105
  CState *m_States;
 
106
 
 
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;
 
116
 
 
117
  HRESULT Result1;
 
118
  HRESULT Result2;
 
119
 
 
120
  UInt32 BlockSizeMax;
 
121
  CDecoder();
 
122
  ~CDecoder();
 
123
  HRESULT Create();
 
124
  void Free();
 
125
 
 
126
  #else
 
127
  CState m_States[1];
 
128
  #endif
 
129
 
 
130
  HRESULT ReadSignatures(bool &wasFinished, UInt32 &crc);
 
131
 
 
132
 
 
133
  HRESULT Flush() { return m_OutStream.Flush(); }
 
134
  void ReleaseStreams()
 
135
  {
 
136
    m_InStream.ReleaseStream();
 
137
    m_OutStream.ReleaseStream();
 
138
  }
 
139
 
 
140
  #ifdef COMPRESS_BZIP2_MT
 
141
  MY_UNKNOWN_IMP2(ICompressSetCoderMt, ICompressGetInStreamProcessedSize)
 
142
  #else
 
143
  MY_UNKNOWN_IMP1(ICompressGetInStreamProcessedSize)
 
144
  #endif
 
145
 
 
146
  
 
147
  STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
 
148
      const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
 
149
 
 
150
  STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);
 
151
 
 
152
  #ifdef COMPRESS_BZIP2_MT
 
153
  STDMETHOD(SetNumberOfThreads)(UInt32 numThreads);
 
154
  #endif
 
155
};
 
156
 
 
157
}}
 
158
 
 
159
#endif