Esempio n. 1
0
HRESULT CZipDecoder::Decode(
    DECL_EXTERNAL_CODECS_LOC_VARS
    CInArchive &archive, const CItemEx &item,
    ISequentialOutStream *realOutStream,
    IArchiveExtractCallback *extractCallback,
    ICompressProgressInfo *compressProgress,
    UInt32 numThreads, Int32 &res)
{
  res = NArchive::NExtract::NOperationResult::kDataError;
  CInStreamReleaser inStreamReleaser;

  bool needCRC = true;
  bool wzAesMode = false;
  bool pkAesMode = false;
  UInt16 methodId = item.CompressionMethod;
  if (item.IsEncrypted())
  {
    if (item.IsStrongEncrypted())
    {
      CStrongCryptoField f;
      if (item.CentralExtra.GetStrongCryptoField(f))
      {
        pkAesMode = true;
      }
      if (!pkAesMode)
      {
        res = NArchive::NExtract::NOperationResult::kUnSupportedMethod;
        return S_OK;
      }
    }
    if (methodId == NFileHeader::NCompressionMethod::kWzAES)
    {
      CWzAesExtraField aesField;
      if (item.CentralExtra.GetWzAesField(aesField))
      {
        wzAesMode = true;
        needCRC = aesField.NeedCrc();
      }
    }
  }
    
  COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC;
  CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
  outStreamSpec->SetStream(realOutStream);
  outStreamSpec->Init(needCRC);
  
  UInt64 authenticationPos;
  
  CMyComPtr<ISequentialInStream> inStream;
  {
    UInt64 packSize = item.PackSize;
    if (wzAesMode)
    {
      if (packSize < NCrypto::NWzAes::kMacSize)
        return S_OK;
      packSize -= NCrypto::NWzAes::kMacSize;
    }
    UInt64 dataPos = item.GetDataPosition();
    inStream.Attach(archive.CreateLimitedStream(dataPos, packSize));
    authenticationPos = dataPos + packSize;
  }
  
  CMyComPtr<ICompressFilter> cryptoFilter;
  if (item.IsEncrypted())
  {
    if (wzAesMode)
    {
      CWzAesExtraField aesField;
      if (!item.CentralExtra.GetWzAesField(aesField))
        return S_OK;
      methodId = aesField.Method;
      if (!_wzAesDecoder)
      {
        _wzAesDecoderSpec = new NCrypto::NWzAes::CDecoder;
        _wzAesDecoder = _wzAesDecoderSpec;
      }
      cryptoFilter = _wzAesDecoder;
      Byte properties = aesField.Strength;
      RINOK(_wzAesDecoderSpec->SetDecoderProperties2(&properties, 1));
    }
    else if (pkAesMode)
    {
      if (!_pkAesDecoder)
      {
        _pkAesDecoderSpec = new NCrypto::NZipStrong::CDecoder;
        _pkAesDecoder = _pkAesDecoderSpec;
      }
      cryptoFilter = _pkAesDecoder;
    }
    else
    {
      if (!_zipCryptoDecoder)
      {
        _zipCryptoDecoderSpec = new NCrypto::NZip::CDecoder;
        _zipCryptoDecoder = _zipCryptoDecoderSpec;
      }
      cryptoFilter = _zipCryptoDecoder;
    }
    CMyComPtr<ICryptoSetPassword> cryptoSetPassword;
    RINOK(cryptoFilter.QueryInterface(IID_ICryptoSetPassword, &cryptoSetPassword));
    
    if (!getTextPassword)
      extractCallback->QueryInterface(IID_ICryptoGetTextPassword, (void **)&getTextPassword);
    
    if (getTextPassword)
    {
      CMyComBSTR password;
      RINOK(getTextPassword->CryptoGetTextPassword(&password));
      AString charPassword;
      if (wzAesMode || pkAesMode)
      {
        charPassword = UnicodeStringToMultiByte((const wchar_t *)password, CP_ACP);
        /*
        for (int i = 0;; i++)
        {
          wchar_t c = password[i];
          if (c == 0)
            break;
          if (c >= 0x80)
          {
            res = NArchive::NExtract::NOperationResult::kDataError;
            return S_OK;
          }
          charPassword += (char)c;
        }
        */
      }
      else
      {
        // we use OEM. WinZip/Windows probably use ANSI for some files
        charPassword = UnicodeStringToMultiByte((const wchar_t *)password, CP_OEMCP);
      }
      HRESULT result = cryptoSetPassword->CryptoSetPassword(
        (const Byte *)(const char *)charPassword, charPassword.Length());
      if (result != S_OK)
        return S_OK;
    }
    else
    {
      RINOK(cryptoSetPassword->CryptoSetPassword(0, 0));
    }
  }
  
  int m;
  for (m = 0; m < methodItems.Size(); m++)
    if (methodItems[m].ZipMethod == methodId)
      break;

  if (m == methodItems.Size())
  {
    CMethodItem mi;
    mi.ZipMethod = methodId;
    if (methodId == NFileHeader::NCompressionMethod::kStored)
      mi.Coder = new NCompress::CCopyCoder;
    else if (methodId == NFileHeader::NCompressionMethod::kShrunk)
      mi.Coder = new NCompress::NShrink::CDecoder;
    else if (methodId == NFileHeader::NCompressionMethod::kImploded)
      mi.Coder = new NCompress::NImplode::NDecoder::CCoder;
    else if (methodId == NFileHeader::NCompressionMethod::kLZMA)
      mi.Coder = new CLzmaDecoder;
    else
    {
      CMethodId szMethodID;
      if (methodId == NFileHeader::NCompressionMethod::kBZip2)
        szMethodID = kMethodId_BZip2;
      else
      {
        if (methodId > 0xFF)
        {
          res = NArchive::NExtract::NOperationResult::kUnSupportedMethod;
          return S_OK;
        }
        szMethodID = kMethodId_ZipBase + (Byte)methodId;
      }

      RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS szMethodID, mi.Coder, false));

      if (mi.Coder == 0)
      {
        res = NArchive::NExtract::NOperationResult::kUnSupportedMethod;
        return S_OK;
      }
    }
    m = methodItems.Add(mi);
  }
  ICompressCoder *coder = methodItems[m].Coder;
  
  {
    CMyComPtr<ICompressSetDecoderProperties2> setDecoderProperties;
    coder->QueryInterface(IID_ICompressSetDecoderProperties2, (void **)&setDecoderProperties);
    if (setDecoderProperties)
    {
      Byte properties = (Byte)item.Flags;
      RINOK(setDecoderProperties->SetDecoderProperties2(&properties, 1));
    }
  }
  
  #ifdef COMPRESS_MT
  {
    CMyComPtr<ICompressSetCoderMt> setCoderMt;
    coder->QueryInterface(IID_ICompressSetCoderMt, (void **)&setCoderMt);
    if (setCoderMt)
    {
      RINOK(setCoderMt->SetNumberOfThreads(numThreads));
    }
  }
  #endif
  
  {
    HRESULT result = S_OK;
    CMyComPtr<ISequentialInStream> inStreamNew;
    if (item.IsEncrypted())
    {
      if (!filterStream)
      {
        filterStreamSpec = new CFilterCoder;
        filterStream = filterStreamSpec;
      }
      filterStreamSpec->Filter = cryptoFilter;
      if (wzAesMode)
      {
        result = _wzAesDecoderSpec->ReadHeader(inStream);
      }
      else if (pkAesMode)
      {
        result =_pkAesDecoderSpec->ReadHeader(inStream, item.FileCRC, item.UnPackSize);
        if (result == S_OK)
        {
          bool passwOK;
          result = _pkAesDecoderSpec->CheckPassword(passwOK);
          if (result == S_OK && !passwOK)
            result = S_FALSE;
        }
      }
      else
      {
        result = _zipCryptoDecoderSpec->ReadHeader(inStream);
      }

      if (result == S_OK)
      {
        RINOK(filterStreamSpec->SetInStream(inStream));
        inStreamReleaser.FilterCoder = filterStreamSpec;
        inStreamNew = filterStream;
        if (wzAesMode)
        {
          if (!_wzAesDecoderSpec->CheckPasswordVerifyCode())
            result = S_FALSE;
        }
      }
    }
    else
      inStreamNew = inStream;
    if (result == S_OK)
      result = coder->Code(inStreamNew, outStream, NULL, &item.UnPackSize, compressProgress);
    if (result == S_FALSE)
      return S_OK;
    if (result == E_NOTIMPL)
    {
      res = NArchive::NExtract::NOperationResult::kUnSupportedMethod;
      return S_OK;
    }

    RINOK(result);
  }
  bool crcOK = true;
  bool authOk = true;
  if (needCRC)
    crcOK = (outStreamSpec->GetCRC() == item.FileCRC);
  if (wzAesMode)
  {
    inStream.Attach(archive.CreateLimitedStream(authenticationPos, NCrypto::NWzAes::kMacSize));
    if (_wzAesDecoderSpec->CheckMac(inStream, authOk) != S_OK)
      authOk = false;
  }
  
  res = ((crcOK && authOk) ?
    NArchive::NExtract::NOperationResult::kOK :
    NArchive::NExtract::NOperationResult::kCRCError);
  return S_OK;
}
Esempio n. 2
0
HRESULT CZipDecoder::Decode(
    CInArchive &archive, const CItemEx &item,
    ISequentialOutStream *realOutStream,
    IArchiveExtractCallback *_extractCallback,
    quint32 numThreads, qint32 &res)
{
    RefPtr<IArchiveExtractCallback> extractCallback(_extractCallback);

    res = NArchive::NExtract::NOperationResult::kDataError;
    CInStreamReleaser inStreamReleaser;

    bool needCRC = true;
    bool aesMode = false;
#ifdef ZIP_STRONG_SUPORT
    bool pkAesMode = false;
#endif
    quint16 methodId = item.CompressionMethod;
    if (item.IsEncrypted()) {
        if (item.IsStrongEncrypted()) {
#ifdef ZIP_STRONG_SUPORT
            CStrongCryptoField f;
            if (item.CentralExtra.GetStrongCryptoField(f)) {
                pkAesMode = true;
            }
            if (!pkAesMode)
#endif
            {
                res = NArchive::NExtract::NOperationResult::kUnSupportedMethod;
                return S_OK;
            }
        }
        if (methodId == NFileHeader::NCompressionMethod::kWzAES) {
            CWzAesExtraField aesField;
            if (item.CentralExtra.GetWzAesField(aesField)) {
                aesMode = true;
                needCRC = aesField.NeedCrc();
            }
        }
    }

    COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC;
    RefPtr<ISequentialOutStream> outStream = outStreamSpec;
    outStreamSpec->SetStream(realOutStream);
    outStreamSpec->Init(needCRC);

    quint64 authenticationPos;

    RefPtr<ISequentialInStream> inStream;
    {
        quint64 packSize = item.PackSize;
        if (aesMode) {
            if (packSize < NCrypto::NWzAES::kMacSize)
                return S_OK;
            packSize -= NCrypto::NWzAES::kMacSize;
        }
        quint64 dataPos = item.GetDataPosition();
        inStream = archive.CreateLimitedStream(dataPos, packSize);
        authenticationPos = dataPos + packSize;
    }

    RefPtr<ICompressFilter> cryptoFilter;
    if (item.IsEncrypted()) {
        if (aesMode) {
            CWzAesExtraField aesField;
            if (!item.CentralExtra.GetWzAesField(aesField))
                return S_OK;
            methodId = aesField.Method;
            if (!_aesDecoder) {
                _aesDecoderSpec = new NCrypto::NWzAES::CDecoder;
                _aesDecoder = _aesDecoderSpec;
            }
            cryptoFilter = _aesDecoder;
            quint8 properties = aesField.Strength;
            RINOK(_aesDecoderSpec->SetDecoderProperties2(&properties, 1));
        }
#ifdef ZIP_STRONG_SUPORT
        else if (pkAesMode) {
            if (!_zsDecoder) {
                _zsDecoderSpec = new NCrypto::NZipStrong::CDecoder;
                _zsDecoder = _zsDecoderSpec;
            }
            cryptoFilter = _zsDecoder;
        }
#endif
        else {
            if (!_zipCryptoDecoder) {
                _zipCryptoDecoderSpec = new NCrypto::NZip::CDecoder;
                _zipCryptoDecoder = _zipCryptoDecoderSpec;
            }
            cryptoFilter = _zipCryptoDecoder;
        }
        RefPtr<ICryptoSetPassword> cryptoSetPassword;
        RINOK(cryptoFilter.QueryInterface(&cryptoSetPassword));

        if (!getTextPassword)
            extractCallback.QueryInterface(&getTextPassword);

        if (getTextPassword) {
            QString password;
            RINOK(getTextPassword->CryptoGetTextPassword(&password));
            QByteArray charPassword;
            if (aesMode
#ifdef ZIP_STRONG_SUPORT
                    || pkAesMode
#endif
               ) {
                charPassword = password.toUtf8();
                /*
                for (int i = 0;; i++)
                {
                  wchar_t c = password[i];
                  if (c == 0)
                    break;
                  if (c >= 0x80)
                  {
                    res = NArchive::NExtract::NOperationResult::kDataError;
                    return S_OK;
                  }
                  charPassword += (char)c;
                }
                */
            } else {
                // we use OEM. WinZip/Windows probably use ANSI for some files
                charPassword = password.toLocal8Bit();
            }
            HRESULT res = cryptoSetPassword->CryptoSetPassword(
                              (const quint8 *)charPassword.constData(), charPassword.length());
            if (res != S_OK)
                return S_OK;
        } else {
            RINOK(cryptoSetPassword->CryptoSetPassword(0, 0));
        }
    }

    int m;
    for (m = 0; m < methodItems.size(); m++)
        if (methodItems[m]->ZipMethod == methodId)
            break;

    if (m == methodItems.size()) {
        CMethodItem mi;
        mi.ZipMethod = methodId;
        if (methodId == NFileHeader::NCompressionMethod::kStored)
            mi.Coder = new NCompress::CCopyCoder;
        else if (methodId == NFileHeader::NCompressionMethod::kShrunk)
            mi.Coder = new NCompress::NShrink::CDecoder;
        else if (methodId == NFileHeader::NCompressionMethod::kImploded)
            mi.Coder = new NCompress::NImplode::NDecoder::CCoder;
        else {
            CMethodId szMethodID;
            if (methodId == NFileHeader::NCompressionMethod::kBZip2)
                szMethodID = kMethodId_BZip2;
            else {
                if (methodId > 0xFF) {
                    res = NArchive::NExtract::NOperationResult::kUnSupportedMethod;
                    return S_OK;
                }
                szMethodID = kMethodId_ZipBase + (quint8)methodId;
            }

            QObject *dec = CreateDecoderForId(szMethodID);
            if (dec)
                mi.Coder = qobject_cast<ICompressCoder *>(dec);

            if (mi.Coder == 0) {
                delete dec;
                res = NArchive::NExtract::NOperationResult::kUnSupportedMethod;
                return S_OK;
            }
        }
        methodItems.append(new CMethodItem(mi));
        m = methodItems.size() - 1;
    }
    RefPtr<ICompressCoder> coder(methodItems[m]->Coder);

    {
        RefPtr<ICompressSetDecoderProperties2> setDecoderProperties;
        coder.QueryInterface(&setDecoderProperties);
        if (setDecoderProperties) {
            quint8 properties = (quint8)item.Flags;
            RINOK(setDecoderProperties->SetDecoderProperties2(&properties, 1));
        }
    }

#ifdef THREADED
    {
        RefPtr<ICompressSetCoderMt> setCoderMt;
        coder.QueryInterface(&setCoderMt);
        if (setCoderMt) {
            RINOK(setCoderMt->SetNumberOfThreads(numThreads));
        }
    }
#endif
    {
        HRESULT result;
        RefPtr<ISequentialInStream> inStreamNew;
        if (item.IsEncrypted()) {
            if (!filterStream) {
                filterStreamSpec = new CFilterCoder;
                filterStream = filterStreamSpec;
            }
            filterStreamSpec->setFilter(cryptoFilter);
            if (aesMode) {
                RINOK(_aesDecoderSpec->ReadHeader(inStream));
            }
#ifdef ZIP_STRONG_SUPORT
            else if (pkAesMode) {
                RINOK(_zsDecoderSpec->ReadHeader(inStream));
            }
#endif
            else {
                RINOK(_zipCryptoDecoderSpec->ReadHeader(inStream));
            }
            RINOK(filterStreamSpec->SetInStream(inStream));
            inStreamReleaser.FilterCoder = filterStreamSpec;
            inStreamNew = filterStream;

            if (aesMode) {
                if (!_aesDecoderSpec->CheckPasswordVerifyCode())
                    return S_OK;
            }
        } else
            inStreamNew = inStream;
        result = coder->Code(inStreamNew, outStream, NULL, &item.UnPackSize);
        if (result == S_FALSE)
            return S_OK;
        RINOK(result);
    }
    bool crcOK = true;
    bool authOk = true;
    if (needCRC)
        crcOK = (outStreamSpec->GetCRC() == item.FileCRC);
    if (aesMode) {
        inStream = archive.CreateLimitedStream(authenticationPos, NCrypto::NWzAES::kMacSize);
        if (_aesDecoderSpec->CheckMac(inStream, authOk) != S_OK)
            authOk = false;
    }

    res = ((crcOK && authOk) ?
           NArchive::NExtract::NOperationResult::kOK :
           NArchive::NExtract::NOperationResult::kCRCError);
    return S_OK;
}