archive_read_support_format_cab.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350
  1. /*-
  2. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_ERRNO_H
  27. #include <errno.h>
  28. #endif
  29. #ifdef HAVE_LIMITS_H
  30. #include <limits.h>
  31. #endif
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #ifdef HAVE_ZLIB_H
  39. #include <cm_zlib.h>
  40. #endif
  41. #include "archive.h"
  42. #include "archive_entry.h"
  43. #include "archive_entry_locale.h"
  44. #include "archive_private.h"
  45. #include "archive_read_private.h"
  46. #include "archive_endian.h"
  47. struct lzx_dec {
  48. /* Decoding status. */
  49. int state;
  50. /*
  51. * Window to see last decoded data, from 32KBi to 2MBi.
  52. */
  53. int w_size;
  54. int w_mask;
  55. /* Window buffer, which is a loop buffer. */
  56. unsigned char *w_buff;
  57. /* The insert position to the window. */
  58. int w_pos;
  59. /* The position where we can copy decoded code from the window. */
  60. int copy_pos;
  61. /* The length how many bytes we can copy decoded code from
  62. * the window. */
  63. int copy_len;
  64. /* Translation reversal for x86 proccessor CALL byte sequence(E8).
  65. * This is used for LZX only. */
  66. uint32_t translation_size;
  67. char translation;
  68. char block_type;
  69. #define VERBATIM_BLOCK 1
  70. #define ALIGNED_OFFSET_BLOCK 2
  71. #define UNCOMPRESSED_BLOCK 3
  72. size_t block_size;
  73. size_t block_bytes_avail;
  74. /* Repeated offset. */
  75. int r0, r1, r2;
  76. unsigned char rbytes[4];
  77. int rbytes_avail;
  78. int length_header;
  79. int position_slot;
  80. int offset_bits;
  81. struct lzx_pos_tbl {
  82. int base;
  83. int footer_bits;
  84. } *pos_tbl;
  85. /*
  86. * Bit stream reader.
  87. */
  88. struct lzx_br {
  89. #define CACHE_TYPE uint64_t
  90. #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
  91. /* Cache buffer. */
  92. CACHE_TYPE cache_buffer;
  93. /* Indicates how many bits avail in cache_buffer. */
  94. int cache_avail;
  95. unsigned char odd;
  96. char have_odd;
  97. } br;
  98. /*
  99. * Huffman coding.
  100. */
  101. struct huffman {
  102. int len_size;
  103. int freq[17];
  104. unsigned char *bitlen;
  105. /*
  106. * Use a index table. It's faster than searching a huffman
  107. * coding tree, which is a binary tree. But a use of a large
  108. * index table causes L1 cache read miss many times.
  109. */
  110. #define HTBL_BITS 10
  111. int max_bits;
  112. int shift_bits;
  113. int tbl_bits;
  114. int tree_used;
  115. int tree_avail;
  116. /* Direct access table. */
  117. uint16_t *tbl;
  118. /* Binary tree table for extra bits over the direct access. */
  119. struct htree_t {
  120. uint16_t left;
  121. uint16_t right;
  122. } *tree;
  123. } at, lt, mt, pt;
  124. int loop;
  125. int error;
  126. };
  127. static const int slots[] = {
  128. 30, 32, 34, 36, 38, 42, 50, 66, 98, 162, 290
  129. };
  130. #define SLOT_BASE 15
  131. #define SLOT_MAX 21/*->25*/
  132. struct lzx_stream {
  133. const unsigned char *next_in;
  134. int64_t avail_in;
  135. int64_t total_in;
  136. unsigned char *next_out;
  137. int64_t avail_out;
  138. int64_t total_out;
  139. struct lzx_dec *ds;
  140. };
  141. /*
  142. * Cabinet file definitions.
  143. */
  144. /* CFHEADER offset */
  145. #define CFHEADER_signature 0
  146. #define CFHEADER_cbCabinet 8
  147. #define CFHEADER_coffFiles 16
  148. #define CFHEADER_versionMinor 24
  149. #define CFHEADER_versionMajor 25
  150. #define CFHEADER_cFolders 26
  151. #define CFHEADER_cFiles 28
  152. #define CFHEADER_flags 30
  153. #define CFHEADER_setID 32
  154. #define CFHEADER_iCabinet 34
  155. #define CFHEADER_cbCFHeader 36
  156. #define CFHEADER_cbCFFolder 38
  157. #define CFHEADER_cbCFData 39
  158. /* CFFOLDER offset */
  159. #define CFFOLDER_coffCabStart 0
  160. #define CFFOLDER_cCFData 4
  161. #define CFFOLDER_typeCompress 6
  162. #define CFFOLDER_abReserve 8
  163. /* CFFILE offset */
  164. #define CFFILE_cbFile 0
  165. #define CFFILE_uoffFolderStart 4
  166. #define CFFILE_iFolder 8
  167. #define CFFILE_date_time 10
  168. #define CFFILE_attribs 14
  169. /* CFDATA offset */
  170. #define CFDATA_csum 0
  171. #define CFDATA_cbData 4
  172. #define CFDATA_cbUncomp 6
  173. static const char *compression_name[] = {
  174. "NONE",
  175. "MSZIP",
  176. "Quantum",
  177. "LZX",
  178. };
  179. struct cfdata {
  180. /* Sum value of this CFDATA. */
  181. uint32_t sum;
  182. uint16_t compressed_size;
  183. uint16_t compressed_bytes_remaining;
  184. uint16_t uncompressed_size;
  185. uint16_t uncompressed_bytes_remaining;
  186. /* To know how many bytes we have decompressed. */
  187. uint16_t uncompressed_avail;
  188. /* Offset from the beginning of compressed data of this CFDATA */
  189. uint16_t read_offset;
  190. int64_t unconsumed;
  191. /* To keep memory image of this CFDATA to compute the sum. */
  192. size_t memimage_size;
  193. unsigned char *memimage;
  194. /* Result of calculation of sum. */
  195. uint32_t sum_calculated;
  196. unsigned char sum_extra[4];
  197. int sum_extra_avail;
  198. const void *sum_ptr;
  199. };
  200. struct cffolder {
  201. uint32_t cfdata_offset_in_cab;
  202. uint16_t cfdata_count;
  203. uint16_t comptype;
  204. #define COMPTYPE_NONE 0x0000
  205. #define COMPTYPE_MSZIP 0x0001
  206. #define COMPTYPE_QUANTUM 0x0002
  207. #define COMPTYPE_LZX 0x0003
  208. uint16_t compdata;
  209. const char *compname;
  210. /* At the time reading CFDATA */
  211. struct cfdata cfdata;
  212. int cfdata_index;
  213. /* Flags to mark progress of decompression. */
  214. char decompress_init;
  215. };
  216. struct cffile {
  217. uint32_t uncompressed_size;
  218. uint32_t offset;
  219. time_t mtime;
  220. uint16_t folder;
  221. #define iFoldCONTINUED_FROM_PREV 0xFFFD
  222. #define iFoldCONTINUED_TO_NEXT 0xFFFE
  223. #define iFoldCONTINUED_PREV_AND_NEXT 0xFFFF
  224. unsigned char attr;
  225. #define ATTR_RDONLY 0x01
  226. #define ATTR_NAME_IS_UTF 0x80
  227. struct archive_string pathname;
  228. };
  229. struct cfheader {
  230. /* Total bytes of all file size in a Cabinet. */
  231. uint32_t total_bytes;
  232. uint32_t files_offset;
  233. uint16_t folder_count;
  234. uint16_t file_count;
  235. uint16_t flags;
  236. #define PREV_CABINET 0x0001
  237. #define NEXT_CABINET 0x0002
  238. #define RESERVE_PRESENT 0x0004
  239. uint16_t setid;
  240. uint16_t cabinet;
  241. /* Version number. */
  242. unsigned char major;
  243. unsigned char minor;
  244. unsigned char cffolder;
  245. unsigned char cfdata;
  246. /* All folders in a cabinet. */
  247. struct cffolder *folder_array;
  248. /* All files in a cabinet. */
  249. struct cffile *file_array;
  250. int file_index;
  251. };
  252. struct cab {
  253. /* entry_bytes_remaining is the number of bytes we expect. */
  254. int64_t entry_offset;
  255. int64_t entry_bytes_remaining;
  256. int64_t entry_unconsumed;
  257. int64_t entry_compressed_bytes_read;
  258. int64_t entry_uncompressed_bytes_read;
  259. struct cffolder *entry_cffolder;
  260. struct cffile *entry_cffile;
  261. struct cfdata *entry_cfdata;
  262. /* Offset from beginning of a cabinet file. */
  263. int64_t cab_offset;
  264. struct cfheader cfheader;
  265. struct archive_wstring ws;
  266. /* Flag to mark progress that an archive was read their first header.*/
  267. char found_header;
  268. char end_of_archive;
  269. char end_of_entry;
  270. char end_of_entry_cleanup;
  271. char read_data_invoked;
  272. int64_t bytes_skipped;
  273. unsigned char *uncompressed_buffer;
  274. size_t uncompressed_buffer_size;
  275. int init_default_conversion;
  276. struct archive_string_conv *sconv;
  277. struct archive_string_conv *sconv_default;
  278. struct archive_string_conv *sconv_utf8;
  279. char format_name[64];
  280. #ifdef HAVE_ZLIB_H
  281. z_stream stream;
  282. char stream_valid;
  283. #endif
  284. struct lzx_stream xstrm;
  285. };
  286. static int archive_read_format_cab_bid(struct archive_read *, int);
  287. static int archive_read_format_cab_options(struct archive_read *,
  288. const char *, const char *);
  289. static int archive_read_format_cab_read_header(struct archive_read *,
  290. struct archive_entry *);
  291. static int archive_read_format_cab_read_data(struct archive_read *,
  292. const void **, size_t *, int64_t *);
  293. static int archive_read_format_cab_read_data_skip(struct archive_read *);
  294. static int archive_read_format_cab_cleanup(struct archive_read *);
  295. static int cab_skip_sfx(struct archive_read *);
  296. static time_t cab_dos_time(const unsigned char *);
  297. static int cab_read_data(struct archive_read *, const void **,
  298. size_t *, int64_t *);
  299. static int cab_read_header(struct archive_read *);
  300. static uint32_t cab_checksum_cfdata_4(const void *, size_t bytes, uint32_t);
  301. static uint32_t cab_checksum_cfdata(const void *, size_t bytes, uint32_t);
  302. static void cab_checksum_update(struct archive_read *, size_t);
  303. static int cab_checksum_finish(struct archive_read *);
  304. static int cab_next_cfdata(struct archive_read *);
  305. static const void *cab_read_ahead_cfdata(struct archive_read *, ssize_t *);
  306. static const void *cab_read_ahead_cfdata_none(struct archive_read *, ssize_t *);
  307. static const void *cab_read_ahead_cfdata_deflate(struct archive_read *,
  308. ssize_t *);
  309. static const void *cab_read_ahead_cfdata_lzx(struct archive_read *,
  310. ssize_t *);
  311. static int64_t cab_consume_cfdata(struct archive_read *, int64_t);
  312. static int64_t cab_minimum_consume_cfdata(struct archive_read *, int64_t);
  313. static int lzx_decode_init(struct lzx_stream *, int);
  314. static int lzx_read_blocks(struct lzx_stream *, int);
  315. static int lzx_decode_blocks(struct lzx_stream *, int);
  316. static void lzx_decode_free(struct lzx_stream *);
  317. static void lzx_translation(struct lzx_stream *, void *, size_t, uint32_t);
  318. static void lzx_cleanup_bitstream(struct lzx_stream *);
  319. static int lzx_decode(struct lzx_stream *, int);
  320. static int lzx_read_pre_tree(struct lzx_stream *);
  321. static int lzx_read_bitlen(struct lzx_stream *, struct huffman *, int);
  322. static int lzx_huffman_init(struct huffman *, size_t, int);
  323. static void lzx_huffman_free(struct huffman *);
  324. static int lzx_make_huffman_table(struct huffman *);
  325. static inline int lzx_decode_huffman(struct huffman *, unsigned);
  326. static int lzx_decode_huffman_tree(struct huffman *, unsigned, int);
  327. int
  328. archive_read_support_format_cab(struct archive *_a)
  329. {
  330. struct archive_read *a = (struct archive_read *)_a;
  331. struct cab *cab;
  332. int r;
  333. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  334. ARCHIVE_STATE_NEW, "archive_read_support_format_cab");
  335. cab = (struct cab *)calloc(1, sizeof(*cab));
  336. if (cab == NULL) {
  337. archive_set_error(&a->archive, ENOMEM,
  338. "Can't allocate CAB data");
  339. return (ARCHIVE_FATAL);
  340. }
  341. archive_string_init(&cab->ws);
  342. archive_wstring_ensure(&cab->ws, 256);
  343. r = __archive_read_register_format(a,
  344. cab,
  345. "cab",
  346. archive_read_format_cab_bid,
  347. archive_read_format_cab_options,
  348. archive_read_format_cab_read_header,
  349. archive_read_format_cab_read_data,
  350. archive_read_format_cab_read_data_skip,
  351. NULL,
  352. archive_read_format_cab_cleanup,
  353. NULL,
  354. NULL);
  355. if (r != ARCHIVE_OK)
  356. free(cab);
  357. return (ARCHIVE_OK);
  358. }
  359. static int
  360. find_cab_magic(const char *p)
  361. {
  362. switch (p[4]) {
  363. case 0:
  364. /*
  365. * Note: Self-Extraction program has 'MSCF' string in their
  366. * program. If we were finding 'MSCF' string only, we got
  367. * wrong place for Cabinet header, thus, we have to check
  368. * following four bytes which are reserved and must be set
  369. * to zero.
  370. */
  371. if (memcmp(p, "MSCF\0\0\0\0", 8) == 0)
  372. return 0;
  373. return 5;
  374. case 'F': return 1;
  375. case 'C': return 2;
  376. case 'S': return 3;
  377. case 'M': return 4;
  378. default: return 5;
  379. }
  380. }
  381. static int
  382. archive_read_format_cab_bid(struct archive_read *a, int best_bid)
  383. {
  384. const char *p;
  385. ssize_t bytes_avail, offset, window;
  386. /* If there's already a better bid than we can ever
  387. make, don't bother testing. */
  388. if (best_bid > 64)
  389. return (-1);
  390. if ((p = __archive_read_ahead(a, 8, NULL)) == NULL)
  391. return (-1);
  392. if (memcmp(p, "MSCF\0\0\0\0", 8) == 0)
  393. return (64);
  394. /*
  395. * Attempt to handle self-extracting archives
  396. * by noting a PE header and searching forward
  397. * up to 128k for a 'MSCF' marker.
  398. */
  399. if (p[0] == 'M' && p[1] == 'Z') {
  400. offset = 0;
  401. window = 4096;
  402. while (offset < (1024 * 128)) {
  403. const char *h = __archive_read_ahead(a, offset + window,
  404. &bytes_avail);
  405. if (h == NULL) {
  406. /* Remaining bytes are less than window. */
  407. window >>= 1;
  408. if (window < 128)
  409. return (0);
  410. continue;
  411. }
  412. p = h + offset;
  413. while (p + 8 < h + bytes_avail) {
  414. int next;
  415. if ((next = find_cab_magic(p)) == 0)
  416. return (64);
  417. p += next;
  418. }
  419. offset = p - h;
  420. }
  421. }
  422. return (0);
  423. }
  424. static int
  425. archive_read_format_cab_options(struct archive_read *a,
  426. const char *key, const char *val)
  427. {
  428. struct cab *cab;
  429. int ret = ARCHIVE_FAILED;
  430. cab = (struct cab *)(a->format->data);
  431. if (strcmp(key, "hdrcharset") == 0) {
  432. if (val == NULL || val[0] == 0)
  433. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  434. "cab: hdrcharset option needs a character-set name");
  435. else {
  436. cab->sconv = archive_string_conversion_from_charset(
  437. &a->archive, val, 0);
  438. if (cab->sconv != NULL)
  439. ret = ARCHIVE_OK;
  440. else
  441. ret = ARCHIVE_FATAL;
  442. }
  443. return (ret);
  444. }
  445. /* Note: The "warn" return is just to inform the options
  446. * supervisor that we didn't handle it. It will generate
  447. * a suitable error if no one used this option. */
  448. return (ARCHIVE_WARN);
  449. }
  450. static int
  451. cab_skip_sfx(struct archive_read *a)
  452. {
  453. const char *p, *q;
  454. size_t skip;
  455. ssize_t bytes, window;
  456. window = 4096;
  457. for (;;) {
  458. const char *h = __archive_read_ahead(a, window, &bytes);
  459. if (h == NULL) {
  460. /* Remaining size are less than window. */
  461. window >>= 1;
  462. if (window < 128) {
  463. archive_set_error(&a->archive,
  464. ARCHIVE_ERRNO_FILE_FORMAT,
  465. "Couldn't find out CAB header");
  466. return (ARCHIVE_FATAL);
  467. }
  468. continue;
  469. }
  470. p = h;
  471. q = p + bytes;
  472. /*
  473. * Scan ahead until we find something that looks
  474. * like the cab header.
  475. */
  476. while (p + 8 < q) {
  477. int next;
  478. if ((next = find_cab_magic(p)) == 0) {
  479. skip = p - h;
  480. __archive_read_consume(a, skip);
  481. return (ARCHIVE_OK);
  482. }
  483. p += next;
  484. }
  485. skip = p - h;
  486. __archive_read_consume(a, skip);
  487. }
  488. }
  489. static int
  490. truncated_error(struct archive_read *a)
  491. {
  492. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  493. "Truncated CAB header");
  494. return (ARCHIVE_FATAL);
  495. }
  496. static ssize_t
  497. cab_strnlen(const unsigned char *p, size_t maxlen)
  498. {
  499. size_t i;
  500. for (i = 0; i <= maxlen; i++) {
  501. if (p[i] == 0)
  502. break;
  503. }
  504. if (i > maxlen)
  505. return (-1);/* invalid */
  506. return ((ssize_t)i);
  507. }
  508. /* Read bytes as much as remaining. */
  509. static const void *
  510. cab_read_ahead_remaining(struct archive_read *a, size_t min, ssize_t *avail)
  511. {
  512. const void *p;
  513. while (min > 0) {
  514. p = __archive_read_ahead(a, min, avail);
  515. if (p != NULL)
  516. return (p);
  517. min--;
  518. }
  519. return (NULL);
  520. }
  521. /* Convert a path separator '\' -> '/' */
  522. static int
  523. cab_convert_path_separator_1(struct archive_string *fn, unsigned char attr)
  524. {
  525. size_t i;
  526. int mb;
  527. /* Easy check if we have '\' in multi-byte string. */
  528. mb = 0;
  529. for (i = 0; i < archive_strlen(fn); i++) {
  530. if (fn->s[i] == '\\') {
  531. if (mb) {
  532. /* This may be second byte of multi-byte
  533. * character. */
  534. break;
  535. }
  536. fn->s[i] = '/';
  537. mb = 0;
  538. } else if ((fn->s[i] & 0x80) && !(attr & ATTR_NAME_IS_UTF))
  539. mb = 1;
  540. else
  541. mb = 0;
  542. }
  543. if (i == archive_strlen(fn))
  544. return (0);
  545. return (-1);
  546. }
  547. /*
  548. * Replace a character '\' with '/' in wide character.
  549. */
  550. static void
  551. cab_convert_path_separator_2(struct cab *cab, struct archive_entry *entry)
  552. {
  553. const wchar_t *wp;
  554. size_t i;
  555. /* If a conversion to wide character failed, force the replacement. */
  556. if ((wp = archive_entry_pathname_w(entry)) != NULL) {
  557. archive_wstrcpy(&(cab->ws), wp);
  558. for (i = 0; i < archive_strlen(&(cab->ws)); i++) {
  559. if (cab->ws.s[i] == L'\\')
  560. cab->ws.s[i] = L'/';
  561. }
  562. archive_entry_copy_pathname_w(entry, cab->ws.s);
  563. }
  564. }
  565. /*
  566. * Read CFHEADER, CFFOLDER and CFFILE.
  567. */
  568. static int
  569. cab_read_header(struct archive_read *a)
  570. {
  571. const unsigned char *p;
  572. struct cab *cab;
  573. struct cfheader *hd;
  574. size_t bytes, used;
  575. ssize_t len;
  576. int64_t skip;
  577. int err, i;
  578. int cur_folder, prev_folder;
  579. uint32_t offset32;
  580. a->archive.archive_format = ARCHIVE_FORMAT_CAB;
  581. if (a->archive.archive_format_name == NULL)
  582. a->archive.archive_format_name = "CAB";
  583. if ((p = __archive_read_ahead(a, 42, NULL)) == NULL)
  584. return (truncated_error(a));
  585. cab = (struct cab *)(a->format->data);
  586. if (cab->found_header == 0 &&
  587. p[0] == 'M' && p[1] == 'Z') {
  588. /* This is an executable? Must be self-extracting... */
  589. err = cab_skip_sfx(a);
  590. if (err < ARCHIVE_WARN)
  591. return (err);
  592. if ((p = __archive_read_ahead(a, sizeof(*p), NULL)) == NULL)
  593. return (truncated_error(a));
  594. }
  595. cab->cab_offset = 0;
  596. /*
  597. * Read CFHEADER.
  598. */
  599. hd = &cab->cfheader;
  600. if (p[CFHEADER_signature+0] != 'M' || p[CFHEADER_signature+1] != 'S' ||
  601. p[CFHEADER_signature+2] != 'C' || p[CFHEADER_signature+3] != 'F') {
  602. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  603. "Couldn't find out CAB header");
  604. return (ARCHIVE_FATAL);
  605. }
  606. hd->total_bytes = archive_le32dec(p + CFHEADER_cbCabinet);
  607. hd->files_offset = archive_le32dec(p + CFHEADER_coffFiles);
  608. hd->minor = p[CFHEADER_versionMinor];
  609. hd->major = p[CFHEADER_versionMajor];
  610. hd->folder_count = archive_le16dec(p + CFHEADER_cFolders);
  611. if (hd->folder_count == 0)
  612. goto invalid;
  613. hd->file_count = archive_le16dec(p + CFHEADER_cFiles);
  614. if (hd->file_count == 0)
  615. goto invalid;
  616. hd->flags = archive_le16dec(p + CFHEADER_flags);
  617. hd->setid = archive_le16dec(p + CFHEADER_setID);
  618. hd->cabinet = archive_le16dec(p + CFHEADER_iCabinet);
  619. used = CFHEADER_iCabinet + 2;
  620. if (hd->flags & RESERVE_PRESENT) {
  621. uint16_t cfheader;
  622. cfheader = archive_le16dec(p + CFHEADER_cbCFHeader);
  623. if (cfheader > 60000U)
  624. goto invalid;
  625. hd->cffolder = p[CFHEADER_cbCFFolder];
  626. hd->cfdata = p[CFHEADER_cbCFData];
  627. used += 4;/* cbCFHeader, cbCFFolder and cbCFData */
  628. used += cfheader;/* abReserve */
  629. } else
  630. hd->cffolder = 0;/* Avoid compiling warning. */
  631. if (hd->flags & PREV_CABINET) {
  632. /* How many bytes are used for szCabinetPrev. */
  633. if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
  634. return (truncated_error(a));
  635. if ((len = cab_strnlen(p + used, 255)) <= 0)
  636. goto invalid;
  637. used += len + 1;
  638. /* How many bytes are used for szDiskPrev. */
  639. if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
  640. return (truncated_error(a));
  641. if ((len = cab_strnlen(p + used, 255)) <= 0)
  642. goto invalid;
  643. used += len + 1;
  644. }
  645. if (hd->flags & NEXT_CABINET) {
  646. /* How many bytes are used for szCabinetNext. */
  647. if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
  648. return (truncated_error(a));
  649. if ((len = cab_strnlen(p + used, 255)) <= 0)
  650. goto invalid;
  651. used += len + 1;
  652. /* How many bytes are used for szDiskNext. */
  653. if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
  654. return (truncated_error(a));
  655. if ((len = cab_strnlen(p + used, 255)) <= 0)
  656. goto invalid;
  657. used += len + 1;
  658. }
  659. __archive_read_consume(a, used);
  660. cab->cab_offset += used;
  661. used = 0;
  662. /*
  663. * Read CFFOLDER.
  664. */
  665. hd->folder_array = (struct cffolder *)calloc(
  666. hd->folder_count, sizeof(struct cffolder));
  667. if (hd->folder_array == NULL)
  668. goto nomem;
  669. bytes = 8;
  670. if (hd->flags & RESERVE_PRESENT)
  671. bytes += hd->cffolder;
  672. bytes *= hd->folder_count;
  673. if ((p = __archive_read_ahead(a, bytes, NULL)) == NULL)
  674. return (truncated_error(a));
  675. offset32 = 0;
  676. for (i = 0; i < hd->folder_count; i++) {
  677. struct cffolder *folder = &(hd->folder_array[i]);
  678. folder->cfdata_offset_in_cab =
  679. archive_le32dec(p + CFFOLDER_coffCabStart);
  680. folder->cfdata_count = archive_le16dec(p+CFFOLDER_cCFData);
  681. folder->comptype =
  682. archive_le16dec(p+CFFOLDER_typeCompress) & 0x0F;
  683. folder->compdata =
  684. archive_le16dec(p+CFFOLDER_typeCompress) >> 8;
  685. /* Get a compression name. */
  686. if (folder->comptype <
  687. sizeof(compression_name) / sizeof(compression_name[0]))
  688. folder->compname = compression_name[folder->comptype];
  689. else
  690. folder->compname = "UNKNOWN";
  691. p += 8;
  692. used += 8;
  693. if (hd->flags & RESERVE_PRESENT) {
  694. p += hd->cffolder;/* abReserve */
  695. used += hd->cffolder;
  696. }
  697. /*
  698. * Sanity check if each data is acceptable.
  699. */
  700. if (offset32 >= folder->cfdata_offset_in_cab)
  701. goto invalid;
  702. offset32 = folder->cfdata_offset_in_cab;
  703. /* Set a request to initialize zlib for the CFDATA of
  704. * this folder. */
  705. folder->decompress_init = 0;
  706. }
  707. __archive_read_consume(a, used);
  708. cab->cab_offset += used;
  709. /*
  710. * Read CFFILE.
  711. */
  712. /* Seek read pointer to the offset of CFFILE if needed. */
  713. skip = (int64_t)hd->files_offset - cab->cab_offset;
  714. if (skip < 0) {
  715. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  716. "Invalid offset of CFFILE %jd < %jd",
  717. (intmax_t)hd->files_offset, (intmax_t)cab->cab_offset);
  718. return (ARCHIVE_FATAL);
  719. }
  720. if (skip) {
  721. __archive_read_consume(a, skip);
  722. cab->cab_offset += skip;
  723. }
  724. /* Allocate memory for CFDATA */
  725. hd->file_array = (struct cffile *)calloc(
  726. hd->file_count, sizeof(struct cffile));
  727. if (hd->file_array == NULL)
  728. goto nomem;
  729. prev_folder = -1;
  730. for (i = 0; i < hd->file_count; i++) {
  731. struct cffile *file = &(hd->file_array[i]);
  732. ssize_t avail;
  733. if ((p = __archive_read_ahead(a, 16, NULL)) == NULL)
  734. return (truncated_error(a));
  735. file->uncompressed_size = archive_le32dec(p + CFFILE_cbFile);
  736. file->offset = archive_le32dec(p + CFFILE_uoffFolderStart);
  737. file->folder = archive_le16dec(p + CFFILE_iFolder);
  738. file->mtime = cab_dos_time(p + CFFILE_date_time);
  739. file->attr = (uint8_t)archive_le16dec(p + CFFILE_attribs);
  740. __archive_read_consume(a, 16);
  741. cab->cab_offset += 16;
  742. if ((p = cab_read_ahead_remaining(a, 256, &avail)) == NULL)
  743. return (truncated_error(a));
  744. if ((len = cab_strnlen(p, avail-1)) <= 0)
  745. goto invalid;
  746. /* Copy a pathname. */
  747. archive_string_init(&(file->pathname));
  748. archive_strncpy(&(file->pathname), p, len);
  749. __archive_read_consume(a, len + 1);
  750. cab->cab_offset += len + 1;
  751. /*
  752. * Sanity check if each data is acceptable.
  753. */
  754. if (file->uncompressed_size > 0x7FFF8000)
  755. goto invalid;/* Too large */
  756. if ((int64_t)file->offset + (int64_t)file->uncompressed_size
  757. > ARCHIVE_LITERAL_LL(0x7FFF8000))
  758. goto invalid;/* Too large */
  759. switch (file->folder) {
  760. case iFoldCONTINUED_TO_NEXT:
  761. /* This must be last file in a folder. */
  762. if (i != hd->file_count -1)
  763. goto invalid;
  764. cur_folder = hd->folder_count -1;
  765. break;
  766. case iFoldCONTINUED_PREV_AND_NEXT:
  767. /* This must be only one file in a folder. */
  768. if (hd->file_count != 1)
  769. goto invalid;
  770. /* FALL THROUGH */
  771. case iFoldCONTINUED_FROM_PREV:
  772. /* This must be first file in a folder. */
  773. if (i != 0)
  774. goto invalid;
  775. prev_folder = cur_folder = 0;
  776. offset32 = file->offset;
  777. break;
  778. default:
  779. if (file->folder >= hd->folder_count)
  780. goto invalid;
  781. cur_folder = file->folder;
  782. break;
  783. }
  784. /* Dot not back track. */
  785. if (cur_folder < prev_folder)
  786. goto invalid;
  787. if (cur_folder != prev_folder)
  788. offset32 = 0;
  789. prev_folder = cur_folder;
  790. /* Make sure there are not any blanks from last file
  791. * contents. */
  792. if (offset32 != file->offset)
  793. goto invalid;
  794. offset32 += file->uncompressed_size;
  795. /* CFDATA is available for file contents. */
  796. if (file->uncompressed_size > 0 &&
  797. hd->folder_array[cur_folder].cfdata_count == 0)
  798. goto invalid;
  799. }
  800. if (hd->cabinet != 0 || hd->flags & (PREV_CABINET | NEXT_CABINET)) {
  801. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  802. "Multivolume cabinet file is unsupported");
  803. return (ARCHIVE_WARN);
  804. }
  805. return (ARCHIVE_OK);
  806. invalid:
  807. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  808. "Invalid CAB header");
  809. return (ARCHIVE_FATAL);
  810. nomem:
  811. archive_set_error(&a->archive, ENOMEM,
  812. "Can't allocate memory for CAB data");
  813. return (ARCHIVE_FATAL);
  814. }
  815. static int
  816. archive_read_format_cab_read_header(struct archive_read *a,
  817. struct archive_entry *entry)
  818. {
  819. struct cab *cab;
  820. struct cfheader *hd;
  821. struct cffolder *prev_folder;
  822. struct cffile *file;
  823. struct archive_string_conv *sconv;
  824. int err = ARCHIVE_OK, r;
  825. cab = (struct cab *)(a->format->data);
  826. if (cab->found_header == 0) {
  827. err = cab_read_header(a);
  828. if (err < ARCHIVE_WARN)
  829. return (err);
  830. /* We've found the header. */
  831. cab->found_header = 1;
  832. }
  833. hd = &cab->cfheader;
  834. if (hd->file_index >= hd->file_count) {
  835. cab->end_of_archive = 1;
  836. return (ARCHIVE_EOF);
  837. }
  838. file = &hd->file_array[hd->file_index++];
  839. cab->end_of_entry = 0;
  840. cab->end_of_entry_cleanup = 0;
  841. cab->entry_compressed_bytes_read = 0;
  842. cab->entry_uncompressed_bytes_read = 0;
  843. cab->entry_unconsumed = 0;
  844. cab->entry_cffile = file;
  845. /*
  846. * Choose a proper folder.
  847. */
  848. prev_folder = cab->entry_cffolder;
  849. switch (file->folder) {
  850. case iFoldCONTINUED_FROM_PREV:
  851. case iFoldCONTINUED_PREV_AND_NEXT:
  852. cab->entry_cffolder = &hd->folder_array[0];
  853. break;
  854. case iFoldCONTINUED_TO_NEXT:
  855. cab->entry_cffolder = &hd->folder_array[hd->folder_count-1];
  856. break;
  857. default:
  858. cab->entry_cffolder = &hd->folder_array[file->folder];
  859. break;
  860. }
  861. /* If a cffolder of this file is changed, reset a cfdata to read
  862. * file contents from next cfdata. */
  863. if (prev_folder != cab->entry_cffolder)
  864. cab->entry_cfdata = NULL;
  865. /* If a pathname is UTF-8, prepare a string conversion object
  866. * for UTF-8 and use it. */
  867. if (file->attr & ATTR_NAME_IS_UTF) {
  868. if (cab->sconv_utf8 == NULL) {
  869. cab->sconv_utf8 =
  870. archive_string_conversion_from_charset(
  871. &(a->archive), "UTF-8", 1);
  872. if (cab->sconv_utf8 == NULL)
  873. return (ARCHIVE_FATAL);
  874. }
  875. sconv = cab->sconv_utf8;
  876. } else if (cab->sconv != NULL) {
  877. /* Choose the conversion specified by the option. */
  878. sconv = cab->sconv;
  879. } else {
  880. /* Choose the default conversion. */
  881. if (!cab->init_default_conversion) {
  882. cab->sconv_default =
  883. archive_string_default_conversion_for_read(
  884. &(a->archive));
  885. cab->init_default_conversion = 1;
  886. }
  887. sconv = cab->sconv_default;
  888. }
  889. /*
  890. * Set a default value and common data
  891. */
  892. r = cab_convert_path_separator_1(&(file->pathname), file->attr);
  893. if (archive_entry_copy_pathname_l(entry, file->pathname.s,
  894. archive_strlen(&(file->pathname)), sconv) != 0) {
  895. if (errno == ENOMEM) {
  896. archive_set_error(&a->archive, ENOMEM,
  897. "Can't allocate memory for Pathname");
  898. return (ARCHIVE_FATAL);
  899. }
  900. archive_set_error(&a->archive,
  901. ARCHIVE_ERRNO_FILE_FORMAT,
  902. "Pathname cannot be converted "
  903. "from %s to current locale.",
  904. archive_string_conversion_charset_name(sconv));
  905. err = ARCHIVE_WARN;
  906. }
  907. if (r < 0) {
  908. /* Convert a path separator '\' -> '/' */
  909. cab_convert_path_separator_2(cab, entry);
  910. }
  911. archive_entry_set_size(entry, file->uncompressed_size);
  912. if (file->attr & ATTR_RDONLY)
  913. archive_entry_set_mode(entry, AE_IFREG | 0555);
  914. else
  915. archive_entry_set_mode(entry, AE_IFREG | 0666);
  916. archive_entry_set_mtime(entry, file->mtime, 0);
  917. cab->entry_bytes_remaining = file->uncompressed_size;
  918. cab->entry_offset = 0;
  919. /* We don't need compress data. */
  920. if (file->uncompressed_size == 0)
  921. cab->end_of_entry_cleanup = cab->end_of_entry = 1;
  922. /* Set up a more descriptive format name. */
  923. sprintf(cab->format_name, "CAB %d.%d (%s)",
  924. hd->major, hd->minor, cab->entry_cffolder->compname);
  925. a->archive.archive_format_name = cab->format_name;
  926. return (err);
  927. }
  928. static int
  929. archive_read_format_cab_read_data(struct archive_read *a,
  930. const void **buff, size_t *size, int64_t *offset)
  931. {
  932. struct cab *cab = (struct cab *)(a->format->data);
  933. int r;
  934. switch (cab->entry_cffile->folder) {
  935. case iFoldCONTINUED_FROM_PREV:
  936. case iFoldCONTINUED_TO_NEXT:
  937. case iFoldCONTINUED_PREV_AND_NEXT:
  938. *buff = NULL;
  939. *size = 0;
  940. *offset = 0;
  941. archive_clear_error(&a->archive);
  942. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  943. "Cannot restore this file split in multivolume.");
  944. return (ARCHIVE_FAILED);
  945. default:
  946. break;
  947. }
  948. if (cab->read_data_invoked == 0) {
  949. if (cab->bytes_skipped) {
  950. if (cab->entry_cfdata == NULL) {
  951. r = cab_next_cfdata(a);
  952. if (r < 0)
  953. return (r);
  954. }
  955. if (cab_consume_cfdata(a, cab->bytes_skipped) < 0)
  956. return (ARCHIVE_FATAL);
  957. cab->bytes_skipped = 0;
  958. }
  959. cab->read_data_invoked = 1;
  960. }
  961. if (cab->entry_unconsumed) {
  962. /* Consume as much as the compressor actually used. */
  963. r = (int)cab_consume_cfdata(a, cab->entry_unconsumed);
  964. cab->entry_unconsumed = 0;
  965. if (r < 0)
  966. return (r);
  967. }
  968. if (cab->end_of_archive || cab->end_of_entry) {
  969. if (!cab->end_of_entry_cleanup) {
  970. /* End-of-entry cleanup done. */
  971. cab->end_of_entry_cleanup = 1;
  972. }
  973. *offset = cab->entry_offset;
  974. *size = 0;
  975. *buff = NULL;
  976. return (ARCHIVE_EOF);
  977. }
  978. return (cab_read_data(a, buff, size, offset));
  979. }
  980. static uint32_t
  981. cab_checksum_cfdata_4(const void *p, size_t bytes, uint32_t seed)
  982. {
  983. const unsigned char *b;
  984. unsigned u32num;
  985. uint32_t sum;
  986. u32num = (unsigned)bytes / 4;
  987. sum = seed;
  988. b = p;
  989. for (;u32num > 0; --u32num) {
  990. sum ^= archive_le32dec(b);
  991. b += 4;
  992. }
  993. return (sum);
  994. }
  995. static uint32_t
  996. cab_checksum_cfdata(const void *p, size_t bytes, uint32_t seed)
  997. {
  998. const unsigned char *b;
  999. uint32_t sum;
  1000. uint32_t t;
  1001. sum = cab_checksum_cfdata_4(p, bytes, seed);
  1002. b = p;
  1003. b += bytes & ~3;
  1004. t = 0;
  1005. switch (bytes & 3) {
  1006. case 3:
  1007. t |= ((uint32_t)(*b++)) << 16;
  1008. /* FALL THROUGH */
  1009. case 2:
  1010. t |= ((uint32_t)(*b++)) << 8;
  1011. /* FALL THROUGH */
  1012. case 1:
  1013. t |= *b;
  1014. /* FALL THROUGH */
  1015. default:
  1016. break;
  1017. }
  1018. sum ^= t;
  1019. return (sum);
  1020. }
  1021. static void
  1022. cab_checksum_update(struct archive_read *a, size_t bytes)
  1023. {
  1024. struct cab *cab = (struct cab *)(a->format->data);
  1025. struct cfdata *cfdata = cab->entry_cfdata;
  1026. const unsigned char *p;
  1027. size_t sumbytes;
  1028. if (cfdata->sum == 0 || cfdata->sum_ptr == NULL)
  1029. return;
  1030. /*
  1031. * Calculate the sum of this CFDATA.
  1032. * Make sure CFDATA must be calculated in four bytes.
  1033. */
  1034. p = cfdata->sum_ptr;
  1035. sumbytes = bytes;
  1036. if (cfdata->sum_extra_avail) {
  1037. while (cfdata->sum_extra_avail < 4 && sumbytes > 0) {
  1038. cfdata->sum_extra[
  1039. cfdata->sum_extra_avail++] = *p++;
  1040. sumbytes--;
  1041. }
  1042. if (cfdata->sum_extra_avail == 4) {
  1043. cfdata->sum_calculated = cab_checksum_cfdata_4(
  1044. cfdata->sum_extra, 4, cfdata->sum_calculated);
  1045. cfdata->sum_extra_avail = 0;
  1046. }
  1047. }
  1048. if (sumbytes) {
  1049. int odd = sumbytes & 3;
  1050. if (sumbytes - odd > 0)
  1051. cfdata->sum_calculated = cab_checksum_cfdata_4(
  1052. p, sumbytes - odd, cfdata->sum_calculated);
  1053. if (odd)
  1054. memcpy(cfdata->sum_extra, p + sumbytes - odd, odd);
  1055. cfdata->sum_extra_avail = odd;
  1056. }
  1057. cfdata->sum_ptr = NULL;
  1058. }
  1059. static int
  1060. cab_checksum_finish(struct archive_read *a)
  1061. {
  1062. struct cab *cab = (struct cab *)(a->format->data);
  1063. struct cfdata *cfdata = cab->entry_cfdata;
  1064. int l;
  1065. /* Do not need to compute a sum. */
  1066. if (cfdata->sum == 0)
  1067. return (ARCHIVE_OK);
  1068. /*
  1069. * Calculate the sum of remaining CFDATA.
  1070. */
  1071. if (cfdata->sum_extra_avail) {
  1072. cfdata->sum_calculated =
  1073. cab_checksum_cfdata(cfdata->sum_extra,
  1074. cfdata->sum_extra_avail, cfdata->sum_calculated);
  1075. cfdata->sum_extra_avail = 0;
  1076. }
  1077. l = 4;
  1078. if (cab->cfheader.flags & RESERVE_PRESENT)
  1079. l += cab->cfheader.cfdata;
  1080. cfdata->sum_calculated = cab_checksum_cfdata(
  1081. cfdata->memimage + CFDATA_cbData, l, cfdata->sum_calculated);
  1082. if (cfdata->sum_calculated != cfdata->sum) {
  1083. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1084. "Checksum error CFDATA[%d] %x:%x in %d bytes",
  1085. cab->entry_cffolder->cfdata_index -1,
  1086. cfdata->sum, cfdata->sum_calculated,
  1087. cfdata->compressed_size);
  1088. return (ARCHIVE_FAILED);
  1089. }
  1090. return (ARCHIVE_OK);
  1091. }
  1092. /*
  1093. * Read CFDATA if needed.
  1094. */
  1095. static int
  1096. cab_next_cfdata(struct archive_read *a)
  1097. {
  1098. struct cab *cab = (struct cab *)(a->format->data);
  1099. struct cfdata *cfdata = cab->entry_cfdata;
  1100. /* There are remaining bytes in current CFDATA, use it first. */
  1101. if (cfdata != NULL && cfdata->uncompressed_bytes_remaining > 0)
  1102. return (ARCHIVE_OK);
  1103. if (cfdata == NULL) {
  1104. int64_t skip;
  1105. cab->entry_cffolder->cfdata_index = 0;
  1106. /* Seek read pointer to the offset of CFDATA if needed. */
  1107. skip = cab->entry_cffolder->cfdata_offset_in_cab
  1108. - cab->cab_offset;
  1109. if (skip < 0) {
  1110. int folder_index;
  1111. switch (cab->entry_cffile->folder) {
  1112. case iFoldCONTINUED_FROM_PREV:
  1113. case iFoldCONTINUED_PREV_AND_NEXT:
  1114. folder_index = 0;
  1115. break;
  1116. case iFoldCONTINUED_TO_NEXT:
  1117. folder_index = cab->cfheader.folder_count-1;
  1118. break;
  1119. default:
  1120. folder_index = cab->entry_cffile->folder;
  1121. break;
  1122. }
  1123. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1124. "Invalid offset of CFDATA in folder(%d) %jd < %jd",
  1125. folder_index,
  1126. (intmax_t)cab->entry_cffolder->cfdata_offset_in_cab,
  1127. (intmax_t)cab->cab_offset);
  1128. return (ARCHIVE_FATAL);
  1129. }
  1130. if (skip > 0) {
  1131. if (__archive_read_consume(a, skip) < 0)
  1132. return (ARCHIVE_FATAL);
  1133. cab->cab_offset =
  1134. cab->entry_cffolder->cfdata_offset_in_cab;
  1135. }
  1136. }
  1137. /*
  1138. * Read a CFDATA.
  1139. */
  1140. if (cab->entry_cffolder->cfdata_index <
  1141. cab->entry_cffolder->cfdata_count) {
  1142. const unsigned char *p;
  1143. int l;
  1144. cfdata = &(cab->entry_cffolder->cfdata);
  1145. cab->entry_cffolder->cfdata_index++;
  1146. cab->entry_cfdata = cfdata;
  1147. cfdata->sum_calculated = 0;
  1148. cfdata->sum_extra_avail = 0;
  1149. cfdata->sum_ptr = NULL;
  1150. l = 8;
  1151. if (cab->cfheader.flags & RESERVE_PRESENT)
  1152. l += cab->cfheader.cfdata;
  1153. if ((p = __archive_read_ahead(a, l, NULL)) == NULL)
  1154. return (truncated_error(a));
  1155. cfdata->sum = archive_le32dec(p + CFDATA_csum);
  1156. cfdata->compressed_size = archive_le16dec(p + CFDATA_cbData);
  1157. cfdata->compressed_bytes_remaining = cfdata->compressed_size;
  1158. cfdata->uncompressed_size =
  1159. archive_le16dec(p + CFDATA_cbUncomp);
  1160. cfdata->uncompressed_bytes_remaining =
  1161. cfdata->uncompressed_size;
  1162. cfdata->uncompressed_avail = 0;
  1163. cfdata->read_offset = 0;
  1164. cfdata->unconsumed = 0;
  1165. /*
  1166. * Sanity check if data size is acceptable.
  1167. */
  1168. if (cfdata->compressed_size == 0 ||
  1169. cfdata->compressed_size > (0x8000+6144))
  1170. goto invalid;
  1171. if (cfdata->uncompressed_size > 0x8000)
  1172. goto invalid;
  1173. if (cfdata->uncompressed_size == 0) {
  1174. switch (cab->entry_cffile->folder) {
  1175. case iFoldCONTINUED_PREV_AND_NEXT:
  1176. case iFoldCONTINUED_TO_NEXT:
  1177. break;
  1178. case iFoldCONTINUED_FROM_PREV:
  1179. default:
  1180. goto invalid;
  1181. }
  1182. }
  1183. /* If CFDATA is not last in a folder, an uncompressed
  1184. * size must be 0x8000(32KBi) */
  1185. if ((cab->entry_cffolder->cfdata_index <
  1186. cab->entry_cffolder->cfdata_count) &&
  1187. cfdata->uncompressed_size != 0x8000)
  1188. goto invalid;
  1189. /* A compressed data size and an uncompressed data size must
  1190. * be the same in no compression mode. */
  1191. if (cab->entry_cffolder->comptype == COMPTYPE_NONE &&
  1192. cfdata->compressed_size != cfdata->uncompressed_size)
  1193. goto invalid;
  1194. /*
  1195. * Save CFDATA image for sum check.
  1196. */
  1197. if (cfdata->memimage_size < (size_t)l) {
  1198. free(cfdata->memimage);
  1199. cfdata->memimage = malloc(l);
  1200. if (cfdata->memimage == NULL) {
  1201. archive_set_error(&a->archive, ENOMEM,
  1202. "Can't allocate memory for CAB data");
  1203. return (ARCHIVE_FATAL);
  1204. }
  1205. cfdata->memimage_size = l;
  1206. }
  1207. memcpy(cfdata->memimage, p, l);
  1208. /* Consume bytes as much as we used. */
  1209. __archive_read_consume(a, l);
  1210. cab->cab_offset += l;
  1211. } else if (cab->entry_cffolder->cfdata_count > 0) {
  1212. /* Run out of all CFDATA in a folder. */
  1213. cfdata->compressed_size = 0;
  1214. cfdata->uncompressed_size = 0;
  1215. cfdata->compressed_bytes_remaining = 0;
  1216. cfdata->uncompressed_bytes_remaining = 0;
  1217. } else {
  1218. /* Current folder does not have any CFDATA. */
  1219. cfdata = &(cab->entry_cffolder->cfdata);
  1220. cab->entry_cfdata = cfdata;
  1221. memset(cfdata, 0, sizeof(*cfdata));
  1222. }
  1223. return (ARCHIVE_OK);
  1224. invalid:
  1225. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1226. "Invalid CFDATA");
  1227. return (ARCHIVE_FATAL);
  1228. }
  1229. /*
  1230. * Read ahead CFDATA.
  1231. */
  1232. static const void *
  1233. cab_read_ahead_cfdata(struct archive_read *a, ssize_t *avail)
  1234. {
  1235. struct cab *cab = (struct cab *)(a->format->data);
  1236. int err;
  1237. err = cab_next_cfdata(a);
  1238. if (err < ARCHIVE_OK) {
  1239. *avail = err;
  1240. return (NULL);
  1241. }
  1242. switch (cab->entry_cffolder->comptype) {
  1243. case COMPTYPE_NONE:
  1244. return (cab_read_ahead_cfdata_none(a, avail));
  1245. case COMPTYPE_MSZIP:
  1246. return (cab_read_ahead_cfdata_deflate(a, avail));
  1247. case COMPTYPE_LZX:
  1248. return (cab_read_ahead_cfdata_lzx(a, avail));
  1249. default: /* Unsupported compression. */
  1250. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1251. "Unsupported CAB compression : %s",
  1252. cab->entry_cffolder->compname);
  1253. *avail = ARCHIVE_FAILED;
  1254. return (NULL);
  1255. }
  1256. }
  1257. /*
  1258. * Read ahead CFDATA as uncompressed data.
  1259. */
  1260. static const void *
  1261. cab_read_ahead_cfdata_none(struct archive_read *a, ssize_t *avail)
  1262. {
  1263. struct cab *cab = (struct cab *)(a->format->data);
  1264. struct cfdata *cfdata;
  1265. const void *d;
  1266. cfdata = cab->entry_cfdata;
  1267. /*
  1268. * Note: '1' here is a performance optimization.
  1269. * Recall that the decompression layer returns a count of
  1270. * available bytes; asking for more than that forces the
  1271. * decompressor to combine reads by copying data.
  1272. */
  1273. d = __archive_read_ahead(a, 1, avail);
  1274. if (*avail <= 0) {
  1275. *avail = truncated_error(a);
  1276. return (NULL);
  1277. }
  1278. if (*avail > cfdata->uncompressed_bytes_remaining)
  1279. *avail = cfdata->uncompressed_bytes_remaining;
  1280. cfdata->uncompressed_avail = cfdata->uncompressed_size;
  1281. cfdata->unconsumed = *avail;
  1282. cfdata->sum_ptr = d;
  1283. return (d);
  1284. }
  1285. /*
  1286. * Read ahead CFDATA as deflate data.
  1287. */
  1288. #ifdef HAVE_ZLIB_H
  1289. static const void *
  1290. cab_read_ahead_cfdata_deflate(struct archive_read *a, ssize_t *avail)
  1291. {
  1292. struct cab *cab = (struct cab *)(a->format->data);
  1293. struct cfdata *cfdata;
  1294. const void *d;
  1295. int r, mszip;
  1296. uint16_t uavail;
  1297. char eod = 0;
  1298. cfdata = cab->entry_cfdata;
  1299. /* If the buffer hasn't been allocated, allocate it now. */
  1300. if (cab->uncompressed_buffer == NULL) {
  1301. cab->uncompressed_buffer_size = 0x8000;
  1302. cab->uncompressed_buffer
  1303. = (unsigned char *)malloc(cab->uncompressed_buffer_size);
  1304. if (cab->uncompressed_buffer == NULL) {
  1305. archive_set_error(&a->archive, ENOMEM,
  1306. "No memory for CAB reader");
  1307. *avail = ARCHIVE_FATAL;
  1308. return (NULL);
  1309. }
  1310. }
  1311. uavail = cfdata->uncompressed_avail;
  1312. if (uavail == cfdata->uncompressed_size) {
  1313. d = cab->uncompressed_buffer + cfdata->read_offset;
  1314. *avail = uavail - cfdata->read_offset;
  1315. return (d);
  1316. }
  1317. if (!cab->entry_cffolder->decompress_init) {
  1318. cab->stream.next_in = NULL;
  1319. cab->stream.avail_in = 0;
  1320. cab->stream.total_in = 0;
  1321. cab->stream.next_out = NULL;
  1322. cab->stream.avail_out = 0;
  1323. cab->stream.total_out = 0;
  1324. if (cab->stream_valid)
  1325. r = inflateReset(&cab->stream);
  1326. else
  1327. r = inflateInit2(&cab->stream,
  1328. -15 /* Don't check for zlib header */);
  1329. if (r != Z_OK) {
  1330. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1331. "Can't initialize deflate decompression.");
  1332. *avail = ARCHIVE_FATAL;
  1333. return (NULL);
  1334. }
  1335. /* Stream structure has been set up. */
  1336. cab->stream_valid = 1;
  1337. /* We've initialized decompression for this stream. */
  1338. cab->entry_cffolder->decompress_init = 1;
  1339. }
  1340. if (cfdata->compressed_bytes_remaining == cfdata->compressed_size)
  1341. mszip = 2;
  1342. else
  1343. mszip = 0;
  1344. eod = 0;
  1345. cab->stream.total_out = uavail;
  1346. /*
  1347. * We always uncompress all data in current CFDATA.
  1348. */
  1349. while (!eod && cab->stream.total_out < cfdata->uncompressed_size) {
  1350. ssize_t bytes_avail;
  1351. cab->stream.next_out =
  1352. cab->uncompressed_buffer + cab->stream.total_out;
  1353. cab->stream.avail_out =
  1354. cfdata->uncompressed_size - cab->stream.total_out;
  1355. d = __archive_read_ahead(a, 1, &bytes_avail);
  1356. if (bytes_avail <= 0) {
  1357. *avail = truncated_error(a);
  1358. return (NULL);
  1359. }
  1360. if (bytes_avail > cfdata->compressed_bytes_remaining)
  1361. bytes_avail = cfdata->compressed_bytes_remaining;
  1362. /*
  1363. * A bug in zlib.h: stream.next_in should be marked 'const'
  1364. * but isn't (the library never alters data through the
  1365. * next_in pointer, only reads it). The result: this ugly
  1366. * cast to remove 'const'.
  1367. */
  1368. cab->stream.next_in = (Bytef *)(uintptr_t)d;
  1369. cab->stream.avail_in = (uInt)bytes_avail;
  1370. cab->stream.total_in = 0;
  1371. /* Cut out a tow-byte MSZIP signature(0x43, 0x4b). */
  1372. if (mszip > 0) {
  1373. if (bytes_avail <= mszip) {
  1374. if (mszip == 2) {
  1375. if (cab->stream.next_in[0] != 0x43)
  1376. goto nomszip;
  1377. if (bytes_avail > 1 &&
  1378. cab->stream.next_in[1] != 0x4b)
  1379. goto nomszip;
  1380. } else if (cab->stream.next_in[0] != 0x4b)
  1381. goto nomszip;
  1382. cfdata->unconsumed = bytes_avail;
  1383. cfdata->sum_ptr = d;
  1384. if (cab_minimum_consume_cfdata(
  1385. a, cfdata->unconsumed) < 0) {
  1386. *avail = ARCHIVE_FATAL;
  1387. return (NULL);
  1388. }
  1389. mszip -= (int)bytes_avail;
  1390. continue;
  1391. }
  1392. if (mszip == 1 && cab->stream.next_in[0] != 0x4b)
  1393. goto nomszip;
  1394. else if (cab->stream.next_in[0] != 0x43 ||
  1395. cab->stream.next_in[1] != 0x4b)
  1396. goto nomszip;
  1397. cab->stream.next_in += mszip;
  1398. cab->stream.avail_in -= mszip;
  1399. cab->stream.total_in += mszip;
  1400. mszip = 0;
  1401. }
  1402. r = inflate(&cab->stream, 0);
  1403. switch (r) {
  1404. case Z_OK:
  1405. break;
  1406. case Z_STREAM_END:
  1407. eod = 1;
  1408. break;
  1409. default:
  1410. goto zlibfailed;
  1411. }
  1412. cfdata->unconsumed = cab->stream.total_in;
  1413. cfdata->sum_ptr = d;
  1414. if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
  1415. *avail = ARCHIVE_FATAL;
  1416. return (NULL);
  1417. }
  1418. }
  1419. uavail = (uint16_t)cab->stream.total_out;
  1420. if (uavail < cfdata->uncompressed_size) {
  1421. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1422. "Invalid uncompressed size (%d < %d)",
  1423. uavail, cfdata->uncompressed_size);
  1424. *avail = ARCHIVE_FATAL;
  1425. return (NULL);
  1426. }
  1427. /*
  1428. * Note: I suspect there is a bug in makecab.exe because, in rare
  1429. * case, compressed bytes are still remaining regardless we have
  1430. * gotten all uncompressed bytes, which size is recoded in CFDATA,
  1431. * as much as we need, and we have to use the garbage so as to
  1432. * correctly compute the sum of CFDATA accordingly.
  1433. */
  1434. if (cfdata->compressed_bytes_remaining > 0) {
  1435. ssize_t bytes_avail;
  1436. d = __archive_read_ahead(a, cfdata->compressed_bytes_remaining,
  1437. &bytes_avail);
  1438. if (bytes_avail <= 0) {
  1439. *avail = truncated_error(a);
  1440. return (NULL);
  1441. }
  1442. cfdata->unconsumed = cfdata->compressed_bytes_remaining;
  1443. cfdata->sum_ptr = d;
  1444. if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
  1445. *avail = ARCHIVE_FATAL;
  1446. return (NULL);
  1447. }
  1448. }
  1449. /*
  1450. * Set dictionary data for decompressing of next CFDATA, which
  1451. * in the same folder. This is why we always do decompress CFDATA
  1452. * even if beginning CFDATA or some of CFDATA are not used in
  1453. * skipping file data.
  1454. */
  1455. if (cab->entry_cffolder->cfdata_index <
  1456. cab->entry_cffolder->cfdata_count) {
  1457. r = inflateReset(&cab->stream);
  1458. if (r != Z_OK)
  1459. goto zlibfailed;
  1460. r = inflateSetDictionary(&cab->stream,
  1461. cab->uncompressed_buffer, cfdata->uncompressed_size);
  1462. if (r != Z_OK)
  1463. goto zlibfailed;
  1464. }
  1465. d = cab->uncompressed_buffer + cfdata->read_offset;
  1466. *avail = uavail - cfdata->read_offset;
  1467. cfdata->uncompressed_avail = uavail;
  1468. return (d);
  1469. zlibfailed:
  1470. switch (r) {
  1471. case Z_MEM_ERROR:
  1472. archive_set_error(&a->archive, ENOMEM,
  1473. "Out of memory for deflate decompression");
  1474. break;
  1475. default:
  1476. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1477. "Deflate decompression failed (%d)", r);
  1478. break;
  1479. }
  1480. *avail = ARCHIVE_FATAL;
  1481. return (NULL);
  1482. nomszip:
  1483. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1484. "CFDATA incorrect(no MSZIP signature)");
  1485. *avail = ARCHIVE_FATAL;
  1486. return (NULL);
  1487. }
  1488. #else /* HAVE_ZLIB_H */
  1489. static const void *
  1490. cab_read_ahead_cfdata_deflate(struct archive_read *a, ssize_t *avail)
  1491. {
  1492. *avail = ARCHIVE_FATAL;
  1493. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1494. "libarchive compiled without deflate support (no libz)");
  1495. return (NULL);
  1496. }
  1497. #endif /* HAVE_ZLIB_H */
  1498. static const void *
  1499. cab_read_ahead_cfdata_lzx(struct archive_read *a, ssize_t *avail)
  1500. {
  1501. struct cab *cab = (struct cab *)(a->format->data);
  1502. struct cfdata *cfdata;
  1503. const void *d;
  1504. int r;
  1505. uint16_t uavail;
  1506. cfdata = cab->entry_cfdata;
  1507. /* If the buffer hasn't been allocated, allocate it now. */
  1508. if (cab->uncompressed_buffer == NULL) {
  1509. cab->uncompressed_buffer_size = 0x8000;
  1510. cab->uncompressed_buffer
  1511. = (unsigned char *)malloc(cab->uncompressed_buffer_size);
  1512. if (cab->uncompressed_buffer == NULL) {
  1513. archive_set_error(&a->archive, ENOMEM,
  1514. "No memory for CAB reader");
  1515. *avail = ARCHIVE_FATAL;
  1516. return (NULL);
  1517. }
  1518. }
  1519. uavail = cfdata->uncompressed_avail;
  1520. if (uavail == cfdata->uncompressed_size) {
  1521. d = cab->uncompressed_buffer + cfdata->read_offset;
  1522. *avail = uavail - cfdata->read_offset;
  1523. return (d);
  1524. }
  1525. if (!cab->entry_cffolder->decompress_init) {
  1526. r = lzx_decode_init(&cab->xstrm,
  1527. cab->entry_cffolder->compdata);
  1528. if (r != ARCHIVE_OK) {
  1529. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1530. "Can't initialize LZX decompression.");
  1531. *avail = ARCHIVE_FATAL;
  1532. return (NULL);
  1533. }
  1534. /* We've initialized decompression for this stream. */
  1535. cab->entry_cffolder->decompress_init = 1;
  1536. }
  1537. /* Clean up remaining bits of previous CFDATA. */
  1538. lzx_cleanup_bitstream(&cab->xstrm);
  1539. cab->xstrm.total_out = uavail;
  1540. while (cab->xstrm.total_out < cfdata->uncompressed_size) {
  1541. ssize_t bytes_avail;
  1542. cab->xstrm.next_out =
  1543. cab->uncompressed_buffer + cab->xstrm.total_out;
  1544. cab->xstrm.avail_out =
  1545. cfdata->uncompressed_size - cab->xstrm.total_out;
  1546. d = __archive_read_ahead(a, 1, &bytes_avail);
  1547. if (bytes_avail <= 0) {
  1548. archive_set_error(&a->archive,
  1549. ARCHIVE_ERRNO_FILE_FORMAT,
  1550. "Truncated CAB file data");
  1551. *avail = ARCHIVE_FATAL;
  1552. return (NULL);
  1553. }
  1554. if (bytes_avail > cfdata->compressed_bytes_remaining)
  1555. bytes_avail = cfdata->compressed_bytes_remaining;
  1556. cab->xstrm.next_in = d;
  1557. cab->xstrm.avail_in = bytes_avail;
  1558. cab->xstrm.total_in = 0;
  1559. r = lzx_decode(&cab->xstrm,
  1560. cfdata->compressed_bytes_remaining == bytes_avail);
  1561. switch (r) {
  1562. case ARCHIVE_OK:
  1563. case ARCHIVE_EOF:
  1564. break;
  1565. default:
  1566. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1567. "LZX decompression failed (%d)", r);
  1568. *avail = ARCHIVE_FATAL;
  1569. return (NULL);
  1570. }
  1571. cfdata->unconsumed = cab->xstrm.total_in;
  1572. cfdata->sum_ptr = d;
  1573. if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
  1574. *avail = ARCHIVE_FATAL;
  1575. return (NULL);
  1576. }
  1577. }
  1578. uavail = (uint16_t)cab->xstrm.total_out;
  1579. /*
  1580. * Make sure a read pointer advances to next CFDATA.
  1581. */
  1582. if (cfdata->compressed_bytes_remaining > 0) {
  1583. ssize_t bytes_avail;
  1584. d = __archive_read_ahead(a, cfdata->compressed_bytes_remaining,
  1585. &bytes_avail);
  1586. if (bytes_avail <= 0) {
  1587. *avail = truncated_error(a);
  1588. return (NULL);
  1589. }
  1590. cfdata->unconsumed = cfdata->compressed_bytes_remaining;
  1591. cfdata->sum_ptr = d;
  1592. if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
  1593. *avail = ARCHIVE_FATAL;
  1594. return (NULL);
  1595. }
  1596. }
  1597. /*
  1598. * Translation reversal of x86 proccessor CALL byte sequence(E8).
  1599. */
  1600. lzx_translation(&cab->xstrm, cab->uncompressed_buffer,
  1601. cfdata->uncompressed_size,
  1602. (cab->entry_cffolder->cfdata_index-1) * 0x8000);
  1603. d = cab->uncompressed_buffer + cfdata->read_offset;
  1604. *avail = uavail - cfdata->read_offset;
  1605. cfdata->uncompressed_avail = uavail;
  1606. return (d);
  1607. }
  1608. /*
  1609. * Consume CFDATA.
  1610. * We always decompress CFDATA to consume CFDATA as much as we need
  1611. * in uncompressed bytes because all CFDATA in a folder are related
  1612. * so we do not skip any CFDATA without decompressing.
  1613. * Note: If the folder of a CFFILE is iFoldCONTINUED_PREV_AND_NEXT or
  1614. * iFoldCONTINUED_FROM_PREV, we won't decompress because a CFDATA for
  1615. * the CFFILE is remaining bytes of previous Multivolume CAB file.
  1616. */
  1617. static int64_t
  1618. cab_consume_cfdata(struct archive_read *a, int64_t consumed_bytes)
  1619. {
  1620. struct cab *cab = (struct cab *)(a->format->data);
  1621. struct cfdata *cfdata;
  1622. int64_t cbytes, rbytes;
  1623. int err;
  1624. rbytes = cab_minimum_consume_cfdata(a, consumed_bytes);
  1625. if (rbytes < 0)
  1626. return (ARCHIVE_FATAL);
  1627. cfdata = cab->entry_cfdata;
  1628. while (rbytes > 0) {
  1629. ssize_t avail;
  1630. if (cfdata->compressed_size == 0) {
  1631. archive_set_error(&a->archive,
  1632. ARCHIVE_ERRNO_FILE_FORMAT,
  1633. "Invalid CFDATA");
  1634. return (ARCHIVE_FATAL);
  1635. }
  1636. cbytes = cfdata->uncompressed_bytes_remaining;
  1637. if (cbytes > rbytes)
  1638. cbytes = rbytes;
  1639. rbytes -= cbytes;
  1640. if (cfdata->uncompressed_avail == 0 &&
  1641. (cab->entry_cffile->folder == iFoldCONTINUED_PREV_AND_NEXT ||
  1642. cab->entry_cffile->folder == iFoldCONTINUED_FROM_PREV)) {
  1643. /* We have not read any data yet. */
  1644. if (cbytes == cfdata->uncompressed_bytes_remaining) {
  1645. /* Skip whole current CFDATA. */
  1646. __archive_read_consume(a,
  1647. cfdata->compressed_size);
  1648. cab->cab_offset += cfdata->compressed_size;
  1649. cfdata->compressed_bytes_remaining = 0;
  1650. cfdata->uncompressed_bytes_remaining = 0;
  1651. err = cab_next_cfdata(a);
  1652. if (err < 0)
  1653. return (err);
  1654. cfdata = cab->entry_cfdata;
  1655. if (cfdata->uncompressed_size == 0) {
  1656. switch (cab->entry_cffile->folder) {
  1657. case iFoldCONTINUED_PREV_AND_NEXT:
  1658. case iFoldCONTINUED_TO_NEXT:
  1659. case iFoldCONTINUED_FROM_PREV:
  1660. rbytes = 0;
  1661. break;
  1662. default:
  1663. break;
  1664. }
  1665. }
  1666. continue;
  1667. }
  1668. cfdata->read_offset += (uint16_t)cbytes;
  1669. cfdata->uncompressed_bytes_remaining -= (uint16_t)cbytes;
  1670. break;
  1671. } else if (cbytes == 0) {
  1672. err = cab_next_cfdata(a);
  1673. if (err < 0)
  1674. return (err);
  1675. cfdata = cab->entry_cfdata;
  1676. if (cfdata->uncompressed_size == 0) {
  1677. switch (cab->entry_cffile->folder) {
  1678. case iFoldCONTINUED_PREV_AND_NEXT:
  1679. case iFoldCONTINUED_TO_NEXT:
  1680. case iFoldCONTINUED_FROM_PREV:
  1681. return (ARCHIVE_FATAL);
  1682. default:
  1683. break;
  1684. }
  1685. }
  1686. continue;
  1687. }
  1688. while (cbytes > 0) {
  1689. (void)cab_read_ahead_cfdata(a, &avail);
  1690. if (avail <= 0)
  1691. return (ARCHIVE_FATAL);
  1692. if (avail > cbytes)
  1693. avail = (ssize_t)cbytes;
  1694. if (cab_minimum_consume_cfdata(a, avail) < 0)
  1695. return (ARCHIVE_FATAL);
  1696. cbytes -= avail;
  1697. }
  1698. }
  1699. return (consumed_bytes);
  1700. }
  1701. /*
  1702. * Consume CFDATA as much as we have already gotten and
  1703. * compute the sum of CFDATA.
  1704. */
  1705. static int64_t
  1706. cab_minimum_consume_cfdata(struct archive_read *a, int64_t consumed_bytes)
  1707. {
  1708. struct cab *cab = (struct cab *)(a->format->data);
  1709. struct cfdata *cfdata;
  1710. int64_t cbytes, rbytes;
  1711. int err;
  1712. cfdata = cab->entry_cfdata;
  1713. rbytes = consumed_bytes;
  1714. if (cab->entry_cffolder->comptype == COMPTYPE_NONE) {
  1715. if (consumed_bytes < cfdata->unconsumed)
  1716. cbytes = consumed_bytes;
  1717. else
  1718. cbytes = cfdata->unconsumed;
  1719. rbytes -= cbytes;
  1720. cfdata->read_offset += (uint16_t)cbytes;
  1721. cfdata->uncompressed_bytes_remaining -= (uint16_t)cbytes;
  1722. cfdata->unconsumed -= cbytes;
  1723. } else {
  1724. cbytes = cfdata->uncompressed_avail - cfdata->read_offset;
  1725. if (cbytes > 0) {
  1726. if (consumed_bytes < cbytes)
  1727. cbytes = consumed_bytes;
  1728. rbytes -= cbytes;
  1729. cfdata->read_offset += (uint16_t)cbytes;
  1730. cfdata->uncompressed_bytes_remaining -= (uint16_t)cbytes;
  1731. }
  1732. if (cfdata->unconsumed) {
  1733. cbytes = cfdata->unconsumed;
  1734. cfdata->unconsumed = 0;
  1735. } else
  1736. cbytes = 0;
  1737. }
  1738. if (cbytes) {
  1739. /* Compute the sum. */
  1740. cab_checksum_update(a, (size_t)cbytes);
  1741. /* Consume as much as the compressor actually used. */
  1742. __archive_read_consume(a, cbytes);
  1743. cab->cab_offset += cbytes;
  1744. cfdata->compressed_bytes_remaining -= (uint16_t)cbytes;
  1745. if (cfdata->compressed_bytes_remaining == 0) {
  1746. err = cab_checksum_finish(a);
  1747. if (err < 0)
  1748. return (err);
  1749. }
  1750. }
  1751. return (rbytes);
  1752. }
  1753. /*
  1754. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  1755. * cab->end_of_entry if it consumes all of the data.
  1756. */
  1757. static int
  1758. cab_read_data(struct archive_read *a, const void **buff,
  1759. size_t *size, int64_t *offset)
  1760. {
  1761. struct cab *cab = (struct cab *)(a->format->data);
  1762. ssize_t bytes_avail;
  1763. if (cab->entry_bytes_remaining == 0) {
  1764. *buff = NULL;
  1765. *size = 0;
  1766. *offset = cab->entry_offset;
  1767. cab->end_of_entry = 1;
  1768. return (ARCHIVE_OK);
  1769. }
  1770. *buff = cab_read_ahead_cfdata(a, &bytes_avail);
  1771. if (bytes_avail <= 0) {
  1772. *buff = NULL;
  1773. *size = 0;
  1774. *offset = 0;
  1775. if (bytes_avail == 0 &&
  1776. cab->entry_cfdata->uncompressed_size == 0) {
  1777. /* All of CFDATA in a folder has been handled. */
  1778. archive_set_error(&a->archive,
  1779. ARCHIVE_ERRNO_FILE_FORMAT, "Invalid CFDATA");
  1780. return (ARCHIVE_FATAL);
  1781. } else
  1782. return ((int)bytes_avail);
  1783. }
  1784. if (bytes_avail > cab->entry_bytes_remaining)
  1785. bytes_avail = (ssize_t)cab->entry_bytes_remaining;
  1786. *size = bytes_avail;
  1787. *offset = cab->entry_offset;
  1788. cab->entry_offset += bytes_avail;
  1789. cab->entry_bytes_remaining -= bytes_avail;
  1790. if (cab->entry_bytes_remaining == 0)
  1791. cab->end_of_entry = 1;
  1792. cab->entry_unconsumed = bytes_avail;
  1793. if (cab->entry_cffolder->comptype == COMPTYPE_NONE) {
  1794. /* Don't consume more than current entry used. */
  1795. if (cab->entry_cfdata->unconsumed > cab->entry_unconsumed)
  1796. cab->entry_cfdata->unconsumed = cab->entry_unconsumed;
  1797. }
  1798. return (ARCHIVE_OK);
  1799. }
  1800. static int
  1801. archive_read_format_cab_read_data_skip(struct archive_read *a)
  1802. {
  1803. struct cab *cab;
  1804. int64_t bytes_skipped;
  1805. int r;
  1806. cab = (struct cab *)(a->format->data);
  1807. if (cab->end_of_archive)
  1808. return (ARCHIVE_EOF);
  1809. if (!cab->read_data_invoked) {
  1810. cab->bytes_skipped += cab->entry_bytes_remaining;
  1811. cab->entry_bytes_remaining = 0;
  1812. /* This entry is finished and done. */
  1813. cab->end_of_entry_cleanup = cab->end_of_entry = 1;
  1814. return (ARCHIVE_OK);
  1815. }
  1816. if (cab->entry_unconsumed) {
  1817. /* Consume as much as the compressor actually used. */
  1818. r = (int)cab_consume_cfdata(a, cab->entry_unconsumed);
  1819. cab->entry_unconsumed = 0;
  1820. if (r < 0)
  1821. return (r);
  1822. } else if (cab->entry_cfdata == NULL) {
  1823. r = cab_next_cfdata(a);
  1824. if (r < 0)
  1825. return (r);
  1826. }
  1827. /* if we've already read to end of data, we're done. */
  1828. if (cab->end_of_entry_cleanup)
  1829. return (ARCHIVE_OK);
  1830. /*
  1831. * If the length is at the beginning, we can skip the
  1832. * compressed data much more quickly.
  1833. */
  1834. bytes_skipped = cab_consume_cfdata(a, cab->entry_bytes_remaining);
  1835. if (bytes_skipped < 0)
  1836. return (ARCHIVE_FATAL);
  1837. /* If the compression type is none(uncompressed), we've already
  1838. * consumed data as much as the current entry size. */
  1839. if (cab->entry_cffolder->comptype == COMPTYPE_NONE &&
  1840. cab->entry_cfdata != NULL)
  1841. cab->entry_cfdata->unconsumed = 0;
  1842. /* This entry is finished and done. */
  1843. cab->end_of_entry_cleanup = cab->end_of_entry = 1;
  1844. return (ARCHIVE_OK);
  1845. }
  1846. static int
  1847. archive_read_format_cab_cleanup(struct archive_read *a)
  1848. {
  1849. struct cab *cab = (struct cab *)(a->format->data);
  1850. struct cfheader *hd = &cab->cfheader;
  1851. int i;
  1852. if (hd->folder_array != NULL) {
  1853. for (i = 0; i < hd->folder_count; i++)
  1854. free(hd->folder_array[i].cfdata.memimage);
  1855. free(hd->folder_array);
  1856. }
  1857. if (hd->file_array != NULL) {
  1858. for (i = 0; i < cab->cfheader.file_count; i++)
  1859. archive_string_free(&(hd->file_array[i].pathname));
  1860. free(hd->file_array);
  1861. }
  1862. #ifdef HAVE_ZLIB_H
  1863. if (cab->stream_valid)
  1864. inflateEnd(&cab->stream);
  1865. #endif
  1866. lzx_decode_free(&cab->xstrm);
  1867. archive_wstring_free(&cab->ws);
  1868. free(cab->uncompressed_buffer);
  1869. free(cab);
  1870. (a->format->data) = NULL;
  1871. return (ARCHIVE_OK);
  1872. }
  1873. /* Convert an MSDOS-style date/time into Unix-style time. */
  1874. static time_t
  1875. cab_dos_time(const unsigned char *p)
  1876. {
  1877. int msTime, msDate;
  1878. struct tm ts;
  1879. msDate = archive_le16dec(p);
  1880. msTime = archive_le16dec(p+2);
  1881. memset(&ts, 0, sizeof(ts));
  1882. ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
  1883. ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
  1884. ts.tm_mday = msDate & 0x1f; /* Day of month. */
  1885. ts.tm_hour = (msTime >> 11) & 0x1f;
  1886. ts.tm_min = (msTime >> 5) & 0x3f;
  1887. ts.tm_sec = (msTime << 1) & 0x3e;
  1888. ts.tm_isdst = -1;
  1889. return (mktime(&ts));
  1890. }
  1891. /*****************************************************************
  1892. *
  1893. * LZX decompression code.
  1894. *
  1895. *****************************************************************/
  1896. /*
  1897. * Initialize LZX decoder.
  1898. *
  1899. * Returns ARCHIVE_OK if initialization was successful.
  1900. * Returns ARCHIVE_FAILED if w_bits has unsupported value.
  1901. * Returns ARCHIVE_FATAL if initialization failed; memory allocation
  1902. * error occurred.
  1903. */
  1904. static int
  1905. lzx_decode_init(struct lzx_stream *strm, int w_bits)
  1906. {
  1907. struct lzx_dec *ds;
  1908. int slot, w_size, w_slot;
  1909. int base, footer;
  1910. int base_inc[18];
  1911. if (strm->ds == NULL) {
  1912. strm->ds = calloc(1, sizeof(*strm->ds));
  1913. if (strm->ds == NULL)
  1914. return (ARCHIVE_FATAL);
  1915. }
  1916. ds = strm->ds;
  1917. ds->error = ARCHIVE_FAILED;
  1918. /* Allow bits from 15(32KBi) up to 21(2MBi) */
  1919. if (w_bits < SLOT_BASE || w_bits > SLOT_MAX)
  1920. return (ARCHIVE_FAILED);
  1921. ds->error = ARCHIVE_FATAL;
  1922. /*
  1923. * Alloc window
  1924. */
  1925. w_size = ds->w_size;
  1926. w_slot = slots[w_bits - SLOT_BASE];
  1927. ds->w_size = 1U << w_bits;
  1928. ds->w_mask = ds->w_size -1;
  1929. if (ds->w_buff == NULL || w_size != ds->w_size) {
  1930. free(ds->w_buff);
  1931. ds->w_buff = malloc(ds->w_size);
  1932. if (ds->w_buff == NULL)
  1933. return (ARCHIVE_FATAL);
  1934. free(ds->pos_tbl);
  1935. ds->pos_tbl = malloc(sizeof(ds->pos_tbl[0]) * w_slot);
  1936. if (ds->pos_tbl == NULL)
  1937. return (ARCHIVE_FATAL);
  1938. lzx_huffman_free(&(ds->mt));
  1939. }
  1940. for (footer = 0; footer < 18; footer++)
  1941. base_inc[footer] = 1 << footer;
  1942. base = footer = 0;
  1943. for (slot = 0; slot < w_slot; slot++) {
  1944. int n;
  1945. if (footer == 0)
  1946. base = slot;
  1947. else
  1948. base += base_inc[footer];
  1949. if (footer < 17) {
  1950. footer = -2;
  1951. for (n = base; n; n >>= 1)
  1952. footer++;
  1953. if (footer <= 0)
  1954. footer = 0;
  1955. }
  1956. ds->pos_tbl[slot].base = base;
  1957. ds->pos_tbl[slot].footer_bits = footer;
  1958. }
  1959. ds->w_pos = 0;
  1960. ds->state = 0;
  1961. ds->br.cache_buffer = 0;
  1962. ds->br.cache_avail = 0;
  1963. ds->r0 = ds->r1 = ds->r2 = 1;
  1964. /* Initialize aligned offset tree. */
  1965. if (lzx_huffman_init(&(ds->at), 8, 8) != ARCHIVE_OK)
  1966. return (ARCHIVE_FATAL);
  1967. /* Initialize pre-tree. */
  1968. if (lzx_huffman_init(&(ds->pt), 20, 10) != ARCHIVE_OK)
  1969. return (ARCHIVE_FATAL);
  1970. /* Initialize Main tree. */
  1971. if (lzx_huffman_init(&(ds->mt), 256+(w_slot<<3), 16)
  1972. != ARCHIVE_OK)
  1973. return (ARCHIVE_FATAL);
  1974. /* Initialize Length tree. */
  1975. if (lzx_huffman_init(&(ds->lt), 249, 16) != ARCHIVE_OK)
  1976. return (ARCHIVE_FATAL);
  1977. ds->error = 0;
  1978. return (ARCHIVE_OK);
  1979. }
  1980. /*
  1981. * Release LZX decoder.
  1982. */
  1983. static void
  1984. lzx_decode_free(struct lzx_stream *strm)
  1985. {
  1986. if (strm->ds == NULL)
  1987. return;
  1988. free(strm->ds->w_buff);
  1989. free(strm->ds->pos_tbl);
  1990. lzx_huffman_free(&(strm->ds->at));
  1991. lzx_huffman_free(&(strm->ds->pt));
  1992. lzx_huffman_free(&(strm->ds->mt));
  1993. lzx_huffman_free(&(strm->ds->lt));
  1994. free(strm->ds);
  1995. strm->ds = NULL;
  1996. }
  1997. /*
  1998. * E8 Call Translation reversal.
  1999. */
  2000. static void
  2001. lzx_translation(struct lzx_stream *strm, void *p, size_t size, uint32_t offset)
  2002. {
  2003. struct lzx_dec *ds = strm->ds;
  2004. unsigned char *b, *end;
  2005. if (!ds->translation || size <= 10)
  2006. return;
  2007. b = p;
  2008. end = b + size - 10;
  2009. while (b < end && (b = memchr(b, 0xE8, end - b)) != NULL) {
  2010. size_t i = b - (unsigned char *)p;
  2011. int32_t cp, displacement, value;
  2012. cp = (int32_t)(offset + (uint32_t)i);
  2013. value = archive_le32dec(&b[1]);
  2014. if (value >= -cp && value < (int32_t)ds->translation_size) {
  2015. if (value >= 0)
  2016. displacement = value - cp;
  2017. else
  2018. displacement = value + ds->translation_size;
  2019. archive_le32enc(&b[1], (uint32_t)displacement);
  2020. }
  2021. b += 5;
  2022. }
  2023. }
  2024. /*
  2025. * Bit stream reader.
  2026. */
  2027. /* Check that the cache buffer has enough bits. */
  2028. #define lzx_br_has(br, n) ((br)->cache_avail >= n)
  2029. /* Get compressed data by bit. */
  2030. #define lzx_br_bits(br, n) \
  2031. (((uint32_t)((br)->cache_buffer >> \
  2032. ((br)->cache_avail - (n)))) & cache_masks[n])
  2033. #define lzx_br_bits_forced(br, n) \
  2034. (((uint32_t)((br)->cache_buffer << \
  2035. ((n) - (br)->cache_avail))) & cache_masks[n])
  2036. /* Read ahead to make sure the cache buffer has enough compressed data we
  2037. * will use.
  2038. * True : completed, there is enough data in the cache buffer.
  2039. * False : we met that strm->next_in is empty, we have to get following
  2040. * bytes. */
  2041. #define lzx_br_read_ahead_0(strm, br, n) \
  2042. (lzx_br_has((br), (n)) || lzx_br_fillup(strm, br))
  2043. /* True : the cache buffer has some bits as much as we need.
  2044. * False : there are no enough bits in the cache buffer to be used,
  2045. * we have to get following bytes if we could. */
  2046. #define lzx_br_read_ahead(strm, br, n) \
  2047. (lzx_br_read_ahead_0((strm), (br), (n)) || lzx_br_has((br), (n)))
  2048. /* Notify how many bits we consumed. */
  2049. #define lzx_br_consume(br, n) ((br)->cache_avail -= (n))
  2050. #define lzx_br_consume_unaligned_bits(br) ((br)->cache_avail &= ~0x0f)
  2051. #define lzx_br_is_unaligned(br) ((br)->cache_avail & 0x0f)
  2052. static const uint32_t cache_masks[] = {
  2053. 0x00000000, 0x00000001, 0x00000003, 0x00000007,
  2054. 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
  2055. 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
  2056. 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
  2057. 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
  2058. 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
  2059. 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
  2060. 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
  2061. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  2062. };
  2063. /*
  2064. * Shift away used bits in the cache data and fill it up with following bits.
  2065. * Call this when cache buffer does not have enough bits you need.
  2066. *
  2067. * Returns 1 if the cache buffer is full.
  2068. * Returns 0 if the cache buffer is not full; input buffer is empty.
  2069. */
  2070. static int
  2071. lzx_br_fillup(struct lzx_stream *strm, struct lzx_br *br)
  2072. {
  2073. /*
  2074. * x86 proccessor family can read misaligned data without an access error.
  2075. */
  2076. int n = CACHE_BITS - br->cache_avail;
  2077. for (;;) {
  2078. switch (n >> 4) {
  2079. case 4:
  2080. if (strm->avail_in >= 8) {
  2081. br->cache_buffer =
  2082. ((uint64_t)strm->next_in[1]) << 56 |
  2083. ((uint64_t)strm->next_in[0]) << 48 |
  2084. ((uint64_t)strm->next_in[3]) << 40 |
  2085. ((uint64_t)strm->next_in[2]) << 32 |
  2086. ((uint32_t)strm->next_in[5]) << 24 |
  2087. ((uint32_t)strm->next_in[4]) << 16 |
  2088. ((uint32_t)strm->next_in[7]) << 8 |
  2089. (uint32_t)strm->next_in[6];
  2090. strm->next_in += 8;
  2091. strm->avail_in -= 8;
  2092. br->cache_avail += 8 * 8;
  2093. return (1);
  2094. }
  2095. break;
  2096. case 3:
  2097. if (strm->avail_in >= 6) {
  2098. br->cache_buffer =
  2099. (br->cache_buffer << 48) |
  2100. ((uint64_t)strm->next_in[1]) << 40 |
  2101. ((uint64_t)strm->next_in[0]) << 32 |
  2102. ((uint32_t)strm->next_in[3]) << 24 |
  2103. ((uint32_t)strm->next_in[2]) << 16 |
  2104. ((uint32_t)strm->next_in[5]) << 8 |
  2105. (uint32_t)strm->next_in[4];
  2106. strm->next_in += 6;
  2107. strm->avail_in -= 6;
  2108. br->cache_avail += 6 * 8;
  2109. return (1);
  2110. }
  2111. break;
  2112. case 0:
  2113. /* We have enough compressed data in
  2114. * the cache buffer.*/
  2115. return (1);
  2116. default:
  2117. break;
  2118. }
  2119. if (strm->avail_in < 2) {
  2120. /* There is not enough compressed data to
  2121. * fill up the cache buffer. */
  2122. if (strm->avail_in == 1) {
  2123. br->odd = *strm->next_in++;
  2124. strm->avail_in--;
  2125. br->have_odd = 1;
  2126. }
  2127. return (0);
  2128. }
  2129. br->cache_buffer =
  2130. (br->cache_buffer << 16) |
  2131. archive_le16dec(strm->next_in);
  2132. strm->next_in += 2;
  2133. strm->avail_in -= 2;
  2134. br->cache_avail += 16;
  2135. n -= 16;
  2136. }
  2137. }
  2138. static void
  2139. lzx_br_fixup(struct lzx_stream *strm, struct lzx_br *br)
  2140. {
  2141. int n = CACHE_BITS - br->cache_avail;
  2142. if (br->have_odd && n >= 16 && strm->avail_in > 0) {
  2143. br->cache_buffer =
  2144. (br->cache_buffer << 16) |
  2145. ((uint16_t)(*strm->next_in)) << 8 | br->odd;
  2146. strm->next_in++;
  2147. strm->avail_in--;
  2148. br->cache_avail += 16;
  2149. br->have_odd = 0;
  2150. }
  2151. }
  2152. static void
  2153. lzx_cleanup_bitstream(struct lzx_stream *strm)
  2154. {
  2155. strm->ds->br.cache_avail = 0;
  2156. strm->ds->br.have_odd = 0;
  2157. }
  2158. /*
  2159. * Decode LZX.
  2160. *
  2161. * 1. Returns ARCHIVE_OK if output buffer or input buffer are empty.
  2162. * Please set available buffer and call this function again.
  2163. * 2. Returns ARCHIVE_EOF if decompression has been completed.
  2164. * 3. Returns ARCHIVE_FAILED if an error occurred; compressed data
  2165. * is broken or you do not set 'last' flag properly.
  2166. */
  2167. #define ST_RD_TRANSLATION 0
  2168. #define ST_RD_TRANSLATION_SIZE 1
  2169. #define ST_RD_BLOCK_TYPE 2
  2170. #define ST_RD_BLOCK_SIZE 3
  2171. #define ST_RD_ALIGNMENT 4
  2172. #define ST_RD_R0 5
  2173. #define ST_RD_R1 6
  2174. #define ST_RD_R2 7
  2175. #define ST_COPY_UNCOMP1 8
  2176. #define ST_COPY_UNCOMP2 9
  2177. #define ST_RD_ALIGNED_OFFSET 10
  2178. #define ST_RD_VERBATIM 11
  2179. #define ST_RD_PRE_MAIN_TREE_256 12
  2180. #define ST_MAIN_TREE_256 13
  2181. #define ST_RD_PRE_MAIN_TREE_REM 14
  2182. #define ST_MAIN_TREE_REM 15
  2183. #define ST_RD_PRE_LENGTH_TREE 16
  2184. #define ST_LENGTH_TREE 17
  2185. #define ST_MAIN 18
  2186. #define ST_LENGTH 19
  2187. #define ST_OFFSET 20
  2188. #define ST_REAL_POS 21
  2189. #define ST_COPY 22
  2190. static int
  2191. lzx_decode(struct lzx_stream *strm, int last)
  2192. {
  2193. struct lzx_dec *ds = strm->ds;
  2194. int64_t avail_in;
  2195. int r;
  2196. if (ds->error)
  2197. return (ds->error);
  2198. avail_in = strm->avail_in;
  2199. lzx_br_fixup(strm, &(ds->br));
  2200. do {
  2201. if (ds->state < ST_MAIN)
  2202. r = lzx_read_blocks(strm, last);
  2203. else {
  2204. int64_t bytes_written = strm->avail_out;
  2205. r = lzx_decode_blocks(strm, last);
  2206. bytes_written -= strm->avail_out;
  2207. strm->next_out += bytes_written;
  2208. strm->total_out += bytes_written;
  2209. }
  2210. } while (r == 100);
  2211. strm->total_in += avail_in - strm->avail_in;
  2212. return (r);
  2213. }
  2214. static int
  2215. lzx_read_blocks(struct lzx_stream *strm, int last)
  2216. {
  2217. struct lzx_dec *ds = strm->ds;
  2218. struct lzx_br *br = &(ds->br);
  2219. int i, r;
  2220. for (;;) {
  2221. switch (ds->state) {
  2222. case ST_RD_TRANSLATION:
  2223. if (!lzx_br_read_ahead(strm, br, 1)) {
  2224. ds->state = ST_RD_TRANSLATION;
  2225. if (last)
  2226. goto failed;
  2227. return (ARCHIVE_OK);
  2228. }
  2229. ds->translation = lzx_br_bits(br, 1);
  2230. lzx_br_consume(br, 1);
  2231. /* FALL THROUGH */
  2232. case ST_RD_TRANSLATION_SIZE:
  2233. if (ds->translation) {
  2234. if (!lzx_br_read_ahead(strm, br, 32)) {
  2235. ds->state = ST_RD_TRANSLATION_SIZE;
  2236. if (last)
  2237. goto failed;
  2238. return (ARCHIVE_OK);
  2239. }
  2240. ds->translation_size = lzx_br_bits(br, 16);
  2241. lzx_br_consume(br, 16);
  2242. ds->translation_size <<= 16;
  2243. ds->translation_size |= lzx_br_bits(br, 16);
  2244. lzx_br_consume(br, 16);
  2245. }
  2246. /* FALL THROUGH */
  2247. case ST_RD_BLOCK_TYPE:
  2248. if (!lzx_br_read_ahead(strm, br, 3)) {
  2249. ds->state = ST_RD_BLOCK_TYPE;
  2250. if (last)
  2251. goto failed;
  2252. return (ARCHIVE_OK);
  2253. }
  2254. ds->block_type = lzx_br_bits(br, 3);
  2255. lzx_br_consume(br, 3);
  2256. /* Check a block type. */
  2257. switch (ds->block_type) {
  2258. case VERBATIM_BLOCK:
  2259. case ALIGNED_OFFSET_BLOCK:
  2260. case UNCOMPRESSED_BLOCK:
  2261. break;
  2262. default:
  2263. goto failed;/* Invalid */
  2264. }
  2265. /* FALL THROUGH */
  2266. case ST_RD_BLOCK_SIZE:
  2267. if (!lzx_br_read_ahead(strm, br, 24)) {
  2268. ds->state = ST_RD_BLOCK_SIZE;
  2269. if (last)
  2270. goto failed;
  2271. return (ARCHIVE_OK);
  2272. }
  2273. ds->block_size = lzx_br_bits(br, 8);
  2274. lzx_br_consume(br, 8);
  2275. ds->block_size <<= 16;
  2276. ds->block_size |= lzx_br_bits(br, 16);
  2277. lzx_br_consume(br, 16);
  2278. if (ds->block_size == 0)
  2279. goto failed;
  2280. ds->block_bytes_avail = ds->block_size;
  2281. if (ds->block_type != UNCOMPRESSED_BLOCK) {
  2282. if (ds->block_type == VERBATIM_BLOCK)
  2283. ds->state = ST_RD_VERBATIM;
  2284. else
  2285. ds->state = ST_RD_ALIGNED_OFFSET;
  2286. break;
  2287. }
  2288. /* FALL THROUGH */
  2289. case ST_RD_ALIGNMENT:
  2290. /*
  2291. * Handle an Uncompressed Block.
  2292. */
  2293. /* Skip padding to align following field on
  2294. * 16-bit boundary. */
  2295. if (lzx_br_is_unaligned(br))
  2296. lzx_br_consume_unaligned_bits(br);
  2297. else {
  2298. if (lzx_br_read_ahead(strm, br, 16))
  2299. lzx_br_consume(br, 16);
  2300. else {
  2301. ds->state = ST_RD_ALIGNMENT;
  2302. if (last)
  2303. goto failed;
  2304. return (ARCHIVE_OK);
  2305. }
  2306. }
  2307. /* Preparation to read repeated offsets R0,R1 and R2. */
  2308. ds->rbytes_avail = 0;
  2309. ds->state = ST_RD_R0;
  2310. /* FALL THROUGH */
  2311. case ST_RD_R0:
  2312. case ST_RD_R1:
  2313. case ST_RD_R2:
  2314. do {
  2315. uint16_t u16;
  2316. /* Drain bits in the cache buffer of
  2317. * bit-stream. */
  2318. if (lzx_br_has(br, 32)) {
  2319. u16 = lzx_br_bits(br, 16);
  2320. lzx_br_consume(br, 16);
  2321. archive_le16enc(ds->rbytes, u16);
  2322. u16 = lzx_br_bits(br, 16);
  2323. lzx_br_consume(br, 16);
  2324. archive_le16enc(ds->rbytes+2, u16);
  2325. ds->rbytes_avail = 4;
  2326. } else if (lzx_br_has(br, 16)) {
  2327. u16 = lzx_br_bits(br, 16);
  2328. lzx_br_consume(br, 16);
  2329. archive_le16enc(ds->rbytes, u16);
  2330. ds->rbytes_avail = 2;
  2331. }
  2332. if (ds->rbytes_avail < 4 && ds->br.have_odd) {
  2333. ds->rbytes[ds->rbytes_avail++] =
  2334. ds->br.odd;
  2335. ds->br.have_odd = 0;
  2336. }
  2337. while (ds->rbytes_avail < 4) {
  2338. if (strm->avail_in <= 0) {
  2339. if (last)
  2340. goto failed;
  2341. return (ARCHIVE_OK);
  2342. }
  2343. ds->rbytes[ds->rbytes_avail++] =
  2344. *strm->next_in++;
  2345. strm->avail_in--;
  2346. }
  2347. ds->rbytes_avail = 0;
  2348. if (ds->state == ST_RD_R0) {
  2349. ds->r0 = archive_le32dec(ds->rbytes);
  2350. if (ds->r0 < 0)
  2351. goto failed;
  2352. ds->state = ST_RD_R1;
  2353. } else if (ds->state == ST_RD_R1) {
  2354. ds->r1 = archive_le32dec(ds->rbytes);
  2355. if (ds->r1 < 0)
  2356. goto failed;
  2357. ds->state = ST_RD_R2;
  2358. } else if (ds->state == ST_RD_R2) {
  2359. ds->r2 = archive_le32dec(ds->rbytes);
  2360. if (ds->r2 < 0)
  2361. goto failed;
  2362. /* We've gotten all repeated offsets. */
  2363. ds->state = ST_COPY_UNCOMP1;
  2364. }
  2365. } while (ds->state != ST_COPY_UNCOMP1);
  2366. /* FALL THROUGH */
  2367. case ST_COPY_UNCOMP1:
  2368. /*
  2369. * Copy bytes form next_in to next_out directly.
  2370. */
  2371. while (ds->block_bytes_avail) {
  2372. int l;
  2373. if (strm->avail_out <= 0)
  2374. /* Output buffer is empty. */
  2375. return (ARCHIVE_OK);
  2376. if (strm->avail_in <= 0) {
  2377. /* Input buffer is empty. */
  2378. if (last)
  2379. goto failed;
  2380. return (ARCHIVE_OK);
  2381. }
  2382. l = (int)ds->block_bytes_avail;
  2383. if (l > ds->w_size - ds->w_pos)
  2384. l = ds->w_size - ds->w_pos;
  2385. if (l > strm->avail_out)
  2386. l = (int)strm->avail_out;
  2387. if (l > strm->avail_in)
  2388. l = (int)strm->avail_in;
  2389. memcpy(strm->next_out, strm->next_in, l);
  2390. memcpy(&(ds->w_buff[ds->w_pos]),
  2391. strm->next_in, l);
  2392. strm->next_in += l;
  2393. strm->avail_in -= l;
  2394. strm->next_out += l;
  2395. strm->avail_out -= l;
  2396. strm->total_out += l;
  2397. ds->w_pos = (ds->w_pos + l) & ds->w_mask;
  2398. ds->block_bytes_avail -= l;
  2399. }
  2400. /* FALL THROUGH */
  2401. case ST_COPY_UNCOMP2:
  2402. /* Re-align; skip padding byte. */
  2403. if (ds->block_size & 1) {
  2404. if (strm->avail_in <= 0) {
  2405. /* Input buffer is empty. */
  2406. ds->state = ST_COPY_UNCOMP2;
  2407. if (last)
  2408. goto failed;
  2409. return (ARCHIVE_OK);
  2410. }
  2411. strm->next_in++;
  2412. strm->avail_in --;
  2413. }
  2414. /* This block ended. */
  2415. ds->state = ST_RD_BLOCK_TYPE;
  2416. return (ARCHIVE_EOF);
  2417. /********************/
  2418. case ST_RD_ALIGNED_OFFSET:
  2419. /*
  2420. * Read Aligned offset tree.
  2421. */
  2422. if (!lzx_br_read_ahead(strm, br, 3 * ds->at.len_size)) {
  2423. ds->state = ST_RD_ALIGNED_OFFSET;
  2424. if (last)
  2425. goto failed;
  2426. return (ARCHIVE_OK);
  2427. }
  2428. memset(ds->at.freq, 0, sizeof(ds->at.freq));
  2429. for (i = 0; i < ds->at.len_size; i++) {
  2430. ds->at.bitlen[i] = lzx_br_bits(br, 3);
  2431. ds->at.freq[ds->at.bitlen[i]]++;
  2432. lzx_br_consume(br, 3);
  2433. }
  2434. if (!lzx_make_huffman_table(&ds->at))
  2435. goto failed;
  2436. /* FALL THROUGH */
  2437. case ST_RD_VERBATIM:
  2438. ds->loop = 0;
  2439. /* FALL THROUGH */
  2440. case ST_RD_PRE_MAIN_TREE_256:
  2441. /*
  2442. * Read Pre-tree for first 256 elements of main tree.
  2443. */
  2444. if (!lzx_read_pre_tree(strm)) {
  2445. ds->state = ST_RD_PRE_MAIN_TREE_256;
  2446. if (last)
  2447. goto failed;
  2448. return (ARCHIVE_OK);
  2449. }
  2450. if (!lzx_make_huffman_table(&ds->pt))
  2451. goto failed;
  2452. ds->loop = 0;
  2453. /* FALL THROUGH */
  2454. case ST_MAIN_TREE_256:
  2455. /*
  2456. * Get path lengths of first 256 elements of main tree.
  2457. */
  2458. r = lzx_read_bitlen(strm, &ds->mt, 256);
  2459. if (r < 0)
  2460. goto failed;
  2461. else if (!r) {
  2462. ds->state = ST_MAIN_TREE_256;
  2463. if (last)
  2464. goto failed;
  2465. return (ARCHIVE_OK);
  2466. }
  2467. ds->loop = 0;
  2468. /* FALL THROUGH */
  2469. case ST_RD_PRE_MAIN_TREE_REM:
  2470. /*
  2471. * Read Pre-tree for remaining elements of main tree.
  2472. */
  2473. if (!lzx_read_pre_tree(strm)) {
  2474. ds->state = ST_RD_PRE_MAIN_TREE_REM;
  2475. if (last)
  2476. goto failed;
  2477. return (ARCHIVE_OK);
  2478. }
  2479. if (!lzx_make_huffman_table(&ds->pt))
  2480. goto failed;
  2481. ds->loop = 256;
  2482. /* FALL THROUGH */
  2483. case ST_MAIN_TREE_REM:
  2484. /*
  2485. * Get path lengths of remaining elements of main tree.
  2486. */
  2487. r = lzx_read_bitlen(strm, &ds->mt, -1);
  2488. if (r < 0)
  2489. goto failed;
  2490. else if (!r) {
  2491. ds->state = ST_MAIN_TREE_REM;
  2492. if (last)
  2493. goto failed;
  2494. return (ARCHIVE_OK);
  2495. }
  2496. if (!lzx_make_huffman_table(&ds->mt))
  2497. goto failed;
  2498. ds->loop = 0;
  2499. /* FALL THROUGH */
  2500. case ST_RD_PRE_LENGTH_TREE:
  2501. /*
  2502. * Read Pre-tree for remaining elements of main tree.
  2503. */
  2504. if (!lzx_read_pre_tree(strm)) {
  2505. ds->state = ST_RD_PRE_LENGTH_TREE;
  2506. if (last)
  2507. goto failed;
  2508. return (ARCHIVE_OK);
  2509. }
  2510. if (!lzx_make_huffman_table(&ds->pt))
  2511. goto failed;
  2512. ds->loop = 0;
  2513. /* FALL THROUGH */
  2514. case ST_LENGTH_TREE:
  2515. /*
  2516. * Get path lengths of remaining elements of main tree.
  2517. */
  2518. r = lzx_read_bitlen(strm, &ds->lt, -1);
  2519. if (r < 0)
  2520. goto failed;
  2521. else if (!r) {
  2522. ds->state = ST_LENGTH_TREE;
  2523. if (last)
  2524. goto failed;
  2525. return (ARCHIVE_OK);
  2526. }
  2527. if (!lzx_make_huffman_table(&ds->lt))
  2528. goto failed;
  2529. ds->state = ST_MAIN;
  2530. return (100);
  2531. }
  2532. }
  2533. failed:
  2534. return (ds->error = ARCHIVE_FAILED);
  2535. }
  2536. static int
  2537. lzx_decode_blocks(struct lzx_stream *strm, int last)
  2538. {
  2539. struct lzx_dec *ds = strm->ds;
  2540. struct lzx_br bre = ds->br;
  2541. struct huffman *at = &(ds->at), *lt = &(ds->lt), *mt = &(ds->mt);
  2542. const struct lzx_pos_tbl *pos_tbl = ds->pos_tbl;
  2543. unsigned char *noutp = strm->next_out;
  2544. unsigned char *endp = noutp + strm->avail_out;
  2545. unsigned char *w_buff = ds->w_buff;
  2546. unsigned char *at_bitlen = at->bitlen;
  2547. unsigned char *lt_bitlen = lt->bitlen;
  2548. unsigned char *mt_bitlen = mt->bitlen;
  2549. size_t block_bytes_avail = ds->block_bytes_avail;
  2550. int at_max_bits = at->max_bits;
  2551. int lt_max_bits = lt->max_bits;
  2552. int mt_max_bits = mt->max_bits;
  2553. int c, copy_len = ds->copy_len, copy_pos = ds->copy_pos;
  2554. int w_pos = ds->w_pos, w_mask = ds->w_mask, w_size = ds->w_size;
  2555. int length_header = ds->length_header;
  2556. int offset_bits = ds->offset_bits;
  2557. int position_slot = ds->position_slot;
  2558. int r0 = ds->r0, r1 = ds->r1, r2 = ds->r2;
  2559. int state = ds->state;
  2560. char block_type = ds->block_type;
  2561. for (;;) {
  2562. switch (state) {
  2563. case ST_MAIN:
  2564. for (;;) {
  2565. if (block_bytes_avail == 0) {
  2566. /* This block ended. */
  2567. ds->state = ST_RD_BLOCK_TYPE;
  2568. ds->br = bre;
  2569. ds->block_bytes_avail =
  2570. block_bytes_avail;
  2571. ds->copy_len = copy_len;
  2572. ds->copy_pos = copy_pos;
  2573. ds->length_header = length_header;
  2574. ds->position_slot = position_slot;
  2575. ds->r0 = r0; ds->r1 = r1; ds->r2 = r2;
  2576. ds->w_pos = w_pos;
  2577. strm->avail_out = endp - noutp;
  2578. return (ARCHIVE_EOF);
  2579. }
  2580. if (noutp >= endp)
  2581. /* Output buffer is empty. */
  2582. goto next_data;
  2583. if (!lzx_br_read_ahead(strm, &bre,
  2584. mt_max_bits)) {
  2585. if (!last)
  2586. goto next_data;
  2587. /* Remaining bits are less than
  2588. * maximum bits(mt.max_bits) but maybe
  2589. * it still remains as much as we need,
  2590. * so we should try to use it with
  2591. * dummy bits. */
  2592. c = lzx_decode_huffman(mt,
  2593. lzx_br_bits_forced(
  2594. &bre, mt_max_bits));
  2595. lzx_br_consume(&bre, mt_bitlen[c]);
  2596. if (!lzx_br_has(&bre, 0))
  2597. goto failed;/* Over read. */
  2598. } else {
  2599. c = lzx_decode_huffman(mt,
  2600. lzx_br_bits(&bre, mt_max_bits));
  2601. lzx_br_consume(&bre, mt_bitlen[c]);
  2602. }
  2603. if (c > UCHAR_MAX)
  2604. break;
  2605. /*
  2606. * 'c' is exactly literal code.
  2607. */
  2608. /* Save a decoded code to reference it
  2609. * afterward. */
  2610. w_buff[w_pos] = c;
  2611. w_pos = (w_pos + 1) & w_mask;
  2612. /* Store the decoded code to output buffer. */
  2613. *noutp++ = c;
  2614. block_bytes_avail--;
  2615. }
  2616. /*
  2617. * Get a match code, its length and offset.
  2618. */
  2619. c -= UCHAR_MAX + 1;
  2620. length_header = c & 7;
  2621. position_slot = c >> 3;
  2622. /* FALL THROUGH */
  2623. case ST_LENGTH:
  2624. /*
  2625. * Get a length.
  2626. */
  2627. if (length_header == 7) {
  2628. if (!lzx_br_read_ahead(strm, &bre,
  2629. lt_max_bits)) {
  2630. if (!last) {
  2631. state = ST_LENGTH;
  2632. goto next_data;
  2633. }
  2634. c = lzx_decode_huffman(lt,
  2635. lzx_br_bits_forced(
  2636. &bre, lt_max_bits));
  2637. lzx_br_consume(&bre, lt_bitlen[c]);
  2638. if (!lzx_br_has(&bre, 0))
  2639. goto failed;/* Over read. */
  2640. } else {
  2641. c = lzx_decode_huffman(lt,
  2642. lzx_br_bits(&bre, lt_max_bits));
  2643. lzx_br_consume(&bre, lt_bitlen[c]);
  2644. }
  2645. copy_len = c + 7 + 2;
  2646. } else
  2647. copy_len = length_header + 2;
  2648. if ((size_t)copy_len > block_bytes_avail)
  2649. goto failed;
  2650. /*
  2651. * Get an offset.
  2652. */
  2653. switch (position_slot) {
  2654. case 0: /* Use repeated offset 0. */
  2655. copy_pos = r0;
  2656. state = ST_REAL_POS;
  2657. continue;
  2658. case 1: /* Use repeated offset 1. */
  2659. copy_pos = r1;
  2660. /* Swap repeated offset. */
  2661. r1 = r0;
  2662. r0 = copy_pos;
  2663. state = ST_REAL_POS;
  2664. continue;
  2665. case 2: /* Use repeated offset 2. */
  2666. copy_pos = r2;
  2667. /* Swap repeated offset. */
  2668. r2 = r0;
  2669. r0 = copy_pos;
  2670. state = ST_REAL_POS;
  2671. continue;
  2672. default:
  2673. offset_bits =
  2674. pos_tbl[position_slot].footer_bits;
  2675. break;
  2676. }
  2677. /* FALL THROUGH */
  2678. case ST_OFFSET:
  2679. /*
  2680. * Get the offset, which is a distance from
  2681. * current window position.
  2682. */
  2683. if (block_type == ALIGNED_OFFSET_BLOCK &&
  2684. offset_bits >= 3) {
  2685. int offbits = offset_bits - 3;
  2686. if (!lzx_br_read_ahead(strm, &bre, offbits)) {
  2687. state = ST_OFFSET;
  2688. if (last)
  2689. goto failed;
  2690. goto next_data;
  2691. }
  2692. copy_pos = lzx_br_bits(&bre, offbits) << 3;
  2693. /* Get an aligned number. */
  2694. if (!lzx_br_read_ahead(strm, &bre,
  2695. offbits + at_max_bits)) {
  2696. if (!last) {
  2697. state = ST_OFFSET;
  2698. goto next_data;
  2699. }
  2700. lzx_br_consume(&bre, offbits);
  2701. c = lzx_decode_huffman(at,
  2702. lzx_br_bits_forced(&bre,
  2703. at_max_bits));
  2704. lzx_br_consume(&bre, at_bitlen[c]);
  2705. if (!lzx_br_has(&bre, 0))
  2706. goto failed;/* Over read. */
  2707. } else {
  2708. lzx_br_consume(&bre, offbits);
  2709. c = lzx_decode_huffman(at,
  2710. lzx_br_bits(&bre, at_max_bits));
  2711. lzx_br_consume(&bre, at_bitlen[c]);
  2712. }
  2713. /* Add an aligned number. */
  2714. copy_pos += c;
  2715. } else {
  2716. if (!lzx_br_read_ahead(strm, &bre,
  2717. offset_bits)) {
  2718. state = ST_OFFSET;
  2719. if (last)
  2720. goto failed;
  2721. goto next_data;
  2722. }
  2723. copy_pos = lzx_br_bits(&bre, offset_bits);
  2724. lzx_br_consume(&bre, offset_bits);
  2725. }
  2726. copy_pos += pos_tbl[position_slot].base -2;
  2727. /* Update repeated offset LRU queue. */
  2728. r2 = r1;
  2729. r1 = r0;
  2730. r0 = copy_pos;
  2731. /* FALL THROUGH */
  2732. case ST_REAL_POS:
  2733. /*
  2734. * Compute a real position in window.
  2735. */
  2736. copy_pos = (w_pos - copy_pos) & w_mask;
  2737. /* FALL THROUGH */
  2738. case ST_COPY:
  2739. /*
  2740. * Copy several bytes as extracted data from the window
  2741. * into the output buffer.
  2742. */
  2743. for (;;) {
  2744. const unsigned char *s;
  2745. int l;
  2746. l = copy_len;
  2747. if (copy_pos > w_pos) {
  2748. if (l > w_size - copy_pos)
  2749. l = w_size - copy_pos;
  2750. } else {
  2751. if (l > w_size - w_pos)
  2752. l = w_size - w_pos;
  2753. }
  2754. if (noutp + l >= endp)
  2755. l = (int)(endp - noutp);
  2756. s = w_buff + copy_pos;
  2757. if (l >= 8 && ((copy_pos + l < w_pos)
  2758. || (w_pos + l < copy_pos))) {
  2759. memcpy(w_buff + w_pos, s, l);
  2760. memcpy(noutp, s, l);
  2761. } else {
  2762. unsigned char *d;
  2763. int li;
  2764. d = w_buff + w_pos;
  2765. for (li = 0; li < l; li++)
  2766. noutp[li] = d[li] = s[li];
  2767. }
  2768. noutp += l;
  2769. copy_pos = (copy_pos + l) & w_mask;
  2770. w_pos = (w_pos + l) & w_mask;
  2771. block_bytes_avail -= l;
  2772. if (copy_len <= l)
  2773. /* A copy of current pattern ended. */
  2774. break;
  2775. copy_len -= l;
  2776. if (noutp >= endp) {
  2777. /* Output buffer is empty. */
  2778. state = ST_COPY;
  2779. goto next_data;
  2780. }
  2781. }
  2782. state = ST_MAIN;
  2783. break;
  2784. }
  2785. }
  2786. failed:
  2787. return (ds->error = ARCHIVE_FAILED);
  2788. next_data:
  2789. ds->br = bre;
  2790. ds->block_bytes_avail = block_bytes_avail;
  2791. ds->copy_len = copy_len;
  2792. ds->copy_pos = copy_pos;
  2793. ds->length_header = length_header;
  2794. ds->offset_bits = offset_bits;
  2795. ds->position_slot = position_slot;
  2796. ds->r0 = r0; ds->r1 = r1; ds->r2 = r2;
  2797. ds->state = state;
  2798. ds->w_pos = w_pos;
  2799. strm->avail_out = endp - noutp;
  2800. return (ARCHIVE_OK);
  2801. }
  2802. static int
  2803. lzx_read_pre_tree(struct lzx_stream *strm)
  2804. {
  2805. struct lzx_dec *ds = strm->ds;
  2806. struct lzx_br *br = &(ds->br);
  2807. int i;
  2808. if (ds->loop == 0)
  2809. memset(ds->pt.freq, 0, sizeof(ds->pt.freq));
  2810. for (i = ds->loop; i < ds->pt.len_size; i++) {
  2811. if (!lzx_br_read_ahead(strm, br, 4)) {
  2812. ds->loop = i;
  2813. return (0);
  2814. }
  2815. ds->pt.bitlen[i] = lzx_br_bits(br, 4);
  2816. ds->pt.freq[ds->pt.bitlen[i]]++;
  2817. lzx_br_consume(br, 4);
  2818. }
  2819. ds->loop = i;
  2820. return (1);
  2821. }
  2822. /*
  2823. * Read a bunch of bit-lengths from pre-tree.
  2824. */
  2825. static int
  2826. lzx_read_bitlen(struct lzx_stream *strm, struct huffman *d, int end)
  2827. {
  2828. struct lzx_dec *ds = strm->ds;
  2829. struct lzx_br *br = &(ds->br);
  2830. int c, i, j, ret, same;
  2831. unsigned rbits;
  2832. i = ds->loop;
  2833. if (i == 0)
  2834. memset(d->freq, 0, sizeof(d->freq));
  2835. ret = 0;
  2836. if (end < 0)
  2837. end = d->len_size;
  2838. while (i < end) {
  2839. ds->loop = i;
  2840. if (!lzx_br_read_ahead(strm, br, ds->pt.max_bits))
  2841. goto getdata;
  2842. rbits = lzx_br_bits(br, ds->pt.max_bits);
  2843. c = lzx_decode_huffman(&(ds->pt), rbits);
  2844. switch (c) {
  2845. case 17:/* several zero lengths, from 4 to 19. */
  2846. if (!lzx_br_read_ahead(strm, br, ds->pt.bitlen[c]+4))
  2847. goto getdata;
  2848. lzx_br_consume(br, ds->pt.bitlen[c]);
  2849. same = lzx_br_bits(br, 4) + 4;
  2850. if (i + same > end)
  2851. return (-1);/* Invalid */
  2852. lzx_br_consume(br, 4);
  2853. for (j = 0; j < same; j++)
  2854. d->bitlen[i++] = 0;
  2855. break;
  2856. case 18:/* many zero lengths, from 20 to 51. */
  2857. if (!lzx_br_read_ahead(strm, br, ds->pt.bitlen[c]+5))
  2858. goto getdata;
  2859. lzx_br_consume(br, ds->pt.bitlen[c]);
  2860. same = lzx_br_bits(br, 5) + 20;
  2861. if (i + same > end)
  2862. return (-1);/* Invalid */
  2863. lzx_br_consume(br, 5);
  2864. memset(d->bitlen + i, 0, same);
  2865. i += same;
  2866. break;
  2867. case 19:/* a few same lengths. */
  2868. if (!lzx_br_read_ahead(strm, br,
  2869. ds->pt.bitlen[c]+1+ds->pt.max_bits))
  2870. goto getdata;
  2871. lzx_br_consume(br, ds->pt.bitlen[c]);
  2872. same = lzx_br_bits(br, 1) + 4;
  2873. if (i + same > end)
  2874. return (-1);
  2875. lzx_br_consume(br, 1);
  2876. rbits = lzx_br_bits(br, ds->pt.max_bits);
  2877. c = lzx_decode_huffman(&(ds->pt), rbits);
  2878. lzx_br_consume(br, ds->pt.bitlen[c]);
  2879. c = (d->bitlen[i] - c + 17) % 17;
  2880. if (c < 0)
  2881. return (-1);/* Invalid */
  2882. for (j = 0; j < same; j++)
  2883. d->bitlen[i++] = c;
  2884. d->freq[c] += same;
  2885. break;
  2886. default:
  2887. lzx_br_consume(br, ds->pt.bitlen[c]);
  2888. c = (d->bitlen[i] - c + 17) % 17;
  2889. if (c < 0)
  2890. return (-1);/* Invalid */
  2891. d->freq[c]++;
  2892. d->bitlen[i++] = c;
  2893. break;
  2894. }
  2895. }
  2896. ret = 1;
  2897. getdata:
  2898. ds->loop = i;
  2899. return (ret);
  2900. }
  2901. static int
  2902. lzx_huffman_init(struct huffman *hf, size_t len_size, int tbl_bits)
  2903. {
  2904. int bits;
  2905. if (hf->bitlen == NULL || hf->len_size != (int)len_size) {
  2906. free(hf->bitlen);
  2907. hf->bitlen = calloc(len_size, sizeof(hf->bitlen[0]));
  2908. if (hf->bitlen == NULL)
  2909. return (ARCHIVE_FATAL);
  2910. hf->len_size = (int)len_size;
  2911. } else
  2912. memset(hf->bitlen, 0, len_size * sizeof(hf->bitlen[0]));
  2913. if (hf->tbl == NULL) {
  2914. if (tbl_bits < HTBL_BITS)
  2915. bits = tbl_bits;
  2916. else
  2917. bits = HTBL_BITS;
  2918. hf->tbl = malloc(((size_t)1 << bits) * sizeof(hf->tbl[0]));
  2919. if (hf->tbl == NULL)
  2920. return (ARCHIVE_FATAL);
  2921. hf->tbl_bits = tbl_bits;
  2922. }
  2923. if (hf->tree == NULL && tbl_bits > HTBL_BITS) {
  2924. hf->tree_avail = 1 << (tbl_bits - HTBL_BITS + 4);
  2925. hf->tree = malloc(hf->tree_avail * sizeof(hf->tree[0]));
  2926. if (hf->tree == NULL)
  2927. return (ARCHIVE_FATAL);
  2928. }
  2929. return (ARCHIVE_OK);
  2930. }
  2931. static void
  2932. lzx_huffman_free(struct huffman *hf)
  2933. {
  2934. free(hf->bitlen);
  2935. free(hf->tbl);
  2936. free(hf->tree);
  2937. }
  2938. /*
  2939. * Make a huffman coding table.
  2940. */
  2941. static int
  2942. lzx_make_huffman_table(struct huffman *hf)
  2943. {
  2944. uint16_t *tbl;
  2945. const unsigned char *bitlen;
  2946. int bitptn[17], weight[17];
  2947. int i, maxbits = 0, ptn, tbl_size, w;
  2948. int diffbits, len_avail;
  2949. /*
  2950. * Initialize bit patterns.
  2951. */
  2952. ptn = 0;
  2953. for (i = 1, w = 1 << 15; i <= 16; i++, w >>= 1) {
  2954. bitptn[i] = ptn;
  2955. weight[i] = w;
  2956. if (hf->freq[i]) {
  2957. ptn += hf->freq[i] * w;
  2958. maxbits = i;
  2959. }
  2960. }
  2961. if ((ptn & 0xffff) != 0 || maxbits > hf->tbl_bits)
  2962. return (0);/* Invalid */
  2963. hf->max_bits = maxbits;
  2964. /*
  2965. * Cut out extra bits which we won't house in the table.
  2966. * This preparation reduces the same calculation in the for-loop
  2967. * making the table.
  2968. */
  2969. if (maxbits < 16) {
  2970. int ebits = 16 - maxbits;
  2971. for (i = 1; i <= maxbits; i++) {
  2972. bitptn[i] >>= ebits;
  2973. weight[i] >>= ebits;
  2974. }
  2975. }
  2976. if (maxbits > HTBL_BITS) {
  2977. int htbl_max;
  2978. uint16_t *p;
  2979. diffbits = maxbits - HTBL_BITS;
  2980. for (i = 1; i <= HTBL_BITS; i++) {
  2981. bitptn[i] >>= diffbits;
  2982. weight[i] >>= diffbits;
  2983. }
  2984. htbl_max = bitptn[HTBL_BITS] +
  2985. weight[HTBL_BITS] * hf->freq[HTBL_BITS];
  2986. p = &(hf->tbl[htbl_max]);
  2987. while (p < &hf->tbl[1U<<HTBL_BITS])
  2988. *p++ = 0;
  2989. } else
  2990. diffbits = 0;
  2991. hf->shift_bits = diffbits;
  2992. /*
  2993. * Make the table.
  2994. */
  2995. tbl_size = 1 << HTBL_BITS;
  2996. tbl = hf->tbl;
  2997. bitlen = hf->bitlen;
  2998. len_avail = hf->len_size;
  2999. hf->tree_used = 0;
  3000. for (i = 0; i < len_avail; i++) {
  3001. uint16_t *p;
  3002. int len, cnt;
  3003. uint16_t bit;
  3004. int extlen;
  3005. struct htree_t *ht;
  3006. if (bitlen[i] == 0)
  3007. continue;
  3008. /* Get a bit pattern */
  3009. len = bitlen[i];
  3010. ptn = bitptn[len];
  3011. cnt = weight[len];
  3012. if (len <= HTBL_BITS) {
  3013. /* Calculate next bit pattern */
  3014. if ((bitptn[len] = ptn + cnt) > tbl_size)
  3015. return (0);/* Invalid */
  3016. /* Update the table */
  3017. p = &(tbl[ptn]);
  3018. while (--cnt >= 0)
  3019. p[cnt] = (uint16_t)i;
  3020. continue;
  3021. }
  3022. /*
  3023. * A bit length is too big to be housed to a direct table,
  3024. * so we use a tree model for its extra bits.
  3025. */
  3026. bitptn[len] = ptn + cnt;
  3027. bit = 1U << (diffbits -1);
  3028. extlen = len - HTBL_BITS;
  3029. p = &(tbl[ptn >> diffbits]);
  3030. if (*p == 0) {
  3031. *p = len_avail + hf->tree_used;
  3032. ht = &(hf->tree[hf->tree_used++]);
  3033. if (hf->tree_used > hf->tree_avail)
  3034. return (0);/* Invalid */
  3035. ht->left = 0;
  3036. ht->right = 0;
  3037. } else {
  3038. if (*p < len_avail ||
  3039. *p >= (len_avail + hf->tree_used))
  3040. return (0);/* Invalid */
  3041. ht = &(hf->tree[*p - len_avail]);
  3042. }
  3043. while (--extlen > 0) {
  3044. if (ptn & bit) {
  3045. if (ht->left < len_avail) {
  3046. ht->left = len_avail + hf->tree_used;
  3047. ht = &(hf->tree[hf->tree_used++]);
  3048. if (hf->tree_used > hf->tree_avail)
  3049. return (0);/* Invalid */
  3050. ht->left = 0;
  3051. ht->right = 0;
  3052. } else {
  3053. ht = &(hf->tree[ht->left - len_avail]);
  3054. }
  3055. } else {
  3056. if (ht->right < len_avail) {
  3057. ht->right = len_avail + hf->tree_used;
  3058. ht = &(hf->tree[hf->tree_used++]);
  3059. if (hf->tree_used > hf->tree_avail)
  3060. return (0);/* Invalid */
  3061. ht->left = 0;
  3062. ht->right = 0;
  3063. } else {
  3064. ht = &(hf->tree[ht->right - len_avail]);
  3065. }
  3066. }
  3067. bit >>= 1;
  3068. }
  3069. if (ptn & bit) {
  3070. if (ht->left != 0)
  3071. return (0);/* Invalid */
  3072. ht->left = (uint16_t)i;
  3073. } else {
  3074. if (ht->right != 0)
  3075. return (0);/* Invalid */
  3076. ht->right = (uint16_t)i;
  3077. }
  3078. }
  3079. return (1);
  3080. }
  3081. static int
  3082. lzx_decode_huffman_tree(struct huffman *hf, unsigned rbits, int c)
  3083. {
  3084. struct htree_t *ht;
  3085. int extlen;
  3086. ht = hf->tree;
  3087. extlen = hf->shift_bits;
  3088. while (c >= hf->len_size) {
  3089. c -= hf->len_size;
  3090. if (extlen-- <= 0 || c >= hf->tree_used)
  3091. return (0);
  3092. if (rbits & (1U << extlen))
  3093. c = ht[c].left;
  3094. else
  3095. c = ht[c].right;
  3096. }
  3097. return (c);
  3098. }
  3099. static inline int
  3100. lzx_decode_huffman(struct huffman *hf, unsigned rbits)
  3101. {
  3102. int c;
  3103. /*
  3104. * At first search an index table for a bit pattern.
  3105. * If it fails, search a huffman tree for.
  3106. */
  3107. c = hf->tbl[rbits >> hf->shift_bits];
  3108. if (c < hf->len_size)
  3109. return (c);
  3110. /* This bit pattern needs to be found out at a huffman tree. */
  3111. return (lzx_decode_huffman_tree(hf, rbits, c));
  3112. }