other-licenses/7zstub/src/7zip/Archive/Common/CoderMixer2.h

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 // CoderMixer2.h
     3 #ifndef __CODER_MIXER2_H
     4 #define __CODER_MIXER2_H
     6 #include "../../../Common/Vector.h"
     7 #include "../../../Common/Types.h"
     8 #include "../../../Common/MyCom.h"
     9 #include "../../ICoder.h"
    11 namespace NCoderMixer2 {
    13 struct CBindPair
    14 {
    15   UInt32 InIndex;
    16   UInt32 OutIndex;
    17 };
    19 struct CCoderStreamsInfo
    20 {
    21   UInt32 NumInStreams;
    22   UInt32 NumOutStreams;
    23 };
    25 struct CBindInfo
    26 {
    27   CRecordVector<CCoderStreamsInfo> Coders;
    28   CRecordVector<CBindPair> BindPairs;
    29   CRecordVector<UInt32> InStreams;
    30   CRecordVector<UInt32> OutStreams;
    32   void Clear()
    33   {
    34     Coders.Clear();
    35     BindPairs.Clear();
    36     InStreams.Clear();
    37     OutStreams.Clear();
    38   }
    40   /*
    41   UInt32 GetCoderStartOutStream(UInt32 coderIndex) const
    42   {
    43     UInt32 numOutStreams = 0;
    44     for (UInt32 i = 0; i < coderIndex; i++)
    45       numOutStreams += Coders[i].NumOutStreams;
    46     return numOutStreams;
    47   }
    48   */
    51   void GetNumStreams(UInt32 &numInStreams, UInt32 &numOutStreams) const
    52   {
    53     numInStreams = 0;
    54     numOutStreams = 0;
    55     for (int i = 0; i < Coders.Size(); i++)
    56     {
    57       const CCoderStreamsInfo &coderStreamsInfo = Coders[i];
    58       numInStreams += coderStreamsInfo.NumInStreams;
    59       numOutStreams += coderStreamsInfo.NumOutStreams;
    60     }
    61   }
    63   int FindBinderForInStream(UInt32 inStream) const
    64   {
    65     for (int i = 0; i < BindPairs.Size(); i++)
    66       if (BindPairs[i].InIndex == inStream)
    67         return i;
    68     return -1;
    69   }
    70   int FindBinderForOutStream(UInt32 outStream) const
    71   {
    72     for (int i = 0; i < BindPairs.Size(); i++)
    73       if (BindPairs[i].OutIndex == outStream)
    74         return i;
    75     return -1;
    76   }
    78   UInt32 GetCoderInStreamIndex(UInt32 coderIndex) const
    79   {
    80     UInt32 streamIndex = 0;
    81     for (UInt32 i = 0; i < coderIndex; i++)
    82       streamIndex += Coders[i].NumInStreams;
    83     return streamIndex;
    84   }
    86   UInt32 GetCoderOutStreamIndex(UInt32 coderIndex) const
    87   {
    88     UInt32 streamIndex = 0;
    89     for (UInt32 i = 0; i < coderIndex; i++)
    90       streamIndex += Coders[i].NumOutStreams;
    91     return streamIndex;
    92   }
    95   void FindInStream(UInt32 streamIndex, UInt32 &coderIndex, 
    96       UInt32 &coderStreamIndex) const
    97   {
    98     for (coderIndex = 0; coderIndex < (UInt32)Coders.Size(); coderIndex++)
    99     {
   100       UInt32 curSize = Coders[coderIndex].NumInStreams;
   101       if (streamIndex < curSize)
   102       {
   103         coderStreamIndex = streamIndex;
   104         return;
   105       }
   106       streamIndex -= curSize;
   107     }
   108     throw 1;
   109   }
   110   void FindOutStream(UInt32 streamIndex, UInt32 &coderIndex, 
   111       UInt32 &coderStreamIndex) const
   112   {
   113     for (coderIndex = 0; coderIndex < (UInt32)Coders.Size(); coderIndex++)
   114     {
   115       UInt32 curSize = Coders[coderIndex].NumOutStreams;
   116       if (streamIndex < curSize)
   117       {
   118         coderStreamIndex = streamIndex;
   119         return;
   120       }
   121       streamIndex -= curSize;
   122     }
   123     throw 1;
   124   }
   125 };
   127 class CBindReverseConverter
   128 {
   129   UInt32 _numSrcOutStreams;
   130   const NCoderMixer2::CBindInfo _srcBindInfo;
   131   CRecordVector<UInt32> _srcInToDestOutMap;
   132   CRecordVector<UInt32> _srcOutToDestInMap;
   133   CRecordVector<UInt32> _destInToSrcOutMap;
   134 public:
   135   UInt32 NumSrcInStreams;
   136   CRecordVector<UInt32> DestOutToSrcInMap;
   138   CBindReverseConverter(const NCoderMixer2::CBindInfo &srcBindInfo);
   139   void CreateReverseBindInfo(NCoderMixer2::CBindInfo &destBindInfo);
   140 };
   142 struct CCoderInfo
   143 {
   144   CMyComPtr<ICompressCoder> Coder;
   145   CMyComPtr<ICompressCoder2> Coder2;
   146   UInt32 NumInStreams;
   147   UInt32 NumOutStreams;
   149   CRecordVector<UInt64> InSizes;
   150   CRecordVector<UInt64> OutSizes;
   151   CRecordVector<const UInt64 *> InSizePointers;
   152   CRecordVector<const UInt64 *> OutSizePointers;
   154   CCoderInfo(UInt32 numInStreams, UInt32 numOutStreams);
   155   void SetCoderInfo(const UInt64 **inSizes, const UInt64 **outSizes);
   156 };
   158 class CCoderMixer2
   159 {
   160 public:
   161   virtual void SetBindInfo(const CBindInfo &bindInfo) = 0;
   162   virtual void ReInit() = 0;
   163   virtual void SetCoderInfo(UInt32 coderIndex, const UInt64 **inSizes, const UInt64 **outSizes) = 0;
   164 };
   166 }
   167 #endif

mercurial