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

LZOutWindow.cpp « LZ « Compress « 7zip - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4101079b27b7e16a15d1d9791182788d5ee37fad (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
// LZOutWindow.cpp

#include "StdAfx.h"

#include "../../../Common/Alloc.h"
#include "LZOutWindow.h"

bool CLZOutWindow::Create(UInt32 windowSize)
{
  const UInt32 kMinBlockSize = 1;
  if (windowSize < kMinBlockSize)
    windowSize = kMinBlockSize;
  if (_buffer != 0 && _windowSize == windowSize)
    return true;

  // It's here to allow Solid decoding / and calling Create for RAR
  _pos = 0;
  _streamPos = 0;
  
  Free();
  _windowSize = windowSize;
  _buffer = (Byte *)::BigAlloc(windowSize);
  return (_buffer != 0);
}

void CLZOutWindow::Free()
{
  ::BigFree(_buffer);
  _buffer = 0;
}

void CLZOutWindow::SetStream(ISequentialOutStream *stream)
{
  ReleaseStream();
  _stream = stream;
  _stream->AddRef();
}

void CLZOutWindow::Init(bool solid)
{
  if(!solid)
  {
    _streamPos = 0;
    _pos = 0;
  }
  #ifdef _NO_EXCEPTIONS
  ErrorCode = S_OK;
  #endif
}

void CLZOutWindow::ReleaseStream()
{
  if(_stream != 0)
  {
    // Flush(); // Test it
    _stream->Release();
    _stream = 0;
  }
}

void CLZOutWindow::FlushWithCheck()
{
  HRESULT result = Flush();
  #ifdef _NO_EXCEPTIONS
  ErrorCode = result;
  #else
  if (result != S_OK)
    throw CLZOutWindowException(result);
  #endif
}

HRESULT CLZOutWindow::Flush()
{
  UInt32 size = _pos - _streamPos;
  if(size == 0)
    return S_OK;
  #ifdef _NO_EXCEPTIONS
  if (ErrorCode != S_OK)
    return ErrorCode;
  #endif

  if(_stream != 0)
  {
    UInt32 processedSize;
    HRESULT result = _stream->Write(_buffer + _streamPos, size, &processedSize);
    if (result != S_OK)
      return result;
    if (size != processedSize)
      return E_FAIL;
  }
  if (_pos >= _windowSize)
    _pos = 0;
  _streamPos = _pos;
  return S_OK;
}