zipin.cpp

来自「seven zip源代码」· C++ 代码 · 共 839 行 · 第 1/2 页

CPP
839
字号
  }
  else
    IncreaseRealPosition(item.PackSize);
  return S_OK;
}

HRESULT CInArchive::ReadLocalItemAfterCdItemFull(CItemEx &item)
{
  if (item.FromLocal)
    return S_OK;
  try
  {
    RINOK(ReadLocalItemAfterCdItem(item));
    if (item.HasDescriptor())
    {
      RINOK(Seek(m_ArchiveInfo.Base + item.GetDataPosition() + item.PackSize));
      if (ReadUInt32() != NSignature::kDataDescriptor)
        return S_FALSE;
      UInt32 crc = ReadUInt32();
      UInt64 packSize, unpackSize;

      /*
      if (IsZip64)
      {
        packSize = ReadUInt64();
        unpackSize = ReadUInt64();
      }
      else
      */
      {
        packSize = ReadUInt32();
        unpackSize = ReadUInt32();
      }

      if (crc != item.FileCRC || item.PackSize != packSize || item.UnPackSize != unpackSize)
        return S_FALSE;
    }
  }
  catch(...) { return S_FALSE; }
  return S_OK;
}
  
HRESULT CInArchive::ReadCdItem(CItemEx &item)
{
  item.FromCentral = true;
  item.MadeByVersion.Version = ReadByte();
  item.MadeByVersion.HostOS = ReadByte();
  item.ExtractVersion.Version = ReadByte();
  item.ExtractVersion.HostOS = ReadByte();
  item.Flags = ReadUInt16(); //  & NFileHeader::NFlags::kUsedBitsMask; 
  item.CompressionMethod = ReadUInt16();
  item.Time = ReadUInt32();
  item.FileCRC = ReadUInt32();
  item.PackSize = ReadUInt32();
  item.UnPackSize = ReadUInt32();
  UInt16 headerNameSize = ReadUInt16();
  UInt16 headerExtraSize = ReadUInt16();
  UInt16 headerCommentSize = ReadUInt16();
  UInt32 headerDiskNumberStart = ReadUInt16();
  item.InternalAttributes = ReadUInt16();
  item.ExternalAttributes = ReadUInt32();
  item.LocalHeaderPosition = ReadUInt32();
  item.Name = ReadFileName(headerNameSize);
  
  if (headerExtraSize > 0)
  {
    ReadExtra(headerExtraSize, item.CentralExtra, item.UnPackSize, item.PackSize, 
        item.LocalHeaderPosition, headerDiskNumberStart);
  }

  if (headerDiskNumberStart != 0)
    throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported);
  
  // May be these strings must be deleted
  /*
  if (item.IsDirectory())
    item.UnPackSize = 0;
  */
  
  ReadBuffer(item.Comment, headerCommentSize);
  return S_OK;
}

HRESULT CInArchive::TryEcd64(UInt64 offset, CCdInfo &cdInfo)
{
  RINOK(Seek(offset));
  const UInt32 kEcd64Size = 56;
  Byte buf[kEcd64Size];
  if(!ReadBytesAndTestSize(buf, kEcd64Size))
    return S_FALSE;
  if (GetUInt32(buf) != NSignature::kZip64EndOfCentralDir)
    return S_FALSE;
  // cdInfo.NumEntries = GetUInt64(buf + 24);
  cdInfo.Size = GetUInt64(buf + 40);
  cdInfo.Offset = GetUInt64(buf + 48);
  return S_OK;
}

