5 #include "Common/StringConvert.h"
6 #include "Common/Defs.h"
7 #include "Common/UTFConvert.h"
8 #include "Common/ComTry.h"
10 #include "Windows/PropVariant.h"
11 #include "Windows/Time.h"
13 #include "../../Common/LimitedStreams.h"
14 #include "../../Common/StreamUtils.h"
15 #include "../../Common/ProgressUtils.h"
17 #include "../../Compress/Copy/CopyCoder.h"
18 #include "../../Compress/Lzx/LzxDecoder.h"
20 #include "../Common/ItemNameUtils.h"
22 #include "ChmHandler.h"
25 using namespace NWindows;
26 using namespace NTime;
31 // #define _CHM_DETAILS
37 kpidSection = kpidUserDefined
42 STATPROPSTG kProps[] =
44 { NULL, kpidPath, VT_BSTR},
45 // { NULL, kpidIsFolder, VT_BOOL},
46 { NULL, kpidSize, VT_UI8},
47 { NULL, kpidMethod, VT_BSTR},
48 { NULL, kpidBlock, VT_UI4}
52 { L"Section", kpidSection, VT_UI4},
53 { NULL, kpidOffset, VT_UI4}
57 STATPROPSTG kArcProps[] =
59 { NULL, kpidNumBlocks, VT_UI8}
64 IMP_IInArchive_ArcProps_NO
66 IMP_IInArchive_ArcProps
68 STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
71 NWindows::NCOM::CPropVariant prop;
77 for (int i = 0; i < m_Database.Sections.Size(); i++)
79 const CSectionInfo &s = m_Database.Sections[i];
80 for (int j = 0; j < s.Methods.Size(); j++)
82 const CMethodInfo &m = s.Methods[j];
84 numBlocks += m.LzxInfo.ResetTable.GetNumBlocks();
97 STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
100 NWindows::NCOM::CPropVariant prop;
101 if (m_Database.NewFormat)
106 prop = (UInt64)m_Database.NewFormatString.Length();
113 if (m_Database.LowLevel)
116 entryIndex = m_Database.Indices[index];
117 const CItem &item = m_Database.Items[entryIndex];
123 if (ConvertUTF8ToUnicode(item.Name, us))
125 if (!m_Database.LowLevel)
131 prop = NItemName::GetOSName2(us);
136 prop = item.IsDirectory();
143 if (!item.IsDirectory())
144 if (item.Section == 0)
146 else if (item.Section < m_Database.Sections.Size())
147 prop = m_Database.Sections[(int)item.Section].GetMethodName();
151 if (m_Database.LowLevel)
153 else if (item.Section != 0)
154 prop = m_Database.GetFolder(index);
160 prop = (UInt32)item.Section;
163 prop = (UInt32)item.Offset;
173 class CPropgressImp: public CProgressVirt
175 CMyComPtr<IArchiveOpenCallback> m_OpenArchiveCallback;
177 STDMETHOD(SetTotal)(const UInt64 *numFiles);
178 STDMETHOD(SetCompleted)(const UInt64 *numFiles);
179 void Init(IArchiveOpenCallback *openArchiveCallback)
180 { m_OpenArchiveCallback = openArchiveCallback; }
183 STDMETHODIMP CPropgressImp::SetTotal(const UInt64 *numFiles)
185 if (m_OpenArchiveCallback)
186 return m_OpenArchiveCallback->SetCompleted(numFiles, NULL);
190 STDMETHODIMP CPropgressImp::SetCompleted(const UInt64 *numFiles)
192 if (m_OpenArchiveCallback)
193 return m_OpenArchiveCallback->SetCompleted(numFiles, NULL);
197 STDMETHODIMP CHandler::Open(IInStream *inStream,
198 const UInt64 *maxCheckStartPosition,
199 IArchiveOpenCallback *openArchiveCallback)
206 CPropgressImp progressImp;
207 progressImp.Init(openArchiveCallback);
208 RINOK(archive.Open(inStream, maxCheckStartPosition, m_Database));
210 if (m_Database.LowLevel)
223 STDMETHODIMP CHandler::Close()
230 class CChmFolderOutStream:
231 public ISequentialOutStream,
237 HRESULT Write2(const void *data, UInt32 size, UInt32 *processedSize, bool isOK);
238 STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
241 UInt64 m_PosInFolder;
242 UInt64 m_PosInSection;
243 const CRecordVector<bool> *m_ExtractStatuses;
249 const CFilesDatabase *m_Database;
250 CMyComPtr<IArchiveExtractCallback> m_ExtractCallback;
255 UInt64 m_RemainFileSize;
256 CMyComPtr<ISequentialOutStream> m_RealOutStream;
259 HRESULT WriteEmptyFiles();
262 const CFilesDatabase *database,
263 IArchiveExtractCallback *extractCallback,
265 HRESULT FlushCorrupted(UInt64 maxSize);
268 void CChmFolderOutStream::Init(
269 const CFilesDatabase *database,
270 IArchiveExtractCallback *extractCallback,
273 m_Database = database;
274 m_ExtractCallback = extractCallback;
275 m_TestMode = testMode;
278 m_FileIsOpen = false;
281 HRESULT CChmFolderOutStream::OpenFile()
283 Int32 askMode = (*m_ExtractStatuses)[m_CurrentIndex] ? (m_TestMode ?
284 NExtract::NAskMode::kTest :
285 NExtract::NAskMode::kExtract) :
286 NExtract::NAskMode::kSkip;
287 m_RealOutStream.Release();
288 RINOK(m_ExtractCallback->GetStream(m_StartIndex + m_CurrentIndex, &m_RealOutStream, askMode));
289 if (!m_RealOutStream && !m_TestMode)
290 askMode = NArchive::NExtract::NAskMode::kSkip;
291 return m_ExtractCallback->PrepareOperation(askMode);
294 HRESULT CChmFolderOutStream::WriteEmptyFiles()
298 for (;m_CurrentIndex < m_NumFiles; m_CurrentIndex++)
300 UInt64 fileSize = m_Database->GetFileSize(m_StartIndex + m_CurrentIndex);
303 HRESULT result = OpenFile();
304 m_RealOutStream.Release();
306 RINOK(m_ExtractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK));
311 // This is WritePart function
312 HRESULT CChmFolderOutStream::Write2(const void *data, UInt32 size, UInt32 *processedSize, bool isOK)
314 UInt32 realProcessed = 0;
315 if (processedSize != NULL)
321 UInt32 numBytesToWrite = (UInt32)MyMin(m_RemainFileSize, (UInt64)(size));
323 if (numBytesToWrite > 0)
329 UInt32 processedSizeLocal = 0;
330 res = m_RealOutStream->Write((const Byte *)data, numBytesToWrite, &processedSizeLocal);
331 numBytesToWrite = processedSizeLocal;
334 realProcessed += numBytesToWrite;
335 if (processedSize != NULL)
336 *processedSize = realProcessed;
337 data = (const void *)((const Byte *)data + numBytesToWrite);
338 size -= numBytesToWrite;
339 m_RemainFileSize -= numBytesToWrite;
340 m_PosInSection += numBytesToWrite;
341 m_PosInFolder += numBytesToWrite;
344 if (m_RemainFileSize == 0)
346 m_RealOutStream.Release();
347 RINOK(m_ExtractCallback->SetOperationResult(
349 NArchive::NExtract::NOperationResult::kOK:
350 NArchive::NExtract::NOperationResult::kDataError));
351 m_FileIsOpen = false;
353 if (realProcessed > 0)
354 break; // with this break this function works as write part
358 if (m_CurrentIndex >= m_NumFiles)
360 int fullIndex = m_StartIndex + m_CurrentIndex;
361 m_RemainFileSize = m_Database->GetFileSize(fullIndex);
362 UInt64 fileOffset = m_Database->GetFileOffset(fullIndex);
363 if (fileOffset < m_PosInSection)
365 if (fileOffset > m_PosInSection)
367 UInt32 numBytesToWrite = (UInt32)MyMin(fileOffset - m_PosInSection, UInt64(size));
368 realProcessed += numBytesToWrite;
369 if (processedSize != NULL)
370 *processedSize = realProcessed;
371 data = (const void *)((const Byte *)data + numBytesToWrite);
372 size -= numBytesToWrite;
373 m_PosInSection += numBytesToWrite;
374 m_PosInFolder += numBytesToWrite;
376 if (fileOffset == m_PosInSection)
385 return WriteEmptyFiles();
388 STDMETHODIMP CChmFolderOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
390 return Write2(data, size, processedSize, true);
393 HRESULT CChmFolderOutStream::FlushCorrupted(UInt64 maxSize)
395 const UInt32 kBufferSize = (1 << 10);
396 Byte buffer[kBufferSize];
397 for (int i = 0; i < kBufferSize; i++)
399 if (maxSize > m_FolderSize)
400 maxSize = m_FolderSize;
401 while (m_PosInFolder < maxSize)
403 UInt32 size = (UInt32)MyMin(maxSize - m_PosInFolder, (UInt64)kBufferSize);
404 UInt32 processedSizeLocal = 0;
405 RINOK(Write2(buffer, size, &processedSizeLocal, false));
406 if (processedSizeLocal == 0)
413 STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems,
414 Int32 _aTestMode, IArchiveExtractCallback *extractCallback)
417 bool allFilesMode = (numItems == UInt32(-1));
420 numItems = m_Database.NewFormat ? 1:
421 (m_Database.LowLevel ?
422 m_Database.Items.Size():
423 m_Database.Indices.Size());
426 bool testMode = (_aTestMode != 0);
428 UInt64 currentTotalSize = 0;
430 NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder();
431 CMyComPtr<ICompressCoder> copyCoder = copyCoderSpec;
434 CLocalProgress *lps = new CLocalProgress;
435 CMyComPtr<ICompressProgressInfo> progress = lps;
436 lps->Init(extractCallback, false);
438 CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
439 CMyComPtr<ISequentialInStream> inStream(streamSpec);
440 streamSpec->SetStream(m_Stream);
442 if (m_Database.LowLevel)
444 UInt64 currentItemSize = 0;
445 UInt64 totalSize = 0;
446 if (m_Database.NewFormat)
447 totalSize = m_Database.NewFormatString.Length();
449 for (i = 0; i < numItems; i++)
450 totalSize += m_Database.Items[allFilesMode ? i : indices[i]].Size;
451 extractCallback->SetTotal(totalSize);
453 for (i = 0; i < numItems; i++, currentTotalSize += currentItemSize)
456 lps->InSize = currentTotalSize; // Change it
457 lps->OutSize = currentTotalSize;
459 RINOK(lps->SetCur());
460 CMyComPtr<ISequentialOutStream> realOutStream;
461 Int32 askMode= testMode ?
462 NArchive::NExtract::NAskMode::kTest :
463 NArchive::NExtract::NAskMode::kExtract;
464 Int32 index = allFilesMode ? i : indices[i];
465 RINOK(extractCallback->GetStream(index, &realOutStream, askMode));
467 if (m_Database.NewFormat)
471 if (!testMode && (!realOutStream))
475 UInt32 size = m_Database.NewFormatString.Length();
476 RINOK(WriteStream(realOutStream, (const char *)m_Database.NewFormatString, size, 0));
478 RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK));
481 const CItem &item = m_Database.Items[index];
483 currentItemSize = item.Size;
485 if (!testMode && (!realOutStream))
487 RINOK(extractCallback->PrepareOperation(askMode));
488 if (item.Section != 0)
490 RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod));
496 RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK));
500 RINOK(m_Stream->Seek(m_Database.ContentOffset + item.Offset, STREAM_SEEK_SET, NULL));
501 streamSpec->Init(item.Size);
503 RINOK(copyCoder->Code(inStream, realOutStream, NULL, NULL, progress));
504 realOutStream.Release();
505 RINOK(extractCallback->SetOperationResult((copyCoderSpec->TotalSize == item.Size) ?
506 NArchive::NExtract::NOperationResult::kOK:
507 NArchive::NExtract::NOperationResult::kDataError));
512 UInt64 lastFolderIndex = ((UInt64)0 - 1);
513 for (i = 0; i < numItems; i++)
515 UInt32 index = allFilesMode ? i : indices[i];
516 int entryIndex = m_Database.Indices[index];
517 const CItem &item = m_Database.Items[entryIndex];
518 UInt64 sectionIndex = item.Section;
519 if (item.IsDirectory() || item.Size == 0)
521 if (sectionIndex == 0)
523 currentTotalSize += item.Size;
526 const CSectionInfo §ion = m_Database.Sections[(int)item.Section];
529 const CLzxInfo &lzxInfo = section.Methods[0].LzxInfo;
530 UInt64 folderIndex = m_Database.GetFolder(index);
531 if (lastFolderIndex == folderIndex)
533 lastFolderIndex = m_Database.GetLastFolder(index);
534 for (; folderIndex <= lastFolderIndex; folderIndex++)
535 currentTotalSize += lzxInfo.GetFolderSize();
539 RINOK(extractCallback->SetTotal(currentTotalSize));
541 NCompress::NLzx::CDecoder *lzxDecoderSpec = 0;
542 CMyComPtr<ICompressCoder> lzxDecoder;
543 CChmFolderOutStream *chmFolderOutStream = 0;
544 CMyComPtr<ISequentialOutStream> outStream;
546 currentTotalSize = 0;
548 CRecordVector<bool> extractStatuses;
549 for (i = 0; i < numItems;)
551 RINOK(extractCallback->SetCompleted(¤tTotalSize));
552 UInt32 index = allFilesMode ? i : indices[i];
554 int entryIndex = m_Database.Indices[index];
555 const CItem &item = m_Database.Items[entryIndex];
556 UInt64 sectionIndex = item.Section;
557 Int32 askMode= testMode ?
558 NArchive::NExtract::NAskMode::kTest :
559 NArchive::NExtract::NAskMode::kExtract;
560 if (item.IsDirectory())
562 CMyComPtr<ISequentialOutStream> realOutStream;
563 RINOK(extractCallback->GetStream(index, &realOutStream, askMode));
564 RINOK(extractCallback->PrepareOperation(askMode));
565 realOutStream.Release();
566 RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK));
570 lps->InSize = currentTotalSize; // Change it
571 lps->OutSize = currentTotalSize;
573 if (item.Size == 0 || sectionIndex == 0)
575 CMyComPtr<ISequentialOutStream> realOutStream;
576 RINOK(extractCallback->GetStream(index, &realOutStream, askMode));
577 if (!testMode && (!realOutStream))
579 RINOK(extractCallback->PrepareOperation(askMode));
580 Int32 opRes = NArchive::NExtract::NOperationResult::kOK;
581 if (!testMode && item.Size != 0)
583 RINOK(m_Stream->Seek(m_Database.ContentOffset + item.Offset, STREAM_SEEK_SET, NULL));
584 streamSpec->Init(item.Size);
585 RINOK(copyCoder->Code(inStream, realOutStream, NULL, NULL, progress));
586 if (copyCoderSpec->TotalSize != item.Size)
587 opRes = NArchive::NExtract::NOperationResult::kDataError;
589 realOutStream.Release();
590 RINOK(extractCallback->SetOperationResult(opRes));
591 currentTotalSize += item.Size;
595 const CSectionInfo §ion = m_Database.Sections[(int)sectionIndex];
597 if (!section.IsLzx())
599 CMyComPtr<ISequentialOutStream> realOutStream;
600 RINOK(extractCallback->GetStream(index, &realOutStream, askMode));
601 if(!testMode && (!realOutStream))
603 RINOK(extractCallback->PrepareOperation(askMode));
604 RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod));
608 const CLzxInfo &lzxInfo = section.Methods[0].LzxInfo;
610 if (chmFolderOutStream == 0)
612 chmFolderOutStream = new CChmFolderOutStream;
613 outStream = chmFolderOutStream;
616 chmFolderOutStream->Init(&m_Database, extractCallback, testMode);
618 if(lzxDecoderSpec == NULL)
620 lzxDecoderSpec = new NCompress::NLzx::CDecoder;
621 lzxDecoder = lzxDecoderSpec;
624 UInt64 folderIndex = m_Database.GetFolder(index);
626 UInt64 compressedPos = m_Database.ContentOffset + section.Offset;
627 UInt32 numDictBits = lzxInfo.GetNumDictBits();
628 RINOK(lzxDecoderSpec->SetParams(numDictBits));
630 const CItem *lastItem = &item;
631 extractStatuses.Clear();
632 extractStatuses.Add(true);
634 for (;; folderIndex++)
636 RINOK(extractCallback->SetCompleted(¤tTotalSize));
638 UInt64 startPos = lzxInfo.GetFolderPos(folderIndex);
639 UInt64 finishPos = lastItem->Offset + lastItem->Size;
640 UInt64 limitFolderIndex = lzxInfo.GetFolder(finishPos);
642 lastFolderIndex = m_Database.GetLastFolder(index);
643 UInt64 folderSize = lzxInfo.GetFolderSize();
644 UInt64 unPackSize = folderSize;
645 if (extractStatuses.IsEmpty())
646 chmFolderOutStream->m_StartIndex = index + 1;
648 chmFolderOutStream->m_StartIndex = index;
649 if (limitFolderIndex == folderIndex)
651 for (; i < numItems; i++)
653 UInt32 nextIndex = allFilesMode ? i : indices[i];
654 int entryIndex = m_Database.Indices[nextIndex];
655 const CItem &nextItem = m_Database.Items[entryIndex];
656 if (nextItem.Section != sectionIndex)
658 UInt64 nextFolderIndex = m_Database.GetFolder(nextIndex);
659 if (nextFolderIndex != folderIndex)
661 for (index++; index < nextIndex; index++)
662 extractStatuses.Add(false);
663 extractStatuses.Add(true);
665 lastItem = &nextItem;
666 if (nextItem.Size != 0)
667 finishPos = nextItem.Offset + nextItem.Size;
668 lastFolderIndex = m_Database.GetLastFolder(index);
671 unPackSize = MyMin(finishPos - startPos, unPackSize);
673 chmFolderOutStream->m_FolderSize = folderSize;
674 chmFolderOutStream->m_PosInFolder = 0;
675 chmFolderOutStream->m_PosInSection = startPos;
676 chmFolderOutStream->m_ExtractStatuses = &extractStatuses;
677 chmFolderOutStream->m_NumFiles = extractStatuses.Size();
678 chmFolderOutStream->m_CurrentIndex = 0;
681 UInt64 startBlock = lzxInfo.GetBlockIndexFromFolderIndex(folderIndex);
682 const CResetTable &rt = lzxInfo.ResetTable;
683 UInt32 numBlocks = (UInt32)rt.GetNumBlocks(unPackSize);
684 for (UInt32 b = 0; b < numBlocks; b++)
686 UInt64 completedSize = currentTotalSize + chmFolderOutStream->m_PosInSection - startPos;
687 RINOK(extractCallback->SetCompleted(&completedSize));
688 UInt64 bCur = startBlock + b;
689 if (bCur >= rt.ResetOffsets.Size())
691 UInt64 offset = rt.ResetOffsets[(int)bCur];
692 UInt64 compressedSize;
693 rt.GetCompressedSizeOfBlock(bCur, compressedSize);
694 UInt64 rem = finishPos - chmFolderOutStream->m_PosInSection;
695 if (rem > rt.BlockSize)
697 RINOK(m_Stream->Seek(compressedPos + offset, STREAM_SEEK_SET, NULL));
698 streamSpec->SetStream(m_Stream);
699 streamSpec->Init(compressedSize);
700 lzxDecoderSpec->SetKeepHistory(b > 0);
701 HRESULT res = lzxDecoder->Code(inStream, outStream, NULL, &rem, NULL);
712 RINOK(chmFolderOutStream->FlushCorrupted(unPackSize));
714 currentTotalSize += folderSize;
715 if (folderIndex == lastFolderIndex)
717 extractStatuses.Clear();
724 STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems)
726 *numItems = m_Database.NewFormat ? 1:
727 (m_Database.LowLevel ?
728 m_Database.Items.Size():
729 m_Database.Indices.Size());