other-licenses/7zstub/src/7zip/Archive/7z/7zMethodID.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 // 7zMethodID.cpp
     3 #include "StdAfx.h"
     5 #include "7zMethodID.h"
     7 namespace NArchive {
     8 namespace N7z {
    10 static wchar_t GetHex(Byte value)
    11 {
    12   return (value < 10) ? ('0' + value) : ('A' + (value - 10));
    13 }
    15 static bool HexCharToInt(wchar_t value, Byte &result)
    16 {
    17   if (value >= '0' && value <= '9')
    18     result = value - '0';
    19   else if (value >= 'a' && value <= 'f')
    20     result = 10 + value - 'a';
    21   else if (value >= 'A' && value <= 'F')
    22     result = 10 + value - 'A';
    23   else
    24     return false;
    25   return true;
    26 }
    28 static bool TwoHexCharsToInt(wchar_t valueHigh, wchar_t valueLow, Byte &result)
    29 {
    30   Byte resultHigh, resultLow;
    31   if (!HexCharToInt(valueHigh, resultHigh))
    32     return false;
    33   if (!HexCharToInt(valueLow, resultLow))
    34     return false;
    35   result = (resultHigh << 4) + resultLow;
    36   return true;
    37 }
    39 UString CMethodID::ConvertToString() const
    40 {
    41   UString result;
    42   for (int i = 0; i < IDSize; i++)
    43   {
    44     Byte b = ID[i];
    45     result += GetHex(b >> 4);
    46     result += GetHex(b & 0xF);
    47   }
    48   return result;
    49 }
    51 bool CMethodID::ConvertFromString(const UString &srcString)
    52 {
    53   int length = srcString.Length();
    54   if ((length & 1) != 0 || (length >> 1) > kMethodIDSize)
    55     return false;
    56   IDSize = length / 2;
    57   UInt32 i;
    58   for(i = 0; i < IDSize; i++)
    59     if (!TwoHexCharsToInt(srcString[i * 2], srcString[i * 2 + 1], ID[i]))
    60       return false;
    61   for(; i < kMethodIDSize; i++)
    62     ID[i] = 0;
    63   return true;
    64 }
    66 bool operator==(const CMethodID &a1, const CMethodID &a2)
    67 {
    68   if (a1.IDSize != a2.IDSize)
    69     return false;
    70   for (UInt32 i = 0; i < a1.IDSize; i++)
    71     if (a1.ID[i] != a2.ID[i])
    72       return false;
    73   return true;
    74 }
    76 }}

mercurial