HRESULT CInArchive::FindCd(CCdInfo &cdInfo)
{
  UInt64 endPosition;
  RINOK(m_Stream->Seek(0, STREAM_SEEK_END, &endPosition));
  const UInt32 kBufSizeMax = (1 << 16) + kEcdSize + kZip64EcdLocatorSize;
  Byte buf[kBufSizeMax];
  UInt32 bufSize = (endPosition < kBufSizeMax) ? (UInt32)endPosition : kBufSizeMax;
  if (bufSize < kEcdSize)
    return S_FALSE;
  UInt64 startPosition = endPosition - bufSize;
  RINOK(m_Stream->Seek(startPosition, STREAM_SEEK_SET, &m_Position));
  if (m_Position != startPosition)
    return S_FALSE;
  if (!ReadBytesAndTestSize(buf, bufSize))
    return S_FALSE;
  for (int i = (int)(bufSize - kEcdSize); i >= 0; i--) 
  {
    if (GetUInt32(buf + i) == NSignature::kEndOfCentralDir)
    {
      if (i >= kZip64EcdLocatorSize)
      {
        const Byte *locator = buf + i - kZip64EcdLocatorSize;
        if (GetUInt32(locator) == NSignature::kZip64EndOfCentralDirLocator)
        {
          UInt64 ecd64Offset = GetUInt64(locator + 8);
          if (TryEcd64(ecd64Offset, cdInfo) == S_OK)
            return S_OK;
          if (TryEcd64(m_ArchiveInfo.StartPosition + ecd64Offset, cdInfo) == S_OK)
          {
            m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition;
            return S_OK;
          }
        }
      }
      if (GetUInt32(buf + i + 4) == 0)
      {
        // cdInfo.NumEntries = GetUInt16(buf + i + 10);
        cdInfo.Size = GetUInt32(buf + i + 12);
        cdInfo.Offset = GetUInt32(buf + i + 16);
        UInt64 curPos = endPosition - bufSize + i;
        UInt64 cdEnd = cdInfo.Size + cdInfo.Offset;
        if (curPos > cdEnd)
          m_ArchiveInfo.Base = curPos - cdEnd;
        return S_OK;
      }
    }
  }
  return S_FALSE;
}

HRESULT CInArchive::TryReadCd(CObjectVector<CItemEx> &items, UInt64 cdOffset, UInt64 cdSize)
{
  items.Clear();
  RINOK(m_Stream->Seek(cdOffset, STREAM_SEEK_SET, &m_Position));
  if (m_Position != cdOffset)
    return S_FALSE;
  while(m_Position - cdOffset < cdSize)
  {
    if(ReadUInt32() != NSignature::kCentralFileHeader)
      return S_FALSE;
    CItemEx cdItem;
    RINOK(ReadCdItem(cdItem));
    items.Add(cdItem);
  }
  return (m_Position - cdOffset == cdSize) ? S_OK : S_FALSE;
}

HRESULT CInArchive::ReadCd(CObjectVector<CItemEx> &items, UInt64 &cdOffset, UInt64 &cdSize)
{
  m_ArchiveInfo.Base = 0;
  CCdInfo cdInfo;
  RINOK(FindCd(cdInfo));
  HRESULT res = S_FALSE;
  cdSize = cdInfo.Size;
  cdOffset = cdInfo.Offset;
  res = TryReadCd(items, m_ArchiveInfo.Base + cdOffset, cdSize);
  if (res == S_FALSE && m_ArchiveInfo.Base == 0)
  {
    res = TryReadCd(items, cdInfo.Offset + m_ArchiveInfo.StartPosition, cdSize);
    if (res == S_OK)
      m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition;
  }
  if (!ReadUInt32(m_Signature))
    return S_FALSE;
  return res;
}

