archive_read_support_format_lha.c 84 KB

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