diff options
Diffstat (limited to 'CPP/7zip/Common')
-rwxr-xr-x | CPP/7zip/Common/FileStreams.cpp | 28 | ||||
-rwxr-xr-x | CPP/7zip/Common/FileStreams.h | 19 | ||||
-rwxr-xr-x | CPP/7zip/Common/InMemStream.cpp | 4 | ||||
-rwxr-xr-x | CPP/7zip/Common/InMemStream.h | 19 | ||||
-rwxr-xr-x | CPP/7zip/Common/LSBFEncoder.cpp | 18 | ||||
-rwxr-xr-x | CPP/7zip/Common/LSBFEncoder.h | 18 | ||||
-rwxr-xr-x | CPP/7zip/Common/MemBlocks.cpp | 26 | ||||
-rwxr-xr-x | CPP/7zip/Common/MemBlocks.h | 8 | ||||
-rwxr-xr-x | CPP/7zip/Common/MethodId.cpp | 4 | ||||
-rwxr-xr-x | CPP/7zip/Common/MethodId.h | 2 | ||||
-rwxr-xr-x | CPP/7zip/Common/OutMemStream.h | 6 | ||||
-rwxr-xr-x | CPP/7zip/Common/RegisterCodec.h | 2 | ||||
-rwxr-xr-x | CPP/7zip/Common/StreamBinder.cpp | 42 | ||||
-rwxr-xr-x | CPP/7zip/Common/StreamBinder.h | 15 |
14 files changed, 112 insertions, 99 deletions
diff --git a/CPP/7zip/Common/FileStreams.cpp b/CPP/7zip/Common/FileStreams.cpp index 8a000e4e..bd90ce79 100755 --- a/CPP/7zip/Common/FileStreams.cpp +++ b/CPP/7zip/Common/FileStreams.cpp @@ -25,7 +25,7 @@ bool CInFileStream::Open(LPCTSTR fileName) return File.Open(fileName); } -#ifdef _WIN32 +#ifdef USE_WIN_FILE #ifndef _UNICODE bool CInFileStream::Open(LPCWSTR fileName) { @@ -34,9 +34,23 @@ bool CInFileStream::Open(LPCWSTR fileName) #endif #endif +bool CInFileStream::OpenShared(LPCTSTR fileName, bool shareForWrite) +{ + return File.OpenShared(fileName, shareForWrite); +} + +#ifdef USE_WIN_FILE +#ifndef _UNICODE +bool CInFileStream::OpenShared(LPCWSTR fileName, bool shareForWrite) +{ + return File.OpenShared(fileName, shareForWrite); +} +#endif +#endif + STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) { - #ifdef _WIN32 + #ifdef USE_WIN_FILE UInt32 realProcessedSize; bool result = File.ReadPart(data, size, realProcessedSize); @@ -98,7 +112,7 @@ STDMETHODIMP CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, if(seekOrigin >= 3) return STG_E_INVALIDFUNCTION; - #ifdef _WIN32 + #ifdef USE_WIN_FILE UInt64 realNewPosition; bool result = File.Seek(offset, seekOrigin, realNewPosition); @@ -132,7 +146,7 @@ bool COutFileStream::Create(LPCTSTR fileName, bool createAlways) return File.Create(fileName, createAlways); } -#ifdef _WIN32 +#ifdef USE_WIN_FILE #ifndef _UNICODE bool COutFileStream::Create(LPCWSTR fileName, bool createAlways) { @@ -143,7 +157,7 @@ bool COutFileStream::Create(LPCWSTR fileName, bool createAlways) STDMETHODIMP COutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) { - #ifdef _WIN32 + #ifdef USE_WIN_FILE UInt32 realProcessedSize; bool result = File.WritePart(data, size, realProcessedSize); @@ -170,7 +184,7 @@ STDMETHODIMP COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, { if(seekOrigin >= 3) return STG_E_INVALIDFUNCTION; - #ifdef _WIN32 + #ifdef USE_WIN_FILE UInt64 realNewPosition; bool result = File.Seek(offset, seekOrigin, realNewPosition); @@ -192,7 +206,7 @@ STDMETHODIMP COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, STDMETHODIMP COutFileStream::SetSize(Int64 newSize) { - #ifdef _WIN32 + #ifdef USE_WIN_FILE UInt64 currentPos; if(!File.Seek(0, FILE_CURRENT, currentPos)) return E_FAIL; diff --git a/CPP/7zip/Common/FileStreams.h b/CPP/7zip/Common/FileStreams.h index 9326372a..a65c96e1 100755 --- a/CPP/7zip/Common/FileStreams.h +++ b/CPP/7zip/Common/FileStreams.h @@ -4,6 +4,10 @@ #define __FILESTREAMS_H #ifdef _WIN32 +#define USE_WIN_FILE +#endif + +#ifdef USE_WIN_FILE #include "../../Windows/FileIO.h" #else #include "../../Common/C_FileIO.h" @@ -18,7 +22,7 @@ class CInFileStream: public CMyUnknownImp { public: - #ifdef _WIN32 + #ifdef USE_WIN_FILE NWindows::NFile::NIO::CInFile File; #else NC::NFile::NIO::CInFile File; @@ -27,12 +31,19 @@ public: virtual ~CInFileStream() {} bool Open(LPCTSTR fileName); - #ifdef _WIN32 + #ifdef USE_WIN_FILE #ifndef _UNICODE bool Open(LPCWSTR fileName); #endif #endif + bool OpenShared(LPCTSTR fileName, bool shareForWrite); + #ifdef USE_WIN_FILE + #ifndef _UNICODE + bool OpenShared(LPCWSTR fileName, bool shareForWrite); + #endif + #endif + MY_UNKNOWN_IMP2(IInStream, IStreamGetSize) STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); @@ -62,14 +73,14 @@ class COutFileStream: public CMyUnknownImp { public: - #ifdef _WIN32 + #ifdef USE_WIN_FILE NWindows::NFile::NIO::COutFile File; #else NC::NFile::NIO::COutFile File; #endif virtual ~COutFileStream() {} bool Create(LPCTSTR fileName, bool createAlways); - #ifdef _WIN32 + #ifdef USE_WIN_FILE #ifndef _UNICODE bool Create(LPCWSTR fileName, bool createAlways); #endif diff --git a/CPP/7zip/Common/InMemStream.cpp b/CPP/7zip/Common/InMemStream.cpp index 036ef3bd..7deef250 100755 --- a/CPP/7zip/Common/InMemStream.cpp +++ b/CPP/7zip/Common/InMemStream.cpp @@ -108,13 +108,13 @@ HRESULT CInMemStreamMt::Read() } } -static DWORD WINAPI CoderThread(void *threadCoderInfo) +static THREAD_FUNC_DECL CoderThread(void *threadCoderInfo) { ((CInMemStreamMt *)threadCoderInfo)->ReadResult = ((CInMemStreamMt *)threadCoderInfo)->Read(); return 0; } -bool CInMemStreamMt::StartReadThread() +HRes CInMemStreamMt::StartReadThread() { // _stopReading = false; NWindows::CThread Thread; diff --git a/CPP/7zip/Common/InMemStream.h b/CPP/7zip/Common/InMemStream.h index fcd0092d..73f344ee 100755 --- a/CPP/7zip/Common/InMemStream.h +++ b/CPP/7zip/Common/InMemStream.h @@ -82,11 +82,12 @@ class CResourceListMt: public CResourceList public: NWindows::NSynchronization::CSemaphore Semaphore; - bool AllocateList(int numItems) + HRes AllocateList(int numItems) { if (!CResourceList::AllocateList(numItems)) return false; - return Semaphore.Create((LONG)numItems, (LONG)numItems); + Semaphore.Close(); + return Semaphore.Create(numItems, numItems); } int AllocateItem() @@ -118,15 +119,15 @@ public: CIntQueueMt(): _numItems(0), _head(0), _cur(0) {} NWindows::NSynchronization::CSemaphore Semaphore; - bool AllocateList(int numItems) + HRes AllocateList(int numItems) { FreeList(); if (numItems == 0) - return true; + return S_OK; if (!CIntListCheck::AllocateList(numItems)) - return false; + return E_OUTOFMEMORY; _numItems = numItems; - return Semaphore.Create((LONG)0, (LONG)numItems); + return Semaphore.Create(0, numItems); } void FreeList() @@ -142,10 +143,8 @@ public: _data[_head++] = value; if (_head == _numItems) _head = 0; - LONG previousCount; - Semaphore.Release(1, &previousCount); + Semaphore.Release(); // printf("\nRelease prev = %d\n", previousCount); - } int GetItem() @@ -253,7 +252,7 @@ public: // to stop reading you must implement // returning Error in IInMemStreamMtCallback::AllocateBlock // and then you must free at least one substream - bool StartReadThread(); + HRes StartReadThread(); void Free(); diff --git a/CPP/7zip/Common/LSBFEncoder.cpp b/CPP/7zip/Common/LSBFEncoder.cpp index 6322cae3..7809a8f6 100755 --- a/CPP/7zip/Common/LSBFEncoder.cpp +++ b/CPP/7zip/Common/LSBFEncoder.cpp @@ -8,22 +8,4 @@ namespace NStream { namespace NLSBF { -void CEncoder::WriteBits(UInt32 value, int numBits) -{ - while(numBits > 0) - { - if (numBits < m_BitPos) - { - m_CurByte |= (value & ((1 << numBits) - 1)) << (8 - m_BitPos); - m_BitPos -= numBits; - return; - } - numBits -= m_BitPos; - m_Stream.WriteByte((Byte)(m_CurByte | (value << (8 - m_BitPos)))); - value >>= m_BitPos; - m_BitPos = 8; - m_CurByte = 0; - } -} - }} diff --git a/CPP/7zip/Common/LSBFEncoder.h b/CPP/7zip/Common/LSBFEncoder.h index 72c84d9f..d50aef0b 100755 --- a/CPP/7zip/Common/LSBFEncoder.h +++ b/CPP/7zip/Common/LSBFEncoder.h @@ -38,7 +38,23 @@ public: m_CurByte = 0; } - void WriteBits(UInt32 value, int numBits); + void WriteBits(UInt32 value, int numBits) + { + while(numBits > 0) + { + if (numBits < m_BitPos) + { + m_CurByte |= (value & ((1 << numBits) - 1)) << (8 - m_BitPos); + m_BitPos -= numBits; + return; + } + numBits -= m_BitPos; + m_Stream.WriteByte((Byte)(m_CurByte | (value << (8 - m_BitPos)))); + value >>= m_BitPos; + m_BitPos = 8; + m_CurByte = 0; + } + } UInt32 GetBitPosition() const { return (8 - m_BitPos); } UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() + (8 - m_BitPos + 7) /8; } diff --git a/CPP/7zip/Common/MemBlocks.cpp b/CPP/7zip/Common/MemBlocks.cpp index d2b79a70..15702957 100755 --- a/CPP/7zip/Common/MemBlocks.cpp +++ b/CPP/7zip/Common/MemBlocks.cpp @@ -51,26 +51,27 @@ void CMemBlockManager::FreeBlock(void *p) } -bool CMemBlockManagerMt::AllocateSpace(size_t numBlocks, size_t numNoLockBlocks) +HRes CMemBlockManagerMt::AllocateSpace(size_t numBlocks, size_t numNoLockBlocks) { if (numNoLockBlocks > numBlocks) - return false; + return E_INVALIDARG; if (!CMemBlockManager::AllocateSpace(numBlocks)) - return false; + return E_OUTOFMEMORY; size_t numLockBlocks = numBlocks - numNoLockBlocks; + Semaphore.Close(); return Semaphore.Create((LONG)numLockBlocks, (LONG)numLockBlocks); } -bool CMemBlockManagerMt::AllocateSpaceAlways(size_t desiredNumberOfBlocks, size_t numNoLockBlocks) +HRes CMemBlockManagerMt::AllocateSpaceAlways(size_t desiredNumberOfBlocks, size_t numNoLockBlocks) { if (numNoLockBlocks > desiredNumberOfBlocks) - return false; + return E_INVALIDARG; for (;;) { - if (AllocateSpace(desiredNumberOfBlocks, numNoLockBlocks)) - return true; + if (AllocateSpace(desiredNumberOfBlocks, numNoLockBlocks) == 0) + return 0; if (desiredNumberOfBlocks == numNoLockBlocks) - return false; + return E_OUTOFMEMORY; desiredNumberOfBlocks = numNoLockBlocks + ((desiredNumberOfBlocks - numNoLockBlocks) >> 1); } } @@ -152,16 +153,17 @@ void CMemLockBlocks::Free(CMemBlockManagerMt *memManager) TotalSize = 0; } -bool CMemLockBlocks::SwitchToNoLockMode(CMemBlockManagerMt *memManager) +HRes CMemLockBlocks::SwitchToNoLockMode(CMemBlockManagerMt *memManager) { if (LockMode) { if (Blocks.Size() > 0) - if (!memManager->ReleaseLockedBlocks(Blocks.Size())) - return false; + { + RINOK(memManager->ReleaseLockedBlocks(Blocks.Size())); + } LockMode = false; } - return true; + return 0; } void CMemLockBlocks::Detach(CMemLockBlocks &blocks, CMemBlockManagerMt *memManager) diff --git a/CPP/7zip/Common/MemBlocks.h b/CPP/7zip/Common/MemBlocks.h index c33f712c..b93f6398 100755 --- a/CPP/7zip/Common/MemBlocks.h +++ b/CPP/7zip/Common/MemBlocks.h @@ -41,12 +41,12 @@ public: CMemBlockManagerMt(size_t blockSize = (1 << 20)): CMemBlockManager(blockSize) {} ~CMemBlockManagerMt() { FreeSpace(); } - bool AllocateSpace(size_t numBlocks, size_t numNoLockBlocks = 0); - bool AllocateSpaceAlways(size_t desiredNumberOfBlocks, size_t numNoLockBlocks = 0); + HRes AllocateSpace(size_t numBlocks, size_t numNoLockBlocks = 0); + HRes AllocateSpaceAlways(size_t desiredNumberOfBlocks, size_t numNoLockBlocks = 0); void FreeSpace(); void *AllocateBlock(); void FreeBlock(void *p, bool lockMode = true); - bool ReleaseLockedBlocks(int number) { return Semaphore.Release(number); } + HRes ReleaseLockedBlocks(int number) { return Semaphore.Release(number); } }; @@ -70,7 +70,7 @@ struct CMemLockBlocks: public CMemBlocks CMemLockBlocks(): LockMode(true) {}; void Free(CMemBlockManagerMt *memManager); void FreeBlock(int index, CMemBlockManagerMt *memManager); - bool SwitchToNoLockMode(CMemBlockManagerMt *memManager); + HRes SwitchToNoLockMode(CMemBlockManagerMt *memManager); void Detach(CMemLockBlocks &blocks, CMemBlockManagerMt *memManager); }; diff --git a/CPP/7zip/Common/MethodId.cpp b/CPP/7zip/Common/MethodId.cpp index 5a0c80aa..1eed8fc1 100755 --- a/CPP/7zip/Common/MethodId.cpp +++ b/CPP/7zip/Common/MethodId.cpp @@ -1,8 +1,8 @@ -// MethodID.cpp +// MethodId.cpp #include "StdAfx.h" -#include "MethodID.h" +#include "MethodId.h" #include "../../Common/String.h" static inline wchar_t GetHex(Byte value) diff --git a/CPP/7zip/Common/MethodId.h b/CPP/7zip/Common/MethodId.h index 42306326..54ebc9f7 100755 --- a/CPP/7zip/Common/MethodId.h +++ b/CPP/7zip/Common/MethodId.h @@ -1,4 +1,4 @@ -// MethodID.h +// MethodId.h #ifndef __7Z_METHOD_ID_H #define __7Z_METHOD_ID_H diff --git a/CPP/7zip/Common/OutMemStream.h b/CPP/7zip/Common/OutMemStream.h index a8bd1711..5ffcea2f 100755 --- a/CPP/7zip/Common/OutMemStream.h +++ b/CPP/7zip/Common/OutMemStream.h @@ -30,6 +30,12 @@ class COutMemStream: public: + HRes CreateEvents() + { + RINOK(StopWritingEvent.CreateIfNotCreated()); + return WriteToRealStreamEvent.CreateIfNotCreated(); + } + void SetOutStream(IOutStream *outStream) { OutStream = outStream; diff --git a/CPP/7zip/Common/RegisterCodec.h b/CPP/7zip/Common/RegisterCodec.h index 6ce4a7f8..1f1cd4df 100755 --- a/CPP/7zip/Common/RegisterCodec.h +++ b/CPP/7zip/Common/RegisterCodec.h @@ -3,7 +3,7 @@ #ifndef __REGISTERCODEC_H #define __REGISTERCODEC_H -#include "../Common/MethodID.h" +#include "../Common/MethodId.h" typedef void * (*CreateCodecP)(); struct CCodecInfo diff --git a/CPP/7zip/Common/StreamBinder.cpp b/CPP/7zip/Common/StreamBinder.cpp index f83939cb..5db9d01f 100755 --- a/CPP/7zip/Common/StreamBinder.cpp +++ b/CPP/7zip/Common/StreamBinder.cpp @@ -51,31 +51,20 @@ STDMETHODIMP CSequentialOutStreamForBinder::Write(const void *data, UInt32 size, // CStreamBinder // (_thereAreBytesToReadEvent && _bufferSize == 0) means that stream is finished. -void CStreamBinder::CreateEvents() +HRes CStreamBinder::CreateEvents() { - _allBytesAreWritenEvent = new CManualResetEvent(true); - _thereAreBytesToReadEvent = new CManualResetEvent(false); - _readStreamIsClosedEvent = new CManualResetEvent(false); + RINOK(_allBytesAreWritenEvent.Create(true)); + RINOK(_thereAreBytesToReadEvent.Create()); + return _readStreamIsClosedEvent.Create(); } void CStreamBinder::ReInit() { - _thereAreBytesToReadEvent->Reset(); - _readStreamIsClosedEvent->Reset(); + _thereAreBytesToReadEvent.Reset(); + _readStreamIsClosedEvent.Reset(); ProcessedSize = 0; } -CStreamBinder::~CStreamBinder() -{ - if (_allBytesAreWritenEvent != NULL) - delete _allBytesAreWritenEvent; - if (_thereAreBytesToReadEvent != NULL) - delete _thereAreBytesToReadEvent; - if (_readStreamIsClosedEvent != NULL) - delete _readStreamIsClosedEvent; -} - - void CStreamBinder::CreateStreams(ISequentialInStream **inStream, @@ -103,8 +92,7 @@ HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize) UInt32 sizeToRead = size; if (size > 0) { - if(!_thereAreBytesToReadEvent->Lock()) - return E_FAIL; + RINOK(_thereAreBytesToReadEvent.Lock()); sizeToRead = MyMin(_bufferSize, size); if (_bufferSize > 0) { @@ -113,8 +101,8 @@ HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize) _bufferSize -= sizeToRead; if (_bufferSize == 0) { - _thereAreBytesToReadEvent->Reset(); - _allBytesAreWritenEvent->Set(); + _thereAreBytesToReadEvent.Reset(); + _allBytesAreWritenEvent.Set(); } } } @@ -126,7 +114,7 @@ HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize) void CStreamBinder::CloseRead() { - _readStreamIsClosedEvent->Set(); + _readStreamIsClosedEvent.Set(); } HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSize) @@ -135,12 +123,12 @@ HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSiz { _buffer = data; _bufferSize = size; - _allBytesAreWritenEvent->Reset(); - _thereAreBytesToReadEvent->Set(); + _allBytesAreWritenEvent.Reset(); + _thereAreBytesToReadEvent.Set(); HANDLE events[2]; - events[0] = *_allBytesAreWritenEvent; - events[1] = *_readStreamIsClosedEvent; + events[0] = _allBytesAreWritenEvent; + events[1] = _readStreamIsClosedEvent; DWORD waitResult = ::WaitForMultipleObjects(2, events, FALSE, INFINITE); if (waitResult != WAIT_OBJECT_0 + 0) { @@ -158,5 +146,5 @@ HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSiz void CStreamBinder::CloseWrite() { // _bufferSize must be = 0 - _thereAreBytesToReadEvent->Set(); + _thereAreBytesToReadEvent.Set(); } diff --git a/CPP/7zip/Common/StreamBinder.h b/CPP/7zip/Common/StreamBinder.h index a66c3acb..1de2372a 100755 --- a/CPP/7zip/Common/StreamBinder.h +++ b/CPP/7zip/Common/StreamBinder.h @@ -8,21 +8,16 @@ class CStreamBinder { - NWindows::NSynchronization::CManualResetEvent *_allBytesAreWritenEvent; - NWindows::NSynchronization::CManualResetEvent *_thereAreBytesToReadEvent; - NWindows::NSynchronization::CManualResetEvent *_readStreamIsClosedEvent; + NWindows::NSynchronization::CManualResetEvent _allBytesAreWritenEvent; + NWindows::NSynchronization::CManualResetEvent _thereAreBytesToReadEvent; + NWindows::NSynchronization::CManualResetEvent _readStreamIsClosedEvent; UInt32 _bufferSize; const void *_buffer; public: // bool ReadingWasClosed; UInt64 ProcessedSize; - CStreamBinder(): - _allBytesAreWritenEvent(NULL), - _thereAreBytesToReadEvent(NULL), - _readStreamIsClosedEvent(NULL) - {} - ~CStreamBinder(); - void CreateEvents(); + CStreamBinder() {} + HRes CreateEvents(); void CreateStreams(ISequentialInStream **inStream, ISequentialOutStream **outStream); |