HRESULT CInArchive::ReadLocalsAndCd(CObjectVector<CItemEx> &items, CProgressVirt *progress, UInt64 &cdOffset)
{
  items.Clear();
  while (m_Signature == NSignature::kLocalFileHeader)
  {
    // FSeek points to next byte after signature
    // NFileHeader::CLocalBlock localHeader;
    CItemEx item;
    item.LocalHeaderPosition = m_Position - m_StreamStartPosition - 4; // points to signature;
    RINOK(ReadLocalItem(item));
    item.FromLocal = true;
    ReadLocalItemDescriptor(item);
    items.Add(item);
    if (progress != 0)
    {
      UInt64 numItems = items.Size();
      RINOK(progress->SetCompleted(&numItems));
    }
    if (!ReadUInt32(m_Signature))
      break;
  }
  cdOffset = m_Position - 4;
  for(int i = 0; i < items.Size(); i++)
  {
    if (progress != 0)
    {
      UInt64 numItems = items.Size();
      RINOK(progress->SetCompleted(&numItems));
    }
    if(m_Signature != NSignature::kCentralFileHeader)
      return S_FALSE;

    CItemEx cdItem;
    RINOK(ReadCdItem(cdItem));

    if (i == 0)
    {
      if (cdItem.LocalHeaderPosition == 0)
        m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition;
    }

    int index;
    int left = 0, right = items.Size();
    for (;;)
    {
      if (left >= right)
        return S_FALSE;
      index = (left + right) / 2;
      UInt64 position = items[index].LocalHeaderPosition - m_ArchiveInfo.Base;
      if (cdItem.LocalHeaderPosition == position)
        break;
      if (cdItem.LocalHeaderPosition < position)
        right = index;
      else
        left = index + 1;
    }
    CItemEx &item = items[index];
    item.LocalHeaderPosition = cdItem.LocalHeaderPosition;
    item.MadeByVersion = cdItem.MadeByVersion;
    item.CentralExtra = cdItem.CentralExtra;

    if (
        // item.ExtractVersion != cdItem.ExtractVersion ||
        item.Flags != cdItem.Flags ||
        item.CompressionMethod != cdItem.CompressionMethod ||
        // item.Time != cdItem.Time ||
        item.FileCRC != cdItem.FileCRC)
      return S_FALSE;

    if (item.Name.Length() != cdItem.Name.Length() ||
        item.PackSize != cdItem.PackSize ||
        item.UnPackSize != cdItem.UnPackSize
      )
      return S_FALSE;
    item.Name = cdItem.Name;
    item.InternalAttributes = cdItem.InternalAttributes;
    item.ExternalAttributes = cdItem.ExternalAttributes;
    item.Comment = cdItem.Comment;
    item.FromCentral = cdItem.FromCentral;
    if (!ReadUInt32(m_Signature))
      return S_FALSE;
  }
  return S_OK;
}

