7zArcIn.c 45 KB

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