7zIn.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /* 7zIn.c */
  2. #include "7zIn.h"
  3. #include "7zDecode.h"
  4. #include "../../7zCrc.h"
  5. #define RINOM(x) { if((x) == 0) return SZE_OUTOFMEMORY; }
  6. void SzArDbExInit(CArchiveDatabaseEx *db)
  7. {
  8. SzArchiveDatabaseInit(&db->Database);
  9. db->FolderStartPackStreamIndex = 0;
  10. db->PackStreamStartPositions = 0;
  11. db->FolderStartFileIndex = 0;
  12. db->FileIndexToFolderIndexMap = 0;
  13. }
  14. void SzArDbExFree(CArchiveDatabaseEx *db, void (*freeFunc)(void *))
  15. {
  16. freeFunc(db->FolderStartPackStreamIndex);
  17. freeFunc(db->PackStreamStartPositions);
  18. freeFunc(db->FolderStartFileIndex);
  19. freeFunc(db->FileIndexToFolderIndexMap);
  20. SzArchiveDatabaseFree(&db->Database, freeFunc);
  21. SzArDbExInit(db);
  22. }
  23. /*
  24. CFileSize GetFolderPackStreamSize(int folderIndex, int streamIndex) const
  25. {
  26. return PackSizes[FolderStartPackStreamIndex[folderIndex] + streamIndex];
  27. }
  28. CFileSize GetFilePackSize(int fileIndex) const
  29. {
  30. int folderIndex = FileIndexToFolderIndexMap[fileIndex];
  31. if (folderIndex >= 0)
  32. {
  33. const CFolder &folderInfo = Folders[folderIndex];
  34. if (FolderStartFileIndex[folderIndex] == fileIndex)
  35. return GetFolderFullPackSize(folderIndex);
  36. }
  37. return 0;
  38. }
  39. */
  40. #define MY_ALLOC(T, p, size, allocFunc) { if ((size) == 0) p = 0; else \
  41. if ((p = (T *)allocFunc((size) * sizeof(T))) == 0) return SZE_OUTOFMEMORY; }
  42. SZ_RESULT SzArDbExFill(CArchiveDatabaseEx *db, void * (*allocFunc)(size_t size))
  43. {
  44. UInt32 startPos = 0;
  45. CFileSize startPosSize = 0;
  46. UInt32 i;
  47. UInt32 folderIndex = 0;
  48. UInt32 indexInFolder = 0;
  49. MY_ALLOC(UInt32, db->FolderStartPackStreamIndex, db->Database.NumFolders, allocFunc);
  50. for(i = 0; i < db->Database.NumFolders; i++)
  51. {
  52. db->FolderStartPackStreamIndex[i] = startPos;
  53. startPos += db->Database.Folders[i].NumPackStreams;
  54. }
  55. MY_ALLOC(CFileSize, db->PackStreamStartPositions, db->Database.NumPackStreams, allocFunc);
  56. for(i = 0; i < db->Database.NumPackStreams; i++)
  57. {
  58. db->PackStreamStartPositions[i] = startPosSize;
  59. startPosSize += db->Database.PackSizes[i];
  60. }
  61. MY_ALLOC(UInt32, db->FolderStartFileIndex, db->Database.NumFolders, allocFunc);
  62. MY_ALLOC(UInt32, db->FileIndexToFolderIndexMap, db->Database.NumFiles, allocFunc);
  63. for (i = 0; i < db->Database.NumFiles; i++)
  64. {
  65. CFileItem *file = db->Database.Files + i;
  66. int emptyStream = !file->HasStream;
  67. if (emptyStream && indexInFolder == 0)
  68. {
  69. db->FileIndexToFolderIndexMap[i] = (UInt32)-1;
  70. continue;
  71. }
  72. if (indexInFolder == 0)
  73. {
  74. /*
  75. v3.13 incorrectly worked with empty folders
  76. v4.07: Loop for skipping empty folders
  77. */
  78. for (;;)
  79. {
  80. if (folderIndex >= db->Database.NumFolders)
  81. return SZE_ARCHIVE_ERROR;
  82. db->FolderStartFileIndex[folderIndex] = i;
  83. if (db->Database.Folders[folderIndex].NumUnPackStreams != 0)
  84. break;
  85. folderIndex++;
  86. }
  87. }
  88. db->FileIndexToFolderIndexMap[i] = folderIndex;
  89. if (emptyStream)
  90. continue;
  91. indexInFolder++;
  92. if (indexInFolder >= db->Database.Folders[folderIndex].NumUnPackStreams)
  93. {
  94. folderIndex++;
  95. indexInFolder = 0;
  96. }
  97. }
  98. return SZ_OK;
  99. }
  100. CFileSize SzArDbGetFolderStreamPos(CArchiveDatabaseEx *db, UInt32 folderIndex, UInt32 indexInFolder)
  101. {
  102. return db->ArchiveInfo.DataStartPosition +
  103. db->PackStreamStartPositions[db->FolderStartPackStreamIndex[folderIndex] + indexInFolder];
  104. }
  105. int SzArDbGetFolderFullPackSize(CArchiveDatabaseEx *db, UInt32 folderIndex, CFileSize *resSize)
  106. {
  107. UInt32 packStreamIndex = db->FolderStartPackStreamIndex[folderIndex];
  108. CFolder *folder = db->Database.Folders + folderIndex;
  109. CFileSize size = 0;
  110. UInt32 i;
  111. for (i = 0; i < folder->NumPackStreams; i++)
  112. {
  113. CFileSize t = size + db->Database.PackSizes[packStreamIndex + i];
  114. if (t < size)
  115. return SZE_FAIL;
  116. size = t;
  117. }
  118. *resSize = size;
  119. return SZ_OK;
  120. }
  121. /*
  122. SZ_RESULT SzReadTime(const CObjectVector<CSzByteBuffer> &dataVector,
  123. CObjectVector<CFileItem> &files, UInt64 type)
  124. {
  125. CBoolVector boolVector;
  126. RINOK(ReadBoolVector2(files.Size(), boolVector))
  127. CStreamSwitch streamSwitch;
  128. RINOK(streamSwitch.Set(this, &dataVector));
  129. for(int i = 0; i < files.Size(); i++)
  130. {
  131. CFileItem &file = files[i];
  132. CArchiveFileTime fileTime;
  133. bool defined = boolVector[i];
  134. if (defined)
  135. {
  136. UInt32 low, high;
  137. RINOK(SzReadUInt32(low));
  138. RINOK(SzReadUInt32(high));
  139. fileTime.dwLowDateTime = low;
  140. fileTime.dwHighDateTime = high;
  141. }
  142. switch(type)
  143. {
  144. case k7zIdCreationTime:
  145. file.IsCreationTimeDefined = defined;
  146. if (defined)
  147. file.CreationTime = fileTime;
  148. break;
  149. case k7zIdLastWriteTime:
  150. file.IsLastWriteTimeDefined = defined;
  151. if (defined)
  152. file.LastWriteTime = fileTime;
  153. break;
  154. case k7zIdLastAccessTime:
  155. file.IsLastAccessTimeDefined = defined;
  156. if (defined)
  157. file.LastAccessTime = fileTime;
  158. break;
  159. }
  160. }
  161. return SZ_OK;
  162. }
  163. */
  164. SZ_RESULT SafeReadDirect(ISzInStream *inStream, Byte *data, size_t size)
  165. {
  166. #ifdef _LZMA_IN_CB
  167. while (size > 0)
  168. {
  169. void *inBufferSpec;
  170. size_t processedSize;
  171. const Byte *inBuffer;
  172. RINOK(inStream->Read(inStream, (void **)&inBufferSpec, size, &processedSize));
  173. inBuffer = (const Byte *)inBufferSpec;
  174. if (processedSize == 0 || processedSize > size)
  175. return SZE_FAIL;
  176. size -= processedSize;
  177. do
  178. {
  179. *data++ = *inBuffer++;
  180. }
  181. while (--processedSize != 0);
  182. }
  183. #else
  184. size_t processedSize;
  185. RINOK(inStream->Read(inStream, data, size, &processedSize));
  186. if (processedSize != size)
  187. return SZE_FAIL;
  188. #endif
  189. return SZ_OK;
  190. }
  191. SZ_RESULT SafeReadDirectByte(ISzInStream *inStream, Byte *data)
  192. {
  193. return SafeReadDirect(inStream, data, 1);
  194. }
  195. SZ_RESULT SafeReadDirectUInt32(ISzInStream *inStream, UInt32 *value, UInt32 *crc)
  196. {
  197. int i;
  198. *value = 0;
  199. for (i = 0; i < 4; i++)
  200. {
  201. Byte b;
  202. RINOK(SafeReadDirectByte(inStream, &b));
  203. *value |= ((UInt32)b << (8 * i));
  204. *crc = CRC_UPDATE_BYTE(*crc, b);
  205. }
  206. return SZ_OK;
  207. }
  208. SZ_RESULT SafeReadDirectUInt64(ISzInStream *inStream, UInt64 *value, UInt32 *crc)
  209. {
  210. int i;
  211. *value = 0;
  212. for (i = 0; i < 8; i++)
  213. {
  214. Byte b;
  215. RINOK(SafeReadDirectByte(inStream, &b));
  216. *value |= ((UInt64)b << (8 * i));
  217. *crc = CRC_UPDATE_BYTE(*crc, b);
  218. }
  219. return SZ_OK;
  220. }
  221. int TestSignatureCandidate(Byte *testBytes)
  222. {
  223. size_t i;
  224. for (i = 0; i < k7zSignatureSize; i++)
  225. if (testBytes[i] != k7zSignature[i])
  226. return 0;
  227. return 1;
  228. }
  229. typedef struct _CSzState
  230. {
  231. Byte *Data;
  232. size_t Size;
  233. }CSzData;
  234. SZ_RESULT SzReadByte(CSzData *sd, Byte *b)
  235. {
  236. if (sd->Size == 0)
  237. return SZE_ARCHIVE_ERROR;
  238. sd->Size--;
  239. *b = *sd->Data++;
  240. return SZ_OK;
  241. }
  242. SZ_RESULT SzReadBytes(CSzData *sd, Byte *data, size_t size)
  243. {
  244. size_t i;
  245. for (i = 0; i < size; i++)
  246. {
  247. RINOK(SzReadByte(sd, data + i));
  248. }
  249. return SZ_OK;
  250. }
  251. SZ_RESULT SzReadUInt32(CSzData *sd, UInt32 *value)
  252. {
  253. int i;
  254. *value = 0;
  255. for (i = 0; i < 4; i++)
  256. {
  257. Byte b;
  258. RINOK(SzReadByte(sd, &b));
  259. *value |= ((UInt32)(b) << (8 * i));
  260. }
  261. return SZ_OK;
  262. }
  263. SZ_RESULT SzReadNumber(CSzData *sd, UInt64 *value)
  264. {
  265. Byte firstByte;
  266. Byte mask = 0x80;
  267. int i;
  268. RINOK(SzReadByte(sd, &firstByte));
  269. *value = 0;
  270. for (i = 0; i < 8; i++)
  271. {
  272. Byte b;
  273. if ((firstByte & mask) == 0)
  274. {
  275. UInt64 highPart = firstByte & (mask - 1);
  276. *value += (highPart << (8 * i));
  277. return SZ_OK;
  278. }
  279. RINOK(SzReadByte(sd, &b));
  280. *value |= ((UInt64)b << (8 * i));
  281. mask >>= 1;
  282. }
  283. return SZ_OK;
  284. }
  285. SZ_RESULT SzReadSize(CSzData *sd, CFileSize *value)
  286. {
  287. UInt64 value64;
  288. RINOK(SzReadNumber(sd, &value64));
  289. *value = (CFileSize)value64;
  290. return SZ_OK;
  291. }
  292. SZ_RESULT SzReadNumber32(CSzData *sd, UInt32 *value)
  293. {
  294. UInt64 value64;
  295. RINOK(SzReadNumber(sd, &value64));
  296. if (value64 >= 0x80000000)
  297. return SZE_NOTIMPL;
  298. if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 2)))
  299. return SZE_NOTIMPL;
  300. *value = (UInt32)value64;
  301. return SZ_OK;
  302. }
  303. SZ_RESULT SzReadID(CSzData *sd, UInt64 *value)
  304. {
  305. return SzReadNumber(sd, value);
  306. }
  307. SZ_RESULT SzSkeepDataSize(CSzData *sd, UInt64 size)
  308. {
  309. if (size > sd->Size)
  310. return SZE_ARCHIVE_ERROR;
  311. sd->Size -= (size_t)size;
  312. sd->Data += (size_t)size;
  313. return SZ_OK;
  314. }
  315. SZ_RESULT SzSkeepData(CSzData *sd)
  316. {
  317. UInt64 size;
  318. RINOK(SzReadNumber(sd, &size));
  319. return SzSkeepDataSize(sd, size);
  320. }
  321. SZ_RESULT SzReadArchiveProperties(CSzData *sd)
  322. {
  323. for (;;)
  324. {
  325. UInt64 type;
  326. RINOK(SzReadID(sd, &type));
  327. if (type == k7zIdEnd)
  328. break;
  329. SzSkeepData(sd);
  330. }
  331. return SZ_OK;
  332. }
  333. SZ_RESULT SzWaitAttribute(CSzData *sd, UInt64 attribute)
  334. {
  335. for (;;)
  336. {
  337. UInt64 type;
  338. RINOK(SzReadID(sd, &type));
  339. if (type == attribute)
  340. return SZ_OK;
  341. if (type == k7zIdEnd)
  342. return SZE_ARCHIVE_ERROR;
  343. RINOK(SzSkeepData(sd));
  344. }
  345. }
  346. SZ_RESULT SzReadBoolVector(CSzData *sd, size_t numItems, Byte **v, void * (*allocFunc)(size_t size))
  347. {
  348. Byte b = 0;
  349. Byte mask = 0;
  350. size_t i;
  351. MY_ALLOC(Byte, *v, numItems, allocFunc);
  352. for (i = 0; i < numItems; i++)
  353. {
  354. if (mask == 0)
  355. {
  356. RINOK(SzReadByte(sd, &b));
  357. mask = 0x80;
  358. }
  359. (*v)[i] = (Byte)(((b & mask) != 0) ? 1 : 0);
  360. mask >>= 1;
  361. }
  362. return SZ_OK;
  363. }
  364. SZ_RESULT SzReadBoolVector2(CSzData *sd, size_t numItems, Byte **v, void * (*allocFunc)(size_t size))
  365. {
  366. Byte allAreDefined;
  367. size_t i;
  368. RINOK(SzReadByte(sd, &allAreDefined));
  369. if (allAreDefined == 0)
  370. return SzReadBoolVector(sd, numItems, v, allocFunc);
  371. MY_ALLOC(Byte, *v, numItems, allocFunc);
  372. for(i = 0; i < numItems; i++)
  373. (*v)[i] = 1;
  374. return SZ_OK;
  375. }
  376. SZ_RESULT SzReadHashDigests(
  377. CSzData *sd,
  378. size_t numItems,
  379. Byte **digestsDefined,
  380. UInt32 **digests,
  381. void * (*allocFunc)(size_t size))
  382. {
  383. size_t i;
  384. RINOK(SzReadBoolVector2(sd, numItems, digestsDefined, allocFunc));
  385. MY_ALLOC(UInt32, *digests, numItems, allocFunc);
  386. for(i = 0; i < numItems; i++)
  387. if ((*digestsDefined)[i])
  388. {
  389. RINOK(SzReadUInt32(sd, (*digests) + i));
  390. }
  391. return SZ_OK;
  392. }
  393. SZ_RESULT SzReadPackInfo(
  394. CSzData *sd,
  395. CFileSize *dataOffset,
  396. UInt32 *numPackStreams,
  397. CFileSize **packSizes,
  398. Byte **packCRCsDefined,
  399. UInt32 **packCRCs,
  400. void * (*allocFunc)(size_t size))
  401. {
  402. UInt32 i;
  403. RINOK(SzReadSize(sd, dataOffset));
  404. RINOK(SzReadNumber32(sd, numPackStreams));
  405. RINOK(SzWaitAttribute(sd, k7zIdSize));
  406. MY_ALLOC(CFileSize, *packSizes, (size_t)*numPackStreams, allocFunc);
  407. for(i = 0; i < *numPackStreams; i++)
  408. {
  409. RINOK(SzReadSize(sd, (*packSizes) + i));
  410. }
  411. for (;;)
  412. {
  413. UInt64 type;
  414. RINOK(SzReadID(sd, &type));
  415. if (type == k7zIdEnd)
  416. break;
  417. if (type == k7zIdCRC)
  418. {
  419. RINOK(SzReadHashDigests(sd, (size_t)*numPackStreams, packCRCsDefined, packCRCs, allocFunc));
  420. continue;
  421. }
  422. RINOK(SzSkeepData(sd));
  423. }
  424. if (*packCRCsDefined == 0)
  425. {
  426. MY_ALLOC(Byte, *packCRCsDefined, (size_t)*numPackStreams, allocFunc);
  427. MY_ALLOC(UInt32, *packCRCs, (size_t)*numPackStreams, allocFunc);
  428. for(i = 0; i < *numPackStreams; i++)
  429. {
  430. (*packCRCsDefined)[i] = 0;
  431. (*packCRCs)[i] = 0;
  432. }
  433. }
  434. return SZ_OK;
  435. }
  436. SZ_RESULT SzReadSwitch(CSzData *sd)
  437. {
  438. Byte external;
  439. RINOK(SzReadByte(sd, &external));
  440. return (external == 0) ? SZ_OK: SZE_ARCHIVE_ERROR;
  441. }
  442. SZ_RESULT SzGetNextFolderItem(CSzData *sd, CFolder *folder, void * (*allocFunc)(size_t size))
  443. {
  444. UInt32 numCoders;
  445. UInt32 numBindPairs;
  446. UInt32 numPackedStreams;
  447. UInt32 i;
  448. UInt32 numInStreams = 0;
  449. UInt32 numOutStreams = 0;
  450. RINOK(SzReadNumber32(sd, &numCoders));
  451. folder->NumCoders = numCoders;
  452. MY_ALLOC(CCoderInfo, folder->Coders, (size_t)numCoders, allocFunc);
  453. for (i = 0; i < numCoders; i++)
  454. SzCoderInfoInit(folder->Coders + i);
  455. for (i = 0; i < numCoders; i++)
  456. {
  457. Byte mainByte;
  458. CCoderInfo *coder = folder->Coders + i;
  459. {
  460. unsigned idSize, j;
  461. Byte longID[15];
  462. RINOK(SzReadByte(sd, &mainByte));
  463. idSize = (unsigned)(mainByte & 0xF);
  464. RINOK(SzReadBytes(sd, longID, idSize));
  465. if (idSize > sizeof(coder->MethodID))
  466. return SZE_NOTIMPL;
  467. coder->MethodID = 0;
  468. for (j = 0; j < idSize; j++)
  469. coder->MethodID |= (CMethodID)longID[idSize - 1 - j] << (8 * j);
  470. if ((mainByte & 0x10) != 0)
  471. {
  472. RINOK(SzReadNumber32(sd, &coder->NumInStreams));
  473. RINOK(SzReadNumber32(sd, &coder->NumOutStreams));
  474. }
  475. else
  476. {
  477. coder->NumInStreams = 1;
  478. coder->NumOutStreams = 1;
  479. }
  480. if ((mainByte & 0x20) != 0)
  481. {
  482. UInt64 propertiesSize = 0;
  483. RINOK(SzReadNumber(sd, &propertiesSize));
  484. if (!SzByteBufferCreate(&coder->Properties, (size_t)propertiesSize, allocFunc))
  485. return SZE_OUTOFMEMORY;
  486. RINOK(SzReadBytes(sd, coder->Properties.Items, (size_t)propertiesSize));
  487. }
  488. }
  489. while ((mainByte & 0x80) != 0)
  490. {
  491. RINOK(SzReadByte(sd, &mainByte));
  492. RINOK(SzSkeepDataSize(sd, (mainByte & 0xF)));
  493. if ((mainByte & 0x10) != 0)
  494. {
  495. UInt32 n;
  496. RINOK(SzReadNumber32(sd, &n));
  497. RINOK(SzReadNumber32(sd, &n));
  498. }
  499. if ((mainByte & 0x20) != 0)
  500. {
  501. UInt64 propertiesSize = 0;
  502. RINOK(SzReadNumber(sd, &propertiesSize));
  503. RINOK(SzSkeepDataSize(sd, propertiesSize));
  504. }
  505. }
  506. numInStreams += (UInt32)coder->NumInStreams;
  507. numOutStreams += (UInt32)coder->NumOutStreams;
  508. }
  509. numBindPairs = numOutStreams - 1;
  510. folder->NumBindPairs = numBindPairs;
  511. MY_ALLOC(CBindPair, folder->BindPairs, (size_t)numBindPairs, allocFunc);
  512. for (i = 0; i < numBindPairs; i++)
  513. {
  514. CBindPair *bindPair = folder->BindPairs + i;;
  515. RINOK(SzReadNumber32(sd, &bindPair->InIndex));
  516. RINOK(SzReadNumber32(sd, &bindPair->OutIndex));
  517. }
  518. numPackedStreams = numInStreams - (UInt32)numBindPairs;
  519. folder->NumPackStreams = numPackedStreams;
  520. MY_ALLOC(UInt32, folder->PackStreams, (size_t)numPackedStreams, allocFunc);
  521. if (numPackedStreams == 1)
  522. {
  523. UInt32 j;
  524. UInt32 pi = 0;
  525. for (j = 0; j < numInStreams; j++)
  526. if (SzFolderFindBindPairForInStream(folder, j) < 0)
  527. {
  528. folder->PackStreams[pi++] = j;
  529. break;
  530. }
  531. }
  532. else
  533. for(i = 0; i < numPackedStreams; i++)
  534. {
  535. RINOK(SzReadNumber32(sd, folder->PackStreams + i));
  536. }
  537. return SZ_OK;
  538. }
  539. SZ_RESULT SzReadUnPackInfo(
  540. CSzData *sd,
  541. UInt32 *numFolders,
  542. CFolder **folders, /* for allocFunc */
  543. void * (*allocFunc)(size_t size),
  544. ISzAlloc *allocTemp)
  545. {
  546. UInt32 i;
  547. RINOK(SzWaitAttribute(sd, k7zIdFolder));
  548. RINOK(SzReadNumber32(sd, numFolders));
  549. {
  550. RINOK(SzReadSwitch(sd));
  551. MY_ALLOC(CFolder, *folders, (size_t)*numFolders, allocFunc);
  552. for(i = 0; i < *numFolders; i++)
  553. SzFolderInit((*folders) + i);
  554. for(i = 0; i < *numFolders; i++)
  555. {
  556. RINOK(SzGetNextFolderItem(sd, (*folders) + i, allocFunc));
  557. }
  558. }
  559. RINOK(SzWaitAttribute(sd, k7zIdCodersUnPackSize));
  560. for(i = 0; i < *numFolders; i++)
  561. {
  562. UInt32 j;
  563. CFolder *folder = (*folders) + i;
  564. UInt32 numOutStreams = SzFolderGetNumOutStreams(folder);
  565. MY_ALLOC(CFileSize, folder->UnPackSizes, (size_t)numOutStreams, allocFunc);
  566. for(j = 0; j < numOutStreams; j++)
  567. {
  568. RINOK(SzReadSize(sd, folder->UnPackSizes + j));
  569. }
  570. }
  571. for (;;)
  572. {
  573. UInt64 type;
  574. RINOK(SzReadID(sd, &type));
  575. if (type == k7zIdEnd)
  576. return SZ_OK;
  577. if (type == k7zIdCRC)
  578. {
  579. SZ_RESULT res;
  580. Byte *crcsDefined = 0;
  581. UInt32 *crcs = 0;
  582. res = SzReadHashDigests(sd, *numFolders, &crcsDefined, &crcs, allocTemp->Alloc);
  583. if (res == SZ_OK)
  584. {
  585. for(i = 0; i < *numFolders; i++)
  586. {
  587. CFolder *folder = (*folders) + i;
  588. folder->UnPackCRCDefined = crcsDefined[i];
  589. folder->UnPackCRC = crcs[i];
  590. }
  591. }
  592. allocTemp->Free(crcs);
  593. allocTemp->Free(crcsDefined);
  594. RINOK(res);
  595. continue;
  596. }
  597. RINOK(SzSkeepData(sd));
  598. }
  599. }
  600. SZ_RESULT SzReadSubStreamsInfo(
  601. CSzData *sd,
  602. UInt32 numFolders,
  603. CFolder *folders,
  604. UInt32 *numUnPackStreams,
  605. CFileSize **unPackSizes,
  606. Byte **digestsDefined,
  607. UInt32 **digests,
  608. ISzAlloc *allocTemp)
  609. {
  610. UInt64 type = 0;
  611. UInt32 i;
  612. UInt32 si = 0;
  613. UInt32 numDigests = 0;
  614. for(i = 0; i < numFolders; i++)
  615. folders[i].NumUnPackStreams = 1;
  616. *numUnPackStreams = numFolders;
  617. for (;;)
  618. {
  619. RINOK(SzReadID(sd, &type));
  620. if (type == k7zIdNumUnPackStream)
  621. {
  622. *numUnPackStreams = 0;
  623. for(i = 0; i < numFolders; i++)
  624. {
  625. UInt32 numStreams;
  626. RINOK(SzReadNumber32(sd, &numStreams));
  627. folders[i].NumUnPackStreams = numStreams;
  628. *numUnPackStreams += numStreams;
  629. }
  630. continue;
  631. }
  632. if (type == k7zIdCRC || type == k7zIdSize)
  633. break;
  634. if (type == k7zIdEnd)
  635. break;
  636. RINOK(SzSkeepData(sd));
  637. }
  638. if (*numUnPackStreams == 0)
  639. {
  640. *unPackSizes = 0;
  641. *digestsDefined = 0;
  642. *digests = 0;
  643. }
  644. else
  645. {
  646. *unPackSizes = (CFileSize *)allocTemp->Alloc((size_t)*numUnPackStreams * sizeof(CFileSize));
  647. RINOM(*unPackSizes);
  648. *digestsDefined = (Byte *)allocTemp->Alloc((size_t)*numUnPackStreams * sizeof(Byte));
  649. RINOM(*digestsDefined);
  650. *digests = (UInt32 *)allocTemp->Alloc((size_t)*numUnPackStreams * sizeof(UInt32));
  651. RINOM(*digests);
  652. }
  653. for(i = 0; i < numFolders; i++)
  654. {
  655. /*
  656. v3.13 incorrectly worked with empty folders
  657. v4.07: we check that folder is empty
  658. */
  659. CFileSize sum = 0;
  660. UInt32 j;
  661. UInt32 numSubstreams = folders[i].NumUnPackStreams;
  662. if (numSubstreams == 0)
  663. continue;
  664. if (type == k7zIdSize)
  665. for (j = 1; j < numSubstreams; j++)
  666. {
  667. CFileSize size;
  668. RINOK(SzReadSize(sd, &size));
  669. (*unPackSizes)[si++] = size;
  670. sum += size;
  671. }
  672. (*unPackSizes)[si++] = SzFolderGetUnPackSize(folders + i) - sum;
  673. }
  674. if (type == k7zIdSize)
  675. {
  676. RINOK(SzReadID(sd, &type));
  677. }
  678. for(i = 0; i < *numUnPackStreams; i++)
  679. {
  680. (*digestsDefined)[i] = 0;
  681. (*digests)[i] = 0;
  682. }
  683. for(i = 0; i < numFolders; i++)
  684. {
  685. UInt32 numSubstreams = folders[i].NumUnPackStreams;
  686. if (numSubstreams != 1 || !folders[i].UnPackCRCDefined)
  687. numDigests += numSubstreams;
  688. }
  689. si = 0;
  690. for (;;)
  691. {
  692. if (type == k7zIdCRC)
  693. {
  694. int digestIndex = 0;
  695. Byte *digestsDefined2 = 0;
  696. UInt32 *digests2 = 0;
  697. SZ_RESULT res = SzReadHashDigests(sd, numDigests, &digestsDefined2, &digests2, allocTemp->Alloc);
  698. if (res == SZ_OK)
  699. {
  700. for (i = 0; i < numFolders; i++)
  701. {
  702. CFolder *folder = folders + i;
  703. UInt32 numSubstreams = folder->NumUnPackStreams;
  704. if (numSubstreams == 1 && folder->UnPackCRCDefined)
  705. {
  706. (*digestsDefined)[si] = 1;
  707. (*digests)[si] = folder->UnPackCRC;
  708. si++;
  709. }
  710. else
  711. {
  712. UInt32 j;
  713. for (j = 0; j < numSubstreams; j++, digestIndex++)
  714. {
  715. (*digestsDefined)[si] = digestsDefined2[digestIndex];
  716. (*digests)[si] = digests2[digestIndex];
  717. si++;
  718. }
  719. }
  720. }
  721. }
  722. allocTemp->Free(digestsDefined2);
  723. allocTemp->Free(digests2);
  724. RINOK(res);
  725. }
  726. else if (type == k7zIdEnd)
  727. return SZ_OK;
  728. else
  729. {
  730. RINOK(SzSkeepData(sd));
  731. }
  732. RINOK(SzReadID(sd, &type));
  733. }
  734. }
  735. SZ_RESULT SzReadStreamsInfo(
  736. CSzData *sd,
  737. CFileSize *dataOffset,
  738. CArchiveDatabase *db,
  739. UInt32 *numUnPackStreams,
  740. CFileSize **unPackSizes, /* allocTemp */
  741. Byte **digestsDefined, /* allocTemp */
  742. UInt32 **digests, /* allocTemp */
  743. void * (*allocFunc)(size_t size),
  744. ISzAlloc *allocTemp)
  745. {
  746. for (;;)
  747. {
  748. UInt64 type;
  749. RINOK(SzReadID(sd, &type));
  750. if ((UInt64)(int)type != type)
  751. return SZE_FAIL;
  752. switch((int)type)
  753. {
  754. case k7zIdEnd:
  755. return SZ_OK;
  756. case k7zIdPackInfo:
  757. {
  758. RINOK(SzReadPackInfo(sd, dataOffset, &db->NumPackStreams,
  759. &db->PackSizes, &db->PackCRCsDefined, &db->PackCRCs, allocFunc));
  760. break;
  761. }
  762. case k7zIdUnPackInfo:
  763. {
  764. RINOK(SzReadUnPackInfo(sd, &db->NumFolders, &db->Folders, allocFunc, allocTemp));
  765. break;
  766. }
  767. case k7zIdSubStreamsInfo:
  768. {
  769. RINOK(SzReadSubStreamsInfo(sd, db->NumFolders, db->Folders,
  770. numUnPackStreams, unPackSizes, digestsDefined, digests, allocTemp));
  771. break;
  772. }
  773. default:
  774. return SZE_FAIL;
  775. }
  776. }
  777. }
  778. Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  779. SZ_RESULT SzReadFileNames(CSzData *sd, UInt32 numFiles, CFileItem *files,
  780. void * (*allocFunc)(size_t size))
  781. {
  782. UInt32 i;
  783. for(i = 0; i < numFiles; i++)
  784. {
  785. UInt32 len = 0;
  786. UInt32 pos = 0;
  787. CFileItem *file = files + i;
  788. while(pos + 2 <= sd->Size)
  789. {
  790. int numAdds;
  791. UInt32 value = (UInt32)(sd->Data[pos] | (((UInt32)sd->Data[pos + 1]) << 8));
  792. pos += 2;
  793. len++;
  794. if (value == 0)
  795. break;
  796. if (value < 0x80)
  797. continue;
  798. if (value >= 0xD800 && value < 0xE000)
  799. {
  800. UInt32 c2;
  801. if (value >= 0xDC00)
  802. return SZE_ARCHIVE_ERROR;
  803. if (pos + 2 > sd->Size)
  804. return SZE_ARCHIVE_ERROR;
  805. c2 = (UInt32)(sd->Data[pos] | (((UInt32)sd->Data[pos + 1]) << 8));
  806. pos += 2;
  807. if (c2 < 0xDC00 || c2 >= 0xE000)
  808. return SZE_ARCHIVE_ERROR;
  809. value = ((value - 0xD800) << 10) | (c2 - 0xDC00);
  810. }
  811. for (numAdds = 1; numAdds < 5; numAdds++)
  812. if (value < (((UInt32)1) << (numAdds * 5 + 6)))
  813. break;
  814. len += numAdds;
  815. }
  816. MY_ALLOC(char, file->Name, (size_t)len, allocFunc);
  817. len = 0;
  818. while(2 <= sd->Size)
  819. {
  820. int numAdds;
  821. UInt32 value = (UInt32)(sd->Data[0] | (((UInt32)sd->Data[1]) << 8));
  822. SzSkeepDataSize(sd, 2);
  823. if (value < 0x80)
  824. {
  825. file->Name[len++] = (char)value;
  826. if (value == 0)
  827. break;
  828. continue;
  829. }
  830. if (value >= 0xD800 && value < 0xE000)
  831. {
  832. UInt32 c2 = (UInt32)(sd->Data[0] | (((UInt32)sd->Data[1]) << 8));
  833. SzSkeepDataSize(sd, 2);
  834. value = ((value - 0xD800) << 10) | (c2 - 0xDC00);
  835. }
  836. for (numAdds = 1; numAdds < 5; numAdds++)
  837. if (value < (((UInt32)1) << (numAdds * 5 + 6)))
  838. break;
  839. file->Name[len++] = (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds)));
  840. do
  841. {
  842. numAdds--;
  843. file->Name[len++] = (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F));
  844. }
  845. while(numAdds > 0);
  846. len += numAdds;
  847. }
  848. }
  849. return SZ_OK;
  850. }
  851. SZ_RESULT SzReadHeader2(
  852. CSzData *sd,
  853. CArchiveDatabaseEx *db, /* allocMain */
  854. CFileSize **unPackSizes, /* allocTemp */
  855. Byte **digestsDefined, /* allocTemp */
  856. UInt32 **digests, /* allocTemp */
  857. Byte **emptyStreamVector, /* allocTemp */
  858. Byte **emptyFileVector, /* allocTemp */
  859. Byte **lwtVector, /* allocTemp */
  860. ISzAlloc *allocMain,
  861. ISzAlloc *allocTemp)
  862. {
  863. UInt64 type;
  864. UInt32 numUnPackStreams = 0;
  865. UInt32 numFiles = 0;
  866. CFileItem *files = 0;
  867. UInt32 numEmptyStreams = 0;
  868. UInt32 i;
  869. RINOK(SzReadID(sd, &type));
  870. if (type == k7zIdArchiveProperties)
  871. {
  872. RINOK(SzReadArchiveProperties(sd));
  873. RINOK(SzReadID(sd, &type));
  874. }
  875. if (type == k7zIdMainStreamsInfo)
  876. {
  877. RINOK(SzReadStreamsInfo(sd,
  878. &db->ArchiveInfo.DataStartPosition,
  879. &db->Database,
  880. &numUnPackStreams,
  881. unPackSizes,
  882. digestsDefined,
  883. digests, allocMain->Alloc, allocTemp));
  884. db->ArchiveInfo.DataStartPosition += db->ArchiveInfo.StartPositionAfterHeader;
  885. RINOK(SzReadID(sd, &type));
  886. }
  887. if (type == k7zIdEnd)
  888. return SZ_OK;
  889. if (type != k7zIdFilesInfo)
  890. return SZE_ARCHIVE_ERROR;
  891. RINOK(SzReadNumber32(sd, &numFiles));
  892. db->Database.NumFiles = numFiles;
  893. MY_ALLOC(CFileItem, files, (size_t)numFiles, allocMain->Alloc);
  894. db->Database.Files = files;
  895. for(i = 0; i < numFiles; i++)
  896. SzFileInit(files + i);
  897. for (;;)
  898. {
  899. UInt64 type;
  900. UInt64 size;
  901. RINOK(SzReadID(sd, &type));
  902. if (type == k7zIdEnd)
  903. break;
  904. RINOK(SzReadNumber(sd, &size));
  905. if ((UInt64)(int)type != type)
  906. {
  907. RINOK(SzSkeepDataSize(sd, size));
  908. }
  909. else
  910. switch((int)type)
  911. {
  912. case k7zIdName:
  913. {
  914. RINOK(SzReadSwitch(sd));
  915. RINOK(SzReadFileNames(sd, numFiles, files, allocMain->Alloc))
  916. break;
  917. }
  918. case k7zIdEmptyStream:
  919. {
  920. RINOK(SzReadBoolVector(sd, numFiles, emptyStreamVector, allocTemp->Alloc));
  921. numEmptyStreams = 0;
  922. for (i = 0; i < numFiles; i++)
  923. if ((*emptyStreamVector)[i])
  924. numEmptyStreams++;
  925. break;
  926. }
  927. case k7zIdEmptyFile:
  928. {
  929. RINOK(SzReadBoolVector(sd, numEmptyStreams, emptyFileVector, allocTemp->Alloc));
  930. break;
  931. }
  932. case k7zIdLastWriteTime:
  933. {
  934. RINOK(SzReadBoolVector2(sd, numFiles, lwtVector, allocTemp->Alloc));
  935. RINOK(SzReadSwitch(sd));
  936. for (i = 0; i < numFiles; i++)
  937. {
  938. CFileItem *f = &files[i];
  939. Byte defined = (*lwtVector)[i];
  940. f->IsLastWriteTimeDefined = defined;
  941. f->LastWriteTime.Low = f->LastWriteTime.High = 0;
  942. if (defined)
  943. {
  944. RINOK(SzReadUInt32(sd, &f->LastWriteTime.Low));
  945. RINOK(SzReadUInt32(sd, &f->LastWriteTime.High));
  946. }
  947. }
  948. break;
  949. }
  950. default:
  951. {
  952. RINOK(SzSkeepDataSize(sd, size));
  953. }
  954. }
  955. }
  956. {
  957. UInt32 emptyFileIndex = 0;
  958. UInt32 sizeIndex = 0;
  959. for(i = 0; i < numFiles; i++)
  960. {
  961. CFileItem *file = files + i;
  962. file->IsAnti = 0;
  963. if (*emptyStreamVector == 0)
  964. file->HasStream = 1;
  965. else
  966. file->HasStream = (Byte)((*emptyStreamVector)[i] ? 0 : 1);
  967. if(file->HasStream)
  968. {
  969. file->IsDirectory = 0;
  970. file->Size = (*unPackSizes)[sizeIndex];
  971. file->FileCRC = (*digests)[sizeIndex];
  972. file->IsFileCRCDefined = (Byte)(*digestsDefined)[sizeIndex];
  973. sizeIndex++;
  974. }
  975. else
  976. {
  977. if (*emptyFileVector == 0)
  978. file->IsDirectory = 1;
  979. else
  980. file->IsDirectory = (Byte)((*emptyFileVector)[emptyFileIndex] ? 0 : 1);
  981. emptyFileIndex++;
  982. file->Size = 0;
  983. file->IsFileCRCDefined = 0;
  984. }
  985. }
  986. }
  987. return SzArDbExFill(db, allocMain->Alloc);
  988. }
  989. SZ_RESULT SzReadHeader(
  990. CSzData *sd,
  991. CArchiveDatabaseEx *db,
  992. ISzAlloc *allocMain,
  993. ISzAlloc *allocTemp)
  994. {
  995. CFileSize *unPackSizes = 0;
  996. Byte *digestsDefined = 0;
  997. UInt32 *digests = 0;
  998. Byte *emptyStreamVector = 0;
  999. Byte *emptyFileVector = 0;
  1000. Byte *lwtVector = 0;
  1001. SZ_RESULT res = SzReadHeader2(sd, db,
  1002. &unPackSizes, &digestsDefined, &digests,
  1003. &emptyStreamVector, &emptyFileVector, &lwtVector,
  1004. allocMain, allocTemp);
  1005. allocTemp->Free(unPackSizes);
  1006. allocTemp->Free(digestsDefined);
  1007. allocTemp->Free(digests);
  1008. allocTemp->Free(emptyStreamVector);
  1009. allocTemp->Free(emptyFileVector);
  1010. allocTemp->Free(lwtVector);
  1011. return res;
  1012. }
  1013. SZ_RESULT SzReadAndDecodePackedStreams2(
  1014. ISzInStream *inStream,
  1015. CSzData *sd,
  1016. CSzByteBuffer *outBuffer,
  1017. CFileSize baseOffset,
  1018. CArchiveDatabase *db,
  1019. CFileSize **unPackSizes,
  1020. Byte **digestsDefined,
  1021. UInt32 **digests,
  1022. #ifndef _LZMA_IN_CB
  1023. Byte **inBuffer,
  1024. #endif
  1025. ISzAlloc *allocTemp)
  1026. {
  1027. UInt32 numUnPackStreams = 0;
  1028. CFileSize dataStartPos;
  1029. CFolder *folder;
  1030. #ifndef _LZMA_IN_CB
  1031. CFileSize packSize = 0;
  1032. UInt32 i = 0;
  1033. #endif
  1034. CFileSize unPackSize;
  1035. SZ_RESULT res;
  1036. RINOK(SzReadStreamsInfo(sd, &dataStartPos, db,
  1037. &numUnPackStreams, unPackSizes, digestsDefined, digests,
  1038. allocTemp->Alloc, allocTemp));
  1039. dataStartPos += baseOffset;
  1040. if (db->NumFolders != 1)
  1041. return SZE_ARCHIVE_ERROR;
  1042. folder = db->Folders;
  1043. unPackSize = SzFolderGetUnPackSize(folder);
  1044. RINOK(inStream->Seek(inStream, dataStartPos));
  1045. #ifndef _LZMA_IN_CB
  1046. for (i = 0; i < db->NumPackStreams; i++)
  1047. packSize += db->PackSizes[i];
  1048. MY_ALLOC(Byte, *inBuffer, (size_t)packSize, allocTemp->Alloc);
  1049. RINOK(SafeReadDirect(inStream, *inBuffer, (size_t)packSize));
  1050. #endif
  1051. if (!SzByteBufferCreate(outBuffer, (size_t)unPackSize, allocTemp->Alloc))
  1052. return SZE_OUTOFMEMORY;
  1053. res = SzDecode(db->PackSizes, folder,
  1054. #ifdef _LZMA_IN_CB
  1055. inStream, dataStartPos,
  1056. #else
  1057. *inBuffer,
  1058. #endif
  1059. outBuffer->Items, (size_t)unPackSize, allocTemp);
  1060. RINOK(res)
  1061. if (folder->UnPackCRCDefined)
  1062. if (CrcCalc(outBuffer->Items, (size_t)unPackSize) != folder->UnPackCRC)
  1063. return SZE_FAIL;
  1064. return SZ_OK;
  1065. }
  1066. SZ_RESULT SzReadAndDecodePackedStreams(
  1067. ISzInStream *inStream,
  1068. CSzData *sd,
  1069. CSzByteBuffer *outBuffer,
  1070. CFileSize baseOffset,
  1071. ISzAlloc *allocTemp)
  1072. {
  1073. CArchiveDatabase db;
  1074. CFileSize *unPackSizes = 0;
  1075. Byte *digestsDefined = 0;
  1076. UInt32 *digests = 0;
  1077. #ifndef _LZMA_IN_CB
  1078. Byte *inBuffer = 0;
  1079. #endif
  1080. SZ_RESULT res;
  1081. SzArchiveDatabaseInit(&db);
  1082. res = SzReadAndDecodePackedStreams2(inStream, sd, outBuffer, baseOffset,
  1083. &db, &unPackSizes, &digestsDefined, &digests,
  1084. #ifndef _LZMA_IN_CB
  1085. &inBuffer,
  1086. #endif
  1087. allocTemp);
  1088. SzArchiveDatabaseFree(&db, allocTemp->Free);
  1089. allocTemp->Free(unPackSizes);
  1090. allocTemp->Free(digestsDefined);
  1091. allocTemp->Free(digests);
  1092. #ifndef _LZMA_IN_CB
  1093. allocTemp->Free(inBuffer);
  1094. #endif
  1095. return res;
  1096. }
  1097. SZ_RESULT SzArchiveOpen2(
  1098. ISzInStream *inStream,
  1099. CArchiveDatabaseEx *db,
  1100. ISzAlloc *allocMain,
  1101. ISzAlloc *allocTemp)
  1102. {
  1103. Byte signature[k7zSignatureSize];
  1104. Byte version;
  1105. UInt32 crcFromArchive;
  1106. UInt64 nextHeaderOffset;
  1107. UInt64 nextHeaderSize;
  1108. UInt32 nextHeaderCRC;
  1109. UInt32 crc = 0;
  1110. CFileSize pos = 0;
  1111. CSzByteBuffer buffer;
  1112. CSzData sd;
  1113. SZ_RESULT res;
  1114. RINOK(SafeReadDirect(inStream, signature, k7zSignatureSize));
  1115. if (!TestSignatureCandidate(signature))
  1116. return SZE_ARCHIVE_ERROR;
  1117. /*
  1118. db.Clear();
  1119. db.ArchiveInfo.StartPosition = _arhiveBeginStreamPosition;
  1120. */
  1121. RINOK(SafeReadDirectByte(inStream, &version));
  1122. if (version != k7zMajorVersion)
  1123. return SZE_ARCHIVE_ERROR;
  1124. RINOK(SafeReadDirectByte(inStream, &version));
  1125. RINOK(SafeReadDirectUInt32(inStream, &crcFromArchive, &crc));
  1126. crc = CRC_INIT_VAL;
  1127. RINOK(SafeReadDirectUInt64(inStream, &nextHeaderOffset, &crc));
  1128. RINOK(SafeReadDirectUInt64(inStream, &nextHeaderSize, &crc));
  1129. RINOK(SafeReadDirectUInt32(inStream, &nextHeaderCRC, &crc));
  1130. pos = k7zStartHeaderSize;
  1131. db->ArchiveInfo.StartPositionAfterHeader = pos;
  1132. if (CRC_GET_DIGEST(crc) != crcFromArchive)
  1133. return SZE_ARCHIVE_ERROR;
  1134. if (nextHeaderSize == 0)
  1135. return SZ_OK;
  1136. RINOK(inStream->Seek(inStream, (CFileSize)(pos + nextHeaderOffset)));
  1137. if (!SzByteBufferCreate(&buffer, (size_t)nextHeaderSize, allocTemp->Alloc))
  1138. return SZE_OUTOFMEMORY;
  1139. res = SafeReadDirect(inStream, buffer.Items, (size_t)nextHeaderSize);
  1140. if (res == SZ_OK)
  1141. {
  1142. res = SZE_ARCHIVE_ERROR;
  1143. if (CrcCalc(buffer.Items, (UInt32)nextHeaderSize) == nextHeaderCRC)
  1144. {
  1145. for (;;)
  1146. {
  1147. UInt64 type;
  1148. sd.Data = buffer.Items;
  1149. sd.Size = buffer.Capacity;
  1150. res = SzReadID(&sd, &type);
  1151. if (res != SZ_OK)
  1152. break;
  1153. if (type == k7zIdHeader)
  1154. {
  1155. res = SzReadHeader(&sd, db, allocMain, allocTemp);
  1156. break;
  1157. }
  1158. if (type != k7zIdEncodedHeader)
  1159. {
  1160. res = SZE_ARCHIVE_ERROR;
  1161. break;
  1162. }
  1163. {
  1164. CSzByteBuffer outBuffer;
  1165. res = SzReadAndDecodePackedStreams(inStream, &sd, &outBuffer,
  1166. db->ArchiveInfo.StartPositionAfterHeader,
  1167. allocTemp);
  1168. if (res != SZ_OK)
  1169. {
  1170. SzByteBufferFree(&outBuffer, allocTemp->Free);
  1171. break;
  1172. }
  1173. SzByteBufferFree(&buffer, allocTemp->Free);
  1174. buffer.Items = outBuffer.Items;
  1175. buffer.Capacity = outBuffer.Capacity;
  1176. }
  1177. }
  1178. }
  1179. }
  1180. SzByteBufferFree(&buffer, allocTemp->Free);
  1181. return res;
  1182. }
  1183. SZ_RESULT SzArchiveOpen(
  1184. ISzInStream *inStream,
  1185. CArchiveDatabaseEx *db,
  1186. ISzAlloc *allocMain,
  1187. ISzAlloc *allocTemp)
  1188. {
  1189. SZ_RESULT res = SzArchiveOpen2(inStream, db, allocMain, allocTemp);
  1190. if (res != SZ_OK)
  1191. SzArDbExFree(db, allocMain->Free);
  1192. return res;
  1193. }