Import libarchive-3.0.2.
[dragonfly.git] / contrib / libarchive / libarchive / archive_read_support_format_lha.c
1 /*-
2  * Copyright (c) 2008-2011 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
26 #include "archive_platform.h"
27
28 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #ifdef HAVE_LIMITS_H
32 #include <limits.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40
41 #include "archive.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"
47
48
49 #define MAXMATCH                256     /* Maximum match length. */
50 #define MINMATCH                3       /* Minimum match length. */
51 /*
52  * Literal table format:
53  * +0              +256                      +510
54  * +---------------+-------------------------+
55  * | literal code  |       match length      |
56  * |   0 ... 255   |  MINMATCH ... MAXMATCH  |
57  * +---------------+-------------------------+
58  *  <---          LT_BITLEN_SIZE         --->
59  */
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)
65
66 struct lzh_dec {
67         /* Decoding status. */
68         int                      state;
69
70         /*
71          * Window to see last 8Ki(lh5),32Ki(lh6),64Ki(lh7) bytes of decoded
72          * data.
73          */
74         int                      w_size;
75         int                      w_mask;
76         /* Window buffer, which is a loop buffer. */
77         unsigned char           *w_buff;
78         /* The insert position to the window. */
79         int                      w_pos;
80         /* The position where we can copy decoded code from the window. */
81         int                      copy_pos;
82         /* The length how many bytes we can copy decoded code from
83          * the window. */
84         int                      copy_len;
85         /* The remaining bytes that we have not copied decoded data from
86          * the window to an output buffer. */
87         int                      w_remaining;
88
89         /*
90          * Bit stream reader.
91          */
92         struct lzh_br {
93 #define CACHE_TYPE              uint64_t
94 #define CACHE_BITS              (8 * sizeof(CACHE_TYPE))
95                 /* Cache buffer. */
96                 CACHE_TYPE       cache_buffer;
97                 /* Indicates how many bits avail in cache_buffer. */
98                 int              cache_avail;
99         } br;
100
101         /*
102          * Huffman coding.
103          */
104         struct huffman {
105                 int              len_size;
106                 int              len_avail;
107                 int              len_bits;
108                 int              freq[17];
109                 unsigned char   *bitlen;
110
111                 /*
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.
115                  */
116 #define HTBL_BITS       10
117                 int              max_bits;
118                 int              shift_bits;
119                 int              tbl_bits;
120                 int              tree_used;
121                 int              tree_avail;
122                 /* Direct access table. */
123                 uint16_t        *tbl;
124                 /* Binary tree table for extra bits over the direct access. */
125                 struct htree_t {
126                         uint16_t left;
127                         uint16_t right;
128                 }               *tree;
129         }                        lt, pt;
130
131         int                      blocks_avail;
132         int                      pos_pt_len_size;
133         int                      pos_pt_len_bits;
134         int                      literal_pt_len_size;
135         int                      literal_pt_len_bits;
136         int                      reading_position;
137         int                      loop;
138         int                      error;
139 };
140
141 struct lzh_stream {
142         const unsigned char     *next_in;
143         int64_t                  avail_in;
144         int64_t                  total_in;
145         unsigned char           *next_out;
146         int64_t                  avail_out;
147         int64_t                  total_out;
148         struct lzh_dec          *ds;
149 };
150
151 struct lha {
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;
157  
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       */
163         int                      setflag;
164 #define BIRTHTIME_IS_SET        1
165 #define ATIME_IS_SET            2
166 #define UNIX_MODE_IS_SET        4
167 #define CRC_IS_SET              8
168         time_t                   birthtime;
169         long                     birthtime_tv_nsec;
170         time_t                   mtime;
171         long                     mtime_tv_nsec;
172         time_t                   atime;
173         long                     atime_tv_nsec;
174         mode_t                   mode;
175         int64_t                  uid;
176         int64_t                  gid;
177         struct archive_string    uname;
178         struct archive_string    gname;
179         uint16_t                 header_crc;
180         uint16_t                 crc;
181         struct archive_string_conv *sconv;
182         struct archive_string_conv *opt_sconv;
183
184         struct archive_string    dirname;
185         struct archive_string    filename;
186         struct archive_wstring   ws;
187
188         unsigned char            dos_attr;
189
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. */
193         char                     directory;
194
195         /* Flags to mark progress of decompression. */
196         char                     decompress_init;
197         char                     end_of_entry;
198         char                     end_of_entry_cleanup;
199         char                     entry_is_compressed;
200
201         unsigned char           *uncompressed_buffer;
202         size_t                   uncompressed_buffer_size;
203
204         char                     format_name[64];
205
206         struct lzh_stream        strm;
207 };
208
209 /*
210  * LHA header common member offset.
211  */
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. */
216
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
250 };
251
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 *);
261
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 *,
275                     int, int);
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);
294
295
296 int
297 archive_read_support_format_lha(struct archive *_a)
298 {
299         struct archive_read *a = (struct archive_read *)_a;
300         struct lha *lha;
301         int r;
302
303         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
304             ARCHIVE_STATE_NEW, "archive_read_support_format_lha");
305
306         lha = (struct lha *)calloc(1, sizeof(*lha));
307         if (lha == NULL) {
308                 archive_set_error(&a->archive, ENOMEM,
309                     "Can't allocate lha data");
310                 return (ARCHIVE_FATAL);
311         }
312         archive_string_init(&lha->ws);
313
314         r = __archive_read_register_format(a,
315             lha,
316             "lha",
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);
323
324         if (r != ARCHIVE_OK)
325                 free(lha);
326         return (ARCHIVE_OK);
327 }
328
329 static size_t
330 lha_check_header_format(const void *h)
331 {
332         const unsigned char *p = h;
333         size_t next_skip_bytes;
334
335         switch (p[H_METHOD_OFFSET+3]) {
336         /*
337          * "-lh0-" ... "-lh7-" "-lhd-"
338          * "-lzs-" "-lz5-"
339          */
340         case '0': case '1': case '2': case '3':
341         case '4': case '5': case '6': case '7':
342         case 'd':
343         case 's':
344                 next_skip_bytes = 4;
345
346                 /* b0 == 0 means the end of an LHa archive file.        */
347                 if (p[0] == 0)
348                         break;
349                 if (p[H_METHOD_OFFSET] != '-' || p[H_METHOD_OFFSET+1] != 'l'
350                     ||  p[H_METHOD_OFFSET+4] != '-')
351                         break;
352
353                 if (p[H_METHOD_OFFSET+2] == 'h') {
354                         /* "-lh?-" */
355                         if (p[H_METHOD_OFFSET+3] == 's')
356                                 break;
357                         if (p[H_LEVEL_OFFSET] == 0)
358                                 return (0);
359                         if (p[H_LEVEL_OFFSET] <= 3 && p[H_ATTR_OFFSET] == 0x20)
360                                 return (0);
361                 }
362                 if (p[H_METHOD_OFFSET+2] == 'z') {
363                         /* LArc extensions: -lzs-,-lz4- and -lz5- */
364                         if (p[H_LEVEL_OFFSET] != 0)
365                                 break;
366                         if (p[H_METHOD_OFFSET+3] == 's'
367                             || p[H_METHOD_OFFSET+3] == '4'
368                             || p[H_METHOD_OFFSET+3] == '5')
369                                 return (0);
370                 }
371                 break;
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;
377         }
378
379         return (next_skip_bytes);
380 }
381
382 static int
383 archive_read_format_lha_bid(struct archive_read *a, int best_bid)
384 {
385         const char *p;
386         const void *buff;
387         ssize_t bytes_avail, offset, window;
388         size_t next;
389
390         /* If there's already a better bid than we can ever
391            make, don't bother testing. */
392         if (best_bid > 30)
393                 return (-1);
394
395         if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL)
396                 return (-1);
397
398         if (lha_check_header_format(p) == 0)
399                 return (30);
400
401         if (p[0] == 'M' && p[1] == 'Z') {
402                 /* PE file */
403                 offset = 0;
404                 window = 4096;
405                 while (offset < (1024 * 20)) {
406                         buff = __archive_read_ahead(a, offset + window,
407                             &bytes_avail);
408                         if (buff == NULL) {
409                                 /* Remaining bytes are less than window. */
410                                 window >>= 1;
411                                 if (window < (H_SIZE + 3))
412                                         return (0);
413                                 continue;
414                         }
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)
418                                         return (30);
419                                 p += next;
420                         }
421                         offset = p - (const char *)buff;
422                 }
423         }
424         return (0);
425 }
426
427 static int
428 archive_read_format_lha_options(struct archive_read *a,
429     const char *key, const char *val)
430 {
431         struct lha *lha;
432         int ret = ARCHIVE_FAILED;
433
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");
439                 else {
440                         lha->opt_sconv =
441                             archive_string_conversion_from_charset(
442                                 &a->archive, val, 0);
443                         if (lha->opt_sconv != NULL)
444                                 ret = ARCHIVE_OK;
445                         else
446                                 ret = ARCHIVE_FATAL;
447                 }
448         } else
449                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
450                     "lha: unknown keyword ``%s''", key);
451
452         return (ret);
453 }
454
455 static int
456 lha_skip_sfx(struct archive_read *a)
457 {
458         const void *h;
459         const char *p, *q;
460         size_t next, skip;
461         ssize_t bytes, window;
462
463         window = 4096;
464         for (;;) {
465                 h = __archive_read_ahead(a, window, &bytes);
466                 if (h == NULL) {
467                         /* Remaining bytes are less than window. */
468                         window >>= 1;
469                         if (window < (H_SIZE + 3))
470                                 goto fatal;
471                         continue;
472                 }
473                 if (bytes < H_SIZE)
474                         goto fatal;
475                 p = h;
476                 q = p + bytes;
477
478                 /*
479                  * Scan ahead until we find something that looks
480                  * like the lha header.
481                  */
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);
486                                 return (ARCHIVE_OK);
487                         }
488                         p += next;
489                 }
490                 skip = p - (const char *)h;
491                 __archive_read_consume(a, skip);
492         }
493 fatal:
494         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
495             "Couldn't find out LHa header");
496         return (ARCHIVE_FATAL);
497 }
498
499 static int
500 truncated_error(struct archive_read *a)
501 {
502         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
503             "Truncated LHa header");
504         return (ARCHIVE_FATAL);
505 }
506
507 static int
508 archive_read_format_lha_read_header(struct archive_read *a,
509     struct archive_entry *entry)
510 {
511         struct archive_string linkname;
512         struct archive_string pathname;
513         struct lha *lha;
514         const unsigned char *p;
515         const char *signature;
516         int err;
517         
518         a->archive.archive_format = ARCHIVE_FORMAT_LHA;
519         if (a->archive.archive_format_name == NULL)
520                 a->archive.archive_format_name = "lha";
521
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;
527
528         if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL) {
529                 /*
530                  * LHa archiver added 0 to the tail of its archive file as
531                  * the mark of the end of the archive.
532                  */
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));
537         }
538
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)
545                         return (err);
546
547                 if ((p = __archive_read_ahead(a, sizeof(*p), NULL)) == NULL)
548                         return (truncated_error(a));
549                 signature = (const char *)p;
550         }
551         /* signature[0] == 0 means the end of an LHa archive file. */
552         if (signature[0] == 0)
553                 return (ARCHIVE_EOF);
554
555         /*
556          * Check the header format and method type.
557          */
558         if (lha_check_header_format(p) != 0) {
559                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
560                     "Bad LHa file");
561                 return (ARCHIVE_FATAL);
562         }
563
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)
573                 lha->directory = 1;
574         else
575                 lha->directory = 0;
576         if (memcmp(lha->method, "lh0", 3) == 0 ||
577             memcmp(lha->method, "lz4", 3) == 0)
578                 lha->entry_is_compressed = 0;
579         else
580                 lha->entry_is_compressed = 1;
581
582         lha->compsize = 0;
583         lha->origsize = 0;
584         lha->setflag = 0;
585         lha->birthtime = 0;
586         lha->birthtime_tv_nsec = 0;
587         lha->mtime = 0;
588         lha->mtime_tv_nsec = 0;
589         lha->atime = 0;
590         lha->atime_tv_nsec = 0;
591         lha->mode = (lha->directory)? 0777 : 0666;
592         lha->uid = 0;
593         lha->gid = 0;
594         archive_string_empty(&lha->dirname);
595         archive_string_empty(&lha->filename);
596         lha->dos_attr = 0;
597         if (lha->opt_sconv != NULL)
598                 lha->sconv = lha->opt_sconv;
599         else
600                 lha->sconv = NULL;
601
602         switch (p[H_LEVEL_OFFSET]) {
603         case 0:
604                 err = lha_read_file_header_0(a, lha);
605                 break;
606         case 1:
607                 err = lha_read_file_header_1(a, lha);
608                 break;
609         case 2:
610                 err = lha_read_file_header_2(a, lha);
611                 break;
612         case 3:
613                 err = lha_read_file_header_3(a, lha);
614                 break;
615         default:
616                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
617                     "Unsupported LHa header level %d", p[H_LEVEL_OFFSET]);
618                 err = ARCHIVE_FATAL;
619                 break;
620         }
621         if (err < ARCHIVE_WARN)
622                 return (err);
623
624
625         if (!lha->directory && archive_strlen(&lha->filename) == 0)
626                 /* The filename has not been set */
627                 return (truncated_error(a));
628
629         /*
630          * Make a pathname from a dirname and a filename.
631          */
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);
636
637         if ((lha->mode & AE_IFMT) == AE_IFLNK) {
638                 /*
639                  * Extract the symlink-name if it's included in the pathname.
640                  */
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);
649                 }
650         } else {
651                 /*
652                  * Make sure a file-type is set.
653                  * The mode has been overridden if it is in the extended data.
654                  */
655                 lha->mode = (lha->mode & ~AE_IFMT) |
656                     ((lha->directory)? AE_IFDIR: AE_IFREG);
657         }
658         if ((lha->setflag & UNIX_MODE_IS_SET) == 0 &&
659             (lha->dos_attr & 1) != 0)
660                 lha->mode &= ~(0222);/* read only. */
661
662         /*
663          * Set basic file parameters.
664          */
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);
671                 }
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));
677                 err = ARCHIVE_WARN;
678         }
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);
687                         }
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));
693                         err = ARCHIVE_WARN;
694                 }
695         } else
696                 archive_entry_set_symlink(entry, NULL);
697         archive_string_free(&linkname);
698         /*
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 '/'.
702          */
703         if (p[H_LEVEL_OFFSET] == 0)
704                 lha_replace_path_separator(lha, entry);
705
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);
718         } else {
719                 archive_entry_unset_birthtime(entry);
720                 archive_entry_unset_ctime(entry);
721         }
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,
725                     lha->atime_tv_nsec);
726         else
727                 archive_entry_unset_atime(entry);
728         if (lha->directory || archive_entry_symlink(entry) != NULL)
729                 archive_entry_unset_size(entry);
730         else
731                 archive_entry_set_size(entry, lha->origsize);
732
733         /*
734          * Prepare variables used to read a file content.
735          */
736         lha->entry_bytes_remaining = lha->compsize;
737         lha->entry_offset = 0;
738         lha->entry_crc_calculated = 0;
739
740         /*
741          * This file does not have a content.
742          */
743         if (lha->directory || lha->compsize == 0)
744                 lha->end_of_entry = 1;
745
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;
749
750         return (err);
751 }
752
753 /*
754  * Replace a DOS path separator '\' by a character '/'.
755  * Some multi-byte character set have  a character '\' in its second byte.
756  */
757 static void
758 lha_replace_path_separator(struct lha *lha, struct archive_entry *entry)
759 {
760         const wchar_t *wp;
761         size_t i;
762
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'\\')
767                                 lha->ws.s[i] = L'/';
768                 }
769                 archive_entry_copy_pathname_w(entry, lha->ws.s);
770         }
771
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'\\')
776                                 lha->ws.s[i] = L'/';
777                 }
778                 archive_entry_copy_symlink_w(entry, lha->ws.s);
779         }
780 }
781
782 /*
783  * Header 0 format
784  *
785  * +0              +1         +2               +7                  +11
786  * +---------------+----------+----------------+-------------------+
787  * |header size(*1)|header sum|compression type|compressed size(*2)|
788  * +---------------+----------+----------------+-------------------+
789  *                             <---------------------(*1)----------*
790  *
791  * +11               +15       +17       +19            +20              +21
792  * +-----------------+---------+---------+--------------+----------------+
793  * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=0)|
794  * +-----------------+---------+---------+--------------+----------------+
795  * *--------------------------------(*1)---------------------------------*
796  *
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)-------------------------->
803  *
804  */
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
813 static int
814 lha_read_file_header_0(struct archive_read *a, struct lha *lha)
815 {
816         const unsigned char *p;
817         int extdsize, namelen;
818         unsigned char headersum, sum_calculated;
819
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);
833         }
834         if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
835                 return (truncated_error(a));
836
837         archive_strncpy(&lha->filename, p + H0_FILE_NAME_OFFSET, namelen);
838         /* When extdsize == -2, A CRC16 value is not present in the header. */
839         if (extdsize >= 0) {
840                 lha->crc = archive_le16dec(p + H0_FILE_NAME_OFFSET + namelen);
841                 lha->setflag |= CRC_IS_SET;
842         }
843         sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
844
845         /* Read an extended header */
846         if (extdsize > 0) {
847                 /* This extended data is set by 'LHa for UNIX' only.
848                  * Maybe fixed size.
849                  */
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;
858                 }
859         }
860         __archive_read_consume(a, lha->header_size);
861
862         if (sum_calculated != headersum) {
863                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
864                     "LHa header sum error");
865                 return (ARCHIVE_FATAL);
866         }
867
868         return (ARCHIVE_OK);
869 }
870
871 /*
872  * Header 1 format
873  *
874  * +0              +1         +2               +7            +11
875  * +---------------+----------+----------------+-------------+
876  * |header size(*1)|header sum|compression type|skip size(*2)|
877  * +---------------+----------+----------------+-------------+
878  *                             <---------------(*1)----------*
879  *
880  * +11               +15       +17       +19            +20              +21
881  * +-----------------+---------+---------+--------------+----------------+
882  * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=1)|
883  * +-----------------+---------+---------+--------------+----------------+
884  * *-------------------------------(*1)----------------------------------*
885  *
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  * +---------------+---------+----------+-----------+-----------+
890  *                  <--(*3)->
891  * *----------------------------(*1)----------------------------*
892  *
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)-------------------->
898  */
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
907 static int
908 lha_read_file_header_1(struct archive_read *a, struct lha *lha)
909 {
910         const unsigned char *p;
911         size_t extdsize;
912         int i, err, err2;
913         int namelen, padding;
914         unsigned char headersum, sum_calculated;
915
916         err = ARCHIVE_OK;
917
918         if ((p = __archive_read_ahead(a, H1_FIXED_SIZE, NULL)) == NULL)
919                 return (truncated_error(a));
920
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;
930
931         if (namelen > 230 || padding < 0)
932                 goto invalid;
933
934         if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
935                 return (truncated_error(a));
936
937         for (i = 0; i < namelen; i++) {
938                 if (p[i + H1_FILE_NAME_OFFSET] == 0xff)
939                         goto invalid;/* Invalid filename. */
940         }
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;
944
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);
949
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)
954                 return (err2);
955         if (err2 < err)
956                 err = err2;
957         /* Get a real compressed file size. */
958         lha->compsize -= extdsize - 2;
959
960         if (sum_calculated != headersum) {
961                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
962                     "LHa header sum error");
963                 return (ARCHIVE_FATAL);
964         }
965         return (err);
966 invalid:
967         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
968             "Invalid LHa header");
969         return (ARCHIVE_FATAL);
970 }
971
972 /*
973  * Header 2 format
974  *
975  * +0              +2               +7                  +11               +15
976  * +---------------+----------------+-------------------+-----------------+
977  * |header size(*1)|compression type|compressed size(*2)|uncompressed size|
978  * +---------------+----------------+-------------------+-----------------+
979  *  <--------------------------------(*1)---------------------------------*
980  *
981  * +15               +19          +20              +21        +23         +24
982  * +-----------------+------------+----------------+----------+-----------+
983  * |data/time(time_t)| 0x20 fixed |header level(=2)|file CRC16|  creator  |
984  * +-----------------+------------+----------------+----------+-----------+
985  * *---------------------------------(*1)---------------------------------*
986  *
987  * +24              +26                 +26+(*3)      +26+(*3)+(*4)
988  * +----------------+-------------------+-------------+-------------------+
989  * |next header size|extended header(*3)| padding(*4) |  compressed data  |
990  * +----------------+-------------------+-------------+-------------------+
991  * *--------------------------(*1)-------------------> <------(*2)------->
992  *
993  */
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
1000 static int
1001 lha_read_file_header_2(struct archive_read *a, struct lha *lha)
1002 {
1003         const unsigned char *p;
1004         size_t extdsize;
1005         int err, padding;
1006         uint16_t header_crc;
1007
1008         if ((p = __archive_read_ahead(a, H2_FIXED_SIZE, NULL)) == NULL)
1009                 return (truncated_error(a));
1010
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;
1017
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);
1022         }
1023
1024         header_crc = lha_crc16(0, p, H2_FIXED_SIZE);
1025         __archive_read_consume(a, H2_FIXED_SIZE);
1026
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)
1031                 return (err);
1032
1033         /* Calculate a padding size. The result will be normally 0 or 1. */
1034         padding = (int)lha->header_size - (int)(H2_FIXED_SIZE + extdsize);
1035         if (padding > 0) {
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);
1040         }
1041
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);
1046         }
1047         return (err);
1048 }
1049
1050 /*
1051  * Header 3 format
1052  *
1053  * +0           +2               +7                  +11               +15
1054  * +------------+----------------+-------------------+-----------------+
1055  * | 0x04 fixed |compression type|compressed size(*2)|uncompressed size|
1056  * +------------+----------------+-------------------+-----------------+
1057  *  <-------------------------------(*1)-------------------------------*
1058  *
1059  * +15               +19          +20              +21        +23         +24
1060  * +-----------------+------------+----------------+----------+-----------+
1061  * |date/time(time_t)| 0x20 fixed |header level(=3)|file CRC16|  creator  |
1062  * +-----------------+------------+----------------+----------+-----------+
1063  * *--------------------------------(*1)----------------------------------*
1064  *
1065  * +24             +28              +32                 +32+(*3)
1066  * +---------------+----------------+-------------------+-----------------+
1067  * |header size(*1)|next header size|extended header(*3)| compressed data |
1068  * +---------------+----------------+-------------------+-----------------+
1069  * *------------------------(*1)-----------------------> <------(*2)----->
1070  *
1071  */
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
1079 static int
1080 lha_read_file_header_3(struct archive_read *a, struct lha *lha)
1081 {
1082         const unsigned char *p;
1083         size_t extdsize;
1084         int err;
1085         uint16_t header_crc;
1086
1087         if ((p = __archive_read_ahead(a, H3_FIXED_SIZE, NULL)) == NULL)
1088                 return (truncated_error(a));
1089
1090         if (archive_le16dec(p + H3_FIELD_LEN_OFFSET) != 4)
1091                 goto invalid;
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;
1098
1099         if (lha->header_size < H3_FIXED_SIZE + 4)
1100                 goto invalid;
1101         header_crc = lha_crc16(0, p, H3_FIXED_SIZE);
1102         __archive_read_consume(a, H3_FIXED_SIZE);
1103
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)
1108                 return (err);
1109
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);
1114         }
1115         return (err);
1116 invalid:
1117         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1118             "Invalid LHa header");
1119         return (ARCHIVE_FATAL);
1120 }
1121
1122 /*
1123  * Extended header format
1124  *
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 --*
1131  *
1132  * If the ex-header size is zero, it is the make of the end of extended
1133  * headers.
1134  *
1135  */
1136 static int
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)
1139 {
1140         const void *h;
1141         const unsigned char *extdheader;
1142         size_t  extdsize;
1143         size_t  datasize;
1144         unsigned int i;
1145         unsigned char extdtype;
1146
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            */
1164
1165         *total_size = sizefield_length;
1166
1167         for (;;) {
1168                 /* Read an extended header size. */
1169                 if ((h =
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);
1176                 else
1177                         extdsize = archive_le32dec(h);
1178                 if (extdsize == 0) {
1179                         /* End of extended header */
1180                         if (crc != NULL)
1181                                 *crc = lha_crc16(*crc, h, sizefield_length);
1182                         __archive_read_consume(a, sizefield_length);
1183                         return (ARCHIVE_OK);
1184                 }
1185
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)
1190                         goto invalid;
1191
1192                 /* Read the extended header. */
1193                 if ((h = __archive_read_ahead(a, extdsize, NULL)) == NULL)
1194                         return (truncated_error(a));
1195                 *total_size += extdsize;
1196
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;
1204
1205                 if (crc != NULL && extdtype != EXT_HEADER_CRC)
1206                         *crc = lha_crc16(*crc, h, extdsize);
1207                 switch (extdtype) {
1208                 case EXT_HEADER_CRC:
1209                         /* We only use a header CRC. Following data will not
1210                          * be used. */
1211                         if (datasize >= 2) {
1212                                 lha->header_crc = archive_le16dec(extdheader);
1213                                 if (crc != NULL) {
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);
1221                                 }
1222                         }
1223                         break;
1224                 case EXT_FILENAME:
1225                         if (datasize == 0) {
1226                                 /* maybe directory header */
1227                                 archive_string_empty(&lha->filename);
1228                                 break;
1229                         }
1230                         archive_strncpy(&lha->filename,
1231                             (const char *)extdheader, datasize);
1232                         break;
1233                 case EXT_DIRECTORY:
1234                         if (datasize == 0)
1235                                 /* no directory name data. exit this case. */
1236                                 break;
1237
1238                         archive_strncpy(&lha->dirname,
1239                             (const char *)extdheader, datasize);
1240                         /*
1241                          * Convert directory delimiter from 0xFF
1242                          * to '/' for local system.
1243                          */
1244                         for (i = 0; i < lha->dirname.length; i++) {
1245                                 if ((unsigned char)lha->dirname.s[i] == 0xFF)
1246                                         lha->dirname.s[i] = '/';
1247                         }
1248                         /* Is last character directory separator? */
1249                         if (lha->dirname.s[lha->dirname.length-1] != '/')
1250                                 /* invalid directory data */
1251                                 goto invalid;
1252                         break;
1253                 case EXT_DOS_ATTR:
1254                         if (datasize == 2)
1255                                 lha->dos_attr = (unsigned char)
1256                                     (archive_le16dec(extdheader) & 0xff);
1257                         break;
1258                 case EXT_TIMESTAMP:
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 |
1272                                     ATIME_IS_SET;
1273                         }
1274                         break;
1275                 case EXT_FILESIZE:
1276                         if (datasize == sizeof(uint64_t) * 2) {
1277                                 lha->compsize = archive_le64dec(extdheader);
1278                                 extdheader += sizeof(uint64_t);
1279                                 lha->origsize = archive_le64dec(extdheader);
1280                         }
1281                         break;
1282                 case EXT_CODEPAGE:
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;
1289
1290                                 archive_string_init(&cp);
1291                                 switch (archive_le32dec(extdheader)) {
1292                                 case 65001: /* UTF-8 */
1293                                         charset = "UTF-8";
1294                                         break;
1295                                 default:
1296                                         archive_string_sprintf(&cp, "CP%d",
1297                                             (int)archive_le32dec(extdheader));
1298                                         charset = cp.s;
1299                                         break;
1300                                 }
1301                                 lha->sconv =
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);
1307                         }
1308                         break;
1309                 case EXT_UNIX_MODE:
1310                         if (datasize == sizeof(uint16_t)) {
1311                                 lha->mode = archive_le16dec(extdheader);
1312                                 lha->setflag |= UNIX_MODE_IS_SET;
1313                         }
1314                         break;
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);
1319                         }
1320                         break;
1321                 case EXT_UNIX_GNAME:
1322                         if (datasize > 0)
1323                                 archive_strncpy(&lha->gname,
1324                                     (const char *)extdheader, datasize);
1325                         break;
1326                 case EXT_UNIX_UNAME:
1327                         if (datasize > 0)
1328                                 archive_strncpy(&lha->uname,
1329                                     (const char *)extdheader, datasize);
1330                         break;
1331                 case EXT_UNIX_MTIME:
1332                         if (datasize == sizeof(uint32_t))
1333                                 lha->mtime = archive_le32dec(extdheader);
1334                         break;
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;
1347                         }
1348                         break;
1349                 case EXT_NEW_ATTR:
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;
1358                         }
1359                         break;
1360                 case EXT_TIMEZONE:              /* Not supported */
1361                 case EXT_UTF16_FILENAME:        /* Not supported */
1362                 case EXT_UTF16_DIRECTORY:       /* Not supported */
1363                 default:
1364                         break;
1365                 }
1366
1367                 __archive_read_consume(a, extdsize);
1368         }
1369 invalid:
1370         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1371             "Invalid extended LHa header");
1372         return (ARCHIVE_FATAL);
1373 }
1374
1375 static int
1376 archive_read_format_lha_read_data(struct archive_read *a,
1377     const void **buff, size_t *size, int64_t *offset)
1378 {
1379         struct lha *lha = (struct lha *)(a->format->data);
1380         int r;
1381
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;
1386         }
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,
1392                                     ARCHIVE_ERRNO_MISC,
1393                                     "LHa data CRC error");
1394                                 return (ARCHIVE_WARN);
1395                         }
1396
1397                         /* End-of-entry cleanup done. */
1398                         lha->end_of_entry_cleanup = 1;
1399                 }
1400                 *offset = lha->entry_offset;
1401                 *size = 0;
1402                 *buff = NULL;
1403                 return (ARCHIVE_EOF);
1404         }
1405
1406         if (lha->entry_is_compressed)
1407                 r =  lha_read_data_lzh(a, buff, size, offset);
1408         else
1409                 /* No compression. */
1410                 r =  lha_read_data_none(a, buff, size, offset);
1411         return (r);
1412 }
1413
1414 /*
1415  * Read a file content in no compression.
1416  *
1417  * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1418  * lha->end_of_entry if it consumes all of the data.
1419  */
1420 static int
1421 lha_read_data_none(struct archive_read *a, const void **buff,
1422     size_t *size, int64_t *offset)
1423 {
1424         struct lha *lha = (struct lha *)(a->format->data);
1425         ssize_t bytes_avail;
1426
1427         if (lha->entry_bytes_remaining == 0) {
1428                 *buff = NULL;
1429                 *size = 0;
1430                 *offset = lha->entry_offset;
1431                 lha->end_of_entry = 1;
1432                 return (ARCHIVE_OK);
1433         }
1434         /*
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.
1439          */
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);
1445         }
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);
1458 }
1459
1460 /*
1461  * Read a file content in LZHUFF encoding.
1462  *
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
1465  * all of the data.
1466  */
1467 static int
1468 lha_read_data_lzh(struct archive_read *a, const void **buff,
1469     size_t *size, int64_t *offset)
1470 {
1471         struct lha *lha = (struct lha *)(a->format->data);
1472         ssize_t bytes_avail;
1473         int r;
1474
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);
1484                 }
1485         }
1486
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);
1490                 switch (r) {
1491                 case ARCHIVE_OK:
1492                         break;
1493                 case ARCHIVE_FAILED:
1494                         /* Unsupported compression. */
1495                         *buff = NULL;
1496                         *size = 0;
1497                         *offset = 0;
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);
1505                 default:
1506                         archive_set_error(&a->archive, ENOMEM,
1507                             "Couldn't allocate memory "
1508                             "for lzh decompression");
1509                         return (ARCHIVE_FATAL);
1510                 }
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;
1515         }
1516
1517         /*
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.
1522          */
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);
1528         }
1529         if (bytes_avail > lha->entry_bytes_remaining)
1530                 bytes_avail = lha->entry_bytes_remaining;
1531
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;
1537         }
1538
1539         r = lzh_decode(&(lha->strm), bytes_avail == lha->entry_bytes_remaining);
1540         switch (r) {
1541         case ARCHIVE_OK:
1542                 break;
1543         case ARCHIVE_EOF:
1544                 lha->end_of_entry = 1;
1545                 break;
1546         default:
1547                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1548                     "Bad lzh data");
1549                 return (ARCHIVE_FAILED);
1550         }
1551         lha->entry_unconsumed = lha->strm.total_in;
1552         lha->entry_bytes_remaining -= lha->strm.total_in;
1553
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;
1561         } else {
1562                 *offset = lha->entry_offset;
1563                 *size = 0;
1564                 *buff = NULL;
1565         }
1566         return (ARCHIVE_OK);
1567 }
1568
1569 /*
1570  * Skip a file content.
1571  */
1572 static int
1573 archive_read_format_lha_read_data_skip(struct archive_read *a)
1574 {
1575         struct lha *lha;
1576         off_t bytes_skipped;
1577
1578         lha = (struct lha *)(a->format->data);
1579
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;
1584         }
1585
1586         /* if we've already read to end of data, we're done. */
1587         if (lha->end_of_entry_cleanup)
1588                 return (ARCHIVE_OK);
1589
1590         /*
1591          * If the length is at the beginning, we can skip the
1592          * compressed data much more quickly.
1593          */
1594         bytes_skipped = __archive_read_consume(a, lha->entry_bytes_remaining);
1595         if (bytes_skipped < 0)
1596                 return (ARCHIVE_FATAL);
1597
1598         /* This entry is finished and done. */
1599         lha->end_of_entry_cleanup = lha->end_of_entry = 1;
1600         return (ARCHIVE_OK);
1601 }
1602
1603 static int
1604 archive_read_format_lha_cleanup(struct archive_read *a)
1605 {
1606         struct lha *lha = (struct lha *)(a->format->data);
1607
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));
1615         free(lha);
1616         (a->format->data) = NULL;
1617         return (ARCHIVE_OK);
1618 }
1619
1620 /*
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.
1624  *
1625  * example.
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'
1629  */
1630 static int
1631 lha_parse_linkname(struct archive_string *linkname,
1632     struct archive_string *pathname)
1633 {
1634         char *  linkptr;
1635         int     symlen;
1636
1637         linkptr = strchr(pathname->s, '|');
1638         if (linkptr != NULL) {
1639                 symlen = strlen(linkptr + 1);
1640                 archive_strncpy(linkname, linkptr+1, symlen);
1641
1642                 *linkptr = 0;
1643                 pathname->length = strlen(pathname->s);
1644
1645                 return (1);
1646         }
1647         return (0);
1648 }
1649
1650 /* Convert an MSDOS-style date/time into Unix-style time. */
1651 static time_t
1652 lha_dos_time(const unsigned char *p)
1653 {
1654         int msTime, msDate;
1655         struct tm ts;
1656
1657         msTime = archive_le16dec(p);
1658         msDate = archive_le16dec(p+2);
1659
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;
1667         ts.tm_isdst = -1;
1668         return (mktime(&ts));
1669 }
1670
1671 /* Convert an MS-Windows-style date/time into Unix-style time. */
1672 static time_t
1673 lha_win_time(uint64_t wintime, long *ns)
1674 {
1675 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
1676
1677         if (wintime >= EPOC_TIME) {
1678                 wintime -= EPOC_TIME;   /* 1970-01-01 00:00:00 (UTC) */
1679                 if (ns != NULL)
1680                         *ns = (long)(wintime % 10000000) * 100;
1681                 return (wintime / 10000000);
1682         } else {
1683                 if (ns != NULL)
1684                         *ns = 0;
1685                 return (0);
1686         }
1687 }
1688
1689 static unsigned char
1690 lha_calcsum(unsigned char sum, const void *pp, int offset, int size)
1691 {
1692         unsigned char const *p = (unsigned char const *)pp;
1693
1694         p += offset;
1695         while (--size >= 0)
1696                 sum += *p++;
1697         return (sum);
1698 }
1699
1700 #define CRC16(crc, v)   do {    \
1701         (crc) = crc16tbl[((crc) ^ v) & 0xFF] ^ ((crc) >> 8);    \
1702 } while (0)
1703
1704 static uint16_t
1705 lha_crc16(uint16_t crc, const void *pp, size_t len)
1706 {
1707         const unsigned char *buff = (const unsigned char *)pp;
1708
1709         while (len >= 8) {
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++);
1714                 len -= 8;
1715         }
1716         switch (len) {
1717         case 7:
1718                 CRC16(crc, *buff++);
1719                 /* FALL THROUGH */
1720         case 6:
1721                 CRC16(crc, *buff++);
1722                 /* FALL THROUGH */
1723         case 5:
1724                 CRC16(crc, *buff++);
1725                 /* FALL THROUGH */
1726         case 4:
1727                 CRC16(crc, *buff++);
1728                 /* FALL THROUGH */
1729         case 3:
1730                 CRC16(crc, *buff++);
1731                 /* FALL THROUGH */
1732         case 2:
1733                 CRC16(crc, *buff++);
1734                 /* FALL THROUGH */
1735         case 1:
1736                 CRC16(crc, *buff);
1737                 /* FALL THROUGH */
1738         case 0:
1739                 break;
1740         }
1741         return (crc);
1742 }
1743
1744
1745 /*
1746  * Initialize LZHUF decoder.
1747  *
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
1751  * error occurred.
1752  */
1753 static int
1754 lzh_decode_init(struct lzh_stream *strm, const char *method)
1755 {
1756         struct lzh_dec *ds;
1757         int w_bits, w_size;
1758
1759         if (strm->ds == NULL) {
1760                 strm->ds = calloc(1, sizeof(*strm->ds));
1761                 if (strm->ds == NULL)
1762                         return (ARCHIVE_FATAL);
1763         }
1764         ds = strm->ds;
1765         ds->error = ARCHIVE_FAILED;
1766         if (method == NULL || method[0] != 'l' || method[1] != 'h')
1767                 return (ARCHIVE_FAILED);
1768         switch (method[2]) {
1769         case '5':
1770                 w_bits = 13;/* 8KiB for window */
1771                 break;
1772         case '6':
1773                 w_bits = 15;/* 32KiB for window */
1774                 break;
1775         case '7':
1776                 w_bits = 16;/* 64KiB for window */
1777                 break;
1778         default:
1779                 return (ARCHIVE_FAILED);/* Not supported. */
1780         }
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) {
1786                 free(ds->w_buff);
1787                 ds->w_buff = malloc(ds->w_size);
1788                 if (ds->w_buff == NULL)
1789                         return (ARCHIVE_FATAL);
1790         }
1791         memset(ds->w_buff, 0x20, ds->w_size);
1792         ds->w_pos = 0;
1793         ds->w_remaining = 0;
1794         ds->state = 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;
1801
1802         if (lzh_huffman_init(&(ds->lt), LT_BITLEN_SIZE, 16)
1803             != ARCHIVE_OK)
1804                 return (ARCHIVE_FATAL);
1805         ds->lt.len_bits = 9;
1806         if (lzh_huffman_init(&(ds->pt), PT_BITLEN_SIZE, 16)
1807             != ARCHIVE_OK)
1808                 return (ARCHIVE_FATAL);
1809         ds->error = 0;
1810
1811         return (ARCHIVE_OK);
1812 }
1813
1814 /*
1815  * Release LZHUF decoder.
1816  */
1817 static void
1818 lzh_decode_free(struct lzh_stream *strm)
1819 {
1820
1821         if (strm->ds == NULL)
1822                 return;
1823         free(strm->ds->w_buff);
1824         lzh_huffman_free(&(strm->ds->lt));
1825         lzh_huffman_free(&(strm->ds->pt));
1826         free(strm->ds);
1827         strm->ds = NULL;
1828 }
1829
1830 /*
1831  * Bit stream reader.
1832  */
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
1843  * will use.
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
1846  *          bytes. */
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)))
1854
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))
1858
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
1865 };
1866
1867 /*
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.
1870  *
1871  * Returns 1 if the cache buffer is full.
1872  * Returns 0 if the cache buffer is not full; input buffer is empty.
1873  */
1874 static int
1875 lzh_br_fillup(struct lzh_stream *strm, struct lzh_br *br)
1876 {
1877         int n = CACHE_BITS - br->cache_avail;
1878
1879         for (;;) {
1880                 switch (n >> 3) {
1881                 case 8:
1882                         if (strm->avail_in >= 8) {
1883                                 br->cache_buffer =
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];
1892                                 strm->next_in += 8;
1893                                 strm->avail_in -= 8;
1894                                 br->cache_avail += 8 * 8;
1895                                 return (1);
1896                         }
1897                         break;
1898                 case 7:
1899                         if (strm->avail_in >= 7) {
1900                                 br->cache_buffer =
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];
1909                                 strm->next_in += 7;
1910                                 strm->avail_in -= 7;
1911                                 br->cache_avail += 7 * 8;
1912                                 return (1);
1913                         }
1914                         break;
1915                 case 6:
1916                         if (strm->avail_in >= 6) {
1917                                 br->cache_buffer =
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];
1925                                 strm->next_in += 6;
1926                                 strm->avail_in -= 6;
1927                                 br->cache_avail += 6 * 8;
1928                                 return (1);
1929                         }
1930                         break;
1931                 case 0:
1932                         /* We have enough compressed data in
1933                          * the cache buffer.*/ 
1934                         return (1);
1935                 default:
1936                         break;
1937                 }
1938                 if (strm->avail_in == 0) {
1939                         /* There is not enough compressed data to fill up the
1940                          * cache buffer. */
1941                         return (0);
1942                 }
1943                 br->cache_buffer =
1944                    (br->cache_buffer << 8) | *strm->next_in++;
1945                 strm->avail_in--;
1946                 br->cache_avail += 8;
1947                 n -= 8;
1948         }
1949 }
1950
1951 /*
1952  * Decode LZHUF.
1953  *
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.
1970  */
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
1986
1987 static int
1988 lzh_decode(struct lzh_stream *strm, int last)
1989 {
1990         struct lzh_dec *ds = strm->ds;
1991         int64_t avail_in;
1992         int r;
1993
1994         if (ds->error)
1995                 return (ds->error);
1996
1997         avail_in = strm->avail_in;
1998         do {
1999                 if (ds->state < ST_GET_LITERAL)
2000                         r = lzh_read_blocks(strm, last);
2001                 else
2002                         r = lzh_decode_blocks(strm, last);
2003         } while (r == 100);
2004         strm->total_in += avail_in - strm->avail_in;
2005         return (r);
2006 }
2007
2008 static int
2009 lzh_copy_from_window(struct lzh_stream *strm, struct lzh_dec *ds)
2010 {
2011         size_t copy_bytes;
2012
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;
2016                 else
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;
2021         } else {
2022                 if (ds->w_remaining <= strm->avail_out)
2023                         copy_bytes = ds->w_remaining;
2024                 else
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;
2029         }
2030         strm->next_out += copy_bytes;
2031         strm->avail_out -= copy_bytes;
2032         strm->total_out += copy_bytes;
2033         if (strm->avail_out == 0)
2034                 return (0);
2035         else
2036                 return (1);
2037 }
2038
2039 static int
2040 lzh_read_blocks(struct lzh_stream *strm, int last)
2041 {
2042         struct lzh_dec *ds = strm->ds;
2043         struct lzh_br *br = &(ds->br);
2044         int c = 0, i;
2045         unsigned rbits;
2046
2047         for (;;) {
2048                 switch (ds->state) {
2049                 case ST_RD_BLOCK:
2050                         /*
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.
2056                          */
2057                         if (!lzh_br_read_ahead_0(strm, br, 16)) {
2058                                 if (!last)
2059                                         /* We need following data. */
2060                                         return (ARCHIVE_OK);
2061                                 if (lzh_br_has(br, 8)) {
2062                                         /*
2063                                          * It seems there are extra bits.
2064                                          *  1. Compressed data is broken.
2065                                          *  2. `last' flag does not properly
2066                                          *     set.
2067                                          */
2068                                         goto failed;
2069                                 }
2070                                 if (ds->w_pos > 0) {
2071                                         if (!lzh_copy_from_window(strm, ds))
2072                                                 return (ARCHIVE_OK);
2073                                 }
2074                                 /* End of compressed data; we have completely
2075                                  * handled all compressed data. */
2076                                 return (ARCHIVE_EOF);
2077                         }
2078                         ds->blocks_avail = lzh_br_bits(br, 16);
2079                         if (ds->blocks_avail == 0)
2080                                 goto failed;
2081                         lzh_br_consume(br, 16);
2082                         /*
2083                          * Read a literal table compressed in huffman
2084                          * coding.
2085                          */
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;
2089                         /* FALL THROUGH */
2090                 case ST_RD_PT_1:
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)) {
2095                                 if (last)
2096                                         goto failed;/* Truncated data. */
2097                                 ds->state = ST_RD_PT_1;
2098                                 return (ARCHIVE_OK);
2099                         }
2100                         ds->pt.len_avail = lzh_br_bits(br, ds->pt.len_bits);
2101                         lzh_br_consume(br, ds->pt.len_bits);
2102                         /* FALL THROUGH */
2103                 case ST_RD_PT_2:
2104                         if (ds->pt.len_avail == 0) {
2105                                 /* There is no bitlen. */
2106                                 if (!lzh_br_read_ahead(strm, br,
2107                                     ds->pt.len_bits)) {
2108                                         if (last)
2109                                                 goto failed;/* Truncated data.*/
2110                                         ds->state = ST_RD_PT_2;
2111                                         return (ARCHIVE_OK);
2112                                 }
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;
2119                                 else
2120                                         ds->state = ST_RD_LITERAL_1;
2121                                 break;
2122                         } else if (ds->pt.len_avail > ds->pt.len_size)
2123                                 goto failed;/* Invalid data. */
2124                         ds->loop = 0;
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;
2129                                 break;
2130                         }
2131                         /* FALL THROUGH */
2132                 case ST_RD_PT_3:
2133                         ds->loop = lzh_read_pt_bitlen(strm, ds->loop, 3);
2134                         if (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);
2140                         }
2141                         /* There are some null in bitlen of the literal. */
2142                         if (!lzh_br_read_ahead(strm, br, 2)) {
2143                                 if (last)
2144                                         goto failed;/* Truncated data. */
2145                                 ds->state = ST_RD_PT_3;
2146                                 return (ARCHIVE_OK);
2147                         }
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;
2154                         ds->loop = i;
2155                         /* FALL THROUGH */
2156                 case ST_RD_PT_4:
2157                         ds->loop = lzh_read_pt_bitlen(strm, ds->loop,
2158                             ds->pt.len_avail);
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);
2165                         }
2166                         if (!lzh_make_huffman_table(&(ds->pt)))
2167                                 goto failed;/* Invalid data */
2168                         if (ds->reading_position) {
2169                                 ds->state = ST_GET_LITERAL;
2170                                 break;
2171                         }
2172                         /* FALL THROUGH */
2173                 case ST_RD_LITERAL_1:
2174                         if (!lzh_br_read_ahead(strm, br, ds->lt.len_bits)) {
2175                                 if (last)
2176                                         goto failed;/* Truncated data. */
2177                                 ds->state = ST_RD_LITERAL_1;
2178                                 return (ARCHIVE_OK);
2179                         }
2180                         ds->lt.len_avail = lzh_br_bits(br, ds->lt.len_bits);
2181                         lzh_br_consume(br, ds->lt.len_bits);
2182                         /* FALL THROUGH */
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,
2187                                     ds->lt.len_bits)) {
2188                                         if (last)
2189                                                 goto failed;/* Truncated data.*/
2190                                         ds->state = ST_RD_LITERAL_2;
2191                                         return (ARCHIVE_OK);
2192                                 }
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;
2198                                 break;
2199                         } else if (ds->lt.len_avail > ds->lt.len_size)
2200                                 goto failed;/* Invalid data */
2201                         ds->loop = 0;
2202                         memset(ds->lt.freq, 0, sizeof(ds->lt.freq));
2203                         /* FALL THROUGH */
2204                 case ST_RD_LITERAL_3:
2205                         i = ds->loop;
2206                         while (i < ds->lt.len_avail) {
2207                                 if (!lzh_br_read_ahead(strm, br,
2208                                     ds->pt.max_bits)) {
2209                                         if (last)
2210                                                 goto failed;/* Truncated data.*/
2211                                         ds->loop = i;
2212                                         ds->state = ST_RD_LITERAL_3;
2213                                         return (ARCHIVE_OK);
2214                                 }
2215                                 rbits = lzh_br_bits(br, ds->pt.max_bits);
2216                                 c = lzh_decode_huffman(&(ds->pt), rbits);
2217                                 if (c > 2) {
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]);
2224                                         c -= 2;
2225                                         ds->lt.freq[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;
2230                                 } else {
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. */
2236                                                         goto failed;
2237                                                 ds->loop = i;
2238                                                 ds->state = ST_RD_LITERAL_3;
2239                                                 return (ARCHIVE_OK);
2240                                         }
2241                                         lzh_br_consume(br, ds->pt.bitlen[c]);
2242                                         c = lzh_br_bits(br, n);
2243                                         lzh_br_consume(br, n);
2244                                         c += (n == 4)?3:20;
2245                                         if (i + c > ds->lt.len_avail)
2246                                                 goto failed;/* Invalid data */
2247                                         memset(&(ds->lt.bitlen[i]), 0, c);
2248                                         i += c;
2249                                 }
2250                         }
2251                         if (i > ds->lt.len_avail ||
2252                             !lzh_make_huffman_table(&(ds->lt)))
2253                                 goto failed;/* Invalid data */
2254                         /* FALL THROUGH */
2255                 case ST_RD_POS_DATA_1:
2256                         /*
2257                          * Read a position table compressed in huffman
2258                          * coding.
2259                          */
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;
2264                         break;
2265                 case ST_GET_LITERAL:
2266                         return (100);
2267                 }
2268         }
2269 failed:
2270         return (ds->error = ARCHIVE_FAILED);
2271 }
2272
2273 static int
2274 lzh_decode_blocks(struct lzh_stream *strm, int last)
2275 {
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;
2288
2289         if (ds->w_remaining > 0) {
2290                 if (!lzh_copy_from_window(strm, ds))
2291                         goto next_data;
2292         }
2293         for (;;) {
2294                 switch (state) {
2295                 case ST_GET_LITERAL:
2296                         for (;;) {
2297                                 if (blocks_avail == 0) {
2298                                         /* We have decoded all blocks.
2299                                          * Let's handle next blocks. */
2300                                         ds->state = ST_RD_BLOCK;
2301                                         ds->br = bre;
2302                                         ds->blocks_avail = 0;
2303                                         ds->w_pos = w_pos;
2304                                         ds->copy_pos = 0;
2305                                         return (100);
2306                                 }
2307
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()
2314                                  * failed. */
2315                                 if (!lzh_br_read_ahead(strm, &bre,
2316                                     lt_max_bits)) {
2317                                         if (!last)
2318                                                 goto next_data;
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
2323                                          * dummy bits. */
2324                                         c = lzh_decode_huffman(lt,
2325                                               lzh_br_bits_forced(&bre,
2326                                                 lt_max_bits));
2327                                         lzh_br_consume(&bre, lt_bitlen[c]);
2328                                         if (!lzh_br_has(&bre, 0))
2329                                                 goto failed;/* Over read. */
2330                                 } else {
2331                                         c = lzh_decode_huffman(lt,
2332                                               lzh_br_bits(&bre, lt_max_bits));
2333                                         lzh_br_consume(&bre, lt_bitlen[c]);
2334                                 }
2335                                 blocks_avail--;
2336                                 if (c > UCHAR_MAX)
2337                                         /* Current block is a match data. */
2338                                         break;
2339                                 /*
2340                                  * 'c' is exactly a literal code.
2341                                  */
2342                                 /* Save a decoded code to reference it
2343                                  * afterward. */
2344                                 w_buff[w_pos] = c;
2345                                 if (++w_pos >= w_size) {
2346                                         w_pos = 0;
2347                                         ds->w_remaining = w_size;
2348                                         if (!lzh_copy_from_window(strm, ds))
2349                                                 goto next_data;
2350                                 }
2351                         }
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;
2356                         /* FALL THROUGH */
2357                 case ST_GET_POS_1:
2358                         /*
2359                          * Get a reference position. 
2360                          */
2361                         if (!lzh_br_read_ahead(strm, &bre, pt_max_bits)) {
2362                                 if (!last) {
2363                                         state = ST_GET_POS_1;
2364                                         ds->copy_len = copy_len;
2365                                         goto next_data;
2366                                 }
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. */
2372                         } else {
2373                                 copy_pos = lzh_decode_huffman(pt,
2374                                     lzh_br_bits(&bre, pt_max_bits));
2375                                 lzh_br_consume(&bre, pt_bitlen[copy_pos]);
2376                         }
2377                         /* FALL THROUGH */
2378                 case ST_GET_POS_2:
2379                         if (copy_pos > 1) {
2380                                 /* We need an additional adjustment number to
2381                                  * the position. */
2382                                 int p = copy_pos - 1;
2383                                 if (!lzh_br_read_ahead(strm, &bre, p)) {
2384                                         if (last)
2385                                                 goto failed;/* Truncated data.*/
2386                                         state = ST_GET_POS_2;
2387                                         ds->copy_len = copy_len;
2388                                         ds->copy_pos = copy_pos;
2389                                         goto next_data;
2390                                 }
2391                                 copy_pos = (1 << p) + lzh_br_bits(&bre, p);
2392                                 lzh_br_consume(&bre, p);
2393                         }
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;
2398                         /* FALL THROUGH */
2399                 case ST_COPY_DATA:
2400                         /*
2401                          * Copy `copy_len' bytes as extracted data from
2402                          * the window into the output buffer.
2403                          */
2404                         for (;;) {
2405                                 int l;
2406
2407                                 l = copy_len;
2408                                 if (copy_pos > w_pos) {
2409                                         if (l > w_size - copy_pos)
2410                                                 l = w_size - copy_pos;
2411                                 } else {
2412                                         if (l > w_size - w_pos)
2413                                                 l = w_size - w_pos;
2414                                 }
2415                                 if ((copy_pos + l < w_pos)
2416                                     || (w_pos + l < copy_pos)) {
2417                                         /* No overlap. */
2418                                         memcpy(w_buff + w_pos,
2419                                             w_buff + copy_pos, l);
2420                                 } else {
2421                                         const unsigned char *s;
2422                                         unsigned char *d;
2423                                         int li;
2424
2425                                         d = w_buff + w_pos;
2426                                         s = w_buff + copy_pos;
2427                                         for (li = 0; li < l; li++)
2428                                                 d[li] = s[li];
2429                                 }
2430                                 w_pos = (w_pos + l) & w_mask;
2431                                 if (w_pos == 0) {
2432                                         ds->w_remaining = w_size;
2433                                         if (!lzh_copy_from_window(strm, ds)) {
2434                                                 if (copy_len <= l)
2435                                                         state = ST_GET_LITERAL;
2436                                                 else {
2437                                                         state = ST_COPY_DATA;
2438                                                         ds->copy_len =
2439                                                             copy_len - l;
2440                                                         ds->copy_pos =
2441                                                             (copy_pos + l)
2442                                                             & w_mask;
2443                                                 }
2444                                                 goto next_data;
2445                                         }
2446                                 }
2447                                 if (copy_len <= l)
2448                                         /* A copy of current pattern ended. */
2449                                         break;
2450                                 copy_len -= l;
2451                                 copy_pos = (copy_pos + l) & w_mask;
2452                         }
2453                         state = ST_GET_LITERAL;
2454                         break;
2455                 }
2456         }
2457 failed:
2458         return (ds->error = ARCHIVE_FAILED);
2459 next_data:
2460         ds->br = bre;
2461         ds->blocks_avail = blocks_avail;
2462         ds->state = state;
2463         ds->w_pos = w_pos;
2464         return (ARCHIVE_OK);
2465 }
2466
2467 static int
2468 lzh_huffman_init(struct huffman *hf, size_t len_size, int tbl_bits)
2469 {
2470         int bits;
2471
2472         if (hf->bitlen == NULL) {
2473                 hf->bitlen = malloc(len_size * sizeof(hf->bitlen[0]));
2474                 if (hf->bitlen == NULL)
2475                         return (ARCHIVE_FATAL);
2476         }
2477         if (hf->tbl == NULL) {
2478                 if (tbl_bits < HTBL_BITS)
2479                         bits = tbl_bits;
2480                 else
2481                         bits = HTBL_BITS;
2482                 hf->tbl = malloc((1 << bits) * sizeof(hf->tbl[0]));
2483                 if (hf->tbl == NULL)
2484                         return (ARCHIVE_FATAL);
2485         }
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);
2491         }
2492         hf->len_size = len_size;
2493         hf->tbl_bits = tbl_bits;
2494         return (ARCHIVE_OK);
2495 }
2496
2497 static void
2498 lzh_huffman_free(struct huffman *hf)
2499 {
2500         free(hf->bitlen);
2501         free(hf->tbl);
2502         free(hf->tree);
2503 }
2504
2505 static int
2506 lzh_read_pt_bitlen(struct lzh_stream *strm, int start, int end)
2507 {
2508         struct lzh_dec *ds = strm->ds;
2509         struct lzh_br * br = &(ds->br);
2510         int c, i;
2511
2512         for (i = start; i < end;) {
2513                 /*
2514                  *  bit pattern     the number we need
2515                  *     000           ->  0
2516                  *     001           ->  1
2517                  *     010           ->  2
2518                  *     ...
2519                  *     110           ->  6
2520                  *     1110          ->  7
2521                  *     11110         ->  8
2522                  *     ...
2523                  *     1111111111110 ->  16
2524                  */
2525                 if (!lzh_br_read_ahead(strm, br, 3))
2526                         return (i);
2527                 if ((c = lzh_br_bits(br, 3)) == 7) {
2528                         int d;
2529                         if (!lzh_br_read_ahead(strm, br, 13))
2530                                 return (i);
2531                         d = lzh_br_bits(br, 13);
2532                         while (d & 0x200) {
2533                                 c++;
2534                                 d <<= 1;
2535                         }
2536                         if (c > 16)
2537                                 return (-1);/* Invalid data. */
2538                         lzh_br_consume(br, c - 3);
2539                 } else
2540                         lzh_br_consume(br, 3);
2541                 ds->pt.bitlen[i++] = c;
2542                 ds->pt.freq[c]++;
2543         }
2544         return (i);
2545 }
2546
2547 static int
2548 lzh_make_fake_table(struct huffman *hf, uint16_t c)
2549 {
2550         if (c >= hf->len_size)
2551                 return (0);
2552         hf->tbl[0] = c;
2553         hf->max_bits = 0;
2554         hf->shift_bits = 0;
2555         hf->bitlen[hf->tbl[0]] = 0;
2556         return (1);
2557 }
2558
2559 /*
2560  * Make a huffman coding table.
2561  */
2562 static int
2563 lzh_make_huffman_table(struct huffman *hf)
2564 {
2565         uint16_t *tbl;
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;
2570
2571         /*
2572          * Initialize bit patterns.
2573          */
2574         ptn = 0;
2575         for (i = 1, w = 1 << 15; i <= 16; i++, w >>= 1) {
2576                 bitptn[i] = ptn;
2577                 weight[i] = w;
2578                 if (hf->freq[i]) {
2579                         ptn += hf->freq[i] * w;
2580                         maxbits = i;
2581                 }
2582         }
2583         if (ptn != 0x10000 || maxbits > hf->tbl_bits)
2584                 return (0);/* Invalid */
2585
2586         hf->max_bits = maxbits;
2587
2588         /*
2589          * Cut out extra bits which we won't house in the table.
2590          * This preparation reduces the same calculation in the for-loop
2591          * making the table.
2592          */
2593         if (maxbits < 16) {
2594                 int ebits = 16 - maxbits;
2595                 for (i = 1; i <= maxbits; i++) {
2596                         bitptn[i] >>= ebits;
2597                         weight[i] >>= ebits;
2598                 }
2599         }
2600         if (maxbits > HTBL_BITS) {
2601                 int htbl_max;
2602                 uint16_t *p;
2603
2604                 diffbits = maxbits - HTBL_BITS;
2605                 for (i = 1; i <= HTBL_BITS; i++) {
2606                         bitptn[i] >>= diffbits;
2607                         weight[i] >>= diffbits;
2608                 }
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])
2613                         *p++ = 0;
2614         } else
2615                 diffbits = 0;
2616         hf->shift_bits = diffbits;
2617
2618         /*
2619          * Make the table.
2620          */
2621         tbl_size = 1 << HTBL_BITS;
2622         tbl = hf->tbl;
2623         bitlen = hf->bitlen;
2624         len_avail = hf->len_avail;
2625         hf->tree_used = 0;
2626         for (i = 0; i < len_avail; i++) {
2627                 uint16_t *p;
2628                 int len, cnt;
2629                 uint16_t bit;
2630                 int extlen;
2631                 struct htree_t *ht;
2632
2633                 if (bitlen[i] == 0)
2634                         continue;
2635                 /* Get a bit pattern */
2636                 len = bitlen[i];
2637                 ptn = bitptn[len];
2638                 cnt = weight[len];
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 */
2644                         p = &(tbl[ptn]);
2645                         while (--cnt >= 0)
2646                                 p[cnt] = (uint16_t)i;
2647                         continue;
2648                 }
2649
2650                 /*
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.
2653                  */
2654                 bitptn[len] = ptn + cnt;
2655                 bit = 1U << (diffbits -1);
2656                 extlen = len - HTBL_BITS;
2657                 
2658                 p = &(tbl[ptn >> diffbits]);
2659                 if (*p == 0) {
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 */
2664                         ht->left = 0;
2665                         ht->right = 0;
2666                 } else {
2667                         if (*p < len_avail ||
2668                             *p >= (len_avail + hf->tree_used))
2669                                 return (0);/* Invalid */
2670                         ht = &(hf->tree[*p - len_avail]);
2671                 }
2672                 while (--extlen > 0) {
2673                         if (ptn & bit) {
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 */
2679                                         ht->left = 0;
2680                                         ht->right = 0;
2681                                 } else {
2682                                         ht = &(hf->tree[ht->left - len_avail]);
2683                                 }
2684                         } else {
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 */
2690                                         ht->left = 0;
2691                                         ht->right = 0;
2692                                 } else {
2693                                         ht = &(hf->tree[ht->right - len_avail]);
2694                                 }
2695                         }
2696                         bit >>= 1;
2697                 }
2698                 if (ptn & bit) {
2699                         if (ht->left != 0)
2700                                 return (0);/* Invalid */
2701                         ht->left = (uint16_t)i;
2702                 } else {
2703                         if (ht->right != 0)
2704                                 return (0);/* Invalid */
2705                         ht->right = (uint16_t)i;
2706                 }
2707         }
2708         return (1);
2709 }
2710
2711 static int
2712 lzh_decode_huffman_tree(struct huffman *hf, unsigned rbits, int c)
2713 {
2714         struct htree_t *ht;
2715         int extlen;
2716
2717         ht = hf->tree;
2718         extlen = hf->shift_bits;
2719         while (c >= hf->len_avail) {
2720                 c -= hf->len_avail;
2721                 if (extlen-- <= 0 || c >= hf->tree_used)
2722                         return (0);
2723                 if (rbits & (1U << extlen))
2724                         c = ht[c].left;
2725                 else
2726                         c = ht[c].right;
2727         }
2728         return (c);
2729 }
2730
2731 static inline int
2732 lzh_decode_huffman(struct huffman *hf, unsigned rbits)
2733 {
2734         int c;
2735         /*
2736          * At first search an index table for a bit pattern.
2737          * If it fails, search a huffman tree for.
2738          */
2739         c = hf->tbl[rbits >> hf->shift_bits];
2740         if (c < hf->len_avail)
2741                 return (c);
2742         /* This bit pattern needs to be found out at a huffman tree. */
2743         return (lzh_decode_huffman_tree(hf, rbits, c));
2744 }
2745