archive_read_support_format_7zip.c 109 KB

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