archive_read_support_format_7zip.c 89 KB

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