HRESULT CInArchive::ReadHeaders(CObjectVector<CItemEx> &items, CProgressVirt *progress)
{
  // m_Signature must be kLocalFileHeaderSignature or
  // kEndOfCentralDirSignature
  // m_Position points to next byte after signature

  IsZip64 = false;
  items.Clear();
  if (progress != 0)
  {
    UInt64 numItems = items.Size();
    RINOK(progress->SetCompleted(&numItems));
  }

  UInt64 cdSize, cdStartOffset;
  HRESULT res = ReadCd(items, cdStartOffset, cdSize);
  if (res != S_FALSE && res != S_OK)
    return res;

  /*
  if (res != S_OK)
    return res;
  res = S_FALSE;
  */

  if (res == S_FALSE)
  {
    m_ArchiveInfo.Base = 0;
    RINOK(m_Stream->Seek(m_ArchiveInfo.StartPosition, STREAM_SEEK_SET, &m_Position));
    if (m_Position != m_ArchiveInfo.StartPosition)
      return S_FALSE;
    if (!ReadUInt32(m_Signature))
      return S_FALSE;
    RINOK(ReadLocalsAndCd(items, progress, cdStartOffset));
    cdSize = (m_Position - 4) - cdStartOffset;
    cdStartOffset -= m_ArchiveInfo.Base;
  }

  UInt32 thisDiskNumber = 0;
  UInt32 startCDDiskNumber = 0;
  UInt64 numEntriesInCDOnThisDisk = 0;
  UInt64 numEntriesInCD = 0;
  UInt64 cdSizeFromRecord = 0;
  UInt64 cdStartOffsetFromRecord = 0;
  bool isZip64 = false;
  UInt64 zip64EcdStartOffset = m_Position - 4 - m_ArchiveInfo.Base;
  if(m_Signature == NSignature::kZip64EndOfCentralDir)
  {
    IsZip64 = isZip64 = true;
    UInt64 recordSize = ReadUInt64();
    /* UInt16 versionMade = */ ReadUInt16();
    /* UInt16 versionNeedExtract = */ ReadUInt16();
    thisDiskNumber = ReadUInt32();
    startCDDiskNumber = ReadUInt32();
    numEntriesInCDOnThisDisk = ReadUInt64();
    numEntriesInCD = ReadUInt64();
    cdSizeFromRecord = ReadUInt64();
    cdStartOffsetFromRecord = ReadUInt64();
    IncreaseRealPosition(recordSize - kZip64EcdSize);
    if (!ReadUInt32(m_Signature))
      return S_FALSE;
    if (thisDiskNumber != 0 || startCDDiskNumber != 0)
      throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported);
    if (numEntriesInCDOnThisDisk != items.Size() ||
        numEntriesInCD != items.Size() ||
        cdSizeFromRecord != cdSize ||
        (cdStartOffsetFromRecord != cdStartOffset &&
        (!items.IsEmpty())))
      return S_FALSE;
  }
  if(m_Signature == NSignature::kZip64EndOfCentralDirLocator)
  {
    /* UInt32 startEndCDDiskNumber = */ ReadUInt32();
    UInt64 endCDStartOffset = ReadUInt64();
    /* UInt32 numberOfDisks = */ ReadUInt32();
    if (zip64EcdStartOffset != endCDStartOffset)
      return S_FALSE;
    if (!ReadUInt32(m_Signature))
      return S_FALSE;
  }
  if(m_Signature != NSignature::kEndOfCentralDir)
      return S_FALSE;

  UInt16 thisDiskNumber16 = ReadUInt16();
  if (!isZip64 || thisDiskNumber16)
    thisDiskNumber = thisDiskNumber16;

  UInt16 startCDDiskNumber16 = ReadUInt16();
  if (!isZip64 || startCDDiskNumber16 != 0xFFFF)
    startCDDiskNumber = startCDDiskNumber16;

  UInt16 numEntriesInCDOnThisDisk16 = ReadUInt16();
  if (!isZip64 || numEntriesInCDOnThisDisk16 != 0xFFFF)
    numEntriesInCDOnThisDisk = numEntriesInCDOnThisDisk16;

  UInt16 numEntriesInCD16 = ReadUInt16();
  if (!isZip64 || numEntriesInCD16 != 0xFFFF)
    numEntriesInCD = numEntriesInCD16;

  UInt32 cdSizeFromRecord32 = ReadUInt32();
  if (!isZip64 || cdSizeFromRecord32 != 0xFFFFFFFF)
    cdSizeFromRecord = cdSizeFromRecord32;

  UInt32 cdStartOffsetFromRecord32 = ReadUInt32();
  if (!isZip64 || cdStartOffsetFromRecord32 != 0xFFFFFFFF)
    cdStartOffsetFromRecord = cdStartOffsetFromRecord32;

  UInt16 commentSize = ReadUInt16();
  ReadBuffer(m_ArchiveInfo.Comment, commentSize);
  
  if (thisDiskNumber != 0 || startCDDiskNumber != 0)
    throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported);
  if ((UInt16)numEntriesInCDOnThisDisk != ((UInt16)items.Size()) ||
      (UInt16)numEntriesInCD != ((UInt16)items.Size()) ||
      (UInt32)cdSizeFromRecord != (UInt32)cdSize ||
      ((UInt32)(cdStartOffsetFromRecord) != (UInt32)cdStartOffset &&
        (!items.IsEmpty())))
      return S_FALSE;
  
  return S_OK;
}

ISequentialInStream* CInArchive::CreateLimitedStream(UInt64 position, UInt64 size)
{
  CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
  CMyComPtr<ISequentialInStream> inStream(streamSpec);
  SeekInArchive(m_ArchiveInfo.Base + position);
  streamSpec->SetStream(m_Stream);
  streamSpec->Init(size);
  return inStream.Detach();
}

IInStream* CInArchive::CreateStream()
{
  CMyComPtr<IInStream> inStream = m_Stream;
  return inStream.Detach();
}

bool CInArchive::SeekInArchive(UInt64 position)
{
  UInt64 newPosition;
  if(m_Stream->Seek(position, STREAM_SEEK_SET, &newPosition) != S_OK)
    return false;
  return (newPosition == position);
}

}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?