archive_read_support_format_7zip.c 97 KB

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