archive_read_support_format_lha.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  1. /*-
  2. * Copyright (c) 2008-2014 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. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_entry_locale.h"
  41. #include "archive_private.h"
  42. #include "archive_read_private.h"
  43. #include "archive_endian.h"
  44. #define MAXMATCH 256 /* Maximum match length. */
  45. #define MINMATCH 3 /* Minimum match length. */
  46. /*
  47. * Literal table format:
  48. * +0 +256 +510
  49. * +---------------+-------------------------+
  50. * | literal code | match length |
  51. * | 0 ... 255 | MINMATCH ... MAXMATCH |
  52. * +---------------+-------------------------+
  53. * <--- LT_BITLEN_SIZE --->
  54. */
  55. /* Literal table size. */
  56. #define LT_BITLEN_SIZE (UCHAR_MAX + 1 + MAXMATCH - MINMATCH + 1)
  57. /* Position table size.
  58. * Note: this used for both position table and pre literal table.*/
  59. #define PT_BITLEN_SIZE (3 + 16)
  60. struct lzh_dec {
  61. /* Decoding status. */
  62. int state;
  63. /*
  64. * Window to see last 8Ki(lh5),32Ki(lh6),64Ki(lh7) bytes of decoded
  65. * data.
  66. */
  67. int w_size;
  68. int w_mask;
  69. /* Window buffer, which is a loop buffer. */
  70. unsigned char *w_buff;
  71. /* The insert position to the window. */
  72. int w_pos;
  73. /* The position where we can copy decoded code from the window. */
  74. int copy_pos;
  75. /* The length how many bytes we can copy decoded code from
  76. * the window. */
  77. int copy_len;
  78. /*
  79. * Bit stream reader.
  80. */
  81. struct lzh_br {
  82. #define CACHE_TYPE uint64_t
  83. #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
  84. /* Cache buffer. */
  85. CACHE_TYPE cache_buffer;
  86. /* Indicates how many bits avail in cache_buffer. */
  87. int cache_avail;
  88. } br;
  89. /*
  90. * Huffman coding.
  91. */
  92. struct huffman {
  93. int len_size;
  94. int len_avail;
  95. int len_bits;
  96. int freq[17];
  97. unsigned char *bitlen;
  98. /*
  99. * Use a index table. It's faster than searching a huffman
  100. * coding tree, which is a binary tree. But a use of a large
  101. * index table causes L1 cache read miss many times.
  102. */
  103. #define HTBL_BITS 10
  104. int max_bits;
  105. int shift_bits;
  106. int tbl_bits;
  107. int tree_used;
  108. int tree_avail;
  109. /* Direct access table. */
  110. uint16_t *tbl;
  111. /* Binary tree table for extra bits over the direct access. */
  112. struct htree_t {
  113. uint16_t left;
  114. uint16_t right;
  115. } *tree;
  116. } lt, pt;
  117. int blocks_avail;
  118. int pos_pt_len_size;
  119. int pos_pt_len_bits;
  120. int literal_pt_len_size;
  121. int literal_pt_len_bits;
  122. int reading_position;
  123. int loop;
  124. int error;
  125. };
  126. struct lzh_stream {
  127. const unsigned char *next_in;
  128. int avail_in;
  129. int64_t total_in;
  130. const unsigned char *ref_ptr;
  131. int avail_out;
  132. int64_t total_out;
  133. struct lzh_dec *ds;
  134. };
  135. struct lha {
  136. /* entry_bytes_remaining is the number of bytes we expect. */
  137. int64_t entry_offset;
  138. int64_t entry_bytes_remaining;
  139. int64_t entry_unconsumed;
  140. uint16_t entry_crc_calculated;
  141. size_t header_size; /* header size */
  142. unsigned char level; /* header level */
  143. char method[3]; /* compress type */
  144. int64_t compsize; /* compressed data size */
  145. int64_t origsize; /* original file size */
  146. int setflag;
  147. #define BIRTHTIME_IS_SET 1
  148. #define ATIME_IS_SET 2
  149. #define UNIX_MODE_IS_SET 4
  150. #define CRC_IS_SET 8
  151. time_t birthtime;
  152. long birthtime_tv_nsec;
  153. time_t mtime;
  154. long mtime_tv_nsec;
  155. time_t atime;
  156. long atime_tv_nsec;
  157. mode_t mode;
  158. int64_t uid;
  159. int64_t gid;
  160. struct archive_string uname;
  161. struct archive_string gname;
  162. uint16_t header_crc;
  163. uint16_t crc;
  164. struct archive_string_conv *sconv;
  165. struct archive_string_conv *opt_sconv;
  166. struct archive_string dirname;
  167. struct archive_string filename;
  168. struct archive_wstring ws;
  169. unsigned char dos_attr;
  170. /* Flag to mark progress that an archive was read their first header.*/
  171. char found_first_header;
  172. /* Flag to mark that indicates an empty directory. */
  173. char directory;
  174. /* Flags to mark progress of decompression. */
  175. char decompress_init;
  176. char end_of_entry;
  177. char end_of_entry_cleanup;
  178. char entry_is_compressed;
  179. char format_name[64];
  180. struct lzh_stream strm;
  181. };
  182. /*
  183. * LHA header common member offset.
  184. */
  185. #define H_METHOD_OFFSET 2 /* Compress type. */
  186. #define H_ATTR_OFFSET 19 /* DOS attribute. */
  187. #define H_LEVEL_OFFSET 20 /* Header Level. */
  188. #define H_SIZE 22 /* Minimum header size. */
  189. static int archive_read_format_lha_bid(struct archive_read *, int);
  190. static int archive_read_format_lha_options(struct archive_read *,
  191. const char *, const char *);
  192. static int archive_read_format_lha_read_header(struct archive_read *,
  193. struct archive_entry *);
  194. static int archive_read_format_lha_read_data(struct archive_read *,
  195. const void **, size_t *, int64_t *);
  196. static int archive_read_format_lha_read_data_skip(struct archive_read *);
  197. static int archive_read_format_lha_cleanup(struct archive_read *);
  198. static void lha_replace_path_separator(struct lha *,
  199. struct archive_entry *);
  200. static int lha_read_file_header_0(struct archive_read *, struct lha *);
  201. static int lha_read_file_header_1(struct archive_read *, struct lha *);
  202. static int lha_read_file_header_2(struct archive_read *, struct lha *);
  203. static int lha_read_file_header_3(struct archive_read *, struct lha *);
  204. static int lha_read_file_extended_header(struct archive_read *,
  205. struct lha *, uint16_t *, int, size_t, size_t *);
  206. static size_t lha_check_header_format(const void *);
  207. static int lha_skip_sfx(struct archive_read *);
  208. static time_t lha_dos_time(const unsigned char *);
  209. static time_t lha_win_time(uint64_t, long *);
  210. static unsigned char lha_calcsum(unsigned char, const void *,
  211. int, size_t);
  212. static int lha_parse_linkname(struct archive_string *,
  213. struct archive_string *);
  214. static int lha_read_data_none(struct archive_read *, const void **,
  215. size_t *, int64_t *);
  216. static int lha_read_data_lzh(struct archive_read *, const void **,
  217. size_t *, int64_t *);
  218. static void lha_crc16_init(void);
  219. static uint16_t lha_crc16(uint16_t, const void *, size_t);
  220. static int lzh_decode_init(struct lzh_stream *, const char *);
  221. static void lzh_decode_free(struct lzh_stream *);
  222. static int lzh_decode(struct lzh_stream *, int);
  223. static int lzh_br_fillup(struct lzh_stream *, struct lzh_br *);
  224. static int lzh_huffman_init(struct huffman *, size_t, int);
  225. static void lzh_huffman_free(struct huffman *);
  226. static int lzh_read_pt_bitlen(struct lzh_stream *, int start, int end);
  227. static int lzh_make_fake_table(struct huffman *, uint16_t);
  228. static int lzh_make_huffman_table(struct huffman *);
  229. static inline int lzh_decode_huffman(struct huffman *, unsigned);
  230. static int lzh_decode_huffman_tree(struct huffman *, unsigned, int);
  231. int
  232. archive_read_support_format_lha(struct archive *_a)
  233. {
  234. struct archive_read *a = (struct archive_read *)_a;
  235. struct lha *lha;
  236. int r;
  237. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  238. ARCHIVE_STATE_NEW, "archive_read_support_format_lha");
  239. lha = (struct lha *)calloc(1, sizeof(*lha));
  240. if (lha == NULL) {
  241. archive_set_error(&a->archive, ENOMEM,
  242. "Can't allocate lha data");
  243. return (ARCHIVE_FATAL);
  244. }
  245. archive_string_init(&lha->ws);
  246. r = __archive_read_register_format(a,
  247. lha,
  248. "lha",
  249. archive_read_format_lha_bid,
  250. archive_read_format_lha_options,
  251. archive_read_format_lha_read_header,
  252. archive_read_format_lha_read_data,
  253. archive_read_format_lha_read_data_skip,
  254. NULL,
  255. archive_read_format_lha_cleanup,
  256. NULL,
  257. NULL);
  258. if (r != ARCHIVE_OK)
  259. free(lha);
  260. return (ARCHIVE_OK);
  261. }
  262. static size_t
  263. lha_check_header_format(const void *h)
  264. {
  265. const unsigned char *p = h;
  266. size_t next_skip_bytes;
  267. switch (p[H_METHOD_OFFSET+3]) {
  268. /*
  269. * "-lh0-" ... "-lh7-" "-lhd-"
  270. * "-lzs-" "-lz5-"
  271. */
  272. case '0': case '1': case '2': case '3':
  273. case '4': case '5': case '6': case '7':
  274. case 'd':
  275. case 's':
  276. next_skip_bytes = 4;
  277. /* b0 == 0 means the end of an LHa archive file. */
  278. if (p[0] == 0)
  279. break;
  280. if (p[H_METHOD_OFFSET] != '-' || p[H_METHOD_OFFSET+1] != 'l'
  281. || p[H_METHOD_OFFSET+4] != '-')
  282. break;
  283. if (p[H_METHOD_OFFSET+2] == 'h') {
  284. /* "-lh?-" */
  285. if (p[H_METHOD_OFFSET+3] == 's')
  286. break;
  287. if (p[H_LEVEL_OFFSET] == 0)
  288. return (0);
  289. if (p[H_LEVEL_OFFSET] <= 3 && p[H_ATTR_OFFSET] == 0x20)
  290. return (0);
  291. }
  292. if (p[H_METHOD_OFFSET+2] == 'z') {
  293. /* LArc extensions: -lzs-,-lz4- and -lz5- */
  294. if (p[H_LEVEL_OFFSET] != 0)
  295. break;
  296. if (p[H_METHOD_OFFSET+3] == 's'
  297. || p[H_METHOD_OFFSET+3] == '4'
  298. || p[H_METHOD_OFFSET+3] == '5')
  299. return (0);
  300. }
  301. break;
  302. case 'h': next_skip_bytes = 1; break;
  303. case 'z': next_skip_bytes = 1; break;
  304. case 'l': next_skip_bytes = 2; break;
  305. case '-': next_skip_bytes = 3; break;
  306. default : next_skip_bytes = 4; break;
  307. }
  308. return (next_skip_bytes);
  309. }
  310. static int
  311. archive_read_format_lha_bid(struct archive_read *a, int best_bid)
  312. {
  313. const char *p;
  314. const void *buff;
  315. ssize_t bytes_avail, offset, window;
  316. size_t next;
  317. /* If there's already a better bid than we can ever
  318. make, don't bother testing. */
  319. if (best_bid > 30)
  320. return (-1);
  321. if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL)
  322. return (-1);
  323. if (lha_check_header_format(p) == 0)
  324. return (30);
  325. if (p[0] == 'M' && p[1] == 'Z') {
  326. /* PE file */
  327. offset = 0;
  328. window = 4096;
  329. while (offset < (1024 * 20)) {
  330. buff = __archive_read_ahead(a, offset + window,
  331. &bytes_avail);
  332. if (buff == NULL) {
  333. /* Remaining bytes are less than window. */
  334. window >>= 1;
  335. if (window < (H_SIZE + 3))
  336. return (0);
  337. continue;
  338. }
  339. p = (const char *)buff + offset;
  340. while (p + H_SIZE < (const char *)buff + bytes_avail) {
  341. if ((next = lha_check_header_format(p)) == 0)
  342. return (30);
  343. p += next;
  344. }
  345. offset = p - (const char *)buff;
  346. }
  347. }
  348. return (0);
  349. }
  350. static int
  351. archive_read_format_lha_options(struct archive_read *a,
  352. const char *key, const char *val)
  353. {
  354. struct lha *lha;
  355. int ret = ARCHIVE_FAILED;
  356. lha = (struct lha *)(a->format->data);
  357. if (strcmp(key, "hdrcharset") == 0) {
  358. if (val == NULL || val[0] == 0)
  359. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  360. "lha: hdrcharset option needs a character-set name");
  361. else {
  362. lha->opt_sconv =
  363. archive_string_conversion_from_charset(
  364. &a->archive, val, 0);
  365. if (lha->opt_sconv != NULL)
  366. ret = ARCHIVE_OK;
  367. else
  368. ret = ARCHIVE_FATAL;
  369. }
  370. return (ret);
  371. }
  372. /* Note: The "warn" return is just to inform the options
  373. * supervisor that we didn't handle it. It will generate
  374. * a suitable error if no one used this option. */
  375. return (ARCHIVE_WARN);
  376. }
  377. static int
  378. lha_skip_sfx(struct archive_read *a)
  379. {
  380. const void *h;
  381. const char *p, *q;
  382. size_t next, skip;
  383. ssize_t bytes, window;
  384. window = 4096;
  385. for (;;) {
  386. h = __archive_read_ahead(a, window, &bytes);
  387. if (h == NULL) {
  388. /* Remaining bytes are less than window. */
  389. window >>= 1;
  390. if (window < (H_SIZE + 3))
  391. goto fatal;
  392. continue;
  393. }
  394. if (bytes < H_SIZE)
  395. goto fatal;
  396. p = h;
  397. q = p + bytes;
  398. /*
  399. * Scan ahead until we find something that looks
  400. * like the lha header.
  401. */
  402. while (p + H_SIZE < q) {
  403. if ((next = lha_check_header_format(p)) == 0) {
  404. skip = p - (const char *)h;
  405. __archive_read_consume(a, skip);
  406. return (ARCHIVE_OK);
  407. }
  408. p += next;
  409. }
  410. skip = p - (const char *)h;
  411. __archive_read_consume(a, skip);
  412. }
  413. fatal:
  414. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  415. "Couldn't find out LHa header");
  416. return (ARCHIVE_FATAL);
  417. }
  418. static int
  419. truncated_error(struct archive_read *a)
  420. {
  421. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  422. "Truncated LHa header");
  423. return (ARCHIVE_FATAL);
  424. }
  425. static int
  426. archive_read_format_lha_read_header(struct archive_read *a,
  427. struct archive_entry *entry)
  428. {
  429. struct archive_string linkname;
  430. struct archive_string pathname;
  431. struct lha *lha;
  432. const unsigned char *p;
  433. const char *signature;
  434. int err;
  435. lha_crc16_init();
  436. a->archive.archive_format = ARCHIVE_FORMAT_LHA;
  437. if (a->archive.archive_format_name == NULL)
  438. a->archive.archive_format_name = "lha";
  439. lha = (struct lha *)(a->format->data);
  440. lha->decompress_init = 0;
  441. lha->end_of_entry = 0;
  442. lha->end_of_entry_cleanup = 0;
  443. lha->entry_unconsumed = 0;
  444. if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL) {
  445. /*
  446. * LHa archiver added 0 to the tail of its archive file as
  447. * the mark of the end of the archive.
  448. */
  449. signature = __archive_read_ahead(a, sizeof(signature[0]), NULL);
  450. if (signature == NULL || signature[0] == 0)
  451. return (ARCHIVE_EOF);
  452. return (truncated_error(a));
  453. }
  454. signature = (const char *)p;
  455. if (lha->found_first_header == 0 &&
  456. signature[0] == 'M' && signature[1] == 'Z') {
  457. /* This is an executable? Must be self-extracting... */
  458. err = lha_skip_sfx(a);
  459. if (err < ARCHIVE_WARN)
  460. return (err);
  461. if ((p = __archive_read_ahead(a, sizeof(*p), NULL)) == NULL)
  462. return (truncated_error(a));
  463. signature = (const char *)p;
  464. }
  465. /* signature[0] == 0 means the end of an LHa archive file. */
  466. if (signature[0] == 0)
  467. return (ARCHIVE_EOF);
  468. /*
  469. * Check the header format and method type.
  470. */
  471. if (lha_check_header_format(p) != 0) {
  472. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  473. "Bad LHa file");
  474. return (ARCHIVE_FATAL);
  475. }
  476. /* We've found the first header. */
  477. lha->found_first_header = 1;
  478. /* Set a default value and common data */
  479. lha->header_size = 0;
  480. lha->level = p[H_LEVEL_OFFSET];
  481. lha->method[0] = p[H_METHOD_OFFSET+1];
  482. lha->method[1] = p[H_METHOD_OFFSET+2];
  483. lha->method[2] = p[H_METHOD_OFFSET+3];
  484. if (memcmp(lha->method, "lhd", 3) == 0)
  485. lha->directory = 1;
  486. else
  487. lha->directory = 0;
  488. if (memcmp(lha->method, "lh0", 3) == 0 ||
  489. memcmp(lha->method, "lz4", 3) == 0)
  490. lha->entry_is_compressed = 0;
  491. else
  492. lha->entry_is_compressed = 1;
  493. lha->compsize = 0;
  494. lha->origsize = 0;
  495. lha->setflag = 0;
  496. lha->birthtime = 0;
  497. lha->birthtime_tv_nsec = 0;
  498. lha->mtime = 0;
  499. lha->mtime_tv_nsec = 0;
  500. lha->atime = 0;
  501. lha->atime_tv_nsec = 0;
  502. lha->mode = (lha->directory)? 0777 : 0666;
  503. lha->uid = 0;
  504. lha->gid = 0;
  505. archive_string_empty(&lha->dirname);
  506. archive_string_empty(&lha->filename);
  507. lha->dos_attr = 0;
  508. if (lha->opt_sconv != NULL)
  509. lha->sconv = lha->opt_sconv;
  510. else
  511. lha->sconv = NULL;
  512. switch (p[H_LEVEL_OFFSET]) {
  513. case 0:
  514. err = lha_read_file_header_0(a, lha);
  515. break;
  516. case 1:
  517. err = lha_read_file_header_1(a, lha);
  518. break;
  519. case 2:
  520. err = lha_read_file_header_2(a, lha);
  521. break;
  522. case 3:
  523. err = lha_read_file_header_3(a, lha);
  524. break;
  525. default:
  526. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  527. "Unsupported LHa header level %d", p[H_LEVEL_OFFSET]);
  528. err = ARCHIVE_FATAL;
  529. break;
  530. }
  531. if (err < ARCHIVE_WARN)
  532. return (err);
  533. if (!lha->directory && archive_strlen(&lha->filename) == 0)
  534. /* The filename has not been set */
  535. return (truncated_error(a));
  536. /*
  537. * Make a pathname from a dirname and a filename.
  538. */
  539. archive_string_concat(&lha->dirname, &lha->filename);
  540. archive_string_init(&pathname);
  541. archive_string_init(&linkname);
  542. archive_string_copy(&pathname, &lha->dirname);
  543. if ((lha->mode & AE_IFMT) == AE_IFLNK) {
  544. /*
  545. * Extract the symlink-name if it's included in the pathname.
  546. */
  547. if (!lha_parse_linkname(&linkname, &pathname)) {
  548. /* We couldn't get the symlink-name. */
  549. archive_set_error(&a->archive,
  550. ARCHIVE_ERRNO_FILE_FORMAT,
  551. "Unknown symlink-name");
  552. archive_string_free(&pathname);
  553. archive_string_free(&linkname);
  554. return (ARCHIVE_FAILED);
  555. }
  556. } else {
  557. /*
  558. * Make sure a file-type is set.
  559. * The mode has been overridden if it is in the extended data.
  560. */
  561. lha->mode = (lha->mode & ~AE_IFMT) |
  562. ((lha->directory)? AE_IFDIR: AE_IFREG);
  563. }
  564. if ((lha->setflag & UNIX_MODE_IS_SET) == 0 &&
  565. (lha->dos_attr & 1) != 0)
  566. lha->mode &= ~(0222);/* read only. */
  567. /*
  568. * Set basic file parameters.
  569. */
  570. if (archive_entry_copy_pathname_l(entry, pathname.s,
  571. pathname.length, lha->sconv) != 0) {
  572. if (errno == ENOMEM) {
  573. archive_set_error(&a->archive, ENOMEM,
  574. "Can't allocate memory for Pathname");
  575. return (ARCHIVE_FATAL);
  576. }
  577. archive_set_error(&a->archive,
  578. ARCHIVE_ERRNO_FILE_FORMAT,
  579. "Pathname cannot be converted "
  580. "from %s to current locale.",
  581. archive_string_conversion_charset_name(lha->sconv));
  582. err = ARCHIVE_WARN;
  583. }
  584. archive_string_free(&pathname);
  585. if (archive_strlen(&linkname) > 0) {
  586. if (archive_entry_copy_symlink_l(entry, linkname.s,
  587. linkname.length, lha->sconv) != 0) {
  588. if (errno == ENOMEM) {
  589. archive_set_error(&a->archive, ENOMEM,
  590. "Can't allocate memory for Linkname");
  591. return (ARCHIVE_FATAL);
  592. }
  593. archive_set_error(&a->archive,
  594. ARCHIVE_ERRNO_FILE_FORMAT,
  595. "Linkname cannot be converted "
  596. "from %s to current locale.",
  597. archive_string_conversion_charset_name(lha->sconv));
  598. err = ARCHIVE_WARN;
  599. }
  600. } else
  601. archive_entry_set_symlink(entry, NULL);
  602. archive_string_free(&linkname);
  603. /*
  604. * When a header level is 0, there is a possibility that
  605. * a pathname and a symlink has '\' character, a directory
  606. * separator in DOS/Windows. So we should convert it to '/'.
  607. */
  608. if (p[H_LEVEL_OFFSET] == 0)
  609. lha_replace_path_separator(lha, entry);
  610. archive_entry_set_mode(entry, lha->mode);
  611. archive_entry_set_uid(entry, lha->uid);
  612. archive_entry_set_gid(entry, lha->gid);
  613. if (archive_strlen(&lha->uname) > 0)
  614. archive_entry_set_uname(entry, lha->uname.s);
  615. if (archive_strlen(&lha->gname) > 0)
  616. archive_entry_set_gname(entry, lha->gname.s);
  617. if (lha->setflag & BIRTHTIME_IS_SET) {
  618. archive_entry_set_birthtime(entry, lha->birthtime,
  619. lha->birthtime_tv_nsec);
  620. archive_entry_set_ctime(entry, lha->birthtime,
  621. lha->birthtime_tv_nsec);
  622. } else {
  623. archive_entry_unset_birthtime(entry);
  624. archive_entry_unset_ctime(entry);
  625. }
  626. archive_entry_set_mtime(entry, lha->mtime, lha->mtime_tv_nsec);
  627. if (lha->setflag & ATIME_IS_SET)
  628. archive_entry_set_atime(entry, lha->atime,
  629. lha->atime_tv_nsec);
  630. else
  631. archive_entry_unset_atime(entry);
  632. if (lha->directory || archive_entry_symlink(entry) != NULL)
  633. archive_entry_unset_size(entry);
  634. else
  635. archive_entry_set_size(entry, lha->origsize);
  636. /*
  637. * Prepare variables used to read a file content.
  638. */
  639. lha->entry_bytes_remaining = lha->compsize;
  640. lha->entry_offset = 0;
  641. lha->entry_crc_calculated = 0;
  642. /*
  643. * This file does not have a content.
  644. */
  645. if (lha->directory || lha->compsize == 0)
  646. lha->end_of_entry = 1;
  647. sprintf(lha->format_name, "lha -%c%c%c-",
  648. lha->method[0], lha->method[1], lha->method[2]);
  649. a->archive.archive_format_name = lha->format_name;
  650. return (err);
  651. }
  652. /*
  653. * Replace a DOS path separator '\' by a character '/'.
  654. * Some multi-byte character set have a character '\' in its second byte.
  655. */
  656. static void
  657. lha_replace_path_separator(struct lha *lha, struct archive_entry *entry)
  658. {
  659. const wchar_t *wp;
  660. size_t i;
  661. if ((wp = archive_entry_pathname_w(entry)) != NULL) {
  662. archive_wstrcpy(&(lha->ws), wp);
  663. for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
  664. if (lha->ws.s[i] == L'\\')
  665. lha->ws.s[i] = L'/';
  666. }
  667. archive_entry_copy_pathname_w(entry, lha->ws.s);
  668. }
  669. if ((wp = archive_entry_symlink_w(entry)) != NULL) {
  670. archive_wstrcpy(&(lha->ws), wp);
  671. for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
  672. if (lha->ws.s[i] == L'\\')
  673. lha->ws.s[i] = L'/';
  674. }
  675. archive_entry_copy_symlink_w(entry, lha->ws.s);
  676. }
  677. }
  678. /*
  679. * Header 0 format
  680. *
  681. * +0 +1 +2 +7 +11
  682. * +---------------+----------+----------------+-------------------+
  683. * |header size(*1)|header sum|compression type|compressed size(*2)|
  684. * +---------------+----------+----------------+-------------------+
  685. * <---------------------(*1)----------*
  686. *
  687. * +11 +15 +17 +19 +20 +21
  688. * +-----------------+---------+---------+--------------+----------------+
  689. * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=0)|
  690. * +-----------------+---------+---------+--------------+----------------+
  691. * *--------------------------------(*1)---------------------------------*
  692. *
  693. * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+2+(*4)
  694. * +---------------+---------+----------+----------------+------------------+
  695. * |name length(*3)|file name|file CRC16|extra header(*4)| compressed data |
  696. * +---------------+---------+----------+----------------+------------------+
  697. * <--(*3)-> <------(*2)------>
  698. * *----------------------(*1)-------------------------->
  699. *
  700. */
  701. #define H0_HEADER_SIZE_OFFSET 0
  702. #define H0_HEADER_SUM_OFFSET 1
  703. #define H0_COMP_SIZE_OFFSET 7
  704. #define H0_ORIG_SIZE_OFFSET 11
  705. #define H0_DOS_TIME_OFFSET 15
  706. #define H0_NAME_LEN_OFFSET 21
  707. #define H0_FILE_NAME_OFFSET 22
  708. #define H0_FIXED_SIZE 24
  709. static int
  710. lha_read_file_header_0(struct archive_read *a, struct lha *lha)
  711. {
  712. const unsigned char *p;
  713. int extdsize, namelen;
  714. unsigned char headersum, sum_calculated;
  715. if ((p = __archive_read_ahead(a, H0_FIXED_SIZE, NULL)) == NULL)
  716. return (truncated_error(a));
  717. lha->header_size = p[H0_HEADER_SIZE_OFFSET] + 2;
  718. headersum = p[H0_HEADER_SUM_OFFSET];
  719. lha->compsize = archive_le32dec(p + H0_COMP_SIZE_OFFSET);
  720. lha->origsize = archive_le32dec(p + H0_ORIG_SIZE_OFFSET);
  721. lha->mtime = lha_dos_time(p + H0_DOS_TIME_OFFSET);
  722. namelen = p[H0_NAME_LEN_OFFSET];
  723. extdsize = (int)lha->header_size - H0_FIXED_SIZE - namelen;
  724. if ((namelen > 221 || extdsize < 0) && extdsize != -2) {
  725. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  726. "Invalid LHa header");
  727. return (ARCHIVE_FATAL);
  728. }
  729. if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
  730. return (truncated_error(a));
  731. archive_strncpy(&lha->filename, p + H0_FILE_NAME_OFFSET, namelen);
  732. /* When extdsize == -2, A CRC16 value is not present in the header. */
  733. if (extdsize >= 0) {
  734. lha->crc = archive_le16dec(p + H0_FILE_NAME_OFFSET + namelen);
  735. lha->setflag |= CRC_IS_SET;
  736. }
  737. sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
  738. /* Read an extended header */
  739. if (extdsize > 0) {
  740. /* This extended data is set by 'LHa for UNIX' only.
  741. * Maybe fixed size.
  742. */
  743. p += H0_FILE_NAME_OFFSET + namelen + 2;
  744. if (p[0] == 'U' && extdsize == 12) {
  745. /* p[1] is a minor version. */
  746. lha->mtime = archive_le32dec(&p[2]);
  747. lha->mode = archive_le16dec(&p[6]);
  748. lha->uid = archive_le16dec(&p[8]);
  749. lha->gid = archive_le16dec(&p[10]);
  750. lha->setflag |= UNIX_MODE_IS_SET;
  751. }
  752. }
  753. __archive_read_consume(a, lha->header_size);
  754. if (sum_calculated != headersum) {
  755. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  756. "LHa header sum error");
  757. return (ARCHIVE_FATAL);
  758. }
  759. return (ARCHIVE_OK);
  760. }
  761. /*
  762. * Header 1 format
  763. *
  764. * +0 +1 +2 +7 +11
  765. * +---------------+----------+----------------+-------------+
  766. * |header size(*1)|header sum|compression type|skip size(*2)|
  767. * +---------------+----------+----------------+-------------+
  768. * <---------------(*1)----------*
  769. *
  770. * +11 +15 +17 +19 +20 +21
  771. * +-----------------+---------+---------+--------------+----------------+
  772. * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=1)|
  773. * +-----------------+---------+---------+--------------+----------------+
  774. * *-------------------------------(*1)----------------------------------*
  775. *
  776. * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+3 +22+(*3)+3+(*4)
  777. * +---------------+---------+----------+-----------+-----------+
  778. * |name length(*3)|file name|file CRC16| creator |padding(*4)|
  779. * +---------------+---------+----------+-----------+-----------+
  780. * <--(*3)->
  781. * *----------------------------(*1)----------------------------*
  782. *
  783. * +22+(*3)+3+(*4) +22+(*3)+3+(*4)+2 +22+(*3)+3+(*4)+2+(*5)
  784. * +----------------+---------------------+------------------------+
  785. * |next header size| extended header(*5) | compressed data |
  786. * +----------------+---------------------+------------------------+
  787. * *------(*1)-----> <--------------------(*2)-------------------->
  788. */
  789. #define H1_HEADER_SIZE_OFFSET 0
  790. #define H1_HEADER_SUM_OFFSET 1
  791. #define H1_COMP_SIZE_OFFSET 7
  792. #define H1_ORIG_SIZE_OFFSET 11
  793. #define H1_DOS_TIME_OFFSET 15
  794. #define H1_NAME_LEN_OFFSET 21
  795. #define H1_FILE_NAME_OFFSET 22
  796. #define H1_FIXED_SIZE 27
  797. static int
  798. lha_read_file_header_1(struct archive_read *a, struct lha *lha)
  799. {
  800. const unsigned char *p;
  801. size_t extdsize;
  802. int i, err, err2;
  803. int namelen, padding;
  804. unsigned char headersum, sum_calculated;
  805. err = ARCHIVE_OK;
  806. if ((p = __archive_read_ahead(a, H1_FIXED_SIZE, NULL)) == NULL)
  807. return (truncated_error(a));
  808. lha->header_size = p[H1_HEADER_SIZE_OFFSET] + 2;
  809. headersum = p[H1_HEADER_SUM_OFFSET];
  810. /* Note: An extended header size is included in a compsize. */
  811. lha->compsize = archive_le32dec(p + H1_COMP_SIZE_OFFSET);
  812. lha->origsize = archive_le32dec(p + H1_ORIG_SIZE_OFFSET);
  813. lha->mtime = lha_dos_time(p + H1_DOS_TIME_OFFSET);
  814. namelen = p[H1_NAME_LEN_OFFSET];
  815. /* Calculate a padding size. The result will be normally 0 only(?) */
  816. padding = ((int)lha->header_size) - H1_FIXED_SIZE - namelen;
  817. if (namelen > 230 || padding < 0)
  818. goto invalid;
  819. if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
  820. return (truncated_error(a));
  821. for (i = 0; i < namelen; i++) {
  822. if (p[i + H1_FILE_NAME_OFFSET] == 0xff)
  823. goto invalid;/* Invalid filename. */
  824. }
  825. archive_strncpy(&lha->filename, p + H1_FILE_NAME_OFFSET, namelen);
  826. lha->crc = archive_le16dec(p + H1_FILE_NAME_OFFSET + namelen);
  827. lha->setflag |= CRC_IS_SET;
  828. sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
  829. /* Consume used bytes but not include `next header size' data
  830. * since it will be consumed in lha_read_file_extended_header(). */
  831. __archive_read_consume(a, lha->header_size - 2);
  832. /* Read extended headers */
  833. err2 = lha_read_file_extended_header(a, lha, NULL, 2,
  834. (size_t)(lha->compsize + 2), &extdsize);
  835. if (err2 < ARCHIVE_WARN)
  836. return (err2);
  837. if (err2 < err)
  838. err = err2;
  839. /* Get a real compressed file size. */
  840. lha->compsize -= extdsize - 2;
  841. if (lha->compsize < 0)
  842. goto invalid; /* Invalid compressed file size */
  843. if (sum_calculated != headersum) {
  844. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  845. "LHa header sum error");
  846. return (ARCHIVE_FATAL);
  847. }
  848. return (err);
  849. invalid:
  850. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  851. "Invalid LHa header");
  852. return (ARCHIVE_FATAL);
  853. }
  854. /*
  855. * Header 2 format
  856. *
  857. * +0 +2 +7 +11 +15
  858. * +---------------+----------------+-------------------+-----------------+
  859. * |header size(*1)|compression type|compressed size(*2)|uncompressed size|
  860. * +---------------+----------------+-------------------+-----------------+
  861. * <--------------------------------(*1)---------------------------------*
  862. *
  863. * +15 +19 +20 +21 +23 +24
  864. * +-----------------+------------+----------------+----------+-----------+
  865. * |data/time(time_t)| 0x20 fixed |header level(=2)|file CRC16| creator |
  866. * +-----------------+------------+----------------+----------+-----------+
  867. * *---------------------------------(*1)---------------------------------*
  868. *
  869. * +24 +26 +26+(*3) +26+(*3)+(*4)
  870. * +----------------+-------------------+-------------+-------------------+
  871. * |next header size|extended header(*3)| padding(*4) | compressed data |
  872. * +----------------+-------------------+-------------+-------------------+
  873. * *--------------------------(*1)-------------------> <------(*2)------->
  874. *
  875. */
  876. #define H2_HEADER_SIZE_OFFSET 0
  877. #define H2_COMP_SIZE_OFFSET 7
  878. #define H2_ORIG_SIZE_OFFSET 11
  879. #define H2_TIME_OFFSET 15
  880. #define H2_CRC_OFFSET 21
  881. #define H2_FIXED_SIZE 24
  882. static int
  883. lha_read_file_header_2(struct archive_read *a, struct lha *lha)
  884. {
  885. const unsigned char *p;
  886. size_t extdsize;
  887. int err, padding;
  888. uint16_t header_crc;
  889. if ((p = __archive_read_ahead(a, H2_FIXED_SIZE, NULL)) == NULL)
  890. return (truncated_error(a));
  891. lha->header_size =archive_le16dec(p + H2_HEADER_SIZE_OFFSET);
  892. lha->compsize = archive_le32dec(p + H2_COMP_SIZE_OFFSET);
  893. lha->origsize = archive_le32dec(p + H2_ORIG_SIZE_OFFSET);
  894. lha->mtime = archive_le32dec(p + H2_TIME_OFFSET);
  895. lha->crc = archive_le16dec(p + H2_CRC_OFFSET);
  896. lha->setflag |= CRC_IS_SET;
  897. if (lha->header_size < H2_FIXED_SIZE) {
  898. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  899. "Invalid LHa header size");
  900. return (ARCHIVE_FATAL);
  901. }
  902. header_crc = lha_crc16(0, p, H2_FIXED_SIZE);
  903. __archive_read_consume(a, H2_FIXED_SIZE);
  904. /* Read extended headers */
  905. err = lha_read_file_extended_header(a, lha, &header_crc, 2,
  906. lha->header_size - H2_FIXED_SIZE, &extdsize);
  907. if (err < ARCHIVE_WARN)
  908. return (err);
  909. /* Calculate a padding size. The result will be normally 0 or 1. */
  910. padding = (int)lha->header_size - (int)(H2_FIXED_SIZE + extdsize);
  911. if (padding > 0) {
  912. if ((p = __archive_read_ahead(a, padding, NULL)) == NULL)
  913. return (truncated_error(a));
  914. header_crc = lha_crc16(header_crc, p, padding);
  915. __archive_read_consume(a, padding);
  916. }
  917. if (header_crc != lha->header_crc) {
  918. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  919. "LHa header CRC error");
  920. return (ARCHIVE_FATAL);
  921. }
  922. return (err);
  923. }
  924. /*
  925. * Header 3 format
  926. *
  927. * +0 +2 +7 +11 +15
  928. * +------------+----------------+-------------------+-----------------+
  929. * | 0x04 fixed |compression type|compressed size(*2)|uncompressed size|
  930. * +------------+----------------+-------------------+-----------------+
  931. * <-------------------------------(*1)-------------------------------*
  932. *
  933. * +15 +19 +20 +21 +23 +24
  934. * +-----------------+------------+----------------+----------+-----------+
  935. * |date/time(time_t)| 0x20 fixed |header level(=3)|file CRC16| creator |
  936. * +-----------------+------------+----------------+----------+-----------+
  937. * *--------------------------------(*1)----------------------------------*
  938. *
  939. * +24 +28 +32 +32+(*3)
  940. * +---------------+----------------+-------------------+-----------------+
  941. * |header size(*1)|next header size|extended header(*3)| compressed data |
  942. * +---------------+----------------+-------------------+-----------------+
  943. * *------------------------(*1)-----------------------> <------(*2)----->
  944. *
  945. */
  946. #define H3_FIELD_LEN_OFFSET 0
  947. #define H3_COMP_SIZE_OFFSET 7
  948. #define H3_ORIG_SIZE_OFFSET 11
  949. #define H3_TIME_OFFSET 15
  950. #define H3_CRC_OFFSET 21
  951. #define H3_HEADER_SIZE_OFFSET 24
  952. #define H3_FIXED_SIZE 28
  953. static int
  954. lha_read_file_header_3(struct archive_read *a, struct lha *lha)
  955. {
  956. const unsigned char *p;
  957. size_t extdsize;
  958. int err;
  959. uint16_t header_crc;
  960. if ((p = __archive_read_ahead(a, H3_FIXED_SIZE, NULL)) == NULL)
  961. return (truncated_error(a));
  962. if (archive_le16dec(p + H3_FIELD_LEN_OFFSET) != 4)
  963. goto invalid;
  964. lha->header_size =archive_le32dec(p + H3_HEADER_SIZE_OFFSET);
  965. lha->compsize = archive_le32dec(p + H3_COMP_SIZE_OFFSET);
  966. lha->origsize = archive_le32dec(p + H3_ORIG_SIZE_OFFSET);
  967. lha->mtime = archive_le32dec(p + H3_TIME_OFFSET);
  968. lha->crc = archive_le16dec(p + H3_CRC_OFFSET);
  969. lha->setflag |= CRC_IS_SET;
  970. if (lha->header_size < H3_FIXED_SIZE + 4)
  971. goto invalid;
  972. header_crc = lha_crc16(0, p, H3_FIXED_SIZE);
  973. __archive_read_consume(a, H3_FIXED_SIZE);
  974. /* Read extended headers */
  975. err = lha_read_file_extended_header(a, lha, &header_crc, 4,
  976. lha->header_size - H3_FIXED_SIZE, &extdsize);
  977. if (err < ARCHIVE_WARN)
  978. return (err);
  979. if (header_crc != lha->header_crc) {
  980. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  981. "LHa header CRC error");
  982. return (ARCHIVE_FATAL);
  983. }
  984. return (err);
  985. invalid:
  986. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  987. "Invalid LHa header");
  988. return (ARCHIVE_FATAL);
  989. }
  990. /*
  991. * Extended header format
  992. *
  993. * +0 +2 +3 -- used in header 1 and 2
  994. * +0 +4 +5 -- used in header 3
  995. * +--------------+---------+-------------------+--------------+--
  996. * |ex-header size|header id| data |ex-header size| .......
  997. * +--------------+---------+-------------------+--------------+--
  998. * <-------------( ex-header size)------------> <-- next extended header --*
  999. *
  1000. * If the ex-header size is zero, it is the make of the end of extended
  1001. * headers.
  1002. *
  1003. */
  1004. static int
  1005. lha_read_file_extended_header(struct archive_read *a, struct lha *lha,
  1006. uint16_t *crc, int sizefield_length, size_t limitsize, size_t *total_size)
  1007. {
  1008. const void *h;
  1009. const unsigned char *extdheader;
  1010. size_t extdsize;
  1011. size_t datasize;
  1012. unsigned int i;
  1013. unsigned char extdtype;
  1014. #define EXT_HEADER_CRC 0x00 /* Header CRC and information*/
  1015. #define EXT_FILENAME 0x01 /* Filename */
  1016. #define EXT_DIRECTORY 0x02 /* Directory name */
  1017. #define EXT_DOS_ATTR 0x40 /* MS-DOS attribute */
  1018. #define EXT_TIMESTAMP 0x41 /* Windows time stamp */
  1019. #define EXT_FILESIZE 0x42 /* Large file size */
  1020. #define EXT_TIMEZONE 0x43 /* Time zone */
  1021. #define EXT_UTF16_FILENAME 0x44 /* UTF-16 filename */
  1022. #define EXT_UTF16_DIRECTORY 0x45 /* UTF-16 directory name */
  1023. #define EXT_CODEPAGE 0x46 /* Codepage */
  1024. #define EXT_UNIX_MODE 0x50 /* File permission */
  1025. #define EXT_UNIX_GID_UID 0x51 /* gid,uid */
  1026. #define EXT_UNIX_GNAME 0x52 /* Group name */
  1027. #define EXT_UNIX_UNAME 0x53 /* User name */
  1028. #define EXT_UNIX_MTIME 0x54 /* Modified time */
  1029. #define EXT_OS2_NEW_ATTR 0x7f /* new attribute(OS/2 only) */
  1030. #define EXT_NEW_ATTR 0xff /* new attribute */
  1031. *total_size = sizefield_length;
  1032. for (;;) {
  1033. /* Read an extended header size. */
  1034. if ((h =
  1035. __archive_read_ahead(a, sizefield_length, NULL)) == NULL)
  1036. return (truncated_error(a));
  1037. /* Check if the size is the zero indicates the end of the
  1038. * extended header. */
  1039. if (sizefield_length == sizeof(uint16_t))
  1040. extdsize = archive_le16dec(h);
  1041. else
  1042. extdsize = archive_le32dec(h);
  1043. if (extdsize == 0) {
  1044. /* End of extended header */
  1045. if (crc != NULL)
  1046. *crc = lha_crc16(*crc, h, sizefield_length);
  1047. __archive_read_consume(a, sizefield_length);
  1048. return (ARCHIVE_OK);
  1049. }
  1050. /* Sanity check to the extended header size. */
  1051. if (((uint64_t)*total_size + extdsize) >
  1052. (uint64_t)limitsize ||
  1053. extdsize <= (size_t)sizefield_length)
  1054. goto invalid;
  1055. /* Read the extended header. */
  1056. if ((h = __archive_read_ahead(a, extdsize, NULL)) == NULL)
  1057. return (truncated_error(a));
  1058. *total_size += extdsize;
  1059. extdheader = (const unsigned char *)h;
  1060. /* Get the extended header type. */
  1061. extdtype = extdheader[sizefield_length];
  1062. /* Calculate an extended data size. */
  1063. datasize = extdsize - (1 + sizefield_length);
  1064. /* Skip an extended header size field and type field. */
  1065. extdheader += sizefield_length + 1;
  1066. if (crc != NULL && extdtype != EXT_HEADER_CRC)
  1067. *crc = lha_crc16(*crc, h, extdsize);
  1068. switch (extdtype) {
  1069. case EXT_HEADER_CRC:
  1070. /* We only use a header CRC. Following data will not
  1071. * be used. */
  1072. if (datasize >= 2) {
  1073. lha->header_crc = archive_le16dec(extdheader);
  1074. if (crc != NULL) {
  1075. static const char zeros[2] = {0, 0};
  1076. *crc = lha_crc16(*crc, h,
  1077. extdsize - datasize);
  1078. /* CRC value itself as zero */
  1079. *crc = lha_crc16(*crc, zeros, 2);
  1080. *crc = lha_crc16(*crc,
  1081. extdheader+2, datasize - 2);
  1082. }
  1083. }
  1084. break;
  1085. case EXT_FILENAME:
  1086. if (datasize == 0) {
  1087. /* maybe directory header */
  1088. archive_string_empty(&lha->filename);
  1089. break;
  1090. }
  1091. if (extdheader[0] == '\0')
  1092. goto invalid;
  1093. archive_strncpy(&lha->filename,
  1094. (const char *)extdheader, datasize);
  1095. break;
  1096. case EXT_DIRECTORY:
  1097. if (datasize == 0 || extdheader[0] == '\0')
  1098. /* no directory name data. exit this case. */
  1099. goto invalid;
  1100. archive_strncpy(&lha->dirname,
  1101. (const char *)extdheader, datasize);
  1102. /*
  1103. * Convert directory delimiter from 0xFF
  1104. * to '/' for local system.
  1105. */
  1106. for (i = 0; i < lha->dirname.length; i++) {
  1107. if ((unsigned char)lha->dirname.s[i] == 0xFF)
  1108. lha->dirname.s[i] = '/';
  1109. }
  1110. /* Is last character directory separator? */
  1111. if (lha->dirname.s[lha->dirname.length-1] != '/')
  1112. /* invalid directory data */
  1113. goto invalid;
  1114. break;
  1115. case EXT_DOS_ATTR:
  1116. if (datasize == 2)
  1117. lha->dos_attr = (unsigned char)
  1118. (archive_le16dec(extdheader) & 0xff);
  1119. break;
  1120. case EXT_TIMESTAMP:
  1121. if (datasize == (sizeof(uint64_t) * 3)) {
  1122. lha->birthtime = lha_win_time(
  1123. archive_le64dec(extdheader),
  1124. &lha->birthtime_tv_nsec);
  1125. extdheader += sizeof(uint64_t);
  1126. lha->mtime = lha_win_time(
  1127. archive_le64dec(extdheader),
  1128. &lha->mtime_tv_nsec);
  1129. extdheader += sizeof(uint64_t);
  1130. lha->atime = lha_win_time(
  1131. archive_le64dec(extdheader),
  1132. &lha->atime_tv_nsec);
  1133. lha->setflag |= BIRTHTIME_IS_SET |
  1134. ATIME_IS_SET;
  1135. }
  1136. break;
  1137. case EXT_FILESIZE:
  1138. if (datasize == sizeof(uint64_t) * 2) {
  1139. lha->compsize = archive_le64dec(extdheader);
  1140. extdheader += sizeof(uint64_t);
  1141. lha->origsize = archive_le64dec(extdheader);
  1142. }
  1143. break;
  1144. case EXT_CODEPAGE:
  1145. /* Get an archived filename charset from codepage.
  1146. * This overwrites the charset specified by
  1147. * hdrcharset option. */
  1148. if (datasize == sizeof(uint32_t)) {
  1149. struct archive_string cp;
  1150. const char *charset;
  1151. archive_string_init(&cp);
  1152. switch (archive_le32dec(extdheader)) {
  1153. case 65001: /* UTF-8 */
  1154. charset = "UTF-8";
  1155. break;
  1156. default:
  1157. archive_string_sprintf(&cp, "CP%d",
  1158. (int)archive_le32dec(extdheader));
  1159. charset = cp.s;
  1160. break;
  1161. }
  1162. lha->sconv =
  1163. archive_string_conversion_from_charset(
  1164. &(a->archive), charset, 1);
  1165. archive_string_free(&cp);
  1166. if (lha->sconv == NULL)
  1167. return (ARCHIVE_FATAL);
  1168. }
  1169. break;
  1170. case EXT_UNIX_MODE:
  1171. if (datasize == sizeof(uint16_t)) {
  1172. lha->mode = archive_le16dec(extdheader);
  1173. lha->setflag |= UNIX_MODE_IS_SET;
  1174. }
  1175. break;
  1176. case EXT_UNIX_GID_UID:
  1177. if (datasize == (sizeof(uint16_t) * 2)) {
  1178. lha->gid = archive_le16dec(extdheader);
  1179. lha->uid = archive_le16dec(extdheader+2);
  1180. }
  1181. break;
  1182. case EXT_UNIX_GNAME:
  1183. if (datasize > 0)
  1184. archive_strncpy(&lha->gname,
  1185. (const char *)extdheader, datasize);
  1186. break;
  1187. case EXT_UNIX_UNAME:
  1188. if (datasize > 0)
  1189. archive_strncpy(&lha->uname,
  1190. (const char *)extdheader, datasize);
  1191. break;
  1192. case EXT_UNIX_MTIME:
  1193. if (datasize == sizeof(uint32_t))
  1194. lha->mtime = archive_le32dec(extdheader);
  1195. break;
  1196. case EXT_OS2_NEW_ATTR:
  1197. /* This extended header is OS/2 depend. */
  1198. if (datasize == 16) {
  1199. lha->dos_attr = (unsigned char)
  1200. (archive_le16dec(extdheader) & 0xff);
  1201. lha->mode = archive_le16dec(extdheader+2);
  1202. lha->gid = archive_le16dec(extdheader+4);
  1203. lha->uid = archive_le16dec(extdheader+6);
  1204. lha->birthtime = archive_le32dec(extdheader+8);
  1205. lha->atime = archive_le32dec(extdheader+12);
  1206. lha->setflag |= UNIX_MODE_IS_SET
  1207. | BIRTHTIME_IS_SET | ATIME_IS_SET;
  1208. }
  1209. break;
  1210. case EXT_NEW_ATTR:
  1211. if (datasize == 20) {
  1212. lha->mode = (mode_t)archive_le32dec(extdheader);
  1213. lha->gid = archive_le32dec(extdheader+4);
  1214. lha->uid = archive_le32dec(extdheader+8);
  1215. lha->birthtime = archive_le32dec(extdheader+12);
  1216. lha->atime = archive_le32dec(extdheader+16);
  1217. lha->setflag |= UNIX_MODE_IS_SET
  1218. | BIRTHTIME_IS_SET | ATIME_IS_SET;
  1219. }
  1220. break;
  1221. case EXT_TIMEZONE: /* Not supported */
  1222. case EXT_UTF16_FILENAME: /* Not supported */
  1223. case EXT_UTF16_DIRECTORY: /* Not supported */
  1224. default:
  1225. break;
  1226. }
  1227. __archive_read_consume(a, extdsize);
  1228. }
  1229. invalid:
  1230. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1231. "Invalid extended LHa header");
  1232. return (ARCHIVE_FATAL);
  1233. }
  1234. static int
  1235. lha_end_of_entry(struct archive_read *a)
  1236. {
  1237. struct lha *lha = (struct lha *)(a->format->data);
  1238. int r = ARCHIVE_EOF;
  1239. if (!lha->end_of_entry_cleanup) {
  1240. if ((lha->setflag & CRC_IS_SET) &&
  1241. lha->crc != lha->entry_crc_calculated) {
  1242. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1243. "LHa data CRC error");
  1244. r = ARCHIVE_WARN;
  1245. }
  1246. /* End-of-entry cleanup done. */
  1247. lha->end_of_entry_cleanup = 1;
  1248. }
  1249. return (r);
  1250. }
  1251. static int
  1252. archive_read_format_lha_read_data(struct archive_read *a,
  1253. const void **buff, size_t *size, int64_t *offset)
  1254. {
  1255. struct lha *lha = (struct lha *)(a->format->data);
  1256. int r;
  1257. if (lha->entry_unconsumed) {
  1258. /* Consume as much as the decompressor actually used. */
  1259. __archive_read_consume(a, lha->entry_unconsumed);
  1260. lha->entry_unconsumed = 0;
  1261. }
  1262. if (lha->end_of_entry) {
  1263. *offset = lha->entry_offset;
  1264. *size = 0;
  1265. *buff = NULL;
  1266. return (lha_end_of_entry(a));
  1267. }
  1268. if (lha->entry_is_compressed)
  1269. r = lha_read_data_lzh(a, buff, size, offset);
  1270. else
  1271. /* No compression. */
  1272. r = lha_read_data_none(a, buff, size, offset);
  1273. return (r);
  1274. }
  1275. /*
  1276. * Read a file content in no compression.
  1277. *
  1278. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  1279. * lha->end_of_entry if it consumes all of the data.
  1280. */
  1281. static int
  1282. lha_read_data_none(struct archive_read *a, const void **buff,
  1283. size_t *size, int64_t *offset)
  1284. {
  1285. struct lha *lha = (struct lha *)(a->format->data);
  1286. ssize_t bytes_avail;
  1287. if (lha->entry_bytes_remaining == 0) {
  1288. *buff = NULL;
  1289. *size = 0;
  1290. *offset = lha->entry_offset;
  1291. lha->end_of_entry = 1;
  1292. return (ARCHIVE_OK);
  1293. }
  1294. /*
  1295. * Note: '1' here is a performance optimization.
  1296. * Recall that the decompression layer returns a count of
  1297. * available bytes; asking for more than that forces the
  1298. * decompressor to combine reads by copying data.
  1299. */
  1300. *buff = __archive_read_ahead(a, 1, &bytes_avail);
  1301. if (bytes_avail <= 0) {
  1302. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1303. "Truncated LHa file data");
  1304. return (ARCHIVE_FATAL);
  1305. }
  1306. if (bytes_avail > lha->entry_bytes_remaining)
  1307. bytes_avail = (ssize_t)lha->entry_bytes_remaining;
  1308. lha->entry_crc_calculated =
  1309. lha_crc16(lha->entry_crc_calculated, *buff, bytes_avail);
  1310. *size = bytes_avail;
  1311. *offset = lha->entry_offset;
  1312. lha->entry_offset += bytes_avail;
  1313. lha->entry_bytes_remaining -= bytes_avail;
  1314. if (lha->entry_bytes_remaining == 0)
  1315. lha->end_of_entry = 1;
  1316. lha->entry_unconsumed = bytes_avail;
  1317. return (ARCHIVE_OK);
  1318. }
  1319. /*
  1320. * Read a file content in LZHUFF encoding.
  1321. *
  1322. * Returns ARCHIVE_OK if successful, returns ARCHIVE_WARN if compression is
  1323. * unsupported, ARCHIVE_FATAL otherwise, sets lha->end_of_entry if it consumes
  1324. * all of the data.
  1325. */
  1326. static int
  1327. lha_read_data_lzh(struct archive_read *a, const void **buff,
  1328. size_t *size, int64_t *offset)
  1329. {
  1330. struct lha *lha = (struct lha *)(a->format->data);
  1331. ssize_t bytes_avail;
  1332. int r;
  1333. /* If we haven't yet read any data, initialize the decompressor. */
  1334. if (!lha->decompress_init) {
  1335. r = lzh_decode_init(&(lha->strm), lha->method);
  1336. switch (r) {
  1337. case ARCHIVE_OK:
  1338. break;
  1339. case ARCHIVE_FAILED:
  1340. /* Unsupported compression. */
  1341. *buff = NULL;
  1342. *size = 0;
  1343. *offset = 0;
  1344. archive_set_error(&a->archive,
  1345. ARCHIVE_ERRNO_FILE_FORMAT,
  1346. "Unsupported lzh compression method -%c%c%c-",
  1347. lha->method[0], lha->method[1], lha->method[2]);
  1348. /* We know compressed size; just skip it. */
  1349. archive_read_format_lha_read_data_skip(a);
  1350. return (ARCHIVE_WARN);
  1351. default:
  1352. archive_set_error(&a->archive, ENOMEM,
  1353. "Couldn't allocate memory "
  1354. "for lzh decompression");
  1355. return (ARCHIVE_FATAL);
  1356. }
  1357. /* We've initialized decompression for this stream. */
  1358. lha->decompress_init = 1;
  1359. lha->strm.avail_out = 0;
  1360. lha->strm.total_out = 0;
  1361. }
  1362. /*
  1363. * Note: '1' here is a performance optimization.
  1364. * Recall that the decompression layer returns a count of
  1365. * available bytes; asking for more than that forces the
  1366. * decompressor to combine reads by copying data.
  1367. */
  1368. lha->strm.next_in = __archive_read_ahead(a, 1, &bytes_avail);
  1369. if (bytes_avail <= 0) {
  1370. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1371. "Truncated LHa file body");
  1372. return (ARCHIVE_FATAL);
  1373. }
  1374. if (bytes_avail > lha->entry_bytes_remaining)
  1375. bytes_avail = (ssize_t)lha->entry_bytes_remaining;
  1376. lha->strm.avail_in = (int)bytes_avail;
  1377. lha->strm.total_in = 0;
  1378. lha->strm.avail_out = 0;
  1379. r = lzh_decode(&(lha->strm), bytes_avail == lha->entry_bytes_remaining);
  1380. switch (r) {
  1381. case ARCHIVE_OK:
  1382. break;
  1383. case ARCHIVE_EOF:
  1384. lha->end_of_entry = 1;
  1385. break;
  1386. default:
  1387. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1388. "Bad lzh data");
  1389. return (ARCHIVE_FAILED);
  1390. }
  1391. lha->entry_unconsumed = lha->strm.total_in;
  1392. lha->entry_bytes_remaining -= lha->strm.total_in;
  1393. if (lha->strm.avail_out) {
  1394. *offset = lha->entry_offset;
  1395. *size = lha->strm.avail_out;
  1396. *buff = lha->strm.ref_ptr;
  1397. lha->entry_crc_calculated =
  1398. lha_crc16(lha->entry_crc_calculated, *buff, *size);
  1399. lha->entry_offset += *size;
  1400. } else {
  1401. *offset = lha->entry_offset;
  1402. *size = 0;
  1403. *buff = NULL;
  1404. if (lha->end_of_entry)
  1405. return (lha_end_of_entry(a));
  1406. }
  1407. return (ARCHIVE_OK);
  1408. }
  1409. /*
  1410. * Skip a file content.
  1411. */
  1412. static int
  1413. archive_read_format_lha_read_data_skip(struct archive_read *a)
  1414. {
  1415. struct lha *lha;
  1416. int64_t bytes_skipped;
  1417. lha = (struct lha *)(a->format->data);
  1418. if (lha->entry_unconsumed) {
  1419. /* Consume as much as the decompressor actually used. */
  1420. __archive_read_consume(a, lha->entry_unconsumed);
  1421. lha->entry_unconsumed = 0;
  1422. }
  1423. /* if we've already read to end of data, we're done. */
  1424. if (lha->end_of_entry_cleanup)
  1425. return (ARCHIVE_OK);
  1426. /*
  1427. * If the length is at the beginning, we can skip the
  1428. * compressed data much more quickly.
  1429. */
  1430. bytes_skipped = __archive_read_consume(a, lha->entry_bytes_remaining);
  1431. if (bytes_skipped < 0)
  1432. return (ARCHIVE_FATAL);
  1433. /* This entry is finished and done. */
  1434. lha->end_of_entry_cleanup = lha->end_of_entry = 1;
  1435. return (ARCHIVE_OK);
  1436. }
  1437. static int
  1438. archive_read_format_lha_cleanup(struct archive_read *a)
  1439. {
  1440. struct lha *lha = (struct lha *)(a->format->data);
  1441. lzh_decode_free(&(lha->strm));
  1442. archive_string_free(&(lha->dirname));
  1443. archive_string_free(&(lha->filename));
  1444. archive_string_free(&(lha->uname));
  1445. archive_string_free(&(lha->gname));
  1446. archive_wstring_free(&(lha->ws));
  1447. free(lha);
  1448. (a->format->data) = NULL;
  1449. return (ARCHIVE_OK);
  1450. }
  1451. /*
  1452. * 'LHa for UNIX' utility has archived a symbolic-link name after
  1453. * a pathname with '|' character.
  1454. * This function extracts the symbolic-link name from the pathname.
  1455. *
  1456. * example.
  1457. * 1. a symbolic-name is 'aaa/bb/cc'
  1458. * 2. a filename is 'xxx/bbb'
  1459. * then a archived pathname is 'xxx/bbb|aaa/bb/cc'
  1460. */
  1461. static int
  1462. lha_parse_linkname(struct archive_string *linkname,
  1463. struct archive_string *pathname)
  1464. {
  1465. char * linkptr;
  1466. size_t symlen;
  1467. linkptr = strchr(pathname->s, '|');
  1468. if (linkptr != NULL) {
  1469. symlen = strlen(linkptr + 1);
  1470. archive_strncpy(linkname, linkptr+1, symlen);
  1471. *linkptr = 0;
  1472. pathname->length = strlen(pathname->s);
  1473. return (1);
  1474. }
  1475. return (0);
  1476. }
  1477. /* Convert an MSDOS-style date/time into Unix-style time. */
  1478. static time_t
  1479. lha_dos_time(const unsigned char *p)
  1480. {
  1481. int msTime, msDate;
  1482. struct tm ts;
  1483. msTime = archive_le16dec(p);
  1484. msDate = archive_le16dec(p+2);
  1485. memset(&ts, 0, sizeof(ts));
  1486. ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
  1487. ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
  1488. ts.tm_mday = msDate & 0x1f; /* Day of month. */
  1489. ts.tm_hour = (msTime >> 11) & 0x1f;
  1490. ts.tm_min = (msTime >> 5) & 0x3f;
  1491. ts.tm_sec = (msTime << 1) & 0x3e;
  1492. ts.tm_isdst = -1;
  1493. return (mktime(&ts));
  1494. }
  1495. /* Convert an MS-Windows-style date/time into Unix-style time. */
  1496. static time_t
  1497. lha_win_time(uint64_t wintime, long *ns)
  1498. {
  1499. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  1500. if (wintime >= EPOC_TIME) {
  1501. wintime -= EPOC_TIME; /* 1970-01-01 00:00:00 (UTC) */
  1502. if (ns != NULL)
  1503. *ns = (long)(wintime % 10000000) * 100;
  1504. return (wintime / 10000000);
  1505. } else {
  1506. if (ns != NULL)
  1507. *ns = 0;
  1508. return (0);
  1509. }
  1510. }
  1511. static unsigned char
  1512. lha_calcsum(unsigned char sum, const void *pp, int offset, size_t size)
  1513. {
  1514. unsigned char const *p = (unsigned char const *)pp;
  1515. p += offset;
  1516. for (;size > 0; --size)
  1517. sum += *p++;
  1518. return (sum);
  1519. }
  1520. static uint16_t crc16tbl[2][256];
  1521. static void
  1522. lha_crc16_init(void)
  1523. {
  1524. unsigned int i;
  1525. static int crc16init = 0;
  1526. if (crc16init)
  1527. return;
  1528. crc16init = 1;
  1529. for (i = 0; i < 256; i++) {
  1530. unsigned int j;
  1531. uint16_t crc = (uint16_t)i;
  1532. for (j = 8; j; j--)
  1533. crc = (crc >> 1) ^ ((crc & 1) * 0xA001);
  1534. crc16tbl[0][i] = crc;
  1535. }
  1536. for (i = 0; i < 256; i++) {
  1537. crc16tbl[1][i] = (crc16tbl[0][i] >> 8)
  1538. ^ crc16tbl[0][crc16tbl[0][i] & 0xff];
  1539. }
  1540. }
  1541. static uint16_t
  1542. lha_crc16(uint16_t crc, const void *pp, size_t len)
  1543. {
  1544. const unsigned char *p = (const unsigned char *)pp;
  1545. const uint16_t *buff;
  1546. const union {
  1547. uint32_t i;
  1548. char c[4];
  1549. } u = { 0x01020304 };
  1550. if (len == 0)
  1551. return crc;
  1552. /* Process unaligned address. */
  1553. if (((uintptr_t)p) & (uintptr_t)0x1) {
  1554. crc = (crc >> 8) ^ crc16tbl[0][(crc ^ *p++) & 0xff];
  1555. len--;
  1556. }
  1557. buff = (const uint16_t *)p;
  1558. /*
  1559. * Modern C compiler such as GCC does not unroll automatically yet
  1560. * without unrolling pragma, and Clang is so. So we should
  1561. * unroll this loop for its performance.
  1562. */
  1563. for (;len >= 8; len -= 8) {
  1564. /* This if statement expects compiler optimization will
  1565. * remove the statement which will not be executed. */
  1566. #undef bswap16
  1567. #ifndef __has_builtin
  1568. # define __has_builtin(x) 0
  1569. #endif
  1570. #if defined(_MSC_VER) && _MSC_VER >= 1400 /* Visual Studio */
  1571. # define bswap16(x) _byteswap_ushort(x)
  1572. #elif defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
  1573. /* GCC 4.8 and later has __builtin_bswap16() */
  1574. # define bswap16(x) __builtin_bswap16(x)
  1575. #elif defined(__clang__) && __has_builtin(__builtin_bswap16)
  1576. /* All clang versions have __builtin_bswap16() */
  1577. # define bswap16(x) __builtin_bswap16(x)
  1578. #else
  1579. # define bswap16(x) ((((x) >> 8) & 0xff) | ((x) << 8))
  1580. #endif
  1581. #define CRC16W do { \
  1582. if(u.c[0] == 1) { /* Big endian */ \
  1583. crc ^= bswap16(*buff); buff++; \
  1584. } else \
  1585. crc ^= *buff++; \
  1586. crc = crc16tbl[1][crc & 0xff] ^ crc16tbl[0][crc >> 8];\
  1587. } while (0)
  1588. CRC16W;
  1589. CRC16W;
  1590. CRC16W;
  1591. CRC16W;
  1592. #undef CRC16W
  1593. #undef bswap16
  1594. }
  1595. p = (const unsigned char *)buff;
  1596. for (;len; len--) {
  1597. crc = (crc >> 8) ^ crc16tbl[0][(crc ^ *p++) & 0xff];
  1598. }
  1599. return crc;
  1600. }
  1601. /*
  1602. * Initialize LZHUF decoder.
  1603. *
  1604. * Returns ARCHIVE_OK if initialization was successful.
  1605. * Returns ARCHIVE_FAILED if method is unsupported.
  1606. * Returns ARCHIVE_FATAL if initialization failed; memory allocation
  1607. * error occurred.
  1608. */
  1609. static int
  1610. lzh_decode_init(struct lzh_stream *strm, const char *method)
  1611. {
  1612. struct lzh_dec *ds;
  1613. int w_bits, w_size;
  1614. if (strm->ds == NULL) {
  1615. strm->ds = calloc(1, sizeof(*strm->ds));
  1616. if (strm->ds == NULL)
  1617. return (ARCHIVE_FATAL);
  1618. }
  1619. ds = strm->ds;
  1620. ds->error = ARCHIVE_FAILED;
  1621. if (method == NULL || method[0] != 'l' || method[1] != 'h')
  1622. return (ARCHIVE_FAILED);
  1623. switch (method[2]) {
  1624. case '5':
  1625. w_bits = 13;/* 8KiB for window */
  1626. break;
  1627. case '6':
  1628. w_bits = 15;/* 32KiB for window */
  1629. break;
  1630. case '7':
  1631. w_bits = 16;/* 64KiB for window */
  1632. break;
  1633. default:
  1634. return (ARCHIVE_FAILED);/* Not supported. */
  1635. }
  1636. ds->error = ARCHIVE_FATAL;
  1637. /* Expand a window size up to 128 KiB for decompressing process
  1638. * performance whatever its original window size is. */
  1639. ds->w_size = 1U << 17;
  1640. ds->w_mask = ds->w_size -1;
  1641. if (ds->w_buff == NULL) {
  1642. ds->w_buff = malloc(ds->w_size);
  1643. if (ds->w_buff == NULL)
  1644. return (ARCHIVE_FATAL);
  1645. }
  1646. w_size = 1U << w_bits;
  1647. memset(ds->w_buff + ds->w_size - w_size, 0x20, w_size);
  1648. ds->w_pos = 0;
  1649. ds->state = 0;
  1650. ds->pos_pt_len_size = w_bits + 1;
  1651. ds->pos_pt_len_bits = (w_bits == 15 || w_bits == 16)? 5: 4;
  1652. ds->literal_pt_len_size = PT_BITLEN_SIZE;
  1653. ds->literal_pt_len_bits = 5;
  1654. ds->br.cache_buffer = 0;
  1655. ds->br.cache_avail = 0;
  1656. if (lzh_huffman_init(&(ds->lt), LT_BITLEN_SIZE, 16)
  1657. != ARCHIVE_OK)
  1658. return (ARCHIVE_FATAL);
  1659. ds->lt.len_bits = 9;
  1660. if (lzh_huffman_init(&(ds->pt), PT_BITLEN_SIZE, 16)
  1661. != ARCHIVE_OK)
  1662. return (ARCHIVE_FATAL);
  1663. ds->error = 0;
  1664. return (ARCHIVE_OK);
  1665. }
  1666. /*
  1667. * Release LZHUF decoder.
  1668. */
  1669. static void
  1670. lzh_decode_free(struct lzh_stream *strm)
  1671. {
  1672. if (strm->ds == NULL)
  1673. return;
  1674. free(strm->ds->w_buff);
  1675. lzh_huffman_free(&(strm->ds->lt));
  1676. lzh_huffman_free(&(strm->ds->pt));
  1677. free(strm->ds);
  1678. strm->ds = NULL;
  1679. }
  1680. /*
  1681. * Bit stream reader.
  1682. */
  1683. /* Check that the cache buffer has enough bits. */
  1684. #define lzh_br_has(br, n) ((br)->cache_avail >= n)
  1685. /* Get compressed data by bit. */
  1686. #define lzh_br_bits(br, n) \
  1687. (((uint16_t)((br)->cache_buffer >> \
  1688. ((br)->cache_avail - (n)))) & cache_masks[n])
  1689. #define lzh_br_bits_forced(br, n) \
  1690. (((uint16_t)((br)->cache_buffer << \
  1691. ((n) - (br)->cache_avail))) & cache_masks[n])
  1692. /* Read ahead to make sure the cache buffer has enough compressed data we
  1693. * will use.
  1694. * True : completed, there is enough data in the cache buffer.
  1695. * False : we met that strm->next_in is empty, we have to get following
  1696. * bytes. */
  1697. #define lzh_br_read_ahead_0(strm, br, n) \
  1698. (lzh_br_has(br, (n)) || lzh_br_fillup(strm, br))
  1699. /* True : the cache buffer has some bits as much as we need.
  1700. * False : there are no enough bits in the cache buffer to be used,
  1701. * we have to get following bytes if we could. */
  1702. #define lzh_br_read_ahead(strm, br, n) \
  1703. (lzh_br_read_ahead_0((strm), (br), (n)) || lzh_br_has((br), (n)))
  1704. /* Notify how many bits we consumed. */
  1705. #define lzh_br_consume(br, n) ((br)->cache_avail -= (n))
  1706. #define lzh_br_unconsume(br, n) ((br)->cache_avail += (n))
  1707. static const uint16_t cache_masks[] = {
  1708. 0x0000, 0x0001, 0x0003, 0x0007,
  1709. 0x000F, 0x001F, 0x003F, 0x007F,
  1710. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  1711. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF,
  1712. 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF
  1713. };
  1714. /*
  1715. * Shift away used bits in the cache data and fill it up with following bits.
  1716. * Call this when cache buffer does not have enough bits you need.
  1717. *
  1718. * Returns 1 if the cache buffer is full.
  1719. * Returns 0 if the cache buffer is not full; input buffer is empty.
  1720. */
  1721. static int
  1722. lzh_br_fillup(struct lzh_stream *strm, struct lzh_br *br)
  1723. {
  1724. int n = CACHE_BITS - br->cache_avail;
  1725. for (;;) {
  1726. const int x = n >> 3;
  1727. if (strm->avail_in >= x) {
  1728. switch (x) {
  1729. case 8:
  1730. br->cache_buffer =
  1731. ((uint64_t)strm->next_in[0]) << 56 |
  1732. ((uint64_t)strm->next_in[1]) << 48 |
  1733. ((uint64_t)strm->next_in[2]) << 40 |
  1734. ((uint64_t)strm->next_in[3]) << 32 |
  1735. ((uint32_t)strm->next_in[4]) << 24 |
  1736. ((uint32_t)strm->next_in[5]) << 16 |
  1737. ((uint32_t)strm->next_in[6]) << 8 |
  1738. (uint32_t)strm->next_in[7];
  1739. strm->next_in += 8;
  1740. strm->avail_in -= 8;
  1741. br->cache_avail += 8 * 8;
  1742. return (1);
  1743. case 7:
  1744. br->cache_buffer =
  1745. (br->cache_buffer << 56) |
  1746. ((uint64_t)strm->next_in[0]) << 48 |
  1747. ((uint64_t)strm->next_in[1]) << 40 |
  1748. ((uint64_t)strm->next_in[2]) << 32 |
  1749. ((uint32_t)strm->next_in[3]) << 24 |
  1750. ((uint32_t)strm->next_in[4]) << 16 |
  1751. ((uint32_t)strm->next_in[5]) << 8 |
  1752. (uint32_t)strm->next_in[6];
  1753. strm->next_in += 7;
  1754. strm->avail_in -= 7;
  1755. br->cache_avail += 7 * 8;
  1756. return (1);
  1757. case 6:
  1758. br->cache_buffer =
  1759. (br->cache_buffer << 48) |
  1760. ((uint64_t)strm->next_in[0]) << 40 |
  1761. ((uint64_t)strm->next_in[1]) << 32 |
  1762. ((uint32_t)strm->next_in[2]) << 24 |
  1763. ((uint32_t)strm->next_in[3]) << 16 |
  1764. ((uint32_t)strm->next_in[4]) << 8 |
  1765. (uint32_t)strm->next_in[5];
  1766. strm->next_in += 6;
  1767. strm->avail_in -= 6;
  1768. br->cache_avail += 6 * 8;
  1769. return (1);
  1770. case 0:
  1771. /* We have enough compressed data in
  1772. * the cache buffer.*/
  1773. return (1);
  1774. default:
  1775. break;
  1776. }
  1777. }
  1778. if (strm->avail_in == 0) {
  1779. /* There is not enough compressed data to fill up the
  1780. * cache buffer. */
  1781. return (0);
  1782. }
  1783. br->cache_buffer =
  1784. (br->cache_buffer << 8) | *strm->next_in++;
  1785. strm->avail_in--;
  1786. br->cache_avail += 8;
  1787. n -= 8;
  1788. }
  1789. }
  1790. /*
  1791. * Decode LZHUF.
  1792. *
  1793. * 1. Returns ARCHIVE_OK if output buffer or input buffer are empty.
  1794. * Please set available buffer and call this function again.
  1795. * 2. Returns ARCHIVE_EOF if decompression has been completed.
  1796. * 3. Returns ARCHIVE_FAILED if an error occurred; compressed data
  1797. * is broken or you do not set 'last' flag properly.
  1798. * 4. 'last' flag is very important, you must set 1 to the flag if there
  1799. * is no input data. The lha compressed data format does not provide how
  1800. * to know the compressed data is really finished.
  1801. * Note: lha command utility check if the total size of output bytes is
  1802. * reached the uncompressed size recorded in its header. it does not mind
  1803. * that the decoding process is properly finished.
  1804. * GNU ZIP can decompress another compressed file made by SCO LZH compress.
  1805. * it handles EOF as null to fill read buffer with zero until the decoding
  1806. * process meet 2 bytes of zeros at reading a size of a next chunk, so the
  1807. * zeros are treated as the mark of the end of the data although the zeros
  1808. * is dummy, not the file data.
  1809. */
  1810. static int lzh_read_blocks(struct lzh_stream *, int);
  1811. static int lzh_decode_blocks(struct lzh_stream *, int);
  1812. #define ST_RD_BLOCK 0
  1813. #define ST_RD_PT_1 1
  1814. #define ST_RD_PT_2 2
  1815. #define ST_RD_PT_3 3
  1816. #define ST_RD_PT_4 4
  1817. #define ST_RD_LITERAL_1 5
  1818. #define ST_RD_LITERAL_2 6
  1819. #define ST_RD_LITERAL_3 7
  1820. #define ST_RD_POS_DATA_1 8
  1821. #define ST_GET_LITERAL 9
  1822. #define ST_GET_POS_1 10
  1823. #define ST_GET_POS_2 11
  1824. #define ST_COPY_DATA 12
  1825. static int
  1826. lzh_decode(struct lzh_stream *strm, int last)
  1827. {
  1828. struct lzh_dec *ds = strm->ds;
  1829. int avail_in;
  1830. int r;
  1831. if (ds->error)
  1832. return (ds->error);
  1833. avail_in = strm->avail_in;
  1834. do {
  1835. if (ds->state < ST_GET_LITERAL)
  1836. r = lzh_read_blocks(strm, last);
  1837. else
  1838. r = lzh_decode_blocks(strm, last);
  1839. } while (r == 100);
  1840. strm->total_in += avail_in - strm->avail_in;
  1841. return (r);
  1842. }
  1843. static void
  1844. lzh_emit_window(struct lzh_stream *strm, size_t s)
  1845. {
  1846. strm->ref_ptr = strm->ds->w_buff;
  1847. strm->avail_out = (int)s;
  1848. strm->total_out += s;
  1849. }
  1850. static int
  1851. lzh_read_blocks(struct lzh_stream *strm, int last)
  1852. {
  1853. struct lzh_dec *ds = strm->ds;
  1854. struct lzh_br *br = &(ds->br);
  1855. int c = 0, i;
  1856. unsigned rbits;
  1857. for (;;) {
  1858. switch (ds->state) {
  1859. case ST_RD_BLOCK:
  1860. /*
  1861. * Read a block number indicates how many blocks
  1862. * we will handle. The block is composed of a
  1863. * literal and a match, sometimes a literal only
  1864. * in particular, there are no reference data at
  1865. * the beginning of the decompression.
  1866. */
  1867. if (!lzh_br_read_ahead_0(strm, br, 16)) {
  1868. if (!last)
  1869. /* We need following data. */
  1870. return (ARCHIVE_OK);
  1871. if (lzh_br_has(br, 8)) {
  1872. /*
  1873. * It seems there are extra bits.
  1874. * 1. Compressed data is broken.
  1875. * 2. `last' flag does not properly
  1876. * set.
  1877. */
  1878. goto failed;
  1879. }
  1880. if (ds->w_pos > 0) {
  1881. lzh_emit_window(strm, ds->w_pos);
  1882. ds->w_pos = 0;
  1883. return (ARCHIVE_OK);
  1884. }
  1885. /* End of compressed data; we have completely
  1886. * handled all compressed data. */
  1887. return (ARCHIVE_EOF);
  1888. }
  1889. ds->blocks_avail = lzh_br_bits(br, 16);
  1890. if (ds->blocks_avail == 0)
  1891. goto failed;
  1892. lzh_br_consume(br, 16);
  1893. /*
  1894. * Read a literal table compressed in huffman
  1895. * coding.
  1896. */
  1897. ds->pt.len_size = ds->literal_pt_len_size;
  1898. ds->pt.len_bits = ds->literal_pt_len_bits;
  1899. ds->reading_position = 0;
  1900. /* FALL THROUGH */
  1901. case ST_RD_PT_1:
  1902. /* Note: ST_RD_PT_1, ST_RD_PT_2 and ST_RD_PT_4 are
  1903. * used in reading both a literal table and a
  1904. * position table. */
  1905. if (!lzh_br_read_ahead(strm, br, ds->pt.len_bits)) {
  1906. if (last)
  1907. goto failed;/* Truncated data. */
  1908. ds->state = ST_RD_PT_1;
  1909. return (ARCHIVE_OK);
  1910. }
  1911. ds->pt.len_avail = lzh_br_bits(br, ds->pt.len_bits);
  1912. lzh_br_consume(br, ds->pt.len_bits);
  1913. /* FALL THROUGH */
  1914. case ST_RD_PT_2:
  1915. if (ds->pt.len_avail == 0) {
  1916. /* There is no bitlen. */
  1917. if (!lzh_br_read_ahead(strm, br,
  1918. ds->pt.len_bits)) {
  1919. if (last)
  1920. goto failed;/* Truncated data.*/
  1921. ds->state = ST_RD_PT_2;
  1922. return (ARCHIVE_OK);
  1923. }
  1924. if (!lzh_make_fake_table(&(ds->pt),
  1925. lzh_br_bits(br, ds->pt.len_bits)))
  1926. goto failed;/* Invalid data. */
  1927. lzh_br_consume(br, ds->pt.len_bits);
  1928. if (ds->reading_position)
  1929. ds->state = ST_GET_LITERAL;
  1930. else
  1931. ds->state = ST_RD_LITERAL_1;
  1932. break;
  1933. } else if (ds->pt.len_avail > ds->pt.len_size)
  1934. goto failed;/* Invalid data. */
  1935. ds->loop = 0;
  1936. memset(ds->pt.freq, 0, sizeof(ds->pt.freq));
  1937. if (ds->pt.len_avail < 3 ||
  1938. ds->pt.len_size == ds->pos_pt_len_size) {
  1939. ds->state = ST_RD_PT_4;
  1940. break;
  1941. }
  1942. /* FALL THROUGH */
  1943. case ST_RD_PT_3:
  1944. ds->loop = lzh_read_pt_bitlen(strm, ds->loop, 3);
  1945. if (ds->loop < 3) {
  1946. if (ds->loop < 0 || last)
  1947. goto failed;/* Invalid data. */
  1948. /* Not completed, get following data. */
  1949. ds->state = ST_RD_PT_3;
  1950. return (ARCHIVE_OK);
  1951. }
  1952. /* There are some null in bitlen of the literal. */
  1953. if (!lzh_br_read_ahead(strm, br, 2)) {
  1954. if (last)
  1955. goto failed;/* Truncated data. */
  1956. ds->state = ST_RD_PT_3;
  1957. return (ARCHIVE_OK);
  1958. }
  1959. c = lzh_br_bits(br, 2);
  1960. lzh_br_consume(br, 2);
  1961. if (c > ds->pt.len_avail - 3)
  1962. goto failed;/* Invalid data. */
  1963. for (i = 3; c-- > 0 ;)
  1964. ds->pt.bitlen[i++] = 0;
  1965. ds->loop = i;
  1966. /* FALL THROUGH */
  1967. case ST_RD_PT_4:
  1968. ds->loop = lzh_read_pt_bitlen(strm, ds->loop,
  1969. ds->pt.len_avail);
  1970. if (ds->loop < ds->pt.len_avail) {
  1971. if (ds->loop < 0 || last)
  1972. goto failed;/* Invalid data. */
  1973. /* Not completed, get following data. */
  1974. ds->state = ST_RD_PT_4;
  1975. return (ARCHIVE_OK);
  1976. }
  1977. if (!lzh_make_huffman_table(&(ds->pt)))
  1978. goto failed;/* Invalid data */
  1979. if (ds->reading_position) {
  1980. ds->state = ST_GET_LITERAL;
  1981. break;
  1982. }
  1983. /* FALL THROUGH */
  1984. case ST_RD_LITERAL_1:
  1985. if (!lzh_br_read_ahead(strm, br, ds->lt.len_bits)) {
  1986. if (last)
  1987. goto failed;/* Truncated data. */
  1988. ds->state = ST_RD_LITERAL_1;
  1989. return (ARCHIVE_OK);
  1990. }
  1991. ds->lt.len_avail = lzh_br_bits(br, ds->lt.len_bits);
  1992. lzh_br_consume(br, ds->lt.len_bits);
  1993. /* FALL THROUGH */
  1994. case ST_RD_LITERAL_2:
  1995. if (ds->lt.len_avail == 0) {
  1996. /* There is no bitlen. */
  1997. if (!lzh_br_read_ahead(strm, br,
  1998. ds->lt.len_bits)) {
  1999. if (last)
  2000. goto failed;/* Truncated data.*/
  2001. ds->state = ST_RD_LITERAL_2;
  2002. return (ARCHIVE_OK);
  2003. }
  2004. if (!lzh_make_fake_table(&(ds->lt),
  2005. lzh_br_bits(br, ds->lt.len_bits)))
  2006. goto failed;/* Invalid data */
  2007. lzh_br_consume(br, ds->lt.len_bits);
  2008. ds->state = ST_RD_POS_DATA_1;
  2009. break;
  2010. } else if (ds->lt.len_avail > ds->lt.len_size)
  2011. goto failed;/* Invalid data */
  2012. ds->loop = 0;
  2013. memset(ds->lt.freq, 0, sizeof(ds->lt.freq));
  2014. /* FALL THROUGH */
  2015. case ST_RD_LITERAL_3:
  2016. i = ds->loop;
  2017. while (i < ds->lt.len_avail) {
  2018. if (!lzh_br_read_ahead(strm, br,
  2019. ds->pt.max_bits)) {
  2020. if (last)
  2021. goto failed;/* Truncated data.*/
  2022. ds->loop = i;
  2023. ds->state = ST_RD_LITERAL_3;
  2024. return (ARCHIVE_OK);
  2025. }
  2026. rbits = lzh_br_bits(br, ds->pt.max_bits);
  2027. c = lzh_decode_huffman(&(ds->pt), rbits);
  2028. if (c > 2) {
  2029. /* Note: 'c' will never be more than
  2030. * eighteen since it's limited by
  2031. * PT_BITLEN_SIZE, which is being set
  2032. * to ds->pt.len_size through
  2033. * ds->literal_pt_len_size. */
  2034. lzh_br_consume(br, ds->pt.bitlen[c]);
  2035. c -= 2;
  2036. ds->lt.freq[c]++;
  2037. ds->lt.bitlen[i++] = c;
  2038. } else if (c == 0) {
  2039. lzh_br_consume(br, ds->pt.bitlen[c]);
  2040. ds->lt.bitlen[i++] = 0;
  2041. } else {
  2042. /* c == 1 or c == 2 */
  2043. int n = (c == 1)?4:9;
  2044. if (!lzh_br_read_ahead(strm, br,
  2045. ds->pt.bitlen[c] + n)) {
  2046. if (last) /* Truncated data. */
  2047. goto failed;
  2048. ds->loop = i;
  2049. ds->state = ST_RD_LITERAL_3;
  2050. return (ARCHIVE_OK);
  2051. }
  2052. lzh_br_consume(br, ds->pt.bitlen[c]);
  2053. c = lzh_br_bits(br, n);
  2054. lzh_br_consume(br, n);
  2055. c += (n == 4)?3:20;
  2056. if (i + c > ds->lt.len_avail)
  2057. goto failed;/* Invalid data */
  2058. memset(&(ds->lt.bitlen[i]), 0, c);
  2059. i += c;
  2060. }
  2061. }
  2062. if (i > ds->lt.len_avail ||
  2063. !lzh_make_huffman_table(&(ds->lt)))
  2064. goto failed;/* Invalid data */
  2065. /* FALL THROUGH */
  2066. case ST_RD_POS_DATA_1:
  2067. /*
  2068. * Read a position table compressed in huffman
  2069. * coding.
  2070. */
  2071. ds->pt.len_size = ds->pos_pt_len_size;
  2072. ds->pt.len_bits = ds->pos_pt_len_bits;
  2073. ds->reading_position = 1;
  2074. ds->state = ST_RD_PT_1;
  2075. break;
  2076. case ST_GET_LITERAL:
  2077. return (100);
  2078. }
  2079. }
  2080. failed:
  2081. return (ds->error = ARCHIVE_FAILED);
  2082. }
  2083. static int
  2084. lzh_decode_blocks(struct lzh_stream *strm, int last)
  2085. {
  2086. struct lzh_dec *ds = strm->ds;
  2087. struct lzh_br bre = ds->br;
  2088. struct huffman *lt = &(ds->lt);
  2089. struct huffman *pt = &(ds->pt);
  2090. unsigned char *w_buff = ds->w_buff;
  2091. unsigned char *lt_bitlen = lt->bitlen;
  2092. unsigned char *pt_bitlen = pt->bitlen;
  2093. int blocks_avail = ds->blocks_avail, c = 0;
  2094. int copy_len = ds->copy_len, copy_pos = ds->copy_pos;
  2095. int w_pos = ds->w_pos, w_mask = ds->w_mask, w_size = ds->w_size;
  2096. int lt_max_bits = lt->max_bits, pt_max_bits = pt->max_bits;
  2097. int state = ds->state;
  2098. for (;;) {
  2099. switch (state) {
  2100. case ST_GET_LITERAL:
  2101. for (;;) {
  2102. if (blocks_avail == 0) {
  2103. /* We have decoded all blocks.
  2104. * Let's handle next blocks. */
  2105. ds->state = ST_RD_BLOCK;
  2106. ds->br = bre;
  2107. ds->blocks_avail = 0;
  2108. ds->w_pos = w_pos;
  2109. ds->copy_pos = 0;
  2110. return (100);
  2111. }
  2112. /* lzh_br_read_ahead() always try to fill the
  2113. * cache buffer up. In specific situation we
  2114. * are close to the end of the data, the cache
  2115. * buffer will not be full and thus we have to
  2116. * determine if the cache buffer has some bits
  2117. * as much as we need after lzh_br_read_ahead()
  2118. * failed. */
  2119. if (!lzh_br_read_ahead(strm, &bre,
  2120. lt_max_bits)) {
  2121. if (!last)
  2122. goto next_data;
  2123. /* Remaining bits are less than
  2124. * maximum bits(lt.max_bits) but maybe
  2125. * it still remains as much as we need,
  2126. * so we should try to use it with
  2127. * dummy bits. */
  2128. c = lzh_decode_huffman(lt,
  2129. lzh_br_bits_forced(&bre,
  2130. lt_max_bits));
  2131. lzh_br_consume(&bre, lt_bitlen[c]);
  2132. if (!lzh_br_has(&bre, 0))
  2133. goto failed;/* Over read. */
  2134. } else {
  2135. c = lzh_decode_huffman(lt,
  2136. lzh_br_bits(&bre, lt_max_bits));
  2137. lzh_br_consume(&bre, lt_bitlen[c]);
  2138. }
  2139. blocks_avail--;
  2140. if (c > UCHAR_MAX)
  2141. /* Current block is a match data. */
  2142. break;
  2143. /*
  2144. * 'c' is exactly a literal code.
  2145. */
  2146. /* Save a decoded code to reference it
  2147. * afterward. */
  2148. w_buff[w_pos] = c;
  2149. if (++w_pos >= w_size) {
  2150. w_pos = 0;
  2151. lzh_emit_window(strm, w_size);
  2152. goto next_data;
  2153. }
  2154. }
  2155. /* 'c' is the length of a match pattern we have
  2156. * already extracted, which has be stored in
  2157. * window(ds->w_buff). */
  2158. copy_len = c - (UCHAR_MAX + 1) + MINMATCH;
  2159. /* FALL THROUGH */
  2160. case ST_GET_POS_1:
  2161. /*
  2162. * Get a reference position.
  2163. */
  2164. if (!lzh_br_read_ahead(strm, &bre, pt_max_bits)) {
  2165. if (!last) {
  2166. state = ST_GET_POS_1;
  2167. ds->copy_len = copy_len;
  2168. goto next_data;
  2169. }
  2170. copy_pos = lzh_decode_huffman(pt,
  2171. lzh_br_bits_forced(&bre, pt_max_bits));
  2172. lzh_br_consume(&bre, pt_bitlen[copy_pos]);
  2173. if (!lzh_br_has(&bre, 0))
  2174. goto failed;/* Over read. */
  2175. } else {
  2176. copy_pos = lzh_decode_huffman(pt,
  2177. lzh_br_bits(&bre, pt_max_bits));
  2178. lzh_br_consume(&bre, pt_bitlen[copy_pos]);
  2179. }
  2180. /* FALL THROUGH */
  2181. case ST_GET_POS_2:
  2182. if (copy_pos > 1) {
  2183. /* We need an additional adjustment number to
  2184. * the position. */
  2185. int p = copy_pos - 1;
  2186. if (!lzh_br_read_ahead(strm, &bre, p)) {
  2187. if (last)
  2188. goto failed;/* Truncated data.*/
  2189. state = ST_GET_POS_2;
  2190. ds->copy_len = copy_len;
  2191. ds->copy_pos = copy_pos;
  2192. goto next_data;
  2193. }
  2194. copy_pos = (1 << p) + lzh_br_bits(&bre, p);
  2195. lzh_br_consume(&bre, p);
  2196. }
  2197. /* The position is actually a distance from the last
  2198. * code we had extracted and thus we have to convert
  2199. * it to a position of the window. */
  2200. copy_pos = (w_pos - copy_pos - 1) & w_mask;
  2201. /* FALL THROUGH */
  2202. case ST_COPY_DATA:
  2203. /*
  2204. * Copy `copy_len' bytes as extracted data from
  2205. * the window into the output buffer.
  2206. */
  2207. for (;;) {
  2208. int l;
  2209. l = copy_len;
  2210. if (copy_pos > w_pos) {
  2211. if (l > w_size - copy_pos)
  2212. l = w_size - copy_pos;
  2213. } else {
  2214. if (l > w_size - w_pos)
  2215. l = w_size - w_pos;
  2216. }
  2217. if ((copy_pos + l < w_pos)
  2218. || (w_pos + l < copy_pos)) {
  2219. /* No overlap. */
  2220. memcpy(w_buff + w_pos,
  2221. w_buff + copy_pos, l);
  2222. } else {
  2223. const unsigned char *s;
  2224. unsigned char *d;
  2225. int li;
  2226. d = w_buff + w_pos;
  2227. s = w_buff + copy_pos;
  2228. for (li = 0; li < l-1;) {
  2229. d[li] = s[li];li++;
  2230. d[li] = s[li];li++;
  2231. }
  2232. if (li < l)
  2233. d[li] = s[li];
  2234. }
  2235. w_pos += l;
  2236. if (w_pos == w_size) {
  2237. w_pos = 0;
  2238. lzh_emit_window(strm, w_size);
  2239. if (copy_len <= l)
  2240. state = ST_GET_LITERAL;
  2241. else {
  2242. state = ST_COPY_DATA;
  2243. ds->copy_len = copy_len - l;
  2244. ds->copy_pos =
  2245. (copy_pos + l) & w_mask;
  2246. }
  2247. goto next_data;
  2248. }
  2249. if (copy_len <= l)
  2250. /* A copy of current pattern ended. */
  2251. break;
  2252. copy_len -= l;
  2253. copy_pos = (copy_pos + l) & w_mask;
  2254. }
  2255. state = ST_GET_LITERAL;
  2256. break;
  2257. }
  2258. }
  2259. failed:
  2260. return (ds->error = ARCHIVE_FAILED);
  2261. next_data:
  2262. ds->br = bre;
  2263. ds->blocks_avail = blocks_avail;
  2264. ds->state = state;
  2265. ds->w_pos = w_pos;
  2266. return (ARCHIVE_OK);
  2267. }
  2268. static int
  2269. lzh_huffman_init(struct huffman *hf, size_t len_size, int tbl_bits)
  2270. {
  2271. int bits;
  2272. if (hf->bitlen == NULL) {
  2273. hf->bitlen = malloc(len_size * sizeof(hf->bitlen[0]));
  2274. if (hf->bitlen == NULL)
  2275. return (ARCHIVE_FATAL);
  2276. }
  2277. if (hf->tbl == NULL) {
  2278. if (tbl_bits < HTBL_BITS)
  2279. bits = tbl_bits;
  2280. else
  2281. bits = HTBL_BITS;
  2282. hf->tbl = malloc(((size_t)1 << bits) * sizeof(hf->tbl[0]));
  2283. if (hf->tbl == NULL)
  2284. return (ARCHIVE_FATAL);
  2285. }
  2286. if (hf->tree == NULL && tbl_bits > HTBL_BITS) {
  2287. hf->tree_avail = 1 << (tbl_bits - HTBL_BITS + 4);
  2288. hf->tree = malloc(hf->tree_avail * sizeof(hf->tree[0]));
  2289. if (hf->tree == NULL)
  2290. return (ARCHIVE_FATAL);
  2291. }
  2292. hf->len_size = (int)len_size;
  2293. hf->tbl_bits = tbl_bits;
  2294. return (ARCHIVE_OK);
  2295. }
  2296. static void
  2297. lzh_huffman_free(struct huffman *hf)
  2298. {
  2299. free(hf->bitlen);
  2300. free(hf->tbl);
  2301. free(hf->tree);
  2302. }
  2303. static const char bitlen_tbl[0x400] = {
  2304. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2305. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2306. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2307. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2308. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2309. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2310. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2311. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2312. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2313. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2314. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2315. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2316. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2317. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2318. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2319. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2320. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2321. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2322. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2323. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2324. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2325. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2326. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2327. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2328. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2329. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2330. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2331. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2332. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2333. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2334. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2335. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2336. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2337. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2338. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2339. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2340. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2341. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2342. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2343. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2344. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2345. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2346. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2347. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2348. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2349. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2350. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2351. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2352. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2353. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2354. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2355. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2356. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2357. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2358. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2359. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2360. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2361. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2362. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2363. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2364. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  2365. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  2366. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  2367. 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 0
  2368. };
  2369. static int
  2370. lzh_read_pt_bitlen(struct lzh_stream *strm, int start, int end)
  2371. {
  2372. struct lzh_dec *ds = strm->ds;
  2373. struct lzh_br *br = &(ds->br);
  2374. int c, i;
  2375. for (i = start; i < end; ) {
  2376. /*
  2377. * bit pattern the number we need
  2378. * 000 -> 0
  2379. * 001 -> 1
  2380. * 010 -> 2
  2381. * ...
  2382. * 110 -> 6
  2383. * 1110 -> 7
  2384. * 11110 -> 8
  2385. * ...
  2386. * 1111111111110 -> 16
  2387. */
  2388. if (!lzh_br_read_ahead(strm, br, 3))
  2389. return (i);
  2390. if ((c = lzh_br_bits(br, 3)) == 7) {
  2391. if (!lzh_br_read_ahead(strm, br, 13))
  2392. return (i);
  2393. c = bitlen_tbl[lzh_br_bits(br, 13) & 0x3FF];
  2394. if (c)
  2395. lzh_br_consume(br, c - 3);
  2396. else
  2397. return (-1);/* Invalid data. */
  2398. } else
  2399. lzh_br_consume(br, 3);
  2400. ds->pt.bitlen[i++] = c;
  2401. ds->pt.freq[c]++;
  2402. }
  2403. return (i);
  2404. }
  2405. static int
  2406. lzh_make_fake_table(struct huffman *hf, uint16_t c)
  2407. {
  2408. if (c >= hf->len_size)
  2409. return (0);
  2410. hf->tbl[0] = c;
  2411. hf->max_bits = 0;
  2412. hf->shift_bits = 0;
  2413. hf->bitlen[hf->tbl[0]] = 0;
  2414. return (1);
  2415. }
  2416. /*
  2417. * Make a huffman coding table.
  2418. */
  2419. static int
  2420. lzh_make_huffman_table(struct huffman *hf)
  2421. {
  2422. uint16_t *tbl;
  2423. const unsigned char *bitlen;
  2424. int bitptn[17], weight[17];
  2425. int i, maxbits = 0, ptn, tbl_size, w;
  2426. int diffbits, len_avail;
  2427. /*
  2428. * Initialize bit patterns.
  2429. */
  2430. ptn = 0;
  2431. for (i = 1, w = 1 << 15; i <= 16; i++, w >>= 1) {
  2432. bitptn[i] = ptn;
  2433. weight[i] = w;
  2434. if (hf->freq[i]) {
  2435. ptn += hf->freq[i] * w;
  2436. maxbits = i;
  2437. }
  2438. }
  2439. if (ptn != 0x10000 || maxbits > hf->tbl_bits)
  2440. return (0);/* Invalid */
  2441. hf->max_bits = maxbits;
  2442. /*
  2443. * Cut out extra bits which we won't house in the table.
  2444. * This preparation reduces the same calculation in the for-loop
  2445. * making the table.
  2446. */
  2447. if (maxbits < 16) {
  2448. int ebits = 16 - maxbits;
  2449. for (i = 1; i <= maxbits; i++) {
  2450. bitptn[i] >>= ebits;
  2451. weight[i] >>= ebits;
  2452. }
  2453. }
  2454. if (maxbits > HTBL_BITS) {
  2455. unsigned htbl_max;
  2456. uint16_t *p;
  2457. diffbits = maxbits - HTBL_BITS;
  2458. for (i = 1; i <= HTBL_BITS; i++) {
  2459. bitptn[i] >>= diffbits;
  2460. weight[i] >>= diffbits;
  2461. }
  2462. htbl_max = bitptn[HTBL_BITS] +
  2463. weight[HTBL_BITS] * hf->freq[HTBL_BITS];
  2464. p = &(hf->tbl[htbl_max]);
  2465. while (p < &hf->tbl[1U<<HTBL_BITS])
  2466. *p++ = 0;
  2467. } else
  2468. diffbits = 0;
  2469. hf->shift_bits = diffbits;
  2470. /*
  2471. * Make the table.
  2472. */
  2473. tbl_size = 1 << HTBL_BITS;
  2474. tbl = hf->tbl;
  2475. bitlen = hf->bitlen;
  2476. len_avail = hf->len_avail;
  2477. hf->tree_used = 0;
  2478. for (i = 0; i < len_avail; i++) {
  2479. uint16_t *p;
  2480. int len, cnt;
  2481. uint16_t bit;
  2482. int extlen;
  2483. struct htree_t *ht;
  2484. if (bitlen[i] == 0)
  2485. continue;
  2486. /* Get a bit pattern */
  2487. len = bitlen[i];
  2488. ptn = bitptn[len];
  2489. cnt = weight[len];
  2490. if (len <= HTBL_BITS) {
  2491. /* Calculate next bit pattern */
  2492. if ((bitptn[len] = ptn + cnt) > tbl_size)
  2493. return (0);/* Invalid */
  2494. /* Update the table */
  2495. p = &(tbl[ptn]);
  2496. if (cnt > 7) {
  2497. uint16_t *pc;
  2498. cnt -= 8;
  2499. pc = &p[cnt];
  2500. pc[0] = (uint16_t)i;
  2501. pc[1] = (uint16_t)i;
  2502. pc[2] = (uint16_t)i;
  2503. pc[3] = (uint16_t)i;
  2504. pc[4] = (uint16_t)i;
  2505. pc[5] = (uint16_t)i;
  2506. pc[6] = (uint16_t)i;
  2507. pc[7] = (uint16_t)i;
  2508. if (cnt > 7) {
  2509. cnt -= 8;
  2510. memcpy(&p[cnt], pc,
  2511. 8 * sizeof(uint16_t));
  2512. pc = &p[cnt];
  2513. while (cnt > 15) {
  2514. cnt -= 16;
  2515. memcpy(&p[cnt], pc,
  2516. 16 * sizeof(uint16_t));
  2517. }
  2518. }
  2519. if (cnt)
  2520. memcpy(p, pc, cnt * sizeof(uint16_t));
  2521. } else {
  2522. while (cnt > 1) {
  2523. p[--cnt] = (uint16_t)i;
  2524. p[--cnt] = (uint16_t)i;
  2525. }
  2526. if (cnt)
  2527. p[--cnt] = (uint16_t)i;
  2528. }
  2529. continue;
  2530. }
  2531. /*
  2532. * A bit length is too big to be housed to a direct table,
  2533. * so we use a tree model for its extra bits.
  2534. */
  2535. bitptn[len] = ptn + cnt;
  2536. bit = 1U << (diffbits -1);
  2537. extlen = len - HTBL_BITS;
  2538. p = &(tbl[ptn >> diffbits]);
  2539. if (*p == 0) {
  2540. *p = len_avail + hf->tree_used;
  2541. ht = &(hf->tree[hf->tree_used++]);
  2542. if (hf->tree_used > hf->tree_avail)
  2543. return (0);/* Invalid */
  2544. ht->left = 0;
  2545. ht->right = 0;
  2546. } else {
  2547. if (*p < len_avail ||
  2548. *p >= (len_avail + hf->tree_used))
  2549. return (0);/* Invalid */
  2550. ht = &(hf->tree[*p - len_avail]);
  2551. }
  2552. while (--extlen > 0) {
  2553. if (ptn & bit) {
  2554. if (ht->left < len_avail) {
  2555. ht->left = len_avail + hf->tree_used;
  2556. ht = &(hf->tree[hf->tree_used++]);
  2557. if (hf->tree_used > hf->tree_avail)
  2558. return (0);/* Invalid */
  2559. ht->left = 0;
  2560. ht->right = 0;
  2561. } else {
  2562. ht = &(hf->tree[ht->left - len_avail]);
  2563. }
  2564. } else {
  2565. if (ht->right < len_avail) {
  2566. ht->right = len_avail + hf->tree_used;
  2567. ht = &(hf->tree[hf->tree_used++]);
  2568. if (hf->tree_used > hf->tree_avail)
  2569. return (0);/* Invalid */
  2570. ht->left = 0;
  2571. ht->right = 0;
  2572. } else {
  2573. ht = &(hf->tree[ht->right - len_avail]);
  2574. }
  2575. }
  2576. bit >>= 1;
  2577. }
  2578. if (ptn & bit) {
  2579. if (ht->left != 0)
  2580. return (0);/* Invalid */
  2581. ht->left = (uint16_t)i;
  2582. } else {
  2583. if (ht->right != 0)
  2584. return (0);/* Invalid */
  2585. ht->right = (uint16_t)i;
  2586. }
  2587. }
  2588. return (1);
  2589. }
  2590. static int
  2591. lzh_decode_huffman_tree(struct huffman *hf, unsigned rbits, int c)
  2592. {
  2593. struct htree_t *ht;
  2594. int extlen;
  2595. ht = hf->tree;
  2596. extlen = hf->shift_bits;
  2597. while (c >= hf->len_avail) {
  2598. c -= hf->len_avail;
  2599. if (extlen-- <= 0 || c >= hf->tree_used)
  2600. return (0);
  2601. if (rbits & (1U << extlen))
  2602. c = ht[c].left;
  2603. else
  2604. c = ht[c].right;
  2605. }
  2606. return (c);
  2607. }
  2608. static inline int
  2609. lzh_decode_huffman(struct huffman *hf, unsigned rbits)
  2610. {
  2611. int c;
  2612. /*
  2613. * At first search an index table for a bit pattern.
  2614. * If it fails, search a huffman tree for.
  2615. */
  2616. c = hf->tbl[rbits >> hf->shift_bits];
  2617. if (c < hf->len_avail || hf->len_avail == 0)
  2618. return (c);
  2619. /* This bit pattern needs to be found out at a huffman tree. */
  2620. return (lzh_decode_huffman_tree(hf, rbits, c));
  2621. }