other-licenses/7zstub/src/7zip/Common/OutBuffer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/7zip/Common/OutBuffer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +// OutByte.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "OutBuffer.h"
     1.9 +
    1.10 +#include "../../Common/Alloc.h"
    1.11 +
    1.12 +bool COutBuffer::Create(UInt32 bufferSize)
    1.13 +{
    1.14 +  const UInt32 kMinBlockSize = 1;
    1.15 +  if (bufferSize < kMinBlockSize)
    1.16 +    bufferSize = kMinBlockSize;
    1.17 +  if (_buffer != 0 && _bufferSize == bufferSize)
    1.18 +    return true;
    1.19 +  Free();
    1.20 +  _bufferSize = bufferSize;
    1.21 +  _buffer = (Byte *)::MidAlloc(bufferSize);
    1.22 +  return (_buffer != 0);
    1.23 +}
    1.24 +
    1.25 +void COutBuffer::Free()
    1.26 +{
    1.27 +  ::MidFree(_buffer);
    1.28 +  _buffer = 0;
    1.29 +}
    1.30 +
    1.31 +void COutBuffer::SetStream(ISequentialOutStream *stream)
    1.32 +{
    1.33 +  _stream = stream;
    1.34 +}
    1.35 +
    1.36 +void COutBuffer::Init()
    1.37 +{
    1.38 +  _streamPos = 0;
    1.39 +  _limitPos = _bufferSize;
    1.40 +  _pos = 0;
    1.41 +  _processedSize = 0;
    1.42 +  _overDict = false;
    1.43 +  #ifdef _NO_EXCEPTIONS
    1.44 +  ErrorCode = S_OK;
    1.45 +  #endif
    1.46 +}
    1.47 +
    1.48 +UInt64 COutBuffer::GetProcessedSize() const
    1.49 +{ 
    1.50 +  UInt64 res = _processedSize + _pos - _streamPos;
    1.51 +  if (_streamPos > _pos) 
    1.52 +    res += _bufferSize;
    1.53 +  return res;
    1.54 +}
    1.55 +
    1.56 +
    1.57 +HRESULT COutBuffer::FlushPart()
    1.58 +{
    1.59 +  // _streamPos < _bufferSize
    1.60 +  UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos);
    1.61 +  HRESULT result = S_OK;
    1.62 +  #ifdef _NO_EXCEPTIONS
    1.63 +  result = ErrorCode;
    1.64 +  #endif
    1.65 +  if (_buffer2 != 0)
    1.66 +  {
    1.67 +    memmove(_buffer2, _buffer + _streamPos, size);
    1.68 +    _buffer2 += size;
    1.69 +  }
    1.70 +
    1.71 +  if (_stream != 0
    1.72 +      #ifdef _NO_EXCEPTIONS
    1.73 +      && (ErrorCode == S_OK)
    1.74 +      #endif
    1.75 +     )
    1.76 +  {
    1.77 +    UInt32 processedSize = 0;
    1.78 +    result = _stream->Write(_buffer + _streamPos, size, &processedSize);
    1.79 +    size = processedSize;
    1.80 +  }
    1.81 +  _streamPos += size;
    1.82 +  if (_streamPos == _bufferSize)
    1.83 +    _streamPos = 0;
    1.84 +  if (_pos == _bufferSize)
    1.85 +  {
    1.86 +    _overDict = true;
    1.87 +    _pos = 0;
    1.88 +  }
    1.89 +  _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize;
    1.90 +  _processedSize += size;
    1.91 +  return result;
    1.92 +}
    1.93 +
    1.94 +HRESULT COutBuffer::Flush()
    1.95 +{
    1.96 +  #ifdef _NO_EXCEPTIONS
    1.97 +  if (ErrorCode != S_OK)
    1.98 +    return ErrorCode;
    1.99 +  #endif
   1.100 +
   1.101 +  while(_streamPos != _pos)
   1.102 +  {
   1.103 +    HRESULT result = FlushPart();
   1.104 +    if (result != S_OK)
   1.105 +      return result;
   1.106 +  }
   1.107 +  return S_OK;
   1.108 +}
   1.109 +
   1.110 +void COutBuffer::FlushWithCheck()
   1.111 +{
   1.112 +  HRESULT result = FlushPart();
   1.113 +  #ifdef _NO_EXCEPTIONS
   1.114 +  ErrorCode = result;
   1.115 +  #else
   1.116 +  if (result != S_OK)
   1.117 +    throw COutBufferException(result);
   1.118 +  #endif
   1.119 +}

mercurial