archive_read_support_format_7zip.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873
  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 points of the file contents
  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. /* Decoding LZMA1 and LZMA2 data. */
  235. #ifdef HAVE_LZMA_H
  236. lzma_stream lzstream;
  237. int lzstream_valid;
  238. #endif
  239. /* Decoding bzip2 data. */
  240. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  241. bz_stream bzstream;
  242. int bzstream_valid;
  243. #endif
  244. /* Decoding deflate data. */
  245. #ifdef HAVE_ZLIB_H
  246. z_stream stream;
  247. int stream_valid;
  248. #endif
  249. /* Decoding 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. * already 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 symname, 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 Byte
  874. ppmd_read(void *p)
  875. {
  876. struct archive_read *a = ((IByteIn*)p)->a;
  877. struct _7zip *zip = (struct _7zip *)(a->format->data);
  878. Byte b;
  879. if (zip->ppstream.avail_in == 0) {
  880. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  881. "Truncated RAR file data");
  882. zip->ppstream.overconsumed = 1;
  883. return (0);
  884. }
  885. b = *zip->ppstream.next_in++;
  886. zip->ppstream.avail_in--;
  887. zip->ppstream.total_in++;
  888. return (b);
  889. }
  890. static int
  891. init_decompression(struct archive_read *a, struct _7zip *zip,
  892. const struct _7z_coder *coder1, const struct _7z_coder *coder2)
  893. {
  894. int r;
  895. zip->codec = coder1->codec;
  896. zip->codec2 = -1;
  897. switch (zip->codec) {
  898. case _7Z_COPY:
  899. case _7Z_BZ2:
  900. case _7Z_DEFLATE:
  901. case _7Z_PPMD:
  902. if (coder2 != NULL) {
  903. if (coder2->codec != _7Z_X86 &&
  904. coder2->codec != _7Z_X86_BCJ2) {
  905. archive_set_error(&a->archive,
  906. ARCHIVE_ERRNO_MISC,
  907. "Unsupported filter %lx for %lx",
  908. coder2->codec, coder1->codec);
  909. return (ARCHIVE_FAILED);
  910. }
  911. zip->codec2 = coder2->codec;
  912. zip->bcj_state = 0;
  913. if (coder2->codec == _7Z_X86)
  914. x86_Init(zip);
  915. }
  916. break;
  917. default:
  918. break;
  919. }
  920. switch (zip->codec) {
  921. case _7Z_COPY:
  922. break;
  923. case _7Z_LZMA: case _7Z_LZMA2:
  924. #ifdef HAVE_LZMA_H
  925. #if LZMA_VERSION_MAJOR >= 5
  926. /* Effectively disable the limiter. */
  927. #define LZMA_MEMLIMIT UINT64_MAX
  928. #else
  929. /* NOTE: This needs to check memory size which running system has. */
  930. #define LZMA_MEMLIMIT (1U << 30)
  931. #endif
  932. {
  933. lzma_options_delta delta_opt;
  934. lzma_filter filters[LZMA_FILTERS_MAX], *ff;
  935. int fi = 0;
  936. if (zip->lzstream_valid) {
  937. lzma_end(&(zip->lzstream));
  938. zip->lzstream_valid = 0;
  939. }
  940. /*
  941. * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
  942. * data made by 7-Zip because 7-Zip does not add End-Of-
  943. * Payload Marker(EOPM) at the end of LZMA compressed data,
  944. * and so liblzma cannot know the end of the compressed data
  945. * without EOPM. So consequently liblzma will not return last
  946. * three or four bytes of uncompressed data because
  947. * LZMA_FILTER_X86 filter does not handle input data if its
  948. * data size is less than five bytes. If liblzma detect EOPM
  949. * or know the uncompressed data size, liblzma will flush out
  950. * the remaining that three or four bytes of uncompressed
  951. * data. That is why we have to use our converting program
  952. * for BCJ+LZMA. If we were able to tell the uncompressed
  953. * size to liblzma when using lzma_raw_decoder() liblzma
  954. * could correctly deal with BCJ+LZMA. But unfortunately
  955. * there is no way to do that.
  956. * Discussion about this can be found at XZ Utils forum.
  957. */
  958. if (coder2 != NULL) {
  959. zip->codec2 = coder2->codec;
  960. filters[fi].options = NULL;
  961. switch (zip->codec2) {
  962. case _7Z_X86:
  963. if (zip->codec == _7Z_LZMA2) {
  964. filters[fi].id = LZMA_FILTER_X86;
  965. fi++;
  966. } else
  967. /* Use our filter. */
  968. x86_Init(zip);
  969. break;
  970. case _7Z_X86_BCJ2:
  971. /* Use our filter. */
  972. zip->bcj_state = 0;
  973. break;
  974. case _7Z_DELTA:
  975. if (coder2->propertiesSize != 1) {
  976. archive_set_error(&a->archive,
  977. ARCHIVE_ERRNO_MISC,
  978. "Invalid Delta parameter");
  979. return (ARCHIVE_FAILED);
  980. }
  981. filters[fi].id = LZMA_FILTER_DELTA;
  982. memset(&delta_opt, 0, sizeof(delta_opt));
  983. delta_opt.type = LZMA_DELTA_TYPE_BYTE;
  984. delta_opt.dist =
  985. (uint32_t)coder2->properties[0] + 1;
  986. filters[fi].options = &delta_opt;
  987. fi++;
  988. break;
  989. /* Following filters have not been tested yet. */
  990. case _7Z_POWERPC:
  991. filters[fi].id = LZMA_FILTER_POWERPC;
  992. fi++;
  993. break;
  994. case _7Z_IA64:
  995. filters[fi].id = LZMA_FILTER_IA64;
  996. fi++;
  997. break;
  998. case _7Z_ARM:
  999. filters[fi].id = LZMA_FILTER_ARM;
  1000. fi++;
  1001. break;
  1002. case _7Z_ARMTHUMB:
  1003. filters[fi].id = LZMA_FILTER_ARMTHUMB;
  1004. fi++;
  1005. break;
  1006. case _7Z_SPARC:
  1007. filters[fi].id = LZMA_FILTER_SPARC;
  1008. fi++;
  1009. break;
  1010. default:
  1011. archive_set_error(&a->archive,
  1012. ARCHIVE_ERRNO_MISC,
  1013. "Unexpected codec ID: %lX", zip->codec2);
  1014. return (ARCHIVE_FAILED);
  1015. }
  1016. }
  1017. if (zip->codec == _7Z_LZMA2)
  1018. filters[fi].id = LZMA_FILTER_LZMA2;
  1019. else
  1020. filters[fi].id = LZMA_FILTER_LZMA1;
  1021. filters[fi].options = NULL;
  1022. ff = &filters[fi];
  1023. r = lzma_properties_decode(&filters[fi], NULL,
  1024. coder1->properties, (size_t)coder1->propertiesSize);
  1025. if (r != LZMA_OK) {
  1026. set_error(a, r);
  1027. return (ARCHIVE_FAILED);
  1028. }
  1029. fi++;
  1030. filters[fi].id = LZMA_VLI_UNKNOWN;
  1031. filters[fi].options = NULL;
  1032. r = lzma_raw_decoder(&(zip->lzstream), filters);
  1033. free(ff->options);
  1034. if (r != LZMA_OK) {
  1035. set_error(a, r);
  1036. return (ARCHIVE_FAILED);
  1037. }
  1038. zip->lzstream_valid = 1;
  1039. zip->lzstream.total_in = 0;
  1040. zip->lzstream.total_out = 0;
  1041. break;
  1042. }
  1043. #else
  1044. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1045. "LZMA codec is unsupported");
  1046. return (ARCHIVE_FAILED);
  1047. #endif
  1048. case _7Z_BZ2:
  1049. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1050. if (zip->bzstream_valid) {
  1051. BZ2_bzDecompressEnd(&(zip->bzstream));
  1052. zip->bzstream_valid = 0;
  1053. }
  1054. r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
  1055. if (r == BZ_MEM_ERROR)
  1056. r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
  1057. if (r != BZ_OK) {
  1058. int err = ARCHIVE_ERRNO_MISC;
  1059. const char *detail = NULL;
  1060. switch (r) {
  1061. case BZ_PARAM_ERROR:
  1062. detail = "invalid setup parameter";
  1063. break;
  1064. case BZ_MEM_ERROR:
  1065. err = ENOMEM;
  1066. detail = "out of memory";
  1067. break;
  1068. case BZ_CONFIG_ERROR:
  1069. detail = "mis-compiled library";
  1070. break;
  1071. }
  1072. archive_set_error(&a->archive, err,
  1073. "Internal error initializing decompressor: %s",
  1074. detail != NULL ? detail : "??");
  1075. zip->bzstream_valid = 0;
  1076. return (ARCHIVE_FAILED);
  1077. }
  1078. zip->bzstream_valid = 1;
  1079. zip->bzstream.total_in_lo32 = 0;
  1080. zip->bzstream.total_in_hi32 = 0;
  1081. zip->bzstream.total_out_lo32 = 0;
  1082. zip->bzstream.total_out_hi32 = 0;
  1083. break;
  1084. #else
  1085. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1086. "BZ2 codec is unsupported");
  1087. return (ARCHIVE_FAILED);
  1088. #endif
  1089. case _7Z_DEFLATE:
  1090. #ifdef HAVE_ZLIB_H
  1091. if (zip->stream_valid)
  1092. r = inflateReset(&(zip->stream));
  1093. else
  1094. r = inflateInit2(&(zip->stream),
  1095. -15 /* Don't check for zlib header */);
  1096. if (r != Z_OK) {
  1097. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1098. "Couldn't initialize zlib stream.");
  1099. return (ARCHIVE_FAILED);
  1100. }
  1101. zip->stream_valid = 1;
  1102. zip->stream.total_in = 0;
  1103. zip->stream.total_out = 0;
  1104. break;
  1105. #else
  1106. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1107. "DEFLATE codec is unsupported");
  1108. return (ARCHIVE_FAILED);
  1109. #endif
  1110. case _7Z_PPMD:
  1111. {
  1112. unsigned order;
  1113. uint32_t msize;
  1114. if (zip->ppmd7_valid) {
  1115. __archive_ppmd7_functions.Ppmd7_Free(
  1116. &zip->ppmd7_context);
  1117. zip->ppmd7_valid = 0;
  1118. }
  1119. if (coder1->propertiesSize < 5) {
  1120. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1121. "Malformed PPMd parameter");
  1122. return (ARCHIVE_FAILED);
  1123. }
  1124. order = coder1->properties[0];
  1125. msize = archive_le32dec(&(coder1->properties[1]));
  1126. if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
  1127. msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
  1128. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1129. "Malformed PPMd parameter");
  1130. return (ARCHIVE_FAILED);
  1131. }
  1132. __archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
  1133. r = __archive_ppmd7_functions.Ppmd7_Alloc(
  1134. &zip->ppmd7_context, msize);
  1135. if (r == 0) {
  1136. archive_set_error(&a->archive, ENOMEM,
  1137. "Coludn't allocate memory for PPMd");
  1138. return (ARCHIVE_FATAL);
  1139. }
  1140. __archive_ppmd7_functions.Ppmd7_Init(
  1141. &zip->ppmd7_context, order);
  1142. __archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
  1143. &zip->range_dec);
  1144. zip->ppmd7_valid = 1;
  1145. zip->ppmd7_stat = 0;
  1146. zip->ppstream.overconsumed = 0;
  1147. zip->ppstream.total_in = 0;
  1148. zip->ppstream.total_out = 0;
  1149. break;
  1150. }
  1151. case _7Z_X86:
  1152. case _7Z_X86_BCJ2:
  1153. case _7Z_POWERPC:
  1154. case _7Z_IA64:
  1155. case _7Z_ARM:
  1156. case _7Z_ARMTHUMB:
  1157. case _7Z_SPARC:
  1158. case _7Z_DELTA:
  1159. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1160. "Unexpected codec ID: %lX", zip->codec);
  1161. return (ARCHIVE_FAILED);
  1162. case _7Z_CRYPTO_MAIN_ZIP:
  1163. case _7Z_CRYPTO_RAR_29:
  1164. case _7Z_CRYPTO_AES_256_SHA_256:
  1165. if (a->entry) {
  1166. archive_entry_set_is_metadata_encrypted(a->entry, 1);
  1167. archive_entry_set_is_data_encrypted(a->entry, 1);
  1168. zip->has_encrypted_entries = 1;
  1169. }
  1170. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1171. "Crypto codec not supported yet (ID: 0x%lX)", zip->codec);
  1172. return (ARCHIVE_FAILED);
  1173. default:
  1174. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1175. "Unknown codec ID: %lX", zip->codec);
  1176. return (ARCHIVE_FAILED);
  1177. }
  1178. return (ARCHIVE_OK);
  1179. }
  1180. static int
  1181. decompress(struct archive_read *a, struct _7zip *zip,
  1182. void *buff, size_t *outbytes, const void *b, size_t *used)
  1183. {
  1184. const uint8_t *t_next_in;
  1185. uint8_t *t_next_out;
  1186. size_t o_avail_in, o_avail_out;
  1187. size_t t_avail_in, t_avail_out;
  1188. uint8_t *bcj2_next_out;
  1189. size_t bcj2_avail_out;
  1190. int r, ret = ARCHIVE_OK;
  1191. t_avail_in = o_avail_in = *used;
  1192. t_avail_out = o_avail_out = *outbytes;
  1193. t_next_in = b;
  1194. t_next_out = buff;
  1195. if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
  1196. int i;
  1197. /* Do not copy out the BCJ remaining bytes when the output
  1198. * buffer size is less than five bytes. */
  1199. if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
  1200. *used = 0;
  1201. *outbytes = 0;
  1202. return (ret);
  1203. }
  1204. for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
  1205. *t_next_out++ = zip->odd_bcj[i];
  1206. t_avail_out--;
  1207. zip->odd_bcj_size--;
  1208. }
  1209. if (o_avail_in == 0 || t_avail_out == 0) {
  1210. *used = o_avail_in - t_avail_in;
  1211. *outbytes = o_avail_out - t_avail_out;
  1212. if (o_avail_in == 0)
  1213. ret = ARCHIVE_EOF;
  1214. return (ret);
  1215. }
  1216. }
  1217. bcj2_next_out = t_next_out;
  1218. bcj2_avail_out = t_avail_out;
  1219. if (zip->codec2 == _7Z_X86_BCJ2) {
  1220. /*
  1221. * Decord a remaining decompressed main stream for BCJ2.
  1222. */
  1223. if (zip->tmp_stream_bytes_remaining) {
  1224. ssize_t bytes;
  1225. size_t remaining = zip->tmp_stream_bytes_remaining;
  1226. bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
  1227. if (bytes < 0) {
  1228. archive_set_error(&(a->archive),
  1229. ARCHIVE_ERRNO_MISC,
  1230. "BCJ2 conversion Failed");
  1231. return (ARCHIVE_FAILED);
  1232. }
  1233. zip->main_stream_bytes_remaining -=
  1234. remaining - zip->tmp_stream_bytes_remaining;
  1235. t_avail_out -= bytes;
  1236. if (o_avail_in == 0 || t_avail_out == 0) {
  1237. *used = 0;
  1238. *outbytes = o_avail_out - t_avail_out;
  1239. if (o_avail_in == 0 &&
  1240. zip->tmp_stream_bytes_remaining)
  1241. ret = ARCHIVE_EOF;
  1242. return (ret);
  1243. }
  1244. t_next_out += bytes;
  1245. bcj2_next_out = t_next_out;
  1246. bcj2_avail_out = t_avail_out;
  1247. }
  1248. t_next_out = zip->tmp_stream_buff;
  1249. t_avail_out = zip->tmp_stream_buff_size;
  1250. }
  1251. switch (zip->codec) {
  1252. case _7Z_COPY:
  1253. {
  1254. size_t bytes =
  1255. (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
  1256. memcpy(t_next_out, t_next_in, bytes);
  1257. t_avail_in -= bytes;
  1258. t_avail_out -= bytes;
  1259. if (o_avail_in == 0)
  1260. ret = ARCHIVE_EOF;
  1261. break;
  1262. }
  1263. #ifdef HAVE_LZMA_H
  1264. case _7Z_LZMA: case _7Z_LZMA2:
  1265. zip->lzstream.next_in = t_next_in;
  1266. zip->lzstream.avail_in = t_avail_in;
  1267. zip->lzstream.next_out = t_next_out;
  1268. zip->lzstream.avail_out = t_avail_out;
  1269. r = lzma_code(&(zip->lzstream), LZMA_RUN);
  1270. switch (r) {
  1271. case LZMA_STREAM_END: /* Found end of stream. */
  1272. lzma_end(&(zip->lzstream));
  1273. zip->lzstream_valid = 0;
  1274. ret = ARCHIVE_EOF;
  1275. break;
  1276. case LZMA_OK: /* Decompressor made some progress. */
  1277. break;
  1278. default:
  1279. archive_set_error(&(a->archive),
  1280. ARCHIVE_ERRNO_MISC,
  1281. "Decompression failed(%d)",
  1282. r);
  1283. return (ARCHIVE_FAILED);
  1284. }
  1285. t_avail_in = zip->lzstream.avail_in;
  1286. t_avail_out = zip->lzstream.avail_out;
  1287. break;
  1288. #endif
  1289. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1290. case _7Z_BZ2:
  1291. zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
  1292. zip->bzstream.avail_in = t_avail_in;
  1293. zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
  1294. zip->bzstream.avail_out = t_avail_out;
  1295. r = BZ2_bzDecompress(&(zip->bzstream));
  1296. switch (r) {
  1297. case BZ_STREAM_END: /* Found end of stream. */
  1298. switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
  1299. case BZ_OK:
  1300. break;
  1301. default:
  1302. archive_set_error(&(a->archive),
  1303. ARCHIVE_ERRNO_MISC,
  1304. "Failed to clean up decompressor");
  1305. return (ARCHIVE_FAILED);
  1306. }
  1307. zip->bzstream_valid = 0;
  1308. ret = ARCHIVE_EOF;
  1309. break;
  1310. case BZ_OK: /* Decompressor made some progress. */
  1311. break;
  1312. default:
  1313. archive_set_error(&(a->archive),
  1314. ARCHIVE_ERRNO_MISC,
  1315. "bzip decompression failed");
  1316. return (ARCHIVE_FAILED);
  1317. }
  1318. t_avail_in = zip->bzstream.avail_in;
  1319. t_avail_out = zip->bzstream.avail_out;
  1320. break;
  1321. #endif
  1322. #ifdef HAVE_ZLIB_H
  1323. case _7Z_DEFLATE:
  1324. zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
  1325. zip->stream.avail_in = (uInt)t_avail_in;
  1326. zip->stream.next_out = t_next_out;
  1327. zip->stream.avail_out = (uInt)t_avail_out;
  1328. r = inflate(&(zip->stream), 0);
  1329. switch (r) {
  1330. case Z_STREAM_END: /* Found end of stream. */
  1331. ret = ARCHIVE_EOF;
  1332. break;
  1333. case Z_OK: /* Decompressor made some progress.*/
  1334. break;
  1335. default:
  1336. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1337. "File decompression failed (%d)", r);
  1338. return (ARCHIVE_FAILED);
  1339. }
  1340. t_avail_in = zip->stream.avail_in;
  1341. t_avail_out = zip->stream.avail_out;
  1342. break;
  1343. #endif
  1344. case _7Z_PPMD:
  1345. {
  1346. uint64_t flush_bytes;
  1347. if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
  1348. t_avail_out <= 0) {
  1349. archive_set_error(&(a->archive),
  1350. ARCHIVE_ERRNO_MISC,
  1351. "Decompression internal error");
  1352. return (ARCHIVE_FAILED);
  1353. }
  1354. zip->ppstream.next_in = t_next_in;
  1355. zip->ppstream.avail_in = t_avail_in;
  1356. zip->ppstream.next_out = t_next_out;
  1357. zip->ppstream.avail_out = t_avail_out;
  1358. if (zip->ppmd7_stat == 0) {
  1359. zip->bytein.a = a;
  1360. zip->bytein.Read = &ppmd_read;
  1361. zip->range_dec.Stream = &zip->bytein;
  1362. r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
  1363. &(zip->range_dec));
  1364. if (r == 0) {
  1365. zip->ppmd7_stat = -1;
  1366. archive_set_error(&a->archive,
  1367. ARCHIVE_ERRNO_MISC,
  1368. "Failed to initialize PPMd range decorder");
  1369. return (ARCHIVE_FAILED);
  1370. }
  1371. if (zip->ppstream.overconsumed) {
  1372. zip->ppmd7_stat = -1;
  1373. return (ARCHIVE_FAILED);
  1374. }
  1375. zip->ppmd7_stat = 1;
  1376. }
  1377. if (t_avail_in == 0)
  1378. /* XXX Flush out remaining decoded data XXX */
  1379. flush_bytes = zip->folder_outbytes_remaining;
  1380. else
  1381. flush_bytes = 0;
  1382. do {
  1383. int sym;
  1384. sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1385. &(zip->ppmd7_context), &(zip->range_dec.p));
  1386. if (sym < 0) {
  1387. zip->ppmd7_stat = -1;
  1388. archive_set_error(&a->archive,
  1389. ARCHIVE_ERRNO_FILE_FORMAT,
  1390. "Failed to decode PPMd");
  1391. return (ARCHIVE_FAILED);
  1392. }
  1393. if (zip->ppstream.overconsumed) {
  1394. zip->ppmd7_stat = -1;
  1395. return (ARCHIVE_FAILED);
  1396. }
  1397. *zip->ppstream.next_out++ = (unsigned char)sym;
  1398. zip->ppstream.avail_out--;
  1399. zip->ppstream.total_out++;
  1400. if (flush_bytes)
  1401. flush_bytes--;
  1402. } while (zip->ppstream.avail_out &&
  1403. (zip->ppstream.avail_in || flush_bytes));
  1404. t_avail_in = (size_t)zip->ppstream.avail_in;
  1405. t_avail_out = (size_t)zip->ppstream.avail_out;
  1406. break;
  1407. }
  1408. default:
  1409. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  1410. "Decompression internal error");
  1411. return (ARCHIVE_FAILED);
  1412. }
  1413. if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
  1414. return (ret);
  1415. *used = o_avail_in - t_avail_in;
  1416. *outbytes = o_avail_out - t_avail_out;
  1417. /*
  1418. * Decord BCJ.
  1419. */
  1420. if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
  1421. size_t l = x86_Convert(zip, buff, *outbytes);
  1422. zip->odd_bcj_size = *outbytes - l;
  1423. if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
  1424. o_avail_in && ret != ARCHIVE_EOF) {
  1425. memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
  1426. zip->odd_bcj_size);
  1427. *outbytes = l;
  1428. } else
  1429. zip->odd_bcj_size = 0;
  1430. }
  1431. /*
  1432. * Decord BCJ2 with a decompressed main stream.
  1433. */
  1434. if (zip->codec2 == _7Z_X86_BCJ2) {
  1435. ssize_t bytes;
  1436. zip->tmp_stream_bytes_avail =
  1437. zip->tmp_stream_buff_size - t_avail_out;
  1438. if (zip->tmp_stream_bytes_avail >
  1439. zip->main_stream_bytes_remaining)
  1440. zip->tmp_stream_bytes_avail =
  1441. zip->main_stream_bytes_remaining;
  1442. zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
  1443. bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
  1444. if (bytes < 0) {
  1445. archive_set_error(&(a->archive),
  1446. ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
  1447. return (ARCHIVE_FAILED);
  1448. }
  1449. zip->main_stream_bytes_remaining -=
  1450. zip->tmp_stream_bytes_avail
  1451. - zip->tmp_stream_bytes_remaining;
  1452. bcj2_avail_out -= bytes;
  1453. *outbytes = o_avail_out - bcj2_avail_out;
  1454. }
  1455. return (ret);
  1456. }
  1457. static int
  1458. free_decompression(struct archive_read *a, struct _7zip *zip)
  1459. {
  1460. int r = ARCHIVE_OK;
  1461. #if !defined(HAVE_ZLIB_H) &&\
  1462. !(defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR))
  1463. (void)a;/* UNUSED */
  1464. #endif
  1465. #ifdef HAVE_LZMA_H
  1466. if (zip->lzstream_valid)
  1467. lzma_end(&(zip->lzstream));
  1468. #endif
  1469. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1470. if (zip->bzstream_valid) {
  1471. if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
  1472. archive_set_error(&a->archive,
  1473. ARCHIVE_ERRNO_MISC,
  1474. "Failed to clean up bzip2 decompressor");
  1475. r = ARCHIVE_FATAL;
  1476. }
  1477. zip->bzstream_valid = 0;
  1478. }
  1479. #endif
  1480. #ifdef HAVE_ZLIB_H
  1481. if (zip->stream_valid) {
  1482. if (inflateEnd(&(zip->stream)) != Z_OK) {
  1483. archive_set_error(&a->archive,
  1484. ARCHIVE_ERRNO_MISC,
  1485. "Failed to clean up zlib decompressor");
  1486. r = ARCHIVE_FATAL;
  1487. }
  1488. zip->stream_valid = 0;
  1489. }
  1490. #endif
  1491. if (zip->ppmd7_valid) {
  1492. __archive_ppmd7_functions.Ppmd7_Free(
  1493. &zip->ppmd7_context);
  1494. zip->ppmd7_valid = 0;
  1495. }
  1496. return (r);
  1497. }
  1498. static int
  1499. parse_7zip_uint64(struct archive_read *a, uint64_t *val)
  1500. {
  1501. const unsigned char *p;
  1502. unsigned char avail, mask;
  1503. int i;
  1504. if ((p = header_bytes(a, 1)) == NULL)
  1505. return (-1);
  1506. avail = *p;
  1507. mask = 0x80;
  1508. *val = 0;
  1509. for (i = 0; i < 8; i++) {
  1510. if (avail & mask) {
  1511. if ((p = header_bytes(a, 1)) == NULL)
  1512. return (-1);
  1513. *val |= ((uint64_t)*p) << (8 * i);
  1514. mask >>= 1;
  1515. continue;
  1516. }
  1517. *val += ((uint64_t)(avail & (mask -1))) << (8 * i);
  1518. break;
  1519. }
  1520. return (0);
  1521. }
  1522. static int
  1523. read_Bools(struct archive_read *a, unsigned char *data, size_t num)
  1524. {
  1525. const unsigned char *p;
  1526. unsigned i, mask = 0, avail = 0;
  1527. for (i = 0; i < num; i++) {
  1528. if (mask == 0) {
  1529. if ((p = header_bytes(a, 1)) == NULL)
  1530. return (-1);
  1531. avail = *p;
  1532. mask = 0x80;
  1533. }
  1534. data[i] = (avail & mask)?1:0;
  1535. mask >>= 1;
  1536. }
  1537. return (0);
  1538. }
  1539. static void
  1540. free_Digest(struct _7z_digests *d)
  1541. {
  1542. free(d->defineds);
  1543. free(d->digests);
  1544. }
  1545. static int
  1546. read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
  1547. {
  1548. const unsigned char *p;
  1549. unsigned i;
  1550. if (num == 0)
  1551. return (-1);
  1552. memset(d, 0, sizeof(*d));
  1553. d->defineds = malloc(num);
  1554. if (d->defineds == NULL)
  1555. return (-1);
  1556. /*
  1557. * Read Bools.
  1558. */
  1559. if ((p = header_bytes(a, 1)) == NULL)
  1560. return (-1);
  1561. if (*p == 0) {
  1562. if (read_Bools(a, d->defineds, num) < 0)
  1563. return (-1);
  1564. } else
  1565. /* All are defined */
  1566. memset(d->defineds, 1, num);
  1567. d->digests = calloc(num, sizeof(*d->digests));
  1568. if (d->digests == NULL)
  1569. return (-1);
  1570. for (i = 0; i < num; i++) {
  1571. if (d->defineds[i]) {
  1572. if ((p = header_bytes(a, 4)) == NULL)
  1573. return (-1);
  1574. d->digests[i] = archive_le32dec(p);
  1575. }
  1576. }
  1577. return (0);
  1578. }
  1579. static void
  1580. free_PackInfo(struct _7z_pack_info *pi)
  1581. {
  1582. free(pi->sizes);
  1583. free(pi->positions);
  1584. free_Digest(&(pi->digest));
  1585. }
  1586. static int
  1587. read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
  1588. {
  1589. const unsigned char *p;
  1590. unsigned i;
  1591. memset(pi, 0, sizeof(*pi));
  1592. /*
  1593. * Read PackPos.
  1594. */
  1595. if (parse_7zip_uint64(a, &(pi->pos)) < 0)
  1596. return (-1);
  1597. /*
  1598. * Read NumPackStreams.
  1599. */
  1600. if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
  1601. return (-1);
  1602. if (pi->numPackStreams == 0)
  1603. return (-1);
  1604. if (UMAX_ENTRY < pi->numPackStreams)
  1605. return (-1);
  1606. /*
  1607. * Read PackSizes[num]
  1608. */
  1609. if ((p = header_bytes(a, 1)) == NULL)
  1610. return (-1);
  1611. if (*p == kEnd)
  1612. /* PackSizes[num] are not present. */
  1613. return (0);
  1614. if (*p != kSize)
  1615. return (-1);
  1616. pi->sizes = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
  1617. pi->positions = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
  1618. if (pi->sizes == NULL || pi->positions == NULL)
  1619. return (-1);
  1620. for (i = 0; i < pi->numPackStreams; i++) {
  1621. if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
  1622. return (-1);
  1623. }
  1624. /*
  1625. * Read PackStreamDigests[num]
  1626. */
  1627. if ((p = header_bytes(a, 1)) == NULL)
  1628. return (-1);
  1629. if (*p == kEnd) {
  1630. /* PackStreamDigests[num] are not present. */
  1631. pi->digest.defineds =
  1632. calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.defineds));
  1633. pi->digest.digests =
  1634. calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.digests));
  1635. if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
  1636. return (-1);
  1637. return (0);
  1638. }
  1639. if (*p != kCRC)
  1640. return (-1);
  1641. if (read_Digests(a, &(pi->digest), (size_t)pi->numPackStreams) < 0)
  1642. return (-1);
  1643. /*
  1644. * Must be marked by kEnd.
  1645. */
  1646. if ((p = header_bytes(a, 1)) == NULL)
  1647. return (-1);
  1648. if (*p != kEnd)
  1649. return (-1);
  1650. return (0);
  1651. }
  1652. static void
  1653. free_Folder(struct _7z_folder *f)
  1654. {
  1655. unsigned i;
  1656. if (f->coders) {
  1657. for (i = 0; i< f->numCoders; i++) {
  1658. free(f->coders[i].properties);
  1659. }
  1660. free(f->coders);
  1661. }
  1662. free(f->bindPairs);
  1663. free(f->packedStreams);
  1664. free(f->unPackSize);
  1665. }
  1666. static int
  1667. read_Folder(struct archive_read *a, struct _7z_folder *f)
  1668. {
  1669. struct _7zip *zip = (struct _7zip *)a->format->data;
  1670. const unsigned char *p;
  1671. uint64_t numInStreamsTotal = 0;
  1672. uint64_t numOutStreamsTotal = 0;
  1673. unsigned i;
  1674. memset(f, 0, sizeof(*f));
  1675. /*
  1676. * Read NumCoders.
  1677. */
  1678. if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
  1679. return (-1);
  1680. if (f->numCoders > 4)
  1681. /* Too many coders. */
  1682. return (-1);
  1683. f->coders = calloc((size_t)f->numCoders, sizeof(*f->coders));
  1684. if (f->coders == NULL)
  1685. return (-1);
  1686. for (i = 0; i< f->numCoders; i++) {
  1687. size_t codec_size;
  1688. int simple, attr;
  1689. if ((p = header_bytes(a, 1)) == NULL)
  1690. return (-1);
  1691. /*
  1692. * 0:3 CodecIdSize
  1693. * 4: 0 - IsSimple
  1694. * 1 - Is not Simple
  1695. * 5: 0 - No Attributes
  1696. * 1 - There are Attributes;
  1697. * 7: Must be zero.
  1698. */
  1699. codec_size = *p & 0xf;
  1700. simple = (*p & 0x10)?0:1;
  1701. attr = *p & 0x20;
  1702. if (*p & 0x80)
  1703. return (-1);/* Not supported. */
  1704. /*
  1705. * Read Decompression Method IDs.
  1706. */
  1707. if ((p = header_bytes(a, codec_size)) == NULL)
  1708. return (-1);
  1709. f->coders[i].codec = decode_codec_id(p, codec_size);
  1710. if (simple) {
  1711. f->coders[i].numInStreams = 1;
  1712. f->coders[i].numOutStreams = 1;
  1713. } else {
  1714. if (parse_7zip_uint64(
  1715. a, &(f->coders[i].numInStreams)) < 0)
  1716. return (-1);
  1717. if (UMAX_ENTRY < f->coders[i].numInStreams)
  1718. return (-1);
  1719. if (parse_7zip_uint64(
  1720. a, &(f->coders[i].numOutStreams)) < 0)
  1721. return (-1);
  1722. if (UMAX_ENTRY < f->coders[i].numOutStreams)
  1723. return (-1);
  1724. }
  1725. if (attr) {
  1726. if (parse_7zip_uint64(
  1727. a, &(f->coders[i].propertiesSize)) < 0)
  1728. return (-1);
  1729. if ((p = header_bytes(
  1730. a, (size_t)f->coders[i].propertiesSize)) == NULL)
  1731. return (-1);
  1732. f->coders[i].properties =
  1733. malloc((size_t)f->coders[i].propertiesSize);
  1734. if (f->coders[i].properties == NULL)
  1735. return (-1);
  1736. memcpy(f->coders[i].properties, p,
  1737. (size_t)f->coders[i].propertiesSize);
  1738. }
  1739. numInStreamsTotal += f->coders[i].numInStreams;
  1740. numOutStreamsTotal += f->coders[i].numOutStreams;
  1741. }
  1742. if (numOutStreamsTotal == 0 ||
  1743. numInStreamsTotal < numOutStreamsTotal-1)
  1744. return (-1);
  1745. f->numBindPairs = numOutStreamsTotal - 1;
  1746. if (zip->header_bytes_remaining < f->numBindPairs)
  1747. return (-1);
  1748. if (f->numBindPairs > 0) {
  1749. f->bindPairs =
  1750. calloc((size_t)f->numBindPairs, sizeof(*f->bindPairs));
  1751. if (f->bindPairs == NULL)
  1752. return (-1);
  1753. } else
  1754. f->bindPairs = NULL;
  1755. for (i = 0; i < f->numBindPairs; i++) {
  1756. if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
  1757. return (-1);
  1758. if (UMAX_ENTRY < f->bindPairs[i].inIndex)
  1759. return (-1);
  1760. if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
  1761. return (-1);
  1762. if (UMAX_ENTRY < f->bindPairs[i].outIndex)
  1763. return (-1);
  1764. }
  1765. f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
  1766. f->packedStreams =
  1767. calloc((size_t)f->numPackedStreams, sizeof(*f->packedStreams));
  1768. if (f->packedStreams == NULL)
  1769. return (-1);
  1770. if (f->numPackedStreams == 1) {
  1771. for (i = 0; i < numInStreamsTotal; i++) {
  1772. unsigned j;
  1773. for (j = 0; j < f->numBindPairs; j++) {
  1774. if (f->bindPairs[j].inIndex == i)
  1775. break;
  1776. }
  1777. if (j == f->numBindPairs)
  1778. break;
  1779. }
  1780. if (i == numInStreamsTotal)
  1781. return (-1);
  1782. f->packedStreams[0] = i;
  1783. } else {
  1784. for (i = 0; i < f->numPackedStreams; i++) {
  1785. if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
  1786. return (-1);
  1787. if (UMAX_ENTRY < f->packedStreams[i])
  1788. return (-1);
  1789. }
  1790. }
  1791. f->numInStreams = numInStreamsTotal;
  1792. f->numOutStreams = numOutStreamsTotal;
  1793. return (0);
  1794. }
  1795. static void
  1796. free_CodersInfo(struct _7z_coders_info *ci)
  1797. {
  1798. unsigned i;
  1799. if (ci->folders) {
  1800. for (i = 0; i < ci->numFolders; i++)
  1801. free_Folder(&(ci->folders[i]));
  1802. free(ci->folders);
  1803. }
  1804. }
  1805. static int
  1806. read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
  1807. {
  1808. const unsigned char *p;
  1809. struct _7z_digests digest;
  1810. unsigned i;
  1811. memset(ci, 0, sizeof(*ci));
  1812. memset(&digest, 0, sizeof(digest));
  1813. if ((p = header_bytes(a, 1)) == NULL)
  1814. goto failed;
  1815. if (*p != kFolder)
  1816. goto failed;
  1817. /*
  1818. * Read NumFolders.
  1819. */
  1820. if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
  1821. goto failed;
  1822. if (UMAX_ENTRY < ci->numFolders)
  1823. return (-1);
  1824. /*
  1825. * Read External.
  1826. */
  1827. if ((p = header_bytes(a, 1)) == NULL)
  1828. goto failed;
  1829. switch (*p) {
  1830. case 0:
  1831. ci->folders =
  1832. calloc((size_t)ci->numFolders, sizeof(*ci->folders));
  1833. if (ci->folders == NULL)
  1834. return (-1);
  1835. for (i = 0; i < ci->numFolders; i++) {
  1836. if (read_Folder(a, &(ci->folders[i])) < 0)
  1837. goto failed;
  1838. }
  1839. break;
  1840. case 1:
  1841. if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
  1842. return (-1);
  1843. if (UMAX_ENTRY < ci->dataStreamIndex)
  1844. return (-1);
  1845. if (ci->numFolders > 0) {
  1846. archive_set_error(&a->archive, -1,
  1847. "Malformed 7-Zip archive");
  1848. goto failed;
  1849. }
  1850. break;
  1851. default:
  1852. archive_set_error(&a->archive, -1,
  1853. "Malformed 7-Zip archive");
  1854. goto failed;
  1855. }
  1856. if ((p = header_bytes(a, 1)) == NULL)
  1857. goto failed;
  1858. if (*p != kCodersUnPackSize)
  1859. goto failed;
  1860. for (i = 0; i < ci->numFolders; i++) {
  1861. struct _7z_folder *folder = &(ci->folders[i]);
  1862. unsigned j;
  1863. folder->unPackSize =
  1864. calloc((size_t)folder->numOutStreams, sizeof(*folder->unPackSize));
  1865. if (folder->unPackSize == NULL)
  1866. goto failed;
  1867. for (j = 0; j < folder->numOutStreams; j++) {
  1868. if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
  1869. goto failed;
  1870. }
  1871. }
  1872. /*
  1873. * Read CRCs.
  1874. */
  1875. if ((p = header_bytes(a, 1)) == NULL)
  1876. goto failed;
  1877. if (*p == kEnd)
  1878. return (0);
  1879. if (*p != kCRC)
  1880. goto failed;
  1881. if (read_Digests(a, &digest, (size_t)ci->numFolders) < 0)
  1882. goto failed;
  1883. for (i = 0; i < ci->numFolders; i++) {
  1884. ci->folders[i].digest_defined = digest.defineds[i];
  1885. ci->folders[i].digest = digest.digests[i];
  1886. }
  1887. /*
  1888. * Must be kEnd.
  1889. */
  1890. if ((p = header_bytes(a, 1)) == NULL)
  1891. goto failed;
  1892. if (*p != kEnd)
  1893. goto failed;
  1894. free_Digest(&digest);
  1895. return (0);
  1896. failed:
  1897. free_Digest(&digest);
  1898. return (-1);
  1899. }
  1900. static uint64_t
  1901. folder_uncompressed_size(struct _7z_folder *f)
  1902. {
  1903. int n = (int)f->numOutStreams;
  1904. unsigned pairs = (unsigned)f->numBindPairs;
  1905. while (--n >= 0) {
  1906. unsigned i;
  1907. for (i = 0; i < pairs; i++) {
  1908. if (f->bindPairs[i].outIndex == (uint64_t)n)
  1909. break;
  1910. }
  1911. if (i >= pairs)
  1912. return (f->unPackSize[n]);
  1913. }
  1914. return (0);
  1915. }
  1916. static void
  1917. free_SubStreamsInfo(struct _7z_substream_info *ss)
  1918. {
  1919. free(ss->unpackSizes);
  1920. free(ss->digestsDefined);
  1921. free(ss->digests);
  1922. }
  1923. static int
  1924. read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
  1925. struct _7z_folder *f, size_t numFolders)
  1926. {
  1927. const unsigned char *p;
  1928. uint64_t *usizes;
  1929. size_t unpack_streams;
  1930. int type;
  1931. unsigned i;
  1932. uint32_t numDigests;
  1933. memset(ss, 0, sizeof(*ss));
  1934. for (i = 0; i < numFolders; i++)
  1935. f[i].numUnpackStreams = 1;
  1936. if ((p = header_bytes(a, 1)) == NULL)
  1937. return (-1);
  1938. type = *p;
  1939. if (type == kNumUnPackStream) {
  1940. unpack_streams = 0;
  1941. for (i = 0; i < numFolders; i++) {
  1942. if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
  1943. return (-1);
  1944. if (UMAX_ENTRY < f[i].numUnpackStreams)
  1945. return (-1);
  1946. if (unpack_streams > SIZE_MAX - UMAX_ENTRY) {
  1947. return (-1);
  1948. }
  1949. unpack_streams += (size_t)f[i].numUnpackStreams;
  1950. }
  1951. if ((p = header_bytes(a, 1)) == NULL)
  1952. return (-1);
  1953. type = *p;
  1954. } else
  1955. unpack_streams = numFolders;
  1956. ss->unpack_streams = unpack_streams;
  1957. if (unpack_streams) {
  1958. ss->unpackSizes = calloc(unpack_streams,
  1959. sizeof(*ss->unpackSizes));
  1960. ss->digestsDefined = calloc(unpack_streams,
  1961. sizeof(*ss->digestsDefined));
  1962. ss->digests = calloc(unpack_streams,
  1963. sizeof(*ss->digests));
  1964. if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
  1965. ss->digests == NULL)
  1966. return (-1);
  1967. }
  1968. usizes = ss->unpackSizes;
  1969. for (i = 0; i < numFolders; i++) {
  1970. unsigned pack;
  1971. uint64_t sum;
  1972. if (f[i].numUnpackStreams == 0)
  1973. continue;
  1974. sum = 0;
  1975. if (type == kSize) {
  1976. for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
  1977. if (parse_7zip_uint64(a, usizes) < 0)
  1978. return (-1);
  1979. sum += *usizes++;
  1980. }
  1981. }
  1982. *usizes++ = folder_uncompressed_size(&f[i]) - sum;
  1983. }
  1984. if (type == kSize) {
  1985. if ((p = header_bytes(a, 1)) == NULL)
  1986. return (-1);
  1987. type = *p;
  1988. }
  1989. for (i = 0; i < unpack_streams; i++) {
  1990. ss->digestsDefined[i] = 0;
  1991. ss->digests[i] = 0;
  1992. }
  1993. numDigests = 0;
  1994. for (i = 0; i < numFolders; i++) {
  1995. if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
  1996. numDigests += (uint32_t)f[i].numUnpackStreams;
  1997. }
  1998. if (type == kCRC) {
  1999. struct _7z_digests tmpDigests;
  2000. unsigned char *digestsDefined = ss->digestsDefined;
  2001. uint32_t * digests = ss->digests;
  2002. int di = 0;
  2003. memset(&tmpDigests, 0, sizeof(tmpDigests));
  2004. if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
  2005. free_Digest(&tmpDigests);
  2006. return (-1);
  2007. }
  2008. for (i = 0; i < numFolders; i++) {
  2009. if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
  2010. *digestsDefined++ = 1;
  2011. *digests++ = f[i].digest;
  2012. } else {
  2013. unsigned j;
  2014. for (j = 0; j < f[i].numUnpackStreams;
  2015. j++, di++) {
  2016. *digestsDefined++ =
  2017. tmpDigests.defineds[di];
  2018. *digests++ =
  2019. tmpDigests.digests[di];
  2020. }
  2021. }
  2022. }
  2023. free_Digest(&tmpDigests);
  2024. if ((p = header_bytes(a, 1)) == NULL)
  2025. return (-1);
  2026. type = *p;
  2027. }
  2028. /*
  2029. * Must be kEnd.
  2030. */
  2031. if (type != kEnd)
  2032. return (-1);
  2033. return (0);
  2034. }
  2035. static void
  2036. free_StreamsInfo(struct _7z_stream_info *si)
  2037. {
  2038. free_PackInfo(&(si->pi));
  2039. free_CodersInfo(&(si->ci));
  2040. free_SubStreamsInfo(&(si->ss));
  2041. }
  2042. static int
  2043. read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
  2044. {
  2045. struct _7zip *zip = (struct _7zip *)a->format->data;
  2046. const unsigned char *p;
  2047. unsigned i;
  2048. memset(si, 0, sizeof(*si));
  2049. if ((p = header_bytes(a, 1)) == NULL)
  2050. return (-1);
  2051. if (*p == kPackInfo) {
  2052. uint64_t packPos;
  2053. if (read_PackInfo(a, &(si->pi)) < 0)
  2054. return (-1);
  2055. if (si->pi.positions == NULL || si->pi.sizes == NULL)
  2056. return (-1);
  2057. /*
  2058. * Calculate packed stream positions.
  2059. */
  2060. packPos = si->pi.pos;
  2061. for (i = 0; i < si->pi.numPackStreams; i++) {
  2062. si->pi.positions[i] = packPos;
  2063. packPos += si->pi.sizes[i];
  2064. if (packPos > zip->header_offset)
  2065. return (-1);
  2066. }
  2067. if ((p = header_bytes(a, 1)) == NULL)
  2068. return (-1);
  2069. }
  2070. if (*p == kUnPackInfo) {
  2071. uint32_t packIndex;
  2072. struct _7z_folder *f;
  2073. if (read_CodersInfo(a, &(si->ci)) < 0)
  2074. return (-1);
  2075. /*
  2076. * Calculate packed stream indexes.
  2077. */
  2078. packIndex = 0;
  2079. f = si->ci.folders;
  2080. for (i = 0; i < si->ci.numFolders; i++) {
  2081. f[i].packIndex = packIndex;
  2082. packIndex += (uint32_t)f[i].numPackedStreams;
  2083. if (packIndex > si->pi.numPackStreams)
  2084. return (-1);
  2085. }
  2086. if ((p = header_bytes(a, 1)) == NULL)
  2087. return (-1);
  2088. }
  2089. if (*p == kSubStreamsInfo) {
  2090. if (read_SubStreamsInfo(a, &(si->ss),
  2091. si->ci.folders, (size_t)si->ci.numFolders) < 0)
  2092. return (-1);
  2093. if ((p = header_bytes(a, 1)) == NULL)
  2094. return (-1);
  2095. }
  2096. /*
  2097. * Must be kEnd.
  2098. */
  2099. if (*p != kEnd)
  2100. return (-1);
  2101. return (0);
  2102. }
  2103. static void
  2104. free_Header(struct _7z_header_info *h)
  2105. {
  2106. free(h->emptyStreamBools);
  2107. free(h->emptyFileBools);
  2108. free(h->antiBools);
  2109. free(h->attrBools);
  2110. }
  2111. static int
  2112. read_Header(struct archive_read *a, struct _7z_header_info *h,
  2113. int check_header_id)
  2114. {
  2115. struct _7zip *zip = (struct _7zip *)a->format->data;
  2116. const unsigned char *p;
  2117. struct _7z_folder *folders;
  2118. struct _7z_stream_info *si = &(zip->si);
  2119. struct _7zip_entry *entries;
  2120. uint32_t folderIndex, indexInFolder;
  2121. unsigned i;
  2122. int eindex, empty_streams, sindex;
  2123. if (check_header_id) {
  2124. /*
  2125. * Read Header.
  2126. */
  2127. if ((p = header_bytes(a, 1)) == NULL)
  2128. return (-1);
  2129. if (*p != kHeader)
  2130. return (-1);
  2131. }
  2132. /*
  2133. * Read ArchiveProperties.
  2134. */
  2135. if ((p = header_bytes(a, 1)) == NULL)
  2136. return (-1);
  2137. if (*p == kArchiveProperties) {
  2138. for (;;) {
  2139. uint64_t size;
  2140. if ((p = header_bytes(a, 1)) == NULL)
  2141. return (-1);
  2142. if (*p == 0)
  2143. break;
  2144. if (parse_7zip_uint64(a, &size) < 0)
  2145. return (-1);
  2146. }
  2147. if ((p = header_bytes(a, 1)) == NULL)
  2148. return (-1);
  2149. }
  2150. /*
  2151. * Read MainStreamsInfo.
  2152. */
  2153. if (*p == kMainStreamsInfo) {
  2154. if (read_StreamsInfo(a, &(zip->si)) < 0)
  2155. return (-1);
  2156. if ((p = header_bytes(a, 1)) == NULL)
  2157. return (-1);
  2158. }
  2159. if (*p == kEnd)
  2160. return (0);
  2161. /*
  2162. * Read FilesInfo.
  2163. */
  2164. if (*p != kFilesInfo)
  2165. return (-1);
  2166. if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
  2167. return (-1);
  2168. if (UMAX_ENTRY < zip->numFiles)
  2169. return (-1);
  2170. zip->entries = calloc((size_t)zip->numFiles, sizeof(*zip->entries));
  2171. if (zip->entries == NULL)
  2172. return (-1);
  2173. entries = zip->entries;
  2174. empty_streams = 0;
  2175. for (;;) {
  2176. int type;
  2177. uint64_t size;
  2178. size_t ll;
  2179. if ((p = header_bytes(a, 1)) == NULL)
  2180. return (-1);
  2181. type = *p;
  2182. if (type == kEnd)
  2183. break;
  2184. if (parse_7zip_uint64(a, &size) < 0)
  2185. return (-1);
  2186. if (zip->header_bytes_remaining < size)
  2187. return (-1);
  2188. ll = (size_t)size;
  2189. switch (type) {
  2190. case kEmptyStream:
  2191. if (h->emptyStreamBools != NULL)
  2192. return (-1);
  2193. h->emptyStreamBools = calloc((size_t)zip->numFiles,
  2194. sizeof(*h->emptyStreamBools));
  2195. if (h->emptyStreamBools == NULL)
  2196. return (-1);
  2197. if (read_Bools(
  2198. a, h->emptyStreamBools, (size_t)zip->numFiles) < 0)
  2199. return (-1);
  2200. empty_streams = 0;
  2201. for (i = 0; i < zip->numFiles; i++) {
  2202. if (h->emptyStreamBools[i])
  2203. empty_streams++;
  2204. }
  2205. break;
  2206. case kEmptyFile:
  2207. if (empty_streams <= 0) {
  2208. /* Unexcepted sequence. Skip this. */
  2209. if (header_bytes(a, ll) == NULL)
  2210. return (-1);
  2211. break;
  2212. }
  2213. if (h->emptyFileBools != NULL)
  2214. return (-1);
  2215. h->emptyFileBools = calloc(empty_streams,
  2216. sizeof(*h->emptyFileBools));
  2217. if (h->emptyFileBools == NULL)
  2218. return (-1);
  2219. if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
  2220. return (-1);
  2221. break;
  2222. case kAnti:
  2223. if (empty_streams <= 0) {
  2224. /* Unexcepted sequence. Skip this. */
  2225. if (header_bytes(a, ll) == NULL)
  2226. return (-1);
  2227. break;
  2228. }
  2229. if (h->antiBools != NULL)
  2230. return (-1);
  2231. h->antiBools = calloc(empty_streams,
  2232. sizeof(*h->antiBools));
  2233. if (h->antiBools == NULL)
  2234. return (-1);
  2235. if (read_Bools(a, h->antiBools, empty_streams) < 0)
  2236. return (-1);
  2237. break;
  2238. case kCTime:
  2239. case kATime:
  2240. case kMTime:
  2241. if (read_Times(a, h, type) < 0)
  2242. return (-1);
  2243. break;
  2244. case kName:
  2245. {
  2246. unsigned char *np;
  2247. size_t nl, nb;
  2248. /* Skip one byte. */
  2249. if ((p = header_bytes(a, 1)) == NULL)
  2250. return (-1);
  2251. ll--;
  2252. if ((ll & 1) || ll < zip->numFiles * 4)
  2253. return (-1);
  2254. if (zip->entry_names != NULL)
  2255. return (-1);
  2256. zip->entry_names = malloc(ll);
  2257. if (zip->entry_names == NULL)
  2258. return (-1);
  2259. np = zip->entry_names;
  2260. nb = ll;
  2261. /*
  2262. * Copy whole file names.
  2263. * NOTE: This loop prevents from expanding
  2264. * the uncompressed buffer in order not to
  2265. * use extra memory resource.
  2266. */
  2267. while (nb) {
  2268. size_t b;
  2269. if (nb > UBUFF_SIZE)
  2270. b = UBUFF_SIZE;
  2271. else
  2272. b = nb;
  2273. if ((p = header_bytes(a, b)) == NULL)
  2274. return (-1);
  2275. memcpy(np, p, b);
  2276. np += b;
  2277. nb -= b;
  2278. }
  2279. np = zip->entry_names;
  2280. nl = ll;
  2281. for (i = 0; i < zip->numFiles; i++) {
  2282. entries[i].utf16name = np;
  2283. #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
  2284. entries[i].wname = (wchar_t *)np;
  2285. #endif
  2286. /* Find a terminator. */
  2287. while (nl >= 2 && (np[0] || np[1])) {
  2288. np += 2;
  2289. nl -= 2;
  2290. }
  2291. if (nl < 2)
  2292. return (-1);/* Terminator not found */
  2293. entries[i].name_len = np - entries[i].utf16name;
  2294. np += 2;
  2295. nl -= 2;
  2296. }
  2297. break;
  2298. }
  2299. case kAttributes:
  2300. {
  2301. int allAreDefined;
  2302. if ((p = header_bytes(a, 2)) == NULL)
  2303. return (-1);
  2304. allAreDefined = *p;
  2305. if (h->attrBools != NULL)
  2306. return (-1);
  2307. h->attrBools = calloc((size_t)zip->numFiles,
  2308. sizeof(*h->attrBools));
  2309. if (h->attrBools == NULL)
  2310. return (-1);
  2311. if (allAreDefined)
  2312. memset(h->attrBools, 1, (size_t)zip->numFiles);
  2313. else {
  2314. if (read_Bools(a, h->attrBools,
  2315. (size_t)zip->numFiles) < 0)
  2316. return (-1);
  2317. }
  2318. for (i = 0; i < zip->numFiles; i++) {
  2319. if (h->attrBools[i]) {
  2320. if ((p = header_bytes(a, 4)) == NULL)
  2321. return (-1);
  2322. entries[i].attr = archive_le32dec(p);
  2323. }
  2324. }
  2325. break;
  2326. }
  2327. case kDummy:
  2328. if (ll == 0)
  2329. break;
  2330. __LA_FALLTHROUGH;
  2331. default:
  2332. if (header_bytes(a, ll) == NULL)
  2333. return (-1);
  2334. break;
  2335. }
  2336. }
  2337. /*
  2338. * Set up entry's attributes.
  2339. */
  2340. folders = si->ci.folders;
  2341. eindex = sindex = 0;
  2342. folderIndex = indexInFolder = 0;
  2343. for (i = 0; i < zip->numFiles; i++) {
  2344. if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
  2345. entries[i].flg |= HAS_STREAM;
  2346. /* The high 16 bits of attributes is a posix file mode. */
  2347. entries[i].mode = entries[i].attr >> 16;
  2348. if (entries[i].flg & HAS_STREAM) {
  2349. if ((size_t)sindex >= si->ss.unpack_streams)
  2350. return (-1);
  2351. if (entries[i].mode == 0)
  2352. entries[i].mode = AE_IFREG | 0666;
  2353. if (si->ss.digestsDefined[sindex])
  2354. entries[i].flg |= CRC32_IS_SET;
  2355. entries[i].ssIndex = sindex;
  2356. sindex++;
  2357. } else {
  2358. int dir;
  2359. if (h->emptyFileBools == NULL)
  2360. dir = 1;
  2361. else {
  2362. if (h->emptyFileBools[eindex])
  2363. dir = 0;
  2364. else
  2365. dir = 1;
  2366. eindex++;
  2367. }
  2368. if (entries[i].mode == 0) {
  2369. if (dir)
  2370. entries[i].mode = AE_IFDIR | 0777;
  2371. else
  2372. entries[i].mode = AE_IFREG | 0666;
  2373. } else if (dir &&
  2374. (entries[i].mode & AE_IFMT) != AE_IFDIR) {
  2375. entries[i].mode &= ~AE_IFMT;
  2376. entries[i].mode |= AE_IFDIR;
  2377. }
  2378. if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
  2379. entries[i].name_len >= 2 &&
  2380. (entries[i].utf16name[entries[i].name_len-2] != '/' ||
  2381. entries[i].utf16name[entries[i].name_len-1] != 0)) {
  2382. entries[i].utf16name[entries[i].name_len] = '/';
  2383. entries[i].utf16name[entries[i].name_len+1] = 0;
  2384. entries[i].name_len += 2;
  2385. }
  2386. entries[i].ssIndex = -1;
  2387. }
  2388. if (entries[i].attr & 0x01)
  2389. entries[i].mode &= ~0222;/* Read only. */
  2390. if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
  2391. /*
  2392. * The entry is an empty file or a directory file,
  2393. * those both have no contents.
  2394. */
  2395. entries[i].folderIndex = -1;
  2396. continue;
  2397. }
  2398. if (indexInFolder == 0) {
  2399. for (;;) {
  2400. if (folderIndex >= si->ci.numFolders)
  2401. return (-1);
  2402. if (folders[folderIndex].numUnpackStreams)
  2403. break;
  2404. folderIndex++;
  2405. }
  2406. }
  2407. entries[i].folderIndex = folderIndex;
  2408. if ((entries[i].flg & HAS_STREAM) == 0)
  2409. continue;
  2410. indexInFolder++;
  2411. if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
  2412. folderIndex++;
  2413. indexInFolder = 0;
  2414. }
  2415. }
  2416. return (0);
  2417. }
  2418. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  2419. static void
  2420. fileTimeToUtc(uint64_t fileTime, time_t *timep, long *ns)
  2421. {
  2422. if (fileTime >= EPOC_TIME) {
  2423. fileTime -= EPOC_TIME;
  2424. /* milli seconds base */
  2425. *timep = (time_t)(fileTime / 10000000);
  2426. /* nano seconds base */
  2427. *ns = (long)(fileTime % 10000000) * 100;
  2428. } else {
  2429. *timep = 0;
  2430. *ns = 0;
  2431. }
  2432. }
  2433. static int
  2434. read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
  2435. {
  2436. struct _7zip *zip = (struct _7zip *)a->format->data;
  2437. const unsigned char *p;
  2438. struct _7zip_entry *entries = zip->entries;
  2439. unsigned char *timeBools;
  2440. int allAreDefined;
  2441. unsigned i;
  2442. timeBools = calloc((size_t)zip->numFiles, sizeof(*timeBools));
  2443. if (timeBools == NULL)
  2444. return (-1);
  2445. /* Read allAreDefined. */
  2446. if ((p = header_bytes(a, 1)) == NULL)
  2447. goto failed;
  2448. allAreDefined = *p;
  2449. if (allAreDefined)
  2450. memset(timeBools, 1, (size_t)zip->numFiles);
  2451. else {
  2452. if (read_Bools(a, timeBools, (size_t)zip->numFiles) < 0)
  2453. goto failed;
  2454. }
  2455. /* Read external. */
  2456. if ((p = header_bytes(a, 1)) == NULL)
  2457. goto failed;
  2458. if (*p) {
  2459. if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
  2460. goto failed;
  2461. if (UMAX_ENTRY < h->dataIndex)
  2462. goto failed;
  2463. }
  2464. for (i = 0; i < zip->numFiles; i++) {
  2465. if (!timeBools[i])
  2466. continue;
  2467. if ((p = header_bytes(a, 8)) == NULL)
  2468. goto failed;
  2469. switch (type) {
  2470. case kCTime:
  2471. fileTimeToUtc(archive_le64dec(p),
  2472. &(entries[i].ctime),
  2473. &(entries[i].ctime_ns));
  2474. entries[i].flg |= CTIME_IS_SET;
  2475. break;
  2476. case kATime:
  2477. fileTimeToUtc(archive_le64dec(p),
  2478. &(entries[i].atime),
  2479. &(entries[i].atime_ns));
  2480. entries[i].flg |= ATIME_IS_SET;
  2481. break;
  2482. case kMTime:
  2483. fileTimeToUtc(archive_le64dec(p),
  2484. &(entries[i].mtime),
  2485. &(entries[i].mtime_ns));
  2486. entries[i].flg |= MTIME_IS_SET;
  2487. break;
  2488. }
  2489. }
  2490. free(timeBools);
  2491. return (0);
  2492. failed:
  2493. free(timeBools);
  2494. return (-1);
  2495. }
  2496. static int
  2497. decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
  2498. {
  2499. struct _7zip *zip = (struct _7zip *)a->format->data;
  2500. errno = 0;
  2501. if (read_StreamsInfo(a, si) < 0) {
  2502. if (errno == ENOMEM)
  2503. archive_set_error(&a->archive, -1,
  2504. "Couldn't allocate memory");
  2505. else
  2506. archive_set_error(&a->archive, -1,
  2507. "Malformed 7-Zip archive");
  2508. return (ARCHIVE_FATAL);
  2509. }
  2510. if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
  2511. archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
  2512. return (ARCHIVE_FATAL);
  2513. }
  2514. if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
  2515. (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
  2516. si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
  2517. archive_set_error(&a->archive, -1, "Malformed Header offset");
  2518. return (ARCHIVE_FATAL);
  2519. }
  2520. return (ARCHIVE_OK);
  2521. }
  2522. static const unsigned char *
  2523. header_bytes(struct archive_read *a, size_t rbytes)
  2524. {
  2525. struct _7zip *zip = (struct _7zip *)a->format->data;
  2526. const unsigned char *p;
  2527. if (zip->header_bytes_remaining < rbytes)
  2528. return (NULL);
  2529. if (zip->pack_stream_bytes_unconsumed)
  2530. read_consume(a);
  2531. if (zip->header_is_encoded == 0) {
  2532. p = __archive_read_ahead(a, rbytes, NULL);
  2533. if (p == NULL)
  2534. return (NULL);
  2535. zip->header_bytes_remaining -= rbytes;
  2536. zip->pack_stream_bytes_unconsumed = rbytes;
  2537. } else {
  2538. const void *buff;
  2539. ssize_t bytes;
  2540. bytes = read_stream(a, &buff, rbytes, rbytes);
  2541. if (bytes <= 0)
  2542. return (NULL);
  2543. zip->header_bytes_remaining -= bytes;
  2544. p = buff;
  2545. }
  2546. /* Update checksum */
  2547. zip->header_crc32 = crc32(zip->header_crc32, p, (unsigned)rbytes);
  2548. return (p);
  2549. }
  2550. static int
  2551. slurp_central_directory(struct archive_read *a, struct _7zip *zip,
  2552. struct _7z_header_info *header)
  2553. {
  2554. const unsigned char *p;
  2555. uint64_t next_header_offset;
  2556. uint64_t next_header_size;
  2557. uint32_t next_header_crc;
  2558. ssize_t bytes_avail;
  2559. int check_header_crc, r;
  2560. if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
  2561. return (ARCHIVE_FATAL);
  2562. if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
  2563. /* This is an executable ? Must be self-extracting... */
  2564. r = skip_sfx(a, bytes_avail);
  2565. if (r < ARCHIVE_WARN)
  2566. return (r);
  2567. if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
  2568. return (ARCHIVE_FATAL);
  2569. }
  2570. zip->seek_base += 32;
  2571. if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
  2572. archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
  2573. return (ARCHIVE_FATAL);
  2574. }
  2575. /* CRC check. */
  2576. if (crc32(0, (const unsigned char *)p + 12, 20)
  2577. != archive_le32dec(p + 8)) {
  2578. archive_set_error(&a->archive, -1, "Header CRC error");
  2579. return (ARCHIVE_FATAL);
  2580. }
  2581. next_header_offset = archive_le64dec(p + 12);
  2582. next_header_size = archive_le64dec(p + 20);
  2583. next_header_crc = archive_le32dec(p + 28);
  2584. if (next_header_size == 0)
  2585. /* There is no entry in an archive file. */
  2586. return (ARCHIVE_EOF);
  2587. if (((int64_t)next_header_offset) < 0) {
  2588. archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
  2589. return (ARCHIVE_FATAL);
  2590. }
  2591. __archive_read_consume(a, 32);
  2592. if (next_header_offset != 0) {
  2593. if (bytes_avail >= (ssize_t)next_header_offset)
  2594. __archive_read_consume(a, next_header_offset);
  2595. else if (__archive_read_seek(a,
  2596. next_header_offset + zip->seek_base, SEEK_SET) < 0)
  2597. return (ARCHIVE_FATAL);
  2598. }
  2599. zip->stream_offset = next_header_offset;
  2600. zip->header_offset = next_header_offset;
  2601. zip->header_bytes_remaining = next_header_size;
  2602. zip->header_crc32 = 0;
  2603. zip->header_is_encoded = 0;
  2604. zip->header_is_being_read = 1;
  2605. zip->has_encrypted_entries = 0;
  2606. check_header_crc = 1;
  2607. if ((p = header_bytes(a, 1)) == NULL) {
  2608. archive_set_error(&a->archive,
  2609. ARCHIVE_ERRNO_FILE_FORMAT,
  2610. "Truncated 7-Zip file body");
  2611. return (ARCHIVE_FATAL);
  2612. }
  2613. /* Parse ArchiveProperties. */
  2614. switch (p[0]) {
  2615. case kEncodedHeader:
  2616. /*
  2617. * The archive has an encoded header and we have to decode it
  2618. * in order to parse the header correctly.
  2619. */
  2620. r = decode_encoded_header_info(a, &(zip->si));
  2621. /* Check the EncodedHeader CRC.*/
  2622. if (r == 0 && zip->header_crc32 != next_header_crc) {
  2623. archive_set_error(&a->archive, -1,
  2624. "Damaged 7-Zip archive");
  2625. r = -1;
  2626. }
  2627. if (r == 0) {
  2628. if (zip->si.ci.folders[0].digest_defined)
  2629. next_header_crc = zip->si.ci.folders[0].digest;
  2630. else
  2631. check_header_crc = 0;
  2632. if (zip->pack_stream_bytes_unconsumed)
  2633. read_consume(a);
  2634. r = setup_decode_folder(a, zip->si.ci.folders, 1);
  2635. if (r == 0) {
  2636. zip->header_bytes_remaining =
  2637. zip->folder_outbytes_remaining;
  2638. r = seek_pack(a);
  2639. }
  2640. }
  2641. /* Clean up StreamsInfo. */
  2642. free_StreamsInfo(&(zip->si));
  2643. memset(&(zip->si), 0, sizeof(zip->si));
  2644. if (r < 0)
  2645. return (ARCHIVE_FATAL);
  2646. zip->header_is_encoded = 1;
  2647. zip->header_crc32 = 0;
  2648. /* FALL THROUGH */
  2649. case kHeader:
  2650. /*
  2651. * Parse the header.
  2652. */
  2653. errno = 0;
  2654. r = read_Header(a, header, zip->header_is_encoded);
  2655. if (r < 0) {
  2656. if (errno == ENOMEM)
  2657. archive_set_error(&a->archive, -1,
  2658. "Couldn't allocate memory");
  2659. else
  2660. archive_set_error(&a->archive, -1,
  2661. "Damaged 7-Zip archive");
  2662. return (ARCHIVE_FATAL);
  2663. }
  2664. /*
  2665. * Must be kEnd.
  2666. */
  2667. if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
  2668. archive_set_error(&a->archive, -1,
  2669. "Malformed 7-Zip archive");
  2670. return (ARCHIVE_FATAL);
  2671. }
  2672. /* Check the Header CRC.*/
  2673. if (check_header_crc && zip->header_crc32 != next_header_crc) {
  2674. archive_set_error(&a->archive, -1,
  2675. "Malformed 7-Zip archive");
  2676. return (ARCHIVE_FATAL);
  2677. }
  2678. break;
  2679. default:
  2680. archive_set_error(&a->archive, -1,
  2681. "Unexpected Property ID = %X", p[0]);
  2682. return (ARCHIVE_FATAL);
  2683. }
  2684. /* Clean up variables be used for decoding the archive header */
  2685. zip->pack_stream_remaining = 0;
  2686. zip->pack_stream_index = 0;
  2687. zip->folder_outbytes_remaining = 0;
  2688. zip->uncompressed_buffer_bytes_remaining = 0;
  2689. zip->pack_stream_bytes_unconsumed = 0;
  2690. zip->header_is_being_read = 0;
  2691. return (ARCHIVE_OK);
  2692. }
  2693. static ssize_t
  2694. get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
  2695. size_t minimum)
  2696. {
  2697. struct _7zip *zip = (struct _7zip *)a->format->data;
  2698. ssize_t bytes_avail;
  2699. if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
  2700. /* Copy mode. */
  2701. *buff = __archive_read_ahead(a, minimum, &bytes_avail);
  2702. if (bytes_avail <= 0) {
  2703. archive_set_error(&a->archive,
  2704. ARCHIVE_ERRNO_FILE_FORMAT,
  2705. "Truncated 7-Zip file data");
  2706. return (ARCHIVE_FATAL);
  2707. }
  2708. if ((size_t)bytes_avail >
  2709. zip->uncompressed_buffer_bytes_remaining)
  2710. bytes_avail = (ssize_t)
  2711. zip->uncompressed_buffer_bytes_remaining;
  2712. if ((size_t)bytes_avail > size)
  2713. bytes_avail = (ssize_t)size;
  2714. zip->pack_stream_bytes_unconsumed = bytes_avail;
  2715. } else if (zip->uncompressed_buffer_pointer == NULL) {
  2716. /* Decompression has failed. */
  2717. archive_set_error(&(a->archive),
  2718. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2719. return (ARCHIVE_FATAL);
  2720. } else {
  2721. /* Packed mode. */
  2722. if (minimum > zip->uncompressed_buffer_bytes_remaining) {
  2723. /*
  2724. * If remaining uncompressed data size is less than
  2725. * the minimum size, fill the buffer up to the
  2726. * minimum size.
  2727. */
  2728. if (extract_pack_stream(a, minimum) < 0)
  2729. return (ARCHIVE_FATAL);
  2730. }
  2731. if (size > zip->uncompressed_buffer_bytes_remaining)
  2732. bytes_avail = (ssize_t)
  2733. zip->uncompressed_buffer_bytes_remaining;
  2734. else
  2735. bytes_avail = (ssize_t)size;
  2736. *buff = zip->uncompressed_buffer_pointer;
  2737. zip->uncompressed_buffer_pointer += bytes_avail;
  2738. }
  2739. zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
  2740. return (bytes_avail);
  2741. }
  2742. static ssize_t
  2743. extract_pack_stream(struct archive_read *a, size_t minimum)
  2744. {
  2745. struct _7zip *zip = (struct _7zip *)a->format->data;
  2746. ssize_t bytes_avail;
  2747. int r;
  2748. if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
  2749. if (minimum == 0)
  2750. minimum = 1;
  2751. if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
  2752. || bytes_avail <= 0) {
  2753. archive_set_error(&a->archive,
  2754. ARCHIVE_ERRNO_FILE_FORMAT,
  2755. "Truncated 7-Zip file body");
  2756. return (ARCHIVE_FATAL);
  2757. }
  2758. if (bytes_avail > (ssize_t)zip->pack_stream_inbytes_remaining)
  2759. bytes_avail = (ssize_t)zip->pack_stream_inbytes_remaining;
  2760. zip->pack_stream_inbytes_remaining -= bytes_avail;
  2761. if (bytes_avail > (ssize_t)zip->folder_outbytes_remaining)
  2762. bytes_avail = (ssize_t)zip->folder_outbytes_remaining;
  2763. zip->folder_outbytes_remaining -= bytes_avail;
  2764. zip->uncompressed_buffer_bytes_remaining = bytes_avail;
  2765. return (ARCHIVE_OK);
  2766. }
  2767. /* If the buffer hasn't been allocated, allocate it now. */
  2768. if (zip->uncompressed_buffer == NULL) {
  2769. zip->uncompressed_buffer_size = UBUFF_SIZE;
  2770. if (zip->uncompressed_buffer_size < minimum) {
  2771. zip->uncompressed_buffer_size = minimum + 1023;
  2772. zip->uncompressed_buffer_size &= ~0x3ff;
  2773. }
  2774. zip->uncompressed_buffer =
  2775. malloc(zip->uncompressed_buffer_size);
  2776. if (zip->uncompressed_buffer == NULL) {
  2777. archive_set_error(&a->archive, ENOMEM,
  2778. "No memory for 7-Zip decompression");
  2779. return (ARCHIVE_FATAL);
  2780. }
  2781. zip->uncompressed_buffer_bytes_remaining = 0;
  2782. } else if (zip->uncompressed_buffer_size < minimum ||
  2783. zip->uncompressed_buffer_bytes_remaining < minimum) {
  2784. /*
  2785. * Make sure the uncompressed buffer can have bytes
  2786. * at least `minimum' bytes.
  2787. * NOTE: This case happen when reading the header.
  2788. */
  2789. size_t used;
  2790. if (zip->uncompressed_buffer_pointer != 0)
  2791. used = zip->uncompressed_buffer_pointer -
  2792. zip->uncompressed_buffer;
  2793. else
  2794. used = 0;
  2795. if (zip->uncompressed_buffer_size < minimum) {
  2796. /*
  2797. * Expand the uncompressed buffer up to
  2798. * the minimum size.
  2799. */
  2800. void *p;
  2801. size_t new_size;
  2802. new_size = minimum + 1023;
  2803. new_size &= ~0x3ff;
  2804. p = realloc(zip->uncompressed_buffer, new_size);
  2805. if (p == NULL) {
  2806. archive_set_error(&a->archive, ENOMEM,
  2807. "No memory for 7-Zip decompression");
  2808. return (ARCHIVE_FATAL);
  2809. }
  2810. zip->uncompressed_buffer = (unsigned char *)p;
  2811. zip->uncompressed_buffer_size = new_size;
  2812. }
  2813. /*
  2814. * Move unconsumed bytes to the head.
  2815. */
  2816. if (used) {
  2817. memmove(zip->uncompressed_buffer,
  2818. zip->uncompressed_buffer + used,
  2819. zip->uncompressed_buffer_bytes_remaining);
  2820. }
  2821. } else
  2822. zip->uncompressed_buffer_bytes_remaining = 0;
  2823. zip->uncompressed_buffer_pointer = NULL;
  2824. for (;;) {
  2825. size_t bytes_in, bytes_out;
  2826. const void *buff_in;
  2827. unsigned char *buff_out;
  2828. int end_of_data;
  2829. /*
  2830. * Note: '1' here is a performance optimization.
  2831. * Recall that the decompression layer returns a count of
  2832. * available bytes; asking for more than that forces the
  2833. * decompressor to combine reads by copying data.
  2834. */
  2835. buff_in = __archive_read_ahead(a, 1, &bytes_avail);
  2836. if (bytes_avail <= 0) {
  2837. archive_set_error(&a->archive,
  2838. ARCHIVE_ERRNO_FILE_FORMAT,
  2839. "Truncated 7-Zip file body");
  2840. return (ARCHIVE_FATAL);
  2841. }
  2842. buff_out = zip->uncompressed_buffer
  2843. + zip->uncompressed_buffer_bytes_remaining;
  2844. bytes_out = zip->uncompressed_buffer_size
  2845. - zip->uncompressed_buffer_bytes_remaining;
  2846. bytes_in = bytes_avail;
  2847. if (bytes_in > zip->pack_stream_inbytes_remaining)
  2848. bytes_in = (size_t)zip->pack_stream_inbytes_remaining;
  2849. /* Drive decompression. */
  2850. r = decompress(a, zip, buff_out, &bytes_out,
  2851. buff_in, &bytes_in);
  2852. switch (r) {
  2853. case ARCHIVE_OK:
  2854. end_of_data = 0;
  2855. break;
  2856. case ARCHIVE_EOF:
  2857. end_of_data = 1;
  2858. break;
  2859. default:
  2860. return (ARCHIVE_FATAL);
  2861. }
  2862. zip->pack_stream_inbytes_remaining -= bytes_in;
  2863. if (bytes_out > zip->folder_outbytes_remaining)
  2864. bytes_out = (size_t)zip->folder_outbytes_remaining;
  2865. zip->folder_outbytes_remaining -= bytes_out;
  2866. zip->uncompressed_buffer_bytes_remaining += bytes_out;
  2867. zip->pack_stream_bytes_unconsumed = bytes_in;
  2868. /*
  2869. * Continue decompression until uncompressed_buffer is full.
  2870. */
  2871. if (zip->uncompressed_buffer_bytes_remaining ==
  2872. zip->uncompressed_buffer_size)
  2873. break;
  2874. if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
  2875. zip->uncompressed_buffer_bytes_remaining + 5 >
  2876. zip->uncompressed_buffer_size)
  2877. break;
  2878. if (zip->pack_stream_inbytes_remaining == 0 &&
  2879. zip->folder_outbytes_remaining == 0)
  2880. break;
  2881. if (end_of_data || (bytes_in == 0 && bytes_out == 0)) {
  2882. archive_set_error(&(a->archive),
  2883. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2884. return (ARCHIVE_FATAL);
  2885. }
  2886. read_consume(a);
  2887. }
  2888. if (zip->uncompressed_buffer_bytes_remaining < minimum) {
  2889. archive_set_error(&(a->archive),
  2890. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2891. return (ARCHIVE_FATAL);
  2892. }
  2893. zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
  2894. return (ARCHIVE_OK);
  2895. }
  2896. static int
  2897. seek_pack(struct archive_read *a)
  2898. {
  2899. struct _7zip *zip = (struct _7zip *)a->format->data;
  2900. int64_t pack_offset;
  2901. if (zip->pack_stream_remaining <= 0) {
  2902. archive_set_error(&(a->archive),
  2903. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2904. return (ARCHIVE_FATAL);
  2905. }
  2906. zip->pack_stream_inbytes_remaining =
  2907. zip->si.pi.sizes[zip->pack_stream_index];
  2908. pack_offset = zip->si.pi.positions[zip->pack_stream_index];
  2909. if (zip->stream_offset != pack_offset) {
  2910. if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
  2911. SEEK_SET))
  2912. return (ARCHIVE_FATAL);
  2913. zip->stream_offset = pack_offset;
  2914. }
  2915. zip->pack_stream_index++;
  2916. zip->pack_stream_remaining--;
  2917. return (ARCHIVE_OK);
  2918. }
  2919. static ssize_t
  2920. read_stream(struct archive_read *a, const void **buff, size_t size,
  2921. size_t minimum)
  2922. {
  2923. struct _7zip *zip = (struct _7zip *)a->format->data;
  2924. uint64_t skip_bytes = 0;
  2925. ssize_t r;
  2926. if (zip->uncompressed_buffer_bytes_remaining == 0) {
  2927. if (zip->pack_stream_inbytes_remaining > 0) {
  2928. r = extract_pack_stream(a, 0);
  2929. if (r < 0)
  2930. return (r);
  2931. return (get_uncompressed_data(a, buff, size, minimum));
  2932. } else if (zip->folder_outbytes_remaining > 0) {
  2933. /* Extract a remaining pack stream. */
  2934. r = extract_pack_stream(a, 0);
  2935. if (r < 0)
  2936. return (r);
  2937. return (get_uncompressed_data(a, buff, size, minimum));
  2938. }
  2939. } else
  2940. return (get_uncompressed_data(a, buff, size, minimum));
  2941. /*
  2942. * Current pack stream has been consumed.
  2943. */
  2944. if (zip->pack_stream_remaining == 0) {
  2945. if (zip->header_is_being_read) {
  2946. /* Invalid sequence. This might happen when
  2947. * reading a malformed archive. */
  2948. archive_set_error(&(a->archive),
  2949. ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
  2950. return (ARCHIVE_FATAL);
  2951. }
  2952. /*
  2953. * All current folder's pack streams have been
  2954. * consumed. Switch to next folder.
  2955. */
  2956. if (zip->folder_index == 0 &&
  2957. (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
  2958. || zip->folder_index != zip->entry->folderIndex)) {
  2959. zip->folder_index = zip->entry->folderIndex;
  2960. skip_bytes =
  2961. zip->si.ci.folders[zip->folder_index].skipped_bytes;
  2962. }
  2963. if (zip->folder_index >= zip->si.ci.numFolders) {
  2964. /*
  2965. * We have consumed all folders and its pack streams.
  2966. */
  2967. *buff = NULL;
  2968. return (0);
  2969. }
  2970. r = setup_decode_folder(a,
  2971. &(zip->si.ci.folders[zip->folder_index]), 0);
  2972. if (r != ARCHIVE_OK)
  2973. return (ARCHIVE_FATAL);
  2974. zip->folder_index++;
  2975. }
  2976. /*
  2977. * Switch to next pack stream.
  2978. */
  2979. r = seek_pack(a);
  2980. if (r < 0)
  2981. return (r);
  2982. /* Extract a new pack stream. */
  2983. r = extract_pack_stream(a, 0);
  2984. if (r < 0)
  2985. return (r);
  2986. /*
  2987. * Skip the bytes we already has skipped in skip_stream().
  2988. */
  2989. while (skip_bytes) {
  2990. ssize_t skipped;
  2991. if (zip->uncompressed_buffer_bytes_remaining == 0) {
  2992. if (zip->pack_stream_inbytes_remaining > 0) {
  2993. r = extract_pack_stream(a, 0);
  2994. if (r < 0)
  2995. return (r);
  2996. } else if (zip->folder_outbytes_remaining > 0) {
  2997. /* Extract a remaining pack stream. */
  2998. r = extract_pack_stream(a, 0);
  2999. if (r < 0)
  3000. return (r);
  3001. } else {
  3002. archive_set_error(&a->archive,
  3003. ARCHIVE_ERRNO_FILE_FORMAT,
  3004. "Truncated 7-Zip file body");
  3005. return (ARCHIVE_FATAL);
  3006. }
  3007. }
  3008. skipped = get_uncompressed_data(
  3009. a, buff, (size_t)skip_bytes, 0);
  3010. if (skipped < 0)
  3011. return (skipped);
  3012. skip_bytes -= skipped;
  3013. if (zip->pack_stream_bytes_unconsumed)
  3014. read_consume(a);
  3015. }
  3016. return (get_uncompressed_data(a, buff, size, minimum));
  3017. }
  3018. static int
  3019. setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
  3020. int header)
  3021. {
  3022. struct _7zip *zip = (struct _7zip *)a->format->data;
  3023. const struct _7z_coder *coder1, *coder2;
  3024. const char *cname = (header)?"archive header":"file content";
  3025. unsigned i;
  3026. int r, found_bcj2 = 0;
  3027. /*
  3028. * Release the memory which the previous folder used for BCJ2.
  3029. */
  3030. for (i = 0; i < 3; i++) {
  3031. free(zip->sub_stream_buff[i]);
  3032. zip->sub_stream_buff[i] = NULL;
  3033. }
  3034. /*
  3035. * Initialize a stream reader.
  3036. */
  3037. zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
  3038. zip->pack_stream_index = (unsigned)folder->packIndex;
  3039. zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
  3040. zip->uncompressed_buffer_bytes_remaining = 0;
  3041. /*
  3042. * Check coder types.
  3043. */
  3044. for (i = 0; i < folder->numCoders; i++) {
  3045. switch(folder->coders[i].codec) {
  3046. case _7Z_CRYPTO_MAIN_ZIP:
  3047. case _7Z_CRYPTO_RAR_29:
  3048. case _7Z_CRYPTO_AES_256_SHA_256: {
  3049. /* For entry that is associated with this folder, mark
  3050. it as encrypted (data+metadata). */
  3051. zip->has_encrypted_entries = 1;
  3052. if (a->entry) {
  3053. archive_entry_set_is_data_encrypted(a->entry, 1);
  3054. archive_entry_set_is_metadata_encrypted(a->entry, 1);
  3055. }
  3056. archive_set_error(&(a->archive),
  3057. ARCHIVE_ERRNO_MISC,
  3058. "The %s is encrypted, "
  3059. "but currently not supported", cname);
  3060. return (ARCHIVE_FATAL);
  3061. }
  3062. case _7Z_X86_BCJ2: {
  3063. found_bcj2++;
  3064. break;
  3065. }
  3066. }
  3067. }
  3068. /* Now that we've checked for encryption, if there were still no
  3069. * encrypted entries found we can say for sure that there are none.
  3070. */
  3071. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  3072. zip->has_encrypted_entries = 0;
  3073. }
  3074. if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
  3075. archive_set_error(&(a->archive),
  3076. ARCHIVE_ERRNO_MISC,
  3077. "The %s is encoded with many filters, "
  3078. "but currently not supported", cname);
  3079. return (ARCHIVE_FATAL);
  3080. }
  3081. coder1 = &(folder->coders[0]);
  3082. if (folder->numCoders == 2)
  3083. coder2 = &(folder->coders[1]);
  3084. else
  3085. coder2 = NULL;
  3086. if (found_bcj2) {
  3087. /*
  3088. * Preparation to decode BCJ2.
  3089. * Decoding BCJ2 requires four sources. Those are at least,
  3090. * as far as I know, two types of the storage form.
  3091. */
  3092. const struct _7z_coder *fc = folder->coders;
  3093. static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
  3094. const struct _7z_coder *scoder[3] =
  3095. {&coder_copy, &coder_copy, &coder_copy};
  3096. const void *buff;
  3097. ssize_t bytes;
  3098. unsigned char *b[3] = {NULL, NULL, NULL};
  3099. uint64_t sunpack[3] ={-1, -1, -1};
  3100. size_t s[3] = {0, 0, 0};
  3101. int idx[3] = {0, 1, 2};
  3102. if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
  3103. folder->numInStreams == 7 && folder->numOutStreams == 4 &&
  3104. zip->pack_stream_remaining == 4) {
  3105. /* Source type 1 made by 7zr or 7z with -m options. */
  3106. if (folder->bindPairs[0].inIndex == 5) {
  3107. /* The form made by 7zr */
  3108. idx[0] = 1; idx[1] = 2; idx[2] = 0;
  3109. scoder[1] = &(fc[1]);
  3110. scoder[2] = &(fc[0]);
  3111. sunpack[1] = folder->unPackSize[1];
  3112. sunpack[2] = folder->unPackSize[0];
  3113. coder1 = &(fc[2]);
  3114. } else {
  3115. /*
  3116. * NOTE: Some patterns do not work.
  3117. * work:
  3118. * 7z a -m0=BCJ2 -m1=COPY -m2=COPY
  3119. * -m3=(any)
  3120. * 7z a -m0=BCJ2 -m1=COPY -m2=(any)
  3121. * -m3=COPY
  3122. * 7z a -m0=BCJ2 -m1=(any) -m2=COPY
  3123. * -m3=COPY
  3124. * not work:
  3125. * other patterns.
  3126. *
  3127. * We have to handle this like `pipe' or
  3128. * our libarchive7s filter frame work,
  3129. * decoding the BCJ2 main stream sequentially,
  3130. * m3 -> m2 -> m1 -> BCJ2.
  3131. *
  3132. */
  3133. if (fc[0].codec == _7Z_COPY &&
  3134. fc[1].codec == _7Z_COPY)
  3135. coder1 = &(folder->coders[2]);
  3136. else if (fc[0].codec == _7Z_COPY &&
  3137. fc[2].codec == _7Z_COPY)
  3138. coder1 = &(folder->coders[1]);
  3139. else if (fc[1].codec == _7Z_COPY &&
  3140. fc[2].codec == _7Z_COPY)
  3141. coder1 = &(folder->coders[0]);
  3142. else {
  3143. archive_set_error(&(a->archive),
  3144. ARCHIVE_ERRNO_MISC,
  3145. "Unsupported form of "
  3146. "BCJ2 streams");
  3147. return (ARCHIVE_FATAL);
  3148. }
  3149. }
  3150. coder2 = &(fc[3]);
  3151. zip->main_stream_bytes_remaining =
  3152. (size_t)folder->unPackSize[2];
  3153. } else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
  3154. zip->pack_stream_remaining == 4 &&
  3155. folder->numInStreams == 5 && folder->numOutStreams == 2) {
  3156. /* Source type 0 made by 7z */
  3157. zip->main_stream_bytes_remaining =
  3158. (size_t)folder->unPackSize[0];
  3159. } else {
  3160. /* We got an unexpected form. */
  3161. archive_set_error(&(a->archive),
  3162. ARCHIVE_ERRNO_MISC,
  3163. "Unsupported form of BCJ2 streams");
  3164. return (ARCHIVE_FATAL);
  3165. }
  3166. /* Skip the main stream at this time. */
  3167. if ((r = seek_pack(a)) < 0)
  3168. return (r);
  3169. zip->pack_stream_bytes_unconsumed =
  3170. (size_t)zip->pack_stream_inbytes_remaining;
  3171. read_consume(a);
  3172. /* Read following three sub streams. */
  3173. for (i = 0; i < 3; i++) {
  3174. const struct _7z_coder *coder = scoder[i];
  3175. if ((r = seek_pack(a)) < 0) {
  3176. free(b[0]); free(b[1]); free(b[2]);
  3177. return (r);
  3178. }
  3179. if (sunpack[i] == (uint64_t)-1)
  3180. zip->folder_outbytes_remaining =
  3181. zip->pack_stream_inbytes_remaining;
  3182. else
  3183. zip->folder_outbytes_remaining = sunpack[i];
  3184. r = init_decompression(a, zip, coder, NULL);
  3185. if (r != ARCHIVE_OK) {
  3186. free(b[0]); free(b[1]); free(b[2]);
  3187. return (ARCHIVE_FATAL);
  3188. }
  3189. /* Allocate memory for the decoded data of a sub
  3190. * stream. */
  3191. b[i] = malloc((size_t)zip->folder_outbytes_remaining);
  3192. if (b[i] == NULL) {
  3193. free(b[0]); free(b[1]); free(b[2]);
  3194. archive_set_error(&a->archive, ENOMEM,
  3195. "No memory for 7-Zip decompression");
  3196. return (ARCHIVE_FATAL);
  3197. }
  3198. /* Extract a sub stream. */
  3199. while (zip->pack_stream_inbytes_remaining > 0) {
  3200. r = (int)extract_pack_stream(a, 0);
  3201. if (r < 0) {
  3202. free(b[0]); free(b[1]); free(b[2]);
  3203. return (r);
  3204. }
  3205. bytes = get_uncompressed_data(a, &buff,
  3206. zip->uncompressed_buffer_bytes_remaining,
  3207. 0);
  3208. if (bytes < 0) {
  3209. free(b[0]); free(b[1]); free(b[2]);
  3210. return ((int)bytes);
  3211. }
  3212. memcpy(b[i]+s[i], buff, bytes);
  3213. s[i] += bytes;
  3214. if (zip->pack_stream_bytes_unconsumed)
  3215. read_consume(a);
  3216. }
  3217. }
  3218. /* Set the sub streams to the right place. */
  3219. for (i = 0; i < 3; i++) {
  3220. zip->sub_stream_buff[i] = b[idx[i]];
  3221. zip->sub_stream_size[i] = s[idx[i]];
  3222. zip->sub_stream_bytes_remaining[i] = s[idx[i]];
  3223. }
  3224. /* Allocate memory used for decoded main stream bytes. */
  3225. if (zip->tmp_stream_buff == NULL) {
  3226. zip->tmp_stream_buff_size = 32 * 1024;
  3227. zip->tmp_stream_buff =
  3228. malloc(zip->tmp_stream_buff_size);
  3229. if (zip->tmp_stream_buff == NULL) {
  3230. archive_set_error(&a->archive, ENOMEM,
  3231. "No memory for 7-Zip decompression");
  3232. return (ARCHIVE_FATAL);
  3233. }
  3234. }
  3235. zip->tmp_stream_bytes_avail = 0;
  3236. zip->tmp_stream_bytes_remaining = 0;
  3237. zip->odd_bcj_size = 0;
  3238. zip->bcj2_outPos = 0;
  3239. /*
  3240. * Reset a stream reader in order to read the main stream
  3241. * of BCJ2.
  3242. */
  3243. zip->pack_stream_remaining = 1;
  3244. zip->pack_stream_index = (unsigned)folder->packIndex;
  3245. zip->folder_outbytes_remaining =
  3246. folder_uncompressed_size(folder);
  3247. zip->uncompressed_buffer_bytes_remaining = 0;
  3248. }
  3249. /*
  3250. * Initialize the decompressor for the new folder's pack streams.
  3251. */
  3252. r = init_decompression(a, zip, coder1, coder2);
  3253. if (r != ARCHIVE_OK)
  3254. return (ARCHIVE_FATAL);
  3255. return (ARCHIVE_OK);
  3256. }
  3257. static int64_t
  3258. skip_stream(struct archive_read *a, size_t skip_bytes)
  3259. {
  3260. struct _7zip *zip = (struct _7zip *)a->format->data;
  3261. const void *p;
  3262. int64_t skipped_bytes;
  3263. size_t bytes = skip_bytes;
  3264. if (zip->folder_index == 0) {
  3265. /*
  3266. * Optimization for a list mode.
  3267. * Avoid unnecessary decoding operations.
  3268. */
  3269. zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
  3270. += skip_bytes;
  3271. return (skip_bytes);
  3272. }
  3273. while (bytes) {
  3274. skipped_bytes = read_stream(a, &p, bytes, 0);
  3275. if (skipped_bytes < 0)
  3276. return (skipped_bytes);
  3277. if (skipped_bytes == 0) {
  3278. archive_set_error(&a->archive,
  3279. ARCHIVE_ERRNO_FILE_FORMAT,
  3280. "Truncated 7-Zip file body");
  3281. return (ARCHIVE_FATAL);
  3282. }
  3283. bytes -= (size_t)skipped_bytes;
  3284. if (zip->pack_stream_bytes_unconsumed)
  3285. read_consume(a);
  3286. }
  3287. return (skip_bytes);
  3288. }
  3289. /*
  3290. * Brought from LZMA SDK.
  3291. *
  3292. * Bra86.c -- Converter for x86 code (BCJ)
  3293. * 2008-10-04 : Igor Pavlov : Public domain
  3294. *
  3295. */
  3296. #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
  3297. static void
  3298. x86_Init(struct _7zip *zip)
  3299. {
  3300. zip->bcj_state = 0;
  3301. zip->bcj_prevPosT = (size_t)0 - 1;
  3302. zip->bcj_prevMask = 0;
  3303. zip->bcj_ip = 5;
  3304. }
  3305. static size_t
  3306. x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
  3307. {
  3308. static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
  3309. static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
  3310. size_t bufferPos, prevPosT;
  3311. uint32_t ip, prevMask;
  3312. if (size < 5)
  3313. return 0;
  3314. bufferPos = 0;
  3315. prevPosT = zip->bcj_prevPosT;
  3316. prevMask = zip->bcj_prevMask;
  3317. ip = zip->bcj_ip;
  3318. for (;;) {
  3319. uint8_t *p = data + bufferPos;
  3320. uint8_t *limit = data + size - 4;
  3321. for (; p < limit; p++)
  3322. if ((*p & 0xFE) == 0xE8)
  3323. break;
  3324. bufferPos = (size_t)(p - data);
  3325. if (p >= limit)
  3326. break;
  3327. prevPosT = bufferPos - prevPosT;
  3328. if (prevPosT > 3)
  3329. prevMask = 0;
  3330. else {
  3331. prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
  3332. if (prevMask != 0) {
  3333. unsigned char b =
  3334. p[4 - kMaskToBitNumber[prevMask]];
  3335. if (!kMaskToAllowedStatus[prevMask] ||
  3336. Test86MSByte(b)) {
  3337. prevPosT = bufferPos;
  3338. prevMask = ((prevMask << 1) & 0x7) | 1;
  3339. bufferPos++;
  3340. continue;
  3341. }
  3342. }
  3343. }
  3344. prevPosT = bufferPos;
  3345. if (Test86MSByte(p[4])) {
  3346. uint32_t src = ((uint32_t)p[4] << 24) |
  3347. ((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
  3348. ((uint32_t)p[1]);
  3349. uint32_t dest;
  3350. for (;;) {
  3351. uint8_t b;
  3352. int b_index;
  3353. dest = src - (ip + (uint32_t)bufferPos);
  3354. if (prevMask == 0)
  3355. break;
  3356. b_index = kMaskToBitNumber[prevMask] * 8;
  3357. b = (uint8_t)(dest >> (24 - b_index));
  3358. if (!Test86MSByte(b))
  3359. break;
  3360. src = dest ^ ((1 << (32 - b_index)) - 1);
  3361. }
  3362. p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
  3363. p[3] = (uint8_t)(dest >> 16);
  3364. p[2] = (uint8_t)(dest >> 8);
  3365. p[1] = (uint8_t)dest;
  3366. bufferPos += 5;
  3367. } else {
  3368. prevMask = ((prevMask << 1) & 0x7) | 1;
  3369. bufferPos++;
  3370. }
  3371. }
  3372. zip->bcj_prevPosT = prevPosT;
  3373. zip->bcj_prevMask = prevMask;
  3374. zip->bcj_ip += (uint32_t)bufferPos;
  3375. return (bufferPos);
  3376. }
  3377. /*
  3378. * Brought from LZMA SDK.
  3379. *
  3380. * Bcj2.c -- Converter for x86 code (BCJ2)
  3381. * 2008-10-04 : Igor Pavlov : Public domain
  3382. *
  3383. */
  3384. #define SZ_ERROR_DATA ARCHIVE_FAILED
  3385. #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
  3386. #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
  3387. #define kNumTopBits 24
  3388. #define kTopValue ((uint32_t)1 << kNumTopBits)
  3389. #define kNumBitModelTotalBits 11
  3390. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  3391. #define kNumMoveBits 5
  3392. #define RC_READ_BYTE (*buffer++)
  3393. #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
  3394. #define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
  3395. { int ii; for (ii = 0; ii < 5; ii++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
  3396. #define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
  3397. #define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
  3398. #define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
  3399. #define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
  3400. static ssize_t
  3401. Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
  3402. {
  3403. size_t inPos = 0, outPos = 0;
  3404. const uint8_t *buf0, *buf1, *buf2, *buf3;
  3405. size_t size0, size1, size2, size3;
  3406. const uint8_t *buffer, *bufferLim;
  3407. unsigned int i, j;
  3408. size0 = zip->tmp_stream_bytes_remaining;
  3409. buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
  3410. size1 = zip->sub_stream_bytes_remaining[0];
  3411. buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
  3412. size2 = zip->sub_stream_bytes_remaining[1];
  3413. buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
  3414. size3 = zip->sub_stream_bytes_remaining[2];
  3415. buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
  3416. buffer = buf3;
  3417. bufferLim = buffer + size3;
  3418. if (zip->bcj_state == 0) {
  3419. /*
  3420. * Initialize.
  3421. */
  3422. zip->bcj2_prevByte = 0;
  3423. for (i = 0;
  3424. i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
  3425. zip->bcj2_p[i] = kBitModelTotal >> 1;
  3426. RC_INIT2;
  3427. zip->bcj_state = 1;
  3428. }
  3429. /*
  3430. * Gather the odd bytes of a previous call.
  3431. */
  3432. for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
  3433. outBuf[outPos++] = zip->odd_bcj[i];
  3434. zip->odd_bcj_size--;
  3435. }
  3436. if (outSize == 0) {
  3437. zip->bcj2_outPos += outPos;
  3438. return (outPos);
  3439. }
  3440. for (;;) {
  3441. uint8_t b;
  3442. CProb *prob;
  3443. uint32_t bound;
  3444. uint32_t ttt;
  3445. size_t limit = size0 - inPos;
  3446. if (outSize - outPos < limit)
  3447. limit = outSize - outPos;
  3448. if (zip->bcj_state == 1) {
  3449. while (limit != 0) {
  3450. uint8_t bb = buf0[inPos];
  3451. outBuf[outPos++] = bb;
  3452. if (IsJ(zip->bcj2_prevByte, bb)) {
  3453. zip->bcj_state = 2;
  3454. break;
  3455. }
  3456. inPos++;
  3457. zip->bcj2_prevByte = bb;
  3458. limit--;
  3459. }
  3460. }
  3461. if (limit == 0 || outPos == outSize)
  3462. break;
  3463. zip->bcj_state = 1;
  3464. b = buf0[inPos++];
  3465. if (b == 0xE8)
  3466. prob = zip->bcj2_p + zip->bcj2_prevByte;
  3467. else if (b == 0xE9)
  3468. prob = zip->bcj2_p + 256;
  3469. else
  3470. prob = zip->bcj2_p + 257;
  3471. IF_BIT_0(prob) {
  3472. UPDATE_0(prob)
  3473. zip->bcj2_prevByte = b;
  3474. } else {
  3475. uint32_t dest;
  3476. const uint8_t *v;
  3477. uint8_t out[4];
  3478. UPDATE_1(prob)
  3479. if (b == 0xE8) {
  3480. v = buf1;
  3481. if (size1 < 4)
  3482. return SZ_ERROR_DATA;
  3483. buf1 += 4;
  3484. size1 -= 4;
  3485. } else {
  3486. v = buf2;
  3487. if (size2 < 4)
  3488. return SZ_ERROR_DATA;
  3489. buf2 += 4;
  3490. size2 -= 4;
  3491. }
  3492. dest = (((uint32_t)v[0] << 24) |
  3493. ((uint32_t)v[1] << 16) |
  3494. ((uint32_t)v[2] << 8) |
  3495. ((uint32_t)v[3])) -
  3496. ((uint32_t)zip->bcj2_outPos + (uint32_t)outPos + 4);
  3497. out[0] = (uint8_t)dest;
  3498. out[1] = (uint8_t)(dest >> 8);
  3499. out[2] = (uint8_t)(dest >> 16);
  3500. out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
  3501. for (i = 0; i < 4 && outPos < outSize; i++)
  3502. outBuf[outPos++] = out[i];
  3503. if (i < 4) {
  3504. /*
  3505. * Save odd bytes which we could not add into
  3506. * the output buffer because of out of space.
  3507. */
  3508. zip->odd_bcj_size = 4 -i;
  3509. for (; i < 4; i++) {
  3510. j = i - 4 + (unsigned)zip->odd_bcj_size;
  3511. zip->odd_bcj[j] = out[i];
  3512. }
  3513. break;
  3514. }
  3515. }
  3516. }
  3517. zip->tmp_stream_bytes_remaining -= inPos;
  3518. zip->sub_stream_bytes_remaining[0] = size1;
  3519. zip->sub_stream_bytes_remaining[1] = size2;
  3520. zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
  3521. zip->bcj2_outPos += outPos;
  3522. return ((ssize_t)outPos);
  3523. }