archive_read_support_format_zip.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406
  1. /*-
  2. * Copyright (c) 2004-2013 Tim Kientzle
  3. * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
  4. * Copyright (c) 2013 Konrad Kleine
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. /*
  29. * The definitive documentation of the Zip file format is:
  30. * http://www.pkware.com/documents/casestudies/APPNOTE.TXT
  31. *
  32. * The Info-Zip project has pioneered various extensions to better
  33. * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
  34. * "Ux", and 0x7875 "ux" extensions for time and ownership
  35. * information.
  36. *
  37. * History of this code: The streaming Zip reader was first added to
  38. * libarchive in January 2005. Support for seekable input sources was
  39. * added in Nov 2011. Zip64 support (including a significant code
  40. * refactoring) was added in 2014.
  41. */
  42. #ifdef HAVE_ERRNO_H
  43. #include <errno.h>
  44. #endif
  45. #ifdef HAVE_STDLIB_H
  46. #include <stdlib.h>
  47. #endif
  48. #ifdef HAVE_ZLIB_H
  49. #include <cm3p/zlib.h>
  50. #endif
  51. #ifdef HAVE_BZLIB_H
  52. #include <cm3p/bzlib.h>
  53. #endif
  54. #ifdef HAVE_LZMA_H
  55. #include <cm3p/lzma.h>
  56. #endif
  57. #ifdef HAVE_ZSTD_H
  58. #include <cm3p/zstd.h>
  59. #endif
  60. #include "archive.h"
  61. #include "archive_digest_private.h"
  62. #include "archive_cryptor_private.h"
  63. #include "archive_endian.h"
  64. #include "archive_entry.h"
  65. #include "archive_entry_locale.h"
  66. #include "archive_hmac_private.h"
  67. #include "archive_private.h"
  68. #include "archive_rb.h"
  69. #include "archive_read_private.h"
  70. #include "archive_time_private.h"
  71. #include "archive_ppmd8_private.h"
  72. #ifndef HAVE_ZLIB_H
  73. #include "archive_crc32.h"
  74. #endif
  75. struct zip_entry {
  76. struct archive_rb_node node;
  77. struct zip_entry *next;
  78. int64_t local_header_offset;
  79. int64_t compressed_size;
  80. int64_t uncompressed_size;
  81. int64_t gid;
  82. int64_t uid;
  83. struct archive_string rsrcname;
  84. time_t mtime;
  85. time_t atime;
  86. time_t ctime;
  87. uint32_t crc32;
  88. uint16_t mode;
  89. uint16_t zip_flags; /* From GP Flags Field */
  90. unsigned char compression;
  91. unsigned char system; /* From "version written by" */
  92. unsigned char flags; /* Our extra markers. */
  93. unsigned char decdat;/* Used for Decryption check */
  94. /* WinZip AES encryption extra field should be available
  95. * when compression is 99. */
  96. struct {
  97. /* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */
  98. unsigned vendor;
  99. #define AES_VENDOR_AE_1 0x0001
  100. #define AES_VENDOR_AE_2 0x0002
  101. /* AES encryption strength:
  102. * 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */
  103. unsigned strength;
  104. /* Actual compression method. */
  105. unsigned char compression;
  106. } aes_extra;
  107. };
  108. struct trad_enc_ctx {
  109. uint32_t keys[3];
  110. };
  111. /* Bits used in zip_flags. */
  112. #define ZIP_ENCRYPTED (1 << 0)
  113. #define ZIP_LENGTH_AT_END (1 << 3) /* Also called "Streaming bit" */
  114. #define ZIP_STRONG_ENCRYPTED (1 << 6)
  115. #define ZIP_UTF8_NAME (1 << 11)
  116. /* See "7.2 Single Password Symmetric Encryption Method"
  117. in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
  118. #define ZIP_CENTRAL_DIRECTORY_ENCRYPTED (1 << 13)
  119. /* Bits used in flags. */
  120. #define LA_USED_ZIP64 (1 << 0)
  121. #define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
  122. /*
  123. * See "WinZip - AES Encryption Information"
  124. * http://www.winzip.com/aes_info.htm
  125. */
  126. /* Value used in compression method. */
  127. #define WINZIP_AES_ENCRYPTION 99
  128. /* Authentication code size. */
  129. #define AUTH_CODE_SIZE 10
  130. /**/
  131. #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
  132. struct zip {
  133. /* Structural information about the archive. */
  134. struct archive_string format_name;
  135. int64_t central_directory_offset;
  136. int64_t central_directory_offset_adjusted;
  137. size_t central_directory_entries_total;
  138. size_t central_directory_entries_on_this_disk;
  139. int has_encrypted_entries;
  140. /* List of entries (seekable Zip only) */
  141. struct zip_entry *zip_entries;
  142. struct archive_rb_tree tree;
  143. struct archive_rb_tree tree_rsrc;
  144. /* Bytes read but not yet consumed via __archive_read_consume() */
  145. size_t unconsumed;
  146. /* Information about entry we're currently reading. */
  147. struct zip_entry *entry;
  148. int64_t entry_bytes_remaining;
  149. /* These count the number of bytes actually read for the entry. */
  150. int64_t entry_compressed_bytes_read;
  151. int64_t entry_uncompressed_bytes_read;
  152. /* Running CRC32 of the decompressed and decrypted data */
  153. unsigned long computed_crc32;
  154. unsigned long (*crc32func)(unsigned long, const void *,
  155. size_t);
  156. char ignore_crc32;
  157. /* Flags to mark progress of decompression. */
  158. char decompress_init;
  159. char end_of_entry;
  160. unsigned char *uncompressed_buffer;
  161. size_t uncompressed_buffer_size;
  162. #ifdef HAVE_ZLIB_H
  163. z_stream stream;
  164. char stream_valid;
  165. #endif
  166. #if HAVE_LZMA_H && HAVE_LIBLZMA
  167. lzma_stream zipx_lzma_stream;
  168. char zipx_lzma_valid;
  169. #endif
  170. #ifdef HAVE_BZLIB_H
  171. bz_stream bzstream;
  172. char bzstream_valid;
  173. #endif
  174. #if HAVE_ZSTD_H && HAVE_LIBZSTD
  175. ZSTD_DStream *zstdstream;
  176. char zstdstream_valid;
  177. #endif
  178. IByteIn zipx_ppmd_stream;
  179. ssize_t zipx_ppmd_read_compressed;
  180. CPpmd8 ppmd8;
  181. char ppmd8_valid;
  182. char ppmd8_stream_failed;
  183. struct archive_string_conv *sconv;
  184. struct archive_string_conv *sconv_default;
  185. struct archive_string_conv *sconv_utf8;
  186. int init_default_conversion;
  187. int process_mac_extensions;
  188. char init_decryption;
  189. /* Decryption buffer. */
  190. /*
  191. * The decrypted data starts at decrypted_ptr and
  192. * extends for decrypted_bytes_remaining. Decryption
  193. * adds new data to the end of this block, data is returned
  194. * to clients from the beginning. When the block hits the
  195. * end of decrypted_buffer, it has to be shuffled back to
  196. * the beginning of the buffer.
  197. */
  198. unsigned char *decrypted_buffer;
  199. unsigned char *decrypted_ptr;
  200. size_t decrypted_buffer_size;
  201. size_t decrypted_bytes_remaining;
  202. size_t decrypted_unconsumed_bytes;
  203. /* Traditional PKWARE decryption. */
  204. struct trad_enc_ctx tctx;
  205. char tctx_valid;
  206. /* WinZip AES decryption. */
  207. /* Contexts used for AES decryption. */
  208. archive_crypto_ctx cctx;
  209. char cctx_valid;
  210. archive_hmac_sha1_ctx hctx;
  211. char hctx_valid;
  212. /* Strong encryption's decryption header information. */
  213. unsigned iv_size;
  214. unsigned alg_id;
  215. unsigned bit_len;
  216. unsigned flags;
  217. unsigned erd_size;
  218. unsigned v_size;
  219. unsigned v_crc32;
  220. uint8_t *iv;
  221. uint8_t *erd;
  222. uint8_t *v_data;
  223. };
  224. /* Many systems define min or MIN, but not all. */
  225. #define zipmin(a,b) ((a) < (b) ? (a) : (b))
  226. #ifdef HAVE_ZLIB_H
  227. static int
  228. zip_read_data_deflate(struct archive_read *a, const void **buff,
  229. size_t *size, int64_t *offset);
  230. #endif
  231. #if HAVE_LZMA_H && HAVE_LIBLZMA
  232. static int
  233. zip_read_data_zipx_lzma_alone(struct archive_read *a, const void **buff,
  234. size_t *size, int64_t *offset);
  235. #endif
  236. /* This function is used by Ppmd8_DecodeSymbol during decompression of Ppmd8
  237. * streams inside ZIP files. It has 2 purposes: one is to fetch the next
  238. * compressed byte from the stream, second one is to increase the counter how
  239. * many compressed bytes were read. */
  240. static Byte
  241. ppmd_read(void* p) {
  242. /* Get the handle to current decompression context. */
  243. struct archive_read *a = ((IByteIn*)p)->a;
  244. struct zip *zip = (struct zip*) a->format->data;
  245. ssize_t bytes_avail = 0;
  246. /* Fetch next byte. */
  247. const uint8_t* data = __archive_read_ahead(a, 1, &bytes_avail);
  248. if(bytes_avail < 1) {
  249. zip->ppmd8_stream_failed = 1;
  250. return 0;
  251. }
  252. __archive_read_consume(a, 1);
  253. /* Increment the counter. */
  254. ++zip->zipx_ppmd_read_compressed;
  255. /* Return the next compressed byte. */
  256. return data[0];
  257. }
  258. /* ------------------------------------------------------------------------ */
  259. /*
  260. Traditional PKWARE Decryption functions.
  261. */
  262. static void
  263. trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
  264. {
  265. uint8_t t;
  266. #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
  267. ctx->keys[0] = CRC32(ctx->keys[0], c);
  268. ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
  269. t = (ctx->keys[1] >> 24) & 0xff;
  270. ctx->keys[2] = CRC32(ctx->keys[2], t);
  271. #undef CRC32
  272. }
  273. static uint8_t
  274. trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
  275. {
  276. unsigned temp = ctx->keys[2] | 2;
  277. return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
  278. }
  279. static void
  280. trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
  281. size_t in_len, uint8_t *out, size_t out_len)
  282. {
  283. unsigned i, max;
  284. max = (unsigned)((in_len < out_len)? in_len: out_len);
  285. for (i = 0; i < max; i++) {
  286. uint8_t t = in[i] ^ trad_enc_decrypt_byte(ctx);
  287. out[i] = t;
  288. trad_enc_update_keys(ctx, t);
  289. }
  290. }
  291. static int
  292. trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len,
  293. const uint8_t *key, size_t key_len, uint8_t *crcchk)
  294. {
  295. uint8_t header[12];
  296. if (key_len < 12) {
  297. *crcchk = 0xff;
  298. return -1;
  299. }
  300. ctx->keys[0] = 305419896L;
  301. ctx->keys[1] = 591751049L;
  302. ctx->keys[2] = 878082192L;
  303. for (;pw_len; --pw_len)
  304. trad_enc_update_keys(ctx, *pw++);
  305. trad_enc_decrypt_update(ctx, key, 12, header, 12);
  306. /* Return the last byte for CRC check. */
  307. *crcchk = header[11];
  308. return 0;
  309. }
  310. #if 0
  311. static void
  312. crypt_derive_key_sha1(const void *p, int size, unsigned char *key,
  313. int key_size)
  314. {
  315. #define MD_SIZE 20
  316. archive_sha1_ctx ctx;
  317. unsigned char md1[MD_SIZE];
  318. unsigned char md2[MD_SIZE * 2];
  319. unsigned char mkb[64];
  320. int i;
  321. archive_sha1_init(&ctx);
  322. archive_sha1_update(&ctx, p, size);
  323. archive_sha1_final(&ctx, md1);
  324. memset(mkb, 0x36, sizeof(mkb));
  325. for (i = 0; i < MD_SIZE; i++)
  326. mkb[i] ^= md1[i];
  327. archive_sha1_init(&ctx);
  328. archive_sha1_update(&ctx, mkb, sizeof(mkb));
  329. archive_sha1_final(&ctx, md2);
  330. memset(mkb, 0x5C, sizeof(mkb));
  331. for (i = 0; i < MD_SIZE; i++)
  332. mkb[i] ^= md1[i];
  333. archive_sha1_init(&ctx);
  334. archive_sha1_update(&ctx, mkb, sizeof(mkb));
  335. archive_sha1_final(&ctx, md2 + MD_SIZE);
  336. if (key_size > 32)
  337. key_size = 32;
  338. memcpy(key, md2, key_size);
  339. #undef MD_SIZE
  340. }
  341. #endif
  342. /*
  343. * Common code for streaming or seeking modes.
  344. *
  345. * Includes code to read local file headers, decompress data
  346. * from entry bodies, and common API.
  347. */
  348. static unsigned long
  349. real_crc32(unsigned long crc, const void *buff, size_t len)
  350. {
  351. return crc32(crc, buff, (unsigned int)len);
  352. }
  353. /* Used by "ignorecrc32" option to speed up tests. */
  354. static unsigned long
  355. fake_crc32(unsigned long crc, const void *buff, size_t len)
  356. {
  357. (void)crc; /* UNUSED */
  358. (void)buff; /* UNUSED */
  359. (void)len; /* UNUSED */
  360. return 0;
  361. }
  362. static const struct {
  363. int id;
  364. const char * name;
  365. } compression_methods[] = {
  366. {0, "uncompressed"}, /* The file is stored (no compression) */
  367. {1, "shrinking"}, /* The file is Shrunk */
  368. {2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
  369. {3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
  370. {4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
  371. {5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
  372. {6, "imploded"}, /* The file is Imploded */
  373. {7, "reserved"}, /* Reserved for Tokenizing compression algorithm */
  374. {8, "deflation"}, /* The file is Deflated */
  375. {9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
  376. {10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
  377. * (old IBM TERSE) */
  378. {11, "reserved"}, /* Reserved by PKWARE */
  379. {12, "bzip"}, /* File is compressed using BZIP2 algorithm */
  380. {13, "reserved"}, /* Reserved by PKWARE */
  381. {14, "lzma"}, /* LZMA (EFS) */
  382. {15, "reserved"}, /* Reserved by PKWARE */
  383. {16, "reserved"}, /* Reserved by PKWARE */
  384. {17, "reserved"}, /* Reserved by PKWARE */
  385. {18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
  386. {19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
  387. {93, "zstd"}, /* Zstandard (zstd) Compression */
  388. {95, "xz"}, /* XZ compressed data */
  389. {96, "jpeg"}, /* JPEG compressed data */
  390. {97, "wav-pack"}, /* WavPack compressed data */
  391. {98, "ppmd-1"}, /* PPMd version I, Rev 1 */
  392. {99, "aes"} /* WinZip AES encryption */
  393. };
  394. static const char *
  395. compression_name(const int compression)
  396. {
  397. static const int num_compression_methods =
  398. sizeof(compression_methods)/sizeof(compression_methods[0]);
  399. int i=0;
  400. while(compression >= 0 && i < num_compression_methods) {
  401. if (compression_methods[i].id == compression)
  402. return compression_methods[i].name;
  403. i++;
  404. }
  405. return "??";
  406. }
  407. /*
  408. * The extra data is stored as a list of
  409. * id1+size1+data1 + id2+size2+data2 ...
  410. * triplets. id and size are 2 bytes each.
  411. */
  412. static int
  413. process_extra(struct archive_read *a, struct archive_entry *entry,
  414. const char *p, size_t extra_length, struct zip_entry* zip_entry)
  415. {
  416. unsigned offset = 0;
  417. struct zip *zip = (struct zip *)(a->format->data);
  418. if (extra_length == 0) {
  419. return ARCHIVE_OK;
  420. }
  421. if (extra_length < 4) {
  422. size_t i = 0;
  423. /* Some ZIP files may have trailing 0 bytes. Let's check they
  424. * are all 0 and ignore them instead of returning an error.
  425. *
  426. * This is not technically correct, but some ZIP files look
  427. * like this and other tools support those files - so let's
  428. * also support them.
  429. */
  430. for (; i < extra_length; i++) {
  431. if (p[i] != 0) {
  432. archive_set_error(&a->archive,
  433. ARCHIVE_ERRNO_FILE_FORMAT,
  434. "Too-small extra data: "
  435. "Need at least 4 bytes, "
  436. "but only found %d bytes",
  437. (int)extra_length);
  438. return ARCHIVE_FAILED;
  439. }
  440. }
  441. return ARCHIVE_OK;
  442. }
  443. while (offset <= extra_length - 4) {
  444. unsigned short headerid = archive_le16dec(p + offset);
  445. unsigned short datasize = archive_le16dec(p + offset + 2);
  446. offset += 4;
  447. if (offset + datasize > extra_length) {
  448. archive_set_error(&a->archive,
  449. ARCHIVE_ERRNO_FILE_FORMAT, "Extra data overflow: "
  450. "Need %d bytes but only found %d bytes",
  451. (int)datasize, (int)(extra_length - offset));
  452. return ARCHIVE_FAILED;
  453. }
  454. #ifdef DEBUG
  455. fprintf(stderr, "Header id 0x%04x, length %d\n",
  456. headerid, datasize);
  457. #endif
  458. switch (headerid) {
  459. case 0x0001:
  460. /* Zip64 extended information extra field. */
  461. zip_entry->flags |= LA_USED_ZIP64;
  462. if (zip_entry->uncompressed_size == 0xffffffff) {
  463. uint64_t t = 0;
  464. if (datasize < 8
  465. || (t = archive_le64dec(p + offset)) >
  466. INT64_MAX) {
  467. archive_set_error(&a->archive,
  468. ARCHIVE_ERRNO_FILE_FORMAT,
  469. "Malformed 64-bit "
  470. "uncompressed size");
  471. return ARCHIVE_FAILED;
  472. }
  473. zip_entry->uncompressed_size = t;
  474. offset += 8;
  475. datasize -= 8;
  476. }
  477. if (zip_entry->compressed_size == 0xffffffff) {
  478. uint64_t t = 0;
  479. if (datasize < 8
  480. || (t = archive_le64dec(p + offset)) >
  481. INT64_MAX) {
  482. archive_set_error(&a->archive,
  483. ARCHIVE_ERRNO_FILE_FORMAT,
  484. "Malformed 64-bit "
  485. "compressed size");
  486. return ARCHIVE_FAILED;
  487. }
  488. zip_entry->compressed_size = t;
  489. offset += 8;
  490. datasize -= 8;
  491. }
  492. if (zip_entry->local_header_offset == 0xffffffff) {
  493. uint64_t t = 0;
  494. if (datasize < 8
  495. || (t = archive_le64dec(p + offset)) >
  496. INT64_MAX) {
  497. archive_set_error(&a->archive,
  498. ARCHIVE_ERRNO_FILE_FORMAT,
  499. "Malformed 64-bit "
  500. "local header offset");
  501. return ARCHIVE_FAILED;
  502. }
  503. zip_entry->local_header_offset = t;
  504. offset += 8;
  505. datasize -= 8;
  506. }
  507. /* archive_le32dec(p + offset) gives disk
  508. * on which file starts, but we don't handle
  509. * multi-volume Zip files. */
  510. break;
  511. #ifdef DEBUG
  512. case 0x0017:
  513. {
  514. /* Strong encryption field. */
  515. if (archive_le16dec(p + offset) == 2) {
  516. unsigned algId =
  517. archive_le16dec(p + offset + 2);
  518. unsigned bitLen =
  519. archive_le16dec(p + offset + 4);
  520. int flags =
  521. archive_le16dec(p + offset + 6);
  522. fprintf(stderr, "algId=0x%04x, bitLen=%u, "
  523. "flgas=%d\n", algId, bitLen,flags);
  524. }
  525. break;
  526. }
  527. #endif
  528. case 0x5455:
  529. {
  530. /* Extended time field "UT". */
  531. int flags;
  532. if (datasize == 0) {
  533. archive_set_error(&a->archive,
  534. ARCHIVE_ERRNO_FILE_FORMAT,
  535. "Incomplete extended time field");
  536. return ARCHIVE_FAILED;
  537. }
  538. flags = p[offset];
  539. offset++;
  540. datasize--;
  541. /* Flag bits indicate which dates are present. */
  542. if (flags & 0x01)
  543. {
  544. #ifdef DEBUG
  545. fprintf(stderr, "mtime: %lld -> %d\n",
  546. (long long)zip_entry->mtime,
  547. archive_le32dec(p + offset));
  548. #endif
  549. if (datasize < 4)
  550. break;
  551. zip_entry->mtime = archive_le32dec(p + offset);
  552. offset += 4;
  553. datasize -= 4;
  554. }
  555. if (flags & 0x02)
  556. {
  557. if (datasize < 4)
  558. break;
  559. zip_entry->atime = archive_le32dec(p + offset);
  560. offset += 4;
  561. datasize -= 4;
  562. }
  563. if (flags & 0x04)
  564. {
  565. if (datasize < 4)
  566. break;
  567. zip_entry->ctime = archive_le32dec(p + offset);
  568. offset += 4;
  569. datasize -= 4;
  570. }
  571. break;
  572. }
  573. case 0x5855:
  574. {
  575. /* Info-ZIP Unix Extra Field (old version) "UX". */
  576. if (datasize >= 8) {
  577. zip_entry->atime = archive_le32dec(p + offset);
  578. zip_entry->mtime =
  579. archive_le32dec(p + offset + 4);
  580. }
  581. if (datasize >= 12) {
  582. zip_entry->uid =
  583. archive_le16dec(p + offset + 8);
  584. zip_entry->gid =
  585. archive_le16dec(p + offset + 10);
  586. }
  587. break;
  588. }
  589. case 0x6c78:
  590. {
  591. /* Experimental 'xl' field */
  592. /*
  593. * Introduced Dec 2013 to provide a way to
  594. * include external file attributes (and other
  595. * fields that ordinarily appear only in
  596. * central directory) in local file header.
  597. * This provides file type and permission
  598. * information necessary to support full
  599. * streaming extraction. Currently being
  600. * discussed with other Zip developers
  601. * ... subject to change.
  602. *
  603. * Format:
  604. * The field starts with a bitmap that specifies
  605. * which additional fields are included. The
  606. * bitmap is variable length and can be extended in
  607. * the future.
  608. *
  609. * n bytes - feature bitmap: first byte has low-order
  610. * 7 bits. If high-order bit is set, a subsequent
  611. * byte holds the next 7 bits, etc.
  612. *
  613. * if bitmap & 1, 2 byte "version made by"
  614. * if bitmap & 2, 2 byte "internal file attributes"
  615. * if bitmap & 4, 4 byte "external file attributes"
  616. * if bitmap & 8, 2 byte comment length + n byte
  617. * comment
  618. */
  619. int bitmap, bitmap_last;
  620. if (datasize < 1)
  621. break;
  622. bitmap_last = bitmap = 0xff & p[offset];
  623. offset += 1;
  624. datasize -= 1;
  625. /* We only support first 7 bits of bitmap; skip rest. */
  626. while ((bitmap_last & 0x80) != 0
  627. && datasize >= 1) {
  628. bitmap_last = p[offset];
  629. offset += 1;
  630. datasize -= 1;
  631. }
  632. if (bitmap & 1) {
  633. /* 2 byte "version made by" */
  634. if (datasize < 2)
  635. break;
  636. zip_entry->system
  637. = archive_le16dec(p + offset) >> 8;
  638. offset += 2;
  639. datasize -= 2;
  640. }
  641. if (bitmap & 2) {
  642. /* 2 byte "internal file attributes" */
  643. uint32_t internal_attributes;
  644. if (datasize < 2)
  645. break;
  646. internal_attributes
  647. = archive_le16dec(p + offset);
  648. /* Not used by libarchive at present. */
  649. (void)internal_attributes; /* UNUSED */
  650. offset += 2;
  651. datasize -= 2;
  652. }
  653. if (bitmap & 4) {
  654. /* 4 byte "external file attributes" */
  655. uint32_t external_attributes;
  656. if (datasize < 4)
  657. break;
  658. external_attributes
  659. = archive_le32dec(p + offset);
  660. if (zip_entry->system == 3) {
  661. zip_entry->mode
  662. = external_attributes >> 16;
  663. } else if (zip_entry->system == 0) {
  664. // Interpret MSDOS directory bit
  665. if (0x10 == (external_attributes &
  666. 0x10)) {
  667. zip_entry->mode =
  668. AE_IFDIR | 0775;
  669. } else {
  670. zip_entry->mode =
  671. AE_IFREG | 0664;
  672. }
  673. if (0x01 == (external_attributes &
  674. 0x01)) {
  675. /* Read-only bit;
  676. * strip write permissions */
  677. zip_entry->mode &= 0555;
  678. }
  679. } else {
  680. zip_entry->mode = 0;
  681. }
  682. offset += 4;
  683. datasize -= 4;
  684. }
  685. if (bitmap & 8) {
  686. /* 2 byte comment length + comment */
  687. uint32_t comment_length;
  688. if (datasize < 2)
  689. break;
  690. comment_length
  691. = archive_le16dec(p + offset);
  692. offset += 2;
  693. datasize -= 2;
  694. if (datasize < comment_length)
  695. break;
  696. /* Comment is not supported by libarchive */
  697. offset += comment_length;
  698. datasize -= comment_length;
  699. }
  700. break;
  701. }
  702. case 0x7075:
  703. {
  704. /* Info-ZIP Unicode Path Extra Field. */
  705. if (datasize < 5 || entry == NULL)
  706. break;
  707. offset += 5;
  708. datasize -= 5;
  709. /* The path name in this field is always encoded
  710. * in UTF-8. */
  711. if (zip->sconv_utf8 == NULL) {
  712. zip->sconv_utf8 =
  713. archive_string_conversion_from_charset(
  714. &a->archive, "UTF-8", 1);
  715. /* If the converter from UTF-8 is not
  716. * available, then the path name from the main
  717. * field will more likely be correct. */
  718. if (zip->sconv_utf8 == NULL)
  719. break;
  720. }
  721. /* Make sure the CRC32 of the filename matches. */
  722. if (!zip->ignore_crc32) {
  723. const char *cp = archive_entry_pathname(entry);
  724. if (cp) {
  725. unsigned long file_crc =
  726. zip->crc32func(0, cp, strlen(cp));
  727. unsigned long utf_crc =
  728. archive_le32dec(p + offset - 4);
  729. if (file_crc != utf_crc) {
  730. #ifdef DEBUG
  731. fprintf(stderr,
  732. "CRC filename mismatch; "
  733. "CDE is %lx, but UTF8 "
  734. "is outdated with %lx\n",
  735. file_crc, utf_crc);
  736. #endif
  737. break;
  738. }
  739. }
  740. }
  741. if (archive_entry_copy_pathname_l(entry,
  742. p + offset, datasize, zip->sconv_utf8) != 0) {
  743. /* Ignore the error, and fallback to the path
  744. * name from the main field. */
  745. #ifdef DEBUG
  746. fprintf(stderr, "Failed to read the ZIP "
  747. "0x7075 extra field path.\n");
  748. #endif
  749. }
  750. break;
  751. }
  752. case 0x7855:
  753. /* Info-ZIP Unix Extra Field (type 2) "Ux". */
  754. #ifdef DEBUG
  755. fprintf(stderr, "uid %d gid %d\n",
  756. archive_le16dec(p + offset),
  757. archive_le16dec(p + offset + 2));
  758. #endif
  759. if (datasize >= 2)
  760. zip_entry->uid = archive_le16dec(p + offset);
  761. if (datasize >= 4)
  762. zip_entry->gid =
  763. archive_le16dec(p + offset + 2);
  764. break;
  765. case 0x7875:
  766. {
  767. /* Info-Zip Unix Extra Field (type 3) "ux". */
  768. int uidsize = 0, gidsize = 0;
  769. /* TODO: support arbitrary uidsize/gidsize. */
  770. if (datasize >= 1 && p[offset] == 1) {/* version=1 */
  771. if (datasize >= 4) {
  772. /* get a uid size. */
  773. uidsize = 0xff & (int)p[offset+1];
  774. if (uidsize == 2)
  775. zip_entry->uid =
  776. archive_le16dec(
  777. p + offset + 2);
  778. else if (uidsize == 4 && datasize >= 6)
  779. zip_entry->uid =
  780. archive_le32dec(
  781. p + offset + 2);
  782. }
  783. if (datasize >= (2 + uidsize + 3)) {
  784. /* get a gid size. */
  785. gidsize = 0xff &
  786. (int)p[offset+2+uidsize];
  787. if (gidsize == 2)
  788. zip_entry->gid =
  789. archive_le16dec(
  790. p+offset+2+uidsize+1);
  791. else if (gidsize == 4 &&
  792. datasize >= (2 + uidsize + 5))
  793. zip_entry->gid =
  794. archive_le32dec(
  795. p+offset+2+uidsize+1);
  796. }
  797. }
  798. break;
  799. }
  800. case 0x9901:
  801. /* WinZip AES extra data field. */
  802. if (datasize < 6) {
  803. archive_set_error(&a->archive,
  804. ARCHIVE_ERRNO_FILE_FORMAT,
  805. "Incomplete AES field");
  806. return ARCHIVE_FAILED;
  807. }
  808. if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
  809. /* Vendor version. */
  810. zip_entry->aes_extra.vendor =
  811. archive_le16dec(p + offset);
  812. /* AES encryption strength. */
  813. zip_entry->aes_extra.strength = p[offset + 4];
  814. /* Actual compression method. */
  815. zip_entry->aes_extra.compression =
  816. p[offset + 5];
  817. }
  818. break;
  819. default:
  820. break;
  821. }
  822. offset += datasize;
  823. }
  824. return ARCHIVE_OK;
  825. }
  826. /*
  827. * Assumes file pointer is at beginning of local file header.
  828. */
  829. static int
  830. zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
  831. struct zip *zip)
  832. {
  833. const char *p;
  834. const void *h;
  835. const wchar_t *wp;
  836. const char *cp;
  837. size_t len, filename_length, extra_length;
  838. struct archive_string_conv *sconv;
  839. struct zip_entry *zip_entry = zip->entry;
  840. struct zip_entry zip_entry_central_dir;
  841. int ret = ARCHIVE_OK;
  842. char version;
  843. /* Save a copy of the original for consistency checks. */
  844. zip_entry_central_dir = *zip_entry;
  845. zip->decompress_init = 0;
  846. zip->end_of_entry = 0;
  847. zip->entry_uncompressed_bytes_read = 0;
  848. zip->entry_compressed_bytes_read = 0;
  849. zip->computed_crc32 = zip->crc32func(0, NULL, 0);
  850. /* Setup default conversion. */
  851. if (zip->sconv == NULL && !zip->init_default_conversion) {
  852. zip->sconv_default =
  853. archive_string_default_conversion_for_read(&(a->archive));
  854. zip->init_default_conversion = 1;
  855. }
  856. if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
  857. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  858. "Truncated ZIP file header");
  859. return (ARCHIVE_FATAL);
  860. }
  861. if (memcmp(p, "PK\003\004", 4) != 0) {
  862. archive_set_error(&a->archive, -1, "Damaged Zip archive");
  863. return ARCHIVE_FATAL;
  864. }
  865. version = p[4];
  866. zip_entry->system = p[5];
  867. zip_entry->zip_flags = archive_le16dec(p + 6);
  868. if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
  869. zip->has_encrypted_entries = 1;
  870. archive_entry_set_is_data_encrypted(entry, 1);
  871. if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
  872. zip_entry->zip_flags & ZIP_ENCRYPTED &&
  873. zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
  874. archive_entry_set_is_metadata_encrypted(entry, 1);
  875. return ARCHIVE_FATAL;
  876. }
  877. }
  878. zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED);
  879. zip_entry->compression = (char)archive_le16dec(p + 8);
  880. zip_entry->mtime = dos_to_unix(archive_le32dec(p + 10));
  881. zip_entry->crc32 = archive_le32dec(p + 14);
  882. if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  883. zip_entry->decdat = p[11];
  884. else
  885. zip_entry->decdat = p[17];
  886. zip_entry->compressed_size = archive_le32dec(p + 18);
  887. zip_entry->uncompressed_size = archive_le32dec(p + 22);
  888. filename_length = archive_le16dec(p + 26);
  889. extra_length = archive_le16dec(p + 28);
  890. __archive_read_consume(a, 30);
  891. /* Read the filename. */
  892. if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
  893. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  894. "Truncated ZIP file header");
  895. return (ARCHIVE_FATAL);
  896. }
  897. if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
  898. /* The filename is stored to be UTF-8. */
  899. if (zip->sconv_utf8 == NULL) {
  900. zip->sconv_utf8 =
  901. archive_string_conversion_from_charset(
  902. &a->archive, "UTF-8", 1);
  903. if (zip->sconv_utf8 == NULL)
  904. return (ARCHIVE_FATAL);
  905. }
  906. sconv = zip->sconv_utf8;
  907. } else if (zip->sconv != NULL)
  908. sconv = zip->sconv;
  909. else
  910. sconv = zip->sconv_default;
  911. if (archive_entry_copy_pathname_l(entry,
  912. h, filename_length, sconv) != 0) {
  913. if (errno == ENOMEM) {
  914. archive_set_error(&a->archive, ENOMEM,
  915. "Can't allocate memory for Pathname");
  916. return (ARCHIVE_FATAL);
  917. }
  918. archive_set_error(&a->archive,
  919. ARCHIVE_ERRNO_FILE_FORMAT,
  920. "Pathname cannot be converted "
  921. "from %s to current locale.",
  922. archive_string_conversion_charset_name(sconv));
  923. ret = ARCHIVE_WARN;
  924. }
  925. __archive_read_consume(a, filename_length);
  926. /* Read the extra data. */
  927. if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
  928. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  929. "Truncated ZIP file header");
  930. return (ARCHIVE_FATAL);
  931. }
  932. if (ARCHIVE_OK != process_extra(a, entry, h, extra_length,
  933. zip_entry)) {
  934. return ARCHIVE_FATAL;
  935. }
  936. __archive_read_consume(a, extra_length);
  937. /* Work around a bug in Info-Zip: When reading from a pipe, it
  938. * stats the pipe instead of synthesizing a file entry. */
  939. if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
  940. zip_entry->mode &= ~ AE_IFMT;
  941. zip_entry->mode |= AE_IFREG;
  942. }
  943. /* If the mode is totally empty, set some sane default. */
  944. if (zip_entry->mode == 0) {
  945. zip_entry->mode |= 0664;
  946. }
  947. /* Windows archivers sometimes use backslash as the directory
  948. * separator. Normalize to slash. */
  949. if (zip_entry->system == 0 &&
  950. (wp = archive_entry_pathname_w(entry)) != NULL) {
  951. if (wcschr(wp, L'/') == NULL && wcschr(wp, L'\\') != NULL) {
  952. size_t i;
  953. struct archive_wstring s;
  954. archive_string_init(&s);
  955. archive_wstrcpy(&s, wp);
  956. for (i = 0; i < archive_strlen(&s); i++) {
  957. if (s.s[i] == '\\')
  958. s.s[i] = '/';
  959. }
  960. archive_entry_copy_pathname_w(entry, s.s);
  961. archive_wstring_free(&s);
  962. }
  963. }
  964. /* Make sure that entries with a trailing '/' are marked as directories
  965. * even if the External File Attributes contains bogus values. If this
  966. * is not a directory and there is no type, assume a regular file. */
  967. if ((zip_entry->mode & AE_IFMT) != AE_IFDIR) {
  968. int has_slash;
  969. wp = archive_entry_pathname_w(entry);
  970. if (wp != NULL) {
  971. len = wcslen(wp);
  972. has_slash = len > 0 && wp[len - 1] == L'/';
  973. } else {
  974. cp = archive_entry_pathname(entry);
  975. len = (cp != NULL)?strlen(cp):0;
  976. has_slash = len > 0 && cp[len - 1] == '/';
  977. }
  978. /* Correct file type as needed. */
  979. if (has_slash) {
  980. zip_entry->mode &= ~AE_IFMT;
  981. zip_entry->mode |= AE_IFDIR;
  982. zip_entry->mode |= 0111;
  983. } else if ((zip_entry->mode & AE_IFMT) == 0) {
  984. zip_entry->mode |= AE_IFREG;
  985. }
  986. }
  987. /* Make sure directories end in '/' */
  988. if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) {
  989. wp = archive_entry_pathname_w(entry);
  990. if (wp != NULL) {
  991. len = wcslen(wp);
  992. if (len > 0 && wp[len - 1] != L'/') {
  993. struct archive_wstring s;
  994. archive_string_init(&s);
  995. archive_wstrcat(&s, wp);
  996. archive_wstrappend_wchar(&s, L'/');
  997. archive_entry_copy_pathname_w(entry, s.s);
  998. archive_wstring_free(&s);
  999. }
  1000. } else {
  1001. cp = archive_entry_pathname(entry);
  1002. len = (cp != NULL)?strlen(cp):0;
  1003. if (len > 0 && cp[len - 1] != '/') {
  1004. struct archive_string s;
  1005. archive_string_init(&s);
  1006. archive_strcat(&s, cp);
  1007. archive_strappend_char(&s, '/');
  1008. archive_entry_set_pathname(entry, s.s);
  1009. archive_string_free(&s);
  1010. }
  1011. }
  1012. }
  1013. if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
  1014. /* If this came from the central dir, its size info
  1015. * is definitive, so ignore the length-at-end flag. */
  1016. zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
  1017. /* If local header is missing a value, use the one from
  1018. the central directory. If both have it, warn about
  1019. mismatches. */
  1020. if (zip_entry->crc32 == 0) {
  1021. zip_entry->crc32 = zip_entry_central_dir.crc32;
  1022. } else if (!zip->ignore_crc32
  1023. && zip_entry->crc32 != zip_entry_central_dir.crc32) {
  1024. archive_set_error(&a->archive,
  1025. ARCHIVE_ERRNO_FILE_FORMAT,
  1026. "Inconsistent CRC32 values");
  1027. ret = ARCHIVE_WARN;
  1028. }
  1029. if (zip_entry->compressed_size == 0
  1030. || zip_entry->compressed_size == 0xffffffff) {
  1031. zip_entry->compressed_size
  1032. = zip_entry_central_dir.compressed_size;
  1033. } else if (zip_entry->compressed_size
  1034. != zip_entry_central_dir.compressed_size) {
  1035. archive_set_error(&a->archive,
  1036. ARCHIVE_ERRNO_FILE_FORMAT,
  1037. "Inconsistent compressed size: "
  1038. "%jd in central directory, %jd in local header",
  1039. (intmax_t)zip_entry_central_dir.compressed_size,
  1040. (intmax_t)zip_entry->compressed_size);
  1041. ret = ARCHIVE_WARN;
  1042. }
  1043. if (zip_entry->uncompressed_size == 0 ||
  1044. zip_entry->uncompressed_size == 0xffffffff) {
  1045. zip_entry->uncompressed_size
  1046. = zip_entry_central_dir.uncompressed_size;
  1047. } else if (zip_entry->uncompressed_size
  1048. != zip_entry_central_dir.uncompressed_size) {
  1049. archive_set_error(&a->archive,
  1050. ARCHIVE_ERRNO_FILE_FORMAT,
  1051. "Inconsistent uncompressed size: "
  1052. "%jd in central directory, %jd in local header",
  1053. (intmax_t)zip_entry_central_dir.uncompressed_size,
  1054. (intmax_t)zip_entry->uncompressed_size);
  1055. ret = ARCHIVE_WARN;
  1056. }
  1057. }
  1058. /* Populate some additional entry fields: */
  1059. archive_entry_set_mode(entry, zip_entry->mode);
  1060. archive_entry_set_uid(entry, zip_entry->uid);
  1061. archive_entry_set_gid(entry, zip_entry->gid);
  1062. archive_entry_set_mtime(entry, zip_entry->mtime, 0);
  1063. archive_entry_set_ctime(entry, zip_entry->ctime, 0);
  1064. archive_entry_set_atime(entry, zip_entry->atime, 0);
  1065. if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
  1066. size_t linkname_length;
  1067. if (zip_entry->compressed_size > 64 * 1024) {
  1068. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1069. "Zip file with oversized link entry");
  1070. return ARCHIVE_FATAL;
  1071. }
  1072. linkname_length = (size_t)zip_entry->compressed_size;
  1073. archive_entry_set_size(entry, 0);
  1074. // take into account link compression if any
  1075. size_t linkname_full_length = linkname_length;
  1076. if (zip->entry->compression != 0)
  1077. {
  1078. // symlink target string appeared to be compressed
  1079. int status = ARCHIVE_FATAL;
  1080. const void *uncompressed_buffer = NULL;
  1081. switch (zip->entry->compression)
  1082. {
  1083. #if HAVE_ZLIB_H
  1084. case 8: /* Deflate compression. */
  1085. zip->entry_bytes_remaining = zip_entry->compressed_size;
  1086. status = zip_read_data_deflate(a, &uncompressed_buffer,
  1087. &linkname_full_length, NULL);
  1088. break;
  1089. #endif
  1090. #if HAVE_LZMA_H && HAVE_LIBLZMA
  1091. case 14: /* ZIPx LZMA compression. */
  1092. /*(see zip file format specification, section 4.4.5)*/
  1093. zip->entry_bytes_remaining = zip_entry->compressed_size;
  1094. status = zip_read_data_zipx_lzma_alone(a, &uncompressed_buffer,
  1095. &linkname_full_length, NULL);
  1096. break;
  1097. #endif
  1098. default: /* Unsupported compression. */
  1099. break;
  1100. }
  1101. if (status == ARCHIVE_OK)
  1102. {
  1103. p = uncompressed_buffer;
  1104. }
  1105. else
  1106. {
  1107. archive_set_error(&a->archive,
  1108. ARCHIVE_ERRNO_FILE_FORMAT,
  1109. "Unsupported ZIP compression method "
  1110. "during decompression of link entry (%d: %s)",
  1111. zip->entry->compression,
  1112. compression_name(zip->entry->compression));
  1113. return ARCHIVE_FAILED;
  1114. }
  1115. }
  1116. else
  1117. {
  1118. p = __archive_read_ahead(a, linkname_length, NULL);
  1119. }
  1120. if (p == NULL) {
  1121. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1122. "Truncated Zip file");
  1123. return ARCHIVE_FATAL;
  1124. }
  1125. sconv = zip->sconv;
  1126. if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
  1127. sconv = zip->sconv_utf8;
  1128. if (sconv == NULL)
  1129. sconv = zip->sconv_default;
  1130. if (archive_entry_copy_symlink_l(entry, p, linkname_full_length,
  1131. sconv) != 0) {
  1132. if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
  1133. (zip->entry->zip_flags & ZIP_UTF8_NAME))
  1134. archive_entry_copy_symlink_l(entry, p,
  1135. linkname_full_length, NULL);
  1136. if (errno == ENOMEM) {
  1137. archive_set_error(&a->archive, ENOMEM,
  1138. "Can't allocate memory for Symlink");
  1139. return (ARCHIVE_FATAL);
  1140. }
  1141. /*
  1142. * Since there is no character-set regulation for
  1143. * symlink name, do not report the conversion error
  1144. * in an automatic conversion.
  1145. */
  1146. if (sconv != zip->sconv_utf8 ||
  1147. (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
  1148. archive_set_error(&a->archive,
  1149. ARCHIVE_ERRNO_FILE_FORMAT,
  1150. "Symlink cannot be converted "
  1151. "from %s to current locale.",
  1152. archive_string_conversion_charset_name(
  1153. sconv));
  1154. ret = ARCHIVE_WARN;
  1155. }
  1156. }
  1157. zip_entry->uncompressed_size = zip_entry->compressed_size = 0;
  1158. if (__archive_read_consume(a, linkname_length) < 0) {
  1159. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1160. "Read error skipping symlink target name");
  1161. return ARCHIVE_FATAL;
  1162. }
  1163. } else if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  1164. || (zip_entry->uncompressed_size > 0
  1165. && zip_entry->uncompressed_size != 0xffffffff)) {
  1166. /* Set the size only if it's meaningful. */
  1167. archive_entry_set_size(entry, zip_entry->uncompressed_size);
  1168. }
  1169. zip->entry_bytes_remaining = zip_entry->compressed_size;
  1170. /* If there's no body, force read_data() to return EOF immediately. */
  1171. if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  1172. && zip->entry_bytes_remaining < 1)
  1173. zip->end_of_entry = 1;
  1174. /* Set up a more descriptive format name. */
  1175. archive_string_empty(&zip->format_name);
  1176. archive_string_sprintf(&zip->format_name, "ZIP %d.%d (%s)",
  1177. version / 10, version % 10,
  1178. compression_name(zip->entry->compression));
  1179. a->archive.archive_format_name = zip->format_name.s;
  1180. return (ret);
  1181. }
  1182. static int
  1183. check_authentication_code(struct archive_read *a, const void *_p)
  1184. {
  1185. struct zip *zip = (struct zip *)(a->format->data);
  1186. /* Check authentication code. */
  1187. if (zip->hctx_valid) {
  1188. const void *p;
  1189. uint8_t hmac[20];
  1190. size_t hmac_len = 20;
  1191. int cmp;
  1192. archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
  1193. if (_p == NULL) {
  1194. /* Read authentication code. */
  1195. p = __archive_read_ahead(a, AUTH_CODE_SIZE, NULL);
  1196. if (p == NULL) {
  1197. archive_set_error(&a->archive,
  1198. ARCHIVE_ERRNO_FILE_FORMAT,
  1199. "Truncated ZIP file data");
  1200. return (ARCHIVE_FATAL);
  1201. }
  1202. } else {
  1203. p = _p;
  1204. }
  1205. cmp = memcmp(hmac, p, AUTH_CODE_SIZE);
  1206. __archive_read_consume(a, AUTH_CODE_SIZE);
  1207. if (cmp != 0) {
  1208. archive_set_error(&a->archive,
  1209. ARCHIVE_ERRNO_MISC,
  1210. "ZIP bad Authentication code");
  1211. return (ARCHIVE_WARN);
  1212. }
  1213. }
  1214. return (ARCHIVE_OK);
  1215. }
  1216. /*
  1217. * The Zip end-of-file marker is inherently ambiguous. The specification
  1218. * in APPNOTE.TXT allows any of four possible formats, and there is no
  1219. * guaranteed-correct way for a reader to know a priori which one the writer
  1220. * will have used. The four formats are:
  1221. * 1. 32-bit format with an initial PK78 marker
  1222. * 2. 32-bit format without that marker
  1223. * 3. 64-bit format with the marker
  1224. * 4. 64-bit format without the marker
  1225. *
  1226. * Mark Adler's `sunzip` streaming unzip program solved this ambiguity
  1227. * by just looking at every possible combination and accepting the
  1228. * longest one that matches the expected values. His approach always
  1229. * consumes the longest possible matching EOF marker, based on an
  1230. * analysis of all the possible failures and how the values could
  1231. * overlap.
  1232. *
  1233. * For example, suppose both of the first two formats listed
  1234. * above match. In that case, we know the next four
  1235. * 32-bit words match this pattern:
  1236. * ```
  1237. * [PK\07\08] [CRC32] [compressed size] [uncompressed size]
  1238. * ```
  1239. * but we know they must also match this pattern:
  1240. * ```
  1241. * [CRC32] [compressed size] [uncompressed size] [other PK marker]
  1242. * ```
  1243. *
  1244. * Since the first word here matches both the PK78 signature in the
  1245. * first form and the CRC32 in the second, we know those two values
  1246. * are equal, the CRC32 must be exactly 0x08074b50. Similarly, the
  1247. * compressed and uncompressed size must also be exactly this value.
  1248. * So we know these four words are all 0x08074b50. If we were to
  1249. * accept the shorter pattern, it would be immediately followed by
  1250. * another PK78 marker, which is not possible in a well-formed ZIP
  1251. * archive unless there is garbage between entries. This implies we
  1252. * should not accept the shorter form in such a case; we should accept
  1253. * the longer form.
  1254. *
  1255. * If the second and third possibilities above both match, we
  1256. * have a slightly different situation. The following words
  1257. * must match both the 32-bit format
  1258. * ```
  1259. * [CRC32] [compressed size] [uncompressed size] [other PK marker]
  1260. * ```
  1261. * and the 64-bit format
  1262. * ```
  1263. * [CRC32] [compressed low] [compressed high] [uncompressed low] [uncompressed high] [other PK marker]
  1264. * ```
  1265. * Since the 32-bit and 64-bit compressed sizes both match, the
  1266. * actual size must fit in 32 bits, which implies the high-order
  1267. * word of the compressed size is zero. So we know the uncompressed
  1268. * low word is zero, which again implies that if we accept the shorter
  1269. * format, there will not be a valid PK marker following it.
  1270. *
  1271. * Similar considerations rule out the shorter form in every other
  1272. * possibly-ambiguous pair. So if two of the four possible formats
  1273. * match, we should accept the longer option.
  1274. *
  1275. * If none of the four formats matches, we know the archive must be
  1276. * corrupted in some fashion. In particular, it's possible that the
  1277. * length-at-end bit was incorrect and we should not really be looking
  1278. * for an EOF marker at all. To allow for this possibility, we
  1279. * evaluate the following words to collect data for a later error
  1280. * report but do not consume any bytes. We instead rely on the later
  1281. * search for a new PK marker to re-sync to the next well-formed
  1282. * entry.
  1283. */
  1284. static void
  1285. consume_end_of_file_marker(struct archive_read *a, struct zip *zip)
  1286. {
  1287. const char *marker;
  1288. const char *p;
  1289. uint64_t compressed32, uncompressed32;
  1290. uint64_t compressed64, uncompressed64;
  1291. uint64_t compressed_actual, uncompressed_actual;
  1292. uint32_t crc32_actual;
  1293. const uint32_t PK78 = 0x08074B50ULL;
  1294. uint8_t crc32_ignored, crc32_may_be_zero;
  1295. /* If there shouldn't be a marker, don't consume it. */
  1296. if ((zip->entry->zip_flags & ZIP_LENGTH_AT_END) == 0) {
  1297. return;
  1298. }
  1299. /* The longest Zip end-of-file record is 24 bytes. Since an
  1300. * end-of-file record can never appear at the end of the
  1301. * archive, we know 24 bytes will be available unless
  1302. * the archive is severely truncated. */
  1303. if (NULL == (marker = __archive_read_ahead(a, 24, NULL))) {
  1304. return;
  1305. }
  1306. p = marker;
  1307. /* The end-of-file record comprises:
  1308. * = Optional PK\007\010 marker
  1309. * = 4-byte CRC32
  1310. * = Compressed size
  1311. * = Uncompressed size
  1312. *
  1313. * The last two fields are either both 32 bits or both 64
  1314. * bits. We check all possible layouts and accept any one
  1315. * that gives us a complete match, else we make a best-effort
  1316. * attempt to parse out the pieces.
  1317. */
  1318. /* CRC32 checking can be tricky:
  1319. * * Test suites sometimes ignore the CRC32
  1320. * * AES AE-2 always writes zero for the CRC32
  1321. * * AES AE-1 sometimes writes zero for the CRC32
  1322. */
  1323. crc32_ignored = zip->ignore_crc32;
  1324. crc32_may_be_zero = 0;
  1325. crc32_actual = zip->computed_crc32;
  1326. if (zip->hctx_valid) {
  1327. switch (zip->entry->aes_extra.vendor) {
  1328. case AES_VENDOR_AE_2:
  1329. crc32_actual = 0;
  1330. break;
  1331. case AES_VENDOR_AE_1:
  1332. default:
  1333. crc32_may_be_zero = 1;
  1334. break;
  1335. }
  1336. }
  1337. /* Values computed from the actual data in the archive. */
  1338. compressed_actual = (uint64_t)zip->entry_compressed_bytes_read;
  1339. uncompressed_actual = (uint64_t)zip->entry_uncompressed_bytes_read;
  1340. /* Longest: PK78 marker, all 64-bit fields (24 bytes total) */
  1341. if (archive_le32dec(p) == PK78
  1342. && ((archive_le32dec(p + 4) == crc32_actual)
  1343. || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
  1344. || crc32_ignored)
  1345. && (archive_le64dec(p + 8) == compressed_actual)
  1346. && (archive_le64dec(p + 16) == uncompressed_actual)) {
  1347. if (!crc32_ignored) {
  1348. zip->entry->crc32 = crc32_actual;
  1349. }
  1350. zip->entry->compressed_size = compressed_actual;
  1351. zip->entry->uncompressed_size = uncompressed_actual;
  1352. zip->unconsumed += 24;
  1353. return;
  1354. }
  1355. /* No PK78 marker, 64-bit fields (20 bytes total) */
  1356. if (((archive_le32dec(p) == crc32_actual)
  1357. || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
  1358. || crc32_ignored)
  1359. && (archive_le64dec(p + 4) == compressed_actual)
  1360. && (archive_le64dec(p + 12) == uncompressed_actual)) {
  1361. if (!crc32_ignored) {
  1362. zip->entry->crc32 = crc32_actual;
  1363. }
  1364. zip->entry->compressed_size = compressed_actual;
  1365. zip->entry->uncompressed_size = uncompressed_actual;
  1366. zip->unconsumed += 20;
  1367. return;
  1368. }
  1369. /* PK78 marker and 32-bit fields (16 bytes total) */
  1370. if (archive_le32dec(p) == PK78
  1371. && ((archive_le32dec(p + 4) == crc32_actual)
  1372. || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
  1373. || crc32_ignored)
  1374. && (archive_le32dec(p + 8) == compressed_actual)
  1375. && (archive_le32dec(p + 12) == uncompressed_actual)) {
  1376. if (!crc32_ignored) {
  1377. zip->entry->crc32 = crc32_actual;
  1378. }
  1379. zip->entry->compressed_size = compressed_actual;
  1380. zip->entry->uncompressed_size = uncompressed_actual;
  1381. zip->unconsumed += 16;
  1382. return;
  1383. }
  1384. /* Shortest: No PK78 marker, all 32-bit fields (12 bytes total) */
  1385. if (((archive_le32dec(p) == crc32_actual)
  1386. || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
  1387. || crc32_ignored)
  1388. && (archive_le32dec(p + 4) == compressed_actual)
  1389. && (archive_le32dec(p + 8) == uncompressed_actual)) {
  1390. if (!crc32_ignored) {
  1391. zip->entry->crc32 = crc32_actual;
  1392. }
  1393. zip->entry->compressed_size = compressed_actual;
  1394. zip->entry->uncompressed_size = uncompressed_actual;
  1395. zip->unconsumed += 12;
  1396. return;
  1397. }
  1398. /* If none of the above patterns gives us a full exact match,
  1399. * then there's something definitely amiss. The fallback code
  1400. * below will parse out some plausible values for error
  1401. * reporting purposes. Note that this won't actually
  1402. * consume anything:
  1403. *
  1404. * = If there really is a marker here, the logic to resync to
  1405. * the next entry will suffice to skip it.
  1406. *
  1407. * = There might not really be a marker: Corruption or bugs
  1408. * may have set the length-at-end bit without a marker ever
  1409. * having actually been written. In this case, we
  1410. * explicitly should not consume any bytes, since that would
  1411. * prevent us from correctly reading the next entry.
  1412. */
  1413. if (archive_le32dec(p) == PK78) {
  1414. p += 4; /* Ignore PK78 if it appears to be present */
  1415. }
  1416. zip->entry->crc32 = archive_le32dec(p); /* Parse CRC32 */
  1417. p += 4;
  1418. /* Consider both 32- and 64-bit interpretations */
  1419. compressed32 = archive_le32dec(p);
  1420. uncompressed32 = archive_le32dec(p + 4);
  1421. compressed64 = archive_le64dec(p);
  1422. uncompressed64 = archive_le64dec(p + 8);
  1423. /* The earlier patterns may have failed because of CRC32
  1424. * mismatch, so it's still possible that both sizes match.
  1425. * Try to match as many as we can...
  1426. */
  1427. if (compressed32 == compressed_actual
  1428. && uncompressed32 == uncompressed_actual) {
  1429. /* Both 32-bit fields match */
  1430. zip->entry->compressed_size = compressed32;
  1431. zip->entry->uncompressed_size = uncompressed32;
  1432. } else if (compressed64 == compressed_actual
  1433. || uncompressed64 == uncompressed_actual) {
  1434. /* One or both 64-bit fields match */
  1435. zip->entry->compressed_size = compressed64;
  1436. zip->entry->uncompressed_size = uncompressed64;
  1437. } else {
  1438. /* Zero or one 32-bit fields match */
  1439. zip->entry->compressed_size = compressed32;
  1440. zip->entry->uncompressed_size = uncompressed32;
  1441. }
  1442. }
  1443. /*
  1444. * Read "uncompressed" data.
  1445. *
  1446. * This is straightforward if we know the size of the data. This is
  1447. * always true for the seeking reader (we've examined the Central
  1448. * Directory already), and will often be true for the streaming reader
  1449. * (the writer was writing uncompressed so probably knows the size).
  1450. *
  1451. * If we don't know the size, then life is more interesting. Note
  1452. * that a careful reading of the Zip specification says that a writer
  1453. * must use ZIP_LENGTH_AT_END if it cannot write the CRC into the
  1454. * local header. And if it uses ZIP_LENGTH_AT_END, then it is
  1455. * prohibited from storing the sizes in the local header. This
  1456. * prevents fully-compliant streaming writers from providing any size
  1457. * clues to a streaming reader. In this case, we have to scan the
  1458. * data as we read to try to locate the end-of-file marker.
  1459. *
  1460. * We assume here that the end-of-file marker always has the
  1461. * PK\007\010 signature. Although it's technically optional, newer
  1462. * writers seem to provide it pretty consistently, and it's not clear
  1463. * how to efficiently recognize an end-of-file marker that lacks it.
  1464. *
  1465. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  1466. * zip->end_of_entry if it consumes all of the data.
  1467. */
  1468. static int
  1469. zip_read_data_none(struct archive_read *a, const void **_buff,
  1470. size_t *size, int64_t *offset)
  1471. {
  1472. struct zip *zip;
  1473. const char *buff;
  1474. ssize_t bytes_avail;
  1475. ssize_t trailing_extra;
  1476. int r;
  1477. (void)offset; /* UNUSED */
  1478. zip = (struct zip *)(a->format->data);
  1479. trailing_extra = zip->hctx_valid ? AUTH_CODE_SIZE : 0;
  1480. if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) {
  1481. const char *p;
  1482. ssize_t grabbing_bytes = 24 + trailing_extra;
  1483. /* Grab at least 24 bytes. */
  1484. buff = __archive_read_ahead(a, grabbing_bytes, &bytes_avail);
  1485. if (bytes_avail < grabbing_bytes) {
  1486. /* Zip archives have end-of-archive markers
  1487. that are longer than this, so a failure to get at
  1488. least 24 bytes really does indicate a truncated
  1489. file. */
  1490. archive_set_error(&a->archive,
  1491. ARCHIVE_ERRNO_FILE_FORMAT,
  1492. "Truncated ZIP file data");
  1493. return (ARCHIVE_FATAL);
  1494. }
  1495. /* Check for a complete PK\007\010 signature, followed
  1496. * by the correct 4-byte CRC. */
  1497. p = buff + trailing_extra;
  1498. if (p[0] == 'P' && p[1] == 'K'
  1499. && p[2] == '\007' && p[3] == '\010'
  1500. && (archive_le32dec(p + 4) == zip->computed_crc32
  1501. || zip->ignore_crc32
  1502. || (zip->hctx_valid
  1503. && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
  1504. zip->end_of_entry = 1;
  1505. if (zip->hctx_valid) {
  1506. r = check_authentication_code(a, buff);
  1507. if (r != ARCHIVE_OK)
  1508. return (r);
  1509. }
  1510. return (ARCHIVE_OK);
  1511. }
  1512. /* If not at EOF, ensure we consume at least one byte. */
  1513. ++p;
  1514. /* Scan forward until we see where a PK\007\010 signature
  1515. * might be. */
  1516. /* Return bytes up until that point. On the next call,
  1517. * the code above will verify the data descriptor. */
  1518. while (p < buff + bytes_avail - 4) {
  1519. if (p[3] == 'P') { p += 3; }
  1520. else if (p[3] == 'K') { p += 2; }
  1521. else if (p[3] == '\007') { p += 1; }
  1522. else if (p[3] == '\010' && p[2] == '\007'
  1523. && p[1] == 'K' && p[0] == 'P') {
  1524. break;
  1525. } else { p += 4; }
  1526. }
  1527. p -= trailing_extra;
  1528. bytes_avail = p - buff;
  1529. } else {
  1530. if (zip->entry_bytes_remaining == 0) {
  1531. zip->end_of_entry = 1;
  1532. if (zip->hctx_valid) {
  1533. r = check_authentication_code(a, NULL);
  1534. if (r != ARCHIVE_OK)
  1535. return (r);
  1536. }
  1537. return (ARCHIVE_OK);
  1538. }
  1539. /* Grab a bunch of bytes. */
  1540. buff = __archive_read_ahead(a, 1, &bytes_avail);
  1541. if (bytes_avail <= 0) {
  1542. archive_set_error(&a->archive,
  1543. ARCHIVE_ERRNO_FILE_FORMAT,
  1544. "Truncated ZIP file data");
  1545. return (ARCHIVE_FATAL);
  1546. }
  1547. if (bytes_avail > zip->entry_bytes_remaining)
  1548. bytes_avail = (ssize_t)zip->entry_bytes_remaining;
  1549. }
  1550. if (zip->tctx_valid || zip->cctx_valid) {
  1551. size_t dec_size = bytes_avail;
  1552. if (dec_size > zip->decrypted_buffer_size)
  1553. dec_size = zip->decrypted_buffer_size;
  1554. if (zip->tctx_valid) {
  1555. trad_enc_decrypt_update(&zip->tctx,
  1556. (const uint8_t *)buff, dec_size,
  1557. zip->decrypted_buffer, dec_size);
  1558. } else {
  1559. size_t dsize = dec_size;
  1560. archive_hmac_sha1_update(&zip->hctx,
  1561. (const uint8_t *)buff, dec_size);
  1562. archive_decrypto_aes_ctr_update(&zip->cctx,
  1563. (const uint8_t *)buff, dec_size,
  1564. zip->decrypted_buffer, &dsize);
  1565. }
  1566. bytes_avail = dec_size;
  1567. buff = (const char *)zip->decrypted_buffer;
  1568. }
  1569. zip->entry_bytes_remaining -= bytes_avail;
  1570. zip->entry_uncompressed_bytes_read += bytes_avail;
  1571. zip->entry_compressed_bytes_read += bytes_avail;
  1572. zip->unconsumed += bytes_avail;
  1573. *size = bytes_avail;
  1574. *_buff = buff;
  1575. return (ARCHIVE_OK);
  1576. }
  1577. #if HAVE_LZMA_H && HAVE_LIBLZMA
  1578. static int
  1579. zipx_xz_init(struct archive_read *a, struct zip *zip)
  1580. {
  1581. lzma_ret r;
  1582. if(zip->zipx_lzma_valid) {
  1583. lzma_end(&zip->zipx_lzma_stream);
  1584. zip->zipx_lzma_valid = 0;
  1585. }
  1586. memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
  1587. r = lzma_stream_decoder(&zip->zipx_lzma_stream, UINT64_MAX, 0);
  1588. if (r != LZMA_OK) {
  1589. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  1590. "xz initialization failed(%d)",
  1591. r);
  1592. return (ARCHIVE_FAILED);
  1593. }
  1594. zip->zipx_lzma_valid = 1;
  1595. free(zip->uncompressed_buffer);
  1596. zip->uncompressed_buffer_size = 256 * 1024;
  1597. zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
  1598. if (zip->uncompressed_buffer == NULL) {
  1599. archive_set_error(&a->archive, ENOMEM,
  1600. "No memory for xz decompression");
  1601. return (ARCHIVE_FATAL);
  1602. }
  1603. zip->decompress_init = 1;
  1604. return (ARCHIVE_OK);
  1605. }
  1606. static int
  1607. zipx_lzma_alone_init(struct archive_read *a, struct zip *zip)
  1608. {
  1609. lzma_ret r;
  1610. const uint8_t* p;
  1611. #pragma pack(push)
  1612. #pragma pack(1)
  1613. struct _alone_header {
  1614. uint8_t bytes[5];
  1615. uint64_t uncompressed_size;
  1616. } alone_header;
  1617. #pragma pack(pop)
  1618. if(zip->zipx_lzma_valid) {
  1619. lzma_end(&zip->zipx_lzma_stream);
  1620. zip->zipx_lzma_valid = 0;
  1621. }
  1622. /* To unpack ZIPX's "LZMA" (id 14) stream we can use standard liblzma
  1623. * that is a part of XZ Utils. The stream format stored inside ZIPX
  1624. * file is a modified "lzma alone" file format, that was used by the
  1625. * `lzma` utility which was later deprecated in favour of `xz` utility.
  1626. * Since those formats are nearly the same, we can use a standard
  1627. * "lzma alone" decoder from XZ Utils. */
  1628. memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
  1629. r = lzma_alone_decoder(&zip->zipx_lzma_stream, UINT64_MAX);
  1630. if (r != LZMA_OK) {
  1631. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  1632. "lzma initialization failed(%d)", r);
  1633. return (ARCHIVE_FAILED);
  1634. }
  1635. /* Flag the cleanup function that we want our lzma-related structures
  1636. * to be freed later. */
  1637. zip->zipx_lzma_valid = 1;
  1638. /* The "lzma alone" file format and the stream format inside ZIPx are
  1639. * almost the same. Here's an example of a structure of "lzma alone"
  1640. * format:
  1641. *
  1642. * $ cat /bin/ls | lzma | xxd | head -n 1
  1643. * 00000000: 5d00 0080 00ff ffff ffff ffff ff00 2814
  1644. *
  1645. * 5 bytes 8 bytes n bytes
  1646. * <lzma_params><uncompressed_size><data...>
  1647. *
  1648. * lzma_params is a 5-byte blob that has to be decoded to extract
  1649. * parameters of this LZMA stream. The uncompressed_size field is an
  1650. * uint64_t value that contains information about the size of the
  1651. * uncompressed file, or UINT64_MAX if this value is unknown.
  1652. * The <data...> part is the actual lzma-compressed data stream.
  1653. *
  1654. * Now here's the structure of the stream inside the ZIPX file:
  1655. *
  1656. * $ cat stream_inside_zipx | xxd | head -n 1
  1657. * 00000000: 0914 0500 5d00 8000 0000 2814 .... ....
  1658. *
  1659. * 2byte 2byte 5 bytes n bytes
  1660. * <magic1><magic2><lzma_params><data...>
  1661. *
  1662. * This means that the ZIPX file contains an additional magic1 and
  1663. * magic2 headers, the lzma_params field contains the same parameter
  1664. * set as in the "lzma alone" format, and the <data...> field is the
  1665. * same as in the "lzma alone" format as well. Note that also the zipx
  1666. * format is missing the uncompressed_size field.
  1667. *
  1668. * So, in order to use the "lzma alone" decoder for the zipx lzma
  1669. * stream, we simply need to shuffle around some fields, prepare a new
  1670. * lzma alone header, feed it into lzma alone decoder so it will
  1671. * initialize itself properly, and then we can start feeding normal
  1672. * zipx lzma stream into the decoder.
  1673. */
  1674. /* Read magic1,magic2,lzma_params from the ZIPX stream. */
  1675. if(zip->entry_bytes_remaining < 9 || (p = __archive_read_ahead(a, 9, NULL)) == NULL) {
  1676. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1677. "Truncated lzma data");
  1678. return (ARCHIVE_FATAL);
  1679. }
  1680. if(p[2] != 0x05 || p[3] != 0x00) {
  1681. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1682. "Invalid lzma data");
  1683. return (ARCHIVE_FATAL);
  1684. }
  1685. /* Prepare an lzma alone header: copy the lzma_params blob into
  1686. * a proper place into the lzma alone header. */
  1687. memcpy(&alone_header.bytes[0], p + 4, 5);
  1688. /* Initialize the 'uncompressed size' field to unknown; we'll manually
  1689. * monitor how many bytes there are still to be uncompressed. */
  1690. alone_header.uncompressed_size = UINT64_MAX;
  1691. if(!zip->uncompressed_buffer) {
  1692. zip->uncompressed_buffer_size = 256 * 1024;
  1693. zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
  1694. if (zip->uncompressed_buffer == NULL) {
  1695. archive_set_error(&a->archive, ENOMEM,
  1696. "No memory for lzma decompression");
  1697. return (ARCHIVE_FATAL);
  1698. }
  1699. }
  1700. zip->zipx_lzma_stream.next_in = (void*) &alone_header;
  1701. zip->zipx_lzma_stream.avail_in = sizeof(alone_header);
  1702. zip->zipx_lzma_stream.total_in = 0;
  1703. zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
  1704. zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
  1705. zip->zipx_lzma_stream.total_out = 0;
  1706. /* Feed only the header into the lzma alone decoder. This will
  1707. * effectively initialize the decoder, and will not produce any
  1708. * output bytes yet. */
  1709. r = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
  1710. if (r != LZMA_OK) {
  1711. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1712. "lzma stream initialization error");
  1713. return ARCHIVE_FATAL;
  1714. }
  1715. /* We've already consumed some bytes, so take this into account. */
  1716. __archive_read_consume(a, 9);
  1717. zip->entry_bytes_remaining -= 9;
  1718. zip->entry_compressed_bytes_read += 9;
  1719. zip->decompress_init = 1;
  1720. return (ARCHIVE_OK);
  1721. }
  1722. static int
  1723. zip_read_data_zipx_xz(struct archive_read *a, const void **buff,
  1724. size_t *size, int64_t *offset)
  1725. {
  1726. struct zip* zip = (struct zip *)(a->format->data);
  1727. int ret;
  1728. lzma_ret lz_ret;
  1729. const void* compressed_buf;
  1730. ssize_t bytes_avail, in_bytes, to_consume = 0;
  1731. (void) offset; /* UNUSED */
  1732. /* Initialize decompressor if not yet initialized. */
  1733. if (!zip->decompress_init) {
  1734. ret = zipx_xz_init(a, zip);
  1735. if (ret != ARCHIVE_OK)
  1736. return (ret);
  1737. }
  1738. compressed_buf = __archive_read_ahead(a, 1, &bytes_avail);
  1739. if (bytes_avail < 0) {
  1740. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1741. "Truncated xz file body");
  1742. return (ARCHIVE_FATAL);
  1743. }
  1744. in_bytes = (ssize_t)zipmin(zip->entry_bytes_remaining, bytes_avail);
  1745. zip->zipx_lzma_stream.next_in = compressed_buf;
  1746. zip->zipx_lzma_stream.avail_in = in_bytes;
  1747. zip->zipx_lzma_stream.total_in = 0;
  1748. zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
  1749. zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
  1750. zip->zipx_lzma_stream.total_out = 0;
  1751. /* Perform the decompression. */
  1752. lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
  1753. switch(lz_ret) {
  1754. case LZMA_DATA_ERROR:
  1755. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1756. "xz data error (error %d)", (int) lz_ret);
  1757. return (ARCHIVE_FATAL);
  1758. case LZMA_NO_CHECK:
  1759. case LZMA_OK:
  1760. break;
  1761. default:
  1762. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1763. "xz unknown error %d", (int) lz_ret);
  1764. return (ARCHIVE_FATAL);
  1765. case LZMA_STREAM_END:
  1766. lzma_end(&zip->zipx_lzma_stream);
  1767. zip->zipx_lzma_valid = 0;
  1768. if((int64_t) zip->zipx_lzma_stream.total_in !=
  1769. zip->entry_bytes_remaining)
  1770. {
  1771. archive_set_error(&a->archive,
  1772. ARCHIVE_ERRNO_MISC,
  1773. "xz premature end of stream");
  1774. return (ARCHIVE_FATAL);
  1775. }
  1776. zip->end_of_entry = 1;
  1777. break;
  1778. }
  1779. to_consume = (ssize_t)zip->zipx_lzma_stream.total_in;
  1780. __archive_read_consume(a, to_consume);
  1781. zip->entry_bytes_remaining -= to_consume;
  1782. zip->entry_compressed_bytes_read += to_consume;
  1783. zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out;
  1784. *size = (size_t)zip->zipx_lzma_stream.total_out;
  1785. *buff = zip->uncompressed_buffer;
  1786. return (ARCHIVE_OK);
  1787. }
  1788. static int
  1789. zip_read_data_zipx_lzma_alone(struct archive_read *a, const void **buff,
  1790. size_t *size, int64_t *offset)
  1791. {
  1792. struct zip* zip = (struct zip *)(a->format->data);
  1793. int ret;
  1794. lzma_ret lz_ret;
  1795. const void* compressed_buf;
  1796. ssize_t bytes_avail, in_bytes, to_consume;
  1797. (void) offset; /* UNUSED */
  1798. /* Initialize decompressor if not yet initialized. */
  1799. if (!zip->decompress_init) {
  1800. ret = zipx_lzma_alone_init(a, zip);
  1801. if (ret != ARCHIVE_OK)
  1802. return (ret);
  1803. }
  1804. /* Fetch more compressed data. The same note as in deflate handler
  1805. * applies here as well:
  1806. *
  1807. * Note: '1' here is a performance optimization. Recall that the
  1808. * decompression layer returns a count of available bytes; asking for
  1809. * more than that forces the decompressor to combine reads by copying
  1810. * data.
  1811. */
  1812. compressed_buf = __archive_read_ahead(a, 1, &bytes_avail);
  1813. if (bytes_avail < 0) {
  1814. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1815. "Truncated lzma file body");
  1816. return (ARCHIVE_FATAL);
  1817. }
  1818. /* Set decompressor parameters. */
  1819. in_bytes = (ssize_t)zipmin(zip->entry_bytes_remaining, bytes_avail);
  1820. zip->zipx_lzma_stream.next_in = compressed_buf;
  1821. zip->zipx_lzma_stream.avail_in = in_bytes;
  1822. zip->zipx_lzma_stream.total_in = 0;
  1823. zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
  1824. zip->zipx_lzma_stream.avail_out =
  1825. /* These lzma_alone streams lack end of stream marker, so let's
  1826. * make sure the unpacker won't try to unpack more than it's
  1827. * supposed to. */
  1828. (size_t)zipmin((int64_t) zip->uncompressed_buffer_size,
  1829. zip->entry->uncompressed_size -
  1830. zip->entry_uncompressed_bytes_read);
  1831. zip->zipx_lzma_stream.total_out = 0;
  1832. /* Perform the decompression. */
  1833. lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
  1834. switch(lz_ret) {
  1835. case LZMA_DATA_ERROR:
  1836. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1837. "lzma data error (error %d)", (int) lz_ret);
  1838. return (ARCHIVE_FATAL);
  1839. /* This case is optional in lzma alone format. It can happen,
  1840. * but most of the files don't have it. (GitHub #1257) */
  1841. case LZMA_STREAM_END:
  1842. if((int64_t) zip->zipx_lzma_stream.total_in !=
  1843. zip->entry_bytes_remaining)
  1844. {
  1845. archive_set_error(&a->archive,
  1846. ARCHIVE_ERRNO_MISC,
  1847. "lzma alone premature end of stream");
  1848. return (ARCHIVE_FATAL);
  1849. }
  1850. zip->end_of_entry = 1;
  1851. break;
  1852. case LZMA_OK:
  1853. break;
  1854. default:
  1855. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1856. "lzma unknown error %d", (int) lz_ret);
  1857. return (ARCHIVE_FATAL);
  1858. }
  1859. to_consume = (ssize_t)zip->zipx_lzma_stream.total_in;
  1860. /* Update pointers. */
  1861. __archive_read_consume(a, to_consume);
  1862. zip->entry_bytes_remaining -= to_consume;
  1863. zip->entry_compressed_bytes_read += to_consume;
  1864. zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out;
  1865. if(zip->entry_bytes_remaining == 0) {
  1866. zip->end_of_entry = 1;
  1867. }
  1868. /* Free lzma decoder handle because we'll no longer need it. */
  1869. /* This cannot be folded into LZMA_STREAM_END handling above
  1870. * because the stream end marker is not required in this format. */
  1871. if(zip->end_of_entry) {
  1872. lzma_end(&zip->zipx_lzma_stream);
  1873. zip->zipx_lzma_valid = 0;
  1874. }
  1875. /* Return values. */
  1876. *size = (size_t)zip->zipx_lzma_stream.total_out;
  1877. *buff = zip->uncompressed_buffer;
  1878. /* If we're here, then we're good! */
  1879. return (ARCHIVE_OK);
  1880. }
  1881. #endif /* HAVE_LZMA_H && HAVE_LIBLZMA */
  1882. static int
  1883. zipx_ppmd8_init(struct archive_read *a, struct zip *zip)
  1884. {
  1885. const void* p;
  1886. uint32_t val;
  1887. uint32_t order;
  1888. uint32_t mem;
  1889. uint32_t restore_method;
  1890. /* Remove previous decompression context if it exists. */
  1891. if(zip->ppmd8_valid) {
  1892. __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
  1893. zip->ppmd8_valid = 0;
  1894. }
  1895. /* Create a new decompression context. */
  1896. __archive_ppmd8_functions.Ppmd8_Construct(&zip->ppmd8);
  1897. zip->ppmd8_stream_failed = 0;
  1898. /* Setup function pointers required by Ppmd8 decompressor. The
  1899. * 'ppmd_read' function will feed new bytes to the decompressor,
  1900. * and will increment the 'zip->zipx_ppmd_read_compressed' counter. */
  1901. zip->ppmd8.Stream.In = &zip->zipx_ppmd_stream;
  1902. zip->zipx_ppmd_stream.a = a;
  1903. zip->zipx_ppmd_stream.Read = &ppmd_read;
  1904. /* Reset number of read bytes to 0. */
  1905. zip->zipx_ppmd_read_compressed = 0;
  1906. /* Read Ppmd8 header (2 bytes). */
  1907. p = __archive_read_ahead(a, 2, NULL);
  1908. if(!p) {
  1909. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1910. "Truncated file data in PPMd8 stream");
  1911. return (ARCHIVE_FATAL);
  1912. }
  1913. __archive_read_consume(a, 2);
  1914. /* Decode the stream's compression parameters. */
  1915. val = archive_le16dec(p);
  1916. order = (val & 15) + 1;
  1917. mem = ((val >> 4) & 0xff) + 1;
  1918. restore_method = (val >> 12);
  1919. if(order < 2 || restore_method > 2) {
  1920. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1921. "Invalid parameter set in PPMd8 stream (order=%" PRIu32 ", "
  1922. "restore=%" PRIu32 ")", order, restore_method);
  1923. return (ARCHIVE_FAILED);
  1924. }
  1925. /* Allocate the memory needed to properly decompress the file. */
  1926. if(!__archive_ppmd8_functions.Ppmd8_Alloc(&zip->ppmd8, mem << 20)) {
  1927. archive_set_error(&a->archive, ENOMEM,
  1928. "Unable to allocate memory for PPMd8 stream: %" PRIu32 " bytes",
  1929. mem << 20);
  1930. return (ARCHIVE_FATAL);
  1931. }
  1932. /* Signal the cleanup function to release Ppmd8 context in the
  1933. * cleanup phase. */
  1934. zip->ppmd8_valid = 1;
  1935. /* Perform further Ppmd8 initialization. */
  1936. if(!__archive_ppmd8_functions.Ppmd8_RangeDec_Init(&zip->ppmd8)) {
  1937. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1938. "PPMd8 stream range decoder initialization error");
  1939. return (ARCHIVE_FATAL);
  1940. }
  1941. __archive_ppmd8_functions.Ppmd8_Init(&zip->ppmd8, order,
  1942. restore_method);
  1943. /* Allocate the buffer that will hold uncompressed data. */
  1944. free(zip->uncompressed_buffer);
  1945. zip->uncompressed_buffer_size = 256 * 1024;
  1946. zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
  1947. if(zip->uncompressed_buffer == NULL) {
  1948. archive_set_error(&a->archive, ENOMEM,
  1949. "No memory for PPMd8 decompression");
  1950. return ARCHIVE_FATAL;
  1951. }
  1952. /* Ppmd8 initialization is done. */
  1953. zip->decompress_init = 1;
  1954. /* We've already read 2 bytes in the output stream. Additionally,
  1955. * Ppmd8 initialization code could read some data as well. So we
  1956. * are advancing the stream by 2 bytes plus whatever number of
  1957. * bytes Ppmd8 init function used. */
  1958. zip->entry_compressed_bytes_read += 2 + zip->zipx_ppmd_read_compressed;
  1959. return ARCHIVE_OK;
  1960. }
  1961. static int
  1962. zip_read_data_zipx_ppmd(struct archive_read *a, const void **buff,
  1963. size_t *size, int64_t *offset)
  1964. {
  1965. struct zip* zip = (struct zip *)(a->format->data);
  1966. int ret;
  1967. size_t consumed_bytes = 0;
  1968. ssize_t bytes_avail = 0;
  1969. (void) offset; /* UNUSED */
  1970. /* If we're here for the first time, initialize Ppmd8 decompression
  1971. * context first. */
  1972. if(!zip->decompress_init) {
  1973. ret = zipx_ppmd8_init(a, zip);
  1974. if(ret != ARCHIVE_OK)
  1975. return ret;
  1976. }
  1977. /* Fetch for more data. We're reading 1 byte here, but libarchive
  1978. * should prefetch more bytes. */
  1979. (void) __archive_read_ahead(a, 1, &bytes_avail);
  1980. if(bytes_avail < 0) {
  1981. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1982. "Truncated PPMd8 file body");
  1983. return (ARCHIVE_FATAL);
  1984. }
  1985. /* This counter will be updated inside ppmd_read(), which at one
  1986. * point will be called by Ppmd8_DecodeSymbol. */
  1987. zip->zipx_ppmd_read_compressed = 0;
  1988. /* Decompression loop. */
  1989. do {
  1990. int sym = __archive_ppmd8_functions.Ppmd8_DecodeSymbol(
  1991. &zip->ppmd8);
  1992. if(sym < 0) {
  1993. zip->end_of_entry = 1;
  1994. break;
  1995. }
  1996. /* This field is set by ppmd_read() when there was no more data
  1997. * to be read. */
  1998. if(zip->ppmd8_stream_failed) {
  1999. archive_set_error(&a->archive,
  2000. ARCHIVE_ERRNO_FILE_FORMAT,
  2001. "Truncated PPMd8 file body");
  2002. return (ARCHIVE_FATAL);
  2003. }
  2004. zip->uncompressed_buffer[consumed_bytes] = (uint8_t) sym;
  2005. ++consumed_bytes;
  2006. } while(consumed_bytes < zip->uncompressed_buffer_size);
  2007. /* Update pointers so we can continue decompression in another call. */
  2008. zip->entry_bytes_remaining -= zip->zipx_ppmd_read_compressed;
  2009. zip->entry_compressed_bytes_read += zip->zipx_ppmd_read_compressed;
  2010. zip->entry_uncompressed_bytes_read += consumed_bytes;
  2011. /* If we're at the end of stream, deinitialize Ppmd8 context. */
  2012. if(zip->end_of_entry) {
  2013. __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
  2014. zip->ppmd8_valid = 0;
  2015. }
  2016. /* Update pointers for libarchive. */
  2017. *buff = zip->uncompressed_buffer;
  2018. *size = consumed_bytes;
  2019. return ARCHIVE_OK;
  2020. }
  2021. #ifdef HAVE_BZLIB_H
  2022. static int
  2023. zipx_bzip2_init(struct archive_read *a, struct zip *zip)
  2024. {
  2025. int r;
  2026. /* Deallocate already existing BZ2 decompression context if it
  2027. * exists. */
  2028. if(zip->bzstream_valid) {
  2029. BZ2_bzDecompressEnd(&zip->bzstream);
  2030. zip->bzstream_valid = 0;
  2031. }
  2032. /* Allocate a new BZ2 decompression context. */
  2033. memset(&zip->bzstream, 0, sizeof(bz_stream));
  2034. r = BZ2_bzDecompressInit(&zip->bzstream, 0, 1);
  2035. if(r != BZ_OK) {
  2036. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  2037. "bzip2 initialization failed(%d)",
  2038. r);
  2039. return ARCHIVE_FAILED;
  2040. }
  2041. /* Mark the bzstream field to be released in cleanup phase. */
  2042. zip->bzstream_valid = 1;
  2043. /* (Re)allocate the buffer that will contain decompressed bytes. */
  2044. free(zip->uncompressed_buffer);
  2045. zip->uncompressed_buffer_size = 256 * 1024;
  2046. zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
  2047. if (zip->uncompressed_buffer == NULL) {
  2048. archive_set_error(&a->archive, ENOMEM,
  2049. "No memory for bzip2 decompression");
  2050. return ARCHIVE_FATAL;
  2051. }
  2052. /* Initialization done. */
  2053. zip->decompress_init = 1;
  2054. return ARCHIVE_OK;
  2055. }
  2056. static int
  2057. zip_read_data_zipx_bzip2(struct archive_read *a, const void **buff,
  2058. size_t *size, int64_t *offset)
  2059. {
  2060. struct zip *zip = (struct zip *)(a->format->data);
  2061. ssize_t bytes_avail = 0, in_bytes, to_consume;
  2062. const void *compressed_buff;
  2063. int r;
  2064. uint64_t total_out;
  2065. (void) offset; /* UNUSED */
  2066. /* Initialize decompression context if we're here for the first time. */
  2067. if(!zip->decompress_init) {
  2068. r = zipx_bzip2_init(a, zip);
  2069. if(r != ARCHIVE_OK)
  2070. return r;
  2071. }
  2072. /* Fetch more compressed bytes. */
  2073. compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
  2074. if(bytes_avail < 0) {
  2075. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2076. "Truncated bzip2 file body");
  2077. return (ARCHIVE_FATAL);
  2078. }
  2079. in_bytes = (ssize_t)zipmin(zip->entry_bytes_remaining, bytes_avail);
  2080. if(in_bytes < 1) {
  2081. /* libbz2 doesn't complain when caller feeds avail_in == 0.
  2082. * It will actually return success in this case, which is
  2083. * undesirable. This is why we need to make this check
  2084. * manually. */
  2085. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2086. "Truncated bzip2 file body");
  2087. return (ARCHIVE_FATAL);
  2088. }
  2089. /* Setup buffer boundaries. */
  2090. zip->bzstream.next_in = (char*)(uintptr_t) compressed_buff;
  2091. zip->bzstream.avail_in = (uint32_t)in_bytes;
  2092. zip->bzstream.total_in_hi32 = 0;
  2093. zip->bzstream.total_in_lo32 = 0;
  2094. zip->bzstream.next_out = (char*) zip->uncompressed_buffer;
  2095. zip->bzstream.avail_out = (uint32_t)zip->uncompressed_buffer_size;
  2096. zip->bzstream.total_out_hi32 = 0;
  2097. zip->bzstream.total_out_lo32 = 0;
  2098. /* Perform the decompression. */
  2099. r = BZ2_bzDecompress(&zip->bzstream);
  2100. switch(r) {
  2101. case BZ_STREAM_END:
  2102. /* If we're at the end of the stream, deinitialize the
  2103. * decompression context now. */
  2104. switch(BZ2_bzDecompressEnd(&zip->bzstream)) {
  2105. case BZ_OK:
  2106. break;
  2107. default:
  2108. archive_set_error(&a->archive,
  2109. ARCHIVE_ERRNO_MISC,
  2110. "Failed to clean up bzip2 "
  2111. "decompressor");
  2112. return ARCHIVE_FATAL;
  2113. }
  2114. zip->end_of_entry = 1;
  2115. break;
  2116. case BZ_OK:
  2117. /* The decompressor has successfully decoded this
  2118. * chunk of data, but more data is still in queue. */
  2119. break;
  2120. default:
  2121. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2122. "bzip2 decompression failed");
  2123. return ARCHIVE_FATAL;
  2124. }
  2125. /* Update the pointers so decompressor can continue decoding. */
  2126. to_consume = zip->bzstream.total_in_lo32;
  2127. __archive_read_consume(a, to_consume);
  2128. total_out = ((uint64_t) zip->bzstream.total_out_hi32 << 32) |
  2129. zip->bzstream.total_out_lo32;
  2130. zip->entry_bytes_remaining -= to_consume;
  2131. zip->entry_compressed_bytes_read += to_consume;
  2132. zip->entry_uncompressed_bytes_read += total_out;
  2133. /* Give libarchive its due. */
  2134. *size = (size_t)total_out;
  2135. *buff = zip->uncompressed_buffer;
  2136. return ARCHIVE_OK;
  2137. }
  2138. #endif
  2139. #if HAVE_ZSTD_H && HAVE_LIBZSTD
  2140. static int
  2141. zipx_zstd_init(struct archive_read *a, struct zip *zip)
  2142. {
  2143. size_t r;
  2144. /* Deallocate already existing Zstd decompression context if it
  2145. * exists. */
  2146. if(zip->zstdstream_valid) {
  2147. ZSTD_freeDStream(zip->zstdstream);
  2148. zip->zstdstream_valid = 0;
  2149. }
  2150. /* Allocate a new Zstd decompression context. */
  2151. zip->zstdstream = ZSTD_createDStream();
  2152. r = ZSTD_initDStream(zip->zstdstream);
  2153. if (ZSTD_isError(r)) {
  2154. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2155. "Error initializing zstd decompressor: %s",
  2156. ZSTD_getErrorName(r));
  2157. return ARCHIVE_FAILED;
  2158. }
  2159. /* Mark the zstdstream field to be released in cleanup phase. */
  2160. zip->zstdstream_valid = 1;
  2161. /* (Re)allocate the buffer that will contain decompressed bytes. */
  2162. free(zip->uncompressed_buffer);
  2163. zip->uncompressed_buffer_size = ZSTD_DStreamOutSize();
  2164. zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
  2165. if (zip->uncompressed_buffer == NULL) {
  2166. archive_set_error(&a->archive, ENOMEM,
  2167. "No memory for Zstd decompression");
  2168. return ARCHIVE_FATAL;
  2169. }
  2170. /* Initialization done. */
  2171. zip->decompress_init = 1;
  2172. return ARCHIVE_OK;
  2173. }
  2174. static int
  2175. zip_read_data_zipx_zstd(struct archive_read *a, const void **buff,
  2176. size_t *size, int64_t *offset)
  2177. {
  2178. struct zip *zip = (struct zip *)(a->format->data);
  2179. ssize_t bytes_avail = 0, in_bytes, to_consume;
  2180. const void *compressed_buff;
  2181. int r;
  2182. size_t ret;
  2183. uint64_t total_out;
  2184. ZSTD_outBuffer out;
  2185. ZSTD_inBuffer in;
  2186. (void) offset; /* UNUSED */
  2187. /* Initialize decompression context if we're here for the first time. */
  2188. if(!zip->decompress_init) {
  2189. r = zipx_zstd_init(a, zip);
  2190. if(r != ARCHIVE_OK)
  2191. return r;
  2192. }
  2193. /* Fetch more compressed bytes */
  2194. compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
  2195. if(bytes_avail < 0) {
  2196. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2197. "Truncated zstd file body");
  2198. return (ARCHIVE_FATAL);
  2199. }
  2200. in_bytes = (ssize_t)zipmin(zip->entry_bytes_remaining, bytes_avail);
  2201. if(in_bytes < 1) {
  2202. /* zstd doesn't complain when caller feeds avail_in == 0.
  2203. * It will actually return success in this case, which is
  2204. * undesirable. This is why we need to make this check
  2205. * manually. */
  2206. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2207. "Truncated zstd file body");
  2208. return (ARCHIVE_FATAL);
  2209. }
  2210. /* Setup buffer boundaries */
  2211. in.src = compressed_buff;
  2212. in.size = in_bytes;
  2213. in.pos = 0;
  2214. out = (ZSTD_outBuffer) { zip->uncompressed_buffer, zip->uncompressed_buffer_size, 0 };
  2215. /* Perform the decompression. */
  2216. ret = ZSTD_decompressStream(zip->zstdstream, &out, &in);
  2217. if (ZSTD_isError(ret)) {
  2218. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2219. "Error during zstd decompression: %s",
  2220. ZSTD_getErrorName(ret));
  2221. return (ARCHIVE_FATAL);
  2222. }
  2223. /* Check end of the stream. */
  2224. if (ret == 0) {
  2225. if ((in.pos == in.size) && (out.pos < out.size)) {
  2226. zip->end_of_entry = 1;
  2227. ZSTD_freeDStream(zip->zstdstream);
  2228. zip->zstdstream_valid = 0;
  2229. }
  2230. }
  2231. /* Update the pointers so decompressor can continue decoding. */
  2232. to_consume = in.pos;
  2233. __archive_read_consume(a, to_consume);
  2234. total_out = out.pos;
  2235. zip->entry_bytes_remaining -= to_consume;
  2236. zip->entry_compressed_bytes_read += to_consume;
  2237. zip->entry_uncompressed_bytes_read += total_out;
  2238. /* Give libarchive its due. */
  2239. *size = (size_t)total_out;
  2240. *buff = zip->uncompressed_buffer;
  2241. return ARCHIVE_OK;
  2242. }
  2243. #endif
  2244. #ifdef HAVE_ZLIB_H
  2245. static int
  2246. zip_deflate_init(struct archive_read *a, struct zip *zip)
  2247. {
  2248. int r;
  2249. /* If we haven't yet read any data, initialize the decompressor. */
  2250. if (!zip->decompress_init) {
  2251. if (zip->stream_valid)
  2252. r = inflateReset(&zip->stream);
  2253. else
  2254. r = inflateInit2(&zip->stream,
  2255. -15 /* Don't check for zlib header */);
  2256. if (r != Z_OK) {
  2257. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2258. "Can't initialize ZIP decompression.");
  2259. return (ARCHIVE_FATAL);
  2260. }
  2261. /* Stream structure has been set up. */
  2262. zip->stream_valid = 1;
  2263. /* We've initialized decompression for this stream. */
  2264. zip->decompress_init = 1;
  2265. }
  2266. return (ARCHIVE_OK);
  2267. }
  2268. static int
  2269. zip_read_data_deflate(struct archive_read *a, const void **buff,
  2270. size_t *size, int64_t *offset)
  2271. {
  2272. struct zip *zip;
  2273. ssize_t bytes_avail, to_consume = 0;
  2274. const void *compressed_buff, *sp;
  2275. int r;
  2276. (void)offset; /* UNUSED */
  2277. zip = (struct zip *)(a->format->data);
  2278. /* If the buffer hasn't been allocated, allocate it now. */
  2279. if (zip->uncompressed_buffer == NULL) {
  2280. zip->uncompressed_buffer_size = 256 * 1024;
  2281. zip->uncompressed_buffer
  2282. = malloc(zip->uncompressed_buffer_size);
  2283. if (zip->uncompressed_buffer == NULL) {
  2284. archive_set_error(&a->archive, ENOMEM,
  2285. "No memory for ZIP decompression");
  2286. return (ARCHIVE_FATAL);
  2287. }
  2288. }
  2289. r = zip_deflate_init(a, zip);
  2290. if (r != ARCHIVE_OK)
  2291. return (r);
  2292. /*
  2293. * Note: '1' here is a performance optimization.
  2294. * Recall that the decompression layer returns a count of
  2295. * available bytes; asking for more than that forces the
  2296. * decompressor to combine reads by copying data.
  2297. */
  2298. compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail);
  2299. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  2300. && bytes_avail > zip->entry_bytes_remaining) {
  2301. bytes_avail = (ssize_t)zip->entry_bytes_remaining;
  2302. }
  2303. if (bytes_avail < 0) {
  2304. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2305. "Truncated ZIP file body");
  2306. return (ARCHIVE_FATAL);
  2307. }
  2308. if (zip->tctx_valid || zip->cctx_valid) {
  2309. if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
  2310. size_t buff_remaining =
  2311. (zip->decrypted_buffer +
  2312. zip->decrypted_buffer_size)
  2313. - (zip->decrypted_ptr +
  2314. zip->decrypted_bytes_remaining);
  2315. if (buff_remaining > (size_t)bytes_avail)
  2316. buff_remaining = (size_t)bytes_avail;
  2317. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
  2318. zip->entry_bytes_remaining > 0) {
  2319. if ((int64_t)(zip->decrypted_bytes_remaining
  2320. + buff_remaining)
  2321. > zip->entry_bytes_remaining) {
  2322. if (zip->entry_bytes_remaining <
  2323. (int64_t)zip->decrypted_bytes_remaining)
  2324. buff_remaining = 0;
  2325. else
  2326. buff_remaining =
  2327. (size_t)zip->entry_bytes_remaining
  2328. - zip->decrypted_bytes_remaining;
  2329. }
  2330. }
  2331. if (buff_remaining > 0) {
  2332. if (zip->tctx_valid) {
  2333. trad_enc_decrypt_update(&zip->tctx,
  2334. compressed_buff, buff_remaining,
  2335. zip->decrypted_ptr
  2336. + zip->decrypted_bytes_remaining,
  2337. buff_remaining);
  2338. } else {
  2339. size_t dsize = buff_remaining;
  2340. archive_decrypto_aes_ctr_update(
  2341. &zip->cctx,
  2342. compressed_buff, buff_remaining,
  2343. zip->decrypted_ptr
  2344. + zip->decrypted_bytes_remaining,
  2345. &dsize);
  2346. }
  2347. zip->decrypted_bytes_remaining +=
  2348. buff_remaining;
  2349. }
  2350. }
  2351. bytes_avail = zip->decrypted_bytes_remaining;
  2352. compressed_buff = (const char *)zip->decrypted_ptr;
  2353. }
  2354. /*
  2355. * A bug in zlib.h: stream.next_in should be marked 'const'
  2356. * but isn't (the library never alters data through the
  2357. * next_in pointer, only reads it). The result: this ugly
  2358. * cast to remove 'const'.
  2359. */
  2360. zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
  2361. zip->stream.avail_in = (uInt)bytes_avail;
  2362. zip->stream.total_in = 0;
  2363. zip->stream.next_out = zip->uncompressed_buffer;
  2364. zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
  2365. zip->stream.total_out = 0;
  2366. r = inflate(&zip->stream, 0);
  2367. switch (r) {
  2368. case Z_OK:
  2369. break;
  2370. case Z_STREAM_END:
  2371. zip->end_of_entry = 1;
  2372. break;
  2373. case Z_MEM_ERROR:
  2374. archive_set_error(&a->archive, ENOMEM,
  2375. "Out of memory for ZIP decompression");
  2376. return (ARCHIVE_FATAL);
  2377. default:
  2378. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2379. "ZIP decompression failed (%d)", r);
  2380. return (ARCHIVE_FATAL);
  2381. }
  2382. /* Consume as much as the compressor actually used. */
  2383. to_consume = zip->stream.total_in;
  2384. __archive_read_consume(a, to_consume);
  2385. zip->entry_bytes_remaining -= to_consume;
  2386. zip->entry_compressed_bytes_read += to_consume;
  2387. zip->entry_uncompressed_bytes_read += zip->stream.total_out;
  2388. if (zip->tctx_valid || zip->cctx_valid) {
  2389. zip->decrypted_bytes_remaining -= to_consume;
  2390. if (zip->decrypted_bytes_remaining == 0)
  2391. zip->decrypted_ptr = zip->decrypted_buffer;
  2392. else
  2393. zip->decrypted_ptr += to_consume;
  2394. }
  2395. if (zip->hctx_valid)
  2396. archive_hmac_sha1_update(&zip->hctx, sp, to_consume);
  2397. if (zip->end_of_entry) {
  2398. if (zip->hctx_valid) {
  2399. r = check_authentication_code(a, NULL);
  2400. if (r != ARCHIVE_OK) {
  2401. return (r);
  2402. }
  2403. }
  2404. }
  2405. *size = zip->stream.total_out;
  2406. *buff = zip->uncompressed_buffer;
  2407. return (ARCHIVE_OK);
  2408. }
  2409. #endif
  2410. static int
  2411. read_decryption_header(struct archive_read *a)
  2412. {
  2413. struct zip *zip = (struct zip *)(a->format->data);
  2414. const char *p;
  2415. unsigned int remaining_size;
  2416. unsigned int ts;
  2417. /*
  2418. * Read an initialization vector data field.
  2419. */
  2420. p = __archive_read_ahead(a, 2, NULL);
  2421. if (p == NULL)
  2422. goto truncated;
  2423. ts = zip->iv_size;
  2424. zip->iv_size = archive_le16dec(p);
  2425. __archive_read_consume(a, 2);
  2426. if (ts < zip->iv_size) {
  2427. free(zip->iv);
  2428. zip->iv = NULL;
  2429. }
  2430. p = __archive_read_ahead(a, zip->iv_size, NULL);
  2431. if (p == NULL)
  2432. goto truncated;
  2433. if (zip->iv == NULL) {
  2434. zip->iv = malloc(zip->iv_size);
  2435. if (zip->iv == NULL)
  2436. goto nomem;
  2437. }
  2438. memcpy(zip->iv, p, zip->iv_size);
  2439. __archive_read_consume(a, zip->iv_size);
  2440. /*
  2441. * Read a size of remaining decryption header field.
  2442. */
  2443. p = __archive_read_ahead(a, 14, NULL);
  2444. if (p == NULL)
  2445. goto truncated;
  2446. remaining_size = archive_le32dec(p);
  2447. if (remaining_size < 16 || remaining_size > (1 << 18))
  2448. goto corrupted;
  2449. /* Check if format version is supported. */
  2450. if (archive_le16dec(p+4) != 3) {
  2451. archive_set_error(&a->archive,
  2452. ARCHIVE_ERRNO_FILE_FORMAT,
  2453. "Unsupported encryption format version: %u",
  2454. archive_le16dec(p+4));
  2455. return (ARCHIVE_FAILED);
  2456. }
  2457. /*
  2458. * Read an encryption algorithm field.
  2459. */
  2460. zip->alg_id = archive_le16dec(p+6);
  2461. switch (zip->alg_id) {
  2462. case 0x6601:/* DES */
  2463. case 0x6602:/* RC2 */
  2464. case 0x6603:/* 3DES 168 */
  2465. case 0x6609:/* 3DES 112 */
  2466. case 0x660E:/* AES 128 */
  2467. case 0x660F:/* AES 192 */
  2468. case 0x6610:/* AES 256 */
  2469. case 0x6702:/* RC2 (version >= 5.2) */
  2470. case 0x6720:/* Blowfish */
  2471. case 0x6721:/* Twofish */
  2472. case 0x6801:/* RC4 */
  2473. /* Supported encryption algorithm. */
  2474. break;
  2475. default:
  2476. archive_set_error(&a->archive,
  2477. ARCHIVE_ERRNO_FILE_FORMAT,
  2478. "Unknown encryption algorithm: %u", zip->alg_id);
  2479. return (ARCHIVE_FAILED);
  2480. }
  2481. /*
  2482. * Read a bit length field.
  2483. */
  2484. zip->bit_len = archive_le16dec(p+8);
  2485. /*
  2486. * Read a flags field.
  2487. */
  2488. zip->flags = archive_le16dec(p+10);
  2489. switch (zip->flags & 0xf000) {
  2490. case 0x0001: /* Password is required to decrypt. */
  2491. case 0x0002: /* Certificates only. */
  2492. case 0x0003: /* Password or certificate required to decrypt. */
  2493. break;
  2494. default:
  2495. archive_set_error(&a->archive,
  2496. ARCHIVE_ERRNO_FILE_FORMAT,
  2497. "Unknown encryption flag: %u", zip->flags);
  2498. return (ARCHIVE_FAILED);
  2499. }
  2500. if ((zip->flags & 0xf000) == 0 ||
  2501. (zip->flags & 0xf000) == 0x4000) {
  2502. archive_set_error(&a->archive,
  2503. ARCHIVE_ERRNO_FILE_FORMAT,
  2504. "Unknown encryption flag: %u", zip->flags);
  2505. return (ARCHIVE_FAILED);
  2506. }
  2507. /*
  2508. * Read an encrypted random data field.
  2509. */
  2510. ts = zip->erd_size;
  2511. zip->erd_size = archive_le16dec(p+12);
  2512. __archive_read_consume(a, 14);
  2513. if ((zip->erd_size & 0xf) != 0 ||
  2514. (zip->erd_size + 16) > remaining_size ||
  2515. (zip->erd_size + 16) < zip->erd_size)
  2516. goto corrupted;
  2517. if (ts < zip->erd_size) {
  2518. free(zip->erd);
  2519. zip->erd = NULL;
  2520. }
  2521. p = __archive_read_ahead(a, zip->erd_size, NULL);
  2522. if (p == NULL)
  2523. goto truncated;
  2524. if (zip->erd == NULL) {
  2525. zip->erd = malloc(zip->erd_size);
  2526. if (zip->erd == NULL)
  2527. goto nomem;
  2528. }
  2529. memcpy(zip->erd, p, zip->erd_size);
  2530. __archive_read_consume(a, zip->erd_size);
  2531. /*
  2532. * Read a reserved data field.
  2533. */
  2534. p = __archive_read_ahead(a, 4, NULL);
  2535. if (p == NULL)
  2536. goto truncated;
  2537. /* Reserved data size should be zero. */
  2538. if (archive_le32dec(p) != 0)
  2539. goto corrupted;
  2540. __archive_read_consume(a, 4);
  2541. /*
  2542. * Read a password validation data field.
  2543. */
  2544. p = __archive_read_ahead(a, 2, NULL);
  2545. if (p == NULL)
  2546. goto truncated;
  2547. ts = zip->v_size;
  2548. zip->v_size = archive_le16dec(p);
  2549. __archive_read_consume(a, 2);
  2550. if ((zip->v_size & 0x0f) != 0 ||
  2551. (zip->erd_size + zip->v_size + 16) > remaining_size ||
  2552. (zip->erd_size + zip->v_size + 16) < (zip->erd_size + zip->v_size))
  2553. goto corrupted;
  2554. if (ts < zip->v_size) {
  2555. free(zip->v_data);
  2556. zip->v_data = NULL;
  2557. }
  2558. p = __archive_read_ahead(a, zip->v_size, NULL);
  2559. if (p == NULL)
  2560. goto truncated;
  2561. if (zip->v_data == NULL) {
  2562. zip->v_data = malloc(zip->v_size);
  2563. if (zip->v_data == NULL)
  2564. goto nomem;
  2565. }
  2566. memcpy(zip->v_data, p, zip->v_size);
  2567. __archive_read_consume(a, zip->v_size);
  2568. p = __archive_read_ahead(a, 4, NULL);
  2569. if (p == NULL)
  2570. goto truncated;
  2571. zip->v_crc32 = archive_le32dec(p);
  2572. __archive_read_consume(a, 4);
  2573. /*return (ARCHIVE_OK);
  2574. * This is not fully implemented yet.*/
  2575. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2576. "Encrypted file is unsupported");
  2577. return (ARCHIVE_FAILED);
  2578. truncated:
  2579. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2580. "Truncated ZIP file data");
  2581. return (ARCHIVE_FATAL);
  2582. corrupted:
  2583. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2584. "Corrupted ZIP file data");
  2585. return (ARCHIVE_FATAL);
  2586. nomem:
  2587. archive_set_error(&a->archive, ENOMEM,
  2588. "No memory for ZIP decryption");
  2589. return (ARCHIVE_FATAL);
  2590. }
  2591. static int
  2592. zip_alloc_decryption_buffer(struct archive_read *a)
  2593. {
  2594. struct zip *zip = (struct zip *)(a->format->data);
  2595. size_t bs = 256 * 1024;
  2596. if (zip->decrypted_buffer == NULL) {
  2597. zip->decrypted_buffer_size = bs;
  2598. zip->decrypted_buffer = malloc(bs);
  2599. if (zip->decrypted_buffer == NULL) {
  2600. archive_set_error(&a->archive, ENOMEM,
  2601. "No memory for ZIP decryption");
  2602. return (ARCHIVE_FATAL);
  2603. }
  2604. }
  2605. zip->decrypted_ptr = zip->decrypted_buffer;
  2606. return (ARCHIVE_OK);
  2607. }
  2608. static int
  2609. init_traditional_PKWARE_decryption(struct archive_read *a)
  2610. {
  2611. struct zip *zip = (struct zip *)(a->format->data);
  2612. const void *p;
  2613. int retry;
  2614. int r;
  2615. if (zip->tctx_valid)
  2616. return (ARCHIVE_OK);
  2617. /*
  2618. Read the 12 bytes encryption header stored at
  2619. the start of the data area.
  2620. */
  2621. #define ENC_HEADER_SIZE 12
  2622. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  2623. && zip->entry_bytes_remaining < ENC_HEADER_SIZE) {
  2624. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2625. "Truncated Zip encrypted body: only %jd bytes available",
  2626. (intmax_t)zip->entry_bytes_remaining);
  2627. return (ARCHIVE_FATAL);
  2628. }
  2629. p = __archive_read_ahead(a, ENC_HEADER_SIZE, NULL);
  2630. if (p == NULL) {
  2631. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2632. "Truncated ZIP file data");
  2633. return (ARCHIVE_FATAL);
  2634. }
  2635. for (retry = 0;; retry++) {
  2636. const char *passphrase;
  2637. uint8_t crcchk;
  2638. passphrase = __archive_read_next_passphrase(a);
  2639. if (passphrase == NULL) {
  2640. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2641. (retry > 0)?
  2642. "Incorrect passphrase":
  2643. "Passphrase required for this entry");
  2644. return (ARCHIVE_FAILED);
  2645. }
  2646. /*
  2647. * Initialize ctx for Traditional PKWARE Decryption.
  2648. */
  2649. r = trad_enc_init(&zip->tctx, passphrase, strlen(passphrase),
  2650. p, ENC_HEADER_SIZE, &crcchk);
  2651. if (r == 0 && crcchk == zip->entry->decdat)
  2652. break;/* The passphrase is OK. */
  2653. if (retry > 10000) {
  2654. /* Avoid infinity loop. */
  2655. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2656. "Too many incorrect passphrases");
  2657. return (ARCHIVE_FAILED);
  2658. }
  2659. }
  2660. __archive_read_consume(a, ENC_HEADER_SIZE);
  2661. zip->tctx_valid = 1;
  2662. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
  2663. zip->entry_bytes_remaining -= ENC_HEADER_SIZE;
  2664. }
  2665. /*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/
  2666. zip->entry_compressed_bytes_read += ENC_HEADER_SIZE;
  2667. zip->decrypted_bytes_remaining = 0;
  2668. return (zip_alloc_decryption_buffer(a));
  2669. #undef ENC_HEADER_SIZE
  2670. }
  2671. static int
  2672. init_WinZip_AES_decryption(struct archive_read *a)
  2673. {
  2674. struct zip *zip = (struct zip *)(a->format->data);
  2675. const void *p;
  2676. const uint8_t *pv;
  2677. size_t key_len, salt_len;
  2678. uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
  2679. int retry;
  2680. int r;
  2681. if (zip->cctx_valid || zip->hctx_valid)
  2682. return (ARCHIVE_OK);
  2683. switch (zip->entry->aes_extra.strength) {
  2684. case 1: salt_len = 8; key_len = 16; break;
  2685. case 2: salt_len = 12; key_len = 24; break;
  2686. case 3: salt_len = 16; key_len = 32; break;
  2687. default: goto corrupted;
  2688. }
  2689. p = __archive_read_ahead(a, salt_len + 2, NULL);
  2690. if (p == NULL)
  2691. goto truncated;
  2692. for (retry = 0;; retry++) {
  2693. const char *passphrase;
  2694. passphrase = __archive_read_next_passphrase(a);
  2695. if (passphrase == NULL) {
  2696. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2697. (retry > 0)?
  2698. "Incorrect passphrase":
  2699. "Passphrase required for this entry");
  2700. return (ARCHIVE_FAILED);
  2701. }
  2702. memset(derived_key, 0, sizeof(derived_key));
  2703. r = archive_pbkdf2_sha1(passphrase, strlen(passphrase),
  2704. p, salt_len, 1000, derived_key, key_len * 2 + 2);
  2705. if (r != 0) {
  2706. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2707. "Decryption is unsupported due to lack of "
  2708. "crypto library");
  2709. return (ARCHIVE_FAILED);
  2710. }
  2711. /* Check password verification value. */
  2712. pv = ((const uint8_t *)p) + salt_len;
  2713. if (derived_key[key_len * 2] == pv[0] &&
  2714. derived_key[key_len * 2 + 1] == pv[1])
  2715. break;/* The passphrase is OK. */
  2716. if (retry > 10000) {
  2717. /* Avoid infinity loop. */
  2718. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2719. "Too many incorrect passphrases");
  2720. return (ARCHIVE_FAILED);
  2721. }
  2722. }
  2723. r = archive_decrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
  2724. if (r != 0) {
  2725. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2726. "Decryption is unsupported due to lack of crypto library");
  2727. return (ARCHIVE_FAILED);
  2728. }
  2729. r = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len, key_len);
  2730. if (r != 0) {
  2731. archive_decrypto_aes_ctr_release(&zip->cctx);
  2732. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2733. "Failed to initialize HMAC-SHA1");
  2734. return (ARCHIVE_FAILED);
  2735. }
  2736. zip->cctx_valid = zip->hctx_valid = 1;
  2737. __archive_read_consume(a, salt_len + 2);
  2738. zip->entry_bytes_remaining -= salt_len + 2 + AUTH_CODE_SIZE;
  2739. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  2740. && zip->entry_bytes_remaining < 0)
  2741. goto corrupted;
  2742. zip->entry_compressed_bytes_read += salt_len + 2 + AUTH_CODE_SIZE;
  2743. zip->decrypted_bytes_remaining = 0;
  2744. zip->entry->compression = zip->entry->aes_extra.compression;
  2745. return (zip_alloc_decryption_buffer(a));
  2746. truncated:
  2747. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2748. "Truncated ZIP file data");
  2749. return (ARCHIVE_FATAL);
  2750. corrupted:
  2751. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2752. "Corrupted ZIP file data");
  2753. return (ARCHIVE_FATAL);
  2754. }
  2755. static int
  2756. archive_read_format_zip_read_data(struct archive_read *a,
  2757. const void **buff, size_t *size, int64_t *offset)
  2758. {
  2759. int r;
  2760. struct zip *zip = (struct zip *)(a->format->data);
  2761. if (zip->has_encrypted_entries ==
  2762. ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  2763. zip->has_encrypted_entries = 0;
  2764. }
  2765. *offset = zip->entry_uncompressed_bytes_read;
  2766. *size = 0;
  2767. *buff = NULL;
  2768. /* If we hit end-of-entry last time, return ARCHIVE_EOF. */
  2769. if (zip->end_of_entry)
  2770. return (ARCHIVE_EOF);
  2771. /* Return EOF immediately if this is a non-regular file. */
  2772. if (AE_IFREG != (zip->entry->mode & AE_IFMT))
  2773. return (ARCHIVE_EOF);
  2774. __archive_read_consume(a, zip->unconsumed);
  2775. zip->unconsumed = 0;
  2776. if (zip->init_decryption) {
  2777. zip->has_encrypted_entries = 1;
  2778. if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
  2779. r = read_decryption_header(a);
  2780. else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
  2781. r = init_WinZip_AES_decryption(a);
  2782. else
  2783. r = init_traditional_PKWARE_decryption(a);
  2784. if (r != ARCHIVE_OK)
  2785. return (r);
  2786. zip->init_decryption = 0;
  2787. }
  2788. switch(zip->entry->compression) {
  2789. case 0: /* No compression. */
  2790. r = zip_read_data_none(a, buff, size, offset);
  2791. break;
  2792. #ifdef HAVE_BZLIB_H
  2793. case 12: /* ZIPx bzip2 compression. */
  2794. r = zip_read_data_zipx_bzip2(a, buff, size, offset);
  2795. break;
  2796. #endif
  2797. #if HAVE_LZMA_H && HAVE_LIBLZMA
  2798. case 14: /* ZIPx LZMA compression. */
  2799. r = zip_read_data_zipx_lzma_alone(a, buff, size, offset);
  2800. break;
  2801. case 95: /* ZIPx XZ compression. */
  2802. r = zip_read_data_zipx_xz(a, buff, size, offset);
  2803. break;
  2804. #endif
  2805. #if HAVE_ZSTD_H && HAVE_LIBZSTD
  2806. case 93: /* ZIPx Zstd compression. */
  2807. r = zip_read_data_zipx_zstd(a, buff, size, offset);
  2808. break;
  2809. #endif
  2810. /* PPMd support is built-in, so we don't need any #if guards. */
  2811. case 98: /* ZIPx PPMd compression. */
  2812. r = zip_read_data_zipx_ppmd(a, buff, size, offset);
  2813. break;
  2814. #ifdef HAVE_ZLIB_H
  2815. case 8: /* Deflate compression. */
  2816. r = zip_read_data_deflate(a, buff, size, offset);
  2817. break;
  2818. #endif
  2819. default: /* Unsupported compression. */
  2820. /* Return a warning. */
  2821. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2822. "Unsupported ZIP compression method (%d: %s)",
  2823. zip->entry->compression, compression_name(zip->entry->compression));
  2824. /* We can't decompress this entry, but we will
  2825. * be able to skip() it and try the next entry. */
  2826. return (ARCHIVE_FAILED);
  2827. }
  2828. if (r != ARCHIVE_OK)
  2829. return (r);
  2830. if (*size > 0) {
  2831. zip->computed_crc32 = zip->crc32func(zip->computed_crc32, *buff,
  2832. (unsigned)*size);
  2833. }
  2834. /* If we hit the end, swallow any end-of-data marker and
  2835. * verify the final check values. */
  2836. if (zip->end_of_entry) {
  2837. consume_end_of_file_marker(a, zip);
  2838. /* Check computed CRC against header */
  2839. if ((!zip->hctx_valid ||
  2840. zip->entry->aes_extra.vendor != AES_VENDOR_AE_2) &&
  2841. zip->entry->crc32 != zip->computed_crc32
  2842. && !zip->ignore_crc32) {
  2843. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2844. "ZIP bad CRC: 0x%lx should be 0x%lx",
  2845. (unsigned long)zip->computed_crc32,
  2846. (unsigned long)zip->entry->crc32);
  2847. return (ARCHIVE_FAILED);
  2848. }
  2849. /* Check file size against header. */
  2850. if (zip->entry->compressed_size !=
  2851. zip->entry_compressed_bytes_read) {
  2852. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2853. "ZIP compressed data is wrong size "
  2854. "(read %jd, expected %jd)",
  2855. (intmax_t)zip->entry_compressed_bytes_read,
  2856. (intmax_t)zip->entry->compressed_size);
  2857. return (ARCHIVE_FAILED);
  2858. }
  2859. /* Size field only stores the lower 32 bits of the actual
  2860. * size. */
  2861. if ((zip->entry->uncompressed_size & UINT32_MAX)
  2862. != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
  2863. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2864. "ZIP uncompressed data is wrong size "
  2865. "(read %jd, expected %jd)\n",
  2866. (intmax_t)zip->entry_uncompressed_bytes_read,
  2867. (intmax_t)zip->entry->uncompressed_size);
  2868. return (ARCHIVE_FAILED);
  2869. }
  2870. }
  2871. return (ARCHIVE_OK);
  2872. }
  2873. static int
  2874. archive_read_format_zip_cleanup(struct archive_read *a)
  2875. {
  2876. struct zip *zip;
  2877. struct zip_entry *zip_entry, *next_zip_entry;
  2878. zip = (struct zip *)(a->format->data);
  2879. #ifdef HAVE_ZLIB_H
  2880. if (zip->stream_valid)
  2881. inflateEnd(&zip->stream);
  2882. #endif
  2883. #if HAVE_LZMA_H && HAVE_LIBLZMA
  2884. if (zip->zipx_lzma_valid) {
  2885. lzma_end(&zip->zipx_lzma_stream);
  2886. }
  2887. #endif
  2888. #ifdef HAVE_BZLIB_H
  2889. if (zip->bzstream_valid) {
  2890. BZ2_bzDecompressEnd(&zip->bzstream);
  2891. }
  2892. #endif
  2893. #if HAVE_ZSTD_H && HAVE_LIBZSTD
  2894. if (zip->zstdstream_valid) {
  2895. ZSTD_freeDStream(zip->zstdstream);
  2896. }
  2897. #endif
  2898. free(zip->uncompressed_buffer);
  2899. if (zip->ppmd8_valid)
  2900. __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
  2901. if (zip->zip_entries) {
  2902. zip_entry = zip->zip_entries;
  2903. while (zip_entry != NULL) {
  2904. next_zip_entry = zip_entry->next;
  2905. archive_string_free(&zip_entry->rsrcname);
  2906. free(zip_entry);
  2907. zip_entry = next_zip_entry;
  2908. }
  2909. }
  2910. free(zip->decrypted_buffer);
  2911. if (zip->cctx_valid)
  2912. archive_decrypto_aes_ctr_release(&zip->cctx);
  2913. if (zip->hctx_valid)
  2914. archive_hmac_sha1_cleanup(&zip->hctx);
  2915. free(zip->iv);
  2916. free(zip->erd);
  2917. free(zip->v_data);
  2918. archive_string_free(&zip->format_name);
  2919. free(zip);
  2920. (a->format->data) = NULL;
  2921. return (ARCHIVE_OK);
  2922. }
  2923. static int
  2924. archive_read_format_zip_has_encrypted_entries(struct archive_read *_a)
  2925. {
  2926. if (_a && _a->format) {
  2927. struct zip * zip = (struct zip *)_a->format->data;
  2928. if (zip) {
  2929. return zip->has_encrypted_entries;
  2930. }
  2931. }
  2932. return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  2933. }
  2934. static int
  2935. archive_read_format_zip_options(struct archive_read *a,
  2936. const char *key, const char *val)
  2937. {
  2938. struct zip *zip;
  2939. int ret = ARCHIVE_FAILED;
  2940. zip = (struct zip *)(a->format->data);
  2941. if (strcmp(key, "compat-2x") == 0) {
  2942. /* Handle filenames as libarchive 2.x */
  2943. zip->init_default_conversion = (val != NULL) ? 1 : 0;
  2944. return (ARCHIVE_OK);
  2945. } else if (strcmp(key, "hdrcharset") == 0) {
  2946. if (val == NULL || val[0] == 0)
  2947. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2948. "zip: hdrcharset option needs a character-set name"
  2949. );
  2950. else {
  2951. zip->sconv = archive_string_conversion_from_charset(
  2952. &a->archive, val, 0);
  2953. if (zip->sconv != NULL) {
  2954. if (strcmp(val, "UTF-8") == 0)
  2955. zip->sconv_utf8 = zip->sconv;
  2956. ret = ARCHIVE_OK;
  2957. } else
  2958. ret = ARCHIVE_FATAL;
  2959. }
  2960. return (ret);
  2961. } else if (strcmp(key, "ignorecrc32") == 0) {
  2962. /* Mostly useful for testing. */
  2963. if (val == NULL || val[0] == 0) {
  2964. zip->crc32func = real_crc32;
  2965. zip->ignore_crc32 = 0;
  2966. } else {
  2967. zip->crc32func = fake_crc32;
  2968. zip->ignore_crc32 = 1;
  2969. }
  2970. return (ARCHIVE_OK);
  2971. } else if (strcmp(key, "mac-ext") == 0) {
  2972. zip->process_mac_extensions = (val != NULL && val[0] != 0);
  2973. return (ARCHIVE_OK);
  2974. }
  2975. /* Note: The "warn" return is just to inform the options
  2976. * supervisor that we didn't handle it. It will generate
  2977. * a suitable error if no one used this option. */
  2978. return (ARCHIVE_WARN);
  2979. }
  2980. int
  2981. archive_read_support_format_zip(struct archive *a)
  2982. {
  2983. int r;
  2984. r = archive_read_support_format_zip_streamable(a);
  2985. if (r != ARCHIVE_OK)
  2986. return r;
  2987. return (archive_read_support_format_zip_seekable(a));
  2988. }
  2989. /* ------------------------------------------------------------------------ */
  2990. /*
  2991. * Streaming-mode support
  2992. */
  2993. static int
  2994. archive_read_support_format_zip_capabilities_streamable(struct archive_read * a)
  2995. {
  2996. (void)a; /* UNUSED */
  2997. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
  2998. ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  2999. }
  3000. static int
  3001. archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
  3002. {
  3003. const char *p;
  3004. (void)best_bid; /* UNUSED */
  3005. if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
  3006. return (-1);
  3007. /*
  3008. * Bid of 29 here comes from:
  3009. * + 16 bits for "PK",
  3010. * + next 16-bit field has 6 options so contributes
  3011. * about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
  3012. *
  3013. * So we've effectively verified ~29 total bits of check data.
  3014. */
  3015. if (p[0] == 'P' && p[1] == 'K') {
  3016. if ((p[2] == '\001' && p[3] == '\002')
  3017. || (p[2] == '\003' && p[3] == '\004')
  3018. || (p[2] == '\005' && p[3] == '\006')
  3019. || (p[2] == '\006' && p[3] == '\006')
  3020. || (p[2] == '\007' && p[3] == '\010')
  3021. || (p[2] == '0' && p[3] == '0'))
  3022. return (29);
  3023. }
  3024. /* TODO: It's worth looking ahead a little bit for a valid
  3025. * PK signature. In particular, that would make it possible
  3026. * to read some UUEncoded SFX files or SFX files coming from
  3027. * a network socket. */
  3028. return (0);
  3029. }
  3030. static int
  3031. archive_read_format_zip_streamable_read_header(struct archive_read *a,
  3032. struct archive_entry *entry)
  3033. {
  3034. struct zip *zip;
  3035. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  3036. if (a->archive.archive_format_name == NULL)
  3037. a->archive.archive_format_name = "ZIP";
  3038. zip = (struct zip *)(a->format->data);
  3039. /*
  3040. * It should be sufficient to call archive_read_next_header() for
  3041. * a reader to determine if an entry is encrypted or not. If the
  3042. * encryption of an entry is only detectable when calling
  3043. * archive_read_data(), so be it. We'll do the same check there
  3044. * as well.
  3045. */
  3046. if (zip->has_encrypted_entries ==
  3047. ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
  3048. zip->has_encrypted_entries = 0;
  3049. /* Make sure we have a zip_entry structure to use. */
  3050. if (zip->zip_entries == NULL) {
  3051. zip->zip_entries = malloc(sizeof(struct zip_entry));
  3052. if (zip->zip_entries == NULL) {
  3053. archive_set_error(&a->archive, ENOMEM,
  3054. "Out of memory");
  3055. return ARCHIVE_FATAL;
  3056. }
  3057. }
  3058. zip->entry = zip->zip_entries;
  3059. memset(zip->entry, 0, sizeof(struct zip_entry));
  3060. if (zip->cctx_valid)
  3061. archive_decrypto_aes_ctr_release(&zip->cctx);
  3062. if (zip->hctx_valid)
  3063. archive_hmac_sha1_cleanup(&zip->hctx);
  3064. zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
  3065. __archive_read_reset_passphrase(a);
  3066. /* Search ahead for the next local file header. */
  3067. __archive_read_consume(a, zip->unconsumed);
  3068. zip->unconsumed = 0;
  3069. for (;;) {
  3070. int64_t skipped = 0;
  3071. const char *p, *end;
  3072. ssize_t bytes;
  3073. p = __archive_read_ahead(a, 4, &bytes);
  3074. if (p == NULL)
  3075. return (ARCHIVE_FATAL);
  3076. end = p + bytes;
  3077. while (p + 4 <= end) {
  3078. if (p[0] == 'P' && p[1] == 'K') {
  3079. if (p[2] == '\003' && p[3] == '\004') {
  3080. /* Regular file entry. */
  3081. __archive_read_consume(a, skipped);
  3082. return zip_read_local_file_header(a,
  3083. entry, zip);
  3084. }
  3085. /*
  3086. * TODO: We cannot restore permissions
  3087. * based only on the local file headers.
  3088. * Consider scanning the central
  3089. * directory and returning additional
  3090. * entries for at least directories.
  3091. * This would allow us to properly set
  3092. * directory permissions.
  3093. *
  3094. * This won't help us fix symlinks
  3095. * and may not help with regular file
  3096. * permissions, either. <sigh>
  3097. */
  3098. if (p[2] == '\001' && p[3] == '\002') {
  3099. return (ARCHIVE_EOF);
  3100. }
  3101. /* End of central directory? Must be an
  3102. * empty archive. */
  3103. if ((p[2] == '\005' && p[3] == '\006')
  3104. || (p[2] == '\006' && p[3] == '\006'))
  3105. return (ARCHIVE_EOF);
  3106. }
  3107. ++p;
  3108. ++skipped;
  3109. }
  3110. __archive_read_consume(a, skipped);
  3111. }
  3112. }
  3113. static int
  3114. archive_read_format_zip_read_data_skip_streamable(struct archive_read *a)
  3115. {
  3116. struct zip *zip;
  3117. int64_t bytes_skipped;
  3118. zip = (struct zip *)(a->format->data);
  3119. bytes_skipped = __archive_read_consume(a, zip->unconsumed);
  3120. zip->unconsumed = 0;
  3121. if (bytes_skipped < 0)
  3122. return (ARCHIVE_FATAL);
  3123. /* If we've already read to end of data, we're done. */
  3124. if (zip->end_of_entry)
  3125. return (ARCHIVE_OK);
  3126. /* So we know we're streaming... */
  3127. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  3128. || zip->entry->compressed_size > 0) {
  3129. /* We know the compressed length, so we can just skip. */
  3130. bytes_skipped = __archive_read_consume(a,
  3131. zip->entry_bytes_remaining);
  3132. if (bytes_skipped < 0)
  3133. return (ARCHIVE_FATAL);
  3134. return (ARCHIVE_OK);
  3135. }
  3136. if (zip->init_decryption) {
  3137. int r;
  3138. zip->has_encrypted_entries = 1;
  3139. if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
  3140. r = read_decryption_header(a);
  3141. else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
  3142. r = init_WinZip_AES_decryption(a);
  3143. else
  3144. r = init_traditional_PKWARE_decryption(a);
  3145. if (r != ARCHIVE_OK)
  3146. return (r);
  3147. zip->init_decryption = 0;
  3148. }
  3149. /* We're streaming and we don't know the length. */
  3150. /* If the body is compressed and we know the format, we can
  3151. * find an exact end-of-entry by decompressing it. */
  3152. switch (zip->entry->compression) {
  3153. #ifdef HAVE_ZLIB_H
  3154. case 8: /* Deflate compression. */
  3155. while (!zip->end_of_entry) {
  3156. int64_t offset = 0;
  3157. const void *buff = NULL;
  3158. size_t size = 0;
  3159. int r;
  3160. r = zip_read_data_deflate(a, &buff, &size, &offset);
  3161. if (r != ARCHIVE_OK)
  3162. return (r);
  3163. }
  3164. return ARCHIVE_OK;
  3165. #endif
  3166. default: /* Uncompressed or unknown. */
  3167. /* Scan for a PK\007\010 signature. */
  3168. for (;;) {
  3169. const char *p, *buff;
  3170. ssize_t bytes_avail;
  3171. buff = __archive_read_ahead(a, 16, &bytes_avail);
  3172. if (bytes_avail < 16) {
  3173. archive_set_error(&a->archive,
  3174. ARCHIVE_ERRNO_FILE_FORMAT,
  3175. "Truncated ZIP file data");
  3176. return (ARCHIVE_FATAL);
  3177. }
  3178. p = buff;
  3179. while (p <= buff + bytes_avail - 16) {
  3180. if (p[3] == 'P') { p += 3; }
  3181. else if (p[3] == 'K') { p += 2; }
  3182. else if (p[3] == '\007') { p += 1; }
  3183. else if (p[3] == '\010' && p[2] == '\007'
  3184. && p[1] == 'K' && p[0] == 'P') {
  3185. if (zip->entry->flags & LA_USED_ZIP64)
  3186. __archive_read_consume(a,
  3187. p - buff + 24);
  3188. else
  3189. __archive_read_consume(a,
  3190. p - buff + 16);
  3191. return ARCHIVE_OK;
  3192. } else { p += 4; }
  3193. }
  3194. __archive_read_consume(a, p - buff);
  3195. }
  3196. }
  3197. }
  3198. int
  3199. archive_read_support_format_zip_streamable(struct archive *_a)
  3200. {
  3201. struct archive_read *a = (struct archive_read *)_a;
  3202. struct zip *zip;
  3203. int r;
  3204. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  3205. ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
  3206. zip = calloc(1, sizeof(*zip));
  3207. if (zip == NULL) {
  3208. archive_set_error(&a->archive, ENOMEM,
  3209. "Can't allocate zip data");
  3210. return (ARCHIVE_FATAL);
  3211. }
  3212. /* Streamable reader doesn't support mac extensions. */
  3213. zip->process_mac_extensions = 0;
  3214. /*
  3215. * Until enough data has been read, we cannot tell about
  3216. * any encrypted entries yet.
  3217. */
  3218. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  3219. zip->crc32func = real_crc32;
  3220. r = __archive_read_register_format(a,
  3221. zip,
  3222. "zip",
  3223. archive_read_format_zip_streamable_bid,
  3224. archive_read_format_zip_options,
  3225. archive_read_format_zip_streamable_read_header,
  3226. archive_read_format_zip_read_data,
  3227. archive_read_format_zip_read_data_skip_streamable,
  3228. NULL,
  3229. archive_read_format_zip_cleanup,
  3230. archive_read_support_format_zip_capabilities_streamable,
  3231. archive_read_format_zip_has_encrypted_entries);
  3232. if (r != ARCHIVE_OK)
  3233. free(zip);
  3234. return (ARCHIVE_OK);
  3235. }
  3236. /* ------------------------------------------------------------------------ */
  3237. /*
  3238. * Seeking-mode support
  3239. */
  3240. static int
  3241. archive_read_support_format_zip_capabilities_seekable(struct archive_read * a)
  3242. {
  3243. (void)a; /* UNUSED */
  3244. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
  3245. ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  3246. }
  3247. /*
  3248. * TODO: This is a performance sink because it forces the read core to
  3249. * drop buffered data from the start of file, which will then have to
  3250. * be re-read again if this bidder loses.
  3251. *
  3252. * We workaround this a little by passing in the best bid so far so
  3253. * that later bidders can do nothing if they know they'll never
  3254. * outbid. But we can certainly do better...
  3255. */
  3256. static int
  3257. read_eocd(struct zip *zip, const char *p, int64_t current_offset)
  3258. {
  3259. uint16_t disk_num;
  3260. uint32_t cd_size, cd_offset;
  3261. disk_num = archive_le16dec(p + 4);
  3262. cd_size = archive_le32dec(p + 12);
  3263. cd_offset = archive_le32dec(p + 16);
  3264. /* Sanity-check the EOCD we've found. */
  3265. /* This must be the first volume. */
  3266. if (disk_num != 0)
  3267. return 0;
  3268. /* Central directory must be on this volume. */
  3269. if (disk_num != archive_le16dec(p + 6))
  3270. return 0;
  3271. /* All central directory entries must be on this volume. */
  3272. if (archive_le16dec(p + 10) != archive_le16dec(p + 8))
  3273. return 0;
  3274. /* Central directory can't extend beyond start of EOCD record. */
  3275. if ((int64_t)cd_offset + cd_size > current_offset)
  3276. return 0;
  3277. /* Save the central directory location for later use. */
  3278. zip->central_directory_offset = cd_offset;
  3279. zip->central_directory_offset_adjusted = current_offset - cd_size;
  3280. /* This is just a tiny bit higher than the maximum
  3281. returned by the streaming Zip bidder. This ensures
  3282. that the more accurate seeking Zip parser wins
  3283. whenever seek is available. */
  3284. return 32;
  3285. }
  3286. /*
  3287. * Examine Zip64 EOCD locator: If it's valid, store the information
  3288. * from it.
  3289. */
  3290. static int
  3291. read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p)
  3292. {
  3293. int64_t eocd64_offset;
  3294. int64_t eocd64_size;
  3295. /* Sanity-check the locator record. */
  3296. /* Central dir must be on first volume. */
  3297. if (archive_le32dec(p + 4) != 0)
  3298. return 0;
  3299. /* Must be only a single volume. */
  3300. if (archive_le32dec(p + 16) != 1)
  3301. return 0;
  3302. /* Find the Zip64 EOCD record. */
  3303. eocd64_offset = archive_le64dec(p + 8);
  3304. if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0)
  3305. return 0;
  3306. if ((p = __archive_read_ahead(a, 56, NULL)) == NULL)
  3307. return 0;
  3308. /* Make sure we can read all of it. */
  3309. eocd64_size = archive_le64dec(p + 4) + 12;
  3310. if (eocd64_size < 56 || eocd64_size > 16384)
  3311. return 0;
  3312. if ((p = __archive_read_ahead(a, (size_t)eocd64_size, NULL)) == NULL)
  3313. return 0;
  3314. /* Sanity-check the EOCD64 */
  3315. if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */
  3316. return 0;
  3317. if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */
  3318. return 0;
  3319. /* CD can't be split. */
  3320. if (archive_le64dec(p + 24) != archive_le64dec(p + 32))
  3321. return 0;
  3322. /* Save the central directory offset for later use. */
  3323. zip->central_directory_offset = archive_le64dec(p + 48);
  3324. /* TODO: Needs scanning backwards to find the eocd64 instead of assuming */
  3325. zip->central_directory_offset_adjusted = zip->central_directory_offset;
  3326. return 32;
  3327. }
  3328. static int
  3329. archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
  3330. {
  3331. struct zip *zip = (struct zip *)a->format->data;
  3332. int64_t file_size, current_offset;
  3333. const char *p;
  3334. int i, tail;
  3335. /* If someone has already bid more than 32, then avoid
  3336. trashing the look-ahead buffers with a seek. */
  3337. if (best_bid > 32)
  3338. return (-1);
  3339. file_size = __archive_read_seek(a, 0, SEEK_END);
  3340. if (file_size <= 0)
  3341. return 0;
  3342. /* Search last 16k of file for end-of-central-directory
  3343. * record (which starts with PK\005\006) */
  3344. tail = (int)zipmin(1024 * 16, file_size);
  3345. current_offset = __archive_read_seek(a, -tail, SEEK_END);
  3346. if (current_offset < 0)
  3347. return 0;
  3348. if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
  3349. return 0;
  3350. /* Boyer-Moore search backwards from the end, since we want
  3351. * to match the last EOCD in the file (there can be more than
  3352. * one if there is an uncompressed Zip archive as a member
  3353. * within this Zip archive). */
  3354. for (i = tail - 22; i > 0;) {
  3355. switch (p[i]) {
  3356. case 'P':
  3357. if (memcmp(p + i, "PK\005\006", 4) == 0) {
  3358. int ret = read_eocd(zip, p + i,
  3359. current_offset + i);
  3360. /* Zip64 EOCD locator precedes
  3361. * regular EOCD if present. */
  3362. if (i >= 20 && memcmp(p + i - 20, "PK\006\007", 4) == 0) {
  3363. int ret_zip64 = read_zip64_eocd(a, zip, p + i - 20);
  3364. if (ret_zip64 > ret)
  3365. ret = ret_zip64;
  3366. }
  3367. return (ret);
  3368. }
  3369. i -= 4;
  3370. break;
  3371. case 'K': i -= 1; break;
  3372. case 005: i -= 2; break;
  3373. case 006: i -= 3; break;
  3374. default: i -= 4; break;
  3375. }
  3376. }
  3377. return 0;
  3378. }
  3379. /* The red-black trees are only used in seeking mode to manage
  3380. * the in-memory copy of the central directory. */
  3381. static int
  3382. cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
  3383. {
  3384. const struct zip_entry *e1 = (const struct zip_entry *)n1;
  3385. const struct zip_entry *e2 = (const struct zip_entry *)n2;
  3386. if (e1->local_header_offset > e2->local_header_offset)
  3387. return -1;
  3388. if (e1->local_header_offset < e2->local_header_offset)
  3389. return 1;
  3390. return 0;
  3391. }
  3392. static int
  3393. cmp_key(const struct archive_rb_node *n, const void *key)
  3394. {
  3395. /* This function won't be called */
  3396. (void)n; /* UNUSED */
  3397. (void)key; /* UNUSED */
  3398. return 1;
  3399. }
  3400. static const struct archive_rb_tree_ops rb_ops = {
  3401. &cmp_node, &cmp_key
  3402. };
  3403. static int
  3404. rsrc_cmp_node(const struct archive_rb_node *n1,
  3405. const struct archive_rb_node *n2)
  3406. {
  3407. const struct zip_entry *e1 = (const struct zip_entry *)n1;
  3408. const struct zip_entry *e2 = (const struct zip_entry *)n2;
  3409. return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
  3410. }
  3411. static int
  3412. rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
  3413. {
  3414. const struct zip_entry *e = (const struct zip_entry *)n;
  3415. return (strcmp((const char *)key, e->rsrcname.s));
  3416. }
  3417. static const struct archive_rb_tree_ops rb_rsrc_ops = {
  3418. &rsrc_cmp_node, &rsrc_cmp_key
  3419. };
  3420. static const char *
  3421. rsrc_basename(const char *name, size_t name_length)
  3422. {
  3423. const char *s, *r;
  3424. r = s = name;
  3425. for (;;) {
  3426. s = memchr(s, '/', name_length - (s - name));
  3427. if (s == NULL)
  3428. break;
  3429. r = ++s;
  3430. }
  3431. return (r);
  3432. }
  3433. static void
  3434. expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
  3435. {
  3436. struct archive_string str;
  3437. struct zip_entry *dir;
  3438. char *s;
  3439. archive_string_init(&str);
  3440. archive_strncpy(&str, name, name_length);
  3441. for (;;) {
  3442. s = strrchr(str.s, '/');
  3443. if (s == NULL)
  3444. break;
  3445. *s = '\0';
  3446. /* Transfer the parent directory from zip->tree_rsrc RB
  3447. * tree to zip->tree RB tree to expose. */
  3448. dir = (struct zip_entry *)
  3449. __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
  3450. if (dir == NULL)
  3451. break;
  3452. __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
  3453. archive_string_free(&dir->rsrcname);
  3454. __archive_rb_tree_insert_node(&zip->tree, &dir->node);
  3455. }
  3456. archive_string_free(&str);
  3457. }
  3458. static int
  3459. slurp_central_directory(struct archive_read *a, struct archive_entry* entry,
  3460. struct zip *zip)
  3461. {
  3462. ssize_t i;
  3463. unsigned found;
  3464. int64_t correction;
  3465. ssize_t bytes_avail;
  3466. const char *p;
  3467. /*
  3468. * Find the start of the central directory. The end-of-CD
  3469. * record has our starting point, but there are lots of
  3470. * Zip archives which have had other data prepended to the
  3471. * file, which makes the recorded offsets all too small.
  3472. * So we search forward from the specified offset until we
  3473. * find the real start of the central directory. Then we
  3474. * know the correction we need to apply to account for leading
  3475. * padding.
  3476. */
  3477. if (__archive_read_seek(a, zip->central_directory_offset_adjusted, SEEK_SET)
  3478. < 0)
  3479. return ARCHIVE_FATAL;
  3480. found = 0;
  3481. while (!found) {
  3482. if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL)
  3483. return ARCHIVE_FATAL;
  3484. for (found = 0, i = 0; !found && i < bytes_avail - 4;) {
  3485. switch (p[i + 3]) {
  3486. case 'P': i += 3; break;
  3487. case 'K': i += 2; break;
  3488. case 001: i += 1; break;
  3489. case 002:
  3490. if (memcmp(p + i, "PK\001\002", 4) == 0) {
  3491. p += i;
  3492. found = 1;
  3493. } else
  3494. i += 4;
  3495. break;
  3496. case 005: i += 1; break;
  3497. case 006:
  3498. if (memcmp(p + i, "PK\005\006", 4) == 0) {
  3499. p += i;
  3500. found = 1;
  3501. } else if (memcmp(p + i, "PK\006\006", 4) == 0) {
  3502. p += i;
  3503. found = 1;
  3504. } else
  3505. i += 1;
  3506. break;
  3507. default: i += 4; break;
  3508. }
  3509. }
  3510. __archive_read_consume(a, i);
  3511. }
  3512. correction = archive_filter_bytes(&a->archive, 0)
  3513. - zip->central_directory_offset;
  3514. __archive_rb_tree_init(&zip->tree, &rb_ops);
  3515. __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
  3516. zip->central_directory_entries_total = 0;
  3517. while (1) {
  3518. struct zip_entry *zip_entry;
  3519. size_t filename_length, extra_length, comment_length;
  3520. uint32_t external_attributes;
  3521. const char *name, *r;
  3522. if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
  3523. return ARCHIVE_FATAL;
  3524. if (memcmp(p, "PK\006\006", 4) == 0
  3525. || memcmp(p, "PK\005\006", 4) == 0) {
  3526. break;
  3527. } else if (memcmp(p, "PK\001\002", 4) != 0) {
  3528. archive_set_error(&a->archive,
  3529. -1, "Invalid central directory signature");
  3530. return ARCHIVE_FATAL;
  3531. }
  3532. if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
  3533. return ARCHIVE_FATAL;
  3534. zip_entry = calloc(1, sizeof(struct zip_entry));
  3535. if (zip_entry == NULL) {
  3536. archive_set_error(&a->archive, ENOMEM,
  3537. "Can't allocate zip entry");
  3538. return ARCHIVE_FATAL;
  3539. }
  3540. zip_entry->next = zip->zip_entries;
  3541. zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY;
  3542. zip->zip_entries = zip_entry;
  3543. zip->central_directory_entries_total++;
  3544. /* version = p[4]; */
  3545. zip_entry->system = p[5];
  3546. /* version_required = archive_le16dec(p + 6); */
  3547. zip_entry->zip_flags = archive_le16dec(p + 8);
  3548. if (zip_entry->zip_flags
  3549. & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){
  3550. zip->has_encrypted_entries = 1;
  3551. }
  3552. zip_entry->compression = (char)archive_le16dec(p + 10);
  3553. zip_entry->mtime = dos_to_unix(archive_le32dec(p + 12));
  3554. zip_entry->crc32 = archive_le32dec(p + 16);
  3555. if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  3556. zip_entry->decdat = p[13];
  3557. else
  3558. zip_entry->decdat = p[19];
  3559. zip_entry->compressed_size = archive_le32dec(p + 20);
  3560. zip_entry->uncompressed_size = archive_le32dec(p + 24);
  3561. filename_length = archive_le16dec(p + 28);
  3562. extra_length = archive_le16dec(p + 30);
  3563. comment_length = archive_le16dec(p + 32);
  3564. /* disk_start = archive_le16dec(p + 34);
  3565. * Better be zero.
  3566. * internal_attributes = archive_le16dec(p + 36);
  3567. * text bit */
  3568. external_attributes = archive_le32dec(p + 38);
  3569. zip_entry->local_header_offset =
  3570. archive_le32dec(p + 42) + correction;
  3571. /* If we can't guess the mode, leave it zero here;
  3572. when we read the local file header we might get
  3573. more information. */
  3574. if (zip_entry->system == 3) {
  3575. zip_entry->mode = external_attributes >> 16;
  3576. } else if (zip_entry->system == 0) {
  3577. // Interpret MSDOS directory bit
  3578. if (0x10 == (external_attributes & 0x10)) {
  3579. zip_entry->mode = AE_IFDIR | 0775;
  3580. } else {
  3581. zip_entry->mode = AE_IFREG | 0664;
  3582. }
  3583. if (0x01 == (external_attributes & 0x01)) {
  3584. // Read-only bit; strip write permissions
  3585. zip_entry->mode &= 0555;
  3586. }
  3587. } else {
  3588. zip_entry->mode = 0;
  3589. }
  3590. /* We're done with the regular data; get the filename and
  3591. * extra data. */
  3592. __archive_read_consume(a, 46);
  3593. p = __archive_read_ahead(a, filename_length + extra_length,
  3594. NULL);
  3595. if (p == NULL) {
  3596. archive_set_error(&a->archive,
  3597. ARCHIVE_ERRNO_FILE_FORMAT,
  3598. "Truncated ZIP file header");
  3599. return ARCHIVE_FATAL;
  3600. }
  3601. if (ARCHIVE_OK != process_extra(a, entry, p + filename_length,
  3602. extra_length, zip_entry)) {
  3603. return ARCHIVE_FATAL;
  3604. }
  3605. /*
  3606. * Mac resource fork files are stored under the
  3607. * "__MACOSX/" directory, so we should check if
  3608. * it is.
  3609. */
  3610. if (!zip->process_mac_extensions) {
  3611. /* Treat every entry as a regular entry. */
  3612. __archive_rb_tree_insert_node(&zip->tree,
  3613. &zip_entry->node);
  3614. } else {
  3615. name = p;
  3616. r = rsrc_basename(name, filename_length);
  3617. if (filename_length >= 9 &&
  3618. strncmp("__MACOSX/", name, 9) == 0) {
  3619. /* If this file is not a resource fork nor
  3620. * a directory. We should treat it as a non
  3621. * resource fork file to expose it. */
  3622. if (name[filename_length-1] != '/' &&
  3623. (r - name < 3 || r[0] != '.' ||
  3624. r[1] != '_')) {
  3625. __archive_rb_tree_insert_node(
  3626. &zip->tree, &zip_entry->node);
  3627. /* Expose its parent directories. */
  3628. expose_parent_dirs(zip, name,
  3629. filename_length);
  3630. } else {
  3631. /* This file is a resource fork file or
  3632. * a directory. */
  3633. archive_strncpy(&(zip_entry->rsrcname),
  3634. name, filename_length);
  3635. __archive_rb_tree_insert_node(
  3636. &zip->tree_rsrc, &zip_entry->node);
  3637. }
  3638. } else {
  3639. /* Generate resource fork name to find its
  3640. * resource file at zip->tree_rsrc. */
  3641. /* If this is an entry ending with slash,
  3642. * make the resource for name slash-less
  3643. * as the actual resource fork doesn't end with '/'.
  3644. */
  3645. size_t tmp_length = filename_length;
  3646. if (tmp_length > 0 && name[tmp_length - 1] == '/') {
  3647. tmp_length--;
  3648. r = rsrc_basename(name, tmp_length);
  3649. }
  3650. archive_strcpy(&(zip_entry->rsrcname),
  3651. "__MACOSX/");
  3652. archive_strncat(&(zip_entry->rsrcname),
  3653. name, r - name);
  3654. archive_strcat(&(zip_entry->rsrcname), "._");
  3655. archive_strncat(&(zip_entry->rsrcname),
  3656. name + (r - name),
  3657. tmp_length - (r - name));
  3658. /* Register an entry to RB tree to sort it by
  3659. * file offset. */
  3660. __archive_rb_tree_insert_node(&zip->tree,
  3661. &zip_entry->node);
  3662. }
  3663. }
  3664. /* Skip the comment too ... */
  3665. __archive_read_consume(a,
  3666. filename_length + extra_length + comment_length);
  3667. }
  3668. return ARCHIVE_OK;
  3669. }
  3670. static ssize_t
  3671. zip_get_local_file_header_size(struct archive_read *a, size_t extra)
  3672. {
  3673. const char *p;
  3674. ssize_t filename_length, extra_length;
  3675. if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) {
  3676. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3677. "Truncated ZIP file header");
  3678. return (ARCHIVE_WARN);
  3679. }
  3680. p += extra;
  3681. if (memcmp(p, "PK\003\004", 4) != 0) {
  3682. archive_set_error(&a->archive, -1, "Damaged Zip archive");
  3683. return ARCHIVE_WARN;
  3684. }
  3685. filename_length = archive_le16dec(p + 26);
  3686. extra_length = archive_le16dec(p + 28);
  3687. return (30 + filename_length + extra_length);
  3688. }
  3689. static int
  3690. zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
  3691. struct zip_entry *rsrc)
  3692. {
  3693. struct zip *zip = (struct zip *)a->format->data;
  3694. unsigned char *metadata, *mp;
  3695. int64_t offset = archive_filter_bytes(&a->archive, 0);
  3696. size_t remaining_bytes, metadata_bytes;
  3697. ssize_t hsize;
  3698. int ret = ARCHIVE_OK, eof;
  3699. switch(rsrc->compression) {
  3700. case 0: /* No compression. */
  3701. if (rsrc->uncompressed_size != rsrc->compressed_size) {
  3702. archive_set_error(&a->archive,
  3703. ARCHIVE_ERRNO_FILE_FORMAT,
  3704. "Malformed OS X metadata entry: "
  3705. "inconsistent size");
  3706. return (ARCHIVE_FATAL);
  3707. }
  3708. #ifdef HAVE_ZLIB_H
  3709. case 8: /* Deflate compression. */
  3710. #endif
  3711. break;
  3712. default: /* Unsupported compression. */
  3713. /* Return a warning. */
  3714. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3715. "Unsupported ZIP compression method (%s)",
  3716. compression_name(rsrc->compression));
  3717. /* We can't decompress this entry, but we will
  3718. * be able to skip() it and try the next entry. */
  3719. return (ARCHIVE_WARN);
  3720. }
  3721. if (rsrc->uncompressed_size > (4 * 1024 * 1024)) {
  3722. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3723. "Mac metadata is too large: %jd > 4M bytes",
  3724. (intmax_t)rsrc->uncompressed_size);
  3725. return (ARCHIVE_WARN);
  3726. }
  3727. if (rsrc->compressed_size > (4 * 1024 * 1024)) {
  3728. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3729. "Mac metadata is too large: %jd > 4M bytes",
  3730. (intmax_t)rsrc->compressed_size);
  3731. return (ARCHIVE_WARN);
  3732. }
  3733. metadata = malloc((size_t)rsrc->uncompressed_size);
  3734. if (metadata == NULL) {
  3735. archive_set_error(&a->archive, ENOMEM,
  3736. "Can't allocate memory for Mac metadata");
  3737. return (ARCHIVE_FATAL);
  3738. }
  3739. if (offset < rsrc->local_header_offset)
  3740. __archive_read_consume(a, rsrc->local_header_offset - offset);
  3741. else if (offset != rsrc->local_header_offset) {
  3742. __archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
  3743. }
  3744. hsize = zip_get_local_file_header_size(a, 0);
  3745. __archive_read_consume(a, hsize);
  3746. remaining_bytes = (size_t)rsrc->compressed_size;
  3747. metadata_bytes = (size_t)rsrc->uncompressed_size;
  3748. mp = metadata;
  3749. eof = 0;
  3750. while (!eof && remaining_bytes) {
  3751. const unsigned char *p;
  3752. ssize_t bytes_avail;
  3753. size_t bytes_used;
  3754. p = __archive_read_ahead(a, 1, &bytes_avail);
  3755. if (p == NULL) {
  3756. archive_set_error(&a->archive,
  3757. ARCHIVE_ERRNO_FILE_FORMAT,
  3758. "Truncated ZIP file header");
  3759. ret = ARCHIVE_WARN;
  3760. goto exit_mac_metadata;
  3761. }
  3762. if ((size_t)bytes_avail > remaining_bytes)
  3763. bytes_avail = remaining_bytes;
  3764. switch(rsrc->compression) {
  3765. case 0: /* No compression. */
  3766. if ((size_t)bytes_avail > metadata_bytes)
  3767. bytes_avail = metadata_bytes;
  3768. memcpy(mp, p, bytes_avail);
  3769. bytes_used = (size_t)bytes_avail;
  3770. metadata_bytes -= bytes_used;
  3771. mp += bytes_used;
  3772. if (metadata_bytes == 0)
  3773. eof = 1;
  3774. break;
  3775. #ifdef HAVE_ZLIB_H
  3776. case 8: /* Deflate compression. */
  3777. {
  3778. int r;
  3779. ret = zip_deflate_init(a, zip);
  3780. if (ret != ARCHIVE_OK)
  3781. goto exit_mac_metadata;
  3782. zip->stream.next_in =
  3783. (Bytef *)(uintptr_t)(const void *)p;
  3784. zip->stream.avail_in = (uInt)bytes_avail;
  3785. zip->stream.total_in = 0;
  3786. zip->stream.next_out = mp;
  3787. zip->stream.avail_out = (uInt)metadata_bytes;
  3788. zip->stream.total_out = 0;
  3789. r = inflate(&zip->stream, 0);
  3790. switch (r) {
  3791. case Z_OK:
  3792. break;
  3793. case Z_STREAM_END:
  3794. eof = 1;
  3795. break;
  3796. case Z_MEM_ERROR:
  3797. archive_set_error(&a->archive, ENOMEM,
  3798. "Out of memory for ZIP decompression");
  3799. ret = ARCHIVE_FATAL;
  3800. goto exit_mac_metadata;
  3801. default:
  3802. archive_set_error(&a->archive,
  3803. ARCHIVE_ERRNO_MISC,
  3804. "ZIP decompression failed (%d)", r);
  3805. ret = ARCHIVE_FATAL;
  3806. goto exit_mac_metadata;
  3807. }
  3808. bytes_used = zip->stream.total_in;
  3809. metadata_bytes -= zip->stream.total_out;
  3810. mp += zip->stream.total_out;
  3811. break;
  3812. }
  3813. #endif
  3814. default:
  3815. bytes_used = 0;
  3816. break;
  3817. }
  3818. __archive_read_consume(a, bytes_used);
  3819. remaining_bytes -= bytes_used;
  3820. }
  3821. archive_entry_copy_mac_metadata(entry, metadata,
  3822. (size_t)rsrc->uncompressed_size - metadata_bytes);
  3823. exit_mac_metadata:
  3824. __archive_read_seek(a, offset, SEEK_SET);
  3825. zip->decompress_init = 0;
  3826. free(metadata);
  3827. return (ret);
  3828. }
  3829. static int
  3830. archive_read_format_zip_seekable_read_header(struct archive_read *a,
  3831. struct archive_entry *entry)
  3832. {
  3833. struct zip *zip = (struct zip *)a->format->data;
  3834. struct zip_entry *rsrc;
  3835. int64_t offset;
  3836. int r, ret = ARCHIVE_OK;
  3837. /*
  3838. * It should be sufficient to call archive_read_next_header() for
  3839. * a reader to determine if an entry is encrypted or not. If the
  3840. * encryption of an entry is only detectable when calling
  3841. * archive_read_data(), so be it. We'll do the same check there
  3842. * as well.
  3843. */
  3844. if (zip->has_encrypted_entries ==
  3845. ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
  3846. zip->has_encrypted_entries = 0;
  3847. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  3848. if (a->archive.archive_format_name == NULL)
  3849. a->archive.archive_format_name = "ZIP";
  3850. if (zip->zip_entries == NULL) {
  3851. r = slurp_central_directory(a, entry, zip);
  3852. if (r != ARCHIVE_OK)
  3853. return r;
  3854. /* Get first entry whose local header offset is lower than
  3855. * other entries in the archive file. */
  3856. zip->entry =
  3857. (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
  3858. } else if (zip->entry != NULL) {
  3859. /* Get next entry in local header offset order. */
  3860. zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
  3861. &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
  3862. }
  3863. if (zip->entry == NULL)
  3864. return ARCHIVE_EOF;
  3865. if (zip->entry->rsrcname.s)
  3866. rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
  3867. &zip->tree_rsrc, zip->entry->rsrcname.s);
  3868. else
  3869. rsrc = NULL;
  3870. if (zip->cctx_valid)
  3871. archive_decrypto_aes_ctr_release(&zip->cctx);
  3872. if (zip->hctx_valid)
  3873. archive_hmac_sha1_cleanup(&zip->hctx);
  3874. zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
  3875. __archive_read_reset_passphrase(a);
  3876. /* File entries are sorted by the header offset, we should mostly
  3877. * use __archive_read_consume to advance a read point to avoid
  3878. * redundant data reading. */
  3879. offset = archive_filter_bytes(&a->archive, 0);
  3880. if (offset < zip->entry->local_header_offset)
  3881. __archive_read_consume(a,
  3882. zip->entry->local_header_offset - offset);
  3883. else if (offset != zip->entry->local_header_offset) {
  3884. __archive_read_seek(a, zip->entry->local_header_offset,
  3885. SEEK_SET);
  3886. }
  3887. zip->unconsumed = 0;
  3888. r = zip_read_local_file_header(a, entry, zip);
  3889. if (r != ARCHIVE_OK)
  3890. return r;
  3891. if (rsrc) {
  3892. int ret2 = zip_read_mac_metadata(a, entry, rsrc);
  3893. if (ret2 < ret)
  3894. ret = ret2;
  3895. }
  3896. return (ret);
  3897. }
  3898. /*
  3899. * We're going to seek for the next header anyway, so we don't
  3900. * need to bother doing anything here.
  3901. */
  3902. static int
  3903. archive_read_format_zip_read_data_skip_seekable(struct archive_read *a)
  3904. {
  3905. struct zip *zip;
  3906. zip = (struct zip *)(a->format->data);
  3907. zip->unconsumed = 0;
  3908. return (ARCHIVE_OK);
  3909. }
  3910. int
  3911. archive_read_support_format_zip_seekable(struct archive *_a)
  3912. {
  3913. struct archive_read *a = (struct archive_read *)_a;
  3914. struct zip *zip;
  3915. int r;
  3916. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  3917. ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
  3918. zip = calloc(1, sizeof(*zip));
  3919. if (zip == NULL) {
  3920. archive_set_error(&a->archive, ENOMEM,
  3921. "Can't allocate zip data");
  3922. return (ARCHIVE_FATAL);
  3923. }
  3924. #ifdef HAVE_COPYFILE_H
  3925. /* Set this by default on Mac OS. */
  3926. zip->process_mac_extensions = 1;
  3927. #endif
  3928. /*
  3929. * Until enough data has been read, we cannot tell about
  3930. * any encrypted entries yet.
  3931. */
  3932. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  3933. zip->crc32func = real_crc32;
  3934. r = __archive_read_register_format(a,
  3935. zip,
  3936. "zip",
  3937. archive_read_format_zip_seekable_bid,
  3938. archive_read_format_zip_options,
  3939. archive_read_format_zip_seekable_read_header,
  3940. archive_read_format_zip_read_data,
  3941. archive_read_format_zip_read_data_skip_seekable,
  3942. NULL,
  3943. archive_read_format_zip_cleanup,
  3944. archive_read_support_format_zip_capabilities_seekable,
  3945. archive_read_format_zip_has_encrypted_entries);
  3946. if (r != ARCHIVE_OK)
  3947. free(zip);
  3948. return (ARCHIVE_OK);
  3949. }
  3950. /*# vim:set noet:*/