HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2898 lines
83KB

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