archive_read_support_format_lha.c 80 KB

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