archive_read_support_format_7zip.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883
  1. /*-
  2. * Copyright (c) 2011 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_BZLIB_H
  34. #include <cm_bzlib.h>
  35. #endif
  36. #ifdef HAVE_LZMA_H
  37. #include <cm_lzma.h>
  38. #endif
  39. #ifdef HAVE_ZLIB_H
  40. #include <cm_zlib.h>
  41. #endif
  42. #include "archive.h"
  43. #include "archive_entry.h"
  44. #include "archive_entry_locale.h"
  45. #include "archive_ppmd7_private.h"
  46. #include "archive_private.h"
  47. #include "archive_read_private.h"
  48. #include "archive_endian.h"
  49. #ifndef HAVE_ZLIB_H
  50. #include "archive_crc32.h"
  51. #endif
  52. #define _7ZIP_SIGNATURE "7z\xBC\xAF\x27\x1C"
  53. #define SFX_MIN_ADDR 0x27000
  54. #define SFX_MAX_ADDR 0x60000
  55. /*
  56. * Codec ID
  57. */
  58. #define _7Z_COPY 0
  59. #define _7Z_LZMA 0x030101
  60. #define _7Z_LZMA2 0x21
  61. #define _7Z_DEFLATE 0x040108
  62. #define _7Z_BZ2 0x040202
  63. #define _7Z_PPMD 0x030401
  64. #define _7Z_DELTA 0x03
  65. #define _7Z_CRYPTO_MAIN_ZIP 0x06F10101 /* Main Zip crypto algo */
  66. #define _7Z_CRYPTO_RAR_29 0x06F10303 /* Rar29 AES-128 + (modified SHA-1) */
  67. #define _7Z_CRYPTO_AES_256_SHA_256 0x06F10701 /* AES-256 + SHA-256 */
  68. #define _7Z_X86 0x03030103
  69. #define _7Z_X86_BCJ2 0x0303011B
  70. #define _7Z_POWERPC 0x03030205
  71. #define _7Z_IA64 0x03030401
  72. #define _7Z_ARM 0x03030501
  73. #define _7Z_ARMTHUMB 0x03030701
  74. #define _7Z_SPARC 0x03030805
  75. /*
  76. * 7-Zip header property IDs.
  77. */
  78. #define kEnd 0x00
  79. #define kHeader 0x01
  80. #define kArchiveProperties 0x02
  81. #define kAdditionalStreamsInfo 0x03
  82. #define kMainStreamsInfo 0x04
  83. #define kFilesInfo 0x05
  84. #define kPackInfo 0x06
  85. #define kUnPackInfo 0x07
  86. #define kSubStreamsInfo 0x08
  87. #define kSize 0x09
  88. #define kCRC 0x0A
  89. #define kFolder 0x0B
  90. #define kCodersUnPackSize 0x0C
  91. #define kNumUnPackStream 0x0D
  92. #define kEmptyStream 0x0E
  93. #define kEmptyFile 0x0F
  94. #define kAnti 0x10
  95. #define kName 0x11
  96. #define kCTime 0x12
  97. #define kATime 0x13
  98. #define kMTime 0x14
  99. #define kAttributes 0x15
  100. #define kEncodedHeader 0x17
  101. #define kDummy 0x19
  102. struct _7z_digests {
  103. unsigned char *defineds;
  104. uint32_t *digests;
  105. };
  106. struct _7z_folder {
  107. uint64_t numCoders;
  108. struct _7z_coder {
  109. unsigned long codec;
  110. uint64_t numInStreams;
  111. uint64_t numOutStreams;
  112. uint64_t propertiesSize;
  113. unsigned char *properties;
  114. } *coders;
  115. uint64_t numBindPairs;
  116. struct {
  117. uint64_t inIndex;
  118. uint64_t outIndex;
  119. } *bindPairs;
  120. uint64_t numPackedStreams;
  121. uint64_t *packedStreams;
  122. uint64_t numInStreams;
  123. uint64_t numOutStreams;
  124. uint64_t *unPackSize;
  125. unsigned char digest_defined;
  126. uint32_t digest;
  127. uint64_t numUnpackStreams;
  128. uint32_t packIndex;
  129. /* Unoperated bytes. */
  130. uint64_t skipped_bytes;
  131. };
  132. struct _7z_coders_info {
  133. uint64_t numFolders;
  134. struct _7z_folder *folders;
  135. uint64_t dataStreamIndex;
  136. };
  137. struct _7z_pack_info {
  138. uint64_t pos;
  139. uint64_t numPackStreams;
  140. uint64_t *sizes;
  141. struct _7z_digests digest;
  142. /* Calculated from pos and numPackStreams. */
  143. uint64_t *positions;
  144. };
  145. struct _7z_substream_info {
  146. size_t unpack_streams;
  147. uint64_t *unpackSizes;
  148. unsigned char *digestsDefined;
  149. uint32_t *digests;
  150. };
  151. struct _7z_stream_info {
  152. struct _7z_pack_info pi;
  153. struct _7z_coders_info ci;
  154. struct _7z_substream_info ss;
  155. };
  156. struct _7z_header_info {
  157. uint64_t dataIndex;
  158. unsigned char *emptyStreamBools;
  159. unsigned char *emptyFileBools;
  160. unsigned char *antiBools;
  161. unsigned char *attrBools;
  162. };
  163. struct _7zip_entry {
  164. size_t name_len;
  165. unsigned char *utf16name;
  166. #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
  167. const wchar_t *wname;
  168. #endif
  169. uint32_t folderIndex;
  170. uint32_t ssIndex;
  171. unsigned flg;
  172. #define MTIME_IS_SET (1<<0)
  173. #define ATIME_IS_SET (1<<1)
  174. #define CTIME_IS_SET (1<<2)
  175. #define CRC32_IS_SET (1<<3)
  176. #define HAS_STREAM (1<<4)
  177. time_t mtime;
  178. time_t atime;
  179. time_t ctime;
  180. long mtime_ns;
  181. long atime_ns;
  182. long ctime_ns;
  183. uint32_t mode;
  184. uint32_t attr;
  185. };
  186. struct _7zip {
  187. /* Structural information about the archive. */
  188. struct _7z_stream_info si;
  189. int header_is_being_read;
  190. int header_is_encoded;
  191. uint64_t header_bytes_remaining;
  192. unsigned long header_crc32;
  193. /* Header offset to check that reading pointes of the file contens
  194. * will not exceed the header. */
  195. uint64_t header_offset;
  196. /* Base offset of the archive file for a seek in case reading SFX. */
  197. uint64_t seek_base;
  198. /* List of entries */
  199. size_t entries_remaining;
  200. uint64_t numFiles;
  201. struct _7zip_entry *entries;
  202. struct _7zip_entry *entry;
  203. unsigned char *entry_names;
  204. /* entry_bytes_remaining is the number of bytes we expect. */
  205. int64_t entry_offset;
  206. uint64_t entry_bytes_remaining;
  207. /* Running CRC32 of the decompressed data */
  208. unsigned long entry_crc32;
  209. /* Flags to mark progress of decompression. */
  210. char end_of_entry;
  211. /* Uncompressed buffer control. */
  212. #define UBUFF_SIZE (64 * 1024)
  213. unsigned char *uncompressed_buffer;
  214. unsigned char *uncompressed_buffer_pointer;
  215. size_t uncompressed_buffer_size;
  216. size_t uncompressed_buffer_bytes_remaining;
  217. /* Offset of the compressed data. */
  218. int64_t stream_offset;
  219. /*
  220. * Decompressing control data.
  221. */
  222. unsigned folder_index;
  223. uint64_t folder_outbytes_remaining;
  224. unsigned pack_stream_index;
  225. unsigned pack_stream_remaining;
  226. uint64_t pack_stream_inbytes_remaining;
  227. size_t pack_stream_bytes_unconsumed;
  228. /* The codec information of a folder. */
  229. unsigned long codec;
  230. unsigned long codec2;
  231. /*
  232. * Decompressor controllers.
  233. */
  234. /* Decording LZMA1 and LZMA2 data. */
  235. #ifdef HAVE_LZMA_H
  236. lzma_stream lzstream;
  237. int lzstream_valid;
  238. #endif
  239. /* Decording bzip2 data. */
  240. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  241. bz_stream bzstream;
  242. int bzstream_valid;
  243. #endif
  244. /* Decording deflate data. */
  245. #ifdef HAVE_ZLIB_H
  246. z_stream stream;
  247. int stream_valid;
  248. #endif
  249. /* Decording PPMd data. */
  250. int ppmd7_stat;
  251. CPpmd7 ppmd7_context;
  252. CPpmd7z_RangeDec range_dec;
  253. IByteIn bytein;
  254. struct {
  255. const unsigned char *next_in;
  256. int64_t avail_in;
  257. int64_t total_in;
  258. unsigned char *next_out;
  259. int64_t avail_out;
  260. int64_t total_out;
  261. int overconsumed;
  262. } ppstream;
  263. int ppmd7_valid;
  264. /* Decoding BCJ and BCJ2 data. */
  265. uint32_t bcj_state;
  266. size_t odd_bcj_size;
  267. unsigned char odd_bcj[4];
  268. /* Decoding BCJ data. */
  269. size_t bcj_prevPosT;
  270. uint32_t bcj_prevMask;
  271. uint32_t bcj_ip;
  272. /* Decoding BCJ2 data. */
  273. size_t main_stream_bytes_remaining;
  274. unsigned char *sub_stream_buff[3];
  275. size_t sub_stream_size[3];
  276. size_t sub_stream_bytes_remaining[3];
  277. unsigned char *tmp_stream_buff;
  278. size_t tmp_stream_buff_size;
  279. size_t tmp_stream_bytes_avail;
  280. size_t tmp_stream_bytes_remaining;
  281. #ifdef _LZMA_PROB32
  282. #define CProb uint32_t
  283. #else
  284. #define CProb uint16_t
  285. #endif
  286. CProb bcj2_p[256 + 2];
  287. uint8_t bcj2_prevByte;
  288. uint32_t bcj2_range;
  289. uint32_t bcj2_code;
  290. uint64_t bcj2_outPos;
  291. /* Filename character-set conversion data. */
  292. struct archive_string_conv *sconv;
  293. char format_name[64];
  294. /* Custom value that is non-zero if this archive contains encrypted entries. */
  295. int has_encrypted_entries;
  296. };
  297. /* Maximum entry size. This limitation prevents reading intentional
  298. * corrupted 7-zip files on assuming there are not so many entries in
  299. * the files. */
  300. #define UMAX_ENTRY ARCHIVE_LITERAL_ULL(100000000)
  301. static int archive_read_format_7zip_has_encrypted_entries(struct archive_read *);
  302. static int archive_read_support_format_7zip_capabilities(struct archive_read *a);
  303. static int archive_read_format_7zip_bid(struct archive_read *, int);
  304. static int archive_read_format_7zip_cleanup(struct archive_read *);
  305. static int archive_read_format_7zip_read_data(struct archive_read *,
  306. const void **, size_t *, int64_t *);
  307. static int archive_read_format_7zip_read_data_skip(struct archive_read *);
  308. static int archive_read_format_7zip_read_header(struct archive_read *,
  309. struct archive_entry *);
  310. static int check_7zip_header_in_sfx(const char *);
  311. static unsigned long decode_codec_id(const unsigned char *, size_t);
  312. static int decode_encoded_header_info(struct archive_read *,
  313. struct _7z_stream_info *);
  314. static int decompress(struct archive_read *, struct _7zip *,
  315. void *, size_t *, const void *, size_t *);
  316. static ssize_t extract_pack_stream(struct archive_read *, size_t);
  317. static void fileTimeToUtc(uint64_t, time_t *, long *);
  318. static uint64_t folder_uncompressed_size(struct _7z_folder *);
  319. static void free_CodersInfo(struct _7z_coders_info *);
  320. static void free_Digest(struct _7z_digests *);
  321. static void free_Folder(struct _7z_folder *);
  322. static void free_Header(struct _7z_header_info *);
  323. static void free_PackInfo(struct _7z_pack_info *);
  324. static void free_StreamsInfo(struct _7z_stream_info *);
  325. static void free_SubStreamsInfo(struct _7z_substream_info *);
  326. static int free_decompression(struct archive_read *, struct _7zip *);
  327. static ssize_t get_uncompressed_data(struct archive_read *, const void **,
  328. size_t, size_t);
  329. static const unsigned char * header_bytes(struct archive_read *, size_t);
  330. static int init_decompression(struct archive_read *, struct _7zip *,
  331. const struct _7z_coder *, const struct _7z_coder *);
  332. static int parse_7zip_uint64(struct archive_read *, uint64_t *);
  333. static int read_Bools(struct archive_read *, unsigned char *, size_t);
  334. static int read_CodersInfo(struct archive_read *,
  335. struct _7z_coders_info *);
  336. static int read_Digests(struct archive_read *, struct _7z_digests *,
  337. size_t);
  338. static int read_Folder(struct archive_read *, struct _7z_folder *);
  339. static int read_Header(struct archive_read *, struct _7z_header_info *,
  340. int);
  341. static int read_PackInfo(struct archive_read *, struct _7z_pack_info *);
  342. static int read_StreamsInfo(struct archive_read *,
  343. struct _7z_stream_info *);
  344. static int read_SubStreamsInfo(struct archive_read *,
  345. struct _7z_substream_info *, struct _7z_folder *, size_t);
  346. static int read_Times(struct archive_read *, struct _7z_header_info *,
  347. int);
  348. static void read_consume(struct archive_read *);
  349. static ssize_t read_stream(struct archive_read *, const void **, size_t,
  350. size_t);
  351. static int seek_pack(struct archive_read *);
  352. static int64_t skip_stream(struct archive_read *, size_t);
  353. static int skip_sfx(struct archive_read *, ssize_t);
  354. static int slurp_central_directory(struct archive_read *, struct _7zip *,
  355. struct _7z_header_info *);
  356. static int setup_decode_folder(struct archive_read *, struct _7z_folder *,
  357. int);
  358. static void x86_Init(struct _7zip *);
  359. static size_t x86_Convert(struct _7zip *, uint8_t *, size_t);
  360. static ssize_t Bcj2_Decode(struct _7zip *, uint8_t *, size_t);
  361. int
  362. archive_read_support_format_7zip(struct archive *_a)
  363. {
  364. struct archive_read *a = (struct archive_read *)_a;
  365. struct _7zip *zip;
  366. int r;
  367. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  368. ARCHIVE_STATE_NEW, "archive_read_support_format_7zip");
  369. zip = calloc(1, sizeof(*zip));
  370. if (zip == NULL) {
  371. archive_set_error(&a->archive, ENOMEM,
  372. "Can't allocate 7zip data");
  373. return (ARCHIVE_FATAL);
  374. }
  375. /*
  376. * Until enough data has been read, we cannot tell about
  377. * any encrypted entries yet.
  378. */
  379. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  380. r = __archive_read_register_format(a,
  381. zip,
  382. "7zip",
  383. archive_read_format_7zip_bid,
  384. NULL,
  385. archive_read_format_7zip_read_header,
  386. archive_read_format_7zip_read_data,
  387. archive_read_format_7zip_read_data_skip,
  388. NULL,
  389. archive_read_format_7zip_cleanup,
  390. archive_read_support_format_7zip_capabilities,
  391. archive_read_format_7zip_has_encrypted_entries);
  392. if (r != ARCHIVE_OK)
  393. free(zip);
  394. return (ARCHIVE_OK);
  395. }
  396. static int
  397. archive_read_support_format_7zip_capabilities(struct archive_read * a)
  398. {
  399. (void)a; /* UNUSED */
  400. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
  401. ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  402. }
  403. static int
  404. archive_read_format_7zip_has_encrypted_entries(struct archive_read *_a)
  405. {
  406. if (_a && _a->format) {
  407. struct _7zip * zip = (struct _7zip *)_a->format->data;
  408. if (zip) {
  409. return zip->has_encrypted_entries;
  410. }
  411. }
  412. return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  413. }
  414. static int
  415. archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
  416. {
  417. const char *p;
  418. /* If someone has already bid more than 32, then avoid
  419. trashing the look-ahead buffers with a seek. */
  420. if (best_bid > 32)
  421. return (-1);
  422. if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
  423. return (0);
  424. /* If first six bytes are the 7-Zip signature,
  425. * return the bid right now. */
  426. if (memcmp(p, _7ZIP_SIGNATURE, 6) == 0)
  427. return (48);
  428. /*
  429. * It may a 7-Zip SFX archive file. If first two bytes are
  430. * 'M' and 'Z' available on Windows or first four bytes are
  431. * "\x7F\x45LF" available on posix like system, seek the 7-Zip
  432. * signature. Although we will perform a seek when reading
  433. * a header, what we do not use __archive_read_seek() here is
  434. * due to a bidding performance.
  435. */
  436. if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
  437. ssize_t offset = SFX_MIN_ADDR;
  438. ssize_t window = 4096;
  439. ssize_t bytes_avail;
  440. while (offset + window <= (SFX_MAX_ADDR)) {
  441. const char *buff = __archive_read_ahead(a,
  442. offset + window, &bytes_avail);
  443. if (buff == NULL) {
  444. /* Remaining bytes are less than window. */
  445. window >>= 1;
  446. if (window < 0x40)
  447. return (0);
  448. continue;
  449. }
  450. p = buff + offset;
  451. while (p + 32 < buff + bytes_avail) {
  452. int step = check_7zip_header_in_sfx(p);
  453. if (step == 0)
  454. return (48);
  455. p += step;
  456. }
  457. offset = p - buff;
  458. }
  459. }
  460. return (0);
  461. }
  462. static int
  463. check_7zip_header_in_sfx(const char *p)
  464. {
  465. switch ((unsigned char)p[5]) {
  466. case 0x1C:
  467. if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0)
  468. return (6);
  469. /*
  470. * Test the CRC because its extraction code has 7-Zip
  471. * Magic Code, so we should do this in order not to
  472. * make a mis-detection.
  473. */
  474. if (crc32(0, (const unsigned char *)p + 12, 20)
  475. != archive_le32dec(p + 8))
  476. return (6);
  477. /* Hit the header! */
  478. return (0);
  479. case 0x37: return (5);
  480. case 0x7A: return (4);
  481. case 0xBC: return (3);
  482. case 0xAF: return (2);
  483. case 0x27: return (1);
  484. default: return (6);
  485. }
  486. }
  487. static int
  488. skip_sfx(struct archive_read *a, ssize_t bytes_avail)
  489. {
  490. const void *h;
  491. const char *p, *q;
  492. size_t skip, offset;
  493. ssize_t bytes, window;
  494. /*
  495. * If bytes_avail > SFX_MIN_ADDR we do not have to call
  496. * __archive_read_seek() at this time since we have
  497. * alredy had enough data.
  498. */
  499. if (bytes_avail > SFX_MIN_ADDR)
  500. __archive_read_consume(a, SFX_MIN_ADDR);
  501. else if (__archive_read_seek(a, SFX_MIN_ADDR, SEEK_SET) < 0)
  502. return (ARCHIVE_FATAL);
  503. offset = 0;
  504. window = 1;
  505. while (offset + window <= SFX_MAX_ADDR - SFX_MIN_ADDR) {
  506. h = __archive_read_ahead(a, window, &bytes);
  507. if (h == NULL) {
  508. /* Remaining bytes are less than window. */
  509. window >>= 1;
  510. if (window < 0x40)
  511. goto fatal;
  512. continue;
  513. }
  514. if (bytes < 6) {
  515. /* This case might happen when window == 1. */
  516. window = 4096;
  517. continue;
  518. }
  519. p = (const char *)h;
  520. q = p + bytes;
  521. /*
  522. * Scan ahead until we find something that looks
  523. * like the 7-Zip header.
  524. */
  525. while (p + 32 < q) {
  526. int step = check_7zip_header_in_sfx(p);
  527. if (step == 0) {
  528. struct _7zip *zip =
  529. (struct _7zip *)a->format->data;
  530. skip = p - (const char *)h;
  531. __archive_read_consume(a, skip);
  532. zip->seek_base = SFX_MIN_ADDR + offset + skip;
  533. return (ARCHIVE_OK);
  534. }
  535. p += step;
  536. }
  537. skip = p - (const char *)h;
  538. __archive_read_consume(a, skip);
  539. offset += skip;
  540. if (window == 1)
  541. window = 4096;
  542. }
  543. fatal:
  544. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  545. "Couldn't find out 7-Zip header");
  546. return (ARCHIVE_FATAL);
  547. }
  548. static int
  549. archive_read_format_7zip_read_header(struct archive_read *a,
  550. struct archive_entry *entry)
  551. {
  552. struct _7zip *zip = (struct _7zip *)a->format->data;
  553. struct _7zip_entry *zip_entry;
  554. int r, ret = ARCHIVE_OK;
  555. struct _7z_folder *folder = 0;
  556. uint64_t fidx = 0;
  557. /*
  558. * It should be sufficient to call archive_read_next_header() for
  559. * a reader to determine if an entry is encrypted or not. If the
  560. * encryption of an entry is only detectable when calling
  561. * archive_read_data(), so be it. We'll do the same check there
  562. * as well.
  563. */
  564. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  565. zip->has_encrypted_entries = 0;
  566. }
  567. a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
  568. if (a->archive.archive_format_name == NULL)
  569. a->archive.archive_format_name = "7-Zip";
  570. if (zip->entries == NULL) {
  571. struct _7z_header_info header;
  572. memset(&header, 0, sizeof(header));
  573. r = slurp_central_directory(a, zip, &header);
  574. free_Header(&header);
  575. if (r != ARCHIVE_OK)
  576. return (r);
  577. zip->entries_remaining = (size_t)zip->numFiles;
  578. zip->entry = zip->entries;
  579. } else {
  580. ++zip->entry;
  581. }
  582. zip_entry = zip->entry;
  583. if (zip->entries_remaining <= 0 || zip_entry == NULL)
  584. return ARCHIVE_EOF;
  585. --zip->entries_remaining;
  586. zip->entry_offset = 0;
  587. zip->end_of_entry = 0;
  588. zip->entry_crc32 = crc32(0, NULL, 0);
  589. /* Setup a string conversion for a filename. */
  590. if (zip->sconv == NULL) {
  591. zip->sconv = archive_string_conversion_from_charset(
  592. &a->archive, "UTF-16LE", 1);
  593. if (zip->sconv == NULL)
  594. return (ARCHIVE_FATAL);
  595. }
  596. /* Figure out if the entry is encrypted by looking at the folder
  597. that is associated to the current 7zip entry. If the folder
  598. has a coder with a _7Z_CRYPTO codec then the folder is encrypted.
  599. Hence the entry must also be encrypted. */
  600. if (zip_entry && zip_entry->folderIndex < zip->si.ci.numFolders) {
  601. folder = &(zip->si.ci.folders[zip_entry->folderIndex]);
  602. for (fidx=0; folder && fidx<folder->numCoders; fidx++) {
  603. switch(folder->coders[fidx].codec) {
  604. case _7Z_CRYPTO_MAIN_ZIP:
  605. case _7Z_CRYPTO_RAR_29:
  606. case _7Z_CRYPTO_AES_256_SHA_256: {
  607. archive_entry_set_is_data_encrypted(entry, 1);
  608. zip->has_encrypted_entries = 1;
  609. break;
  610. }
  611. }
  612. }
  613. }
  614. /* Now that we've checked for encryption, if there were still no
  615. * encrypted entries found we can say for sure that there are none.
  616. */
  617. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  618. zip->has_encrypted_entries = 0;
  619. }
  620. if (archive_entry_copy_pathname_l(entry,
  621. (const char *)zip_entry->utf16name,
  622. zip_entry->name_len, zip->sconv) != 0) {
  623. if (errno == ENOMEM) {
  624. archive_set_error(&a->archive, ENOMEM,
  625. "Can't allocate memory for Pathname");
  626. return (ARCHIVE_FATAL);
  627. }
  628. archive_set_error(&a->archive,
  629. ARCHIVE_ERRNO_FILE_FORMAT,
  630. "Pathname cannot be converted "
  631. "from %s to current locale.",
  632. archive_string_conversion_charset_name(zip->sconv));
  633. ret = ARCHIVE_WARN;
  634. }
  635. /* Populate some additional entry fields: */
  636. archive_entry_set_mode(entry, zip_entry->mode);
  637. if (zip_entry->flg & MTIME_IS_SET)
  638. archive_entry_set_mtime(entry, zip_entry->mtime,
  639. zip_entry->mtime_ns);
  640. if (zip_entry->flg & CTIME_IS_SET)
  641. archive_entry_set_ctime(entry, zip_entry->ctime,
  642. zip_entry->ctime_ns);
  643. if (zip_entry->flg & ATIME_IS_SET)
  644. archive_entry_set_atime(entry, zip_entry->atime,
  645. zip_entry->atime_ns);
  646. if (zip_entry->ssIndex != (uint32_t)-1) {
  647. zip->entry_bytes_remaining =
  648. zip->si.ss.unpackSizes[zip_entry->ssIndex];
  649. archive_entry_set_size(entry, zip->entry_bytes_remaining);
  650. } else {
  651. zip->entry_bytes_remaining = 0;
  652. archive_entry_set_size(entry, 0);
  653. }
  654. /* If there's no body, force read_data() to return EOF immediately. */
  655. if (zip->entry_bytes_remaining < 1)
  656. zip->end_of_entry = 1;
  657. if ((zip_entry->mode & AE_IFMT) == AE_IFLNK) {
  658. unsigned char *symname = NULL;
  659. size_t symsize = 0;
  660. /*
  661. * Symbolic-name is recorded as its contents. We have to
  662. * read the contents at this time.
  663. */
  664. while (zip->entry_bytes_remaining > 0) {
  665. const void *buff;
  666. unsigned char *mem;
  667. size_t size;
  668. int64_t offset;
  669. r = archive_read_format_7zip_read_data(a, &buff,
  670. &size, &offset);
  671. if (r < ARCHIVE_WARN) {
  672. free(symname);
  673. return (r);
  674. }
  675. mem = realloc(symname, symsize + size + 1);
  676. if (mem == NULL) {
  677. free(symname);
  678. archive_set_error(&a->archive, ENOMEM,
  679. "Can't allocate memory for Symname");
  680. return (ARCHIVE_FATAL);
  681. }
  682. symname = mem;
  683. memcpy(symname+symsize, buff, size);
  684. symsize += size;
  685. }
  686. if (symsize == 0) {
  687. /* If there is no synname, handle it as a regular
  688. * file. */
  689. zip_entry->mode &= ~AE_IFMT;
  690. zip_entry->mode |= AE_IFREG;
  691. archive_entry_set_mode(entry, zip_entry->mode);
  692. } else {
  693. symname[symsize] = '\0';
  694. archive_entry_copy_symlink(entry,
  695. (const char *)symname);
  696. }
  697. free(symname);
  698. archive_entry_set_size(entry, 0);
  699. }
  700. /* Set up a more descriptive format name. */
  701. sprintf(zip->format_name, "7-Zip");
  702. a->archive.archive_format_name = zip->format_name;
  703. return (ret);
  704. }
  705. static int
  706. archive_read_format_7zip_read_data(struct archive_read *a,
  707. const void **buff, size_t *size, int64_t *offset)
  708. {
  709. struct _7zip *zip;
  710. ssize_t bytes;
  711. int ret = ARCHIVE_OK;
  712. zip = (struct _7zip *)(a->format->data);
  713. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  714. zip->has_encrypted_entries = 0;
  715. }
  716. if (zip->pack_stream_bytes_unconsumed)
  717. read_consume(a);
  718. *offset = zip->entry_offset;
  719. *size = 0;
  720. *buff = NULL;
  721. /*
  722. * If we hit end-of-entry last time, clean up and return
  723. * ARCHIVE_EOF this time.
  724. */
  725. if (zip->end_of_entry)
  726. return (ARCHIVE_EOF);
  727. bytes = read_stream(a, buff,
  728. (size_t)zip->entry_bytes_remaining, 0);
  729. if (bytes < 0)
  730. return ((int)bytes);
  731. if (bytes == 0) {
  732. archive_set_error(&a->archive,
  733. ARCHIVE_ERRNO_FILE_FORMAT,
  734. "Truncated 7-Zip file body");
  735. return (ARCHIVE_FATAL);
  736. }
  737. zip->entry_bytes_remaining -= bytes;
  738. if (zip->entry_bytes_remaining == 0)
  739. zip->end_of_entry = 1;
  740. /* Update checksum */
  741. if ((zip->entry->flg & CRC32_IS_SET) && bytes)
  742. zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
  743. (unsigned)bytes);
  744. /* If we hit the end, swallow any end-of-data marker. */
  745. if (zip->end_of_entry) {
  746. /* Check computed CRC against file contents. */
  747. if ((zip->entry->flg & CRC32_IS_SET) &&
  748. zip->si.ss.digests[zip->entry->ssIndex] !=
  749. zip->entry_crc32) {
  750. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  751. "7-Zip bad CRC: 0x%lx should be 0x%lx",
  752. (unsigned long)zip->entry_crc32,
  753. (unsigned long)zip->si.ss.digests[
  754. zip->entry->ssIndex]);
  755. ret = ARCHIVE_WARN;
  756. }
  757. }
  758. *size = bytes;
  759. *offset = zip->entry_offset;
  760. zip->entry_offset += bytes;
  761. return (ret);
  762. }
  763. static int
  764. archive_read_format_7zip_read_data_skip(struct archive_read *a)
  765. {
  766. struct _7zip *zip;
  767. int64_t bytes_skipped;
  768. zip = (struct _7zip *)(a->format->data);
  769. if (zip->pack_stream_bytes_unconsumed)
  770. read_consume(a);
  771. /* If we've already read to end of data, we're done. */
  772. if (zip->end_of_entry)
  773. return (ARCHIVE_OK);
  774. /*
  775. * If the length is at the beginning, we can skip the
  776. * compressed data much more quickly.
  777. */
  778. bytes_skipped = skip_stream(a, (size_t)zip->entry_bytes_remaining);
  779. if (bytes_skipped < 0)
  780. return (ARCHIVE_FATAL);
  781. zip->entry_bytes_remaining = 0;
  782. /* This entry is finished and done. */
  783. zip->end_of_entry = 1;
  784. return (ARCHIVE_OK);
  785. }
  786. static int
  787. archive_read_format_7zip_cleanup(struct archive_read *a)
  788. {
  789. struct _7zip *zip;
  790. zip = (struct _7zip *)(a->format->data);
  791. free_StreamsInfo(&(zip->si));
  792. free(zip->entries);
  793. free(zip->entry_names);
  794. free_decompression(a, zip);
  795. free(zip->uncompressed_buffer);
  796. free(zip->sub_stream_buff[0]);
  797. free(zip->sub_stream_buff[1]);
  798. free(zip->sub_stream_buff[2]);
  799. free(zip->tmp_stream_buff);
  800. free(zip);
  801. (a->format->data) = NULL;
  802. return (ARCHIVE_OK);
  803. }
  804. static void
  805. read_consume(struct archive_read *a)
  806. {
  807. struct _7zip *zip = (struct _7zip *)a->format->data;
  808. if (zip->pack_stream_bytes_unconsumed) {
  809. __archive_read_consume(a, zip->pack_stream_bytes_unconsumed);
  810. zip->stream_offset += zip->pack_stream_bytes_unconsumed;
  811. zip->pack_stream_bytes_unconsumed = 0;
  812. }
  813. }
  814. #ifdef HAVE_LZMA_H
  815. /*
  816. * Set an error code and choose an error message for liblzma.
  817. */
  818. static void
  819. set_error(struct archive_read *a, int ret)
  820. {
  821. switch (ret) {
  822. case LZMA_STREAM_END: /* Found end of stream. */
  823. case LZMA_OK: /* Decompressor made some progress. */
  824. break;
  825. case LZMA_MEM_ERROR:
  826. archive_set_error(&a->archive, ENOMEM,
  827. "Lzma library error: Cannot allocate memory");
  828. break;
  829. case LZMA_MEMLIMIT_ERROR:
  830. archive_set_error(&a->archive, ENOMEM,
  831. "Lzma library error: Out of memory");
  832. break;
  833. case LZMA_FORMAT_ERROR:
  834. archive_set_error(&a->archive,
  835. ARCHIVE_ERRNO_MISC,
  836. "Lzma library error: format not recognized");
  837. break;
  838. case LZMA_OPTIONS_ERROR:
  839. archive_set_error(&a->archive,
  840. ARCHIVE_ERRNO_MISC,
  841. "Lzma library error: Invalid options");
  842. break;
  843. case LZMA_DATA_ERROR:
  844. archive_set_error(&a->archive,
  845. ARCHIVE_ERRNO_MISC,
  846. "Lzma library error: Corrupted input data");
  847. break;
  848. case LZMA_BUF_ERROR:
  849. archive_set_error(&a->archive,
  850. ARCHIVE_ERRNO_MISC,
  851. "Lzma library error: No progress is possible");
  852. break;
  853. default:
  854. /* Return an error. */
  855. archive_set_error(&a->archive,
  856. ARCHIVE_ERRNO_MISC,
  857. "Lzma decompression failed: Unknown error");
  858. break;
  859. }
  860. }
  861. #endif
  862. static unsigned long
  863. decode_codec_id(const unsigned char *codecId, size_t id_size)
  864. {
  865. unsigned i;
  866. unsigned long id = 0;
  867. for (i = 0; i < id_size; i++) {
  868. id <<= 8;
  869. id += codecId[i];
  870. }
  871. return (id);
  872. }
  873. static void *
  874. ppmd_alloc(void *p, size_t size)
  875. {
  876. (void)p;
  877. return malloc(size);
  878. }
  879. static void
  880. ppmd_free(void *p, void *address)
  881. {
  882. (void)p;
  883. free(address);
  884. }
  885. static Byte
  886. ppmd_read(void *p)
  887. {
  888. struct archive_read *a = ((IByteIn*)p)->a;
  889. struct _7zip *zip = (struct _7zip *)(a->format->data);
  890. Byte b;
  891. if (zip->ppstream.avail_in == 0) {
  892. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  893. "Truncated RAR file data");
  894. zip->ppstream.overconsumed = 1;
  895. return (0);
  896. }
  897. b = *zip->ppstream.next_in++;
  898. zip->ppstream.avail_in--;
  899. zip->ppstream.total_in++;
  900. return (b);
  901. }
  902. static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
  903. static int
  904. init_decompression(struct archive_read *a, struct _7zip *zip,
  905. const struct _7z_coder *coder1, const struct _7z_coder *coder2)
  906. {
  907. int r;
  908. zip->codec = coder1->codec;
  909. zip->codec2 = -1;
  910. switch (zip->codec) {
  911. case _7Z_COPY:
  912. case _7Z_BZ2:
  913. case _7Z_DEFLATE:
  914. case _7Z_PPMD:
  915. if (coder2 != NULL) {
  916. if (coder2->codec != _7Z_X86 &&
  917. coder2->codec != _7Z_X86_BCJ2) {
  918. archive_set_error(&a->archive,
  919. ARCHIVE_ERRNO_MISC,
  920. "Unsupported filter %lx for %lx",
  921. coder2->codec, coder1->codec);
  922. return (ARCHIVE_FAILED);
  923. }
  924. zip->codec2 = coder2->codec;
  925. zip->bcj_state = 0;
  926. if (coder2->codec == _7Z_X86)
  927. x86_Init(zip);
  928. }
  929. break;
  930. default:
  931. break;
  932. }
  933. switch (zip->codec) {
  934. case _7Z_COPY:
  935. break;
  936. case _7Z_LZMA: case _7Z_LZMA2:
  937. #ifdef HAVE_LZMA_H
  938. #if LZMA_VERSION_MAJOR >= 5
  939. /* Effectively disable the limiter. */
  940. #define LZMA_MEMLIMIT UINT64_MAX
  941. #else
  942. /* NOTE: This needs to check memory size which running system has. */
  943. #define LZMA_MEMLIMIT (1U << 30)
  944. #endif
  945. {
  946. lzma_options_delta delta_opt;
  947. lzma_filter filters[LZMA_FILTERS_MAX];
  948. #if LZMA_VERSION < 50010000
  949. lzma_filter *ff;
  950. #endif
  951. int fi = 0;
  952. if (zip->lzstream_valid) {
  953. lzma_end(&(zip->lzstream));
  954. zip->lzstream_valid = 0;
  955. }
  956. /*
  957. * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
  958. * data made by 7-Zip because 7-Zip does not add End-Of-
  959. * Payload Marker(EOPM) at the end of LZMA compressed data,
  960. * and so liblzma cannot know the end of the compressed data
  961. * without EOPM. So consequently liblzma will not return last
  962. * three or four bytes of uncompressed data because
  963. * LZMA_FILTER_X86 filter does not handle input data if its
  964. * data size is less than five bytes. If liblzma detect EOPM
  965. * or know the uncompressed data size, liblzma will flush out
  966. * the remaining that three or four bytes of uncompressed
  967. * data. That is why we have to use our converting program
  968. * for BCJ+LZMA. If we were able to tell the uncompressed
  969. * size to liblzma when using lzma_raw_decoder() liblzma
  970. * could correctly deal with BCJ+LZMA. But unfortunately
  971. * there is no way to do that.
  972. * Discussion about this can be found at XZ Utils forum.
  973. */
  974. if (coder2 != NULL) {
  975. zip->codec2 = coder2->codec;
  976. filters[fi].options = NULL;
  977. switch (zip->codec2) {
  978. case _7Z_X86:
  979. if (zip->codec == _7Z_LZMA2) {
  980. filters[fi].id = LZMA_FILTER_X86;
  981. fi++;
  982. } else
  983. /* Use our filter. */
  984. x86_Init(zip);
  985. break;
  986. case _7Z_X86_BCJ2:
  987. /* Use our filter. */
  988. zip->bcj_state = 0;
  989. break;
  990. case _7Z_DELTA:
  991. filters[fi].id = LZMA_FILTER_DELTA;
  992. memset(&delta_opt, 0, sizeof(delta_opt));
  993. delta_opt.type = LZMA_DELTA_TYPE_BYTE;
  994. delta_opt.dist = 1;
  995. filters[fi].options = &delta_opt;
  996. fi++;
  997. break;
  998. /* Following filters have not been tested yet. */
  999. case _7Z_POWERPC:
  1000. filters[fi].id = LZMA_FILTER_POWERPC;
  1001. fi++;
  1002. break;
  1003. case _7Z_IA64:
  1004. filters[fi].id = LZMA_FILTER_IA64;
  1005. fi++;
  1006. break;
  1007. case _7Z_ARM:
  1008. filters[fi].id = LZMA_FILTER_ARM;
  1009. fi++;
  1010. break;
  1011. case _7Z_ARMTHUMB:
  1012. filters[fi].id = LZMA_FILTER_ARMTHUMB;
  1013. fi++;
  1014. break;
  1015. case _7Z_SPARC:
  1016. filters[fi].id = LZMA_FILTER_SPARC;
  1017. fi++;
  1018. break;
  1019. default:
  1020. archive_set_error(&a->archive,
  1021. ARCHIVE_ERRNO_MISC,
  1022. "Unexpected codec ID: %lX", zip->codec2);
  1023. return (ARCHIVE_FAILED);
  1024. }
  1025. }
  1026. if (zip->codec == _7Z_LZMA2)
  1027. filters[fi].id = LZMA_FILTER_LZMA2;
  1028. else
  1029. filters[fi].id = LZMA_FILTER_LZMA1;
  1030. filters[fi].options = NULL;
  1031. #if LZMA_VERSION < 50010000
  1032. ff = &filters[fi];
  1033. #endif
  1034. r = lzma_properties_decode(&filters[fi], NULL,
  1035. coder1->properties, (size_t)coder1->propertiesSize);
  1036. if (r != LZMA_OK) {
  1037. set_error(a, r);
  1038. return (ARCHIVE_FAILED);
  1039. }
  1040. fi++;
  1041. filters[fi].id = LZMA_VLI_UNKNOWN;
  1042. filters[fi].options = NULL;
  1043. r = lzma_raw_decoder(&(zip->lzstream), filters);
  1044. #if LZMA_VERSION < 50010000
  1045. free(ff->options);
  1046. #endif
  1047. if (r != LZMA_OK) {
  1048. set_error(a, r);
  1049. return (ARCHIVE_FAILED);
  1050. }
  1051. zip->lzstream_valid = 1;
  1052. zip->lzstream.total_in = 0;
  1053. zip->lzstream.total_out = 0;
  1054. break;
  1055. }
  1056. #else
  1057. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1058. "LZMA codec is unsupported");
  1059. return (ARCHIVE_FAILED);
  1060. #endif
  1061. case _7Z_BZ2:
  1062. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1063. if (zip->bzstream_valid) {
  1064. BZ2_bzDecompressEnd(&(zip->bzstream));
  1065. zip->bzstream_valid = 0;
  1066. }
  1067. r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
  1068. if (r == BZ_MEM_ERROR)
  1069. r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
  1070. if (r != BZ_OK) {
  1071. int err = ARCHIVE_ERRNO_MISC;
  1072. const char *detail = NULL;
  1073. switch (r) {
  1074. case BZ_PARAM_ERROR:
  1075. detail = "invalid setup parameter";
  1076. break;
  1077. case BZ_MEM_ERROR:
  1078. err = ENOMEM;
  1079. detail = "out of memory";
  1080. break;
  1081. case BZ_CONFIG_ERROR:
  1082. detail = "mis-compiled library";
  1083. break;
  1084. }
  1085. archive_set_error(&a->archive, err,
  1086. "Internal error initializing decompressor: %s",
  1087. detail != NULL ? detail : "??");
  1088. zip->bzstream_valid = 0;
  1089. return (ARCHIVE_FAILED);
  1090. }
  1091. zip->bzstream_valid = 1;
  1092. zip->bzstream.total_in_lo32 = 0;
  1093. zip->bzstream.total_in_hi32 = 0;
  1094. zip->bzstream.total_out_lo32 = 0;
  1095. zip->bzstream.total_out_hi32 = 0;
  1096. break;
  1097. #else
  1098. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1099. "BZ2 codec is unsupported");
  1100. return (ARCHIVE_FAILED);
  1101. #endif
  1102. case _7Z_DEFLATE:
  1103. #ifdef HAVE_ZLIB_H
  1104. if (zip->stream_valid)
  1105. r = inflateReset(&(zip->stream));
  1106. else
  1107. r = inflateInit2(&(zip->stream),
  1108. -15 /* Don't check for zlib header */);
  1109. if (r != Z_OK) {
  1110. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1111. "Couldn't initialize zlib stream.");
  1112. return (ARCHIVE_FAILED);
  1113. }
  1114. zip->stream_valid = 1;
  1115. zip->stream.total_in = 0;
  1116. zip->stream.total_out = 0;
  1117. break;
  1118. #else
  1119. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1120. "DEFLATE codec is unsupported");
  1121. return (ARCHIVE_FAILED);
  1122. #endif
  1123. case _7Z_PPMD:
  1124. {
  1125. unsigned order;
  1126. uint32_t msize;
  1127. if (zip->ppmd7_valid) {
  1128. __archive_ppmd7_functions.Ppmd7_Free(
  1129. &zip->ppmd7_context, &g_szalloc);
  1130. zip->ppmd7_valid = 0;
  1131. }
  1132. if (coder1->propertiesSize < 5) {
  1133. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1134. "Malformed PPMd parameter");
  1135. return (ARCHIVE_FAILED);
  1136. }
  1137. order = coder1->properties[0];
  1138. msize = archive_le32dec(&(coder1->properties[1]));
  1139. if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
  1140. msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
  1141. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1142. "Malformed PPMd parameter");
  1143. return (ARCHIVE_FAILED);
  1144. }
  1145. __archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
  1146. r = __archive_ppmd7_functions.Ppmd7_Alloc(
  1147. &zip->ppmd7_context, msize, &g_szalloc);
  1148. if (r == 0) {
  1149. archive_set_error(&a->archive, ENOMEM,
  1150. "Coludn't allocate memory for PPMd");
  1151. return (ARCHIVE_FATAL);
  1152. }
  1153. __archive_ppmd7_functions.Ppmd7_Init(
  1154. &zip->ppmd7_context, order);
  1155. __archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
  1156. &zip->range_dec);
  1157. zip->ppmd7_valid = 1;
  1158. zip->ppmd7_stat = 0;
  1159. zip->ppstream.overconsumed = 0;
  1160. zip->ppstream.total_in = 0;
  1161. zip->ppstream.total_out = 0;
  1162. break;
  1163. }
  1164. case _7Z_X86:
  1165. case _7Z_X86_BCJ2:
  1166. case _7Z_POWERPC:
  1167. case _7Z_IA64:
  1168. case _7Z_ARM:
  1169. case _7Z_ARMTHUMB:
  1170. case _7Z_SPARC:
  1171. case _7Z_DELTA:
  1172. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1173. "Unexpected codec ID: %lX", zip->codec);
  1174. return (ARCHIVE_FAILED);
  1175. case _7Z_CRYPTO_MAIN_ZIP:
  1176. case _7Z_CRYPTO_RAR_29:
  1177. case _7Z_CRYPTO_AES_256_SHA_256:
  1178. if (a->entry) {
  1179. archive_entry_set_is_metadata_encrypted(a->entry, 1);
  1180. archive_entry_set_is_data_encrypted(a->entry, 1);
  1181. zip->has_encrypted_entries = 1;
  1182. }
  1183. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1184. "Crypto codec not supported yet (ID: 0x%lX)", zip->codec);
  1185. return (ARCHIVE_FAILED);
  1186. default:
  1187. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1188. "Unknown codec ID: %lX", zip->codec);
  1189. return (ARCHIVE_FAILED);
  1190. }
  1191. return (ARCHIVE_OK);
  1192. }
  1193. static int
  1194. decompress(struct archive_read *a, struct _7zip *zip,
  1195. void *buff, size_t *outbytes, const void *b, size_t *used)
  1196. {
  1197. const uint8_t *t_next_in;
  1198. uint8_t *t_next_out;
  1199. size_t o_avail_in, o_avail_out;
  1200. size_t t_avail_in, t_avail_out;
  1201. uint8_t *bcj2_next_out;
  1202. size_t bcj2_avail_out;
  1203. int r, ret = ARCHIVE_OK;
  1204. t_avail_in = o_avail_in = *used;
  1205. t_avail_out = o_avail_out = *outbytes;
  1206. t_next_in = b;
  1207. t_next_out = buff;
  1208. if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
  1209. int i;
  1210. /* Do not copy out the BCJ remaining bytes when the output
  1211. * buffer size is less than five bytes. */
  1212. if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
  1213. *used = 0;
  1214. *outbytes = 0;
  1215. return (ret);
  1216. }
  1217. for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
  1218. *t_next_out++ = zip->odd_bcj[i];
  1219. t_avail_out--;
  1220. zip->odd_bcj_size--;
  1221. }
  1222. if (o_avail_in == 0 || t_avail_out == 0) {
  1223. *used = o_avail_in - t_avail_in;
  1224. *outbytes = o_avail_out - t_avail_out;
  1225. if (o_avail_in == 0)
  1226. ret = ARCHIVE_EOF;
  1227. return (ret);
  1228. }
  1229. }
  1230. bcj2_next_out = t_next_out;
  1231. bcj2_avail_out = t_avail_out;
  1232. if (zip->codec2 == _7Z_X86_BCJ2) {
  1233. /*
  1234. * Decord a remaining decompressed main stream for BCJ2.
  1235. */
  1236. if (zip->tmp_stream_bytes_remaining) {
  1237. ssize_t bytes;
  1238. size_t remaining = zip->tmp_stream_bytes_remaining;
  1239. bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
  1240. if (bytes < 0) {
  1241. archive_set_error(&(a->archive),
  1242. ARCHIVE_ERRNO_MISC,
  1243. "BCJ2 conversion Failed");
  1244. return (ARCHIVE_FAILED);
  1245. }
  1246. zip->main_stream_bytes_remaining -=
  1247. remaining - zip->tmp_stream_bytes_remaining;
  1248. t_avail_out -= bytes;
  1249. if (o_avail_in == 0 || t_avail_out == 0) {
  1250. *used = 0;
  1251. *outbytes = o_avail_out - t_avail_out;
  1252. if (o_avail_in == 0 &&
  1253. zip->tmp_stream_bytes_remaining)
  1254. ret = ARCHIVE_EOF;
  1255. return (ret);
  1256. }
  1257. t_next_out += bytes;
  1258. bcj2_next_out = t_next_out;
  1259. bcj2_avail_out = t_avail_out;
  1260. }
  1261. t_next_out = zip->tmp_stream_buff;
  1262. t_avail_out = zip->tmp_stream_buff_size;
  1263. }
  1264. switch (zip->codec) {
  1265. case _7Z_COPY:
  1266. {
  1267. size_t bytes =
  1268. (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
  1269. memcpy(t_next_out, t_next_in, bytes);
  1270. t_avail_in -= bytes;
  1271. t_avail_out -= bytes;
  1272. if (o_avail_in == 0)
  1273. ret = ARCHIVE_EOF;
  1274. break;
  1275. }
  1276. #ifdef HAVE_LZMA_H
  1277. case _7Z_LZMA: case _7Z_LZMA2:
  1278. zip->lzstream.next_in = t_next_in;
  1279. zip->lzstream.avail_in = t_avail_in;
  1280. zip->lzstream.next_out = t_next_out;
  1281. zip->lzstream.avail_out = t_avail_out;
  1282. r = lzma_code(&(zip->lzstream), LZMA_RUN);
  1283. switch (r) {
  1284. case LZMA_STREAM_END: /* Found end of stream. */
  1285. lzma_end(&(zip->lzstream));
  1286. zip->lzstream_valid = 0;
  1287. ret = ARCHIVE_EOF;
  1288. break;
  1289. case LZMA_OK: /* Decompressor made some progress. */
  1290. break;
  1291. default:
  1292. archive_set_error(&(a->archive),
  1293. ARCHIVE_ERRNO_MISC,
  1294. "Decompression failed(%d)",
  1295. r);
  1296. return (ARCHIVE_FAILED);
  1297. }
  1298. t_avail_in = zip->lzstream.avail_in;
  1299. t_avail_out = zip->lzstream.avail_out;
  1300. break;
  1301. #endif
  1302. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1303. case _7Z_BZ2:
  1304. zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
  1305. zip->bzstream.avail_in = t_avail_in;
  1306. zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
  1307. zip->bzstream.avail_out = t_avail_out;
  1308. r = BZ2_bzDecompress(&(zip->bzstream));
  1309. switch (r) {
  1310. case BZ_STREAM_END: /* Found end of stream. */
  1311. switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
  1312. case BZ_OK:
  1313. break;
  1314. default:
  1315. archive_set_error(&(a->archive),
  1316. ARCHIVE_ERRNO_MISC,
  1317. "Failed to clean up decompressor");
  1318. return (ARCHIVE_FAILED);
  1319. }
  1320. zip->bzstream_valid = 0;
  1321. ret = ARCHIVE_EOF;
  1322. break;
  1323. case BZ_OK: /* Decompressor made some progress. */
  1324. break;
  1325. default:
  1326. archive_set_error(&(a->archive),
  1327. ARCHIVE_ERRNO_MISC,
  1328. "bzip decompression failed");
  1329. return (ARCHIVE_FAILED);
  1330. }
  1331. t_avail_in = zip->bzstream.avail_in;
  1332. t_avail_out = zip->bzstream.avail_out;
  1333. break;
  1334. #endif
  1335. #ifdef HAVE_ZLIB_H
  1336. case _7Z_DEFLATE:
  1337. zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
  1338. zip->stream.avail_in = (uInt)t_avail_in;
  1339. zip->stream.next_out = t_next_out;
  1340. zip->stream.avail_out = (uInt)t_avail_out;
  1341. r = inflate(&(zip->stream), 0);
  1342. switch (r) {
  1343. case Z_STREAM_END: /* Found end of stream. */
  1344. ret = ARCHIVE_EOF;
  1345. break;
  1346. case Z_OK: /* Decompressor made some progress.*/
  1347. break;
  1348. default:
  1349. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1350. "File decompression failed (%d)", r);
  1351. return (ARCHIVE_FAILED);
  1352. }
  1353. t_avail_in = zip->stream.avail_in;
  1354. t_avail_out = zip->stream.avail_out;
  1355. break;
  1356. #endif
  1357. case _7Z_PPMD:
  1358. {
  1359. uint64_t flush_bytes;
  1360. if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
  1361. t_avail_out <= 0) {
  1362. archive_set_error(&(a->archive),
  1363. ARCHIVE_ERRNO_MISC,
  1364. "Decompression internal error");
  1365. return (ARCHIVE_FAILED);
  1366. }
  1367. zip->ppstream.next_in = t_next_in;
  1368. zip->ppstream.avail_in = t_avail_in;
  1369. zip->ppstream.next_out = t_next_out;
  1370. zip->ppstream.avail_out = t_avail_out;
  1371. if (zip->ppmd7_stat == 0) {
  1372. zip->bytein.a = a;
  1373. zip->bytein.Read = &ppmd_read;
  1374. zip->range_dec.Stream = &zip->bytein;
  1375. r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
  1376. &(zip->range_dec));
  1377. if (r == 0) {
  1378. zip->ppmd7_stat = -1;
  1379. archive_set_error(&a->archive,
  1380. ARCHIVE_ERRNO_MISC,
  1381. "Failed to initialize PPMd range decorder");
  1382. return (ARCHIVE_FAILED);
  1383. }
  1384. if (zip->ppstream.overconsumed) {
  1385. zip->ppmd7_stat = -1;
  1386. return (ARCHIVE_FAILED);
  1387. }
  1388. zip->ppmd7_stat = 1;
  1389. }
  1390. if (t_avail_in == 0)
  1391. /* XXX Flush out remaining decoded data XXX */
  1392. flush_bytes = zip->folder_outbytes_remaining;
  1393. else
  1394. flush_bytes = 0;
  1395. do {
  1396. int sym;
  1397. sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1398. &(zip->ppmd7_context), &(zip->range_dec.p));
  1399. if (sym < 0) {
  1400. zip->ppmd7_stat = -1;
  1401. archive_set_error(&a->archive,
  1402. ARCHIVE_ERRNO_FILE_FORMAT,
  1403. "Failed to decode PPMd");
  1404. return (ARCHIVE_FAILED);
  1405. }
  1406. if (zip->ppstream.overconsumed) {
  1407. zip->ppmd7_stat = -1;
  1408. return (ARCHIVE_FAILED);
  1409. }
  1410. *zip->ppstream.next_out++ = (unsigned char)sym;
  1411. zip->ppstream.avail_out--;
  1412. zip->ppstream.total_out++;
  1413. if (flush_bytes)
  1414. flush_bytes--;
  1415. } while (zip->ppstream.avail_out &&
  1416. (zip->ppstream.avail_in || flush_bytes));
  1417. t_avail_in = (size_t)zip->ppstream.avail_in;
  1418. t_avail_out = (size_t)zip->ppstream.avail_out;
  1419. break;
  1420. }
  1421. default:
  1422. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  1423. "Decompression internal error");
  1424. return (ARCHIVE_FAILED);
  1425. }
  1426. if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
  1427. return (ret);
  1428. *used = o_avail_in - t_avail_in;
  1429. *outbytes = o_avail_out - t_avail_out;
  1430. /*
  1431. * Decord BCJ.
  1432. */
  1433. if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
  1434. size_t l = x86_Convert(zip, buff, *outbytes);
  1435. zip->odd_bcj_size = *outbytes - l;
  1436. if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
  1437. o_avail_in && ret != ARCHIVE_EOF) {
  1438. memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
  1439. zip->odd_bcj_size);
  1440. *outbytes = l;
  1441. } else
  1442. zip->odd_bcj_size = 0;
  1443. }
  1444. /*
  1445. * Decord BCJ2 with a decompressed main stream.
  1446. */
  1447. if (zip->codec2 == _7Z_X86_BCJ2) {
  1448. ssize_t bytes;
  1449. zip->tmp_stream_bytes_avail =
  1450. zip->tmp_stream_buff_size - t_avail_out;
  1451. if (zip->tmp_stream_bytes_avail >
  1452. zip->main_stream_bytes_remaining)
  1453. zip->tmp_stream_bytes_avail =
  1454. zip->main_stream_bytes_remaining;
  1455. zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
  1456. bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
  1457. if (bytes < 0) {
  1458. archive_set_error(&(a->archive),
  1459. ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
  1460. return (ARCHIVE_FAILED);
  1461. }
  1462. zip->main_stream_bytes_remaining -=
  1463. zip->tmp_stream_bytes_avail
  1464. - zip->tmp_stream_bytes_remaining;
  1465. bcj2_avail_out -= bytes;
  1466. *outbytes = o_avail_out - bcj2_avail_out;
  1467. }
  1468. return (ret);
  1469. }
  1470. static int
  1471. free_decompression(struct archive_read *a, struct _7zip *zip)
  1472. {
  1473. int r = ARCHIVE_OK;
  1474. #if !defined(HAVE_ZLIB_H) &&\
  1475. !(defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR))
  1476. (void)a;/* UNUSED */
  1477. #endif
  1478. #ifdef HAVE_LZMA_H
  1479. if (zip->lzstream_valid)
  1480. lzma_end(&(zip->lzstream));
  1481. #endif
  1482. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1483. if (zip->bzstream_valid) {
  1484. if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
  1485. archive_set_error(&a->archive,
  1486. ARCHIVE_ERRNO_MISC,
  1487. "Failed to clean up bzip2 decompressor");
  1488. r = ARCHIVE_FATAL;
  1489. }
  1490. zip->bzstream_valid = 0;
  1491. }
  1492. #endif
  1493. #ifdef HAVE_ZLIB_H
  1494. if (zip->stream_valid) {
  1495. if (inflateEnd(&(zip->stream)) != Z_OK) {
  1496. archive_set_error(&a->archive,
  1497. ARCHIVE_ERRNO_MISC,
  1498. "Failed to clean up zlib decompressor");
  1499. r = ARCHIVE_FATAL;
  1500. }
  1501. zip->stream_valid = 0;
  1502. }
  1503. #endif
  1504. if (zip->ppmd7_valid) {
  1505. __archive_ppmd7_functions.Ppmd7_Free(
  1506. &zip->ppmd7_context, &g_szalloc);
  1507. zip->ppmd7_valid = 0;
  1508. }
  1509. return (r);
  1510. }
  1511. static int
  1512. parse_7zip_uint64(struct archive_read *a, uint64_t *val)
  1513. {
  1514. const unsigned char *p;
  1515. unsigned char avail, mask;
  1516. int i;
  1517. if ((p = header_bytes(a, 1)) == NULL)
  1518. return (-1);
  1519. avail = *p;
  1520. mask = 0x80;
  1521. *val = 0;
  1522. for (i = 0; i < 8; i++) {
  1523. if (avail & mask) {
  1524. if ((p = header_bytes(a, 1)) == NULL)
  1525. return (-1);
  1526. *val |= ((uint64_t)*p) << (8 * i);
  1527. mask >>= 1;
  1528. continue;
  1529. }
  1530. *val += ((uint64_t)(avail & (mask -1))) << (8 * i);
  1531. break;
  1532. }
  1533. return (0);
  1534. }
  1535. static int
  1536. read_Bools(struct archive_read *a, unsigned char *data, size_t num)
  1537. {
  1538. const unsigned char *p;
  1539. unsigned i, mask = 0, avail = 0;
  1540. for (i = 0; i < num; i++) {
  1541. if (mask == 0) {
  1542. if ((p = header_bytes(a, 1)) == NULL)
  1543. return (-1);
  1544. avail = *p;
  1545. mask = 0x80;
  1546. }
  1547. data[i] = (avail & mask)?1:0;
  1548. mask >>= 1;
  1549. }
  1550. return (0);
  1551. }
  1552. static void
  1553. free_Digest(struct _7z_digests *d)
  1554. {
  1555. free(d->defineds);
  1556. free(d->digests);
  1557. }
  1558. static int
  1559. read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
  1560. {
  1561. const unsigned char *p;
  1562. unsigned i;
  1563. if (num == 0)
  1564. return (-1);
  1565. memset(d, 0, sizeof(*d));
  1566. d->defineds = malloc(num);
  1567. if (d->defineds == NULL)
  1568. return (-1);
  1569. /*
  1570. * Read Bools.
  1571. */
  1572. if ((p = header_bytes(a, 1)) == NULL)
  1573. return (-1);
  1574. if (*p == 0) {
  1575. if (read_Bools(a, d->defineds, num) < 0)
  1576. return (-1);
  1577. } else
  1578. /* All are defined */
  1579. memset(d->defineds, 1, num);
  1580. d->digests = calloc(num, sizeof(*d->digests));
  1581. if (d->digests == NULL)
  1582. return (-1);
  1583. for (i = 0; i < num; i++) {
  1584. if (d->defineds[i]) {
  1585. if ((p = header_bytes(a, 4)) == NULL)
  1586. return (-1);
  1587. d->digests[i] = archive_le32dec(p);
  1588. }
  1589. }
  1590. return (0);
  1591. }
  1592. static void
  1593. free_PackInfo(struct _7z_pack_info *pi)
  1594. {
  1595. free(pi->sizes);
  1596. free(pi->positions);
  1597. free_Digest(&(pi->digest));
  1598. }
  1599. static int
  1600. read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
  1601. {
  1602. const unsigned char *p;
  1603. unsigned i;
  1604. memset(pi, 0, sizeof(*pi));
  1605. /*
  1606. * Read PackPos.
  1607. */
  1608. if (parse_7zip_uint64(a, &(pi->pos)) < 0)
  1609. return (-1);
  1610. /*
  1611. * Read NumPackStreams.
  1612. */
  1613. if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
  1614. return (-1);
  1615. if (pi->numPackStreams == 0)
  1616. return (-1);
  1617. if (UMAX_ENTRY < pi->numPackStreams)
  1618. return (-1);
  1619. /*
  1620. * Read PackSizes[num]
  1621. */
  1622. if ((p = header_bytes(a, 1)) == NULL)
  1623. return (-1);
  1624. if (*p == kEnd)
  1625. /* PackSizes[num] are not present. */
  1626. return (0);
  1627. if (*p != kSize)
  1628. return (-1);
  1629. pi->sizes = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
  1630. pi->positions = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
  1631. if (pi->sizes == NULL || pi->positions == NULL)
  1632. return (-1);
  1633. for (i = 0; i < pi->numPackStreams; i++) {
  1634. if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
  1635. return (-1);
  1636. }
  1637. /*
  1638. * Read PackStreamDigests[num]
  1639. */
  1640. if ((p = header_bytes(a, 1)) == NULL)
  1641. return (-1);
  1642. if (*p == kEnd) {
  1643. /* PackStreamDigests[num] are not present. */
  1644. pi->digest.defineds =
  1645. calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.defineds));
  1646. pi->digest.digests =
  1647. calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.digests));
  1648. if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
  1649. return (-1);
  1650. return (0);
  1651. }
  1652. if (*p != kSize)
  1653. return (-1);
  1654. if (read_Digests(a, &(pi->digest), (size_t)pi->numPackStreams) < 0)
  1655. return (-1);
  1656. /*
  1657. * Must be marked by kEnd.
  1658. */
  1659. if ((p = header_bytes(a, 1)) == NULL)
  1660. return (-1);
  1661. if (*p != kEnd)
  1662. return (-1);
  1663. return (0);
  1664. }
  1665. static void
  1666. free_Folder(struct _7z_folder *f)
  1667. {
  1668. unsigned i;
  1669. if (f->coders) {
  1670. for (i = 0; i< f->numCoders; i++) {
  1671. free(f->coders[i].properties);
  1672. }
  1673. free(f->coders);
  1674. }
  1675. free(f->bindPairs);
  1676. free(f->packedStreams);
  1677. free(f->unPackSize);
  1678. }
  1679. static int
  1680. read_Folder(struct archive_read *a, struct _7z_folder *f)
  1681. {
  1682. struct _7zip *zip = (struct _7zip *)a->format->data;
  1683. const unsigned char *p;
  1684. uint64_t numInStreamsTotal = 0;
  1685. uint64_t numOutStreamsTotal = 0;
  1686. unsigned i;
  1687. memset(f, 0, sizeof(*f));
  1688. /*
  1689. * Read NumCoders.
  1690. */
  1691. if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
  1692. return (-1);
  1693. if (f->numCoders > 4)
  1694. /* Too many coders. */
  1695. return (-1);
  1696. f->coders = calloc((size_t)f->numCoders, sizeof(*f->coders));
  1697. if (f->coders == NULL)
  1698. return (-1);
  1699. for (i = 0; i< f->numCoders; i++) {
  1700. size_t codec_size;
  1701. int simple, attr;
  1702. if ((p = header_bytes(a, 1)) == NULL)
  1703. return (-1);
  1704. /*
  1705. * 0:3 CodecIdSize
  1706. * 4: 0 - IsSimple
  1707. * 1 - Is not Simple
  1708. * 5: 0 - No Attributes
  1709. * 1 - There are Attributes;
  1710. * 7: Must be zero.
  1711. */
  1712. codec_size = *p & 0xf;
  1713. simple = (*p & 0x10)?0:1;
  1714. attr = *p & 0x20;
  1715. if (*p & 0x80)
  1716. return (-1);/* Not supported. */
  1717. /*
  1718. * Read Decompression Method IDs.
  1719. */
  1720. if ((p = header_bytes(a, codec_size)) == NULL)
  1721. return (-1);
  1722. f->coders[i].codec = decode_codec_id(p, codec_size);
  1723. if (simple) {
  1724. f->coders[i].numInStreams = 1;
  1725. f->coders[i].numOutStreams = 1;
  1726. } else {
  1727. if (parse_7zip_uint64(
  1728. a, &(f->coders[i].numInStreams)) < 0)
  1729. return (-1);
  1730. if (UMAX_ENTRY < f->coders[i].numInStreams)
  1731. return (-1);
  1732. if (parse_7zip_uint64(
  1733. a, &(f->coders[i].numOutStreams)) < 0)
  1734. return (-1);
  1735. if (UMAX_ENTRY < f->coders[i].numOutStreams)
  1736. return (-1);
  1737. }
  1738. if (attr) {
  1739. if (parse_7zip_uint64(
  1740. a, &(f->coders[i].propertiesSize)) < 0)
  1741. return (-1);
  1742. if ((p = header_bytes(
  1743. a, (size_t)f->coders[i].propertiesSize)) == NULL)
  1744. return (-1);
  1745. f->coders[i].properties =
  1746. malloc((size_t)f->coders[i].propertiesSize);
  1747. if (f->coders[i].properties == NULL)
  1748. return (-1);
  1749. memcpy(f->coders[i].properties, p,
  1750. (size_t)f->coders[i].propertiesSize);
  1751. }
  1752. numInStreamsTotal += f->coders[i].numInStreams;
  1753. numOutStreamsTotal += f->coders[i].numOutStreams;
  1754. }
  1755. if (numOutStreamsTotal == 0 ||
  1756. numInStreamsTotal < numOutStreamsTotal-1)
  1757. return (-1);
  1758. f->numBindPairs = numOutStreamsTotal - 1;
  1759. if (zip->header_bytes_remaining < f->numBindPairs)
  1760. return (-1);
  1761. if (f->numBindPairs > 0) {
  1762. f->bindPairs =
  1763. calloc((size_t)f->numBindPairs, sizeof(*f->bindPairs));
  1764. if (f->bindPairs == NULL)
  1765. return (-1);
  1766. } else
  1767. f->bindPairs = NULL;
  1768. for (i = 0; i < f->numBindPairs; i++) {
  1769. if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
  1770. return (-1);
  1771. if (UMAX_ENTRY < f->bindPairs[i].inIndex)
  1772. return (-1);
  1773. if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
  1774. return (-1);
  1775. if (UMAX_ENTRY < f->bindPairs[i].outIndex)
  1776. return (-1);
  1777. }
  1778. f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
  1779. f->packedStreams =
  1780. calloc((size_t)f->numPackedStreams, sizeof(*f->packedStreams));
  1781. if (f->packedStreams == NULL)
  1782. return (-1);
  1783. if (f->numPackedStreams == 1) {
  1784. for (i = 0; i < numInStreamsTotal; i++) {
  1785. unsigned j;
  1786. for (j = 0; j < f->numBindPairs; j++) {
  1787. if (f->bindPairs[j].inIndex == i)
  1788. break;
  1789. }
  1790. if (j == f->numBindPairs)
  1791. break;
  1792. }
  1793. if (i == numInStreamsTotal)
  1794. return (-1);
  1795. f->packedStreams[0] = i;
  1796. } else {
  1797. for (i = 0; i < f->numPackedStreams; i++) {
  1798. if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
  1799. return (-1);
  1800. if (UMAX_ENTRY < f->packedStreams[i])
  1801. return (-1);
  1802. }
  1803. }
  1804. f->numInStreams = numInStreamsTotal;
  1805. f->numOutStreams = numOutStreamsTotal;
  1806. return (0);
  1807. }
  1808. static void
  1809. free_CodersInfo(struct _7z_coders_info *ci)
  1810. {
  1811. unsigned i;
  1812. if (ci->folders) {
  1813. for (i = 0; i < ci->numFolders; i++)
  1814. free_Folder(&(ci->folders[i]));
  1815. free(ci->folders);
  1816. }
  1817. }
  1818. static int
  1819. read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
  1820. {
  1821. const unsigned char *p;
  1822. struct _7z_digests digest;
  1823. unsigned i;
  1824. memset(ci, 0, sizeof(*ci));
  1825. memset(&digest, 0, sizeof(digest));
  1826. if ((p = header_bytes(a, 1)) == NULL)
  1827. goto failed;
  1828. if (*p != kFolder)
  1829. goto failed;
  1830. /*
  1831. * Read NumFolders.
  1832. */
  1833. if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
  1834. goto failed;
  1835. if (UMAX_ENTRY < ci->numFolders)
  1836. return (-1);
  1837. /*
  1838. * Read External.
  1839. */
  1840. if ((p = header_bytes(a, 1)) == NULL)
  1841. goto failed;
  1842. switch (*p) {
  1843. case 0:
  1844. ci->folders =
  1845. calloc((size_t)ci->numFolders, sizeof(*ci->folders));
  1846. if (ci->folders == NULL)
  1847. return (-1);
  1848. for (i = 0; i < ci->numFolders; i++) {
  1849. if (read_Folder(a, &(ci->folders[i])) < 0)
  1850. goto failed;
  1851. }
  1852. break;
  1853. case 1:
  1854. if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
  1855. return (-1);
  1856. if (UMAX_ENTRY < ci->dataStreamIndex)
  1857. return (-1);
  1858. if (ci->numFolders > 0) {
  1859. archive_set_error(&a->archive, -1,
  1860. "Malformed 7-Zip archive");
  1861. goto failed;
  1862. }
  1863. break;
  1864. default:
  1865. archive_set_error(&a->archive, -1,
  1866. "Malformed 7-Zip archive");
  1867. goto failed;
  1868. }
  1869. if ((p = header_bytes(a, 1)) == NULL)
  1870. goto failed;
  1871. if (*p != kCodersUnPackSize)
  1872. goto failed;
  1873. for (i = 0; i < ci->numFolders; i++) {
  1874. struct _7z_folder *folder = &(ci->folders[i]);
  1875. unsigned j;
  1876. folder->unPackSize =
  1877. calloc((size_t)folder->numOutStreams, sizeof(*folder->unPackSize));
  1878. if (folder->unPackSize == NULL)
  1879. goto failed;
  1880. for (j = 0; j < folder->numOutStreams; j++) {
  1881. if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
  1882. goto failed;
  1883. }
  1884. }
  1885. /*
  1886. * Read CRCs.
  1887. */
  1888. if ((p = header_bytes(a, 1)) == NULL)
  1889. goto failed;
  1890. if (*p == kEnd)
  1891. return (0);
  1892. if (*p != kCRC)
  1893. goto failed;
  1894. if (read_Digests(a, &digest, (size_t)ci->numFolders) < 0)
  1895. goto failed;
  1896. for (i = 0; i < ci->numFolders; i++) {
  1897. ci->folders[i].digest_defined = digest.defineds[i];
  1898. ci->folders[i].digest = digest.digests[i];
  1899. }
  1900. /*
  1901. * Must be kEnd.
  1902. */
  1903. if ((p = header_bytes(a, 1)) == NULL)
  1904. goto failed;
  1905. if (*p != kEnd)
  1906. goto failed;
  1907. free_Digest(&digest);
  1908. return (0);
  1909. failed:
  1910. free_Digest(&digest);
  1911. return (-1);
  1912. }
  1913. static uint64_t
  1914. folder_uncompressed_size(struct _7z_folder *f)
  1915. {
  1916. int n = (int)f->numOutStreams;
  1917. unsigned pairs = (unsigned)f->numBindPairs;
  1918. while (--n >= 0) {
  1919. unsigned i;
  1920. for (i = 0; i < pairs; i++) {
  1921. if (f->bindPairs[i].outIndex == (uint64_t)n)
  1922. break;
  1923. }
  1924. if (i >= pairs)
  1925. return (f->unPackSize[n]);
  1926. }
  1927. return (0);
  1928. }
  1929. static void
  1930. free_SubStreamsInfo(struct _7z_substream_info *ss)
  1931. {
  1932. free(ss->unpackSizes);
  1933. free(ss->digestsDefined);
  1934. free(ss->digests);
  1935. }
  1936. static int
  1937. read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
  1938. struct _7z_folder *f, size_t numFolders)
  1939. {
  1940. const unsigned char *p;
  1941. uint64_t *usizes;
  1942. size_t unpack_streams;
  1943. int type;
  1944. unsigned i;
  1945. uint32_t numDigests;
  1946. memset(ss, 0, sizeof(*ss));
  1947. for (i = 0; i < numFolders; i++)
  1948. f[i].numUnpackStreams = 1;
  1949. if ((p = header_bytes(a, 1)) == NULL)
  1950. return (-1);
  1951. type = *p;
  1952. if (type == kNumUnPackStream) {
  1953. unpack_streams = 0;
  1954. for (i = 0; i < numFolders; i++) {
  1955. if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
  1956. return (-1);
  1957. if (UMAX_ENTRY < f[i].numUnpackStreams)
  1958. return (-1);
  1959. if (unpack_streams > SIZE_MAX - UMAX_ENTRY) {
  1960. return (-1);
  1961. }
  1962. unpack_streams += (size_t)f[i].numUnpackStreams;
  1963. }
  1964. if ((p = header_bytes(a, 1)) == NULL)
  1965. return (-1);
  1966. type = *p;
  1967. } else
  1968. unpack_streams = numFolders;
  1969. ss->unpack_streams = unpack_streams;
  1970. if (unpack_streams) {
  1971. ss->unpackSizes = calloc(unpack_streams,
  1972. sizeof(*ss->unpackSizes));
  1973. ss->digestsDefined = calloc(unpack_streams,
  1974. sizeof(*ss->digestsDefined));
  1975. ss->digests = calloc(unpack_streams,
  1976. sizeof(*ss->digests));
  1977. if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
  1978. ss->digests == NULL)
  1979. return (-1);
  1980. }
  1981. usizes = ss->unpackSizes;
  1982. for (i = 0; i < numFolders; i++) {
  1983. unsigned pack;
  1984. uint64_t sum;
  1985. if (f[i].numUnpackStreams == 0)
  1986. continue;
  1987. sum = 0;
  1988. if (type == kSize) {
  1989. for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
  1990. if (parse_7zip_uint64(a, usizes) < 0)
  1991. return (-1);
  1992. sum += *usizes++;
  1993. }
  1994. }
  1995. *usizes++ = folder_uncompressed_size(&f[i]) - sum;
  1996. }
  1997. if (type == kSize) {
  1998. if ((p = header_bytes(a, 1)) == NULL)
  1999. return (-1);
  2000. type = *p;
  2001. }
  2002. for (i = 0; i < unpack_streams; i++) {
  2003. ss->digestsDefined[i] = 0;
  2004. ss->digests[i] = 0;
  2005. }
  2006. numDigests = 0;
  2007. for (i = 0; i < numFolders; i++) {
  2008. if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
  2009. numDigests += (uint32_t)f[i].numUnpackStreams;
  2010. }
  2011. if (type == kCRC) {
  2012. struct _7z_digests tmpDigests;
  2013. unsigned char *digestsDefined = ss->digestsDefined;
  2014. uint32_t * digests = ss->digests;
  2015. int di = 0;
  2016. memset(&tmpDigests, 0, sizeof(tmpDigests));
  2017. if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
  2018. free_Digest(&tmpDigests);
  2019. return (-1);
  2020. }
  2021. for (i = 0; i < numFolders; i++) {
  2022. if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
  2023. *digestsDefined++ = 1;
  2024. *digests++ = f[i].digest;
  2025. } else {
  2026. unsigned j;
  2027. for (j = 0; j < f[i].numUnpackStreams;
  2028. j++, di++) {
  2029. *digestsDefined++ =
  2030. tmpDigests.defineds[di];
  2031. *digests++ =
  2032. tmpDigests.digests[di];
  2033. }
  2034. }
  2035. }
  2036. free_Digest(&tmpDigests);
  2037. if ((p = header_bytes(a, 1)) == NULL)
  2038. return (-1);
  2039. type = *p;
  2040. }
  2041. /*
  2042. * Must be kEnd.
  2043. */
  2044. if (type != kEnd)
  2045. return (-1);
  2046. return (0);
  2047. }
  2048. static void
  2049. free_StreamsInfo(struct _7z_stream_info *si)
  2050. {
  2051. free_PackInfo(&(si->pi));
  2052. free_CodersInfo(&(si->ci));
  2053. free_SubStreamsInfo(&(si->ss));
  2054. }
  2055. static int
  2056. read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
  2057. {
  2058. struct _7zip *zip = (struct _7zip *)a->format->data;
  2059. const unsigned char *p;
  2060. unsigned i;
  2061. memset(si, 0, sizeof(*si));
  2062. if ((p = header_bytes(a, 1)) == NULL)
  2063. return (-1);
  2064. if (*p == kPackInfo) {
  2065. uint64_t packPos;
  2066. if (read_PackInfo(a, &(si->pi)) < 0)
  2067. return (-1);
  2068. if (si->pi.positions == NULL || si->pi.sizes == NULL)
  2069. return (-1);
  2070. /*
  2071. * Calculate packed stream positions.
  2072. */
  2073. packPos = si->pi.pos;
  2074. for (i = 0; i < si->pi.numPackStreams; i++) {
  2075. si->pi.positions[i] = packPos;
  2076. packPos += si->pi.sizes[i];
  2077. if (packPos > zip->header_offset)
  2078. return (-1);
  2079. }
  2080. if ((p = header_bytes(a, 1)) == NULL)
  2081. return (-1);
  2082. }
  2083. if (*p == kUnPackInfo) {
  2084. uint32_t packIndex;
  2085. struct _7z_folder *f;
  2086. if (read_CodersInfo(a, &(si->ci)) < 0)
  2087. return (-1);
  2088. /*
  2089. * Calculate packed stream indexes.
  2090. */
  2091. packIndex = 0;
  2092. f = si->ci.folders;
  2093. for (i = 0; i < si->ci.numFolders; i++) {
  2094. f[i].packIndex = packIndex;
  2095. packIndex += (uint32_t)f[i].numPackedStreams;
  2096. if (packIndex > si->pi.numPackStreams)
  2097. return (-1);
  2098. }
  2099. if ((p = header_bytes(a, 1)) == NULL)
  2100. return (-1);
  2101. }
  2102. if (*p == kSubStreamsInfo) {
  2103. if (read_SubStreamsInfo(a, &(si->ss),
  2104. si->ci.folders, (size_t)si->ci.numFolders) < 0)
  2105. return (-1);
  2106. if ((p = header_bytes(a, 1)) == NULL)
  2107. return (-1);
  2108. }
  2109. /*
  2110. * Must be kEnd.
  2111. */
  2112. if (*p != kEnd)
  2113. return (-1);
  2114. return (0);
  2115. }
  2116. static void
  2117. free_Header(struct _7z_header_info *h)
  2118. {
  2119. free(h->emptyStreamBools);
  2120. free(h->emptyFileBools);
  2121. free(h->antiBools);
  2122. free(h->attrBools);
  2123. }
  2124. static int
  2125. read_Header(struct archive_read *a, struct _7z_header_info *h,
  2126. int check_header_id)
  2127. {
  2128. struct _7zip *zip = (struct _7zip *)a->format->data;
  2129. const unsigned char *p;
  2130. struct _7z_folder *folders;
  2131. struct _7z_stream_info *si = &(zip->si);
  2132. struct _7zip_entry *entries;
  2133. uint32_t folderIndex, indexInFolder;
  2134. unsigned i;
  2135. int eindex, empty_streams, sindex;
  2136. if (check_header_id) {
  2137. /*
  2138. * Read Header.
  2139. */
  2140. if ((p = header_bytes(a, 1)) == NULL)
  2141. return (-1);
  2142. if (*p != kHeader)
  2143. return (-1);
  2144. }
  2145. /*
  2146. * Read ArchiveProperties.
  2147. */
  2148. if ((p = header_bytes(a, 1)) == NULL)
  2149. return (-1);
  2150. if (*p == kArchiveProperties) {
  2151. for (;;) {
  2152. uint64_t size;
  2153. if ((p = header_bytes(a, 1)) == NULL)
  2154. return (-1);
  2155. if (*p == 0)
  2156. break;
  2157. if (parse_7zip_uint64(a, &size) < 0)
  2158. return (-1);
  2159. }
  2160. if ((p = header_bytes(a, 1)) == NULL)
  2161. return (-1);
  2162. }
  2163. /*
  2164. * Read MainStreamsInfo.
  2165. */
  2166. if (*p == kMainStreamsInfo) {
  2167. if (read_StreamsInfo(a, &(zip->si)) < 0)
  2168. return (-1);
  2169. if ((p = header_bytes(a, 1)) == NULL)
  2170. return (-1);
  2171. }
  2172. if (*p == kEnd)
  2173. return (0);
  2174. /*
  2175. * Read FilesInfo.
  2176. */
  2177. if (*p != kFilesInfo)
  2178. return (-1);
  2179. if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
  2180. return (-1);
  2181. if (UMAX_ENTRY < zip->numFiles)
  2182. return (-1);
  2183. zip->entries = calloc((size_t)zip->numFiles, sizeof(*zip->entries));
  2184. if (zip->entries == NULL)
  2185. return (-1);
  2186. entries = zip->entries;
  2187. empty_streams = 0;
  2188. for (;;) {
  2189. int type;
  2190. uint64_t size;
  2191. size_t ll;
  2192. if ((p = header_bytes(a, 1)) == NULL)
  2193. return (-1);
  2194. type = *p;
  2195. if (type == kEnd)
  2196. break;
  2197. if (parse_7zip_uint64(a, &size) < 0)
  2198. return (-1);
  2199. if (zip->header_bytes_remaining < size)
  2200. return (-1);
  2201. ll = (size_t)size;
  2202. switch (type) {
  2203. case kEmptyStream:
  2204. h->emptyStreamBools = calloc((size_t)zip->numFiles,
  2205. sizeof(*h->emptyStreamBools));
  2206. if (h->emptyStreamBools == NULL)
  2207. return (-1);
  2208. if (read_Bools(
  2209. a, h->emptyStreamBools, (size_t)zip->numFiles) < 0)
  2210. return (-1);
  2211. empty_streams = 0;
  2212. for (i = 0; i < zip->numFiles; i++) {
  2213. if (h->emptyStreamBools[i])
  2214. empty_streams++;
  2215. }
  2216. break;
  2217. case kEmptyFile:
  2218. if (empty_streams <= 0) {
  2219. /* Unexcepted sequence. Skip this. */
  2220. if (header_bytes(a, ll) == NULL)
  2221. return (-1);
  2222. break;
  2223. }
  2224. h->emptyFileBools = calloc(empty_streams,
  2225. sizeof(*h->emptyFileBools));
  2226. if (h->emptyFileBools == NULL)
  2227. return (-1);
  2228. if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
  2229. return (-1);
  2230. break;
  2231. case kAnti:
  2232. if (empty_streams <= 0) {
  2233. /* Unexcepted sequence. Skip this. */
  2234. if (header_bytes(a, ll) == NULL)
  2235. return (-1);
  2236. break;
  2237. }
  2238. h->antiBools = calloc(empty_streams,
  2239. sizeof(*h->antiBools));
  2240. if (h->antiBools == NULL)
  2241. return (-1);
  2242. if (read_Bools(a, h->antiBools, empty_streams) < 0)
  2243. return (-1);
  2244. break;
  2245. case kCTime:
  2246. case kATime:
  2247. case kMTime:
  2248. if (read_Times(a, h, type) < 0)
  2249. return (-1);
  2250. break;
  2251. case kName:
  2252. {
  2253. unsigned char *np;
  2254. size_t nl, nb;
  2255. /* Skip one byte. */
  2256. if ((p = header_bytes(a, 1)) == NULL)
  2257. return (-1);
  2258. ll--;
  2259. if ((ll & 1) || ll < zip->numFiles * 4)
  2260. return (-1);
  2261. zip->entry_names = malloc(ll);
  2262. if (zip->entry_names == NULL)
  2263. return (-1);
  2264. np = zip->entry_names;
  2265. nb = ll;
  2266. /*
  2267. * Copy whole file names.
  2268. * NOTE: This loop prevents from expanding
  2269. * the uncompressed buffer in order not to
  2270. * use extra memory resource.
  2271. */
  2272. while (nb) {
  2273. size_t b;
  2274. if (nb > UBUFF_SIZE)
  2275. b = UBUFF_SIZE;
  2276. else
  2277. b = nb;
  2278. if ((p = header_bytes(a, b)) == NULL)
  2279. return (-1);
  2280. memcpy(np, p, b);
  2281. np += b;
  2282. nb -= b;
  2283. }
  2284. np = zip->entry_names;
  2285. nl = ll;
  2286. for (i = 0; i < zip->numFiles; i++) {
  2287. entries[i].utf16name = np;
  2288. #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
  2289. entries[i].wname = (wchar_t *)np;
  2290. #endif
  2291. /* Find a terminator. */
  2292. while (nl >= 2 && (np[0] || np[1])) {
  2293. np += 2;
  2294. nl -= 2;
  2295. }
  2296. if (nl < 2)
  2297. return (-1);/* Terminator not found */
  2298. entries[i].name_len = np - entries[i].utf16name;
  2299. np += 2;
  2300. nl -= 2;
  2301. }
  2302. break;
  2303. }
  2304. case kAttributes:
  2305. {
  2306. int allAreDefined;
  2307. if ((p = header_bytes(a, 2)) == NULL)
  2308. return (-1);
  2309. allAreDefined = *p;
  2310. h->attrBools = calloc((size_t)zip->numFiles,
  2311. sizeof(*h->attrBools));
  2312. if (h->attrBools == NULL)
  2313. return (-1);
  2314. if (allAreDefined)
  2315. memset(h->attrBools, 1, (size_t)zip->numFiles);
  2316. else {
  2317. if (read_Bools(a, h->attrBools,
  2318. (size_t)zip->numFiles) < 0)
  2319. return (-1);
  2320. }
  2321. for (i = 0; i < zip->numFiles; i++) {
  2322. if (h->attrBools[i]) {
  2323. if ((p = header_bytes(a, 4)) == NULL)
  2324. return (-1);
  2325. entries[i].attr = archive_le32dec(p);
  2326. }
  2327. }
  2328. break;
  2329. }
  2330. case kDummy:
  2331. if (ll == 0)
  2332. break;
  2333. default:
  2334. if (header_bytes(a, ll) == NULL)
  2335. return (-1);
  2336. break;
  2337. }
  2338. }
  2339. /*
  2340. * Set up entry's attributes.
  2341. */
  2342. folders = si->ci.folders;
  2343. eindex = sindex = 0;
  2344. folderIndex = indexInFolder = 0;
  2345. for (i = 0; i < zip->numFiles; i++) {
  2346. if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
  2347. entries[i].flg |= HAS_STREAM;
  2348. /* The high 16 bits of attributes is a posix file mode. */
  2349. entries[i].mode = entries[i].attr >> 16;
  2350. if (entries[i].flg & HAS_STREAM) {
  2351. if ((size_t)sindex >= si->ss.unpack_streams)
  2352. return (-1);
  2353. if (entries[i].mode == 0)
  2354. entries[i].mode = AE_IFREG | 0666;
  2355. if (si->ss.digestsDefined[sindex])
  2356. entries[i].flg |= CRC32_IS_SET;
  2357. entries[i].ssIndex = sindex;
  2358. sindex++;
  2359. } else {
  2360. int dir;
  2361. if (h->emptyFileBools == NULL)
  2362. dir = 1;
  2363. else {
  2364. if (h->emptyFileBools[eindex])
  2365. dir = 0;
  2366. else
  2367. dir = 1;
  2368. eindex++;
  2369. }
  2370. if (entries[i].mode == 0) {
  2371. if (dir)
  2372. entries[i].mode = AE_IFDIR | 0777;
  2373. else
  2374. entries[i].mode = AE_IFREG | 0666;
  2375. } else if (dir &&
  2376. (entries[i].mode & AE_IFMT) != AE_IFDIR) {
  2377. entries[i].mode &= ~AE_IFMT;
  2378. entries[i].mode |= AE_IFDIR;
  2379. }
  2380. if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
  2381. entries[i].name_len >= 2 &&
  2382. (entries[i].utf16name[entries[i].name_len-2] != '/' ||
  2383. entries[i].utf16name[entries[i].name_len-1] != 0)) {
  2384. entries[i].utf16name[entries[i].name_len] = '/';
  2385. entries[i].utf16name[entries[i].name_len+1] = 0;
  2386. entries[i].name_len += 2;
  2387. }
  2388. entries[i].ssIndex = -1;
  2389. }
  2390. if (entries[i].attr & 0x01)
  2391. entries[i].mode &= ~0222;/* Read only. */
  2392. if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
  2393. /*
  2394. * The entry is an empty file or a directory file,
  2395. * those both have no contents.
  2396. */
  2397. entries[i].folderIndex = -1;
  2398. continue;
  2399. }
  2400. if (indexInFolder == 0) {
  2401. for (;;) {
  2402. if (folderIndex >= si->ci.numFolders)
  2403. return (-1);
  2404. if (folders[folderIndex].numUnpackStreams)
  2405. break;
  2406. folderIndex++;
  2407. }
  2408. }
  2409. entries[i].folderIndex = folderIndex;
  2410. if ((entries[i].flg & HAS_STREAM) == 0)
  2411. continue;
  2412. indexInFolder++;
  2413. if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
  2414. folderIndex++;
  2415. indexInFolder = 0;
  2416. }
  2417. }
  2418. return (0);
  2419. }
  2420. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  2421. static void
  2422. fileTimeToUtc(uint64_t fileTime, time_t *timep, long *ns)
  2423. {
  2424. if (fileTime >= EPOC_TIME) {
  2425. fileTime -= EPOC_TIME;
  2426. /* milli seconds base */
  2427. *timep = (time_t)(fileTime / 10000000);
  2428. /* nano seconds base */
  2429. *ns = (long)(fileTime % 10000000) * 100;
  2430. } else {
  2431. *timep = 0;
  2432. *ns = 0;
  2433. }
  2434. }
  2435. static int
  2436. read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
  2437. {
  2438. struct _7zip *zip = (struct _7zip *)a->format->data;
  2439. const unsigned char *p;
  2440. struct _7zip_entry *entries = zip->entries;
  2441. unsigned char *timeBools;
  2442. int allAreDefined;
  2443. unsigned i;
  2444. timeBools = calloc((size_t)zip->numFiles, sizeof(*timeBools));
  2445. if (timeBools == NULL)
  2446. return (-1);
  2447. /* Read allAreDefined. */
  2448. if ((p = header_bytes(a, 1)) == NULL)
  2449. goto failed;
  2450. allAreDefined = *p;
  2451. if (allAreDefined)
  2452. memset(timeBools, 1, (size_t)zip->numFiles);
  2453. else {
  2454. if (read_Bools(a, timeBools, (size_t)zip->numFiles) < 0)
  2455. goto failed;
  2456. }
  2457. /* Read external. */
  2458. if ((p = header_bytes(a, 1)) == NULL)
  2459. goto failed;
  2460. if (*p) {
  2461. if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
  2462. goto failed;
  2463. if (UMAX_ENTRY < h->dataIndex)
  2464. goto failed;
  2465. }
  2466. for (i = 0; i < zip->numFiles; i++) {
  2467. if (!timeBools[i])
  2468. continue;
  2469. if ((p = header_bytes(a, 8)) == NULL)
  2470. goto failed;
  2471. switch (type) {
  2472. case kCTime:
  2473. fileTimeToUtc(archive_le64dec(p),
  2474. &(entries[i].ctime),
  2475. &(entries[i].ctime_ns));
  2476. entries[i].flg |= CTIME_IS_SET;
  2477. break;
  2478. case kATime:
  2479. fileTimeToUtc(archive_le64dec(p),
  2480. &(entries[i].atime),
  2481. &(entries[i].atime_ns));
  2482. entries[i].flg |= ATIME_IS_SET;
  2483. break;
  2484. case kMTime:
  2485. fileTimeToUtc(archive_le64dec(p),
  2486. &(entries[i].mtime),
  2487. &(entries[i].mtime_ns));
  2488. entries[i].flg |= MTIME_IS_SET;
  2489. break;
  2490. }
  2491. }
  2492. free(timeBools);
  2493. return (0);
  2494. failed:
  2495. free(timeBools);
  2496. return (-1);
  2497. }
  2498. static int
  2499. decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
  2500. {
  2501. struct _7zip *zip = (struct _7zip *)a->format->data;
  2502. errno = 0;
  2503. if (read_StreamsInfo(a, si) < 0) {
  2504. if (errno == ENOMEM)
  2505. archive_set_error(&a->archive, -1,
  2506. "Couldn't allocate memory");
  2507. else
  2508. archive_set_error(&a->archive, -1,
  2509. "Malformed 7-Zip archive");
  2510. return (ARCHIVE_FATAL);
  2511. }
  2512. if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
  2513. archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
  2514. return (ARCHIVE_FATAL);
  2515. }
  2516. if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
  2517. (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
  2518. si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
  2519. archive_set_error(&a->archive, -1, "Malformed Header offset");
  2520. return (ARCHIVE_FATAL);
  2521. }
  2522. return (ARCHIVE_OK);
  2523. }
  2524. static const unsigned char *
  2525. header_bytes(struct archive_read *a, size_t rbytes)
  2526. {
  2527. struct _7zip *zip = (struct _7zip *)a->format->data;
  2528. const unsigned char *p;
  2529. if (zip->header_bytes_remaining < rbytes)
  2530. return (NULL);
  2531. if (zip->pack_stream_bytes_unconsumed)
  2532. read_consume(a);
  2533. if (zip->header_is_encoded == 0) {
  2534. p = __archive_read_ahead(a, rbytes, NULL);
  2535. if (p == NULL)
  2536. return (NULL);
  2537. zip->header_bytes_remaining -= rbytes;
  2538. zip->pack_stream_bytes_unconsumed = rbytes;
  2539. } else {
  2540. const void *buff;
  2541. ssize_t bytes;
  2542. bytes = read_stream(a, &buff, rbytes, rbytes);
  2543. if (bytes <= 0)
  2544. return (NULL);
  2545. zip->header_bytes_remaining -= bytes;
  2546. p = buff;
  2547. }
  2548. /* Update checksum */
  2549. zip->header_crc32 = crc32(zip->header_crc32, p, (unsigned)rbytes);
  2550. return (p);
  2551. }
  2552. static int
  2553. slurp_central_directory(struct archive_read *a, struct _7zip *zip,
  2554. struct _7z_header_info *header)
  2555. {
  2556. const unsigned char *p;
  2557. uint64_t next_header_offset;
  2558. uint64_t next_header_size;
  2559. uint32_t next_header_crc;
  2560. ssize_t bytes_avail;
  2561. int check_header_crc, r;
  2562. if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
  2563. return (ARCHIVE_FATAL);
  2564. if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
  2565. /* This is an executable ? Must be self-extracting... */
  2566. r = skip_sfx(a, bytes_avail);
  2567. if (r < ARCHIVE_WARN)
  2568. return (r);
  2569. if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
  2570. return (ARCHIVE_FATAL);
  2571. }
  2572. zip->seek_base += 32;
  2573. if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
  2574. archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
  2575. return (ARCHIVE_FATAL);
  2576. }
  2577. /* CRC check. */
  2578. if (crc32(0, (const unsigned char *)p + 12, 20)
  2579. != archive_le32dec(p + 8)) {
  2580. archive_set_error(&a->archive, -1, "Header CRC error");
  2581. return (ARCHIVE_FATAL);
  2582. }
  2583. next_header_offset = archive_le64dec(p + 12);
  2584. next_header_size = archive_le64dec(p + 20);
  2585. next_header_crc = archive_le32dec(p + 28);
  2586. if (next_header_size == 0)
  2587. /* There is no entry in an archive file. */
  2588. return (ARCHIVE_EOF);
  2589. if (((int64_t)next_header_offset) < 0) {
  2590. archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
  2591. return (ARCHIVE_FATAL);
  2592. }
  2593. __archive_read_consume(a, 32);
  2594. if (next_header_offset != 0) {
  2595. if (bytes_avail >= (ssize_t)next_header_offset)
  2596. __archive_read_consume(a, next_header_offset);
  2597. else if (__archive_read_seek(a,
  2598. next_header_offset + zip->seek_base, SEEK_SET) < 0)
  2599. return (ARCHIVE_FATAL);
  2600. }
  2601. zip->stream_offset = next_header_offset;
  2602. zip->header_offset = next_header_offset;
  2603. zip->header_bytes_remaining = next_header_size;
  2604. zip->header_crc32 = 0;
  2605. zip->header_is_encoded = 0;
  2606. zip->header_is_being_read = 1;
  2607. zip->has_encrypted_entries = 0;
  2608. check_header_crc = 1;
  2609. if ((p = header_bytes(a, 1)) == NULL) {
  2610. archive_set_error(&a->archive,
  2611. ARCHIVE_ERRNO_FILE_FORMAT,
  2612. "Truncated 7-Zip file body");
  2613. return (ARCHIVE_FATAL);
  2614. }
  2615. /* Parse ArchiveProperties. */
  2616. switch (p[0]) {
  2617. case kEncodedHeader:
  2618. /*
  2619. * The archive has an encoded header and we have to decode it
  2620. * in order to parse the header correctly.
  2621. */
  2622. r = decode_encoded_header_info(a, &(zip->si));
  2623. /* Check the EncodedHeader CRC.*/
  2624. if (r == 0 && zip->header_crc32 != next_header_crc) {
  2625. archive_set_error(&a->archive, -1,
  2626. "Damaged 7-Zip archive");
  2627. r = -1;
  2628. }
  2629. if (r == 0) {
  2630. if (zip->si.ci.folders[0].digest_defined)
  2631. next_header_crc = zip->si.ci.folders[0].digest;
  2632. else
  2633. check_header_crc = 0;
  2634. if (zip->pack_stream_bytes_unconsumed)
  2635. read_consume(a);
  2636. r = setup_decode_folder(a, zip->si.ci.folders, 1);
  2637. if (r == 0) {
  2638. zip->header_bytes_remaining =
  2639. zip->folder_outbytes_remaining;
  2640. r = seek_pack(a);
  2641. }
  2642. }
  2643. /* Clean up StreamsInfo. */
  2644. free_StreamsInfo(&(zip->si));
  2645. memset(&(zip->si), 0, sizeof(zip->si));
  2646. if (r < 0)
  2647. return (ARCHIVE_FATAL);
  2648. zip->header_is_encoded = 1;
  2649. zip->header_crc32 = 0;
  2650. /* FALL THROUGH */
  2651. case kHeader:
  2652. /*
  2653. * Parse the header.
  2654. */
  2655. errno = 0;
  2656. r = read_Header(a, header, zip->header_is_encoded);
  2657. if (r < 0) {
  2658. if (errno == ENOMEM)
  2659. archive_set_error(&a->archive, -1,
  2660. "Couldn't allocate memory");
  2661. else
  2662. archive_set_error(&a->archive, -1,
  2663. "Damaged 7-Zip archive");
  2664. return (ARCHIVE_FATAL);
  2665. }
  2666. /*
  2667. * Must be kEnd.
  2668. */
  2669. if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
  2670. archive_set_error(&a->archive, -1,
  2671. "Malformed 7-Zip archive");
  2672. return (ARCHIVE_FATAL);
  2673. }
  2674. /* Check the Header CRC.*/
  2675. if (check_header_crc && zip->header_crc32 != next_header_crc) {
  2676. archive_set_error(&a->archive, -1,
  2677. "Malformed 7-Zip archive");
  2678. return (ARCHIVE_FATAL);
  2679. }
  2680. break;
  2681. default:
  2682. archive_set_error(&a->archive, -1,
  2683. "Unexpected Property ID = %X", p[0]);
  2684. return (ARCHIVE_FATAL);
  2685. }
  2686. /* Clean up variables be used for decoding the archive header */
  2687. zip->pack_stream_remaining = 0;
  2688. zip->pack_stream_index = 0;
  2689. zip->folder_outbytes_remaining = 0;
  2690. zip->uncompressed_buffer_bytes_remaining = 0;
  2691. zip->pack_stream_bytes_unconsumed = 0;
  2692. zip->header_is_being_read = 0;
  2693. return (ARCHIVE_OK);
  2694. }
  2695. static ssize_t
  2696. get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
  2697. size_t minimum)
  2698. {
  2699. struct _7zip *zip = (struct _7zip *)a->format->data;
  2700. ssize_t bytes_avail;
  2701. if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
  2702. /* Copy mode. */
  2703. /*
  2704. * Note: '1' here is a performance optimization.
  2705. * Recall that the decompression layer returns a count of
  2706. * available bytes; asking for more than that forces the
  2707. * decompressor to combine reads by copying data.
  2708. */
  2709. *buff = __archive_read_ahead(a, 1, &bytes_avail);
  2710. if (bytes_avail <= 0) {
  2711. archive_set_error(&a->archive,
  2712. ARCHIVE_ERRNO_FILE_FORMAT,
  2713. "Truncated 7-Zip file data");
  2714. return (ARCHIVE_FATAL);
  2715. }
  2716. if ((size_t)bytes_avail >
  2717. zip->uncompressed_buffer_bytes_remaining)
  2718. bytes_avail = (ssize_t)
  2719. zip->uncompressed_buffer_bytes_remaining;
  2720. if ((size_t)bytes_avail > size)
  2721. bytes_avail = (ssize_t)size;
  2722. zip->pack_stream_bytes_unconsumed = bytes_avail;
  2723. } else if (zip->uncompressed_buffer_pointer == NULL) {
  2724. /* Decompression has failed. */
  2725. archive_set_error(&(a->archive),
  2726. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2727. return (ARCHIVE_FATAL);
  2728. } else {
  2729. /* Packed mode. */
  2730. if (minimum > zip->uncompressed_buffer_bytes_remaining) {
  2731. /*
  2732. * If remaining uncompressed data size is less than
  2733. * the minimum size, fill the buffer up to the
  2734. * minimum size.
  2735. */
  2736. if (extract_pack_stream(a, minimum) < 0)
  2737. return (ARCHIVE_FATAL);
  2738. }
  2739. if (size > zip->uncompressed_buffer_bytes_remaining)
  2740. bytes_avail = (ssize_t)
  2741. zip->uncompressed_buffer_bytes_remaining;
  2742. else
  2743. bytes_avail = (ssize_t)size;
  2744. *buff = zip->uncompressed_buffer_pointer;
  2745. zip->uncompressed_buffer_pointer += bytes_avail;
  2746. }
  2747. zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
  2748. return (bytes_avail);
  2749. }
  2750. static ssize_t
  2751. extract_pack_stream(struct archive_read *a, size_t minimum)
  2752. {
  2753. struct _7zip *zip = (struct _7zip *)a->format->data;
  2754. ssize_t bytes_avail;
  2755. int r;
  2756. if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
  2757. if (minimum == 0)
  2758. minimum = 1;
  2759. if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
  2760. || bytes_avail <= 0) {
  2761. archive_set_error(&a->archive,
  2762. ARCHIVE_ERRNO_FILE_FORMAT,
  2763. "Truncated 7-Zip file body");
  2764. return (ARCHIVE_FATAL);
  2765. }
  2766. if (bytes_avail > (ssize_t)zip->pack_stream_inbytes_remaining)
  2767. bytes_avail = (ssize_t)zip->pack_stream_inbytes_remaining;
  2768. zip->pack_stream_inbytes_remaining -= bytes_avail;
  2769. if (bytes_avail > (ssize_t)zip->folder_outbytes_remaining)
  2770. bytes_avail = (ssize_t)zip->folder_outbytes_remaining;
  2771. zip->folder_outbytes_remaining -= bytes_avail;
  2772. zip->uncompressed_buffer_bytes_remaining = bytes_avail;
  2773. return (ARCHIVE_OK);
  2774. }
  2775. /* If the buffer hasn't been allocated, allocate it now. */
  2776. if (zip->uncompressed_buffer == NULL) {
  2777. zip->uncompressed_buffer_size = UBUFF_SIZE;
  2778. if (zip->uncompressed_buffer_size < minimum) {
  2779. zip->uncompressed_buffer_size = minimum + 1023;
  2780. zip->uncompressed_buffer_size &= ~0x3ff;
  2781. }
  2782. zip->uncompressed_buffer =
  2783. malloc(zip->uncompressed_buffer_size);
  2784. if (zip->uncompressed_buffer == NULL) {
  2785. archive_set_error(&a->archive, ENOMEM,
  2786. "No memory for 7-Zip decompression");
  2787. return (ARCHIVE_FATAL);
  2788. }
  2789. zip->uncompressed_buffer_bytes_remaining = 0;
  2790. } else if (zip->uncompressed_buffer_size < minimum ||
  2791. zip->uncompressed_buffer_bytes_remaining < minimum) {
  2792. /*
  2793. * Make sure the uncompressed buffer can have bytes
  2794. * at least `minimum' bytes.
  2795. * NOTE: This case happen when reading the header.
  2796. */
  2797. size_t used;
  2798. if (zip->uncompressed_buffer_pointer != 0)
  2799. used = zip->uncompressed_buffer_pointer -
  2800. zip->uncompressed_buffer;
  2801. else
  2802. used = 0;
  2803. if (zip->uncompressed_buffer_size < minimum) {
  2804. /*
  2805. * Expand the uncompressed buffer up to
  2806. * the minimum size.
  2807. */
  2808. void *p;
  2809. size_t new_size;
  2810. new_size = minimum + 1023;
  2811. new_size &= ~0x3ff;
  2812. p = realloc(zip->uncompressed_buffer, new_size);
  2813. if (p == NULL) {
  2814. archive_set_error(&a->archive, ENOMEM,
  2815. "No memory for 7-Zip decompression");
  2816. return (ARCHIVE_FATAL);
  2817. }
  2818. zip->uncompressed_buffer = (unsigned char *)p;
  2819. zip->uncompressed_buffer_size = new_size;
  2820. }
  2821. /*
  2822. * Move unconsumed bytes to the head.
  2823. */
  2824. if (used) {
  2825. memmove(zip->uncompressed_buffer,
  2826. zip->uncompressed_buffer + used,
  2827. zip->uncompressed_buffer_bytes_remaining);
  2828. }
  2829. } else
  2830. zip->uncompressed_buffer_bytes_remaining = 0;
  2831. zip->uncompressed_buffer_pointer = NULL;
  2832. for (;;) {
  2833. size_t bytes_in, bytes_out;
  2834. const void *buff_in;
  2835. unsigned char *buff_out;
  2836. int end_of_data;
  2837. /*
  2838. * Note: '1' here is a performance optimization.
  2839. * Recall that the decompression layer returns a count of
  2840. * available bytes; asking for more than that forces the
  2841. * decompressor to combine reads by copying data.
  2842. */
  2843. buff_in = __archive_read_ahead(a, 1, &bytes_avail);
  2844. if (bytes_avail <= 0) {
  2845. archive_set_error(&a->archive,
  2846. ARCHIVE_ERRNO_FILE_FORMAT,
  2847. "Truncated 7-Zip file body");
  2848. return (ARCHIVE_FATAL);
  2849. }
  2850. buff_out = zip->uncompressed_buffer
  2851. + zip->uncompressed_buffer_bytes_remaining;
  2852. bytes_out = zip->uncompressed_buffer_size
  2853. - zip->uncompressed_buffer_bytes_remaining;
  2854. bytes_in = bytes_avail;
  2855. if (bytes_in > zip->pack_stream_inbytes_remaining)
  2856. bytes_in = (size_t)zip->pack_stream_inbytes_remaining;
  2857. /* Drive decompression. */
  2858. r = decompress(a, zip, buff_out, &bytes_out,
  2859. buff_in, &bytes_in);
  2860. switch (r) {
  2861. case ARCHIVE_OK:
  2862. end_of_data = 0;
  2863. break;
  2864. case ARCHIVE_EOF:
  2865. end_of_data = 1;
  2866. break;
  2867. default:
  2868. return (ARCHIVE_FATAL);
  2869. }
  2870. zip->pack_stream_inbytes_remaining -= bytes_in;
  2871. if (bytes_out > zip->folder_outbytes_remaining)
  2872. bytes_out = (size_t)zip->folder_outbytes_remaining;
  2873. zip->folder_outbytes_remaining -= bytes_out;
  2874. zip->uncompressed_buffer_bytes_remaining += bytes_out;
  2875. zip->pack_stream_bytes_unconsumed = bytes_in;
  2876. /*
  2877. * Continue decompression until uncompressed_buffer is full.
  2878. */
  2879. if (zip->uncompressed_buffer_bytes_remaining ==
  2880. zip->uncompressed_buffer_size)
  2881. break;
  2882. if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
  2883. zip->uncompressed_buffer_bytes_remaining + 5 >
  2884. zip->uncompressed_buffer_size)
  2885. break;
  2886. if (zip->pack_stream_inbytes_remaining == 0 &&
  2887. zip->folder_outbytes_remaining == 0)
  2888. break;
  2889. if (end_of_data || (bytes_in == 0 && bytes_out == 0)) {
  2890. archive_set_error(&(a->archive),
  2891. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2892. return (ARCHIVE_FATAL);
  2893. }
  2894. read_consume(a);
  2895. }
  2896. if (zip->uncompressed_buffer_bytes_remaining < minimum) {
  2897. archive_set_error(&(a->archive),
  2898. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2899. return (ARCHIVE_FATAL);
  2900. }
  2901. zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
  2902. return (ARCHIVE_OK);
  2903. }
  2904. static int
  2905. seek_pack(struct archive_read *a)
  2906. {
  2907. struct _7zip *zip = (struct _7zip *)a->format->data;
  2908. int64_t pack_offset;
  2909. if (zip->pack_stream_remaining <= 0) {
  2910. archive_set_error(&(a->archive),
  2911. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2912. return (ARCHIVE_FATAL);
  2913. }
  2914. zip->pack_stream_inbytes_remaining =
  2915. zip->si.pi.sizes[zip->pack_stream_index];
  2916. pack_offset = zip->si.pi.positions[zip->pack_stream_index];
  2917. if (zip->stream_offset != pack_offset) {
  2918. if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
  2919. SEEK_SET))
  2920. return (ARCHIVE_FATAL);
  2921. zip->stream_offset = pack_offset;
  2922. }
  2923. zip->pack_stream_index++;
  2924. zip->pack_stream_remaining--;
  2925. return (ARCHIVE_OK);
  2926. }
  2927. static ssize_t
  2928. read_stream(struct archive_read *a, const void **buff, size_t size,
  2929. size_t minimum)
  2930. {
  2931. struct _7zip *zip = (struct _7zip *)a->format->data;
  2932. uint64_t skip_bytes = 0;
  2933. ssize_t r;
  2934. if (zip->uncompressed_buffer_bytes_remaining == 0) {
  2935. if (zip->pack_stream_inbytes_remaining > 0) {
  2936. r = extract_pack_stream(a, 0);
  2937. if (r < 0)
  2938. return (r);
  2939. return (get_uncompressed_data(a, buff, size, minimum));
  2940. } else if (zip->folder_outbytes_remaining > 0) {
  2941. /* Extract a remaining pack stream. */
  2942. r = extract_pack_stream(a, 0);
  2943. if (r < 0)
  2944. return (r);
  2945. return (get_uncompressed_data(a, buff, size, minimum));
  2946. }
  2947. } else
  2948. return (get_uncompressed_data(a, buff, size, minimum));
  2949. /*
  2950. * Current pack stream has been consumed.
  2951. */
  2952. if (zip->pack_stream_remaining == 0) {
  2953. if (zip->header_is_being_read) {
  2954. /* Invalid sequence. This might happen when
  2955. * reading a malformed archive. */
  2956. archive_set_error(&(a->archive),
  2957. ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
  2958. return (ARCHIVE_FATAL);
  2959. }
  2960. /*
  2961. * All current folder's pack streams have been
  2962. * consumed. Switch to next folder.
  2963. */
  2964. if (zip->folder_index == 0 &&
  2965. (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
  2966. || zip->folder_index != zip->entry->folderIndex)) {
  2967. zip->folder_index = zip->entry->folderIndex;
  2968. skip_bytes =
  2969. zip->si.ci.folders[zip->folder_index].skipped_bytes;
  2970. }
  2971. if (zip->folder_index >= zip->si.ci.numFolders) {
  2972. /*
  2973. * We have consumed all folders and its pack streams.
  2974. */
  2975. *buff = NULL;
  2976. return (0);
  2977. }
  2978. r = setup_decode_folder(a,
  2979. &(zip->si.ci.folders[zip->folder_index]), 0);
  2980. if (r != ARCHIVE_OK)
  2981. return (ARCHIVE_FATAL);
  2982. zip->folder_index++;
  2983. }
  2984. /*
  2985. * Switch to next pack stream.
  2986. */
  2987. r = seek_pack(a);
  2988. if (r < 0)
  2989. return (r);
  2990. /* Extract a new pack stream. */
  2991. r = extract_pack_stream(a, 0);
  2992. if (r < 0)
  2993. return (r);
  2994. /*
  2995. * Skip the bytes we alrady has skipped in skip_stream().
  2996. */
  2997. while (skip_bytes) {
  2998. ssize_t skipped;
  2999. if (zip->uncompressed_buffer_bytes_remaining == 0) {
  3000. if (zip->pack_stream_inbytes_remaining > 0) {
  3001. r = extract_pack_stream(a, 0);
  3002. if (r < 0)
  3003. return (r);
  3004. } else if (zip->folder_outbytes_remaining > 0) {
  3005. /* Extract a remaining pack stream. */
  3006. r = extract_pack_stream(a, 0);
  3007. if (r < 0)
  3008. return (r);
  3009. } else {
  3010. archive_set_error(&a->archive,
  3011. ARCHIVE_ERRNO_FILE_FORMAT,
  3012. "Truncated 7-Zip file body");
  3013. return (ARCHIVE_FATAL);
  3014. }
  3015. }
  3016. skipped = get_uncompressed_data(
  3017. a, buff, (size_t)skip_bytes, 0);
  3018. if (skipped < 0)
  3019. return (skipped);
  3020. skip_bytes -= skipped;
  3021. if (zip->pack_stream_bytes_unconsumed)
  3022. read_consume(a);
  3023. }
  3024. return (get_uncompressed_data(a, buff, size, minimum));
  3025. }
  3026. static int
  3027. setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
  3028. int header)
  3029. {
  3030. struct _7zip *zip = (struct _7zip *)a->format->data;
  3031. const struct _7z_coder *coder1, *coder2;
  3032. const char *cname = (header)?"archive header":"file content";
  3033. unsigned i;
  3034. int r, found_bcj2 = 0;
  3035. /*
  3036. * Release the memory which the previous folder used for BCJ2.
  3037. */
  3038. for (i = 0; i < 3; i++) {
  3039. if (zip->sub_stream_buff[i] != NULL)
  3040. free(zip->sub_stream_buff[i]);
  3041. zip->sub_stream_buff[i] = NULL;
  3042. }
  3043. /*
  3044. * Initialize a stream reader.
  3045. */
  3046. zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
  3047. zip->pack_stream_index = (unsigned)folder->packIndex;
  3048. zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
  3049. zip->uncompressed_buffer_bytes_remaining = 0;
  3050. /*
  3051. * Check coder types.
  3052. */
  3053. for (i = 0; i < folder->numCoders; i++) {
  3054. switch(folder->coders[i].codec) {
  3055. case _7Z_CRYPTO_MAIN_ZIP:
  3056. case _7Z_CRYPTO_RAR_29:
  3057. case _7Z_CRYPTO_AES_256_SHA_256: {
  3058. /* For entry that is associated with this folder, mark
  3059. it as encrypted (data+metadata). */
  3060. zip->has_encrypted_entries = 1;
  3061. if (a->entry) {
  3062. archive_entry_set_is_data_encrypted(a->entry, 1);
  3063. archive_entry_set_is_metadata_encrypted(a->entry, 1);
  3064. }
  3065. archive_set_error(&(a->archive),
  3066. ARCHIVE_ERRNO_MISC,
  3067. "The %s is encrypted, "
  3068. "but currently not supported", cname);
  3069. return (ARCHIVE_FATAL);
  3070. }
  3071. case _7Z_X86_BCJ2: {
  3072. found_bcj2++;
  3073. break;
  3074. }
  3075. }
  3076. }
  3077. /* Now that we've checked for encryption, if there were still no
  3078. * encrypted entries found we can say for sure that there are none.
  3079. */
  3080. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  3081. zip->has_encrypted_entries = 0;
  3082. }
  3083. if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
  3084. archive_set_error(&(a->archive),
  3085. ARCHIVE_ERRNO_MISC,
  3086. "The %s is encoded with many filters, "
  3087. "but currently not supported", cname);
  3088. return (ARCHIVE_FATAL);
  3089. }
  3090. coder1 = &(folder->coders[0]);
  3091. if (folder->numCoders == 2)
  3092. coder2 = &(folder->coders[1]);
  3093. else
  3094. coder2 = NULL;
  3095. if (found_bcj2) {
  3096. /*
  3097. * Preparation to decode BCJ2.
  3098. * Decoding BCJ2 requires four sources. Those are at least,
  3099. * as far as I know, two types of the storage form.
  3100. */
  3101. const struct _7z_coder *fc = folder->coders;
  3102. static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
  3103. const struct _7z_coder *scoder[3] =
  3104. {&coder_copy, &coder_copy, &coder_copy};
  3105. const void *buff;
  3106. ssize_t bytes;
  3107. unsigned char *b[3] = {NULL, NULL, NULL};
  3108. uint64_t sunpack[3] ={-1, -1, -1};
  3109. size_t s[3] = {0, 0, 0};
  3110. int idx[3] = {0, 1, 2};
  3111. if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
  3112. folder->numInStreams == 7 && folder->numOutStreams == 4 &&
  3113. zip->pack_stream_remaining == 4) {
  3114. /* Source type 1 made by 7zr or 7z with -m options. */
  3115. if (folder->bindPairs[0].inIndex == 5) {
  3116. /* The form made by 7zr */
  3117. idx[0] = 1; idx[1] = 2; idx[2] = 0;
  3118. scoder[1] = &(fc[1]);
  3119. scoder[2] = &(fc[0]);
  3120. sunpack[1] = folder->unPackSize[1];
  3121. sunpack[2] = folder->unPackSize[0];
  3122. coder1 = &(fc[2]);
  3123. } else {
  3124. /*
  3125. * NOTE: Some patterns do not work.
  3126. * work:
  3127. * 7z a -m0=BCJ2 -m1=COPY -m2=COPY
  3128. * -m3=(any)
  3129. * 7z a -m0=BCJ2 -m1=COPY -m2=(any)
  3130. * -m3=COPY
  3131. * 7z a -m0=BCJ2 -m1=(any) -m2=COPY
  3132. * -m3=COPY
  3133. * not work:
  3134. * other patterns.
  3135. *
  3136. * We have to handle this like `pipe' or
  3137. * our libarchive7s filter frame work,
  3138. * decoding the BCJ2 main stream sequentially,
  3139. * m3 -> m2 -> m1 -> BCJ2.
  3140. *
  3141. */
  3142. if (fc[0].codec == _7Z_COPY &&
  3143. fc[1].codec == _7Z_COPY)
  3144. coder1 = &(folder->coders[2]);
  3145. else if (fc[0].codec == _7Z_COPY &&
  3146. fc[2].codec == _7Z_COPY)
  3147. coder1 = &(folder->coders[1]);
  3148. else if (fc[1].codec == _7Z_COPY &&
  3149. fc[2].codec == _7Z_COPY)
  3150. coder1 = &(folder->coders[0]);
  3151. else {
  3152. archive_set_error(&(a->archive),
  3153. ARCHIVE_ERRNO_MISC,
  3154. "Unsupported form of "
  3155. "BCJ2 streams");
  3156. return (ARCHIVE_FATAL);
  3157. }
  3158. }
  3159. coder2 = &(fc[3]);
  3160. zip->main_stream_bytes_remaining =
  3161. (size_t)folder->unPackSize[2];
  3162. } else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
  3163. zip->pack_stream_remaining == 4 &&
  3164. folder->numInStreams == 5 && folder->numOutStreams == 2) {
  3165. /* Source type 0 made by 7z */
  3166. zip->main_stream_bytes_remaining =
  3167. (size_t)folder->unPackSize[0];
  3168. } else {
  3169. /* We got an unexpected form. */
  3170. archive_set_error(&(a->archive),
  3171. ARCHIVE_ERRNO_MISC,
  3172. "Unsupported form of BCJ2 streams");
  3173. return (ARCHIVE_FATAL);
  3174. }
  3175. /* Skip the main stream at this time. */
  3176. if ((r = seek_pack(a)) < 0)
  3177. return (r);
  3178. zip->pack_stream_bytes_unconsumed =
  3179. (size_t)zip->pack_stream_inbytes_remaining;
  3180. read_consume(a);
  3181. /* Read following three sub streams. */
  3182. for (i = 0; i < 3; i++) {
  3183. const struct _7z_coder *coder = scoder[i];
  3184. if ((r = seek_pack(a)) < 0) {
  3185. free(b[0]); free(b[1]); free(b[2]);
  3186. return (r);
  3187. }
  3188. if (sunpack[i] == (uint64_t)-1)
  3189. zip->folder_outbytes_remaining =
  3190. zip->pack_stream_inbytes_remaining;
  3191. else
  3192. zip->folder_outbytes_remaining = sunpack[i];
  3193. r = init_decompression(a, zip, coder, NULL);
  3194. if (r != ARCHIVE_OK) {
  3195. free(b[0]); free(b[1]); free(b[2]);
  3196. return (ARCHIVE_FATAL);
  3197. }
  3198. /* Allocate memory for the decorded data of a sub
  3199. * stream. */
  3200. b[i] = malloc((size_t)zip->folder_outbytes_remaining);
  3201. if (b[i] == NULL) {
  3202. free(b[0]); free(b[1]); free(b[2]);
  3203. archive_set_error(&a->archive, ENOMEM,
  3204. "No memory for 7-Zip decompression");
  3205. return (ARCHIVE_FATAL);
  3206. }
  3207. /* Extract a sub stream. */
  3208. while (zip->pack_stream_inbytes_remaining > 0) {
  3209. r = (int)extract_pack_stream(a, 0);
  3210. if (r < 0) {
  3211. free(b[0]); free(b[1]); free(b[2]);
  3212. return (r);
  3213. }
  3214. bytes = get_uncompressed_data(a, &buff,
  3215. zip->uncompressed_buffer_bytes_remaining,
  3216. 0);
  3217. if (bytes < 0) {
  3218. free(b[0]); free(b[1]); free(b[2]);
  3219. return ((int)bytes);
  3220. }
  3221. memcpy(b[i]+s[i], buff, bytes);
  3222. s[i] += bytes;
  3223. if (zip->pack_stream_bytes_unconsumed)
  3224. read_consume(a);
  3225. }
  3226. }
  3227. /* Set the sub streams to the right place. */
  3228. for (i = 0; i < 3; i++) {
  3229. zip->sub_stream_buff[i] = b[idx[i]];
  3230. zip->sub_stream_size[i] = s[idx[i]];
  3231. zip->sub_stream_bytes_remaining[i] = s[idx[i]];
  3232. }
  3233. /* Allocate memory used for decoded main stream bytes. */
  3234. if (zip->tmp_stream_buff == NULL) {
  3235. zip->tmp_stream_buff_size = 32 * 1024;
  3236. zip->tmp_stream_buff =
  3237. malloc(zip->tmp_stream_buff_size);
  3238. if (zip->tmp_stream_buff == NULL) {
  3239. archive_set_error(&a->archive, ENOMEM,
  3240. "No memory for 7-Zip decompression");
  3241. return (ARCHIVE_FATAL);
  3242. }
  3243. }
  3244. zip->tmp_stream_bytes_avail = 0;
  3245. zip->tmp_stream_bytes_remaining = 0;
  3246. zip->odd_bcj_size = 0;
  3247. zip->bcj2_outPos = 0;
  3248. /*
  3249. * Reset a stream reader in order to read the main stream
  3250. * of BCJ2.
  3251. */
  3252. zip->pack_stream_remaining = 1;
  3253. zip->pack_stream_index = (unsigned)folder->packIndex;
  3254. zip->folder_outbytes_remaining =
  3255. folder_uncompressed_size(folder);
  3256. zip->uncompressed_buffer_bytes_remaining = 0;
  3257. }
  3258. /*
  3259. * Initialize the decompressor for the new folder's pack streams.
  3260. */
  3261. r = init_decompression(a, zip, coder1, coder2);
  3262. if (r != ARCHIVE_OK)
  3263. return (ARCHIVE_FATAL);
  3264. return (ARCHIVE_OK);
  3265. }
  3266. static int64_t
  3267. skip_stream(struct archive_read *a, size_t skip_bytes)
  3268. {
  3269. struct _7zip *zip = (struct _7zip *)a->format->data;
  3270. const void *p;
  3271. int64_t skipped_bytes;
  3272. size_t bytes = skip_bytes;
  3273. if (zip->folder_index == 0) {
  3274. /*
  3275. * Optimization for a list mode.
  3276. * Avoid unncecessary decoding operations.
  3277. */
  3278. zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
  3279. += skip_bytes;
  3280. return (skip_bytes);
  3281. }
  3282. while (bytes) {
  3283. skipped_bytes = read_stream(a, &p, bytes, 0);
  3284. if (skipped_bytes < 0)
  3285. return (skipped_bytes);
  3286. if (skipped_bytes == 0) {
  3287. archive_set_error(&a->archive,
  3288. ARCHIVE_ERRNO_FILE_FORMAT,
  3289. "Truncated 7-Zip file body");
  3290. return (ARCHIVE_FATAL);
  3291. }
  3292. bytes -= (size_t)skipped_bytes;
  3293. if (zip->pack_stream_bytes_unconsumed)
  3294. read_consume(a);
  3295. }
  3296. return (skip_bytes);
  3297. }
  3298. /*
  3299. * Brought from LZMA SDK.
  3300. *
  3301. * Bra86.c -- Converter for x86 code (BCJ)
  3302. * 2008-10-04 : Igor Pavlov : Public domain
  3303. *
  3304. */
  3305. #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
  3306. static void
  3307. x86_Init(struct _7zip *zip)
  3308. {
  3309. zip->bcj_state = 0;
  3310. zip->bcj_prevPosT = (size_t)0 - 1;
  3311. zip->bcj_prevMask = 0;
  3312. zip->bcj_ip = 5;
  3313. }
  3314. static size_t
  3315. x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
  3316. {
  3317. static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
  3318. static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
  3319. size_t bufferPos, prevPosT;
  3320. uint32_t ip, prevMask;
  3321. if (size < 5)
  3322. return 0;
  3323. bufferPos = 0;
  3324. prevPosT = zip->bcj_prevPosT;
  3325. prevMask = zip->bcj_prevMask;
  3326. ip = zip->bcj_ip;
  3327. for (;;) {
  3328. uint8_t *p = data + bufferPos;
  3329. uint8_t *limit = data + size - 4;
  3330. for (; p < limit; p++)
  3331. if ((*p & 0xFE) == 0xE8)
  3332. break;
  3333. bufferPos = (size_t)(p - data);
  3334. if (p >= limit)
  3335. break;
  3336. prevPosT = bufferPos - prevPosT;
  3337. if (prevPosT > 3)
  3338. prevMask = 0;
  3339. else {
  3340. prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
  3341. if (prevMask != 0) {
  3342. unsigned char b =
  3343. p[4 - kMaskToBitNumber[prevMask]];
  3344. if (!kMaskToAllowedStatus[prevMask] ||
  3345. Test86MSByte(b)) {
  3346. prevPosT = bufferPos;
  3347. prevMask = ((prevMask << 1) & 0x7) | 1;
  3348. bufferPos++;
  3349. continue;
  3350. }
  3351. }
  3352. }
  3353. prevPosT = bufferPos;
  3354. if (Test86MSByte(p[4])) {
  3355. uint32_t src = ((uint32_t)p[4] << 24) |
  3356. ((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
  3357. ((uint32_t)p[1]);
  3358. uint32_t dest;
  3359. for (;;) {
  3360. uint8_t b;
  3361. int b_index;
  3362. dest = src - (ip + (uint32_t)bufferPos);
  3363. if (prevMask == 0)
  3364. break;
  3365. b_index = kMaskToBitNumber[prevMask] * 8;
  3366. b = (uint8_t)(dest >> (24 - b_index));
  3367. if (!Test86MSByte(b))
  3368. break;
  3369. src = dest ^ ((1 << (32 - b_index)) - 1);
  3370. }
  3371. p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
  3372. p[3] = (uint8_t)(dest >> 16);
  3373. p[2] = (uint8_t)(dest >> 8);
  3374. p[1] = (uint8_t)dest;
  3375. bufferPos += 5;
  3376. } else {
  3377. prevMask = ((prevMask << 1) & 0x7) | 1;
  3378. bufferPos++;
  3379. }
  3380. }
  3381. zip->bcj_prevPosT = prevPosT;
  3382. zip->bcj_prevMask = prevMask;
  3383. zip->bcj_ip += (uint32_t)bufferPos;
  3384. return (bufferPos);
  3385. }
  3386. /*
  3387. * Brought from LZMA SDK.
  3388. *
  3389. * Bcj2.c -- Converter for x86 code (BCJ2)
  3390. * 2008-10-04 : Igor Pavlov : Public domain
  3391. *
  3392. */
  3393. #define SZ_ERROR_DATA ARCHIVE_FAILED
  3394. #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
  3395. #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
  3396. #define kNumTopBits 24
  3397. #define kTopValue ((uint32_t)1 << kNumTopBits)
  3398. #define kNumBitModelTotalBits 11
  3399. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  3400. #define kNumMoveBits 5
  3401. #define RC_READ_BYTE (*buffer++)
  3402. #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
  3403. #define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
  3404. { int ii; for (ii = 0; ii < 5; ii++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
  3405. #define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
  3406. #define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
  3407. #define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
  3408. #define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
  3409. static ssize_t
  3410. Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
  3411. {
  3412. size_t inPos = 0, outPos = 0;
  3413. const uint8_t *buf0, *buf1, *buf2, *buf3;
  3414. size_t size0, size1, size2, size3;
  3415. const uint8_t *buffer, *bufferLim;
  3416. unsigned int i, j;
  3417. size0 = zip->tmp_stream_bytes_remaining;
  3418. buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
  3419. size1 = zip->sub_stream_bytes_remaining[0];
  3420. buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
  3421. size2 = zip->sub_stream_bytes_remaining[1];
  3422. buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
  3423. size3 = zip->sub_stream_bytes_remaining[2];
  3424. buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
  3425. buffer = buf3;
  3426. bufferLim = buffer + size3;
  3427. if (zip->bcj_state == 0) {
  3428. /*
  3429. * Initialize.
  3430. */
  3431. zip->bcj2_prevByte = 0;
  3432. for (i = 0;
  3433. i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
  3434. zip->bcj2_p[i] = kBitModelTotal >> 1;
  3435. RC_INIT2;
  3436. zip->bcj_state = 1;
  3437. }
  3438. /*
  3439. * Gather the odd bytes of a previous call.
  3440. */
  3441. for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
  3442. outBuf[outPos++] = zip->odd_bcj[i];
  3443. zip->odd_bcj_size--;
  3444. }
  3445. if (outSize == 0) {
  3446. zip->bcj2_outPos += outPos;
  3447. return (outPos);
  3448. }
  3449. for (;;) {
  3450. uint8_t b;
  3451. CProb *prob;
  3452. uint32_t bound;
  3453. uint32_t ttt;
  3454. size_t limit = size0 - inPos;
  3455. if (outSize - outPos < limit)
  3456. limit = outSize - outPos;
  3457. if (zip->bcj_state == 1) {
  3458. while (limit != 0) {
  3459. uint8_t bb = buf0[inPos];
  3460. outBuf[outPos++] = bb;
  3461. if (IsJ(zip->bcj2_prevByte, bb)) {
  3462. zip->bcj_state = 2;
  3463. break;
  3464. }
  3465. inPos++;
  3466. zip->bcj2_prevByte = bb;
  3467. limit--;
  3468. }
  3469. }
  3470. if (limit == 0 || outPos == outSize)
  3471. break;
  3472. zip->bcj_state = 1;
  3473. b = buf0[inPos++];
  3474. if (b == 0xE8)
  3475. prob = zip->bcj2_p + zip->bcj2_prevByte;
  3476. else if (b == 0xE9)
  3477. prob = zip->bcj2_p + 256;
  3478. else
  3479. prob = zip->bcj2_p + 257;
  3480. IF_BIT_0(prob) {
  3481. UPDATE_0(prob)
  3482. zip->bcj2_prevByte = b;
  3483. } else {
  3484. uint32_t dest;
  3485. const uint8_t *v;
  3486. uint8_t out[4];
  3487. UPDATE_1(prob)
  3488. if (b == 0xE8) {
  3489. v = buf1;
  3490. if (size1 < 4)
  3491. return SZ_ERROR_DATA;
  3492. buf1 += 4;
  3493. size1 -= 4;
  3494. } else {
  3495. v = buf2;
  3496. if (size2 < 4)
  3497. return SZ_ERROR_DATA;
  3498. buf2 += 4;
  3499. size2 -= 4;
  3500. }
  3501. dest = (((uint32_t)v[0] << 24) |
  3502. ((uint32_t)v[1] << 16) |
  3503. ((uint32_t)v[2] << 8) |
  3504. ((uint32_t)v[3])) -
  3505. ((uint32_t)zip->bcj2_outPos + (uint32_t)outPos + 4);
  3506. out[0] = (uint8_t)dest;
  3507. out[1] = (uint8_t)(dest >> 8);
  3508. out[2] = (uint8_t)(dest >> 16);
  3509. out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
  3510. for (i = 0; i < 4 && outPos < outSize; i++)
  3511. outBuf[outPos++] = out[i];
  3512. if (i < 4) {
  3513. /*
  3514. * Save odd bytes which we could not add into
  3515. * the output buffer because of out of space.
  3516. */
  3517. zip->odd_bcj_size = 4 -i;
  3518. for (; i < 4; i++) {
  3519. j = i - 4 + (unsigned)zip->odd_bcj_size;
  3520. zip->odd_bcj[j] = out[i];
  3521. }
  3522. break;
  3523. }
  3524. }
  3525. }
  3526. zip->tmp_stream_bytes_remaining -= inPos;
  3527. zip->sub_stream_bytes_remaining[0] = size1;
  3528. zip->sub_stream_bytes_remaining[1] = size2;
  3529. zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
  3530. zip->bcj2_outPos += outPos;
  3531. return ((ssize_t)outPos);
  3532. }