archive_read_support_format_lha.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  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. if (lha->entry_bytes_remaining < 0) {
  641. archive_set_error(&a->archive,
  642. ARCHIVE_ERRNO_FILE_FORMAT,
  643. "Invalid LHa entry size");
  644. return (ARCHIVE_FATAL);
  645. }
  646. lha->entry_offset = 0;
  647. lha->entry_crc_calculated = 0;
  648. /*
  649. * This file does not have a content.
  650. */
  651. if (lha->directory || lha->compsize == 0)
  652. lha->end_of_entry = 1;
  653. sprintf(lha->format_name, "lha -%c%c%c-",
  654. lha->method[0], lha->method[1], lha->method[2]);
  655. a->archive.archive_format_name = lha->format_name;
  656. return (err);
  657. }
  658. /*
  659. * Replace a DOS path separator '\' by a character '/'.
  660. * Some multi-byte character set have a character '\' in its second byte.
  661. */
  662. static void
  663. lha_replace_path_separator(struct lha *lha, struct archive_entry *entry)
  664. {
  665. const wchar_t *wp;
  666. size_t i;
  667. if ((wp = archive_entry_pathname_w(entry)) != NULL) {
  668. archive_wstrcpy(&(lha->ws), wp);
  669. for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
  670. if (lha->ws.s[i] == L'\\')
  671. lha->ws.s[i] = L'/';
  672. }
  673. archive_entry_copy_pathname_w(entry, lha->ws.s);
  674. }
  675. if ((wp = archive_entry_symlink_w(entry)) != NULL) {
  676. archive_wstrcpy(&(lha->ws), wp);
  677. for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
  678. if (lha->ws.s[i] == L'\\')
  679. lha->ws.s[i] = L'/';
  680. }
  681. archive_entry_copy_symlink_w(entry, lha->ws.s);
  682. }
  683. }
  684. /*
  685. * Header 0 format
  686. *
  687. * +0 +1 +2 +7 +11
  688. * +---------------+----------+----------------+-------------------+
  689. * |header size(*1)|header sum|compression type|compressed size(*2)|
  690. * +---------------+----------+----------------+-------------------+
  691. * <---------------------(*1)----------*
  692. *
  693. * +11 +15 +17 +19 +20 +21
  694. * +-----------------+---------+---------+--------------+----------------+
  695. * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=0)|
  696. * +-----------------+---------+---------+--------------+----------------+
  697. * *--------------------------------(*1)---------------------------------*
  698. *
  699. * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+2+(*4)
  700. * +---------------+---------+----------+----------------+------------------+
  701. * |name length(*3)|file name|file CRC16|extra header(*4)| compressed data |
  702. * +---------------+---------+----------+----------------+------------------+
  703. * <--(*3)-> <------(*2)------>
  704. * *----------------------(*1)-------------------------->
  705. *
  706. */
  707. #define H0_HEADER_SIZE_OFFSET 0
  708. #define H0_HEADER_SUM_OFFSET 1
  709. #define H0_COMP_SIZE_OFFSET 7
  710. #define H0_ORIG_SIZE_OFFSET 11
  711. #define H0_DOS_TIME_OFFSET 15
  712. #define H0_NAME_LEN_OFFSET 21
  713. #define H0_FILE_NAME_OFFSET 22
  714. #define H0_FIXED_SIZE 24
  715. static int
  716. lha_read_file_header_0(struct archive_read *a, struct lha *lha)
  717. {
  718. const unsigned char *p;
  719. int extdsize, namelen;
  720. unsigned char headersum, sum_calculated;
  721. if ((p = __archive_read_ahead(a, H0_FIXED_SIZE, NULL)) == NULL)
  722. return (truncated_error(a));
  723. lha->header_size = p[H0_HEADER_SIZE_OFFSET] + 2;
  724. headersum = p[H0_HEADER_SUM_OFFSET];
  725. lha->compsize = archive_le32dec(p + H0_COMP_SIZE_OFFSET);
  726. lha->origsize = archive_le32dec(p + H0_ORIG_SIZE_OFFSET);
  727. lha->mtime = lha_dos_time(p + H0_DOS_TIME_OFFSET);
  728. namelen = p[H0_NAME_LEN_OFFSET];
  729. extdsize = (int)lha->header_size - H0_FIXED_SIZE - namelen;
  730. if ((namelen > 221 || extdsize < 0) && extdsize != -2) {
  731. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  732. "Invalid LHa header");
  733. return (ARCHIVE_FATAL);
  734. }
  735. if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
  736. return (truncated_error(a));
  737. archive_strncpy(&lha->filename, p + H0_FILE_NAME_OFFSET, namelen);
  738. /* When extdsize == -2, A CRC16 value is not present in the header. */
  739. if (extdsize >= 0) {
  740. lha->crc = archive_le16dec(p + H0_FILE_NAME_OFFSET + namelen);
  741. lha->setflag |= CRC_IS_SET;
  742. }
  743. sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
  744. /* Read an extended header */
  745. if (extdsize > 0) {
  746. /* This extended data is set by 'LHa for UNIX' only.
  747. * Maybe fixed size.
  748. */
  749. p += H0_FILE_NAME_OFFSET + namelen + 2;
  750. if (p[0] == 'U' && extdsize == 12) {
  751. /* p[1] is a minor version. */
  752. lha->mtime = archive_le32dec(&p[2]);
  753. lha->mode = archive_le16dec(&p[6]);
  754. lha->uid = archive_le16dec(&p[8]);
  755. lha->gid = archive_le16dec(&p[10]);
  756. lha->setflag |= UNIX_MODE_IS_SET;
  757. }
  758. }
  759. __archive_read_consume(a, lha->header_size);
  760. if (sum_calculated != headersum) {
  761. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  762. "LHa header sum error");
  763. return (ARCHIVE_FATAL);
  764. }
  765. return (ARCHIVE_OK);
  766. }
  767. /*
  768. * Header 1 format
  769. *
  770. * +0 +1 +2 +7 +11
  771. * +---------------+----------+----------------+-------------+
  772. * |header size(*1)|header sum|compression type|skip size(*2)|
  773. * +---------------+----------+----------------+-------------+
  774. * <---------------(*1)----------*
  775. *
  776. * +11 +15 +17 +19 +20 +21
  777. * +-----------------+---------+---------+--------------+----------------+
  778. * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=1)|
  779. * +-----------------+---------+---------+--------------+----------------+
  780. * *-------------------------------(*1)----------------------------------*
  781. *
  782. * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+3 +22+(*3)+3+(*4)
  783. * +---------------+---------+----------+-----------+-----------+
  784. * |name length(*3)|file name|file CRC16| creator |padding(*4)|
  785. * +---------------+---------+----------+-----------+-----------+
  786. * <--(*3)->
  787. * *----------------------------(*1)----------------------------*
  788. *
  789. * +22+(*3)+3+(*4) +22+(*3)+3+(*4)+2 +22+(*3)+3+(*4)+2+(*5)
  790. * +----------------+---------------------+------------------------+
  791. * |next header size| extended header(*5) | compressed data |
  792. * +----------------+---------------------+------------------------+
  793. * *------(*1)-----> <--------------------(*2)-------------------->
  794. */
  795. #define H1_HEADER_SIZE_OFFSET 0
  796. #define H1_HEADER_SUM_OFFSET 1
  797. #define H1_COMP_SIZE_OFFSET 7
  798. #define H1_ORIG_SIZE_OFFSET 11
  799. #define H1_DOS_TIME_OFFSET 15
  800. #define H1_NAME_LEN_OFFSET 21
  801. #define H1_FILE_NAME_OFFSET 22
  802. #define H1_FIXED_SIZE 27
  803. static int
  804. lha_read_file_header_1(struct archive_read *a, struct lha *lha)
  805. {
  806. const unsigned char *p;
  807. size_t extdsize;
  808. int i, err, err2;
  809. int namelen, padding;
  810. unsigned char headersum, sum_calculated;
  811. err = ARCHIVE_OK;
  812. if ((p = __archive_read_ahead(a, H1_FIXED_SIZE, NULL)) == NULL)
  813. return (truncated_error(a));
  814. lha->header_size = p[H1_HEADER_SIZE_OFFSET] + 2;
  815. headersum = p[H1_HEADER_SUM_OFFSET];
  816. /* Note: An extended header size is included in a compsize. */
  817. lha->compsize = archive_le32dec(p + H1_COMP_SIZE_OFFSET);
  818. lha->origsize = archive_le32dec(p + H1_ORIG_SIZE_OFFSET);
  819. lha->mtime = lha_dos_time(p + H1_DOS_TIME_OFFSET);
  820. namelen = p[H1_NAME_LEN_OFFSET];
  821. /* Calculate a padding size. The result will be normally 0 only(?) */
  822. padding = ((int)lha->header_size) - H1_FIXED_SIZE - namelen;
  823. if (namelen > 230 || padding < 0)
  824. goto invalid;
  825. if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
  826. return (truncated_error(a));
  827. for (i = 0; i < namelen; i++) {
  828. if (p[i + H1_FILE_NAME_OFFSET] == 0xff)
  829. goto invalid;/* Invalid filename. */
  830. }
  831. archive_strncpy(&lha->filename, p + H1_FILE_NAME_OFFSET, namelen);
  832. lha->crc = archive_le16dec(p + H1_FILE_NAME_OFFSET + namelen);
  833. lha->setflag |= CRC_IS_SET;
  834. sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
  835. /* Consume used bytes but not include `next header size' data
  836. * since it will be consumed in lha_read_file_extended_header(). */
  837. __archive_read_consume(a, lha->header_size - 2);
  838. /* Read extended headers */
  839. err2 = lha_read_file_extended_header(a, lha, NULL, 2,
  840. (size_t)(lha->compsize + 2), &extdsize);
  841. if (err2 < ARCHIVE_WARN)
  842. return (err2);
  843. if (err2 < err)
  844. err = err2;
  845. /* Get a real compressed file size. */
  846. lha->compsize -= extdsize - 2;
  847. if (lha->compsize < 0)
  848. goto invalid; /* Invalid compressed file size */
  849. if (sum_calculated != headersum) {
  850. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  851. "LHa header sum error");
  852. return (ARCHIVE_FATAL);
  853. }
  854. return (err);
  855. invalid:
  856. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  857. "Invalid LHa header");
  858. return (ARCHIVE_FATAL);
  859. }
  860. /*
  861. * Header 2 format
  862. *
  863. * +0 +2 +7 +11 +15
  864. * +---------------+----------------+-------------------+-----------------+
  865. * |header size(*1)|compression type|compressed size(*2)|uncompressed size|
  866. * +---------------+----------------+-------------------+-----------------+
  867. * <--------------------------------(*1)---------------------------------*
  868. *
  869. * +15 +19 +20 +21 +23 +24
  870. * +-----------------+------------+----------------+----------+-----------+
  871. * |data/time(time_t)| 0x20 fixed |header level(=2)|file CRC16| creator |
  872. * +-----------------+------------+----------------+----------+-----------+
  873. * *---------------------------------(*1)---------------------------------*
  874. *
  875. * +24 +26 +26+(*3) +26+(*3)+(*4)
  876. * +----------------+-------------------+-------------+-------------------+
  877. * |next header size|extended header(*3)| padding(*4) | compressed data |
  878. * +----------------+-------------------+-------------+-------------------+
  879. * *--------------------------(*1)-------------------> <------(*2)------->
  880. *
  881. */
  882. #define H2_HEADER_SIZE_OFFSET 0
  883. #define H2_COMP_SIZE_OFFSET 7
  884. #define H2_ORIG_SIZE_OFFSET 11
  885. #define H2_TIME_OFFSET 15
  886. #define H2_CRC_OFFSET 21
  887. #define H2_FIXED_SIZE 24
  888. static int
  889. lha_read_file_header_2(struct archive_read *a, struct lha *lha)
  890. {
  891. const unsigned char *p;
  892. size_t extdsize;
  893. int err, padding;
  894. uint16_t header_crc;
  895. if ((p = __archive_read_ahead(a, H2_FIXED_SIZE, NULL)) == NULL)
  896. return (truncated_error(a));
  897. lha->header_size =archive_le16dec(p + H2_HEADER_SIZE_OFFSET);
  898. lha->compsize = archive_le32dec(p + H2_COMP_SIZE_OFFSET);
  899. lha->origsize = archive_le32dec(p + H2_ORIG_SIZE_OFFSET);
  900. lha->mtime = archive_le32dec(p + H2_TIME_OFFSET);
  901. lha->crc = archive_le16dec(p + H2_CRC_OFFSET);
  902. lha->setflag |= CRC_IS_SET;
  903. if (lha->header_size < H2_FIXED_SIZE) {
  904. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  905. "Invalid LHa header size");
  906. return (ARCHIVE_FATAL);
  907. }
  908. header_crc = lha_crc16(0, p, H2_FIXED_SIZE);
  909. __archive_read_consume(a, H2_FIXED_SIZE);
  910. /* Read extended headers */
  911. err = lha_read_file_extended_header(a, lha, &header_crc, 2,
  912. lha->header_size - H2_FIXED_SIZE, &extdsize);
  913. if (err < ARCHIVE_WARN)
  914. return (err);
  915. /* Calculate a padding size. The result will be normally 0 or 1. */
  916. padding = (int)lha->header_size - (int)(H2_FIXED_SIZE + extdsize);
  917. if (padding > 0) {
  918. if ((p = __archive_read_ahead(a, padding, NULL)) == NULL)
  919. return (truncated_error(a));
  920. header_crc = lha_crc16(header_crc, p, padding);
  921. __archive_read_consume(a, padding);
  922. }
  923. if (header_crc != lha->header_crc) {
  924. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  925. "LHa header CRC error");
  926. return (ARCHIVE_FATAL);
  927. }
  928. return (err);
  929. }
  930. /*
  931. * Header 3 format
  932. *
  933. * +0 +2 +7 +11 +15
  934. * +------------+----------------+-------------------+-----------------+
  935. * | 0x04 fixed |compression type|compressed size(*2)|uncompressed size|
  936. * +------------+----------------+-------------------+-----------------+
  937. * <-------------------------------(*1)-------------------------------*
  938. *
  939. * +15 +19 +20 +21 +23 +24
  940. * +-----------------+------------+----------------+----------+-----------+
  941. * |date/time(time_t)| 0x20 fixed |header level(=3)|file CRC16| creator |
  942. * +-----------------+------------+----------------+----------+-----------+
  943. * *--------------------------------(*1)----------------------------------*
  944. *
  945. * +24 +28 +32 +32+(*3)
  946. * +---------------+----------------+-------------------+-----------------+
  947. * |header size(*1)|next header size|extended header(*3)| compressed data |
  948. * +---------------+----------------+-------------------+-----------------+
  949. * *------------------------(*1)-----------------------> <------(*2)----->
  950. *
  951. */
  952. #define H3_FIELD_LEN_OFFSET 0
  953. #define H3_COMP_SIZE_OFFSET 7
  954. #define H3_ORIG_SIZE_OFFSET 11
  955. #define H3_TIME_OFFSET 15
  956. #define H3_CRC_OFFSET 21
  957. #define H3_HEADER_SIZE_OFFSET 24
  958. #define H3_FIXED_SIZE 28
  959. static int
  960. lha_read_file_header_3(struct archive_read *a, struct lha *lha)
  961. {
  962. const unsigned char *p;
  963. size_t extdsize;
  964. int err;
  965. uint16_t header_crc;
  966. if ((p = __archive_read_ahead(a, H3_FIXED_SIZE, NULL)) == NULL)
  967. return (truncated_error(a));
  968. if (archive_le16dec(p + H3_FIELD_LEN_OFFSET) != 4)
  969. goto invalid;
  970. lha->header_size =archive_le32dec(p + H3_HEADER_SIZE_OFFSET);
  971. lha->compsize = archive_le32dec(p + H3_COMP_SIZE_OFFSET);
  972. lha->origsize = archive_le32dec(p + H3_ORIG_SIZE_OFFSET);
  973. lha->mtime = archive_le32dec(p + H3_TIME_OFFSET);
  974. lha->crc = archive_le16dec(p + H3_CRC_OFFSET);
  975. lha->setflag |= CRC_IS_SET;
  976. if (lha->header_size < H3_FIXED_SIZE + 4)
  977. goto invalid;
  978. header_crc = lha_crc16(0, p, H3_FIXED_SIZE);
  979. __archive_read_consume(a, H3_FIXED_SIZE);
  980. /* Read extended headers */
  981. err = lha_read_file_extended_header(a, lha, &header_crc, 4,
  982. lha->header_size - H3_FIXED_SIZE, &extdsize);
  983. if (err < ARCHIVE_WARN)
  984. return (err);
  985. if (header_crc != lha->header_crc) {
  986. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  987. "LHa header CRC error");
  988. return (ARCHIVE_FATAL);
  989. }
  990. return (err);
  991. invalid:
  992. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  993. "Invalid LHa header");
  994. return (ARCHIVE_FATAL);
  995. }
  996. /*
  997. * Extended header format
  998. *
  999. * +0 +2 +3 -- used in header 1 and 2
  1000. * +0 +4 +5 -- used in header 3
  1001. * +--------------+---------+-------------------+--------------+--
  1002. * |ex-header size|header id| data |ex-header size| .......
  1003. * +--------------+---------+-------------------+--------------+--
  1004. * <-------------( ex-header size)------------> <-- next extended header --*
  1005. *
  1006. * If the ex-header size is zero, it is the make of the end of extended
  1007. * headers.
  1008. *
  1009. */
  1010. static int
  1011. lha_read_file_extended_header(struct archive_read *a, struct lha *lha,
  1012. uint16_t *crc, int sizefield_length, size_t limitsize, size_t *total_size)
  1013. {
  1014. const void *h;
  1015. const unsigned char *extdheader;
  1016. size_t extdsize;
  1017. size_t datasize;
  1018. unsigned int i;
  1019. unsigned char extdtype;
  1020. #define EXT_HEADER_CRC 0x00 /* Header CRC and information*/
  1021. #define EXT_FILENAME 0x01 /* Filename */
  1022. #define EXT_DIRECTORY 0x02 /* Directory name */
  1023. #define EXT_DOS_ATTR 0x40 /* MS-DOS attribute */
  1024. #define EXT_TIMESTAMP 0x41 /* Windows time stamp */
  1025. #define EXT_FILESIZE 0x42 /* Large file size */
  1026. #define EXT_TIMEZONE 0x43 /* Time zone */
  1027. #define EXT_UTF16_FILENAME 0x44 /* UTF-16 filename */
  1028. #define EXT_UTF16_DIRECTORY 0x45 /* UTF-16 directory name */
  1029. #define EXT_CODEPAGE 0x46 /* Codepage */
  1030. #define EXT_UNIX_MODE 0x50 /* File permission */
  1031. #define EXT_UNIX_GID_UID 0x51 /* gid,uid */
  1032. #define EXT_UNIX_GNAME 0x52 /* Group name */
  1033. #define EXT_UNIX_UNAME 0x53 /* User name */
  1034. #define EXT_UNIX_MTIME 0x54 /* Modified time */
  1035. #define EXT_OS2_NEW_ATTR 0x7f /* new attribute(OS/2 only) */
  1036. #define EXT_NEW_ATTR 0xff /* new attribute */
  1037. *total_size = sizefield_length;
  1038. for (;;) {
  1039. /* Read an extended header size. */
  1040. if ((h =
  1041. __archive_read_ahead(a, sizefield_length, NULL)) == NULL)
  1042. return (truncated_error(a));
  1043. /* Check if the size is the zero indicates the end of the
  1044. * extended header. */
  1045. if (sizefield_length == sizeof(uint16_t))
  1046. extdsize = archive_le16dec(h);
  1047. else
  1048. extdsize = archive_le32dec(h);
  1049. if (extdsize == 0) {
  1050. /* End of extended header */
  1051. if (crc != NULL)
  1052. *crc = lha_crc16(*crc, h, sizefield_length);
  1053. __archive_read_consume(a, sizefield_length);
  1054. return (ARCHIVE_OK);
  1055. }
  1056. /* Sanity check to the extended header size. */
  1057. if (((uint64_t)*total_size + extdsize) >
  1058. (uint64_t)limitsize ||
  1059. extdsize <= (size_t)sizefield_length)
  1060. goto invalid;
  1061. /* Read the extended header. */
  1062. if ((h = __archive_read_ahead(a, extdsize, NULL)) == NULL)
  1063. return (truncated_error(a));
  1064. *total_size += extdsize;
  1065. extdheader = (const unsigned char *)h;
  1066. /* Get the extended header type. */
  1067. extdtype = extdheader[sizefield_length];
  1068. /* Calculate an extended data size. */
  1069. datasize = extdsize - (1 + sizefield_length);
  1070. /* Skip an extended header size field and type field. */
  1071. extdheader += sizefield_length + 1;
  1072. if (crc != NULL && extdtype != EXT_HEADER_CRC)
  1073. *crc = lha_crc16(*crc, h, extdsize);
  1074. switch (extdtype) {
  1075. case EXT_HEADER_CRC:
  1076. /* We only use a header CRC. Following data will not
  1077. * be used. */
  1078. if (datasize >= 2) {
  1079. lha->header_crc = archive_le16dec(extdheader);
  1080. if (crc != NULL) {
  1081. static const char zeros[2] = {0, 0};
  1082. *crc = lha_crc16(*crc, h,
  1083. extdsize - datasize);
  1084. /* CRC value itself as zero */
  1085. *crc = lha_crc16(*crc, zeros, 2);
  1086. *crc = lha_crc16(*crc,
  1087. extdheader+2, datasize - 2);
  1088. }
  1089. }
  1090. break;
  1091. case EXT_FILENAME:
  1092. if (datasize == 0) {
  1093. /* maybe directory header */
  1094. archive_string_empty(&lha->filename);
  1095. break;
  1096. }
  1097. if (extdheader[0] == '\0')
  1098. goto invalid;
  1099. archive_strncpy(&lha->filename,
  1100. (const char *)extdheader, datasize);
  1101. break;
  1102. case EXT_DIRECTORY:
  1103. if (datasize == 0 || extdheader[0] == '\0')
  1104. /* no directory name data. exit this case. */
  1105. goto invalid;
  1106. archive_strncpy(&lha->dirname,
  1107. (const char *)extdheader, datasize);
  1108. /*
  1109. * Convert directory delimiter from 0xFF
  1110. * to '/' for local system.
  1111. */
  1112. for (i = 0; i < lha->dirname.length; i++) {
  1113. if ((unsigned char)lha->dirname.s[i] == 0xFF)
  1114. lha->dirname.s[i] = '/';
  1115. }
  1116. /* Is last character directory separator? */
  1117. if (lha->dirname.s[lha->dirname.length-1] != '/')
  1118. /* invalid directory data */
  1119. goto invalid;
  1120. break;
  1121. case EXT_DOS_ATTR:
  1122. if (datasize == 2)
  1123. lha->dos_attr = (unsigned char)
  1124. (archive_le16dec(extdheader) & 0xff);
  1125. break;
  1126. case EXT_TIMESTAMP:
  1127. if (datasize == (sizeof(uint64_t) * 3)) {
  1128. lha->birthtime = lha_win_time(
  1129. archive_le64dec(extdheader),
  1130. &lha->birthtime_tv_nsec);
  1131. extdheader += sizeof(uint64_t);
  1132. lha->mtime = lha_win_time(
  1133. archive_le64dec(extdheader),
  1134. &lha->mtime_tv_nsec);
  1135. extdheader += sizeof(uint64_t);
  1136. lha->atime = lha_win_time(
  1137. archive_le64dec(extdheader),
  1138. &lha->atime_tv_nsec);
  1139. lha->setflag |= BIRTHTIME_IS_SET |
  1140. ATIME_IS_SET;
  1141. }
  1142. break;
  1143. case EXT_FILESIZE:
  1144. if (datasize == sizeof(uint64_t) * 2) {
  1145. lha->compsize = archive_le64dec(extdheader);
  1146. extdheader += sizeof(uint64_t);
  1147. lha->origsize = archive_le64dec(extdheader);
  1148. }
  1149. break;
  1150. case EXT_CODEPAGE:
  1151. /* Get an archived filename charset from codepage.
  1152. * This overwrites the charset specified by
  1153. * hdrcharset option. */
  1154. if (datasize == sizeof(uint32_t)) {
  1155. struct archive_string cp;
  1156. const char *charset;
  1157. archive_string_init(&cp);
  1158. switch (archive_le32dec(extdheader)) {
  1159. case 65001: /* UTF-8 */
  1160. charset = "UTF-8";
  1161. break;
  1162. default:
  1163. archive_string_sprintf(&cp, "CP%d",
  1164. (int)archive_le32dec(extdheader));
  1165. charset = cp.s;
  1166. break;
  1167. }
  1168. lha->sconv =
  1169. archive_string_conversion_from_charset(
  1170. &(a->archive), charset, 1);
  1171. archive_string_free(&cp);
  1172. if (lha->sconv == NULL)
  1173. return (ARCHIVE_FATAL);
  1174. }
  1175. break;
  1176. case EXT_UNIX_MODE:
  1177. if (datasize == sizeof(uint16_t)) {
  1178. lha->mode = archive_le16dec(extdheader);
  1179. lha->setflag |= UNIX_MODE_IS_SET;
  1180. }
  1181. break;
  1182. case EXT_UNIX_GID_UID:
  1183. if (datasize == (sizeof(uint16_t) * 2)) {
  1184. lha->gid = archive_le16dec(extdheader);
  1185. lha->uid = archive_le16dec(extdheader+2);
  1186. }
  1187. break;
  1188. case EXT_UNIX_GNAME:
  1189. if (datasize > 0)
  1190. archive_strncpy(&lha->gname,
  1191. (const char *)extdheader, datasize);
  1192. break;
  1193. case EXT_UNIX_UNAME:
  1194. if (datasize > 0)
  1195. archive_strncpy(&lha->uname,
  1196. (const char *)extdheader, datasize);
  1197. break;
  1198. case EXT_UNIX_MTIME:
  1199. if (datasize == sizeof(uint32_t))
  1200. lha->mtime = archive_le32dec(extdheader);
  1201. break;
  1202. case EXT_OS2_NEW_ATTR:
  1203. /* This extended header is OS/2 depend. */
  1204. if (datasize == 16) {
  1205. lha->dos_attr = (unsigned char)
  1206. (archive_le16dec(extdheader) & 0xff);
  1207. lha->mode = archive_le16dec(extdheader+2);
  1208. lha->gid = archive_le16dec(extdheader+4);
  1209. lha->uid = archive_le16dec(extdheader+6);
  1210. lha->birthtime = archive_le32dec(extdheader+8);
  1211. lha->atime = archive_le32dec(extdheader+12);
  1212. lha->setflag |= UNIX_MODE_IS_SET
  1213. | BIRTHTIME_IS_SET | ATIME_IS_SET;
  1214. }
  1215. break;
  1216. case EXT_NEW_ATTR:
  1217. if (datasize == 20) {
  1218. lha->mode = (mode_t)archive_le32dec(extdheader);
  1219. lha->gid = archive_le32dec(extdheader+4);
  1220. lha->uid = archive_le32dec(extdheader+8);
  1221. lha->birthtime = archive_le32dec(extdheader+12);
  1222. lha->atime = archive_le32dec(extdheader+16);
  1223. lha->setflag |= UNIX_MODE_IS_SET
  1224. | BIRTHTIME_IS_SET | ATIME_IS_SET;
  1225. }
  1226. break;
  1227. case EXT_TIMEZONE: /* Not supported */
  1228. case EXT_UTF16_FILENAME: /* Not supported */
  1229. case EXT_UTF16_DIRECTORY: /* Not supported */
  1230. default:
  1231. break;
  1232. }
  1233. __archive_read_consume(a, extdsize);
  1234. }
  1235. invalid:
  1236. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1237. "Invalid extended LHa header");
  1238. return (ARCHIVE_FATAL);
  1239. }
  1240. static int
  1241. lha_end_of_entry(struct archive_read *a)
  1242. {
  1243. struct lha *lha = (struct lha *)(a->format->data);
  1244. int r = ARCHIVE_EOF;
  1245. if (!lha->end_of_entry_cleanup) {
  1246. if ((lha->setflag & CRC_IS_SET) &&
  1247. lha->crc != lha->entry_crc_calculated) {
  1248. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1249. "LHa data CRC error");
  1250. r = ARCHIVE_WARN;
  1251. }
  1252. /* End-of-entry cleanup done. */
  1253. lha->end_of_entry_cleanup = 1;
  1254. }
  1255. return (r);
  1256. }
  1257. static int
  1258. archive_read_format_lha_read_data(struct archive_read *a,
  1259. const void **buff, size_t *size, int64_t *offset)
  1260. {
  1261. struct lha *lha = (struct lha *)(a->format->data);
  1262. int r;
  1263. if (lha->entry_unconsumed) {
  1264. /* Consume as much as the decompressor actually used. */
  1265. __archive_read_consume(a, lha->entry_unconsumed);
  1266. lha->entry_unconsumed = 0;
  1267. }
  1268. if (lha->end_of_entry) {
  1269. *offset = lha->entry_offset;
  1270. *size = 0;
  1271. *buff = NULL;
  1272. return (lha_end_of_entry(a));
  1273. }
  1274. if (lha->entry_is_compressed)
  1275. r = lha_read_data_lzh(a, buff, size, offset);
  1276. else
  1277. /* No compression. */
  1278. r = lha_read_data_none(a, buff, size, offset);
  1279. return (r);
  1280. }
  1281. /*
  1282. * Read a file content in no compression.
  1283. *
  1284. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  1285. * lha->end_of_entry if it consumes all of the data.
  1286. */
  1287. static int
  1288. lha_read_data_none(struct archive_read *a, const void **buff,
  1289. size_t *size, int64_t *offset)
  1290. {
  1291. struct lha *lha = (struct lha *)(a->format->data);
  1292. ssize_t bytes_avail;
  1293. if (lha->entry_bytes_remaining == 0) {
  1294. *buff = NULL;
  1295. *size = 0;
  1296. *offset = lha->entry_offset;
  1297. lha->end_of_entry = 1;
  1298. return (ARCHIVE_OK);
  1299. }
  1300. /*
  1301. * Note: '1' here is a performance optimization.
  1302. * Recall that the decompression layer returns a count of
  1303. * available bytes; asking for more than that forces the
  1304. * decompressor to combine reads by copying data.
  1305. */
  1306. *buff = __archive_read_ahead(a, 1, &bytes_avail);
  1307. if (bytes_avail <= 0) {
  1308. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1309. "Truncated LHa file data");
  1310. return (ARCHIVE_FATAL);
  1311. }
  1312. if (bytes_avail > lha->entry_bytes_remaining)
  1313. bytes_avail = (ssize_t)lha->entry_bytes_remaining;
  1314. lha->entry_crc_calculated =
  1315. lha_crc16(lha->entry_crc_calculated, *buff, bytes_avail);
  1316. *size = bytes_avail;
  1317. *offset = lha->entry_offset;
  1318. lha->entry_offset += bytes_avail;
  1319. lha->entry_bytes_remaining -= bytes_avail;
  1320. if (lha->entry_bytes_remaining == 0)
  1321. lha->end_of_entry = 1;
  1322. lha->entry_unconsumed = bytes_avail;
  1323. return (ARCHIVE_OK);
  1324. }
  1325. /*
  1326. * Read a file content in LZHUFF encoding.
  1327. *
  1328. * Returns ARCHIVE_OK if successful, returns ARCHIVE_WARN if compression is
  1329. * unsupported, ARCHIVE_FATAL otherwise, sets lha->end_of_entry if it consumes
  1330. * all of the data.
  1331. */
  1332. static int
  1333. lha_read_data_lzh(struct archive_read *a, const void **buff,
  1334. size_t *size, int64_t *offset)
  1335. {
  1336. struct lha *lha = (struct lha *)(a->format->data);
  1337. ssize_t bytes_avail;
  1338. int r;
  1339. /* If we haven't yet read any data, initialize the decompressor. */
  1340. if (!lha->decompress_init) {
  1341. r = lzh_decode_init(&(lha->strm), lha->method);
  1342. switch (r) {
  1343. case ARCHIVE_OK:
  1344. break;
  1345. case ARCHIVE_FAILED:
  1346. /* Unsupported compression. */
  1347. *buff = NULL;
  1348. *size = 0;
  1349. *offset = 0;
  1350. archive_set_error(&a->archive,
  1351. ARCHIVE_ERRNO_FILE_FORMAT,
  1352. "Unsupported lzh compression method -%c%c%c-",
  1353. lha->method[0], lha->method[1], lha->method[2]);
  1354. /* We know compressed size; just skip it. */
  1355. archive_read_format_lha_read_data_skip(a);
  1356. return (ARCHIVE_WARN);
  1357. default:
  1358. archive_set_error(&a->archive, ENOMEM,
  1359. "Couldn't allocate memory "
  1360. "for lzh decompression");
  1361. return (ARCHIVE_FATAL);
  1362. }
  1363. /* We've initialized decompression for this stream. */
  1364. lha->decompress_init = 1;
  1365. lha->strm.avail_out = 0;
  1366. lha->strm.total_out = 0;
  1367. }
  1368. /*
  1369. * Note: '1' here is a performance optimization.
  1370. * Recall that the decompression layer returns a count of
  1371. * available bytes; asking for more than that forces the
  1372. * decompressor to combine reads by copying data.
  1373. */
  1374. lha->strm.next_in = __archive_read_ahead(a, 1, &bytes_avail);
  1375. if (bytes_avail <= 0) {
  1376. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1377. "Truncated LHa file body");
  1378. return (ARCHIVE_FATAL);
  1379. }
  1380. if (bytes_avail > lha->entry_bytes_remaining)
  1381. bytes_avail = (ssize_t)lha->entry_bytes_remaining;
  1382. lha->strm.avail_in = (int)bytes_avail;
  1383. lha->strm.total_in = 0;
  1384. lha->strm.avail_out = 0;
  1385. r = lzh_decode(&(lha->strm), bytes_avail == lha->entry_bytes_remaining);
  1386. switch (r) {
  1387. case ARCHIVE_OK:
  1388. break;
  1389. case ARCHIVE_EOF:
  1390. lha->end_of_entry = 1;
  1391. break;
  1392. default:
  1393. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1394. "Bad lzh data");
  1395. return (ARCHIVE_FAILED);
  1396. }
  1397. lha->entry_unconsumed = lha->strm.total_in;
  1398. lha->entry_bytes_remaining -= lha->strm.total_in;
  1399. if (lha->strm.avail_out) {
  1400. *offset = lha->entry_offset;
  1401. *size = lha->strm.avail_out;
  1402. *buff = lha->strm.ref_ptr;
  1403. lha->entry_crc_calculated =
  1404. lha_crc16(lha->entry_crc_calculated, *buff, *size);
  1405. lha->entry_offset += *size;
  1406. } else {
  1407. *offset = lha->entry_offset;
  1408. *size = 0;
  1409. *buff = NULL;
  1410. if (lha->end_of_entry)
  1411. return (lha_end_of_entry(a));
  1412. }
  1413. return (ARCHIVE_OK);
  1414. }
  1415. /*
  1416. * Skip a file content.
  1417. */
  1418. static int
  1419. archive_read_format_lha_read_data_skip(struct archive_read *a)
  1420. {
  1421. struct lha *lha;
  1422. int64_t bytes_skipped;
  1423. lha = (struct lha *)(a->format->data);
  1424. if (lha->entry_unconsumed) {
  1425. /* Consume as much as the decompressor actually used. */
  1426. __archive_read_consume(a, lha->entry_unconsumed);
  1427. lha->entry_unconsumed = 0;
  1428. }
  1429. /* if we've already read to end of data, we're done. */
  1430. if (lha->end_of_entry_cleanup)
  1431. return (ARCHIVE_OK);
  1432. /*
  1433. * If the length is at the beginning, we can skip the
  1434. * compressed data much more quickly.
  1435. */
  1436. bytes_skipped = __archive_read_consume(a, lha->entry_bytes_remaining);
  1437. if (bytes_skipped < 0)
  1438. return (ARCHIVE_FATAL);
  1439. /* This entry is finished and done. */
  1440. lha->end_of_entry_cleanup = lha->end_of_entry = 1;
  1441. return (ARCHIVE_OK);
  1442. }
  1443. static int
  1444. archive_read_format_lha_cleanup(struct archive_read *a)
  1445. {
  1446. struct lha *lha = (struct lha *)(a->format->data);
  1447. lzh_decode_free(&(lha->strm));
  1448. archive_string_free(&(lha->dirname));
  1449. archive_string_free(&(lha->filename));
  1450. archive_string_free(&(lha->uname));
  1451. archive_string_free(&(lha->gname));
  1452. archive_wstring_free(&(lha->ws));
  1453. free(lha);
  1454. (a->format->data) = NULL;
  1455. return (ARCHIVE_OK);
  1456. }
  1457. /*
  1458. * 'LHa for UNIX' utility has archived a symbolic-link name after
  1459. * a pathname with '|' character.
  1460. * This function extracts the symbolic-link name from the pathname.
  1461. *
  1462. * example.
  1463. * 1. a symbolic-name is 'aaa/bb/cc'
  1464. * 2. a filename is 'xxx/bbb'
  1465. * then a archived pathname is 'xxx/bbb|aaa/bb/cc'
  1466. */
  1467. static int
  1468. lha_parse_linkname(struct archive_string *linkname,
  1469. struct archive_string *pathname)
  1470. {
  1471. char * linkptr;
  1472. size_t symlen;
  1473. linkptr = strchr(pathname->s, '|');
  1474. if (linkptr != NULL) {
  1475. symlen = strlen(linkptr + 1);
  1476. archive_strncpy(linkname, linkptr+1, symlen);
  1477. *linkptr = 0;
  1478. pathname->length = strlen(pathname->s);
  1479. return (1);
  1480. }
  1481. return (0);
  1482. }
  1483. /* Convert an MSDOS-style date/time into Unix-style time. */
  1484. static time_t
  1485. lha_dos_time(const unsigned char *p)
  1486. {
  1487. int msTime, msDate;
  1488. struct tm ts;
  1489. msTime = archive_le16dec(p);
  1490. msDate = archive_le16dec(p+2);
  1491. memset(&ts, 0, sizeof(ts));
  1492. ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
  1493. ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
  1494. ts.tm_mday = msDate & 0x1f; /* Day of month. */
  1495. ts.tm_hour = (msTime >> 11) & 0x1f;
  1496. ts.tm_min = (msTime >> 5) & 0x3f;
  1497. ts.tm_sec = (msTime << 1) & 0x3e;
  1498. ts.tm_isdst = -1;
  1499. return (mktime(&ts));
  1500. }
  1501. /* Convert an MS-Windows-style date/time into Unix-style time. */
  1502. static time_t
  1503. lha_win_time(uint64_t wintime, long *ns)
  1504. {
  1505. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  1506. if (wintime >= EPOC_TIME) {
  1507. wintime -= EPOC_TIME; /* 1970-01-01 00:00:00 (UTC) */
  1508. if (ns != NULL)
  1509. *ns = (long)(wintime % 10000000) * 100;
  1510. return (wintime / 10000000);
  1511. } else {
  1512. if (ns != NULL)
  1513. *ns = 0;
  1514. return (0);
  1515. }
  1516. }
  1517. static unsigned char
  1518. lha_calcsum(unsigned char sum, const void *pp, int offset, size_t size)
  1519. {
  1520. unsigned char const *p = (unsigned char const *)pp;
  1521. p += offset;
  1522. for (;size > 0; --size)
  1523. sum += *p++;
  1524. return (sum);
  1525. }
  1526. static uint16_t crc16tbl[2][256];
  1527. static void
  1528. lha_crc16_init(void)
  1529. {
  1530. unsigned int i;
  1531. static int crc16init = 0;
  1532. if (crc16init)
  1533. return;
  1534. crc16init = 1;
  1535. for (i = 0; i < 256; i++) {
  1536. unsigned int j;
  1537. uint16_t crc = (uint16_t)i;
  1538. for (j = 8; j; j--)
  1539. crc = (crc >> 1) ^ ((crc & 1) * 0xA001);
  1540. crc16tbl[0][i] = crc;
  1541. }
  1542. for (i = 0; i < 256; i++) {
  1543. crc16tbl[1][i] = (crc16tbl[0][i] >> 8)
  1544. ^ crc16tbl[0][crc16tbl[0][i] & 0xff];
  1545. }
  1546. }
  1547. static uint16_t
  1548. lha_crc16(uint16_t crc, const void *pp, size_t len)
  1549. {
  1550. const unsigned char *p = (const unsigned char *)pp;
  1551. const uint16_t *buff;
  1552. const union {
  1553. uint32_t i;
  1554. char c[4];
  1555. } u = { 0x01020304 };
  1556. if (len == 0)
  1557. return crc;
  1558. /* Process unaligned address. */
  1559. if (((uintptr_t)p) & (uintptr_t)0x1) {
  1560. crc = (crc >> 8) ^ crc16tbl[0][(crc ^ *p++) & 0xff];
  1561. len--;
  1562. }
  1563. buff = (const uint16_t *)p;
  1564. /*
  1565. * Modern C compiler such as GCC does not unroll automatically yet
  1566. * without unrolling pragma, and Clang is so. So we should
  1567. * unroll this loop for its performance.
  1568. */
  1569. for (;len >= 8; len -= 8) {
  1570. /* This if statement expects compiler optimization will
  1571. * remove the statement which will not be executed. */
  1572. #undef bswap16
  1573. #ifndef __has_builtin
  1574. # define __has_builtin(x) 0
  1575. #endif
  1576. #if defined(_MSC_VER) && _MSC_VER >= 1400 /* Visual Studio */
  1577. # define bswap16(x) _byteswap_ushort(x)
  1578. #elif defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
  1579. /* GCC 4.8 and later has __builtin_bswap16() */
  1580. # define bswap16(x) __builtin_bswap16(x)
  1581. #elif defined(__clang__) && __has_builtin(__builtin_bswap16)
  1582. /* All clang versions have __builtin_bswap16() */
  1583. # define bswap16(x) __builtin_bswap16(x)
  1584. #else
  1585. # define bswap16(x) ((((x) >> 8) & 0xff) | ((x) << 8))
  1586. #endif
  1587. #define CRC16W do { \
  1588. if(u.c[0] == 1) { /* Big endian */ \
  1589. crc ^= bswap16(*buff); buff++; \
  1590. } else \
  1591. crc ^= *buff++; \
  1592. crc = crc16tbl[1][crc & 0xff] ^ crc16tbl[0][crc >> 8];\
  1593. } while (0)
  1594. CRC16W;
  1595. CRC16W;
  1596. CRC16W;
  1597. CRC16W;
  1598. #undef CRC16W
  1599. #undef bswap16
  1600. }
  1601. p = (const unsigned char *)buff;
  1602. for (;len; len--) {
  1603. crc = (crc >> 8) ^ crc16tbl[0][(crc ^ *p++) & 0xff];
  1604. }
  1605. return crc;
  1606. }
  1607. /*
  1608. * Initialize LZHUF decoder.
  1609. *
  1610. * Returns ARCHIVE_OK if initialization was successful.
  1611. * Returns ARCHIVE_FAILED if method is unsupported.
  1612. * Returns ARCHIVE_FATAL if initialization failed; memory allocation
  1613. * error occurred.
  1614. */
  1615. static int
  1616. lzh_decode_init(struct lzh_stream *strm, const char *method)
  1617. {
  1618. struct lzh_dec *ds;
  1619. int w_bits, w_size;
  1620. if (strm->ds == NULL) {
  1621. strm->ds = calloc(1, sizeof(*strm->ds));
  1622. if (strm->ds == NULL)
  1623. return (ARCHIVE_FATAL);
  1624. }
  1625. ds = strm->ds;
  1626. ds->error = ARCHIVE_FAILED;
  1627. if (method == NULL || method[0] != 'l' || method[1] != 'h')
  1628. return (ARCHIVE_FAILED);
  1629. switch (method[2]) {
  1630. case '5':
  1631. w_bits = 13;/* 8KiB for window */
  1632. break;
  1633. case '6':
  1634. w_bits = 15;/* 32KiB for window */
  1635. break;
  1636. case '7':
  1637. w_bits = 16;/* 64KiB for window */
  1638. break;
  1639. default:
  1640. return (ARCHIVE_FAILED);/* Not supported. */
  1641. }
  1642. ds->error = ARCHIVE_FATAL;
  1643. /* Expand a window size up to 128 KiB for decompressing process
  1644. * performance whatever its original window size is. */
  1645. ds->w_size = 1U << 17;
  1646. ds->w_mask = ds->w_size -1;
  1647. if (ds->w_buff == NULL) {
  1648. ds->w_buff = malloc(ds->w_size);
  1649. if (ds->w_buff == NULL)
  1650. return (ARCHIVE_FATAL);
  1651. }
  1652. w_size = 1U << w_bits;
  1653. memset(ds->w_buff + ds->w_size - w_size, 0x20, w_size);
  1654. ds->w_pos = 0;
  1655. ds->state = 0;
  1656. ds->pos_pt_len_size = w_bits + 1;
  1657. ds->pos_pt_len_bits = (w_bits == 15 || w_bits == 16)? 5: 4;
  1658. ds->literal_pt_len_size = PT_BITLEN_SIZE;
  1659. ds->literal_pt_len_bits = 5;
  1660. ds->br.cache_buffer = 0;
  1661. ds->br.cache_avail = 0;
  1662. if (lzh_huffman_init(&(ds->lt), LT_BITLEN_SIZE, 16)
  1663. != ARCHIVE_OK)
  1664. return (ARCHIVE_FATAL);
  1665. ds->lt.len_bits = 9;
  1666. if (lzh_huffman_init(&(ds->pt), PT_BITLEN_SIZE, 16)
  1667. != ARCHIVE_OK)
  1668. return (ARCHIVE_FATAL);
  1669. ds->error = 0;
  1670. return (ARCHIVE_OK);
  1671. }
  1672. /*
  1673. * Release LZHUF decoder.
  1674. */
  1675. static void
  1676. lzh_decode_free(struct lzh_stream *strm)
  1677. {
  1678. if (strm->ds == NULL)
  1679. return;
  1680. free(strm->ds->w_buff);
  1681. lzh_huffman_free(&(strm->ds->lt));
  1682. lzh_huffman_free(&(strm->ds->pt));
  1683. free(strm->ds);
  1684. strm->ds = NULL;
  1685. }
  1686. /*
  1687. * Bit stream reader.
  1688. */
  1689. /* Check that the cache buffer has enough bits. */
  1690. #define lzh_br_has(br, n) ((br)->cache_avail >= n)
  1691. /* Get compressed data by bit. */
  1692. #define lzh_br_bits(br, n) \
  1693. (((uint16_t)((br)->cache_buffer >> \
  1694. ((br)->cache_avail - (n)))) & cache_masks[n])
  1695. #define lzh_br_bits_forced(br, n) \
  1696. (((uint16_t)((br)->cache_buffer << \
  1697. ((n) - (br)->cache_avail))) & cache_masks[n])
  1698. /* Read ahead to make sure the cache buffer has enough compressed data we
  1699. * will use.
  1700. * True : completed, there is enough data in the cache buffer.
  1701. * False : we met that strm->next_in is empty, we have to get following
  1702. * bytes. */
  1703. #define lzh_br_read_ahead_0(strm, br, n) \
  1704. (lzh_br_has(br, (n)) || lzh_br_fillup(strm, br))
  1705. /* True : the cache buffer has some bits as much as we need.
  1706. * False : there are no enough bits in the cache buffer to be used,
  1707. * we have to get following bytes if we could. */
  1708. #define lzh_br_read_ahead(strm, br, n) \
  1709. (lzh_br_read_ahead_0((strm), (br), (n)) || lzh_br_has((br), (n)))
  1710. /* Notify how many bits we consumed. */
  1711. #define lzh_br_consume(br, n) ((br)->cache_avail -= (n))
  1712. #define lzh_br_unconsume(br, n) ((br)->cache_avail += (n))
  1713. static const uint16_t cache_masks[] = {
  1714. 0x0000, 0x0001, 0x0003, 0x0007,
  1715. 0x000F, 0x001F, 0x003F, 0x007F,
  1716. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  1717. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF,
  1718. 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF
  1719. };
  1720. /*
  1721. * Shift away used bits in the cache data and fill it up with following bits.
  1722. * Call this when cache buffer does not have enough bits you need.
  1723. *
  1724. * Returns 1 if the cache buffer is full.
  1725. * Returns 0 if the cache buffer is not full; input buffer is empty.
  1726. */
  1727. static int
  1728. lzh_br_fillup(struct lzh_stream *strm, struct lzh_br *br)
  1729. {
  1730. int n = CACHE_BITS - br->cache_avail;
  1731. for (;;) {
  1732. const int x = n >> 3;
  1733. if (strm->avail_in >= x) {
  1734. switch (x) {
  1735. case 8:
  1736. br->cache_buffer =
  1737. ((uint64_t)strm->next_in[0]) << 56 |
  1738. ((uint64_t)strm->next_in[1]) << 48 |
  1739. ((uint64_t)strm->next_in[2]) << 40 |
  1740. ((uint64_t)strm->next_in[3]) << 32 |
  1741. ((uint32_t)strm->next_in[4]) << 24 |
  1742. ((uint32_t)strm->next_in[5]) << 16 |
  1743. ((uint32_t)strm->next_in[6]) << 8 |
  1744. (uint32_t)strm->next_in[7];
  1745. strm->next_in += 8;
  1746. strm->avail_in -= 8;
  1747. br->cache_avail += 8 * 8;
  1748. return (1);
  1749. case 7:
  1750. br->cache_buffer =
  1751. (br->cache_buffer << 56) |
  1752. ((uint64_t)strm->next_in[0]) << 48 |
  1753. ((uint64_t)strm->next_in[1]) << 40 |
  1754. ((uint64_t)strm->next_in[2]) << 32 |
  1755. ((uint32_t)strm->next_in[3]) << 24 |
  1756. ((uint32_t)strm->next_in[4]) << 16 |
  1757. ((uint32_t)strm->next_in[5]) << 8 |
  1758. (uint32_t)strm->next_in[6];
  1759. strm->next_in += 7;
  1760. strm->avail_in -= 7;
  1761. br->cache_avail += 7 * 8;
  1762. return (1);
  1763. case 6:
  1764. br->cache_buffer =
  1765. (br->cache_buffer << 48) |
  1766. ((uint64_t)strm->next_in[0]) << 40 |
  1767. ((uint64_t)strm->next_in[1]) << 32 |
  1768. ((uint32_t)strm->next_in[2]) << 24 |
  1769. ((uint32_t)strm->next_in[3]) << 16 |
  1770. ((uint32_t)strm->next_in[4]) << 8 |
  1771. (uint32_t)strm->next_in[5];
  1772. strm->next_in += 6;
  1773. strm->avail_in -= 6;
  1774. br->cache_avail += 6 * 8;
  1775. return (1);
  1776. case 0:
  1777. /* We have enough compressed data in
  1778. * the cache buffer.*/
  1779. return (1);
  1780. default:
  1781. break;
  1782. }
  1783. }
  1784. if (strm->avail_in == 0) {
  1785. /* There is not enough compressed data to fill up the
  1786. * cache buffer. */
  1787. return (0);
  1788. }
  1789. br->cache_buffer =
  1790. (br->cache_buffer << 8) | *strm->next_in++;
  1791. strm->avail_in--;
  1792. br->cache_avail += 8;
  1793. n -= 8;
  1794. }
  1795. }
  1796. /*
  1797. * Decode LZHUF.
  1798. *
  1799. * 1. Returns ARCHIVE_OK if output buffer or input buffer are empty.
  1800. * Please set available buffer and call this function again.
  1801. * 2. Returns ARCHIVE_EOF if decompression has been completed.
  1802. * 3. Returns ARCHIVE_FAILED if an error occurred; compressed data
  1803. * is broken or you do not set 'last' flag properly.
  1804. * 4. 'last' flag is very important, you must set 1 to the flag if there
  1805. * is no input data. The lha compressed data format does not provide how
  1806. * to know the compressed data is really finished.
  1807. * Note: lha command utility check if the total size of output bytes is
  1808. * reached the uncompressed size recorded in its header. it does not mind
  1809. * that the decoding process is properly finished.
  1810. * GNU ZIP can decompress another compressed file made by SCO LZH compress.
  1811. * it handles EOF as null to fill read buffer with zero until the decoding
  1812. * process meet 2 bytes of zeros at reading a size of a next chunk, so the
  1813. * zeros are treated as the mark of the end of the data although the zeros
  1814. * is dummy, not the file data.
  1815. */
  1816. static int lzh_read_blocks(struct lzh_stream *, int);
  1817. static int lzh_decode_blocks(struct lzh_stream *, int);
  1818. #define ST_RD_BLOCK 0
  1819. #define ST_RD_PT_1 1
  1820. #define ST_RD_PT_2 2
  1821. #define ST_RD_PT_3 3
  1822. #define ST_RD_PT_4 4
  1823. #define ST_RD_LITERAL_1 5
  1824. #define ST_RD_LITERAL_2 6
  1825. #define ST_RD_LITERAL_3 7
  1826. #define ST_RD_POS_DATA_1 8
  1827. #define ST_GET_LITERAL 9
  1828. #define ST_GET_POS_1 10
  1829. #define ST_GET_POS_2 11
  1830. #define ST_COPY_DATA 12
  1831. static int
  1832. lzh_decode(struct lzh_stream *strm, int last)
  1833. {
  1834. struct lzh_dec *ds = strm->ds;
  1835. int avail_in;
  1836. int r;
  1837. if (ds->error)
  1838. return (ds->error);
  1839. avail_in = strm->avail_in;
  1840. do {
  1841. if (ds->state < ST_GET_LITERAL)
  1842. r = lzh_read_blocks(strm, last);
  1843. else
  1844. r = lzh_decode_blocks(strm, last);
  1845. } while (r == 100);
  1846. strm->total_in += avail_in - strm->avail_in;
  1847. return (r);
  1848. }
  1849. static void
  1850. lzh_emit_window(struct lzh_stream *strm, size_t s)
  1851. {
  1852. strm->ref_ptr = strm->ds->w_buff;
  1853. strm->avail_out = (int)s;
  1854. strm->total_out += s;
  1855. }
  1856. static int
  1857. lzh_read_blocks(struct lzh_stream *strm, int last)
  1858. {
  1859. struct lzh_dec *ds = strm->ds;
  1860. struct lzh_br *br = &(ds->br);
  1861. int c = 0, i;
  1862. unsigned rbits;
  1863. for (;;) {
  1864. switch (ds->state) {
  1865. case ST_RD_BLOCK:
  1866. /*
  1867. * Read a block number indicates how many blocks
  1868. * we will handle. The block is composed of a
  1869. * literal and a match, sometimes a literal only
  1870. * in particular, there are no reference data at
  1871. * the beginning of the decompression.
  1872. */
  1873. if (!lzh_br_read_ahead_0(strm, br, 16)) {
  1874. if (!last)
  1875. /* We need following data. */
  1876. return (ARCHIVE_OK);
  1877. if (lzh_br_has(br, 8)) {
  1878. /*
  1879. * It seems there are extra bits.
  1880. * 1. Compressed data is broken.
  1881. * 2. `last' flag does not properly
  1882. * set.
  1883. */
  1884. goto failed;
  1885. }
  1886. if (ds->w_pos > 0) {
  1887. lzh_emit_window(strm, ds->w_pos);
  1888. ds->w_pos = 0;
  1889. return (ARCHIVE_OK);
  1890. }
  1891. /* End of compressed data; we have completely
  1892. * handled all compressed data. */
  1893. return (ARCHIVE_EOF);
  1894. }
  1895. ds->blocks_avail = lzh_br_bits(br, 16);
  1896. if (ds->blocks_avail == 0)
  1897. goto failed;
  1898. lzh_br_consume(br, 16);
  1899. /*
  1900. * Read a literal table compressed in huffman
  1901. * coding.
  1902. */
  1903. ds->pt.len_size = ds->literal_pt_len_size;
  1904. ds->pt.len_bits = ds->literal_pt_len_bits;
  1905. ds->reading_position = 0;
  1906. /* FALL THROUGH */
  1907. case ST_RD_PT_1:
  1908. /* Note: ST_RD_PT_1, ST_RD_PT_2 and ST_RD_PT_4 are
  1909. * used in reading both a literal table and a
  1910. * position table. */
  1911. if (!lzh_br_read_ahead(strm, br, ds->pt.len_bits)) {
  1912. if (last)
  1913. goto failed;/* Truncated data. */
  1914. ds->state = ST_RD_PT_1;
  1915. return (ARCHIVE_OK);
  1916. }
  1917. ds->pt.len_avail = lzh_br_bits(br, ds->pt.len_bits);
  1918. lzh_br_consume(br, ds->pt.len_bits);
  1919. /* FALL THROUGH */
  1920. case ST_RD_PT_2:
  1921. if (ds->pt.len_avail == 0) {
  1922. /* There is no bitlen. */
  1923. if (!lzh_br_read_ahead(strm, br,
  1924. ds->pt.len_bits)) {
  1925. if (last)
  1926. goto failed;/* Truncated data.*/
  1927. ds->state = ST_RD_PT_2;
  1928. return (ARCHIVE_OK);
  1929. }
  1930. if (!lzh_make_fake_table(&(ds->pt),
  1931. lzh_br_bits(br, ds->pt.len_bits)))
  1932. goto failed;/* Invalid data. */
  1933. lzh_br_consume(br, ds->pt.len_bits);
  1934. if (ds->reading_position)
  1935. ds->state = ST_GET_LITERAL;
  1936. else
  1937. ds->state = ST_RD_LITERAL_1;
  1938. break;
  1939. } else if (ds->pt.len_avail > ds->pt.len_size)
  1940. goto failed;/* Invalid data. */
  1941. ds->loop = 0;
  1942. memset(ds->pt.freq, 0, sizeof(ds->pt.freq));
  1943. if (ds->pt.len_avail < 3 ||
  1944. ds->pt.len_size == ds->pos_pt_len_size) {
  1945. ds->state = ST_RD_PT_4;
  1946. break;
  1947. }
  1948. /* FALL THROUGH */
  1949. case ST_RD_PT_3:
  1950. ds->loop = lzh_read_pt_bitlen(strm, ds->loop, 3);
  1951. if (ds->loop < 3) {
  1952. if (ds->loop < 0 || last)
  1953. goto failed;/* Invalid data. */
  1954. /* Not completed, get following data. */
  1955. ds->state = ST_RD_PT_3;
  1956. return (ARCHIVE_OK);
  1957. }
  1958. /* There are some null in bitlen of the literal. */
  1959. if (!lzh_br_read_ahead(strm, br, 2)) {
  1960. if (last)
  1961. goto failed;/* Truncated data. */
  1962. ds->state = ST_RD_PT_3;
  1963. return (ARCHIVE_OK);
  1964. }
  1965. c = lzh_br_bits(br, 2);
  1966. lzh_br_consume(br, 2);
  1967. if (c > ds->pt.len_avail - 3)
  1968. goto failed;/* Invalid data. */
  1969. for (i = 3; c-- > 0 ;)
  1970. ds->pt.bitlen[i++] = 0;
  1971. ds->loop = i;
  1972. /* FALL THROUGH */
  1973. case ST_RD_PT_4:
  1974. ds->loop = lzh_read_pt_bitlen(strm, ds->loop,
  1975. ds->pt.len_avail);
  1976. if (ds->loop < ds->pt.len_avail) {
  1977. if (ds->loop < 0 || last)
  1978. goto failed;/* Invalid data. */
  1979. /* Not completed, get following data. */
  1980. ds->state = ST_RD_PT_4;
  1981. return (ARCHIVE_OK);
  1982. }
  1983. if (!lzh_make_huffman_table(&(ds->pt)))
  1984. goto failed;/* Invalid data */
  1985. if (ds->reading_position) {
  1986. ds->state = ST_GET_LITERAL;
  1987. break;
  1988. }
  1989. /* FALL THROUGH */
  1990. case ST_RD_LITERAL_1:
  1991. if (!lzh_br_read_ahead(strm, br, ds->lt.len_bits)) {
  1992. if (last)
  1993. goto failed;/* Truncated data. */
  1994. ds->state = ST_RD_LITERAL_1;
  1995. return (ARCHIVE_OK);
  1996. }
  1997. ds->lt.len_avail = lzh_br_bits(br, ds->lt.len_bits);
  1998. lzh_br_consume(br, ds->lt.len_bits);
  1999. /* FALL THROUGH */
  2000. case ST_RD_LITERAL_2:
  2001. if (ds->lt.len_avail == 0) {
  2002. /* There is no bitlen. */
  2003. if (!lzh_br_read_ahead(strm, br,
  2004. ds->lt.len_bits)) {
  2005. if (last)
  2006. goto failed;/* Truncated data.*/
  2007. ds->state = ST_RD_LITERAL_2;
  2008. return (ARCHIVE_OK);
  2009. }
  2010. if (!lzh_make_fake_table(&(ds->lt),
  2011. lzh_br_bits(br, ds->lt.len_bits)))
  2012. goto failed;/* Invalid data */
  2013. lzh_br_consume(br, ds->lt.len_bits);
  2014. ds->state = ST_RD_POS_DATA_1;
  2015. break;
  2016. } else if (ds->lt.len_avail > ds->lt.len_size)
  2017. goto failed;/* Invalid data */
  2018. ds->loop = 0;
  2019. memset(ds->lt.freq, 0, sizeof(ds->lt.freq));
  2020. /* FALL THROUGH */
  2021. case ST_RD_LITERAL_3:
  2022. i = ds->loop;
  2023. while (i < ds->lt.len_avail) {
  2024. if (!lzh_br_read_ahead(strm, br,
  2025. ds->pt.max_bits)) {
  2026. if (last)
  2027. goto failed;/* Truncated data.*/
  2028. ds->loop = i;
  2029. ds->state = ST_RD_LITERAL_3;
  2030. return (ARCHIVE_OK);
  2031. }
  2032. rbits = lzh_br_bits(br, ds->pt.max_bits);
  2033. c = lzh_decode_huffman(&(ds->pt), rbits);
  2034. if (c > 2) {
  2035. /* Note: 'c' will never be more than
  2036. * eighteen since it's limited by
  2037. * PT_BITLEN_SIZE, which is being set
  2038. * to ds->pt.len_size through
  2039. * ds->literal_pt_len_size. */
  2040. lzh_br_consume(br, ds->pt.bitlen[c]);
  2041. c -= 2;
  2042. ds->lt.freq[c]++;
  2043. ds->lt.bitlen[i++] = c;
  2044. } else if (c == 0) {
  2045. lzh_br_consume(br, ds->pt.bitlen[c]);
  2046. ds->lt.bitlen[i++] = 0;
  2047. } else {
  2048. /* c == 1 or c == 2 */
  2049. int n = (c == 1)?4:9;
  2050. if (!lzh_br_read_ahead(strm, br,
  2051. ds->pt.bitlen[c] + n)) {
  2052. if (last) /* Truncated data. */
  2053. goto failed;
  2054. ds->loop = i;
  2055. ds->state = ST_RD_LITERAL_3;
  2056. return (ARCHIVE_OK);
  2057. }
  2058. lzh_br_consume(br, ds->pt.bitlen[c]);
  2059. c = lzh_br_bits(br, n);
  2060. lzh_br_consume(br, n);
  2061. c += (n == 4)?3:20;
  2062. if (i + c > ds->lt.len_avail)
  2063. goto failed;/* Invalid data */
  2064. memset(&(ds->lt.bitlen[i]), 0, c);
  2065. i += c;
  2066. }
  2067. }
  2068. if (i > ds->lt.len_avail ||
  2069. !lzh_make_huffman_table(&(ds->lt)))
  2070. goto failed;/* Invalid data */
  2071. /* FALL THROUGH */
  2072. case ST_RD_POS_DATA_1:
  2073. /*
  2074. * Read a position table compressed in huffman
  2075. * coding.
  2076. */
  2077. ds->pt.len_size = ds->pos_pt_len_size;
  2078. ds->pt.len_bits = ds->pos_pt_len_bits;
  2079. ds->reading_position = 1;
  2080. ds->state = ST_RD_PT_1;
  2081. break;
  2082. case ST_GET_LITERAL:
  2083. return (100);
  2084. }
  2085. }
  2086. failed:
  2087. return (ds->error = ARCHIVE_FAILED);
  2088. }
  2089. static int
  2090. lzh_decode_blocks(struct lzh_stream *strm, int last)
  2091. {
  2092. struct lzh_dec *ds = strm->ds;
  2093. struct lzh_br bre = ds->br;
  2094. struct huffman *lt = &(ds->lt);
  2095. struct huffman *pt = &(ds->pt);
  2096. unsigned char *w_buff = ds->w_buff;
  2097. unsigned char *lt_bitlen = lt->bitlen;
  2098. unsigned char *pt_bitlen = pt->bitlen;
  2099. int blocks_avail = ds->blocks_avail, c = 0;
  2100. int copy_len = ds->copy_len, copy_pos = ds->copy_pos;
  2101. int w_pos = ds->w_pos, w_mask = ds->w_mask, w_size = ds->w_size;
  2102. int lt_max_bits = lt->max_bits, pt_max_bits = pt->max_bits;
  2103. int state = ds->state;
  2104. for (;;) {
  2105. switch (state) {
  2106. case ST_GET_LITERAL:
  2107. for (;;) {
  2108. if (blocks_avail == 0) {
  2109. /* We have decoded all blocks.
  2110. * Let's handle next blocks. */
  2111. ds->state = ST_RD_BLOCK;
  2112. ds->br = bre;
  2113. ds->blocks_avail = 0;
  2114. ds->w_pos = w_pos;
  2115. ds->copy_pos = 0;
  2116. return (100);
  2117. }
  2118. /* lzh_br_read_ahead() always try to fill the
  2119. * cache buffer up. In specific situation we
  2120. * are close to the end of the data, the cache
  2121. * buffer will not be full and thus we have to
  2122. * determine if the cache buffer has some bits
  2123. * as much as we need after lzh_br_read_ahead()
  2124. * failed. */
  2125. if (!lzh_br_read_ahead(strm, &bre,
  2126. lt_max_bits)) {
  2127. if (!last)
  2128. goto next_data;
  2129. /* Remaining bits are less than
  2130. * maximum bits(lt.max_bits) but maybe
  2131. * it still remains as much as we need,
  2132. * so we should try to use it with
  2133. * dummy bits. */
  2134. c = lzh_decode_huffman(lt,
  2135. lzh_br_bits_forced(&bre,
  2136. lt_max_bits));
  2137. lzh_br_consume(&bre, lt_bitlen[c]);
  2138. if (!lzh_br_has(&bre, 0))
  2139. goto failed;/* Over read. */
  2140. } else {
  2141. c = lzh_decode_huffman(lt,
  2142. lzh_br_bits(&bre, lt_max_bits));
  2143. lzh_br_consume(&bre, lt_bitlen[c]);
  2144. }
  2145. blocks_avail--;
  2146. if (c > UCHAR_MAX)
  2147. /* Current block is a match data. */
  2148. break;
  2149. /*
  2150. * 'c' is exactly a literal code.
  2151. */
  2152. /* Save a decoded code to reference it
  2153. * afterward. */
  2154. w_buff[w_pos] = c;
  2155. if (++w_pos >= w_size) {
  2156. w_pos = 0;
  2157. lzh_emit_window(strm, w_size);
  2158. goto next_data;
  2159. }
  2160. }
  2161. /* 'c' is the length of a match pattern we have
  2162. * already extracted, which has be stored in
  2163. * window(ds->w_buff). */
  2164. copy_len = c - (UCHAR_MAX + 1) + MINMATCH;
  2165. /* FALL THROUGH */
  2166. case ST_GET_POS_1:
  2167. /*
  2168. * Get a reference position.
  2169. */
  2170. if (!lzh_br_read_ahead(strm, &bre, pt_max_bits)) {
  2171. if (!last) {
  2172. state = ST_GET_POS_1;
  2173. ds->copy_len = copy_len;
  2174. goto next_data;
  2175. }
  2176. copy_pos = lzh_decode_huffman(pt,
  2177. lzh_br_bits_forced(&bre, pt_max_bits));
  2178. lzh_br_consume(&bre, pt_bitlen[copy_pos]);
  2179. if (!lzh_br_has(&bre, 0))
  2180. goto failed;/* Over read. */
  2181. } else {
  2182. copy_pos = lzh_decode_huffman(pt,
  2183. lzh_br_bits(&bre, pt_max_bits));
  2184. lzh_br_consume(&bre, pt_bitlen[copy_pos]);
  2185. }
  2186. /* FALL THROUGH */
  2187. case ST_GET_POS_2:
  2188. if (copy_pos > 1) {
  2189. /* We need an additional adjustment number to
  2190. * the position. */
  2191. int p = copy_pos - 1;
  2192. if (!lzh_br_read_ahead(strm, &bre, p)) {
  2193. if (last)
  2194. goto failed;/* Truncated data.*/
  2195. state = ST_GET_POS_2;
  2196. ds->copy_len = copy_len;
  2197. ds->copy_pos = copy_pos;
  2198. goto next_data;
  2199. }
  2200. copy_pos = (1 << p) + lzh_br_bits(&bre, p);
  2201. lzh_br_consume(&bre, p);
  2202. }
  2203. /* The position is actually a distance from the last
  2204. * code we had extracted and thus we have to convert
  2205. * it to a position of the window. */
  2206. copy_pos = (w_pos - copy_pos - 1) & w_mask;
  2207. /* FALL THROUGH */
  2208. case ST_COPY_DATA:
  2209. /*
  2210. * Copy `copy_len' bytes as extracted data from
  2211. * the window into the output buffer.
  2212. */
  2213. for (;;) {
  2214. int l;
  2215. l = copy_len;
  2216. if (copy_pos > w_pos) {
  2217. if (l > w_size - copy_pos)
  2218. l = w_size - copy_pos;
  2219. } else {
  2220. if (l > w_size - w_pos)
  2221. l = w_size - w_pos;
  2222. }
  2223. if ((copy_pos + l < w_pos)
  2224. || (w_pos + l < copy_pos)) {
  2225. /* No overlap. */
  2226. memcpy(w_buff + w_pos,
  2227. w_buff + copy_pos, l);
  2228. } else {
  2229. const unsigned char *s;
  2230. unsigned char *d;
  2231. int li;
  2232. d = w_buff + w_pos;
  2233. s = w_buff + copy_pos;
  2234. for (li = 0; li < l-1;) {
  2235. d[li] = s[li];li++;
  2236. d[li] = s[li];li++;
  2237. }
  2238. if (li < l)
  2239. d[li] = s[li];
  2240. }
  2241. w_pos += l;
  2242. if (w_pos == w_size) {
  2243. w_pos = 0;
  2244. lzh_emit_window(strm, w_size);
  2245. if (copy_len <= l)
  2246. state = ST_GET_LITERAL;
  2247. else {
  2248. state = ST_COPY_DATA;
  2249. ds->copy_len = copy_len - l;
  2250. ds->copy_pos =
  2251. (copy_pos + l) & w_mask;
  2252. }
  2253. goto next_data;
  2254. }
  2255. if (copy_len <= l)
  2256. /* A copy of current pattern ended. */
  2257. break;
  2258. copy_len -= l;
  2259. copy_pos = (copy_pos + l) & w_mask;
  2260. }
  2261. state = ST_GET_LITERAL;
  2262. break;
  2263. }
  2264. }
  2265. failed:
  2266. return (ds->error = ARCHIVE_FAILED);
  2267. next_data:
  2268. ds->br = bre;
  2269. ds->blocks_avail = blocks_avail;
  2270. ds->state = state;
  2271. ds->w_pos = w_pos;
  2272. return (ARCHIVE_OK);
  2273. }
  2274. static int
  2275. lzh_huffman_init(struct huffman *hf, size_t len_size, int tbl_bits)
  2276. {
  2277. int bits;
  2278. if (hf->bitlen == NULL) {
  2279. hf->bitlen = malloc(len_size * sizeof(hf->bitlen[0]));
  2280. if (hf->bitlen == NULL)
  2281. return (ARCHIVE_FATAL);
  2282. }
  2283. if (hf->tbl == NULL) {
  2284. if (tbl_bits < HTBL_BITS)
  2285. bits = tbl_bits;
  2286. else
  2287. bits = HTBL_BITS;
  2288. hf->tbl = malloc(((size_t)1 << bits) * sizeof(hf->tbl[0]));
  2289. if (hf->tbl == NULL)
  2290. return (ARCHIVE_FATAL);
  2291. }
  2292. if (hf->tree == NULL && tbl_bits > HTBL_BITS) {
  2293. hf->tree_avail = 1 << (tbl_bits - HTBL_BITS + 4);
  2294. hf->tree = malloc(hf->tree_avail * sizeof(hf->tree[0]));
  2295. if (hf->tree == NULL)
  2296. return (ARCHIVE_FATAL);
  2297. }
  2298. hf->len_size = (int)len_size;
  2299. hf->tbl_bits = tbl_bits;
  2300. return (ARCHIVE_OK);
  2301. }
  2302. static void
  2303. lzh_huffman_free(struct huffman *hf)
  2304. {
  2305. free(hf->bitlen);
  2306. free(hf->tbl);
  2307. free(hf->tree);
  2308. }
  2309. static const char bitlen_tbl[0x400] = {
  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. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2337. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2338. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2339. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2340. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2341. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  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. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2353. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2354. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2355. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2356. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  2357. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  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. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2361. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2362. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2363. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2364. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2365. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  2366. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2367. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2368. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2369. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  2370. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  2371. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  2372. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  2373. 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 0
  2374. };
  2375. static int
  2376. lzh_read_pt_bitlen(struct lzh_stream *strm, int start, int end)
  2377. {
  2378. struct lzh_dec *ds = strm->ds;
  2379. struct lzh_br *br = &(ds->br);
  2380. int c, i;
  2381. for (i = start; i < end; ) {
  2382. /*
  2383. * bit pattern the number we need
  2384. * 000 -> 0
  2385. * 001 -> 1
  2386. * 010 -> 2
  2387. * ...
  2388. * 110 -> 6
  2389. * 1110 -> 7
  2390. * 11110 -> 8
  2391. * ...
  2392. * 1111111111110 -> 16
  2393. */
  2394. if (!lzh_br_read_ahead(strm, br, 3))
  2395. return (i);
  2396. if ((c = lzh_br_bits(br, 3)) == 7) {
  2397. if (!lzh_br_read_ahead(strm, br, 13))
  2398. return (i);
  2399. c = bitlen_tbl[lzh_br_bits(br, 13) & 0x3FF];
  2400. if (c)
  2401. lzh_br_consume(br, c - 3);
  2402. else
  2403. return (-1);/* Invalid data. */
  2404. } else
  2405. lzh_br_consume(br, 3);
  2406. ds->pt.bitlen[i++] = c;
  2407. ds->pt.freq[c]++;
  2408. }
  2409. return (i);
  2410. }
  2411. static int
  2412. lzh_make_fake_table(struct huffman *hf, uint16_t c)
  2413. {
  2414. if (c >= hf->len_size)
  2415. return (0);
  2416. hf->tbl[0] = c;
  2417. hf->max_bits = 0;
  2418. hf->shift_bits = 0;
  2419. hf->bitlen[hf->tbl[0]] = 0;
  2420. return (1);
  2421. }
  2422. /*
  2423. * Make a huffman coding table.
  2424. */
  2425. static int
  2426. lzh_make_huffman_table(struct huffman *hf)
  2427. {
  2428. uint16_t *tbl;
  2429. const unsigned char *bitlen;
  2430. int bitptn[17], weight[17];
  2431. int i, maxbits = 0, ptn, tbl_size, w;
  2432. int diffbits, len_avail;
  2433. /*
  2434. * Initialize bit patterns.
  2435. */
  2436. ptn = 0;
  2437. for (i = 1, w = 1 << 15; i <= 16; i++, w >>= 1) {
  2438. bitptn[i] = ptn;
  2439. weight[i] = w;
  2440. if (hf->freq[i]) {
  2441. ptn += hf->freq[i] * w;
  2442. maxbits = i;
  2443. }
  2444. }
  2445. if (ptn != 0x10000 || maxbits > hf->tbl_bits)
  2446. return (0);/* Invalid */
  2447. hf->max_bits = maxbits;
  2448. /*
  2449. * Cut out extra bits which we won't house in the table.
  2450. * This preparation reduces the same calculation in the for-loop
  2451. * making the table.
  2452. */
  2453. if (maxbits < 16) {
  2454. int ebits = 16 - maxbits;
  2455. for (i = 1; i <= maxbits; i++) {
  2456. bitptn[i] >>= ebits;
  2457. weight[i] >>= ebits;
  2458. }
  2459. }
  2460. if (maxbits > HTBL_BITS) {
  2461. unsigned htbl_max;
  2462. uint16_t *p;
  2463. diffbits = maxbits - HTBL_BITS;
  2464. for (i = 1; i <= HTBL_BITS; i++) {
  2465. bitptn[i] >>= diffbits;
  2466. weight[i] >>= diffbits;
  2467. }
  2468. htbl_max = bitptn[HTBL_BITS] +
  2469. weight[HTBL_BITS] * hf->freq[HTBL_BITS];
  2470. p = &(hf->tbl[htbl_max]);
  2471. while (p < &hf->tbl[1U<<HTBL_BITS])
  2472. *p++ = 0;
  2473. } else
  2474. diffbits = 0;
  2475. hf->shift_bits = diffbits;
  2476. /*
  2477. * Make the table.
  2478. */
  2479. tbl_size = 1 << HTBL_BITS;
  2480. tbl = hf->tbl;
  2481. bitlen = hf->bitlen;
  2482. len_avail = hf->len_avail;
  2483. hf->tree_used = 0;
  2484. for (i = 0; i < len_avail; i++) {
  2485. uint16_t *p;
  2486. int len, cnt;
  2487. uint16_t bit;
  2488. int extlen;
  2489. struct htree_t *ht;
  2490. if (bitlen[i] == 0)
  2491. continue;
  2492. /* Get a bit pattern */
  2493. len = bitlen[i];
  2494. ptn = bitptn[len];
  2495. cnt = weight[len];
  2496. if (len <= HTBL_BITS) {
  2497. /* Calculate next bit pattern */
  2498. if ((bitptn[len] = ptn + cnt) > tbl_size)
  2499. return (0);/* Invalid */
  2500. /* Update the table */
  2501. p = &(tbl[ptn]);
  2502. if (cnt > 7) {
  2503. uint16_t *pc;
  2504. cnt -= 8;
  2505. pc = &p[cnt];
  2506. pc[0] = (uint16_t)i;
  2507. pc[1] = (uint16_t)i;
  2508. pc[2] = (uint16_t)i;
  2509. pc[3] = (uint16_t)i;
  2510. pc[4] = (uint16_t)i;
  2511. pc[5] = (uint16_t)i;
  2512. pc[6] = (uint16_t)i;
  2513. pc[7] = (uint16_t)i;
  2514. if (cnt > 7) {
  2515. cnt -= 8;
  2516. memcpy(&p[cnt], pc,
  2517. 8 * sizeof(uint16_t));
  2518. pc = &p[cnt];
  2519. while (cnt > 15) {
  2520. cnt -= 16;
  2521. memcpy(&p[cnt], pc,
  2522. 16 * sizeof(uint16_t));
  2523. }
  2524. }
  2525. if (cnt)
  2526. memcpy(p, pc, cnt * sizeof(uint16_t));
  2527. } else {
  2528. while (cnt > 1) {
  2529. p[--cnt] = (uint16_t)i;
  2530. p[--cnt] = (uint16_t)i;
  2531. }
  2532. if (cnt)
  2533. p[--cnt] = (uint16_t)i;
  2534. }
  2535. continue;
  2536. }
  2537. /*
  2538. * A bit length is too big to be housed to a direct table,
  2539. * so we use a tree model for its extra bits.
  2540. */
  2541. bitptn[len] = ptn + cnt;
  2542. bit = 1U << (diffbits -1);
  2543. extlen = len - HTBL_BITS;
  2544. p = &(tbl[ptn >> diffbits]);
  2545. if (*p == 0) {
  2546. *p = len_avail + hf->tree_used;
  2547. ht = &(hf->tree[hf->tree_used++]);
  2548. if (hf->tree_used > hf->tree_avail)
  2549. return (0);/* Invalid */
  2550. ht->left = 0;
  2551. ht->right = 0;
  2552. } else {
  2553. if (*p < len_avail ||
  2554. *p >= (len_avail + hf->tree_used))
  2555. return (0);/* Invalid */
  2556. ht = &(hf->tree[*p - len_avail]);
  2557. }
  2558. while (--extlen > 0) {
  2559. if (ptn & bit) {
  2560. if (ht->left < len_avail) {
  2561. ht->left = len_avail + hf->tree_used;
  2562. ht = &(hf->tree[hf->tree_used++]);
  2563. if (hf->tree_used > hf->tree_avail)
  2564. return (0);/* Invalid */
  2565. ht->left = 0;
  2566. ht->right = 0;
  2567. } else {
  2568. ht = &(hf->tree[ht->left - len_avail]);
  2569. }
  2570. } else {
  2571. if (ht->right < len_avail) {
  2572. ht->right = len_avail + hf->tree_used;
  2573. ht = &(hf->tree[hf->tree_used++]);
  2574. if (hf->tree_used > hf->tree_avail)
  2575. return (0);/* Invalid */
  2576. ht->left = 0;
  2577. ht->right = 0;
  2578. } else {
  2579. ht = &(hf->tree[ht->right - len_avail]);
  2580. }
  2581. }
  2582. bit >>= 1;
  2583. }
  2584. if (ptn & bit) {
  2585. if (ht->left != 0)
  2586. return (0);/* Invalid */
  2587. ht->left = (uint16_t)i;
  2588. } else {
  2589. if (ht->right != 0)
  2590. return (0);/* Invalid */
  2591. ht->right = (uint16_t)i;
  2592. }
  2593. }
  2594. return (1);
  2595. }
  2596. static int
  2597. lzh_decode_huffman_tree(struct huffman *hf, unsigned rbits, int c)
  2598. {
  2599. struct htree_t *ht;
  2600. int extlen;
  2601. ht = hf->tree;
  2602. extlen = hf->shift_bits;
  2603. while (c >= hf->len_avail) {
  2604. c -= hf->len_avail;
  2605. if (extlen-- <= 0 || c >= hf->tree_used)
  2606. return (0);
  2607. if (rbits & (1U << extlen))
  2608. c = ht[c].left;
  2609. else
  2610. c = ht[c].right;
  2611. }
  2612. return (c);
  2613. }
  2614. static inline int
  2615. lzh_decode_huffman(struct huffman *hf, unsigned rbits)
  2616. {
  2617. int c;
  2618. /*
  2619. * At first search an index table for a bit pattern.
  2620. * If it fails, search a huffman tree for.
  2621. */
  2622. c = hf->tbl[rbits >> hf->shift_bits];
  2623. if (c < hf->len_avail || hf->len_avail == 0)
  2624. return (c);
  2625. /* This bit pattern needs to be found out at a huffman tree. */
  2626. return (lzh_decode_huffman_tree(hf, rbits, c));
  2627. }