2 * Copyright (c) 2008-2011 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
42 #include "archive_entry.h"
43 #include "archive_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_read_private.h"
46 #include "archive_endian.h"
49 #define MAXMATCH 256 /* Maximum match length. */
50 #define MINMATCH 3 /* Minimum match length. */
52 * Literal table format:
54 * +---------------+-------------------------+
55 * | literal code | match length |
56 * | 0 ... 255 | MINMATCH ... MAXMATCH |
57 * +---------------+-------------------------+
58 * <--- LT_BITLEN_SIZE --->
60 /* Literal table size. */
61 #define LT_BITLEN_SIZE (UCHAR_MAX + 1 + MAXMATCH - MINMATCH + 1)
62 /* Position table size.
63 * Note: this used for both position table and pre literal table.*/
64 #define PT_BITLEN_SIZE (3 + 16)
67 /* Decoding status. */
71 * Window to see last 8Ki(lh5),32Ki(lh6),64Ki(lh7) bytes of decoded
76 /* Window buffer, which is a loop buffer. */
77 unsigned char *w_buff;
78 /* The insert position to the window. */
80 /* The position where we can copy decoded code from the window. */
82 /* The length how many bytes we can copy decoded code from
85 /* The remaining bytes that we have not copied decoded data from
86 * the window to an output buffer. */
93 #define CACHE_TYPE uint64_t
94 #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
96 CACHE_TYPE cache_buffer;
97 /* Indicates how many bits avail in cache_buffer. */
109 unsigned char *bitlen;
112 * Use a index table. It's faster than searching a huffman
113 * coding tree, which is a binary tree. But a use of a large
114 * index table causes L1 cache read miss many times.
122 /* Direct access table. */
124 /* Binary tree table for extra bits over the direct access. */
134 int literal_pt_len_size;
135 int literal_pt_len_bits;
136 int reading_position;
142 const unsigned char *next_in;
145 unsigned char *next_out;
152 /* entry_bytes_remaining is the number of bytes we expect. */
153 int64_t entry_offset;
154 int64_t entry_bytes_remaining;
155 int64_t entry_unconsumed;
156 uint16_t entry_crc_calculated;
158 size_t header_size; /* header size */
159 unsigned char level; /* header level */
160 char method[3]; /* compress type */
161 int64_t compsize; /* compressed data size */
162 int64_t origsize; /* original file size */
164 #define BIRTHTIME_IS_SET 1
165 #define ATIME_IS_SET 2
166 #define UNIX_MODE_IS_SET 4
169 long birthtime_tv_nsec;
177 struct archive_string uname;
178 struct archive_string gname;
181 struct archive_string_conv *sconv;
182 struct archive_string_conv *opt_sconv;
184 struct archive_string dirname;
185 struct archive_string filename;
186 struct archive_wstring ws;
188 unsigned char dos_attr;
190 /* Flag to mark progress that an archive was read their first header.*/
191 char found_first_header;
192 /* Flag to mark that indicates an empty directory. */
195 /* Flags to mark progress of decompression. */
196 char decompress_init;
198 char end_of_entry_cleanup;
199 char entry_is_compressed;
201 unsigned char *uncompressed_buffer;
202 size_t uncompressed_buffer_size;
204 char format_name[64];
206 struct lzh_stream strm;
210 * LHA header common member offset.
212 #define H_METHOD_OFFSET 2 /* Compress type. */
213 #define H_ATTR_OFFSET 19 /* DOS attribute. */
214 #define H_LEVEL_OFFSET 20 /* Header Level. */
215 #define H_SIZE 22 /* Minimum header size. */
217 static const uint16_t crc16tbl[256] = {
218 0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,
219 0xC601,0x06C0,0x0780,0xC741,0x0500,0xC5C1,0xC481,0x0440,
220 0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,0xCE81,0x0E40,
221 0x0A00,0xCAC1,0xCB81,0x0B40,0xC901,0x09C0,0x0880,0xC841,
222 0xD801,0x18C0,0x1980,0xD941,0x1B00,0xDBC1,0xDA81,0x1A40,
223 0x1E00,0xDEC1,0xDF81,0x1F40,0xDD01,0x1DC0,0x1C80,0xDC41,
224 0x1400,0xD4C1,0xD581,0x1540,0xD701,0x17C0,0x1680,0xD641,
225 0xD201,0x12C0,0x1380,0xD341,0x1100,0xD1C1,0xD081,0x1040,
226 0xF001,0x30C0,0x3180,0xF141,0x3300,0xF3C1,0xF281,0x3240,
227 0x3600,0xF6C1,0xF781,0x3740,0xF501,0x35C0,0x3480,0xF441,
228 0x3C00,0xFCC1,0xFD81,0x3D40,0xFF01,0x3FC0,0x3E80,0xFE41,
229 0xFA01,0x3AC0,0x3B80,0xFB41,0x3900,0xF9C1,0xF881,0x3840,
230 0x2800,0xE8C1,0xE981,0x2940,0xEB01,0x2BC0,0x2A80,0xEA41,
231 0xEE01,0x2EC0,0x2F80,0xEF41,0x2D00,0xEDC1,0xEC81,0x2C40,
232 0xE401,0x24C0,0x2580,0xE541,0x2700,0xE7C1,0xE681,0x2640,
233 0x2200,0xE2C1,0xE381,0x2340,0xE101,0x21C0,0x2080,0xE041,
234 0xA001,0x60C0,0x6180,0xA141,0x6300,0xA3C1,0xA281,0x6240,
235 0x6600,0xA6C1,0xA781,0x6740,0xA501,0x65C0,0x6480,0xA441,
236 0x6C00,0xACC1,0xAD81,0x6D40,0xAF01,0x6FC0,0x6E80,0xAE41,
237 0xAA01,0x6AC0,0x6B80,0xAB41,0x6900,0xA9C1,0xA881,0x6840,
238 0x7800,0xB8C1,0xB981,0x7940,0xBB01,0x7BC0,0x7A80,0xBA41,
239 0xBE01,0x7EC0,0x7F80,0xBF41,0x7D00,0xBDC1,0xBC81,0x7C40,
240 0xB401,0x74C0,0x7580,0xB541,0x7700,0xB7C1,0xB681,0x7640,
241 0x7200,0xB2C1,0xB381,0x7340,0xB101,0x71C0,0x7080,0xB041,
242 0x5000,0x90C1,0x9181,0x5140,0x9301,0x53C0,0x5280,0x9241,
243 0x9601,0x56C0,0x5780,0x9741,0x5500,0x95C1,0x9481,0x5440,
244 0x9C01,0x5CC0,0x5D80,0x9D41,0x5F00,0x9FC1,0x9E81,0x5E40,
245 0x5A00,0x9AC1,0x9B81,0x5B40,0x9901,0x59C0,0x5880,0x9841,
246 0x8801,0x48C0,0x4980,0x8941,0x4B00,0x8BC1,0x8A81,0x4A40,
247 0x4E00,0x8EC1,0x8F81,0x4F40,0x8D01,0x4DC0,0x4C80,0x8C41,
248 0x4400,0x84C1,0x8581,0x4540,0x8701,0x47C0,0x4680,0x8641,
249 0x8201,0x42C0,0x4380,0x8341,0x4100,0x81C1,0x8081,0x4040
252 static int archive_read_format_lha_bid(struct archive_read *, int);
253 static int archive_read_format_lha_options(struct archive_read *,
254 const char *, const char *);
255 static int archive_read_format_lha_read_header(struct archive_read *,
256 struct archive_entry *);
257 static int archive_read_format_lha_read_data(struct archive_read *,
258 const void **, size_t *, int64_t *);
259 static int archive_read_format_lha_read_data_skip(struct archive_read *);
260 static int archive_read_format_lha_cleanup(struct archive_read *);
262 static void lha_replace_path_separator(struct lha *,
263 struct archive_entry *);
264 static int lha_read_file_header_0(struct archive_read *, struct lha *);
265 static int lha_read_file_header_1(struct archive_read *, struct lha *);
266 static int lha_read_file_header_2(struct archive_read *, struct lha *);
267 static int lha_read_file_header_3(struct archive_read *, struct lha *);
268 static int lha_read_file_extended_header(struct archive_read *,
269 struct lha *, uint16_t *, int, size_t, size_t *);
270 static size_t lha_check_header_format(const void *);
271 static int lha_skip_sfx(struct archive_read *);
272 static time_t lha_dos_time(const unsigned char *);
273 static time_t lha_win_time(uint64_t, long *);
274 static unsigned char lha_calcsum(unsigned char, const void *,
276 static int lha_parse_linkname(struct archive_string *,
277 struct archive_string *);
278 static int lha_read_data_none(struct archive_read *, const void **,
279 size_t *, int64_t *);
280 static int lha_read_data_lzh(struct archive_read *, const void **,
281 size_t *, int64_t *);
282 static uint16_t lha_crc16(uint16_t, const void *, size_t);
283 static int lzh_decode_init(struct lzh_stream *, const char *);
284 static void lzh_decode_free(struct lzh_stream *);
285 static int lzh_decode(struct lzh_stream *, int);
286 static int lzh_br_fillup(struct lzh_stream *, struct lzh_br *);
287 static int lzh_huffman_init(struct huffman *, size_t, int);
288 static void lzh_huffman_free(struct huffman *);
289 static int lzh_read_pt_bitlen(struct lzh_stream *, int start, int end);
290 static int lzh_make_fake_table(struct huffman *, uint16_t);
291 static int lzh_make_huffman_table(struct huffman *);
292 static int inline lzh_decode_huffman(struct huffman *, unsigned);
293 static int lzh_decode_huffman_tree(struct huffman *, unsigned, int);
297 archive_read_support_format_lha(struct archive *_a)
299 struct archive_read *a = (struct archive_read *)_a;
303 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
304 ARCHIVE_STATE_NEW, "archive_read_support_format_lha");
306 lha = (struct lha *)calloc(1, sizeof(*lha));
308 archive_set_error(&a->archive, ENOMEM,
309 "Can't allocate lha data");
310 return (ARCHIVE_FATAL);
312 archive_string_init(&lha->ws);
314 r = __archive_read_register_format(a,
317 archive_read_format_lha_bid,
318 archive_read_format_lha_options,
319 archive_read_format_lha_read_header,
320 archive_read_format_lha_read_data,
321 archive_read_format_lha_read_data_skip,
322 archive_read_format_lha_cleanup);
330 lha_check_header_format(const void *h)
332 const unsigned char *p = h;
333 size_t next_skip_bytes;
335 switch (p[H_METHOD_OFFSET+3]) {
337 * "-lh0-" ... "-lh7-" "-lhd-"
340 case '0': case '1': case '2': case '3':
341 case '4': case '5': case '6': case '7':
346 /* b0 == 0 means the end of an LHa archive file. */
349 if (p[H_METHOD_OFFSET] != '-' || p[H_METHOD_OFFSET+1] != 'l'
350 || p[H_METHOD_OFFSET+4] != '-')
353 if (p[H_METHOD_OFFSET+2] == 'h') {
355 if (p[H_METHOD_OFFSET+3] == 's')
357 if (p[H_LEVEL_OFFSET] == 0)
359 if (p[H_LEVEL_OFFSET] <= 3 && p[H_ATTR_OFFSET] == 0x20)
362 if (p[H_METHOD_OFFSET+2] == 'z') {
363 /* LArc extensions: -lzs-,-lz4- and -lz5- */
364 if (p[H_LEVEL_OFFSET] != 0)
366 if (p[H_METHOD_OFFSET+3] == 's'
367 || p[H_METHOD_OFFSET+3] == '4'
368 || p[H_METHOD_OFFSET+3] == '5')
372 case 'h': next_skip_bytes = 1; break;
373 case 'z': next_skip_bytes = 1; break;
374 case 'l': next_skip_bytes = 2; break;
375 case '-': next_skip_bytes = 3; break;
376 default : next_skip_bytes = 4; break;
379 return (next_skip_bytes);
383 archive_read_format_lha_bid(struct archive_read *a, int best_bid)
387 ssize_t bytes_avail, offset, window;
390 /* If there's already a better bid than we can ever
391 make, don't bother testing. */
395 if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL)
398 if (lha_check_header_format(p) == 0)
401 if (p[0] == 'M' && p[1] == 'Z') {
405 while (offset < (1024 * 20)) {
406 buff = __archive_read_ahead(a, offset + window,
409 /* Remaining bytes are less than window. */
411 if (window < (H_SIZE + 3))
415 p = (const char *)buff + offset;
416 while (p + H_SIZE < (const char *)buff + bytes_avail) {
417 if ((next = lha_check_header_format(p)) == 0)
421 offset = p - (const char *)buff;
428 archive_read_format_lha_options(struct archive_read *a,
429 const char *key, const char *val)
432 int ret = ARCHIVE_FAILED;
434 lha = (struct lha *)(a->format->data);
435 if (strcmp(key, "hdrcharset") == 0) {
436 if (val == NULL || val[0] == 0)
437 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
438 "lha: hdrcharset option needs a character-set name");
441 archive_string_conversion_from_charset(
442 &a->archive, val, 0);
443 if (lha->opt_sconv != NULL)
449 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
450 "lha: unknown keyword ``%s''", key);
456 lha_skip_sfx(struct archive_read *a)
461 ssize_t bytes, window;
465 h = __archive_read_ahead(a, window, &bytes);
467 /* Remaining bytes are less than window. */
469 if (window < (H_SIZE + 3))
479 * Scan ahead until we find something that looks
480 * like the lha header.
482 while (p + H_SIZE < q) {
483 if ((next = lha_check_header_format(p)) == 0) {
484 skip = p - (const char *)h;
485 __archive_read_consume(a, skip);
490 skip = p - (const char *)h;
491 __archive_read_consume(a, skip);
494 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
495 "Couldn't find out LHa header");
496 return (ARCHIVE_FATAL);
500 truncated_error(struct archive_read *a)
502 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
503 "Truncated LHa header");
504 return (ARCHIVE_FATAL);
508 archive_read_format_lha_read_header(struct archive_read *a,
509 struct archive_entry *entry)
511 struct archive_string linkname;
512 struct archive_string pathname;
514 const unsigned char *p;
515 const char *signature;
518 a->archive.archive_format = ARCHIVE_FORMAT_LHA;
519 if (a->archive.archive_format_name == NULL)
520 a->archive.archive_format_name = "lha";
522 lha = (struct lha *)(a->format->data);
523 lha->decompress_init = 0;
524 lha->end_of_entry = 0;
525 lha->end_of_entry_cleanup = 0;
526 lha->entry_unconsumed = 0;
528 if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL) {
530 * LHa archiver added 0 to the tail of its archive file as
531 * the mark of the end of the archive.
533 signature = __archive_read_ahead(a, sizeof(signature[0]), NULL);
534 if (signature == NULL || signature[0] == 0)
535 return (ARCHIVE_EOF);
536 return (truncated_error(a));
539 signature = (const char *)p;
540 if (lha->found_first_header == 0 &&
541 signature[0] == 'M' && signature[1] == 'Z') {
542 /* This is an executable? Must be self-extracting... */
543 err = lha_skip_sfx(a);
544 if (err < ARCHIVE_WARN)
547 if ((p = __archive_read_ahead(a, sizeof(*p), NULL)) == NULL)
548 return (truncated_error(a));
549 signature = (const char *)p;
551 /* signature[0] == 0 means the end of an LHa archive file. */
552 if (signature[0] == 0)
553 return (ARCHIVE_EOF);
556 * Check the header format and method type.
558 if (lha_check_header_format(p) != 0) {
559 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
561 return (ARCHIVE_FATAL);
564 /* We've found the first header. */
565 lha->found_first_header = 1;
566 /* Set a default value and common data */
567 lha->header_size = 0;
568 lha->level = p[H_LEVEL_OFFSET];
569 lha->method[0] = p[H_METHOD_OFFSET+1];
570 lha->method[1] = p[H_METHOD_OFFSET+2];
571 lha->method[2] = p[H_METHOD_OFFSET+3];
572 if (memcmp(lha->method, "lhd", 3) == 0)
576 if (memcmp(lha->method, "lh0", 3) == 0 ||
577 memcmp(lha->method, "lz4", 3) == 0)
578 lha->entry_is_compressed = 0;
580 lha->entry_is_compressed = 1;
586 lha->birthtime_tv_nsec = 0;
588 lha->mtime_tv_nsec = 0;
590 lha->atime_tv_nsec = 0;
591 lha->mode = (lha->directory)? 0777 : 0666;
594 archive_string_empty(&lha->dirname);
595 archive_string_empty(&lha->filename);
597 if (lha->opt_sconv != NULL)
598 lha->sconv = lha->opt_sconv;
602 switch (p[H_LEVEL_OFFSET]) {
604 err = lha_read_file_header_0(a, lha);
607 err = lha_read_file_header_1(a, lha);
610 err = lha_read_file_header_2(a, lha);
613 err = lha_read_file_header_3(a, lha);
616 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
617 "Unsupported LHa header level %d", p[H_LEVEL_OFFSET]);
621 if (err < ARCHIVE_WARN)
625 if (!lha->directory && archive_strlen(&lha->filename) == 0)
626 /* The filename has not been set */
627 return (truncated_error(a));
630 * Make a pathname from a dirname and a filename.
632 archive_string_concat(&lha->dirname, &lha->filename);
633 archive_string_init(&pathname);
634 archive_string_init(&linkname);
635 archive_string_copy(&pathname, &lha->dirname);
637 if ((lha->mode & AE_IFMT) == AE_IFLNK) {
639 * Extract the symlink-name if it's included in the pathname.
641 if (!lha_parse_linkname(&linkname, &pathname)) {
642 /* We couldn't get the symlink-name. */
643 archive_set_error(&a->archive,
644 ARCHIVE_ERRNO_FILE_FORMAT,
645 "Unknown symlink-name");
646 archive_string_free(&pathname);
647 archive_string_free(&linkname);
648 return (ARCHIVE_FAILED);
652 * Make sure a file-type is set.
653 * The mode has been overridden if it is in the extended data.
655 lha->mode = (lha->mode & ~AE_IFMT) |
656 ((lha->directory)? AE_IFDIR: AE_IFREG);
658 if ((lha->setflag & UNIX_MODE_IS_SET) == 0 &&
659 (lha->dos_attr & 1) != 0)
660 lha->mode &= ~(0222);/* read only. */
663 * Set basic file parameters.
665 if (archive_entry_copy_pathname_l(entry, pathname.s,
666 pathname.length, lha->sconv) != 0) {
667 if (errno == ENOMEM) {
668 archive_set_error(&a->archive, ENOMEM,
669 "Can't allocate memory for Pathname");
670 return (ARCHIVE_FATAL);
672 archive_set_error(&a->archive,
673 ARCHIVE_ERRNO_FILE_FORMAT,
674 "Pathname cannot be converted "
675 "from %s to current locale.",
676 archive_string_conversion_charset_name(lha->sconv));
679 archive_string_free(&pathname);
680 if (archive_strlen(&linkname) > 0) {
681 if (archive_entry_copy_symlink_l(entry, linkname.s,
682 linkname.length, lha->sconv) != 0) {
683 if (errno == ENOMEM) {
684 archive_set_error(&a->archive, ENOMEM,
685 "Can't allocate memory for Linkname");
686 return (ARCHIVE_FATAL);
688 archive_set_error(&a->archive,
689 ARCHIVE_ERRNO_FILE_FORMAT,
690 "Linkname cannot be converted "
691 "from %s to current locale.",
692 archive_string_conversion_charset_name(lha->sconv));
696 archive_entry_set_symlink(entry, NULL);
697 archive_string_free(&linkname);
699 * When a header level is 0, there is a possibilty that
700 * a pathname and a symlink has '\' character, a directory
701 * separator in DOS/Windows. So we should convert it to '/'.
703 if (p[H_LEVEL_OFFSET] == 0)
704 lha_replace_path_separator(lha, entry);
706 archive_entry_set_mode(entry, lha->mode);
707 archive_entry_set_uid(entry, lha->uid);
708 archive_entry_set_gid(entry, lha->gid);
709 if (archive_strlen(&lha->uname) > 0)
710 archive_entry_set_uname(entry, lha->uname.s);
711 if (archive_strlen(&lha->gname) > 0)
712 archive_entry_set_gname(entry, lha->gname.s);
713 if (lha->setflag & BIRTHTIME_IS_SET) {
714 archive_entry_set_birthtime(entry, lha->birthtime,
715 lha->birthtime_tv_nsec);
716 archive_entry_set_ctime(entry, lha->birthtime,
717 lha->birthtime_tv_nsec);
719 archive_entry_unset_birthtime(entry);
720 archive_entry_unset_ctime(entry);
722 archive_entry_set_mtime(entry, lha->mtime, lha->mtime_tv_nsec);
723 if (lha->setflag & ATIME_IS_SET)
724 archive_entry_set_atime(entry, lha->atime,
727 archive_entry_unset_atime(entry);
728 if (lha->directory || archive_entry_symlink(entry) != NULL)
729 archive_entry_unset_size(entry);
731 archive_entry_set_size(entry, lha->origsize);
734 * Prepare variables used to read a file content.
736 lha->entry_bytes_remaining = lha->compsize;
737 lha->entry_offset = 0;
738 lha->entry_crc_calculated = 0;
741 * This file does not have a content.
743 if (lha->directory || lha->compsize == 0)
744 lha->end_of_entry = 1;
746 sprintf(lha->format_name, "lha -%c%c%c-",
747 lha->method[0], lha->method[1], lha->method[2]);
748 a->archive.archive_format_name = lha->format_name;
754 * Replace a DOS path separator '\' by a character '/'.
755 * Some multi-byte character set have a character '\' in its second byte.
758 lha_replace_path_separator(struct lha *lha, struct archive_entry *entry)
763 if ((wp = archive_entry_pathname_w(entry)) != NULL) {
764 archive_wstrcpy(&(lha->ws), wp);
765 for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
766 if (lha->ws.s[i] == L'\\')
769 archive_entry_copy_pathname_w(entry, lha->ws.s);
772 if ((wp = archive_entry_symlink_w(entry)) != NULL) {
773 archive_wstrcpy(&(lha->ws), wp);
774 for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
775 if (lha->ws.s[i] == L'\\')
778 archive_entry_copy_symlink_w(entry, lha->ws.s);
786 * +---------------+----------+----------------+-------------------+
787 * |header size(*1)|header sum|compression type|compressed size(*2)|
788 * +---------------+----------+----------------+-------------------+
789 * <---------------------(*1)----------*
791 * +11 +15 +17 +19 +20 +21
792 * +-----------------+---------+---------+--------------+----------------+
793 * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=0)|
794 * +-----------------+---------+---------+--------------+----------------+
795 * *--------------------------------(*1)---------------------------------*
797 * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+2+(*4)
798 * +---------------+---------+----------+----------------+------------------+
799 * |name length(*3)|file name|file CRC16|extra header(*4)| compressed data |
800 * +---------------+---------+----------+----------------+------------------+
801 * <--(*3)-> <------(*2)------>
802 * *----------------------(*1)-------------------------->
805 #define H0_HEADER_SIZE_OFFSET 0
806 #define H0_HEADER_SUM_OFFSET 1
807 #define H0_COMP_SIZE_OFFSET 7
808 #define H0_ORIG_SIZE_OFFSET 11
809 #define H0_DOS_TIME_OFFSET 15
810 #define H0_NAME_LEN_OFFSET 21
811 #define H0_FILE_NAME_OFFSET 22
812 #define H0_FIXED_SIZE 24
814 lha_read_file_header_0(struct archive_read *a, struct lha *lha)
816 const unsigned char *p;
817 int extdsize, namelen;
818 unsigned char headersum, sum_calculated;
820 if ((p = __archive_read_ahead(a, H0_FIXED_SIZE, NULL)) == NULL)
821 return (truncated_error(a));
822 lha->header_size = p[H0_HEADER_SIZE_OFFSET] + 2;
823 headersum = p[H0_HEADER_SUM_OFFSET];
824 lha->compsize = archive_le32dec(p + H0_COMP_SIZE_OFFSET);
825 lha->origsize = archive_le32dec(p + H0_ORIG_SIZE_OFFSET);
826 lha->mtime = lha_dos_time(p + H0_DOS_TIME_OFFSET);
827 namelen = p[H0_NAME_LEN_OFFSET];
828 extdsize = (int)lha->header_size - H0_FIXED_SIZE - namelen;
829 if ((namelen > 221 || extdsize < 0) && extdsize != -2) {
830 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
831 "Invalid LHa header");
832 return (ARCHIVE_FATAL);
834 if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
835 return (truncated_error(a));
837 archive_strncpy(&lha->filename, p + H0_FILE_NAME_OFFSET, namelen);
838 /* When extdsize == -2, A CRC16 value is not present in the header. */
840 lha->crc = archive_le16dec(p + H0_FILE_NAME_OFFSET + namelen);
841 lha->setflag |= CRC_IS_SET;
843 sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
845 /* Read an extended header */
847 /* This extended data is set by 'LHa for UNIX' only.
850 p += H0_FILE_NAME_OFFSET + namelen + 2;
851 if (p[0] == 'U' && extdsize == 12) {
852 /* p[1] is a minor version. */
853 lha->mtime = archive_le32dec(&p[2]);
854 lha->mode = archive_le16dec(&p[6]);
855 lha->uid = archive_le16dec(&p[8]);
856 lha->gid = archive_le16dec(&p[10]);
857 lha->setflag |= UNIX_MODE_IS_SET;
860 __archive_read_consume(a, lha->header_size);
862 if (sum_calculated != headersum) {
863 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
864 "LHa header sum error");
865 return (ARCHIVE_FATAL);
875 * +---------------+----------+----------------+-------------+
876 * |header size(*1)|header sum|compression type|skip size(*2)|
877 * +---------------+----------+----------------+-------------+
878 * <---------------(*1)----------*
880 * +11 +15 +17 +19 +20 +21
881 * +-----------------+---------+---------+--------------+----------------+
882 * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=1)|
883 * +-----------------+---------+---------+--------------+----------------+
884 * *-------------------------------(*1)----------------------------------*
886 * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+3 +22+(*3)+3+(*4)
887 * +---------------+---------+----------+-----------+-----------+
888 * |name length(*3)|file name|file CRC16| creator |padding(*4)|
889 * +---------------+---------+----------+-----------+-----------+
891 * *----------------------------(*1)----------------------------*
893 * +22+(*3)+3+(*4) +22+(*3)+3+(*4)+2 +22+(*3)+3+(*4)+2+(*5)
894 * +----------------+---------------------+------------------------+
895 * |next header size| extended header(*5) | compressed data |
896 * +----------------+---------------------+------------------------+
897 * *------(*1)-----> <--------------------(*2)-------------------->
899 #define H1_HEADER_SIZE_OFFSET 0
900 #define H1_HEADER_SUM_OFFSET 1
901 #define H1_COMP_SIZE_OFFSET 7
902 #define H1_ORIG_SIZE_OFFSET 11
903 #define H1_DOS_TIME_OFFSET 15
904 #define H1_NAME_LEN_OFFSET 21
905 #define H1_FILE_NAME_OFFSET 22
906 #define H1_FIXED_SIZE 27
908 lha_read_file_header_1(struct archive_read *a, struct lha *lha)
910 const unsigned char *p;
913 int namelen, padding;
914 unsigned char headersum, sum_calculated;
918 if ((p = __archive_read_ahead(a, H1_FIXED_SIZE, NULL)) == NULL)
919 return (truncated_error(a));
921 lha->header_size = p[H1_HEADER_SIZE_OFFSET] + 2;
922 headersum = p[H1_HEADER_SUM_OFFSET];
923 /* Note: An extended header size is included in a compsize. */
924 lha->compsize = archive_le32dec(p + H1_COMP_SIZE_OFFSET);
925 lha->origsize = archive_le32dec(p + H1_ORIG_SIZE_OFFSET);
926 lha->mtime = lha_dos_time(p + H1_DOS_TIME_OFFSET);
927 namelen = p[H1_NAME_LEN_OFFSET];
928 /* Calculate a padding size. The result will be normally 0 only(?) */
929 padding = ((int)lha->header_size) - H1_FIXED_SIZE - namelen;
931 if (namelen > 230 || padding < 0)
934 if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
935 return (truncated_error(a));
937 for (i = 0; i < namelen; i++) {
938 if (p[i + H1_FILE_NAME_OFFSET] == 0xff)
939 goto invalid;/* Invalid filename. */
941 archive_strncpy(&lha->filename, p + H1_FILE_NAME_OFFSET, namelen);
942 lha->crc = archive_le16dec(p + H1_FILE_NAME_OFFSET + namelen);
943 lha->setflag |= CRC_IS_SET;
945 sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
946 /* Consume used bytes but not include `next header size' data
947 * since it will be consumed in lha_read_file_extended_header(). */
948 __archive_read_consume(a, lha->header_size - 2);
950 /* Read extended headers */
951 err2 = lha_read_file_extended_header(a, lha, NULL, 2,
952 lha->compsize + 2, &extdsize);
953 if (err2 < ARCHIVE_WARN)
957 /* Get a real compressed file size. */
958 lha->compsize -= extdsize - 2;
960 if (sum_calculated != headersum) {
961 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
962 "LHa header sum error");
963 return (ARCHIVE_FATAL);
967 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
968 "Invalid LHa header");
969 return (ARCHIVE_FATAL);
976 * +---------------+----------------+-------------------+-----------------+
977 * |header size(*1)|compression type|compressed size(*2)|uncompressed size|
978 * +---------------+----------------+-------------------+-----------------+
979 * <--------------------------------(*1)---------------------------------*
981 * +15 +19 +20 +21 +23 +24
982 * +-----------------+------------+----------------+----------+-----------+
983 * |data/time(time_t)| 0x20 fixed |header level(=2)|file CRC16| creator |
984 * +-----------------+------------+----------------+----------+-----------+
985 * *---------------------------------(*1)---------------------------------*
987 * +24 +26 +26+(*3) +26+(*3)+(*4)
988 * +----------------+-------------------+-------------+-------------------+
989 * |next header size|extended header(*3)| padding(*4) | compressed data |
990 * +----------------+-------------------+-------------+-------------------+
991 * *--------------------------(*1)-------------------> <------(*2)------->
994 #define H2_HEADER_SIZE_OFFSET 0
995 #define H2_COMP_SIZE_OFFSET 7
996 #define H2_ORIG_SIZE_OFFSET 11
997 #define H2_TIME_OFFSET 15
998 #define H2_CRC_OFFSET 21
999 #define H2_FIXED_SIZE 24
1001 lha_read_file_header_2(struct archive_read *a, struct lha *lha)
1003 const unsigned char *p;
1006 uint16_t header_crc;
1008 if ((p = __archive_read_ahead(a, H2_FIXED_SIZE, NULL)) == NULL)
1009 return (truncated_error(a));
1011 lha->header_size =archive_le16dec(p + H2_HEADER_SIZE_OFFSET);
1012 lha->compsize = archive_le32dec(p + H2_COMP_SIZE_OFFSET);
1013 lha->origsize = archive_le32dec(p + H2_ORIG_SIZE_OFFSET);
1014 lha->mtime = archive_le32dec(p + H2_TIME_OFFSET);
1015 lha->crc = archive_le16dec(p + H2_CRC_OFFSET);
1016 lha->setflag |= CRC_IS_SET;
1018 if (lha->header_size < H2_FIXED_SIZE) {
1019 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1020 "Invalid LHa header size");
1021 return (ARCHIVE_FATAL);
1024 header_crc = lha_crc16(0, p, H2_FIXED_SIZE);
1025 __archive_read_consume(a, H2_FIXED_SIZE);
1027 /* Read extended headers */
1028 err = lha_read_file_extended_header(a, lha, &header_crc, 2,
1029 lha->header_size - H2_FIXED_SIZE, &extdsize);
1030 if (err < ARCHIVE_WARN)
1033 /* Calculate a padding size. The result will be normally 0 or 1. */
1034 padding = (int)lha->header_size - (int)(H2_FIXED_SIZE + extdsize);
1036 if ((p = __archive_read_ahead(a, padding, NULL)) == NULL)
1037 return (truncated_error(a));
1038 header_crc = lha_crc16(header_crc, p, padding);
1039 __archive_read_consume(a, padding);
1042 if (header_crc != lha->header_crc) {
1043 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1044 "LHa header CRC error");
1045 return (ARCHIVE_FATAL);
1054 * +------------+----------------+-------------------+-----------------+
1055 * | 0x04 fixed |compression type|compressed size(*2)|uncompressed size|
1056 * +------------+----------------+-------------------+-----------------+
1057 * <-------------------------------(*1)-------------------------------*
1059 * +15 +19 +20 +21 +23 +24
1060 * +-----------------+------------+----------------+----------+-----------+
1061 * |date/time(time_t)| 0x20 fixed |header level(=3)|file CRC16| creator |
1062 * +-----------------+------------+----------------+----------+-----------+
1063 * *--------------------------------(*1)----------------------------------*
1065 * +24 +28 +32 +32+(*3)
1066 * +---------------+----------------+-------------------+-----------------+
1067 * |header size(*1)|next header size|extended header(*3)| compressed data |
1068 * +---------------+----------------+-------------------+-----------------+
1069 * *------------------------(*1)-----------------------> <------(*2)----->
1072 #define H3_FIELD_LEN_OFFSET 0
1073 #define H3_COMP_SIZE_OFFSET 7
1074 #define H3_ORIG_SIZE_OFFSET 11
1075 #define H3_TIME_OFFSET 15
1076 #define H3_CRC_OFFSET 21
1077 #define H3_HEADER_SIZE_OFFSET 24
1078 #define H3_FIXED_SIZE 28
1080 lha_read_file_header_3(struct archive_read *a, struct lha *lha)
1082 const unsigned char *p;
1085 uint16_t header_crc;
1087 if ((p = __archive_read_ahead(a, H3_FIXED_SIZE, NULL)) == NULL)
1088 return (truncated_error(a));
1090 if (archive_le16dec(p + H3_FIELD_LEN_OFFSET) != 4)
1092 lha->header_size =archive_le32dec(p + H3_HEADER_SIZE_OFFSET);
1093 lha->compsize = archive_le32dec(p + H3_COMP_SIZE_OFFSET);
1094 lha->origsize = archive_le32dec(p + H3_ORIG_SIZE_OFFSET);
1095 lha->mtime = archive_le32dec(p + H3_TIME_OFFSET);
1096 lha->crc = archive_le16dec(p + H3_CRC_OFFSET);
1097 lha->setflag |= CRC_IS_SET;
1099 if (lha->header_size < H3_FIXED_SIZE + 4)
1101 header_crc = lha_crc16(0, p, H3_FIXED_SIZE);
1102 __archive_read_consume(a, H3_FIXED_SIZE);
1104 /* Read extended headers */
1105 err = lha_read_file_extended_header(a, lha, &header_crc, 4,
1106 lha->header_size - H3_FIXED_SIZE, &extdsize);
1107 if (err < ARCHIVE_WARN)
1110 if (header_crc != lha->header_crc) {
1111 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1112 "LHa header CRC error");
1113 return (ARCHIVE_FATAL);
1117 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1118 "Invalid LHa header");
1119 return (ARCHIVE_FATAL);
1123 * Extended header format
1125 * +0 +2 +3 -- used in header 1 and 2
1126 * +0 +4 +5 -- used in header 3
1127 * +--------------+---------+-------------------+--------------+--
1128 * |ex-header size|header id| data |ex-header size| .......
1129 * +--------------+---------+-------------------+--------------+--
1130 * <-------------( ex-header size)------------> <-- next extended header --*
1132 * If the ex-header size is zero, it is the make of the end of extended
1137 lha_read_file_extended_header(struct archive_read *a, struct lha *lha,
1138 uint16_t *crc, int sizefield_length, size_t limitsize, size_t *total_size)
1141 const unsigned char *extdheader;
1145 unsigned char extdtype;
1147 #define EXT_HEADER_CRC 0x00 /* Header CRC and information*/
1148 #define EXT_FILENAME 0x01 /* Filename */
1149 #define EXT_DIRECTORY 0x02 /* Directory name */
1150 #define EXT_DOS_ATTR 0x40 /* MS-DOS attribute */
1151 #define EXT_TIMESTAMP 0x41 /* Windows time stamp */
1152 #define EXT_FILESIZE 0x42 /* Large file size */
1153 #define EXT_TIMEZONE 0x43 /* Time zone */
1154 #define EXT_UTF16_FILENAME 0x44 /* UTF-16 filename */
1155 #define EXT_UTF16_DIRECTORY 0x45 /* UTF-16 directory name */
1156 #define EXT_CODEPAGE 0x46 /* Codepage */
1157 #define EXT_UNIX_MODE 0x50 /* File permission */
1158 #define EXT_UNIX_GID_UID 0x51 /* gid,uid */
1159 #define EXT_UNIX_GNAME 0x52 /* Group name */
1160 #define EXT_UNIX_UNAME 0x53 /* User name */
1161 #define EXT_UNIX_MTIME 0x54 /* Modified time */
1162 #define EXT_OS2_NEW_ATTR 0x7f /* new attribute(OS/2 only) */
1163 #define EXT_NEW_ATTR 0xff /* new attribute */
1165 *total_size = sizefield_length;
1168 /* Read an extended header size. */
1170 __archive_read_ahead(a, sizefield_length, NULL)) == NULL)
1171 return (truncated_error(a));
1172 /* Check if the size is the zero indicates the end of the
1173 * extended header. */
1174 if (sizefield_length == sizeof(uint16_t))
1175 extdsize = archive_le16dec(h);
1177 extdsize = archive_le32dec(h);
1178 if (extdsize == 0) {
1179 /* End of extended header */
1181 *crc = lha_crc16(*crc, h, sizefield_length);
1182 __archive_read_consume(a, sizefield_length);
1183 return (ARCHIVE_OK);
1186 /* Sanity check to the extended header size. */
1187 if (((uint64_t)*total_size + extdsize) >
1188 (uint64_t)limitsize ||
1189 extdsize <= (size_t)sizefield_length)
1192 /* Read the extended header. */
1193 if ((h = __archive_read_ahead(a, extdsize, NULL)) == NULL)
1194 return (truncated_error(a));
1195 *total_size += extdsize;
1197 extdheader = (const unsigned char *)h;
1198 /* Get the extended header type. */
1199 extdtype = extdheader[sizefield_length];
1200 /* Calculate an extended data size. */
1201 datasize = extdsize - (1 + sizefield_length);
1202 /* Skip an extended header size field and type field. */
1203 extdheader += sizefield_length + 1;
1205 if (crc != NULL && extdtype != EXT_HEADER_CRC)
1206 *crc = lha_crc16(*crc, h, extdsize);
1208 case EXT_HEADER_CRC:
1209 /* We only use a header CRC. Following data will not
1211 if (datasize >= 2) {
1212 lha->header_crc = archive_le16dec(extdheader);
1214 static const char zeros[2] = {0, 0};
1215 *crc = lha_crc16(*crc, h,
1216 extdsize - datasize);
1217 /* CRC value itself as zero */
1218 *crc = lha_crc16(*crc, zeros, 2);
1219 *crc = lha_crc16(*crc,
1220 extdheader+2, datasize - 2);
1225 if (datasize == 0) {
1226 /* maybe directory header */
1227 archive_string_empty(&lha->filename);
1230 archive_strncpy(&lha->filename,
1231 (const char *)extdheader, datasize);
1235 /* no directory name data. exit this case. */
1238 archive_strncpy(&lha->dirname,
1239 (const char *)extdheader, datasize);
1241 * Convert directory delimiter from 0xFF
1242 * to '/' for local system.
1244 for (i = 0; i < lha->dirname.length; i++) {
1245 if ((unsigned char)lha->dirname.s[i] == 0xFF)
1246 lha->dirname.s[i] = '/';
1248 /* Is last character directory separator? */
1249 if (lha->dirname.s[lha->dirname.length-1] != '/')
1250 /* invalid directory data */
1255 lha->dos_attr = (unsigned char)
1256 (archive_le16dec(extdheader) & 0xff);
1259 if (datasize == (sizeof(uint64_t) * 3)) {
1260 lha->birthtime = lha_win_time(
1261 archive_le64dec(extdheader),
1262 &lha->birthtime_tv_nsec);
1263 extdheader += sizeof(uint64_t);
1264 lha->mtime = lha_win_time(
1265 archive_le64dec(extdheader),
1266 &lha->mtime_tv_nsec);
1267 extdheader += sizeof(uint64_t);
1268 lha->atime = lha_win_time(
1269 archive_le64dec(extdheader),
1270 &lha->atime_tv_nsec);
1271 lha->setflag |= BIRTHTIME_IS_SET |
1276 if (datasize == sizeof(uint64_t) * 2) {
1277 lha->compsize = archive_le64dec(extdheader);
1278 extdheader += sizeof(uint64_t);
1279 lha->origsize = archive_le64dec(extdheader);
1283 /* Get an archived filename charset from codepage.
1284 * This overwrites the charset specified by
1285 * hdrcharset option. */
1286 if (datasize == sizeof(uint32_t)) {
1287 struct archive_string cp;
1288 const char *charset;
1290 archive_string_init(&cp);
1291 switch (archive_le32dec(extdheader)) {
1292 case 65001: /* UTF-8 */
1296 archive_string_sprintf(&cp, "CP%d",
1297 (int)archive_le32dec(extdheader));
1302 archive_string_conversion_from_charset(
1303 &(a->archive), charset, 1);
1304 archive_string_free(&cp);
1305 if (lha->sconv == NULL)
1306 return (ARCHIVE_FATAL);
1310 if (datasize == sizeof(uint16_t)) {
1311 lha->mode = archive_le16dec(extdheader);
1312 lha->setflag |= UNIX_MODE_IS_SET;
1315 case EXT_UNIX_GID_UID:
1316 if (datasize == (sizeof(uint16_t) * 2)) {
1317 lha->gid = archive_le16dec(extdheader);
1318 lha->uid = archive_le16dec(extdheader+2);
1321 case EXT_UNIX_GNAME:
1323 archive_strncpy(&lha->gname,
1324 (const char *)extdheader, datasize);
1326 case EXT_UNIX_UNAME:
1328 archive_strncpy(&lha->uname,
1329 (const char *)extdheader, datasize);
1331 case EXT_UNIX_MTIME:
1332 if (datasize == sizeof(uint32_t))
1333 lha->mtime = archive_le32dec(extdheader);
1335 case EXT_OS2_NEW_ATTR:
1336 /* This extended header is OS/2 depend. */
1337 if (datasize == 16) {
1338 lha->dos_attr = (unsigned char)
1339 (archive_le16dec(extdheader) & 0xff);
1340 lha->mode = archive_le16dec(extdheader+2);
1341 lha->gid = archive_le16dec(extdheader+4);
1342 lha->uid = archive_le16dec(extdheader+6);
1343 lha->birthtime = archive_le32dec(extdheader+8);
1344 lha->atime = archive_le32dec(extdheader+12);
1345 lha->setflag |= UNIX_MODE_IS_SET
1346 | BIRTHTIME_IS_SET | ATIME_IS_SET;
1350 if (datasize == 20) {
1351 lha->mode = (mode_t)archive_le32dec(extdheader);
1352 lha->gid = archive_le32dec(extdheader+4);
1353 lha->uid = archive_le32dec(extdheader+8);
1354 lha->birthtime = archive_le32dec(extdheader+12);
1355 lha->atime = archive_le32dec(extdheader+16);
1356 lha->setflag |= UNIX_MODE_IS_SET
1357 | BIRTHTIME_IS_SET | ATIME_IS_SET;
1360 case EXT_TIMEZONE: /* Not supported */
1361 case EXT_UTF16_FILENAME: /* Not supported */
1362 case EXT_UTF16_DIRECTORY: /* Not supported */
1367 __archive_read_consume(a, extdsize);
1370 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1371 "Invalid extended LHa header");
1372 return (ARCHIVE_FATAL);
1376 archive_read_format_lha_read_data(struct archive_read *a,
1377 const void **buff, size_t *size, int64_t *offset)
1379 struct lha *lha = (struct lha *)(a->format->data);
1382 if (lha->entry_unconsumed) {
1383 /* Consume as much as the decompressor actually used. */
1384 __archive_read_consume(a, lha->entry_unconsumed);
1385 lha->entry_unconsumed = 0;
1387 if (lha->end_of_entry) {
1388 if (!lha->end_of_entry_cleanup) {
1389 if ((lha->setflag & CRC_IS_SET) &&
1390 lha->crc != lha->entry_crc_calculated) {
1391 archive_set_error(&a->archive,
1393 "LHa data CRC error");
1394 return (ARCHIVE_WARN);
1397 /* End-of-entry cleanup done. */
1398 lha->end_of_entry_cleanup = 1;
1400 *offset = lha->entry_offset;
1403 return (ARCHIVE_EOF);
1406 if (lha->entry_is_compressed)
1407 r = lha_read_data_lzh(a, buff, size, offset);
1409 /* No compression. */
1410 r = lha_read_data_none(a, buff, size, offset);
1415 * Read a file content in no compression.
1417 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1418 * lha->end_of_entry if it consumes all of the data.
1421 lha_read_data_none(struct archive_read *a, const void **buff,
1422 size_t *size, int64_t *offset)
1424 struct lha *lha = (struct lha *)(a->format->data);
1425 ssize_t bytes_avail;
1427 if (lha->entry_bytes_remaining == 0) {
1430 *offset = lha->entry_offset;
1431 lha->end_of_entry = 1;
1432 return (ARCHIVE_OK);
1435 * Note: '1' here is a performance optimization.
1436 * Recall that the decompression layer returns a count of
1437 * available bytes; asking for more than that forces the
1438 * decompressor to combine reads by copying data.
1440 *buff = __archive_read_ahead(a, 1, &bytes_avail);
1441 if (bytes_avail <= 0) {
1442 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1443 "Truncated LHa file data");
1444 return (ARCHIVE_FATAL);
1446 if (bytes_avail > lha->entry_bytes_remaining)
1447 bytes_avail = lha->entry_bytes_remaining;
1448 lha->entry_crc_calculated =
1449 lha_crc16(lha->entry_crc_calculated, *buff, bytes_avail);
1450 *size = bytes_avail;
1451 *offset = lha->entry_offset;
1452 lha->entry_offset += bytes_avail;
1453 lha->entry_bytes_remaining -= bytes_avail;
1454 if (lha->entry_bytes_remaining == 0)
1455 lha->end_of_entry = 1;
1456 lha->entry_unconsumed = bytes_avail;
1457 return (ARCHIVE_OK);
1461 * Read a file content in LZHUFF encoding.
1463 * Returns ARCHIVE_OK if successful, returns ARCHIVE_WARN if compression is
1464 * unsupported, ARCHIVE_FATAL otherwise, sets lha->end_of_entry if it consumes
1468 lha_read_data_lzh(struct archive_read *a, const void **buff,
1469 size_t *size, int64_t *offset)
1471 struct lha *lha = (struct lha *)(a->format->data);
1472 ssize_t bytes_avail;
1475 /* If the buffer hasn't been allocated, allocate it now. */
1476 if (lha->uncompressed_buffer == NULL) {
1477 lha->uncompressed_buffer_size = 64 * 1024;
1478 lha->uncompressed_buffer
1479 = (unsigned char *)malloc(lha->uncompressed_buffer_size);
1480 if (lha->uncompressed_buffer == NULL) {
1481 archive_set_error(&a->archive, ENOMEM,
1482 "No memory for lzh decompression");
1483 return (ARCHIVE_FATAL);
1487 /* If we haven't yet read any data, initialize the decompressor. */
1488 if (!lha->decompress_init) {
1489 r = lzh_decode_init(&(lha->strm), lha->method);
1493 case ARCHIVE_FAILED:
1494 /* Unsupported compression. */
1498 archive_set_error(&a->archive,
1499 ARCHIVE_ERRNO_FILE_FORMAT,
1500 "Unsupported lzh compression method -%c%c%c-",
1501 lha->method[0], lha->method[1], lha->method[2]);
1502 /* We know compressed size; just skip it. */
1503 archive_read_format_lha_read_data_skip(a);
1504 return (ARCHIVE_WARN);
1506 archive_set_error(&a->archive, ENOMEM,
1507 "Couldn't allocate memory "
1508 "for lzh decompression");
1509 return (ARCHIVE_FATAL);
1511 /* We've initialized decompression for this stream. */
1512 lha->decompress_init = 1;
1513 lha->strm.avail_out = 0;
1514 lha->strm.total_out = 0;
1518 * Note: '1' here is a performance optimization.
1519 * Recall that the decompression layer returns a count of
1520 * available bytes; asking for more than that forces the
1521 * decompressor to combine reads by copying data.
1523 lha->strm.next_in = __archive_read_ahead(a, 1, &bytes_avail);
1524 if (bytes_avail <= 0) {
1525 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1526 "Truncated LHa file body");
1527 return (ARCHIVE_FATAL);
1529 if (bytes_avail > lha->entry_bytes_remaining)
1530 bytes_avail = lha->entry_bytes_remaining;
1532 lha->strm.avail_in = bytes_avail;
1533 lha->strm.total_in = 0;
1534 if (lha->strm.avail_out == 0) {
1535 lha->strm.next_out = lha->uncompressed_buffer;
1536 lha->strm.avail_out = lha->uncompressed_buffer_size;
1539 r = lzh_decode(&(lha->strm), bytes_avail == lha->entry_bytes_remaining);
1544 lha->end_of_entry = 1;
1547 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1549 return (ARCHIVE_FAILED);
1551 lha->entry_unconsumed = lha->strm.total_in;
1552 lha->entry_bytes_remaining -= lha->strm.total_in;
1554 if (lha->strm.avail_out == 0 || lha->end_of_entry) {
1555 *offset = lha->entry_offset;
1556 *size = lha->strm.next_out - lha->uncompressed_buffer;
1557 *buff = lha->uncompressed_buffer;
1558 lha->entry_crc_calculated =
1559 lha_crc16(lha->entry_crc_calculated, *buff, *size);
1560 lha->entry_offset += *size;
1562 *offset = lha->entry_offset;
1566 return (ARCHIVE_OK);
1570 * Skip a file content.
1573 archive_read_format_lha_read_data_skip(struct archive_read *a)
1576 off_t bytes_skipped;
1578 lha = (struct lha *)(a->format->data);
1580 if (lha->entry_unconsumed) {
1581 /* Consume as much as the decompressor actually used. */
1582 __archive_read_consume(a, lha->entry_unconsumed);
1583 lha->entry_unconsumed = 0;
1586 /* if we've already read to end of data, we're done. */
1587 if (lha->end_of_entry_cleanup)
1588 return (ARCHIVE_OK);
1591 * If the length is at the beginning, we can skip the
1592 * compressed data much more quickly.
1594 bytes_skipped = __archive_read_consume(a, lha->entry_bytes_remaining);
1595 if (bytes_skipped < 0)
1596 return (ARCHIVE_FATAL);
1598 /* This entry is finished and done. */
1599 lha->end_of_entry_cleanup = lha->end_of_entry = 1;
1600 return (ARCHIVE_OK);
1604 archive_read_format_lha_cleanup(struct archive_read *a)
1606 struct lha *lha = (struct lha *)(a->format->data);
1608 lzh_decode_free(&(lha->strm));
1609 free(lha->uncompressed_buffer);
1610 archive_string_free(&(lha->dirname));
1611 archive_string_free(&(lha->filename));
1612 archive_string_free(&(lha->uname));
1613 archive_string_free(&(lha->gname));
1614 archive_wstring_free(&(lha->ws));
1616 (a->format->data) = NULL;
1617 return (ARCHIVE_OK);
1621 * 'LHa for UNIX' utility has archived a symbolic-link name after
1622 * a pathname with '|' character.
1623 * This function extracts the symbolic-link name from the pathname.
1626 * 1. a symbolic-name is 'aaa/bb/cc'
1627 * 2. a filename is 'xxx/bbb'
1628 * then a archived pathname is 'xxx/bbb|aaa/bb/cc'
1631 lha_parse_linkname(struct archive_string *linkname,
1632 struct archive_string *pathname)
1637 linkptr = strchr(pathname->s, '|');
1638 if (linkptr != NULL) {
1639 symlen = strlen(linkptr + 1);
1640 archive_strncpy(linkname, linkptr+1, symlen);
1643 pathname->length = strlen(pathname->s);
1650 /* Convert an MSDOS-style date/time into Unix-style time. */
1652 lha_dos_time(const unsigned char *p)
1657 msTime = archive_le16dec(p);
1658 msDate = archive_le16dec(p+2);
1660 memset(&ts, 0, sizeof(ts));
1661 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
1662 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
1663 ts.tm_mday = msDate & 0x1f; /* Day of month. */
1664 ts.tm_hour = (msTime >> 11) & 0x1f;
1665 ts.tm_min = (msTime >> 5) & 0x3f;
1666 ts.tm_sec = (msTime << 1) & 0x3e;
1668 return (mktime(&ts));
1671 /* Convert an MS-Windows-style date/time into Unix-style time. */
1673 lha_win_time(uint64_t wintime, long *ns)
1675 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
1677 if (wintime >= EPOC_TIME) {
1678 wintime -= EPOC_TIME; /* 1970-01-01 00:00:00 (UTC) */
1680 *ns = (long)(wintime % 10000000) * 100;
1681 return (wintime / 10000000);
1689 static unsigned char
1690 lha_calcsum(unsigned char sum, const void *pp, int offset, int size)
1692 unsigned char const *p = (unsigned char const *)pp;
1700 #define CRC16(crc, v) do { \
1701 (crc) = crc16tbl[((crc) ^ v) & 0xFF] ^ ((crc) >> 8); \
1705 lha_crc16(uint16_t crc, const void *pp, size_t len)
1707 const unsigned char *buff = (const unsigned char *)pp;
1710 CRC16(crc, *buff++); CRC16(crc, *buff++);
1711 CRC16(crc, *buff++); CRC16(crc, *buff++);
1712 CRC16(crc, *buff++); CRC16(crc, *buff++);
1713 CRC16(crc, *buff++); CRC16(crc, *buff++);
1718 CRC16(crc, *buff++);
1721 CRC16(crc, *buff++);
1724 CRC16(crc, *buff++);
1727 CRC16(crc, *buff++);
1730 CRC16(crc, *buff++);
1733 CRC16(crc, *buff++);
1746 * Initialize LZHUF decoder.
1748 * Returns ARCHIVE_OK if initialization was successful.
1749 * Returns ARCHIVE_FAILED if method is unsupported.
1750 * Returns ARCHIVE_FATAL if initialization failed; memory allocation
1754 lzh_decode_init(struct lzh_stream *strm, const char *method)
1759 if (strm->ds == NULL) {
1760 strm->ds = calloc(1, sizeof(*strm->ds));
1761 if (strm->ds == NULL)
1762 return (ARCHIVE_FATAL);
1765 ds->error = ARCHIVE_FAILED;
1766 if (method == NULL || method[0] != 'l' || method[1] != 'h')
1767 return (ARCHIVE_FAILED);
1768 switch (method[2]) {
1770 w_bits = 13;/* 8KiB for window */
1773 w_bits = 15;/* 32KiB for window */
1776 w_bits = 16;/* 64KiB for window */
1779 return (ARCHIVE_FAILED);/* Not supported. */
1781 ds->error = ARCHIVE_FATAL;
1782 w_size = ds->w_size;
1783 ds->w_size = 1U << w_bits;
1784 ds->w_mask = ds->w_size -1;
1785 if (ds->w_buff == NULL || w_size != ds->w_size) {
1787 ds->w_buff = malloc(ds->w_size);
1788 if (ds->w_buff == NULL)
1789 return (ARCHIVE_FATAL);
1791 memset(ds->w_buff, 0x20, ds->w_size);
1793 ds->w_remaining = 0;
1795 ds->pos_pt_len_size = w_bits + 1;
1796 ds->pos_pt_len_bits = (w_bits == 15 || w_bits == 16)? 5: 4;
1797 ds->literal_pt_len_size = PT_BITLEN_SIZE;
1798 ds->literal_pt_len_bits = 5;
1799 ds->br.cache_buffer = 0;
1800 ds->br.cache_avail = 0;
1802 if (lzh_huffman_init(&(ds->lt), LT_BITLEN_SIZE, 16)
1804 return (ARCHIVE_FATAL);
1805 ds->lt.len_bits = 9;
1806 if (lzh_huffman_init(&(ds->pt), PT_BITLEN_SIZE, 16)
1808 return (ARCHIVE_FATAL);
1811 return (ARCHIVE_OK);
1815 * Release LZHUF decoder.
1818 lzh_decode_free(struct lzh_stream *strm)
1821 if (strm->ds == NULL)
1823 free(strm->ds->w_buff);
1824 lzh_huffman_free(&(strm->ds->lt));
1825 lzh_huffman_free(&(strm->ds->pt));
1831 * Bit stream reader.
1833 /* Check that the cache buffer has enough bits. */
1834 #define lzh_br_has(br, n) ((br)->cache_avail >= n)
1835 /* Get compressed data by bit. */
1836 #define lzh_br_bits(br, n) \
1837 (((uint16_t)((br)->cache_buffer >> \
1838 ((br)->cache_avail - (n)))) & cache_masks[n])
1839 #define lzh_br_bits_forced(br, n) \
1840 (((uint16_t)((br)->cache_buffer << \
1841 ((n) - (br)->cache_avail))) & cache_masks[n])
1842 /* Read ahead to make sure the cache buffer has enough compressed data we
1844 * True : completed, there is enough data in the cache buffer.
1845 * False : we met that strm->next_in is empty, we have to get following
1847 #define lzh_br_read_ahead_0(strm, br, n) \
1848 (lzh_br_has(br, (n)) || lzh_br_fillup(strm, br))
1849 /* True : the cache buffer has some bits as much as we need.
1850 * False : there are no enough bits in the cache buffer to be used,
1851 * we have to get following bytes if we could. */
1852 #define lzh_br_read_ahead(strm, br, n) \
1853 (lzh_br_read_ahead_0((strm), (br), (n)) || lzh_br_has((br), (n)))
1855 /* Notify how many bits we consumed. */
1856 #define lzh_br_consume(br, n) ((br)->cache_avail -= (n))
1857 #define lzh_br_unconsume(br, n) ((br)->cache_avail += (n))
1859 static const uint16_t cache_masks[] = {
1860 0x0000, 0x0001, 0x0003, 0x0007,
1861 0x000F, 0x001F, 0x003F, 0x007F,
1862 0x00FF, 0x01FF, 0x03FF, 0x07FF,
1863 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF,
1864 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF
1868 * Shift away used bits in the cache data and fill it up with following bits.
1869 * Call this when cache buffer does not have enough bits you need.
1871 * Returns 1 if the cache buffer is full.
1872 * Returns 0 if the cache buffer is not full; input buffer is empty.
1875 lzh_br_fillup(struct lzh_stream *strm, struct lzh_br *br)
1877 int n = CACHE_BITS - br->cache_avail;
1882 if (strm->avail_in >= 8) {
1884 ((uint64_t)strm->next_in[0]) << 56 |
1885 ((uint64_t)strm->next_in[1]) << 48 |
1886 ((uint64_t)strm->next_in[2]) << 40 |
1887 ((uint64_t)strm->next_in[3]) << 32 |
1888 ((uint32_t)strm->next_in[4]) << 24 |
1889 ((uint32_t)strm->next_in[5]) << 16 |
1890 ((uint32_t)strm->next_in[6]) << 8 |
1891 (uint32_t)strm->next_in[7];
1893 strm->avail_in -= 8;
1894 br->cache_avail += 8 * 8;
1899 if (strm->avail_in >= 7) {
1901 (br->cache_buffer << 56) |
1902 ((uint64_t)strm->next_in[0]) << 48 |
1903 ((uint64_t)strm->next_in[1]) << 40 |
1904 ((uint64_t)strm->next_in[2]) << 32 |
1905 ((uint32_t)strm->next_in[3]) << 24 |
1906 ((uint32_t)strm->next_in[4]) << 16 |
1907 ((uint32_t)strm->next_in[5]) << 8 |
1908 (uint32_t)strm->next_in[6];
1910 strm->avail_in -= 7;
1911 br->cache_avail += 7 * 8;
1916 if (strm->avail_in >= 6) {
1918 (br->cache_buffer << 48) |
1919 ((uint64_t)strm->next_in[0]) << 40 |
1920 ((uint64_t)strm->next_in[1]) << 32 |
1921 ((uint32_t)strm->next_in[2]) << 24 |
1922 ((uint32_t)strm->next_in[3]) << 16 |
1923 ((uint32_t)strm->next_in[4]) << 8 |
1924 (uint32_t)strm->next_in[5];
1926 strm->avail_in -= 6;
1927 br->cache_avail += 6 * 8;
1932 /* We have enough compressed data in
1933 * the cache buffer.*/
1938 if (strm->avail_in == 0) {
1939 /* There is not enough compressed data to fill up the
1944 (br->cache_buffer << 8) | *strm->next_in++;
1946 br->cache_avail += 8;
1954 * 1. Returns ARCHIVE_OK if output buffer or input buffer are empty.
1955 * Please set available buffer and call this function again.
1956 * 2. Returns ARCHIVE_EOF if decompression has been completed.
1957 * 3. Returns ARCHIVE_FAILED if an error occurred; compressed data
1958 * is broken or you do not set 'last' flag properly.
1959 * 4. 'last' flag is very important, you must set 1 to the flag if there
1960 * is no input data. The lha compressed data format does not provide how
1961 * to know the compressed data is really finished.
1962 * Note: lha command utility check if the total size of output bytes is
1963 * reached the uncompressed size recorded in its header. it does not mind
1964 * that the decoding process is properly finished.
1965 * GNU ZIP can decompress another compressed file made by SCO LZH compress.
1966 * it handles EOF as null to fill read buffer with zero until the decoding
1967 * process meet 2 bytes of zeros at reading a size of a next chunk, so the
1968 * zeros are treated as the mark of the end of the data although the zeros
1969 * is dummy, not the file data.
1971 static int lzh_read_blocks(struct lzh_stream *, int);
1972 static int lzh_decode_blocks(struct lzh_stream *, int);
1973 #define ST_RD_BLOCK 0
1974 #define ST_RD_PT_1 1
1975 #define ST_RD_PT_2 2
1976 #define ST_RD_PT_3 3
1977 #define ST_RD_PT_4 4
1978 #define ST_RD_LITERAL_1 5
1979 #define ST_RD_LITERAL_2 6
1980 #define ST_RD_LITERAL_3 7
1981 #define ST_RD_POS_DATA_1 8
1982 #define ST_GET_LITERAL 9
1983 #define ST_GET_POS_1 10
1984 #define ST_GET_POS_2 11
1985 #define ST_COPY_DATA 12
1988 lzh_decode(struct lzh_stream *strm, int last)
1990 struct lzh_dec *ds = strm->ds;
1997 avail_in = strm->avail_in;
1999 if (ds->state < ST_GET_LITERAL)
2000 r = lzh_read_blocks(strm, last);
2002 r = lzh_decode_blocks(strm, last);
2004 strm->total_in += avail_in - strm->avail_in;
2009 lzh_copy_from_window(struct lzh_stream *strm, struct lzh_dec *ds)
2013 if (ds->w_remaining == 0 && ds->w_pos > 0) {
2014 if (ds->w_pos - ds->copy_pos <= strm->avail_out)
2015 copy_bytes = ds->w_pos - ds->copy_pos;
2017 copy_bytes = strm->avail_out;
2018 memcpy(strm->next_out,
2019 ds->w_buff + ds->copy_pos, copy_bytes);
2020 ds->copy_pos += copy_bytes;
2022 if (ds->w_remaining <= strm->avail_out)
2023 copy_bytes = ds->w_remaining;
2025 copy_bytes = strm->avail_out;
2026 memcpy(strm->next_out,
2027 ds->w_buff + ds->w_size - ds->w_remaining, copy_bytes);
2028 ds->w_remaining -= copy_bytes;
2030 strm->next_out += copy_bytes;
2031 strm->avail_out -= copy_bytes;
2032 strm->total_out += copy_bytes;
2033 if (strm->avail_out == 0)
2040 lzh_read_blocks(struct lzh_stream *strm, int last)
2042 struct lzh_dec *ds = strm->ds;
2043 struct lzh_br *br = &(ds->br);
2048 switch (ds->state) {
2051 * Read a block number indicates how many blocks
2052 * we will handle. The block is composed of a
2053 * literal and a match, sometimes a literal only
2054 * in particular, there are no reference data at
2055 * the beginning of the decompression.
2057 if (!lzh_br_read_ahead_0(strm, br, 16)) {
2059 /* We need following data. */
2060 return (ARCHIVE_OK);
2061 if (lzh_br_has(br, 8)) {
2063 * It seems there are extra bits.
2064 * 1. Compressed data is broken.
2065 * 2. `last' flag does not properly
2070 if (ds->w_pos > 0) {
2071 if (!lzh_copy_from_window(strm, ds))
2072 return (ARCHIVE_OK);
2074 /* End of compressed data; we have completely
2075 * handled all compressed data. */
2076 return (ARCHIVE_EOF);
2078 ds->blocks_avail = lzh_br_bits(br, 16);
2079 if (ds->blocks_avail == 0)
2081 lzh_br_consume(br, 16);
2083 * Read a literal table compressed in huffman
2086 ds->pt.len_size = ds->literal_pt_len_size;
2087 ds->pt.len_bits = ds->literal_pt_len_bits;
2088 ds->reading_position = 0;
2091 /* Note: ST_RD_PT_1, ST_RD_PT_2 and ST_RD_PT_4 are
2092 * used in reading both a literal table and a
2093 * position table. */
2094 if (!lzh_br_read_ahead(strm, br, ds->pt.len_bits)) {
2096 goto failed;/* Truncated data. */
2097 ds->state = ST_RD_PT_1;
2098 return (ARCHIVE_OK);
2100 ds->pt.len_avail = lzh_br_bits(br, ds->pt.len_bits);
2101 lzh_br_consume(br, ds->pt.len_bits);
2104 if (ds->pt.len_avail == 0) {
2105 /* There is no bitlen. */
2106 if (!lzh_br_read_ahead(strm, br,
2109 goto failed;/* Truncated data.*/
2110 ds->state = ST_RD_PT_2;
2111 return (ARCHIVE_OK);
2113 if (!lzh_make_fake_table(&(ds->pt),
2114 lzh_br_bits(br, ds->pt.len_bits)))
2115 goto failed;/* Invalid data. */
2116 lzh_br_consume(br, ds->pt.len_bits);
2117 if (ds->reading_position)
2118 ds->state = ST_GET_LITERAL;
2120 ds->state = ST_RD_LITERAL_1;
2122 } else if (ds->pt.len_avail > ds->pt.len_size)
2123 goto failed;/* Invalid data. */
2125 memset(ds->pt.freq, 0, sizeof(ds->pt.freq));
2126 if (ds->pt.len_avail < 3 ||
2127 ds->pt.len_size == ds->pos_pt_len_size) {
2128 ds->state = ST_RD_PT_4;
2133 ds->loop = lzh_read_pt_bitlen(strm, ds->loop, 3);
2135 if (ds->loop < 0 || last)
2136 goto failed;/* Invalid data. */
2137 /* Not completed, get following data. */
2138 ds->state = ST_RD_PT_3;
2139 return (ARCHIVE_OK);
2141 /* There are some null in bitlen of the literal. */
2142 if (!lzh_br_read_ahead(strm, br, 2)) {
2144 goto failed;/* Truncated data. */
2145 ds->state = ST_RD_PT_3;
2146 return (ARCHIVE_OK);
2148 c = lzh_br_bits(br, 2);
2149 lzh_br_consume(br, 2);
2150 if (c > ds->pt.len_avail - 3)
2151 goto failed;/* Invalid data. */
2152 for (i = 3; c-- > 0 ;)
2153 ds->pt.bitlen[i++] = 0;
2157 ds->loop = lzh_read_pt_bitlen(strm, ds->loop,
2159 if (ds->loop < ds->pt.len_avail) {
2160 if (ds->loop < 0 || last)
2161 goto failed;/* Invalid data. */
2162 /* Not completed, get following data. */
2163 ds->state = ST_RD_PT_4;
2164 return (ARCHIVE_OK);
2166 if (!lzh_make_huffman_table(&(ds->pt)))
2167 goto failed;/* Invalid data */
2168 if (ds->reading_position) {
2169 ds->state = ST_GET_LITERAL;
2173 case ST_RD_LITERAL_1:
2174 if (!lzh_br_read_ahead(strm, br, ds->lt.len_bits)) {
2176 goto failed;/* Truncated data. */
2177 ds->state = ST_RD_LITERAL_1;
2178 return (ARCHIVE_OK);
2180 ds->lt.len_avail = lzh_br_bits(br, ds->lt.len_bits);
2181 lzh_br_consume(br, ds->lt.len_bits);
2183 case ST_RD_LITERAL_2:
2184 if (ds->lt.len_avail == 0) {
2185 /* There is no bitlen. */
2186 if (!lzh_br_read_ahead(strm, br,
2189 goto failed;/* Truncated data.*/
2190 ds->state = ST_RD_LITERAL_2;
2191 return (ARCHIVE_OK);
2193 if (!lzh_make_fake_table(&(ds->lt),
2194 lzh_br_bits(br, ds->lt.len_bits)))
2195 goto failed;/* Invalid data */
2196 lzh_br_consume(br, ds->lt.len_bits);
2197 ds->state = ST_RD_POS_DATA_1;
2199 } else if (ds->lt.len_avail > ds->lt.len_size)
2200 goto failed;/* Invalid data */
2202 memset(ds->lt.freq, 0, sizeof(ds->lt.freq));
2204 case ST_RD_LITERAL_3:
2206 while (i < ds->lt.len_avail) {
2207 if (!lzh_br_read_ahead(strm, br,
2210 goto failed;/* Truncated data.*/
2212 ds->state = ST_RD_LITERAL_3;
2213 return (ARCHIVE_OK);
2215 rbits = lzh_br_bits(br, ds->pt.max_bits);
2216 c = lzh_decode_huffman(&(ds->pt), rbits);
2218 /* Note: 'c' will never be more than
2219 * eighteen since it's limited by
2220 * PT_BITLEN_SIZE, which is being set
2221 * to ds->pt.len_size through
2222 * ds->literal_pt_len_size. */
2223 lzh_br_consume(br, ds->pt.bitlen[c]);
2226 ds->lt.bitlen[i++] = c;
2227 } else if (c == 0) {
2228 lzh_br_consume(br, ds->pt.bitlen[c]);
2229 ds->lt.bitlen[i++] = 0;
2231 /* c == 1 or c == 2 */
2232 int n = (c == 1)?4:9;
2233 if (!lzh_br_read_ahead(strm, br,
2234 ds->pt.bitlen[c] + n)) {
2235 if (last) /* Truncated data. */
2238 ds->state = ST_RD_LITERAL_3;
2239 return (ARCHIVE_OK);
2241 lzh_br_consume(br, ds->pt.bitlen[c]);
2242 c = lzh_br_bits(br, n);
2243 lzh_br_consume(br, n);
2245 if (i + c > ds->lt.len_avail)
2246 goto failed;/* Invalid data */
2247 memset(&(ds->lt.bitlen[i]), 0, c);
2251 if (i > ds->lt.len_avail ||
2252 !lzh_make_huffman_table(&(ds->lt)))
2253 goto failed;/* Invalid data */
2255 case ST_RD_POS_DATA_1:
2257 * Read a position table compressed in huffman
2260 ds->pt.len_size = ds->pos_pt_len_size;
2261 ds->pt.len_bits = ds->pos_pt_len_bits;
2262 ds->reading_position = 1;
2263 ds->state = ST_RD_PT_1;
2265 case ST_GET_LITERAL:
2270 return (ds->error = ARCHIVE_FAILED);
2274 lzh_decode_blocks(struct lzh_stream *strm, int last)
2276 struct lzh_dec *ds = strm->ds;
2277 struct lzh_br bre = ds->br;
2278 struct huffman *lt = &(ds->lt);
2279 struct huffman *pt = &(ds->pt);
2280 unsigned char *w_buff = ds->w_buff;
2281 unsigned char *lt_bitlen = lt->bitlen;
2282 unsigned char *pt_bitlen = pt->bitlen;
2283 int blocks_avail = ds->blocks_avail, c = 0;
2284 int copy_len = ds->copy_len, copy_pos = ds->copy_pos;
2285 int w_pos = ds->w_pos, w_mask = ds->w_mask, w_size = ds->w_size;
2286 int lt_max_bits = lt->max_bits, pt_max_bits = pt->max_bits;
2287 int state = ds->state;
2289 if (ds->w_remaining > 0) {
2290 if (!lzh_copy_from_window(strm, ds))
2295 case ST_GET_LITERAL:
2297 if (blocks_avail == 0) {
2298 /* We have decoded all blocks.
2299 * Let's handle next blocks. */
2300 ds->state = ST_RD_BLOCK;
2302 ds->blocks_avail = 0;
2308 /* lzh_br_read_ahead() always try to fill the
2309 * cache buffer up. In specific situation we
2310 * are close to the end of the data, the cache
2311 * buffer will not be full and thus we have to
2312 * determine if the cache buffer has some bits
2313 * as much as we need after lzh_br_read_ahead()
2315 if (!lzh_br_read_ahead(strm, &bre,
2319 /* Remaining bits are less than
2320 * maximum bits(lt.max_bits) but maybe
2321 * it still remains as much as we need,
2322 * so we should try to use it with
2324 c = lzh_decode_huffman(lt,
2325 lzh_br_bits_forced(&bre,
2327 lzh_br_consume(&bre, lt_bitlen[c]);
2328 if (!lzh_br_has(&bre, 0))
2329 goto failed;/* Over read. */
2331 c = lzh_decode_huffman(lt,
2332 lzh_br_bits(&bre, lt_max_bits));
2333 lzh_br_consume(&bre, lt_bitlen[c]);
2337 /* Current block is a match data. */
2340 * 'c' is exactly a literal code.
2342 /* Save a decoded code to reference it
2345 if (++w_pos >= w_size) {
2347 ds->w_remaining = w_size;
2348 if (!lzh_copy_from_window(strm, ds))
2352 /* 'c' is the length of a match pattern we have
2353 * already extracted, which has be stored in
2354 * window(ds->w_buff). */
2355 copy_len = c - (UCHAR_MAX + 1) + MINMATCH;
2359 * Get a reference position.
2361 if (!lzh_br_read_ahead(strm, &bre, pt_max_bits)) {
2363 state = ST_GET_POS_1;
2364 ds->copy_len = copy_len;
2367 copy_pos = lzh_decode_huffman(pt,
2368 lzh_br_bits_forced(&bre, pt_max_bits));
2369 lzh_br_consume(&bre, pt_bitlen[copy_pos]);
2370 if (!lzh_br_has(&bre, 0))
2371 goto failed;/* Over read. */
2373 copy_pos = lzh_decode_huffman(pt,
2374 lzh_br_bits(&bre, pt_max_bits));
2375 lzh_br_consume(&bre, pt_bitlen[copy_pos]);
2380 /* We need an additional adjustment number to
2382 int p = copy_pos - 1;
2383 if (!lzh_br_read_ahead(strm, &bre, p)) {
2385 goto failed;/* Truncated data.*/
2386 state = ST_GET_POS_2;
2387 ds->copy_len = copy_len;
2388 ds->copy_pos = copy_pos;
2391 copy_pos = (1 << p) + lzh_br_bits(&bre, p);
2392 lzh_br_consume(&bre, p);
2394 /* The position is actually a distance from the last
2395 * code we had extracted and thus we have to convert
2396 * it to a position of the window. */
2397 copy_pos = (w_pos - copy_pos - 1) & w_mask;
2401 * Copy `copy_len' bytes as extracted data from
2402 * the window into the output buffer.
2408 if (copy_pos > w_pos) {
2409 if (l > w_size - copy_pos)
2410 l = w_size - copy_pos;
2412 if (l > w_size - w_pos)
2415 if ((copy_pos + l < w_pos)
2416 || (w_pos + l < copy_pos)) {
2418 memcpy(w_buff + w_pos,
2419 w_buff + copy_pos, l);
2421 const unsigned char *s;
2426 s = w_buff + copy_pos;
2427 for (li = 0; li < l; li++)
2430 w_pos = (w_pos + l) & w_mask;
2432 ds->w_remaining = w_size;
2433 if (!lzh_copy_from_window(strm, ds)) {
2435 state = ST_GET_LITERAL;
2437 state = ST_COPY_DATA;
2448 /* A copy of current pattern ended. */
2451 copy_pos = (copy_pos + l) & w_mask;
2453 state = ST_GET_LITERAL;
2458 return (ds->error = ARCHIVE_FAILED);
2461 ds->blocks_avail = blocks_avail;
2464 return (ARCHIVE_OK);
2468 lzh_huffman_init(struct huffman *hf, size_t len_size, int tbl_bits)
2472 if (hf->bitlen == NULL) {
2473 hf->bitlen = malloc(len_size * sizeof(hf->bitlen[0]));
2474 if (hf->bitlen == NULL)
2475 return (ARCHIVE_FATAL);
2477 if (hf->tbl == NULL) {
2478 if (tbl_bits < HTBL_BITS)
2482 hf->tbl = malloc((1 << bits) * sizeof(hf->tbl[0]));
2483 if (hf->tbl == NULL)
2484 return (ARCHIVE_FATAL);
2486 if (hf->tree == NULL && tbl_bits > HTBL_BITS) {
2487 hf->tree_avail = 1 << (tbl_bits - HTBL_BITS + 4);
2488 hf->tree = malloc(hf->tree_avail * sizeof(hf->tree[0]));
2489 if (hf->tree == NULL)
2490 return (ARCHIVE_FATAL);
2492 hf->len_size = len_size;
2493 hf->tbl_bits = tbl_bits;
2494 return (ARCHIVE_OK);
2498 lzh_huffman_free(struct huffman *hf)
2506 lzh_read_pt_bitlen(struct lzh_stream *strm, int start, int end)
2508 struct lzh_dec *ds = strm->ds;
2509 struct lzh_br * br = &(ds->br);
2512 for (i = start; i < end;) {
2514 * bit pattern the number we need
2523 * 1111111111110 -> 16
2525 if (!lzh_br_read_ahead(strm, br, 3))
2527 if ((c = lzh_br_bits(br, 3)) == 7) {
2529 if (!lzh_br_read_ahead(strm, br, 13))
2531 d = lzh_br_bits(br, 13);
2537 return (-1);/* Invalid data. */
2538 lzh_br_consume(br, c - 3);
2540 lzh_br_consume(br, 3);
2541 ds->pt.bitlen[i++] = c;
2548 lzh_make_fake_table(struct huffman *hf, uint16_t c)
2550 if (c >= hf->len_size)
2555 hf->bitlen[hf->tbl[0]] = 0;
2560 * Make a huffman coding table.
2563 lzh_make_huffman_table(struct huffman *hf)
2566 const unsigned char *bitlen;
2567 int bitptn[17], weight[17];
2568 int i, maxbits = 0, ptn, tbl_size, w;
2569 int diffbits, len_avail;
2572 * Initialize bit patterns.
2575 for (i = 1, w = 1 << 15; i <= 16; i++, w >>= 1) {
2579 ptn += hf->freq[i] * w;
2583 if (ptn != 0x10000 || maxbits > hf->tbl_bits)
2584 return (0);/* Invalid */
2586 hf->max_bits = maxbits;
2589 * Cut out extra bits which we won't house in the table.
2590 * This preparation reduces the same calculation in the for-loop
2594 int ebits = 16 - maxbits;
2595 for (i = 1; i <= maxbits; i++) {
2596 bitptn[i] >>= ebits;
2597 weight[i] >>= ebits;
2600 if (maxbits > HTBL_BITS) {
2604 diffbits = maxbits - HTBL_BITS;
2605 for (i = 1; i <= HTBL_BITS; i++) {
2606 bitptn[i] >>= diffbits;
2607 weight[i] >>= diffbits;
2609 htbl_max = bitptn[HTBL_BITS] +
2610 weight[HTBL_BITS] * hf->freq[HTBL_BITS];
2611 p = &(hf->tbl[htbl_max]);
2612 while (p < &hf->tbl[1U<<HTBL_BITS])
2616 hf->shift_bits = diffbits;
2621 tbl_size = 1 << HTBL_BITS;
2623 bitlen = hf->bitlen;
2624 len_avail = hf->len_avail;
2626 for (i = 0; i < len_avail; i++) {
2635 /* Get a bit pattern */
2639 if (len <= HTBL_BITS) {
2640 /* Calculate next bit pattern */
2641 if ((bitptn[len] = ptn + cnt) > tbl_size)
2642 return (0);/* Invalid */
2643 /* Update the table */
2646 p[cnt] = (uint16_t)i;
2651 * A bit length is too big to be housed to a direct table,
2652 * so we use a tree model for its extra bits.
2654 bitptn[len] = ptn + cnt;
2655 bit = 1U << (diffbits -1);
2656 extlen = len - HTBL_BITS;
2658 p = &(tbl[ptn >> diffbits]);
2660 *p = len_avail + hf->tree_used;
2661 ht = &(hf->tree[hf->tree_used++]);
2662 if (hf->tree_used > hf->tree_avail)
2663 return (0);/* Invalid */
2667 if (*p < len_avail ||
2668 *p >= (len_avail + hf->tree_used))
2669 return (0);/* Invalid */
2670 ht = &(hf->tree[*p - len_avail]);
2672 while (--extlen > 0) {
2674 if (ht->left < len_avail) {
2675 ht->left = len_avail + hf->tree_used;
2676 ht = &(hf->tree[hf->tree_used++]);
2677 if (hf->tree_used > hf->tree_avail)
2678 return (0);/* Invalid */
2682 ht = &(hf->tree[ht->left - len_avail]);
2685 if (ht->right < len_avail) {
2686 ht->right = len_avail + hf->tree_used;
2687 ht = &(hf->tree[hf->tree_used++]);
2688 if (hf->tree_used > hf->tree_avail)
2689 return (0);/* Invalid */
2693 ht = &(hf->tree[ht->right - len_avail]);
2700 return (0);/* Invalid */
2701 ht->left = (uint16_t)i;
2704 return (0);/* Invalid */
2705 ht->right = (uint16_t)i;
2712 lzh_decode_huffman_tree(struct huffman *hf, unsigned rbits, int c)
2718 extlen = hf->shift_bits;
2719 while (c >= hf->len_avail) {
2721 if (extlen-- <= 0 || c >= hf->tree_used)
2723 if (rbits & (1U << extlen))
2732 lzh_decode_huffman(struct huffman *hf, unsigned rbits)
2736 * At first search an index table for a bit pattern.
2737 * If it fails, search a huffman tree for.
2739 c = hf->tbl[rbits >> hf->shift_bits];
2740 if (c < hf->len_avail)
2742 /* This bit pattern needs to be found out at a huffman tree. */
2743 return (lzh_decode_huffman_tree(hf, rbits, c));