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.

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

mercurial