Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PPMDDecoder.cpp « PPMD « Compress « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d0a2f52b521f8ece92ad8f1953a2ff8c11ec45d (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
// PPMDDecoder.cpp

#include "StdAfx.h"

#include "Common/Defs.h"
#include "Windows/Defs.h"

#include "PPMDDecoder.h"

namespace NCompress {
namespace NPPMD {

const int kLenIdFinished = -1;
const int kLenIdNeedInit = -2;

STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *properties, UInt32 size)
{
  if (size < 5)
    return E_INVALIDARG;
  _order = properties[0];
  _usedMemorySize = 0;
  for (int i = 0; i < 4; i++)
    _usedMemorySize += ((UInt32)(properties[1 + i])) << (i * 8);

  if (_usedMemorySize > kMaxMemBlockSize)
    return E_NOTIMPL;

  if (!_rangeDecoder.Create(1 << 20))
    return E_OUTOFMEMORY;
  if (!_info.SubAllocator.StartSubAllocator(_usedMemorySize)) 
    return E_OUTOFMEMORY;

  return S_OK;
}

class CDecoderFlusher
{
  CDecoder *_coder;
public:
  bool NeedFlush;
  CDecoderFlusher(CDecoder *coder): _coder(coder), NeedFlush(true) {}
  ~CDecoderFlusher()
  {
    if (NeedFlush)
      _coder->Flush();
    _coder->ReleaseStreams();
  }
};

HRESULT CDecoder::CodeSpec(UInt32 size, Byte *memStream)
{
  if (_outSizeDefined)
  {
    const UInt64 rem = _outSize - _processedSize;
    if (size > rem)
      size = (UInt32)rem;
  }
  const UInt32 startSize = size;

  if (_remainLen == kLenIdFinished)
    return S_OK;
  if (_remainLen == kLenIdNeedInit)
  {
    _rangeDecoder.Init();
    _remainLen = 0;
    _info.MaxOrder = 0;
    _info.StartModelRare(_order);
  }
  while (size != 0)
  {
    int symbol = _info.DecodeSymbol(&_rangeDecoder);
    if (symbol < 0)
    {
      _remainLen = kLenIdFinished;
      break;
    }
    if (memStream != 0)
      *memStream++ = (Byte)symbol;
    else
      _outStream.WriteByte((Byte)symbol);
    size--;
  }
  _processedSize += startSize - size;
  return S_OK;
}

STDMETHODIMP CDecoder::CodeReal(ISequentialInStream *inStream,
      ISequentialOutStream *outStream, const UInt64 * /* inSize */, const UInt64 *outSize,
      ICompressProgressInfo *progress)
{
  if (!_outStream.Create(1 << 20))
    return E_OUTOFMEMORY;
  
  SetInStream(inStream);
  _outStream.SetStream(outStream);
  SetOutStreamSize(outSize);
  CDecoderFlusher flusher(this);

  for (;;)
  {
    _processedSize = _outStream.GetProcessedSize();
    UInt32 curSize = (1 << 18);
    RINOK(CodeSpec(curSize, NULL));
    if (_remainLen == kLenIdFinished)
      break;
    if (progress != NULL)
    {
      UInt64 inSize = _rangeDecoder.GetProcessedSize();
      RINOK(progress->SetRatioInfo(&inSize, &_processedSize));
    }
    if (_outSizeDefined)
      if (_outStream.GetProcessedSize() >= _outSize)
        break;
  }
  flusher.NeedFlush = false;
  return Flush();
}

#ifdef _NO_EXCEPTIONS

#define PPMD_TRY_BEGIN
#define PPMD_TRY_END

#else

#define PPMD_TRY_BEGIN try { 
#define PPMD_TRY_END } \
  catch(const CInBufferException &e)  { return e.ErrorCode; } \
  catch(const COutBufferException &e)  { return e.ErrorCode; } \
  catch(...) { return S_FALSE; }

#endif


STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream,
    ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
    ICompressProgressInfo *progress)
{
  PPMD_TRY_BEGIN
  return CodeReal(inStream, outStream, inSize, outSize, progress);
  PPMD_TRY_END
}

STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream)
{
  _rangeDecoder.SetStream(inStream);
  return S_OK;
}

STDMETHODIMP CDecoder::ReleaseInStream()
{
  _rangeDecoder.ReleaseStream();
  return S_OK;
}

STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize)
{
  _outSizeDefined = (outSize != NULL);
  if (_outSizeDefined)
    _outSize = *outSize;
  _processedSize = 0;
  _remainLen = kLenIdNeedInit;
  _outStream.Init();
  return S_OK;
}

#ifdef _ST_MODE

STDMETHODIMP CDecoder::Read(void *data, UInt32 size, UInt32 *processedSize)
{
  PPMD_TRY_BEGIN
  if (processedSize)
    *processedSize = 0;
  const UInt64 startPos = _processedSize;
  RINOK(CodeSpec(size, (Byte *)data));
  if (processedSize)
    *processedSize = (UInt32)(_processedSize - startPos);
  return Flush();
  PPMD_TRY_END
}

#endif

}}