BCM WL 6.30.102.9 (r366174)
[tomato.git] / release / src-rt / linux / linux-2.6 / scripts / squashfs / lzma / C / 7zip / Common / OutBuffer.cpp
blobe76e6f4d6cde1a707c57ee82f8a625667817fd15
1 // OutByte.cpp
3 #include "StdAfx.h"
5 #include "OutBuffer.h"
7 #include "../../Common/Alloc.h"
9 bool COutBuffer::Create(UInt32 bufferSize)
11 const UInt32 kMinBlockSize = 1;
12 if (bufferSize < kMinBlockSize)
13 bufferSize = kMinBlockSize;
14 if (_buffer != 0 && _bufferSize == bufferSize)
15 return true;
16 Free();
17 _bufferSize = bufferSize;
18 _buffer = (Byte *)::MidAlloc(bufferSize);
19 return (_buffer != 0);
22 void COutBuffer::Free()
24 ::MidFree(_buffer);
25 _buffer = 0;
28 void COutBuffer::SetStream(ISequentialOutStream *stream)
30 _stream = stream;
33 void COutBuffer::Init()
35 _streamPos = 0;
36 _limitPos = _bufferSize;
37 _pos = 0;
38 _processedSize = 0;
39 _overDict = false;
40 #ifdef _NO_EXCEPTIONS
41 ErrorCode = S_OK;
42 #endif
45 UInt64 COutBuffer::GetProcessedSize() const
47 UInt64 res = _processedSize + _pos - _streamPos;
48 if (_streamPos > _pos)
49 res += _bufferSize;
50 return res;
54 HRESULT COutBuffer::FlushPart()
56 // _streamPos < _bufferSize
57 UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos);
58 HRESULT result = S_OK;
59 #ifdef _NO_EXCEPTIONS
60 if (ErrorCode != S_OK)
61 result = ErrorCode;
62 #endif
63 if (_buffer2 != 0)
65 memmove(_buffer2, _buffer + _streamPos, size);
66 _buffer2 += size;
69 if (_stream != 0
70 #ifdef _NO_EXCEPTIONS
71 && (ErrorCode != S_OK)
72 #endif
75 UInt32 processedSize = 0;
76 result = _stream->Write(_buffer + _streamPos, size, &processedSize);
77 size = processedSize;
79 _streamPos += size;
80 if (_streamPos == _bufferSize)
81 _streamPos = 0;
82 if (_pos == _bufferSize)
84 _overDict = true;
85 _pos = 0;
87 _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize;
88 _processedSize += size;
89 return result;
92 HRESULT COutBuffer::Flush()
94 #ifdef _NO_EXCEPTIONS
95 if (ErrorCode != S_OK)
96 return ErrorCode;
97 #endif
99 while(_streamPos != _pos)
101 HRESULT result = FlushPart();
102 if (result != S_OK)
103 return result;
105 return S_OK;
108 void COutBuffer::FlushWithCheck()
110 HRESULT result = FlushPart();
111 #ifdef _NO_EXCEPTIONS
112 ErrorCode = result;
113 #else
114 if (result != S_OK)
115 throw COutBufferException(result);
116 #endif