7zExtract.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* 7zExtract.c */
  2. #include "7zExtract.h"
  3. #include "7zDecode.h"
  4. #include "../../7zCrc.h"
  5. SZ_RESULT SzExtract(
  6. ISzInStream *inStream,
  7. CArchiveDatabaseEx *db,
  8. UInt32 fileIndex,
  9. UInt32 *blockIndex,
  10. Byte **outBuffer,
  11. size_t *outBufferSize,
  12. size_t *offset,
  13. size_t *outSizeProcessed,
  14. ISzAlloc *allocMain,
  15. ISzAlloc *allocTemp)
  16. {
  17. UInt32 folderIndex = db->FileIndexToFolderIndexMap[fileIndex];
  18. SZ_RESULT res = SZ_OK;
  19. *offset = 0;
  20. *outSizeProcessed = 0;
  21. if (folderIndex == (UInt32)-1)
  22. {
  23. allocMain->Free(*outBuffer);
  24. *blockIndex = folderIndex;
  25. *outBuffer = 0;
  26. *outBufferSize = 0;
  27. return SZ_OK;
  28. }
  29. if (*outBuffer == 0 || *blockIndex != folderIndex)
  30. {
  31. CFolder *folder = db->Database.Folders + folderIndex;
  32. CFileSize unPackSizeSpec = SzFolderGetUnPackSize(folder);
  33. size_t unPackSize = (size_t)unPackSizeSpec;
  34. CFileSize startOffset = SzArDbGetFolderStreamPos(db, folderIndex, 0);
  35. #ifndef _LZMA_IN_CB
  36. Byte *inBuffer = 0;
  37. size_t processedSize;
  38. CFileSize packSizeSpec;
  39. size_t packSize;
  40. RINOK(SzArDbGetFolderFullPackSize(db, folderIndex, &packSizeSpec));
  41. packSize = (size_t)packSizeSpec;
  42. if (packSize != packSizeSpec)
  43. return SZE_OUTOFMEMORY;
  44. #endif
  45. if (unPackSize != unPackSizeSpec)
  46. return SZE_OUTOFMEMORY;
  47. *blockIndex = folderIndex;
  48. allocMain->Free(*outBuffer);
  49. *outBuffer = 0;
  50. RINOK(inStream->Seek(inStream, startOffset));
  51. #ifndef _LZMA_IN_CB
  52. if (packSize != 0)
  53. {
  54. inBuffer = (Byte *)allocTemp->Alloc(packSize);
  55. if (inBuffer == 0)
  56. return SZE_OUTOFMEMORY;
  57. }
  58. res = inStream->Read(inStream, inBuffer, packSize, &processedSize);
  59. if (res == SZ_OK && processedSize != packSize)
  60. res = SZE_FAIL;
  61. #endif
  62. if (res == SZ_OK)
  63. {
  64. *outBufferSize = unPackSize;
  65. if (unPackSize != 0)
  66. {
  67. *outBuffer = (Byte *)allocMain->Alloc(unPackSize);
  68. if (*outBuffer == 0)
  69. res = SZE_OUTOFMEMORY;
  70. }
  71. if (res == SZ_OK)
  72. {
  73. res = SzDecode(db->Database.PackSizes +
  74. db->FolderStartPackStreamIndex[folderIndex], folder,
  75. #ifdef _LZMA_IN_CB
  76. inStream, startOffset,
  77. #else
  78. inBuffer,
  79. #endif
  80. *outBuffer, unPackSize, allocTemp);
  81. if (res == SZ_OK)
  82. {
  83. if (folder->UnPackCRCDefined)
  84. {
  85. if (CrcCalc(*outBuffer, unPackSize) != folder->UnPackCRC)
  86. res = SZE_CRC_ERROR;
  87. }
  88. }
  89. }
  90. }
  91. #ifndef _LZMA_IN_CB
  92. allocTemp->Free(inBuffer);
  93. #endif
  94. }
  95. if (res == SZ_OK)
  96. {
  97. UInt32 i;
  98. CFileItem *fileItem = db->Database.Files + fileIndex;
  99. *offset = 0;
  100. for(i = db->FolderStartFileIndex[folderIndex]; i < fileIndex; i++)
  101. *offset += (UInt32)db->Database.Files[i].Size;
  102. *outSizeProcessed = (size_t)fileItem->Size;
  103. if (*offset + *outSizeProcessed > *outBufferSize)
  104. return SZE_FAIL;
  105. {
  106. if (fileItem->IsFileCRCDefined)
  107. {
  108. if (CrcCalc(*outBuffer + *offset, *outSizeProcessed) != fileItem->FileCRC)
  109. res = SZE_CRC_ERROR;
  110. }
  111. }
  112. }
  113. return res;
  114. }