other-licenses/7zstub/src/7zip/Compress/Copy/CopyCoder.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // Compress/CopyCoder.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4
michael@0 5 #include "CopyCoder.h"
michael@0 6 #include "../../../Common/Alloc.h"
michael@0 7 #include "../../Common/StreamUtils.h"
michael@0 8
michael@0 9 namespace NCompress {
michael@0 10
michael@0 11 static const UInt32 kBufferSize = 1 << 17;
michael@0 12
michael@0 13 CCopyCoder::~CCopyCoder()
michael@0 14 {
michael@0 15 ::MidFree(_buffer);
michael@0 16 }
michael@0 17
michael@0 18 STDMETHODIMP CCopyCoder::Code(ISequentialInStream *inStream,
michael@0 19 ISequentialOutStream *outStream,
michael@0 20 const UInt64 *inSize, const UInt64 *outSize,
michael@0 21 ICompressProgressInfo *progress)
michael@0 22 {
michael@0 23 if (_buffer == 0)
michael@0 24 {
michael@0 25 _buffer = (Byte *)::MidAlloc(kBufferSize);
michael@0 26 if (_buffer == 0)
michael@0 27 return E_OUTOFMEMORY;
michael@0 28 }
michael@0 29
michael@0 30 TotalSize = 0;
michael@0 31 while(true)
michael@0 32 {
michael@0 33 UInt32 realProcessedSize;
michael@0 34 UInt32 size = kBufferSize;
michael@0 35 if (outSize != 0)
michael@0 36 if (size > *outSize - TotalSize)
michael@0 37 size = (UInt32)(*outSize - TotalSize);
michael@0 38 RINOK(inStream->Read(_buffer, size, &realProcessedSize));
michael@0 39 if(realProcessedSize == 0)
michael@0 40 break;
michael@0 41 RINOK(WriteStream(outStream, _buffer, realProcessedSize, NULL));
michael@0 42 TotalSize += realProcessedSize;
michael@0 43 if (progress != NULL)
michael@0 44 {
michael@0 45 RINOK(progress->SetRatioInfo(&TotalSize, &TotalSize));
michael@0 46 }
michael@0 47 }
michael@0 48 return S_OK;
michael@0 49 }
michael@0 50
michael@0 51 }
michael@0 52

mercurial