7zArcIn.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. /* 7zArcIn.c -- 7z Input functions
  2. 2018-12-31 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <string.h>
  5. #include "7z.h"
  6. #include "7zBuf.h"
  7. #include "7zCrc.h"
  8. #include "CpuArch.h"
  9. #define MY_ALLOC(T, p, size, alloc) { \
  10. if ((p = (T *)ISzAlloc_Alloc(alloc, (size) * sizeof(T))) == NULL) return SZ_ERROR_MEM; }
  11. #define MY_ALLOC_ZE(T, p, size, alloc) { if ((size) == 0) p = NULL; else MY_ALLOC(T, p, size, alloc) }
  12. #define MY_ALLOC_AND_CPY(to, size, from, alloc) \
  13. { MY_ALLOC(Byte, to, size, alloc); memcpy(to, from, size); }
  14. #define MY_ALLOC_ZE_AND_CPY(to, size, from, alloc) \
  15. { if ((size) == 0) to = NULL; else { MY_ALLOC_AND_CPY(to, size, from, alloc) } }
  16. #define k7zMajorVersion 0
  17. enum EIdEnum
  18. {
  19. k7zIdEnd,
  20. k7zIdHeader,
  21. k7zIdArchiveProperties,
  22. k7zIdAdditionalStreamsInfo,
  23. k7zIdMainStreamsInfo,
  24. k7zIdFilesInfo,
  25. k7zIdPackInfo,
  26. k7zIdUnpackInfo,
  27. k7zIdSubStreamsInfo,
  28. k7zIdSize,
  29. k7zIdCRC,
  30. k7zIdFolder,
  31. k7zIdCodersUnpackSize,
  32. k7zIdNumUnpackStream,
  33. k7zIdEmptyStream,
  34. k7zIdEmptyFile,
  35. k7zIdAnti,
  36. k7zIdName,
  37. k7zIdCTime,
  38. k7zIdATime,
  39. k7zIdMTime,
  40. k7zIdWinAttrib,
  41. k7zIdComment,
  42. k7zIdEncodedHeader,
  43. k7zIdStartPos,
  44. k7zIdDummy
  45. // k7zNtSecure,
  46. // k7zParent,
  47. // k7zIsReal
  48. };
  49. const Byte k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
  50. #define SzBitUi32s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; }
  51. static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAllocPtr alloc)
  52. {
  53. if (num == 0)
  54. {
  55. p->Defs = NULL;
  56. p->Vals = NULL;
  57. }
  58. else
  59. {
  60. MY_ALLOC(Byte, p->Defs, (num + 7) >> 3, alloc);
  61. MY_ALLOC(UInt32, p->Vals, num, alloc);
  62. }
  63. return SZ_OK;
  64. }
  65. void SzBitUi32s_Free(CSzBitUi32s *p, ISzAllocPtr alloc)
  66. {
  67. ISzAlloc_Free(alloc, p->Defs); p->Defs = NULL;
  68. ISzAlloc_Free(alloc, p->Vals); p->Vals = NULL;
  69. }
  70. #define SzBitUi64s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; }
  71. void SzBitUi64s_Free(CSzBitUi64s *p, ISzAllocPtr alloc)
  72. {
  73. ISzAlloc_Free(alloc, p->Defs); p->Defs = NULL;
  74. ISzAlloc_Free(alloc, p->Vals); p->Vals = NULL;
  75. }
  76. static void SzAr_Init(CSzAr *p)
  77. {
  78. p->NumPackStreams = 0;
  79. p->NumFolders = 0;
  80. p->PackPositions = NULL;
  81. SzBitUi32s_Init(&p->FolderCRCs);
  82. p->FoCodersOffsets = NULL;
  83. p->FoStartPackStreamIndex = NULL;
  84. p->FoToCoderUnpackSizes = NULL;
  85. p->FoToMainUnpackSizeIndex = NULL;
  86. p->CoderUnpackSizes = NULL;
  87. p->CodersData = NULL;
  88. }
  89. static void SzAr_Free(CSzAr *p, ISzAllocPtr alloc)
  90. {
  91. ISzAlloc_Free(alloc, p->PackPositions);
  92. SzBitUi32s_Free(&p->FolderCRCs, alloc);
  93. ISzAlloc_Free(alloc, p->FoCodersOffsets);
  94. ISzAlloc_Free(alloc, p->FoStartPackStreamIndex);
  95. ISzAlloc_Free(alloc, p->FoToCoderUnpackSizes);
  96. ISzAlloc_Free(alloc, p->FoToMainUnpackSizeIndex);
  97. ISzAlloc_Free(alloc, p->CoderUnpackSizes);
  98. ISzAlloc_Free(alloc, p->CodersData);
  99. SzAr_Init(p);
  100. }
  101. void SzArEx_Init(CSzArEx *p)
  102. {
  103. SzAr_Init(&p->db);
  104. p->NumFiles = 0;
  105. p->dataPos = 0;
  106. p->UnpackPositions = NULL;
  107. p->IsDirs = NULL;
  108. p->FolderToFile = NULL;
  109. p->FileToFolder = NULL;
  110. p->FileNameOffsets = NULL;
  111. p->FileNames = NULL;
  112. SzBitUi32s_Init(&p->CRCs);
  113. SzBitUi32s_Init(&p->Attribs);
  114. // SzBitUi32s_Init(&p->Parents);
  115. SzBitUi64s_Init(&p->MTime);
  116. SzBitUi64s_Init(&p->CTime);
  117. }
  118. void SzArEx_Free(CSzArEx *p, ISzAllocPtr alloc)
  119. {
  120. ISzAlloc_Free(alloc, p->UnpackPositions);
  121. ISzAlloc_Free(alloc, p->IsDirs);
  122. ISzAlloc_Free(alloc, p->FolderToFile);
  123. ISzAlloc_Free(alloc, p->FileToFolder);
  124. ISzAlloc_Free(alloc, p->FileNameOffsets);
  125. ISzAlloc_Free(alloc, p->FileNames);
  126. SzBitUi32s_Free(&p->CRCs, alloc);
  127. SzBitUi32s_Free(&p->Attribs, alloc);
  128. // SzBitUi32s_Free(&p->Parents, alloc);
  129. SzBitUi64s_Free(&p->MTime, alloc);
  130. SzBitUi64s_Free(&p->CTime, alloc);
  131. SzAr_Free(&p->db, alloc);
  132. SzArEx_Init(p);
  133. }
  134. static int TestSignatureCandidate(const Byte *testBytes)
  135. {
  136. unsigned i;
  137. for (i = 0; i < k7zSignatureSize; i++)
  138. if (testBytes[i] != k7zSignature[i])
  139. return 0;
  140. return 1;
  141. }
  142. #define SzData_Clear(p) { (p)->Data = NULL; (p)->Size = 0; }
  143. #define SZ_READ_BYTE_SD(_sd_, dest) if ((_sd_)->Size == 0) return SZ_ERROR_ARCHIVE; (_sd_)->Size--; dest = *(_sd_)->Data++;
  144. #define SZ_READ_BYTE(dest) SZ_READ_BYTE_SD(sd, dest)
  145. #define SZ_READ_BYTE_2(dest) if (sd.Size == 0) return SZ_ERROR_ARCHIVE; sd.Size--; dest = *sd.Data++;
  146. #define SKIP_DATA(sd, size) { sd->Size -= (size_t)(size); sd->Data += (size_t)(size); }
  147. #define SKIP_DATA2(sd, size) { sd.Size -= (size_t)(size); sd.Data += (size_t)(size); }
  148. #define SZ_READ_32(dest) if (sd.Size < 4) return SZ_ERROR_ARCHIVE; \
  149. dest = GetUi32(sd.Data); SKIP_DATA2(sd, 4);
  150. static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value)
  151. {
  152. Byte firstByte, mask;
  153. unsigned i;
  154. UInt32 v;
  155. SZ_READ_BYTE(firstByte);
  156. if ((firstByte & 0x80) == 0)
  157. {
  158. *value = firstByte;
  159. return SZ_OK;
  160. }
  161. SZ_READ_BYTE(v);
  162. if ((firstByte & 0x40) == 0)
  163. {
  164. *value = (((UInt32)firstByte & 0x3F) << 8) | v;
  165. return SZ_OK;
  166. }
  167. SZ_READ_BYTE(mask);
  168. *value = v | ((UInt32)mask << 8);
  169. mask = 0x20;
  170. for (i = 2; i < 8; i++)
  171. {
  172. Byte b;
  173. if ((firstByte & mask) == 0)
  174. {
  175. UInt64 highPart = (unsigned)firstByte & (unsigned)(mask - 1);
  176. *value |= (highPart << (8 * i));
  177. return SZ_OK;
  178. }
  179. SZ_READ_BYTE(b);
  180. *value |= ((UInt64)b << (8 * i));
  181. mask >>= 1;
  182. }
  183. return SZ_OK;
  184. }
  185. static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value)
  186. {
  187. Byte firstByte;
  188. UInt64 value64;
  189. if (sd->Size == 0)
  190. return SZ_ERROR_ARCHIVE;
  191. firstByte = *sd->Data;
  192. if ((firstByte & 0x80) == 0)
  193. {
  194. *value = firstByte;
  195. sd->Data++;
  196. sd->Size--;
  197. return SZ_OK;
  198. }
  199. RINOK(ReadNumber(sd, &value64));
  200. if (value64 >= (UInt32)0x80000000 - 1)
  201. return SZ_ERROR_UNSUPPORTED;
  202. if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 4)))
  203. return SZ_ERROR_UNSUPPORTED;
  204. *value = (UInt32)value64;
  205. return SZ_OK;
  206. }
  207. #define ReadID(sd, value) ReadNumber(sd, value)
  208. static SRes SkipData(CSzData *sd)
  209. {
  210. UInt64 size;
  211. RINOK(ReadNumber(sd, &size));
  212. if (size > sd->Size)
  213. return SZ_ERROR_ARCHIVE;
  214. SKIP_DATA(sd, size);
  215. return SZ_OK;
  216. }
  217. static SRes WaitId(CSzData *sd, UInt32 id)
  218. {
  219. for (;;)
  220. {
  221. UInt64 type;
  222. RINOK(ReadID(sd, &type));
  223. if (type == id)
  224. return SZ_OK;
  225. if (type == k7zIdEnd)
  226. return SZ_ERROR_ARCHIVE;
  227. RINOK(SkipData(sd));
  228. }
  229. }
  230. static SRes RememberBitVector(CSzData *sd, UInt32 numItems, const Byte **v)
  231. {
  232. UInt32 numBytes = (numItems + 7) >> 3;
  233. if (numBytes > sd->Size)
  234. return SZ_ERROR_ARCHIVE;
  235. *v = sd->Data;
  236. SKIP_DATA(sd, numBytes);
  237. return SZ_OK;
  238. }
  239. static UInt32 CountDefinedBits(const Byte *bits, UInt32 numItems)
  240. {
  241. Byte b = 0;
  242. unsigned m = 0;
  243. UInt32 sum = 0;
  244. for (; numItems != 0; numItems--)
  245. {
  246. if (m == 0)
  247. {
  248. b = *bits++;
  249. m = 8;
  250. }
  251. m--;
  252. sum += ((b >> m) & 1);
  253. }
  254. return sum;
  255. }
  256. static MY_NO_INLINE SRes ReadBitVector(CSzData *sd, UInt32 numItems, Byte **v, ISzAllocPtr alloc)
  257. {
  258. Byte allAreDefined;
  259. Byte *v2;
  260. UInt32 numBytes = (numItems + 7) >> 3;
  261. *v = NULL;
  262. SZ_READ_BYTE(allAreDefined);
  263. if (numBytes == 0)
  264. return SZ_OK;
  265. if (allAreDefined == 0)
  266. {
  267. if (numBytes > sd->Size)
  268. return SZ_ERROR_ARCHIVE;
  269. MY_ALLOC_AND_CPY(*v, numBytes, sd->Data, alloc);
  270. SKIP_DATA(sd, numBytes);
  271. return SZ_OK;
  272. }
  273. MY_ALLOC(Byte, *v, numBytes, alloc);
  274. v2 = *v;
  275. memset(v2, 0xFF, (size_t)numBytes);
  276. {
  277. unsigned numBits = (unsigned)numItems & 7;
  278. if (numBits != 0)
  279. v2[(size_t)numBytes - 1] = (Byte)((((UInt32)1 << numBits) - 1) << (8 - numBits));
  280. }
  281. return SZ_OK;
  282. }
  283. static MY_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc)
  284. {
  285. UInt32 i;
  286. CSzData sd;
  287. UInt32 *vals;
  288. const Byte *defs;
  289. MY_ALLOC_ZE(UInt32, crcs->Vals, numItems, alloc);
  290. sd = *sd2;
  291. defs = crcs->Defs;
  292. vals = crcs->Vals;
  293. for (i = 0; i < numItems; i++)
  294. if (SzBitArray_Check(defs, i))
  295. {
  296. SZ_READ_32(vals[i]);
  297. }
  298. else
  299. vals[i] = 0;
  300. *sd2 = sd;
  301. return SZ_OK;
  302. }
  303. static SRes ReadBitUi32s(CSzData *sd, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc)
  304. {
  305. SzBitUi32s_Free(crcs, alloc);
  306. RINOK(ReadBitVector(sd, numItems, &crcs->Defs, alloc));
  307. return ReadUi32s(sd, numItems, crcs, alloc);
  308. }
  309. static SRes SkipBitUi32s(CSzData *sd, UInt32 numItems)
  310. {
  311. Byte allAreDefined;
  312. UInt32 numDefined = numItems;
  313. SZ_READ_BYTE(allAreDefined);
  314. if (!allAreDefined)
  315. {
  316. size_t numBytes = (numItems + 7) >> 3;
  317. if (numBytes > sd->Size)
  318. return SZ_ERROR_ARCHIVE;
  319. numDefined = CountDefinedBits(sd->Data, numItems);
  320. SKIP_DATA(sd, numBytes);
  321. }
  322. if (numDefined > (sd->Size >> 2))
  323. return SZ_ERROR_ARCHIVE;
  324. SKIP_DATA(sd, (size_t)numDefined * 4);
  325. return SZ_OK;
  326. }
  327. static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAllocPtr alloc)
  328. {
  329. RINOK(SzReadNumber32(sd, &p->NumPackStreams));
  330. RINOK(WaitId(sd, k7zIdSize));
  331. MY_ALLOC(UInt64, p->PackPositions, (size_t)p->NumPackStreams + 1, alloc);
  332. {
  333. UInt64 sum = 0;
  334. UInt32 i;
  335. UInt32 numPackStreams = p->NumPackStreams;
  336. for (i = 0; i < numPackStreams; i++)
  337. {
  338. UInt64 packSize;
  339. p->PackPositions[i] = sum;
  340. RINOK(ReadNumber(sd, &packSize));
  341. sum += packSize;
  342. if (sum < packSize)
  343. return SZ_ERROR_ARCHIVE;
  344. }
  345. p->PackPositions[i] = sum;
  346. }
  347. for (;;)
  348. {
  349. UInt64 type;
  350. RINOK(ReadID(sd, &type));
  351. if (type == k7zIdEnd)
  352. return SZ_OK;
  353. if (type == k7zIdCRC)
  354. {
  355. /* CRC of packed streams is unused now */
  356. RINOK(SkipBitUi32s(sd, p->NumPackStreams));
  357. continue;
  358. }
  359. RINOK(SkipData(sd));
  360. }
  361. }
  362. /*
  363. static SRes SzReadSwitch(CSzData *sd)
  364. {
  365. Byte external;
  366. RINOK(SzReadByte(sd, &external));
  367. return (external == 0) ? SZ_OK: SZ_ERROR_UNSUPPORTED;
  368. }
  369. */
  370. #define k_NumCodersStreams_in_Folder_MAX (SZ_NUM_BONDS_IN_FOLDER_MAX + SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX)
  371. SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
  372. {
  373. UInt32 numCoders, i;
  374. UInt32 numInStreams = 0;
  375. const Byte *dataStart = sd->Data;
  376. f->NumCoders = 0;
  377. f->NumBonds = 0;
  378. f->NumPackStreams = 0;
  379. f->UnpackStream = 0;
  380. RINOK(SzReadNumber32(sd, &numCoders));
  381. if (numCoders == 0 || numCoders > SZ_NUM_CODERS_IN_FOLDER_MAX)
  382. return SZ_ERROR_UNSUPPORTED;
  383. for (i = 0; i < numCoders; i++)
  384. {
  385. Byte mainByte;
  386. CSzCoderInfo *coder = f->Coders + i;
  387. unsigned idSize, j;
  388. UInt64 id;
  389. SZ_READ_BYTE(mainByte);
  390. if ((mainByte & 0xC0) != 0)
  391. return SZ_ERROR_UNSUPPORTED;
  392. idSize = (unsigned)(mainByte & 0xF);
  393. if (idSize > sizeof(id))
  394. return SZ_ERROR_UNSUPPORTED;
  395. if (idSize > sd->Size)
  396. return SZ_ERROR_ARCHIVE;
  397. id = 0;
  398. for (j = 0; j < idSize; j++)
  399. {
  400. id = ((id << 8) | *sd->Data);
  401. sd->Data++;
  402. sd->Size--;
  403. }
  404. if (id > (UInt32)0xFFFFFFFF)
  405. return SZ_ERROR_UNSUPPORTED;
  406. coder->MethodID = (UInt32)id;
  407. coder->NumStreams = 1;
  408. coder->PropsOffset = 0;
  409. coder->PropsSize = 0;
  410. if ((mainByte & 0x10) != 0)
  411. {
  412. UInt32 numStreams;
  413. RINOK(SzReadNumber32(sd, &numStreams));
  414. if (numStreams > k_NumCodersStreams_in_Folder_MAX)
  415. return SZ_ERROR_UNSUPPORTED;
  416. coder->NumStreams = (Byte)numStreams;
  417. RINOK(SzReadNumber32(sd, &numStreams));
  418. if (numStreams != 1)
  419. return SZ_ERROR_UNSUPPORTED;
  420. }
  421. numInStreams += coder->NumStreams;
  422. if (numInStreams > k_NumCodersStreams_in_Folder_MAX)
  423. return SZ_ERROR_UNSUPPORTED;
  424. if ((mainByte & 0x20) != 0)
  425. {
  426. UInt32 propsSize = 0;
  427. RINOK(SzReadNumber32(sd, &propsSize));
  428. if (propsSize > sd->Size)
  429. return SZ_ERROR_ARCHIVE;
  430. if (propsSize >= 0x80)
  431. return SZ_ERROR_UNSUPPORTED;
  432. coder->PropsOffset = sd->Data - dataStart;
  433. coder->PropsSize = (Byte)propsSize;
  434. sd->Data += (size_t)propsSize;
  435. sd->Size -= (size_t)propsSize;
  436. }
  437. }
  438. /*
  439. if (numInStreams == 1 && numCoders == 1)
  440. {
  441. f->NumPackStreams = 1;
  442. f->PackStreams[0] = 0;
  443. }
  444. else
  445. */
  446. {
  447. Byte streamUsed[k_NumCodersStreams_in_Folder_MAX];
  448. UInt32 numBonds, numPackStreams;
  449. numBonds = numCoders - 1;
  450. if (numInStreams < numBonds)
  451. return SZ_ERROR_ARCHIVE;
  452. if (numBonds > SZ_NUM_BONDS_IN_FOLDER_MAX)
  453. return SZ_ERROR_UNSUPPORTED;
  454. f->NumBonds = numBonds;
  455. numPackStreams = numInStreams - numBonds;
  456. if (numPackStreams > SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX)
  457. return SZ_ERROR_UNSUPPORTED;
  458. f->NumPackStreams = numPackStreams;
  459. for (i = 0; i < numInStreams; i++)
  460. streamUsed[i] = False;
  461. if (numBonds != 0)
  462. {
  463. Byte coderUsed[SZ_NUM_CODERS_IN_FOLDER_MAX];
  464. for (i = 0; i < numCoders; i++)
  465. coderUsed[i] = False;
  466. for (i = 0; i < numBonds; i++)
  467. {
  468. CSzBond *bp = f->Bonds + i;
  469. RINOK(SzReadNumber32(sd, &bp->InIndex));
  470. if (bp->InIndex >= numInStreams || streamUsed[bp->InIndex])
  471. return SZ_ERROR_ARCHIVE;
  472. streamUsed[bp->InIndex] = True;
  473. RINOK(SzReadNumber32(sd, &bp->OutIndex));
  474. if (bp->OutIndex >= numCoders || coderUsed[bp->OutIndex])
  475. return SZ_ERROR_ARCHIVE;
  476. coderUsed[bp->OutIndex] = True;
  477. }
  478. for (i = 0; i < numCoders; i++)
  479. if (!coderUsed[i])
  480. {
  481. f->UnpackStream = i;
  482. break;
  483. }
  484. if (i == numCoders)
  485. return SZ_ERROR_ARCHIVE;
  486. }
  487. if (numPackStreams == 1)
  488. {
  489. for (i = 0; i < numInStreams; i++)
  490. if (!streamUsed[i])
  491. break;
  492. if (i == numInStreams)
  493. return SZ_ERROR_ARCHIVE;
  494. f->PackStreams[0] = i;
  495. }
  496. else
  497. for (i = 0; i < numPackStreams; i++)
  498. {
  499. UInt32 index;
  500. RINOK(SzReadNumber32(sd, &index));
  501. if (index >= numInStreams || streamUsed[index])
  502. return SZ_ERROR_ARCHIVE;
  503. streamUsed[index] = True;
  504. f->PackStreams[i] = index;
  505. }
  506. }
  507. f->NumCoders = numCoders;
  508. return SZ_OK;
  509. }
  510. static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num)
  511. {
  512. CSzData sd;
  513. sd = *sd2;
  514. for (; num != 0; num--)
  515. {
  516. Byte firstByte, mask;
  517. unsigned i;
  518. SZ_READ_BYTE_2(firstByte);
  519. if ((firstByte & 0x80) == 0)
  520. continue;
  521. if ((firstByte & 0x40) == 0)
  522. {
  523. if (sd.Size == 0)
  524. return SZ_ERROR_ARCHIVE;
  525. sd.Size--;
  526. sd.Data++;
  527. continue;
  528. }
  529. mask = 0x20;
  530. for (i = 2; i < 8 && (firstByte & mask) != 0; i++)
  531. mask >>= 1;
  532. if (i > sd.Size)
  533. return SZ_ERROR_ARCHIVE;
  534. SKIP_DATA2(sd, i);
  535. }
  536. *sd2 = sd;
  537. return SZ_OK;
  538. }
  539. #define k_Scan_NumCoders_MAX 64
  540. #define k_Scan_NumCodersStreams_in_Folder_MAX 64
  541. static SRes ReadUnpackInfo(CSzAr *p,
  542. CSzData *sd2,
  543. UInt32 numFoldersMax,
  544. const CBuf *tempBufs, UInt32 numTempBufs,
  545. ISzAllocPtr alloc)
  546. {
  547. CSzData sd;
  548. UInt32 fo, numFolders, numCodersOutStreams, packStreamIndex;
  549. const Byte *startBufPtr;
  550. Byte external;
  551. RINOK(WaitId(sd2, k7zIdFolder));
  552. RINOK(SzReadNumber32(sd2, &numFolders));
  553. if (numFolders > numFoldersMax)
  554. return SZ_ERROR_UNSUPPORTED;
  555. p->NumFolders = numFolders;
  556. SZ_READ_BYTE_SD(sd2, external);
  557. if (external == 0)
  558. sd = *sd2;
  559. else
  560. {
  561. UInt32 index;
  562. RINOK(SzReadNumber32(sd2, &index));
  563. if (index >= numTempBufs)
  564. return SZ_ERROR_ARCHIVE;
  565. sd.Data = tempBufs[index].data;
  566. sd.Size = tempBufs[index].size;
  567. }
  568. MY_ALLOC(size_t, p->FoCodersOffsets, (size_t)numFolders + 1, alloc);
  569. MY_ALLOC(UInt32, p->FoStartPackStreamIndex, (size_t)numFolders + 1, alloc);
  570. MY_ALLOC(UInt32, p->FoToCoderUnpackSizes, (size_t)numFolders + 1, alloc);
  571. MY_ALLOC_ZE(Byte, p->FoToMainUnpackSizeIndex, (size_t)numFolders, alloc);
  572. startBufPtr = sd.Data;
  573. packStreamIndex = 0;
  574. numCodersOutStreams = 0;
  575. for (fo = 0; fo < numFolders; fo++)
  576. {
  577. UInt32 numCoders, ci, numInStreams = 0;
  578. p->FoCodersOffsets[fo] = sd.Data - startBufPtr;
  579. RINOK(SzReadNumber32(&sd, &numCoders));
  580. if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX)
  581. return SZ_ERROR_UNSUPPORTED;
  582. for (ci = 0; ci < numCoders; ci++)
  583. {
  584. Byte mainByte;
  585. unsigned idSize;
  586. UInt32 coderInStreams;
  587. SZ_READ_BYTE_2(mainByte);
  588. if ((mainByte & 0xC0) != 0)
  589. return SZ_ERROR_UNSUPPORTED;
  590. idSize = (mainByte & 0xF);
  591. if (idSize > 8)
  592. return SZ_ERROR_UNSUPPORTED;
  593. if (idSize > sd.Size)
  594. return SZ_ERROR_ARCHIVE;
  595. SKIP_DATA2(sd, idSize);
  596. coderInStreams = 1;
  597. if ((mainByte & 0x10) != 0)
  598. {
  599. UInt32 coderOutStreams;
  600. RINOK(SzReadNumber32(&sd, &coderInStreams));
  601. RINOK(SzReadNumber32(&sd, &coderOutStreams));
  602. if (coderInStreams > k_Scan_NumCodersStreams_in_Folder_MAX || coderOutStreams != 1)
  603. return SZ_ERROR_UNSUPPORTED;
  604. }
  605. numInStreams += coderInStreams;
  606. if ((mainByte & 0x20) != 0)
  607. {
  608. UInt32 propsSize;
  609. RINOK(SzReadNumber32(&sd, &propsSize));
  610. if (propsSize > sd.Size)
  611. return SZ_ERROR_ARCHIVE;
  612. SKIP_DATA2(sd, propsSize);
  613. }
  614. }
  615. {
  616. UInt32 indexOfMainStream = 0;
  617. UInt32 numPackStreams = 1;
  618. if (numCoders != 1 || numInStreams != 1)
  619. {
  620. Byte streamUsed[k_Scan_NumCodersStreams_in_Folder_MAX];
  621. Byte coderUsed[k_Scan_NumCoders_MAX];
  622. UInt32 i;
  623. UInt32 numBonds = numCoders - 1;
  624. if (numInStreams < numBonds)
  625. return SZ_ERROR_ARCHIVE;
  626. if (numInStreams > k_Scan_NumCodersStreams_in_Folder_MAX)
  627. return SZ_ERROR_UNSUPPORTED;
  628. for (i = 0; i < numInStreams; i++)
  629. streamUsed[i] = False;
  630. for (i = 0; i < numCoders; i++)
  631. coderUsed[i] = False;
  632. for (i = 0; i < numBonds; i++)
  633. {
  634. UInt32 index;
  635. RINOK(SzReadNumber32(&sd, &index));
  636. if (index >= numInStreams || streamUsed[index])
  637. return SZ_ERROR_ARCHIVE;
  638. streamUsed[index] = True;
  639. RINOK(SzReadNumber32(&sd, &index));
  640. if (index >= numCoders || coderUsed[index])
  641. return SZ_ERROR_ARCHIVE;
  642. coderUsed[index] = True;
  643. }
  644. numPackStreams = numInStreams - numBonds;
  645. if (numPackStreams != 1)
  646. for (i = 0; i < numPackStreams; i++)
  647. {
  648. UInt32 index;
  649. RINOK(SzReadNumber32(&sd, &index));
  650. if (index >= numInStreams || streamUsed[index])
  651. return SZ_ERROR_ARCHIVE;
  652. streamUsed[index] = True;
  653. }
  654. for (i = 0; i < numCoders; i++)
  655. if (!coderUsed[i])
  656. {
  657. indexOfMainStream = i;
  658. break;
  659. }
  660. if (i == numCoders)
  661. return SZ_ERROR_ARCHIVE;
  662. }
  663. p->FoStartPackStreamIndex[fo] = packStreamIndex;
  664. p->FoToCoderUnpackSizes[fo] = numCodersOutStreams;
  665. p->FoToMainUnpackSizeIndex[fo] = (Byte)indexOfMainStream;
  666. numCodersOutStreams += numCoders;
  667. if (numCodersOutStreams < numCoders)
  668. return SZ_ERROR_UNSUPPORTED;
  669. if (numPackStreams > p->NumPackStreams - packStreamIndex)
  670. return SZ_ERROR_ARCHIVE;
  671. packStreamIndex += numPackStreams;
  672. }
  673. }
  674. p->FoToCoderUnpackSizes[fo] = numCodersOutStreams;
  675. {
  676. size_t dataSize = sd.Data - startBufPtr;
  677. p->FoStartPackStreamIndex[fo] = packStreamIndex;
  678. p->FoCodersOffsets[fo] = dataSize;
  679. MY_ALLOC_ZE_AND_CPY(p->CodersData, dataSize, startBufPtr, alloc);
  680. }
  681. if (external != 0)
  682. {
  683. if (sd.Size != 0)
  684. return SZ_ERROR_ARCHIVE;
  685. sd = *sd2;
  686. }
  687. RINOK(WaitId(&sd, k7zIdCodersUnpackSize));
  688. MY_ALLOC_ZE(UInt64, p->CoderUnpackSizes, (size_t)numCodersOutStreams, alloc);
  689. {
  690. UInt32 i;
  691. for (i = 0; i < numCodersOutStreams; i++)
  692. {
  693. RINOK(ReadNumber(&sd, p->CoderUnpackSizes + i));
  694. }
  695. }
  696. for (;;)
  697. {
  698. UInt64 type;
  699. RINOK(ReadID(&sd, &type));
  700. if (type == k7zIdEnd)
  701. {
  702. *sd2 = sd;
  703. return SZ_OK;
  704. }
  705. if (type == k7zIdCRC)
  706. {
  707. RINOK(ReadBitUi32s(&sd, numFolders, &p->FolderCRCs, alloc));
  708. continue;
  709. }
  710. RINOK(SkipData(&sd));
  711. }
  712. }
  713. UInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex)
  714. {
  715. return p->CoderUnpackSizes[p->FoToCoderUnpackSizes[folderIndex] + p->FoToMainUnpackSizeIndex[folderIndex]];
  716. }
  717. typedef struct
  718. {
  719. UInt32 NumTotalSubStreams;
  720. UInt32 NumSubDigests;
  721. CSzData sdNumSubStreams;
  722. CSzData sdSizes;
  723. CSzData sdCRCs;
  724. } CSubStreamInfo;
  725. static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi)
  726. {
  727. UInt64 type = 0;
  728. UInt32 numSubDigests = 0;
  729. UInt32 numFolders = p->NumFolders;
  730. UInt32 numUnpackStreams = numFolders;
  731. UInt32 numUnpackSizesInData = 0;
  732. for (;;)
  733. {
  734. RINOK(ReadID(sd, &type));
  735. if (type == k7zIdNumUnpackStream)
  736. {
  737. UInt32 i;
  738. ssi->sdNumSubStreams.Data = sd->Data;
  739. numUnpackStreams = 0;
  740. numSubDigests = 0;
  741. for (i = 0; i < numFolders; i++)
  742. {
  743. UInt32 numStreams;
  744. RINOK(SzReadNumber32(sd, &numStreams));
  745. if (numUnpackStreams > numUnpackStreams + numStreams)
  746. return SZ_ERROR_UNSUPPORTED;
  747. numUnpackStreams += numStreams;
  748. if (numStreams != 0)
  749. numUnpackSizesInData += (numStreams - 1);
  750. if (numStreams != 1 || !SzBitWithVals_Check(&p->FolderCRCs, i))
  751. numSubDigests += numStreams;
  752. }
  753. ssi->sdNumSubStreams.Size = sd->Data - ssi->sdNumSubStreams.Data;
  754. continue;
  755. }
  756. if (type == k7zIdCRC || type == k7zIdSize || type == k7zIdEnd)
  757. break;
  758. RINOK(SkipData(sd));
  759. }
  760. if (!ssi->sdNumSubStreams.Data)
  761. {
  762. numSubDigests = numFolders;
  763. if (p->FolderCRCs.Defs)
  764. numSubDigests = numFolders - CountDefinedBits(p->FolderCRCs.Defs, numFolders);
  765. }
  766. ssi->NumTotalSubStreams = numUnpackStreams;
  767. ssi->NumSubDigests = numSubDigests;
  768. if (type == k7zIdSize)
  769. {
  770. ssi->sdSizes.Data = sd->Data;
  771. RINOK(SkipNumbers(sd, numUnpackSizesInData));
  772. ssi->sdSizes.Size = sd->Data - ssi->sdSizes.Data;
  773. RINOK(ReadID(sd, &type));
  774. }
  775. for (;;)
  776. {
  777. if (type == k7zIdEnd)
  778. return SZ_OK;
  779. if (type == k7zIdCRC)
  780. {
  781. ssi->sdCRCs.Data = sd->Data;
  782. RINOK(SkipBitUi32s(sd, numSubDigests));
  783. ssi->sdCRCs.Size = sd->Data - ssi->sdCRCs.Data;
  784. }
  785. else
  786. {
  787. RINOK(SkipData(sd));
  788. }
  789. RINOK(ReadID(sd, &type));
  790. }
  791. }
  792. static SRes SzReadStreamsInfo(CSzAr *p,
  793. CSzData *sd,
  794. UInt32 numFoldersMax, const CBuf *tempBufs, UInt32 numTempBufs,
  795. UInt64 *dataOffset,
  796. CSubStreamInfo *ssi,
  797. ISzAllocPtr alloc)
  798. {
  799. UInt64 type;
  800. SzData_Clear(&ssi->sdSizes);
  801. SzData_Clear(&ssi->sdCRCs);
  802. SzData_Clear(&ssi->sdNumSubStreams);
  803. *dataOffset = 0;
  804. RINOK(ReadID(sd, &type));
  805. if (type == k7zIdPackInfo)
  806. {
  807. RINOK(ReadNumber(sd, dataOffset));
  808. RINOK(ReadPackInfo(p, sd, alloc));
  809. RINOK(ReadID(sd, &type));
  810. }
  811. if (type == k7zIdUnpackInfo)
  812. {
  813. RINOK(ReadUnpackInfo(p, sd, numFoldersMax, tempBufs, numTempBufs, alloc));
  814. RINOK(ReadID(sd, &type));
  815. }
  816. if (type == k7zIdSubStreamsInfo)
  817. {
  818. RINOK(ReadSubStreamsInfo(p, sd, ssi));
  819. RINOK(ReadID(sd, &type));
  820. }
  821. else
  822. {
  823. ssi->NumTotalSubStreams = p->NumFolders;
  824. // ssi->NumSubDigests = 0;
  825. }
  826. return (type == k7zIdEnd ? SZ_OK : SZ_ERROR_UNSUPPORTED);
  827. }
  828. static SRes SzReadAndDecodePackedStreams(
  829. ILookInStream *inStream,
  830. CSzData *sd,
  831. CBuf *tempBufs,
  832. UInt32 numFoldersMax,
  833. UInt64 baseOffset,
  834. CSzAr *p,
  835. ISzAllocPtr allocTemp)
  836. {
  837. UInt64 dataStartPos;
  838. UInt32 fo;
  839. CSubStreamInfo ssi;
  840. RINOK(SzReadStreamsInfo(p, sd, numFoldersMax, NULL, 0, &dataStartPos, &ssi, allocTemp));
  841. dataStartPos += baseOffset;
  842. if (p->NumFolders == 0)
  843. return SZ_ERROR_ARCHIVE;
  844. for (fo = 0; fo < p->NumFolders; fo++)
  845. Buf_Init(tempBufs + fo);
  846. for (fo = 0; fo < p->NumFolders; fo++)
  847. {
  848. CBuf *tempBuf = tempBufs + fo;
  849. UInt64 unpackSize = SzAr_GetFolderUnpackSize(p, fo);
  850. if ((size_t)unpackSize != unpackSize)
  851. return SZ_ERROR_MEM;
  852. if (!Buf_Create(tempBuf, (size_t)unpackSize, allocTemp))
  853. return SZ_ERROR_MEM;
  854. }
  855. for (fo = 0; fo < p->NumFolders; fo++)
  856. {
  857. const CBuf *tempBuf = tempBufs + fo;
  858. RINOK(LookInStream_SeekTo(inStream, dataStartPos));
  859. RINOK(SzAr_DecodeFolder(p, fo, inStream, dataStartPos, tempBuf->data, tempBuf->size, allocTemp));
  860. }
  861. return SZ_OK;
  862. }
  863. static SRes SzReadFileNames(const Byte *data, size_t size, UInt32 numFiles, size_t *offsets)
  864. {
  865. size_t pos = 0;
  866. *offsets++ = 0;
  867. if (numFiles == 0)
  868. return (size == 0) ? SZ_OK : SZ_ERROR_ARCHIVE;
  869. if (size < 2)
  870. return SZ_ERROR_ARCHIVE;
  871. if (data[size - 2] != 0 || data[size - 1] != 0)
  872. return SZ_ERROR_ARCHIVE;
  873. do
  874. {
  875. const Byte *p;
  876. if (pos == size)
  877. return SZ_ERROR_ARCHIVE;
  878. for (p = data + pos;
  879. #ifdef _WIN32
  880. *(const UInt16 *)p != 0
  881. #else
  882. p[0] != 0 || p[1] != 0
  883. #endif
  884. ; p += 2);
  885. pos = p - data + 2;
  886. *offsets++ = (pos >> 1);
  887. }
  888. while (--numFiles);
  889. return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
  890. }
  891. static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num,
  892. CSzData *sd2,
  893. const CBuf *tempBufs, UInt32 numTempBufs,
  894. ISzAllocPtr alloc)
  895. {
  896. CSzData sd;
  897. UInt32 i;
  898. CNtfsFileTime *vals;
  899. Byte *defs;
  900. Byte external;
  901. RINOK(ReadBitVector(sd2, num, &p->Defs, alloc));
  902. SZ_READ_BYTE_SD(sd2, external);
  903. if (external == 0)
  904. sd = *sd2;
  905. else
  906. {
  907. UInt32 index;
  908. RINOK(SzReadNumber32(sd2, &index));
  909. if (index >= numTempBufs)
  910. return SZ_ERROR_ARCHIVE;
  911. sd.Data = tempBufs[index].data;
  912. sd.Size = tempBufs[index].size;
  913. }
  914. MY_ALLOC_ZE(CNtfsFileTime, p->Vals, num, alloc);
  915. vals = p->Vals;
  916. defs = p->Defs;
  917. for (i = 0; i < num; i++)
  918. if (SzBitArray_Check(defs, i))
  919. {
  920. if (sd.Size < 8)
  921. return SZ_ERROR_ARCHIVE;
  922. vals[i].Low = GetUi32(sd.Data);
  923. vals[i].High = GetUi32(sd.Data + 4);
  924. SKIP_DATA2(sd, 8);
  925. }
  926. else
  927. vals[i].High = vals[i].Low = 0;
  928. if (external == 0)
  929. *sd2 = sd;
  930. return SZ_OK;
  931. }
  932. #define NUM_ADDITIONAL_STREAMS_MAX 8
  933. static SRes SzReadHeader2(
  934. CSzArEx *p, /* allocMain */
  935. CSzData *sd,
  936. ILookInStream *inStream,
  937. CBuf *tempBufs, UInt32 *numTempBufs,
  938. ISzAllocPtr allocMain,
  939. ISzAllocPtr allocTemp
  940. )
  941. {
  942. CSubStreamInfo ssi;
  943. {
  944. UInt64 type;
  945. SzData_Clear(&ssi.sdSizes);
  946. SzData_Clear(&ssi.sdCRCs);
  947. SzData_Clear(&ssi.sdNumSubStreams);
  948. ssi.NumSubDigests = 0;
  949. ssi.NumTotalSubStreams = 0;
  950. RINOK(ReadID(sd, &type));
  951. if (type == k7zIdArchiveProperties)
  952. {
  953. for (;;)
  954. {
  955. UInt64 type2;
  956. RINOK(ReadID(sd, &type2));
  957. if (type2 == k7zIdEnd)
  958. break;
  959. RINOK(SkipData(sd));
  960. }
  961. RINOK(ReadID(sd, &type));
  962. }
  963. if (type == k7zIdAdditionalStreamsInfo)
  964. {
  965. CSzAr tempAr;
  966. SRes res;
  967. SzAr_Init(&tempAr);
  968. res = SzReadAndDecodePackedStreams(inStream, sd, tempBufs, NUM_ADDITIONAL_STREAMS_MAX,
  969. p->startPosAfterHeader, &tempAr, allocTemp);
  970. *numTempBufs = tempAr.NumFolders;
  971. SzAr_Free(&tempAr, allocTemp);
  972. if (res != SZ_OK)
  973. return res;
  974. RINOK(ReadID(sd, &type));
  975. }
  976. if (type == k7zIdMainStreamsInfo)
  977. {
  978. RINOK(SzReadStreamsInfo(&p->db, sd, (UInt32)1 << 30, tempBufs, *numTempBufs,
  979. &p->dataPos, &ssi, allocMain));
  980. p->dataPos += p->startPosAfterHeader;
  981. RINOK(ReadID(sd, &type));
  982. }
  983. if (type == k7zIdEnd)
  984. {
  985. return SZ_OK;
  986. }
  987. if (type != k7zIdFilesInfo)
  988. return SZ_ERROR_ARCHIVE;
  989. }
  990. {
  991. UInt32 numFiles = 0;
  992. UInt32 numEmptyStreams = 0;
  993. const Byte *emptyStreams = NULL;
  994. const Byte *emptyFiles = NULL;
  995. RINOK(SzReadNumber32(sd, &numFiles));
  996. p->NumFiles = numFiles;
  997. for (;;)
  998. {
  999. UInt64 type;
  1000. UInt64 size;
  1001. RINOK(ReadID(sd, &type));
  1002. if (type == k7zIdEnd)
  1003. break;
  1004. RINOK(ReadNumber(sd, &size));
  1005. if (size > sd->Size)
  1006. return SZ_ERROR_ARCHIVE;
  1007. if (type >= ((UInt32)1 << 8))
  1008. {
  1009. SKIP_DATA(sd, size);
  1010. }
  1011. else switch ((unsigned)type)
  1012. {
  1013. case k7zIdName:
  1014. {
  1015. size_t namesSize;
  1016. const Byte *namesData;
  1017. Byte external;
  1018. SZ_READ_BYTE(external);
  1019. if (external == 0)
  1020. {
  1021. namesSize = (size_t)size - 1;
  1022. namesData = sd->Data;
  1023. }
  1024. else
  1025. {
  1026. UInt32 index;
  1027. RINOK(SzReadNumber32(sd, &index));
  1028. if (index >= *numTempBufs)
  1029. return SZ_ERROR_ARCHIVE;
  1030. namesData = (tempBufs)[index].data;
  1031. namesSize = (tempBufs)[index].size;
  1032. }
  1033. if ((namesSize & 1) != 0)
  1034. return SZ_ERROR_ARCHIVE;
  1035. MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain);
  1036. MY_ALLOC_ZE_AND_CPY(p->FileNames, namesSize, namesData, allocMain);
  1037. RINOK(SzReadFileNames(p->FileNames, namesSize, numFiles, p->FileNameOffsets))
  1038. if (external == 0)
  1039. {
  1040. SKIP_DATA(sd, namesSize);
  1041. }
  1042. break;
  1043. }
  1044. case k7zIdEmptyStream:
  1045. {
  1046. RINOK(RememberBitVector(sd, numFiles, &emptyStreams));
  1047. numEmptyStreams = CountDefinedBits(emptyStreams, numFiles);
  1048. emptyFiles = NULL;
  1049. break;
  1050. }
  1051. case k7zIdEmptyFile:
  1052. {
  1053. RINOK(RememberBitVector(sd, numEmptyStreams, &emptyFiles));
  1054. break;
  1055. }
  1056. case k7zIdWinAttrib:
  1057. {
  1058. Byte external;
  1059. CSzData sdSwitch;
  1060. CSzData *sdPtr;
  1061. SzBitUi32s_Free(&p->Attribs, allocMain);
  1062. RINOK(ReadBitVector(sd, numFiles, &p->Attribs.Defs, allocMain));
  1063. SZ_READ_BYTE(external);
  1064. if (external == 0)
  1065. sdPtr = sd;
  1066. else
  1067. {
  1068. UInt32 index;
  1069. RINOK(SzReadNumber32(sd, &index));
  1070. if (index >= *numTempBufs)
  1071. return SZ_ERROR_ARCHIVE;
  1072. sdSwitch.Data = (tempBufs)[index].data;
  1073. sdSwitch.Size = (tempBufs)[index].size;
  1074. sdPtr = &sdSwitch;
  1075. }
  1076. RINOK(ReadUi32s(sdPtr, numFiles, &p->Attribs, allocMain));
  1077. break;
  1078. }
  1079. /*
  1080. case k7zParent:
  1081. {
  1082. SzBitUi32s_Free(&p->Parents, allocMain);
  1083. RINOK(ReadBitVector(sd, numFiles, &p->Parents.Defs, allocMain));
  1084. RINOK(SzReadSwitch(sd));
  1085. RINOK(ReadUi32s(sd, numFiles, &p->Parents, allocMain));
  1086. break;
  1087. }
  1088. */
  1089. case k7zIdMTime: RINOK(ReadTime(&p->MTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break;
  1090. case k7zIdCTime: RINOK(ReadTime(&p->CTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break;
  1091. default:
  1092. {
  1093. SKIP_DATA(sd, size);
  1094. }
  1095. }
  1096. }
  1097. if (numFiles - numEmptyStreams != ssi.NumTotalSubStreams)
  1098. return SZ_ERROR_ARCHIVE;
  1099. for (;;)
  1100. {
  1101. UInt64 type;
  1102. RINOK(ReadID(sd, &type));
  1103. if (type == k7zIdEnd)
  1104. break;
  1105. RINOK(SkipData(sd));
  1106. }
  1107. {
  1108. UInt32 i;
  1109. UInt32 emptyFileIndex = 0;
  1110. UInt32 folderIndex = 0;
  1111. UInt32 remSubStreams = 0;
  1112. UInt32 numSubStreams = 0;
  1113. UInt64 unpackPos = 0;
  1114. const Byte *digestsDefs = NULL;
  1115. const Byte *digestsVals = NULL;
  1116. UInt32 digestsValsIndex = 0;
  1117. UInt32 digestIndex;
  1118. Byte allDigestsDefined = 0;
  1119. Byte isDirMask = 0;
  1120. Byte crcMask = 0;
  1121. Byte mask = 0x80;
  1122. MY_ALLOC(UInt32, p->FolderToFile, p->db.NumFolders + 1, allocMain);
  1123. MY_ALLOC_ZE(UInt32, p->FileToFolder, p->NumFiles, allocMain);
  1124. MY_ALLOC(UInt64, p->UnpackPositions, p->NumFiles + 1, allocMain);
  1125. MY_ALLOC_ZE(Byte, p->IsDirs, (p->NumFiles + 7) >> 3, allocMain);
  1126. RINOK(SzBitUi32s_Alloc(&p->CRCs, p->NumFiles, allocMain));
  1127. if (ssi.sdCRCs.Size != 0)
  1128. {
  1129. SZ_READ_BYTE_SD(&ssi.sdCRCs, allDigestsDefined);
  1130. if (allDigestsDefined)
  1131. digestsVals = ssi.sdCRCs.Data;
  1132. else
  1133. {
  1134. size_t numBytes = (ssi.NumSubDigests + 7) >> 3;
  1135. digestsDefs = ssi.sdCRCs.Data;
  1136. digestsVals = digestsDefs + numBytes;
  1137. }
  1138. }
  1139. digestIndex = 0;
  1140. for (i = 0; i < numFiles; i++, mask >>= 1)
  1141. {
  1142. if (mask == 0)
  1143. {
  1144. UInt32 byteIndex = (i - 1) >> 3;
  1145. p->IsDirs[byteIndex] = isDirMask;
  1146. p->CRCs.Defs[byteIndex] = crcMask;
  1147. isDirMask = 0;
  1148. crcMask = 0;
  1149. mask = 0x80;
  1150. }
  1151. p->UnpackPositions[i] = unpackPos;
  1152. p->CRCs.Vals[i] = 0;
  1153. if (emptyStreams && SzBitArray_Check(emptyStreams, i))
  1154. {
  1155. if (emptyFiles)
  1156. {
  1157. if (!SzBitArray_Check(emptyFiles, emptyFileIndex))
  1158. isDirMask |= mask;
  1159. emptyFileIndex++;
  1160. }
  1161. else
  1162. isDirMask |= mask;
  1163. if (remSubStreams == 0)
  1164. {
  1165. p->FileToFolder[i] = (UInt32)-1;
  1166. continue;
  1167. }
  1168. }
  1169. if (remSubStreams == 0)
  1170. {
  1171. for (;;)
  1172. {
  1173. if (folderIndex >= p->db.NumFolders)
  1174. return SZ_ERROR_ARCHIVE;
  1175. p->FolderToFile[folderIndex] = i;
  1176. numSubStreams = 1;
  1177. if (ssi.sdNumSubStreams.Data)
  1178. {
  1179. RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams));
  1180. }
  1181. remSubStreams = numSubStreams;
  1182. if (numSubStreams != 0)
  1183. break;
  1184. {
  1185. UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  1186. unpackPos += folderUnpackSize;
  1187. if (unpackPos < folderUnpackSize)
  1188. return SZ_ERROR_ARCHIVE;
  1189. }
  1190. folderIndex++;
  1191. }
  1192. }
  1193. p->FileToFolder[i] = folderIndex;
  1194. if (emptyStreams && SzBitArray_Check(emptyStreams, i))
  1195. continue;
  1196. if (--remSubStreams == 0)
  1197. {
  1198. UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  1199. UInt64 startFolderUnpackPos = p->UnpackPositions[p->FolderToFile[folderIndex]];
  1200. if (folderUnpackSize < unpackPos - startFolderUnpackPos)
  1201. return SZ_ERROR_ARCHIVE;
  1202. unpackPos = startFolderUnpackPos + folderUnpackSize;
  1203. if (unpackPos < folderUnpackSize)
  1204. return SZ_ERROR_ARCHIVE;
  1205. if (numSubStreams == 1 && SzBitWithVals_Check(&p->db.FolderCRCs, i))
  1206. {
  1207. p->CRCs.Vals[i] = p->db.FolderCRCs.Vals[folderIndex];
  1208. crcMask |= mask;
  1209. }
  1210. else if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex)))
  1211. {
  1212. p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4);
  1213. digestsValsIndex++;
  1214. crcMask |= mask;
  1215. }
  1216. folderIndex++;
  1217. }
  1218. else
  1219. {
  1220. UInt64 v;
  1221. RINOK(ReadNumber(&ssi.sdSizes, &v));
  1222. unpackPos += v;
  1223. if (unpackPos < v)
  1224. return SZ_ERROR_ARCHIVE;
  1225. if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex)))
  1226. {
  1227. p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4);
  1228. digestsValsIndex++;
  1229. crcMask |= mask;
  1230. }
  1231. }
  1232. }
  1233. if (mask != 0x80)
  1234. {
  1235. UInt32 byteIndex = (i - 1) >> 3;
  1236. p->IsDirs[byteIndex] = isDirMask;
  1237. p->CRCs.Defs[byteIndex] = crcMask;
  1238. }
  1239. p->UnpackPositions[i] = unpackPos;
  1240. if (remSubStreams != 0)
  1241. return SZ_ERROR_ARCHIVE;
  1242. for (;;)
  1243. {
  1244. p->FolderToFile[folderIndex] = i;
  1245. if (folderIndex >= p->db.NumFolders)
  1246. break;
  1247. if (!ssi.sdNumSubStreams.Data)
  1248. return SZ_ERROR_ARCHIVE;
  1249. RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams));
  1250. if (numSubStreams != 0)
  1251. return SZ_ERROR_ARCHIVE;
  1252. /*
  1253. {
  1254. UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  1255. unpackPos += folderUnpackSize;
  1256. if (unpackPos < folderUnpackSize)
  1257. return SZ_ERROR_ARCHIVE;
  1258. }
  1259. */
  1260. folderIndex++;
  1261. }
  1262. if (ssi.sdNumSubStreams.Data && ssi.sdNumSubStreams.Size != 0)
  1263. return SZ_ERROR_ARCHIVE;
  1264. }
  1265. }
  1266. return SZ_OK;
  1267. }
  1268. static SRes SzReadHeader(
  1269. CSzArEx *p,
  1270. CSzData *sd,
  1271. ILookInStream *inStream,
  1272. ISzAllocPtr allocMain,
  1273. ISzAllocPtr allocTemp)
  1274. {
  1275. UInt32 i;
  1276. UInt32 numTempBufs = 0;
  1277. SRes res;
  1278. CBuf tempBufs[NUM_ADDITIONAL_STREAMS_MAX];
  1279. for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++)
  1280. Buf_Init(tempBufs + i);
  1281. res = SzReadHeader2(p, sd, inStream,
  1282. tempBufs, &numTempBufs,
  1283. allocMain, allocTemp);
  1284. for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++)
  1285. Buf_Free(tempBufs + i, allocTemp);
  1286. RINOK(res);
  1287. if (sd->Size != 0)
  1288. return SZ_ERROR_FAIL;
  1289. return res;
  1290. }
  1291. static SRes SzArEx_Open2(
  1292. CSzArEx *p,
  1293. ILookInStream *inStream,
  1294. ISzAllocPtr allocMain,
  1295. ISzAllocPtr allocTemp)
  1296. {
  1297. Byte header[k7zStartHeaderSize];
  1298. Int64 startArcPos;
  1299. UInt64 nextHeaderOffset, nextHeaderSize;
  1300. size_t nextHeaderSizeT;
  1301. UInt32 nextHeaderCRC;
  1302. CBuf buf;
  1303. SRes res;
  1304. startArcPos = 0;
  1305. RINOK(ILookInStream_Seek(inStream, &startArcPos, SZ_SEEK_CUR));
  1306. RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize, SZ_ERROR_NO_ARCHIVE));
  1307. if (!TestSignatureCandidate(header))
  1308. return SZ_ERROR_NO_ARCHIVE;
  1309. if (header[6] != k7zMajorVersion)
  1310. return SZ_ERROR_UNSUPPORTED;
  1311. nextHeaderOffset = GetUi64(header + 12);
  1312. nextHeaderSize = GetUi64(header + 20);
  1313. nextHeaderCRC = GetUi32(header + 28);
  1314. p->startPosAfterHeader = startArcPos + k7zStartHeaderSize;
  1315. if (CrcCalc(header + 12, 20) != GetUi32(header + 8))
  1316. return SZ_ERROR_CRC;
  1317. nextHeaderSizeT = (size_t)nextHeaderSize;
  1318. if (nextHeaderSizeT != nextHeaderSize)
  1319. return SZ_ERROR_MEM;
  1320. if (nextHeaderSizeT == 0)
  1321. return SZ_OK;
  1322. if (nextHeaderOffset > nextHeaderOffset + nextHeaderSize ||
  1323. nextHeaderOffset > nextHeaderOffset + nextHeaderSize + k7zStartHeaderSize)
  1324. return SZ_ERROR_NO_ARCHIVE;
  1325. {
  1326. Int64 pos = 0;
  1327. RINOK(ILookInStream_Seek(inStream, &pos, SZ_SEEK_END));
  1328. if ((UInt64)pos < startArcPos + nextHeaderOffset ||
  1329. (UInt64)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset ||
  1330. (UInt64)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize)
  1331. return SZ_ERROR_INPUT_EOF;
  1332. }
  1333. RINOK(LookInStream_SeekTo(inStream, startArcPos + k7zStartHeaderSize + nextHeaderOffset));
  1334. if (!Buf_Create(&buf, nextHeaderSizeT, allocTemp))
  1335. return SZ_ERROR_MEM;
  1336. res = LookInStream_Read(inStream, buf.data, nextHeaderSizeT);
  1337. if (res == SZ_OK)
  1338. {
  1339. res = SZ_ERROR_ARCHIVE;
  1340. if (CrcCalc(buf.data, nextHeaderSizeT) == nextHeaderCRC)
  1341. {
  1342. CSzData sd;
  1343. UInt64 type;
  1344. sd.Data = buf.data;
  1345. sd.Size = buf.size;
  1346. res = ReadID(&sd, &type);
  1347. if (res == SZ_OK && type == k7zIdEncodedHeader)
  1348. {
  1349. CSzAr tempAr;
  1350. CBuf tempBuf;
  1351. Buf_Init(&tempBuf);
  1352. SzAr_Init(&tempAr);
  1353. res = SzReadAndDecodePackedStreams(inStream, &sd, &tempBuf, 1, p->startPosAfterHeader, &tempAr, allocTemp);
  1354. SzAr_Free(&tempAr, allocTemp);
  1355. if (res != SZ_OK)
  1356. {
  1357. Buf_Free(&tempBuf, allocTemp);
  1358. }
  1359. else
  1360. {
  1361. Buf_Free(&buf, allocTemp);
  1362. buf.data = tempBuf.data;
  1363. buf.size = tempBuf.size;
  1364. sd.Data = buf.data;
  1365. sd.Size = buf.size;
  1366. res = ReadID(&sd, &type);
  1367. }
  1368. }
  1369. if (res == SZ_OK)
  1370. {
  1371. if (type == k7zIdHeader)
  1372. {
  1373. /*
  1374. CSzData sd2;
  1375. unsigned ttt;
  1376. for (ttt = 0; ttt < 40000; ttt++)
  1377. {
  1378. SzArEx_Free(p, allocMain);
  1379. sd2 = sd;
  1380. res = SzReadHeader(p, &sd2, inStream, allocMain, allocTemp);
  1381. if (res != SZ_OK)
  1382. break;
  1383. }
  1384. */
  1385. res = SzReadHeader(p, &sd, inStream, allocMain, allocTemp);
  1386. }
  1387. else
  1388. res = SZ_ERROR_UNSUPPORTED;
  1389. }
  1390. }
  1391. }
  1392. Buf_Free(&buf, allocTemp);
  1393. return res;
  1394. }
  1395. SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream,
  1396. ISzAllocPtr allocMain, ISzAllocPtr allocTemp)
  1397. {
  1398. SRes res = SzArEx_Open2(p, inStream, allocMain, allocTemp);
  1399. if (res != SZ_OK)
  1400. SzArEx_Free(p, allocMain);
  1401. return res;
  1402. }
  1403. SRes SzArEx_Extract(
  1404. const CSzArEx *p,
  1405. ILookInStream *inStream,
  1406. UInt32 fileIndex,
  1407. UInt32 *blockIndex,
  1408. Byte **tempBuf,
  1409. size_t *outBufferSize,
  1410. size_t *offset,
  1411. size_t *outSizeProcessed,
  1412. ISzAllocPtr allocMain,
  1413. ISzAllocPtr allocTemp)
  1414. {
  1415. UInt32 folderIndex = p->FileToFolder[fileIndex];
  1416. SRes res = SZ_OK;
  1417. *offset = 0;
  1418. *outSizeProcessed = 0;
  1419. if (folderIndex == (UInt32)-1)
  1420. {
  1421. ISzAlloc_Free(allocMain, *tempBuf);
  1422. *blockIndex = folderIndex;
  1423. *tempBuf = NULL;
  1424. *outBufferSize = 0;
  1425. return SZ_OK;
  1426. }
  1427. if (*tempBuf == NULL || *blockIndex != folderIndex)
  1428. {
  1429. UInt64 unpackSizeSpec = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  1430. /*
  1431. UInt64 unpackSizeSpec =
  1432. p->UnpackPositions[p->FolderToFile[(size_t)folderIndex + 1]] -
  1433. p->UnpackPositions[p->FolderToFile[folderIndex]];
  1434. */
  1435. size_t unpackSize = (size_t)unpackSizeSpec;
  1436. if (unpackSize != unpackSizeSpec)
  1437. return SZ_ERROR_MEM;
  1438. *blockIndex = folderIndex;
  1439. ISzAlloc_Free(allocMain, *tempBuf);
  1440. *tempBuf = NULL;
  1441. if (res == SZ_OK)
  1442. {
  1443. *outBufferSize = unpackSize;
  1444. if (unpackSize != 0)
  1445. {
  1446. *tempBuf = (Byte *)ISzAlloc_Alloc(allocMain, unpackSize);
  1447. if (*tempBuf == NULL)
  1448. res = SZ_ERROR_MEM;
  1449. }
  1450. if (res == SZ_OK)
  1451. {
  1452. res = SzAr_DecodeFolder(&p->db, folderIndex,
  1453. inStream, p->dataPos, *tempBuf, unpackSize, allocTemp);
  1454. }
  1455. }
  1456. }
  1457. if (res == SZ_OK)
  1458. {
  1459. UInt64 unpackPos = p->UnpackPositions[fileIndex];
  1460. *offset = (size_t)(unpackPos - p->UnpackPositions[p->FolderToFile[folderIndex]]);
  1461. *outSizeProcessed = (size_t)(p->UnpackPositions[(size_t)fileIndex + 1] - unpackPos);
  1462. if (*offset + *outSizeProcessed > *outBufferSize)
  1463. return SZ_ERROR_FAIL;
  1464. if (SzBitWithVals_Check(&p->CRCs, fileIndex))
  1465. if (CrcCalc(*tempBuf + *offset, *outSizeProcessed) != p->CRCs.Vals[fileIndex])
  1466. res = SZ_ERROR_CRC;
  1467. }
  1468. return res;
  1469. }
  1470. size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest)
  1471. {
  1472. size_t offs = p->FileNameOffsets[fileIndex];
  1473. size_t len = p->FileNameOffsets[fileIndex + 1] - offs;
  1474. if (dest != 0)
  1475. {
  1476. size_t i;
  1477. const Byte *src = p->FileNames + offs * 2;
  1478. for (i = 0; i < len; i++)
  1479. dest[i] = GetUi16(src + i * 2);
  1480. }
  1481. return len;
  1482. }
  1483. /*
  1484. size_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex)
  1485. {
  1486. size_t len;
  1487. if (!p->FileNameOffsets)
  1488. return 1;
  1489. len = 0;
  1490. for (;;)
  1491. {
  1492. UInt32 parent = (UInt32)(Int32)-1;
  1493. len += p->FileNameOffsets[fileIndex + 1] - p->FileNameOffsets[fileIndex];
  1494. if SzBitWithVals_Check(&p->Parents, fileIndex)
  1495. parent = p->Parents.Vals[fileIndex];
  1496. if (parent == (UInt32)(Int32)-1)
  1497. return len;
  1498. fileIndex = parent;
  1499. }
  1500. }
  1501. UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16 *dest)
  1502. {
  1503. BoolInt needSlash;
  1504. if (!p->FileNameOffsets)
  1505. {
  1506. *(--dest) = 0;
  1507. return dest;
  1508. }
  1509. needSlash = False;
  1510. for (;;)
  1511. {
  1512. UInt32 parent = (UInt32)(Int32)-1;
  1513. size_t curLen = p->FileNameOffsets[fileIndex + 1] - p->FileNameOffsets[fileIndex];
  1514. SzArEx_GetFileNameUtf16(p, fileIndex, dest - curLen);
  1515. if (needSlash)
  1516. *(dest - 1) = '/';
  1517. needSlash = True;
  1518. dest -= curLen;
  1519. if SzBitWithVals_Check(&p->Parents, fileIndex)
  1520. parent = p->Parents.Vals[fileIndex];
  1521. if (parent == (UInt32)(Int32)-1)
  1522. return dest;
  1523. fileIndex = parent;
  1524. }
  1525. }
  1526. */