archive_read_support_format_zip.c 117 KB

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