Import libarchive-3.2.0.
[dragonfly.git] / contrib / libarchive / libarchive / archive_read_support_format_rar.c
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011 Andres Mejia
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "archive_platform.h"
28
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #include <time.h>
33 #include <limits.h>
34 #ifdef HAVE_ZLIB_H
35 #include <zlib.h> /* crc32 */
36 #endif
37
38 #include "archive.h"
39 #ifndef HAVE_ZLIB_H
40 #include "archive_crc32.h"
41 #endif
42 #include "archive_endian.h"
43 #include "archive_entry.h"
44 #include "archive_entry_locale.h"
45 #include "archive_ppmd7_private.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
48
49 /* RAR signature, also known as the mark header */
50 #define RAR_SIGNATURE "\x52\x61\x72\x21\x1A\x07\x00"
51
52 /* Header types */
53 #define MARK_HEAD    0x72
54 #define MAIN_HEAD    0x73
55 #define FILE_HEAD    0x74
56 #define COMM_HEAD    0x75
57 #define AV_HEAD      0x76
58 #define SUB_HEAD     0x77
59 #define PROTECT_HEAD 0x78
60 #define SIGN_HEAD    0x79
61 #define NEWSUB_HEAD  0x7a
62 #define ENDARC_HEAD  0x7b
63
64 /* Main Header Flags */
65 #define MHD_VOLUME       0x0001
66 #define MHD_COMMENT      0x0002
67 #define MHD_LOCK         0x0004
68 #define MHD_SOLID        0x0008
69 #define MHD_NEWNUMBERING 0x0010
70 #define MHD_AV           0x0020
71 #define MHD_PROTECT      0x0040
72 #define MHD_PASSWORD     0x0080
73 #define MHD_FIRSTVOLUME  0x0100
74 #define MHD_ENCRYPTVER   0x0200
75
76 /* Flags common to all headers */
77 #define HD_MARKDELETION     0x4000
78 #define HD_ADD_SIZE_PRESENT 0x8000
79
80 /* File Header Flags */
81 #define FHD_SPLIT_BEFORE 0x0001
82 #define FHD_SPLIT_AFTER  0x0002
83 #define FHD_PASSWORD     0x0004
84 #define FHD_COMMENT      0x0008
85 #define FHD_SOLID        0x0010
86 #define FHD_LARGE        0x0100
87 #define FHD_UNICODE      0x0200
88 #define FHD_SALT         0x0400
89 #define FHD_VERSION      0x0800
90 #define FHD_EXTTIME      0x1000
91 #define FHD_EXTFLAGS     0x2000
92
93 /* File dictionary sizes */
94 #define DICTIONARY_SIZE_64   0x00
95 #define DICTIONARY_SIZE_128  0x20
96 #define DICTIONARY_SIZE_256  0x40
97 #define DICTIONARY_SIZE_512  0x60
98 #define DICTIONARY_SIZE_1024 0x80
99 #define DICTIONARY_SIZE_2048 0xA0
100 #define DICTIONARY_SIZE_4096 0xC0
101 #define FILE_IS_DIRECTORY    0xE0
102 #define DICTIONARY_MASK      FILE_IS_DIRECTORY
103
104 /* OS Flags */
105 #define OS_MSDOS  0
106 #define OS_OS2    1
107 #define OS_WIN32  2
108 #define OS_UNIX   3
109 #define OS_MAC_OS 4
110 #define OS_BEOS   5
111
112 /* Compression Methods */
113 #define COMPRESS_METHOD_STORE   0x30
114 /* LZSS */
115 #define COMPRESS_METHOD_FASTEST 0x31
116 #define COMPRESS_METHOD_FAST    0x32
117 #define COMPRESS_METHOD_NORMAL  0x33
118 /* PPMd Variant H */
119 #define COMPRESS_METHOD_GOOD    0x34
120 #define COMPRESS_METHOD_BEST    0x35
121
122 #define CRC_POLYNOMIAL 0xEDB88320
123
124 #define NS_UNIT 10000000
125
126 #define DICTIONARY_MAX_SIZE 0x400000
127
128 #define MAINCODE_SIZE      299
129 #define OFFSETCODE_SIZE    60
130 #define LOWOFFSETCODE_SIZE 17
131 #define LENGTHCODE_SIZE    28
132 #define HUFFMAN_TABLE_SIZE \
133   MAINCODE_SIZE + OFFSETCODE_SIZE + LOWOFFSETCODE_SIZE + LENGTHCODE_SIZE
134
135 #define MAX_SYMBOL_LENGTH 0xF
136 #define MAX_SYMBOLS       20
137
138 /*
139  * Considering L1,L2 cache miss and a calling of write system-call,
140  * the best size of the output buffer(uncompressed buffer) is 128K.
141  * If the structure of extracting process is changed, this value
142  * might be researched again.
143  */
144 #define UNP_BUFFER_SIZE   (128 * 1024)
145
146 /* Define this here for non-Windows platforms */
147 #if !((defined(__WIN32__) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__))
148 #define FILE_ATTRIBUTE_DIRECTORY 0x10
149 #endif
150
151 /* Fields common to all headers */
152 struct rar_header
153 {
154   char crc[2];
155   char type;
156   char flags[2];
157   char size[2];
158 };
159
160 /* Fields common to all file headers */
161 struct rar_file_header
162 {
163   char pack_size[4];
164   char unp_size[4];
165   char host_os;
166   char file_crc[4];
167   char file_time[4];
168   char unp_ver;
169   char method;
170   char name_size[2];
171   char file_attr[4];
172 };
173
174 struct huffman_tree_node
175 {
176   int branches[2];
177 };
178
179 struct huffman_table_entry
180 {
181   unsigned int length;
182   int value;
183 };
184
185 struct huffman_code
186 {
187   struct huffman_tree_node *tree;
188   int numentries;
189   int numallocatedentries;
190   int minlength;
191   int maxlength;
192   int tablesize;
193   struct huffman_table_entry *table;
194 };
195
196 struct lzss
197 {
198   unsigned char *window;
199   int mask;
200   int64_t position;
201 };
202
203 struct data_block_offsets
204 {
205   int64_t header_size;
206   int64_t start_offset;
207   int64_t end_offset;
208 };
209
210 struct rar
211 {
212   /* Entries from main RAR header */
213   unsigned main_flags;
214   unsigned long file_crc;
215   char reserved1[2];
216   char reserved2[4];
217   char encryptver;
218
219   /* File header entries */
220   char compression_method;
221   unsigned file_flags;
222   int64_t packed_size;
223   int64_t unp_size;
224   time_t mtime;
225   long mnsec;
226   mode_t mode;
227   char *filename;
228   char *filename_save;
229   size_t filename_save_size;
230   size_t filename_allocated;
231
232   /* File header optional entries */
233   char salt[8];
234   time_t atime;
235   long ansec;
236   time_t ctime;
237   long cnsec;
238   time_t arctime;
239   long arcnsec;
240
241   /* Fields to help with tracking decompression of files. */
242   int64_t bytes_unconsumed;
243   int64_t bytes_remaining;
244   int64_t bytes_uncopied;
245   int64_t offset;
246   int64_t offset_outgoing;
247   int64_t offset_seek;
248   char valid;
249   unsigned int unp_offset;
250   unsigned int unp_buffer_size;
251   unsigned char *unp_buffer;
252   unsigned int dictionary_size;
253   char start_new_block;
254   char entry_eof;
255   unsigned long crc_calculated;
256   int found_first_header;
257   char has_endarc_header;
258   struct data_block_offsets *dbo;
259   unsigned int cursor;
260   unsigned int nodes;
261
262   /* LZSS members */
263   struct huffman_code maincode;
264   struct huffman_code offsetcode;
265   struct huffman_code lowoffsetcode;
266   struct huffman_code lengthcode;
267   unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
268   struct lzss lzss;
269   char output_last_match;
270   unsigned int lastlength;
271   unsigned int lastoffset;
272   unsigned int oldoffset[4];
273   unsigned int lastlowoffset;
274   unsigned int numlowoffsetrepeats;
275   int64_t filterstart;
276   char start_new_table;
277
278   /* PPMd Variant H members */
279   char ppmd_valid;
280   char ppmd_eod;
281   char is_ppmd_block;
282   int ppmd_escape;
283   CPpmd7 ppmd7_context;
284   CPpmd7z_RangeDec range_dec;
285   IByteIn bytein;
286
287   /*
288    * String conversion object.
289    */
290   int init_default_conversion;
291   struct archive_string_conv *sconv_default;
292   struct archive_string_conv *opt_sconv;
293   struct archive_string_conv *sconv_utf8;
294   struct archive_string_conv *sconv_utf16be;
295
296   /*
297    * Bit stream reader.
298    */
299   struct rar_br {
300 #define CACHE_TYPE      uint64_t
301 #define CACHE_BITS      (8 * sizeof(CACHE_TYPE))
302     /* Cache buffer. */
303     CACHE_TYPE           cache_buffer;
304     /* Indicates how many bits avail in cache_buffer. */
305     int                  cache_avail;
306     ssize_t              avail_in;
307     const unsigned char *next_in;
308   } br;
309
310   /*
311    * Custom field to denote that this archive contains encrypted entries
312    */
313   int has_encrypted_entries;
314 };
315
316 static int archive_read_support_format_rar_capabilities(struct archive_read *);
317 static int archive_read_format_rar_has_encrypted_entries(struct archive_read *);
318 static int archive_read_format_rar_bid(struct archive_read *, int);
319 static int archive_read_format_rar_options(struct archive_read *,
320     const char *, const char *);
321 static int archive_read_format_rar_read_header(struct archive_read *,
322     struct archive_entry *);
323 static int archive_read_format_rar_read_data(struct archive_read *,
324     const void **, size_t *, int64_t *);
325 static int archive_read_format_rar_read_data_skip(struct archive_read *a);
326 static int64_t archive_read_format_rar_seek_data(struct archive_read *, int64_t,
327     int);
328 static int archive_read_format_rar_cleanup(struct archive_read *);
329
330 /* Support functions */
331 static int read_header(struct archive_read *, struct archive_entry *, char);
332 static time_t get_time(int);
333 static int read_exttime(const char *, struct rar *, const char *);
334 static int read_symlink_stored(struct archive_read *, struct archive_entry *,
335                                struct archive_string_conv *);
336 static int read_data_stored(struct archive_read *, const void **, size_t *,
337                             int64_t *);
338 static int read_data_compressed(struct archive_read *, const void **, size_t *,
339                           int64_t *);
340 static int rar_br_preparation(struct archive_read *, struct rar_br *);
341 static int parse_codes(struct archive_read *);
342 static void free_codes(struct archive_read *);
343 static int read_next_symbol(struct archive_read *, struct huffman_code *);
344 static int create_code(struct archive_read *, struct huffman_code *,
345                         unsigned char *, int, char);
346 static int add_value(struct archive_read *, struct huffman_code *, int, int,
347                      int);
348 static int new_node(struct huffman_code *);
349 static int make_table(struct archive_read *, struct huffman_code *);
350 static int make_table_recurse(struct archive_read *, struct huffman_code *, int,
351                               struct huffman_table_entry *, int, int);
352 static int64_t expand(struct archive_read *, int64_t);
353 static int copy_from_lzss_window(struct archive_read *, const void **,
354                                    int64_t, int);
355 static const void *rar_read_ahead(struct archive_read *, size_t, ssize_t *);
356
357 /*
358  * Bit stream reader.
359  */
360 /* Check that the cache buffer has enough bits. */
361 #define rar_br_has(br, n) ((br)->cache_avail >= n)
362 /* Get compressed data by bit. */
363 #define rar_br_bits(br, n)        \
364   (((uint32_t)((br)->cache_buffer >>    \
365     ((br)->cache_avail - (n)))) & cache_masks[n])
366 #define rar_br_bits_forced(br, n)     \
367   (((uint32_t)((br)->cache_buffer <<    \
368     ((n) - (br)->cache_avail))) & cache_masks[n])
369 /* Read ahead to make sure the cache buffer has enough compressed data we
370  * will use.
371  *  True  : completed, there is enough data in the cache buffer.
372  *  False : there is no data in the stream. */
373 #define rar_br_read_ahead(a, br, n) \
374   ((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
375 /* Notify how many bits we consumed. */
376 #define rar_br_consume(br, n) ((br)->cache_avail -= (n))
377 #define rar_br_consume_unalined_bits(br) ((br)->cache_avail &= ~7)
378
379 static const uint32_t cache_masks[] = {
380   0x00000000, 0x00000001, 0x00000003, 0x00000007,
381   0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
382   0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
383   0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
384   0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
385   0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
386   0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
387   0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
388   0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
389 };
390
391 /*
392  * Shift away used bits in the cache data and fill it up with following bits.
393  * Call this when cache buffer does not have enough bits you need.
394  *
395  * Returns 1 if the cache buffer is full.
396  * Returns 0 if the cache buffer is not full; input buffer is empty.
397  */
398 static int
399 rar_br_fillup(struct archive_read *a, struct rar_br *br)
400 {
401   struct rar *rar = (struct rar *)(a->format->data);
402   int n = CACHE_BITS - br->cache_avail;
403
404   for (;;) {
405     switch (n >> 3) {
406     case 8:
407       if (br->avail_in >= 8) {
408         br->cache_buffer =
409             ((uint64_t)br->next_in[0]) << 56 |
410             ((uint64_t)br->next_in[1]) << 48 |
411             ((uint64_t)br->next_in[2]) << 40 |
412             ((uint64_t)br->next_in[3]) << 32 |
413             ((uint32_t)br->next_in[4]) << 24 |
414             ((uint32_t)br->next_in[5]) << 16 |
415             ((uint32_t)br->next_in[6]) << 8 |
416              (uint32_t)br->next_in[7];
417         br->next_in += 8;
418         br->avail_in -= 8;
419         br->cache_avail += 8 * 8;
420         rar->bytes_unconsumed += 8;
421         rar->bytes_remaining -= 8;
422         return (1);
423       }
424       break;
425     case 7:
426       if (br->avail_in >= 7) {
427         br->cache_buffer =
428            (br->cache_buffer << 56) |
429             ((uint64_t)br->next_in[0]) << 48 |
430             ((uint64_t)br->next_in[1]) << 40 |
431             ((uint64_t)br->next_in[2]) << 32 |
432             ((uint32_t)br->next_in[3]) << 24 |
433             ((uint32_t)br->next_in[4]) << 16 |
434             ((uint32_t)br->next_in[5]) << 8 |
435              (uint32_t)br->next_in[6];
436         br->next_in += 7;
437         br->avail_in -= 7;
438         br->cache_avail += 7 * 8;
439         rar->bytes_unconsumed += 7;
440         rar->bytes_remaining -= 7;
441         return (1);
442       }
443       break;
444     case 6:
445       if (br->avail_in >= 6) {
446         br->cache_buffer =
447            (br->cache_buffer << 48) |
448             ((uint64_t)br->next_in[0]) << 40 |
449             ((uint64_t)br->next_in[1]) << 32 |
450             ((uint32_t)br->next_in[2]) << 24 |
451             ((uint32_t)br->next_in[3]) << 16 |
452             ((uint32_t)br->next_in[4]) << 8 |
453              (uint32_t)br->next_in[5];
454         br->next_in += 6;
455         br->avail_in -= 6;
456         br->cache_avail += 6 * 8;
457         rar->bytes_unconsumed += 6;
458         rar->bytes_remaining -= 6;
459         return (1);
460       }
461       break;
462     case 0:
463       /* We have enough compressed data in
464        * the cache buffer.*/
465       return (1);
466     default:
467       break;
468     }
469     if (br->avail_in <= 0) {
470
471       if (rar->bytes_unconsumed > 0) {
472         /* Consume as much as the decompressor
473          * actually used. */
474         __archive_read_consume(a, rar->bytes_unconsumed);
475         rar->bytes_unconsumed = 0;
476       }
477       br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
478       if (br->next_in == NULL)
479         return (0);
480       if (br->avail_in == 0)
481         return (0);
482     }
483     br->cache_buffer =
484        (br->cache_buffer << 8) | *br->next_in++;
485     br->avail_in--;
486     br->cache_avail += 8;
487     n -= 8;
488     rar->bytes_unconsumed++;
489     rar->bytes_remaining--;
490   }
491 }
492
493 static int
494 rar_br_preparation(struct archive_read *a, struct rar_br *br)
495 {
496   struct rar *rar = (struct rar *)(a->format->data);
497
498   if (rar->bytes_remaining > 0) {
499     br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
500     if (br->next_in == NULL) {
501       archive_set_error(&a->archive,
502           ARCHIVE_ERRNO_FILE_FORMAT,
503           "Truncated RAR file data");
504       return (ARCHIVE_FATAL);
505     }
506     if (br->cache_avail == 0)
507       (void)rar_br_fillup(a, br);
508   }
509   return (ARCHIVE_OK);
510 }
511
512 /* Find last bit set */
513 static inline int
514 rar_fls(unsigned int word)
515 {
516   word |= (word >>  1);
517   word |= (word >>  2);
518   word |= (word >>  4);
519   word |= (word >>  8);
520   word |= (word >> 16);
521   return word - (word >> 1);
522 }
523
524 /* LZSS functions */
525 static inline int64_t
526 lzss_position(struct lzss *lzss)
527 {
528   return lzss->position;
529 }
530
531 static inline int
532 lzss_mask(struct lzss *lzss)
533 {
534   return lzss->mask;
535 }
536
537 static inline int
538 lzss_size(struct lzss *lzss)
539 {
540   return lzss->mask + 1;
541 }
542
543 static inline int
544 lzss_offset_for_position(struct lzss *lzss, int64_t pos)
545 {
546   return (int)(pos & lzss->mask);
547 }
548
549 static inline unsigned char *
550 lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
551 {
552   return &lzss->window[lzss_offset_for_position(lzss, pos)];
553 }
554
555 static inline int
556 lzss_current_offset(struct lzss *lzss)
557 {
558   return lzss_offset_for_position(lzss, lzss->position);
559 }
560
561 static inline uint8_t *
562 lzss_current_pointer(struct lzss *lzss)
563 {
564   return lzss_pointer_for_position(lzss, lzss->position);
565 }
566
567 static inline void
568 lzss_emit_literal(struct rar *rar, uint8_t literal)
569 {
570   *lzss_current_pointer(&rar->lzss) = literal;
571   rar->lzss.position++;
572 }
573
574 static inline void
575 lzss_emit_match(struct rar *rar, int offset, int length)
576 {
577   int dstoffs = lzss_current_offset(&rar->lzss);
578   int srcoffs = (dstoffs - offset) & lzss_mask(&rar->lzss);
579   int l, li, remaining;
580   unsigned char *d, *s;
581
582   remaining = length;
583   while (remaining > 0) {
584     l = remaining;
585     if (dstoffs > srcoffs) {
586       if (l > lzss_size(&rar->lzss) - dstoffs)
587         l = lzss_size(&rar->lzss) - dstoffs;
588     } else {
589       if (l > lzss_size(&rar->lzss) - srcoffs)
590         l = lzss_size(&rar->lzss) - srcoffs;
591     }
592     d = &(rar->lzss.window[dstoffs]);
593     s = &(rar->lzss.window[srcoffs]);
594     if ((dstoffs + l < srcoffs) || (srcoffs + l < dstoffs))
595       memcpy(d, s, l);
596     else {
597       for (li = 0; li < l; li++)
598         d[li] = s[li];
599     }
600     remaining -= l;
601     dstoffs = (dstoffs + l) & lzss_mask(&(rar->lzss));
602     srcoffs = (srcoffs + l) & lzss_mask(&(rar->lzss));
603   }
604   rar->lzss.position += length;
605 }
606
607 static void *
608 ppmd_alloc(void *p, size_t size)
609 {
610   (void)p;
611   return malloc(size);
612 }
613 static void
614 ppmd_free(void *p, void *address)
615 {
616   (void)p;
617   free(address);
618 }
619 static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
620
621 static Byte
622 ppmd_read(void *p)
623 {
624   struct archive_read *a = ((IByteIn*)p)->a;
625   struct rar *rar = (struct rar *)(a->format->data);
626   struct rar_br *br = &(rar->br);
627   Byte b;
628   if (!rar_br_read_ahead(a, br, 8))
629   {
630     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
631                       "Truncated RAR file data");
632     rar->valid = 0;
633     return 0;
634   }
635   b = rar_br_bits(br, 8);
636   rar_br_consume(br, 8);
637   return b;
638 }
639
640 int
641 archive_read_support_format_rar(struct archive *_a)
642 {
643   struct archive_read *a = (struct archive_read *)_a;
644   struct rar *rar;
645   int r;
646
647   archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
648                       "archive_read_support_format_rar");
649
650   rar = (struct rar *)malloc(sizeof(*rar));
651   if (rar == NULL)
652   {
653     archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
654     return (ARCHIVE_FATAL);
655   }
656   memset(rar, 0, sizeof(*rar));
657
658         /*
659          * Until enough data has been read, we cannot tell about
660          * any encrypted entries yet.
661          */
662         rar->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
663
664   r = __archive_read_register_format(a,
665                                      rar,
666                                      "rar",
667                                      archive_read_format_rar_bid,
668                                      archive_read_format_rar_options,
669                                      archive_read_format_rar_read_header,
670                                      archive_read_format_rar_read_data,
671                                      archive_read_format_rar_read_data_skip,
672                                      archive_read_format_rar_seek_data,
673                                      archive_read_format_rar_cleanup,
674                                      archive_read_support_format_rar_capabilities,
675                                      archive_read_format_rar_has_encrypted_entries);
676
677   if (r != ARCHIVE_OK)
678     free(rar);
679   return (r);
680 }
681
682 static int
683 archive_read_support_format_rar_capabilities(struct archive_read * a)
684 {
685         (void)a; /* UNUSED */
686         return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
687                         | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
688 }
689
690 static int
691 archive_read_format_rar_has_encrypted_entries(struct archive_read *_a)
692 {
693         if (_a && _a->format) {
694                 struct rar * rar = (struct rar *)_a->format->data;
695                 if (rar) {
696                         return rar->has_encrypted_entries;
697                 }
698         }
699         return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
700 }
701
702
703 static int
704 archive_read_format_rar_bid(struct archive_read *a, int best_bid)
705 {
706   const char *p;
707
708   /* If there's already a bid > 30, we'll never win. */
709   if (best_bid > 30)
710           return (-1);
711
712   if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
713     return (-1);
714
715   if (memcmp(p, RAR_SIGNATURE, 7) == 0)
716     return (30);
717
718   if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
719     /* This is a PE file */
720     ssize_t offset = 0x10000;
721     ssize_t window = 4096;
722     ssize_t bytes_avail;
723     while (offset + window <= (1024 * 128)) {
724       const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
725       if (buff == NULL) {
726         /* Remaining bytes are less than window. */
727         window >>= 1;
728         if (window < 0x40)
729           return (0);
730         continue;
731       }
732       p = buff + offset;
733       while (p + 7 < buff + bytes_avail) {
734         if (memcmp(p, RAR_SIGNATURE, 7) == 0)
735           return (30);
736         p += 0x10;
737       }
738       offset = p - buff;
739     }
740   }
741   return (0);
742 }
743
744 static int
745 skip_sfx(struct archive_read *a)
746 {
747   const void *h;
748   const char *p, *q;
749   size_t skip, total;
750   ssize_t bytes, window;
751
752   total = 0;
753   window = 4096;
754   while (total + window <= (1024 * 128)) {
755     h = __archive_read_ahead(a, window, &bytes);
756     if (h == NULL) {
757       /* Remaining bytes are less than window. */
758       window >>= 1;
759       if (window < 0x40)
760         goto fatal;
761       continue;
762     }
763     if (bytes < 0x40)
764       goto fatal;
765     p = h;
766     q = p + bytes;
767
768     /*
769      * Scan ahead until we find something that looks
770      * like the RAR header.
771      */
772     while (p + 7 < q) {
773       if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
774         skip = p - (const char *)h;
775         __archive_read_consume(a, skip);
776         return (ARCHIVE_OK);
777       }
778       p += 0x10;
779     }
780     skip = p - (const char *)h;
781     __archive_read_consume(a, skip);
782         total += skip;
783   }
784 fatal:
785   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
786       "Couldn't find out RAR header");
787   return (ARCHIVE_FATAL);
788 }
789
790 static int
791 archive_read_format_rar_options(struct archive_read *a,
792     const char *key, const char *val)
793 {
794   struct rar *rar;
795   int ret = ARCHIVE_FAILED;
796
797   rar = (struct rar *)(a->format->data);
798   if (strcmp(key, "hdrcharset")  == 0) {
799     if (val == NULL || val[0] == 0)
800       archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
801           "rar: hdrcharset option needs a character-set name");
802     else {
803       rar->opt_sconv =
804           archive_string_conversion_from_charset(
805               &a->archive, val, 0);
806       if (rar->opt_sconv != NULL)
807         ret = ARCHIVE_OK;
808       else
809         ret = ARCHIVE_FATAL;
810     }
811     return (ret);
812   }
813
814   /* Note: The "warn" return is just to inform the options
815    * supervisor that we didn't handle it.  It will generate
816    * a suitable error if no one used this option. */
817   return (ARCHIVE_WARN);
818 }
819
820 static int
821 archive_read_format_rar_read_header(struct archive_read *a,
822                                     struct archive_entry *entry)
823 {
824   const void *h;
825   const char *p;
826   struct rar *rar;
827   size_t skip;
828   char head_type;
829   int ret;
830   unsigned flags;
831   unsigned long crc32_expected;
832
833   a->archive.archive_format = ARCHIVE_FORMAT_RAR;
834   if (a->archive.archive_format_name == NULL)
835     a->archive.archive_format_name = "RAR";
836
837   rar = (struct rar *)(a->format->data);
838
839   /*
840    * It should be sufficient to call archive_read_next_header() for
841    * a reader to determine if an entry is encrypted or not. If the
842    * encryption of an entry is only detectable when calling
843    * archive_read_data(), so be it. We'll do the same check there
844    * as well.
845    */
846   if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
847           rar->has_encrypted_entries = 0;
848   }
849
850   /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
851   * this fails.
852   */
853   if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
854     return (ARCHIVE_EOF);
855
856   p = h;
857   if (rar->found_first_header == 0 &&
858      ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
859     /* This is an executable ? Must be self-extracting... */
860     ret = skip_sfx(a);
861     if (ret < ARCHIVE_WARN)
862       return (ret);
863   }
864   rar->found_first_header = 1;
865
866   while (1)
867   {
868     unsigned long crc32_val;
869
870     if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
871       return (ARCHIVE_FATAL);
872     p = h;
873
874     head_type = p[2];
875     switch(head_type)
876     {
877     case MARK_HEAD:
878       if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
879         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
880           "Invalid marker header");
881         return (ARCHIVE_FATAL);
882       }
883       __archive_read_consume(a, 7);
884       break;
885
886     case MAIN_HEAD:
887       rar->main_flags = archive_le16dec(p + 3);
888       skip = archive_le16dec(p + 5);
889       if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
890         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
891           "Invalid header size");
892         return (ARCHIVE_FATAL);
893       }
894       if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
895         return (ARCHIVE_FATAL);
896       p = h;
897       memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
898       memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
899              sizeof(rar->reserved2));
900       if (rar->main_flags & MHD_ENCRYPTVER) {
901         if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
902           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
903             "Invalid header size");
904           return (ARCHIVE_FATAL);
905         }
906         rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
907                             sizeof(rar->reserved2));
908       }
909
910       /* Main header is password encrytped, so we cannot read any
911          file names or any other info about files from the header. */
912       if (rar->main_flags & MHD_PASSWORD)
913       {
914         archive_entry_set_is_metadata_encrypted(entry, 1);
915         archive_entry_set_is_data_encrypted(entry, 1);
916         rar->has_encrypted_entries = 1;
917          archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
918                           "RAR encryption support unavailable.");
919         return (ARCHIVE_FATAL);
920       }
921
922       crc32_val = crc32(0, (const unsigned char *)p + 2, (unsigned)skip - 2);
923       if ((crc32_val & 0xffff) != archive_le16dec(p)) {
924         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
925           "Header CRC error");
926         return (ARCHIVE_FATAL);
927       }
928       __archive_read_consume(a, skip);
929       break;
930
931     case FILE_HEAD:
932       return read_header(a, entry, head_type);
933
934     case COMM_HEAD:
935     case AV_HEAD:
936     case SUB_HEAD:
937     case PROTECT_HEAD:
938     case SIGN_HEAD:
939     case ENDARC_HEAD:
940       flags = archive_le16dec(p + 3);
941       skip = archive_le16dec(p + 5);
942       if (skip < 7) {
943         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
944           "Invalid header size too small");
945         return (ARCHIVE_FATAL);
946       }
947       if (flags & HD_ADD_SIZE_PRESENT)
948       {
949         if (skip < 7 + 4) {
950           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
951             "Invalid header size too small");
952           return (ARCHIVE_FATAL);
953         }
954         if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
955           return (ARCHIVE_FATAL);
956         p = h;
957         skip += archive_le32dec(p + 7);
958       }
959
960       /* Skip over the 2-byte CRC at the beginning of the header. */
961       crc32_expected = archive_le16dec(p);
962       __archive_read_consume(a, 2);
963       skip -= 2;
964
965       /* Skim the entire header and compute the CRC. */
966       crc32_val = 0;
967       while (skip > 0) {
968               size_t to_read = skip;
969               ssize_t did_read;
970               if (to_read > 32 * 1024) {
971                       to_read = 32 * 1024;
972               }
973               if ((h = __archive_read_ahead(a, to_read, &did_read)) == NULL) {
974                       return (ARCHIVE_FATAL);
975               }
976               p = h;
977               crc32_val = crc32(crc32_val, (const unsigned char *)p, (unsigned)did_read);
978               __archive_read_consume(a, did_read);
979               skip -= did_read;
980       }
981       if ((crc32_val & 0xffff) != crc32_expected) {
982               archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
983                   "Header CRC error");
984               return (ARCHIVE_FATAL);
985       }
986       if (head_type == ENDARC_HEAD)
987               return (ARCHIVE_EOF);
988       break;
989
990     case NEWSUB_HEAD:
991       if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
992         return ret;
993       break;
994
995     default:
996       archive_set_error(&a->archive,  ARCHIVE_ERRNO_FILE_FORMAT,
997                         "Bad RAR file");
998       return (ARCHIVE_FATAL);
999     }
1000   }
1001 }
1002
1003 static int
1004 archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
1005                                   size_t *size, int64_t *offset)
1006 {
1007   struct rar *rar = (struct rar *)(a->format->data);
1008   int ret;
1009
1010   if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
1011           rar->has_encrypted_entries = 0;
1012   }
1013
1014   if (rar->bytes_unconsumed > 0) {
1015       /* Consume as much as the decompressor actually used. */
1016       __archive_read_consume(a, rar->bytes_unconsumed);
1017       rar->bytes_unconsumed = 0;
1018   }
1019
1020   *buff = NULL;
1021   if (rar->entry_eof || rar->offset_seek >= rar->unp_size) {
1022     *size = 0;
1023     *offset = rar->offset;
1024     if (*offset < rar->unp_size)
1025       *offset = rar->unp_size;
1026     return (ARCHIVE_EOF);
1027   }
1028
1029   switch (rar->compression_method)
1030   {
1031   case COMPRESS_METHOD_STORE:
1032     ret = read_data_stored(a, buff, size, offset);
1033     break;
1034
1035   case COMPRESS_METHOD_FASTEST:
1036   case COMPRESS_METHOD_FAST:
1037   case COMPRESS_METHOD_NORMAL:
1038   case COMPRESS_METHOD_GOOD:
1039   case COMPRESS_METHOD_BEST:
1040     ret = read_data_compressed(a, buff, size, offset);
1041     if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN)
1042       __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1043     break;
1044
1045   default:
1046     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1047                       "Unsupported compression method for RAR file.");
1048     ret = ARCHIVE_FATAL;
1049     break;
1050   }
1051   return (ret);
1052 }
1053
1054 static int
1055 archive_read_format_rar_read_data_skip(struct archive_read *a)
1056 {
1057   struct rar *rar;
1058   int64_t bytes_skipped;
1059   int ret;
1060
1061   rar = (struct rar *)(a->format->data);
1062
1063   if (rar->bytes_unconsumed > 0) {
1064       /* Consume as much as the decompressor actually used. */
1065       __archive_read_consume(a, rar->bytes_unconsumed);
1066       rar->bytes_unconsumed = 0;
1067   }
1068
1069   if (rar->bytes_remaining > 0) {
1070     bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
1071     if (bytes_skipped < 0)
1072       return (ARCHIVE_FATAL);
1073   }
1074
1075   /* Compressed data to skip must be read from each header in a multivolume
1076    * archive.
1077    */
1078   if (rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER)
1079   {
1080     ret = archive_read_format_rar_read_header(a, a->entry);
1081     if (ret == (ARCHIVE_EOF))
1082       ret = archive_read_format_rar_read_header(a, a->entry);
1083     if (ret != (ARCHIVE_OK))
1084       return ret;
1085     return archive_read_format_rar_read_data_skip(a);
1086   }
1087
1088   return (ARCHIVE_OK);
1089 }
1090
1091 static int64_t
1092 archive_read_format_rar_seek_data(struct archive_read *a, int64_t offset,
1093     int whence)
1094 {
1095   int64_t client_offset, ret;
1096   unsigned int i;
1097   struct rar *rar = (struct rar *)(a->format->data);
1098
1099   if (rar->compression_method == COMPRESS_METHOD_STORE)
1100   {
1101     /* Modify the offset for use with SEEK_SET */
1102     switch (whence)
1103     {
1104       case SEEK_CUR:
1105         client_offset = rar->offset_seek;
1106         break;
1107       case SEEK_END:
1108         client_offset = rar->unp_size;
1109         break;
1110       case SEEK_SET:
1111       default:
1112         client_offset = 0;
1113     }
1114     client_offset += offset;
1115     if (client_offset < 0)
1116     {
1117       /* Can't seek past beginning of data block */
1118       return -1;
1119     }
1120     else if (client_offset > rar->unp_size)
1121     {
1122       /*
1123        * Set the returned offset but only seek to the end of
1124        * the data block.
1125        */
1126       rar->offset_seek = client_offset;
1127       client_offset = rar->unp_size;
1128     }
1129
1130     client_offset += rar->dbo[0].start_offset;
1131     i = 0;
1132     while (i < rar->cursor)
1133     {
1134       i++;
1135       client_offset += rar->dbo[i].start_offset - rar->dbo[i-1].end_offset;
1136     }
1137     if (rar->main_flags & MHD_VOLUME)
1138     {
1139       /* Find the appropriate offset among the multivolume archive */
1140       while (1)
1141       {
1142         if (client_offset < rar->dbo[rar->cursor].start_offset &&
1143           rar->file_flags & FHD_SPLIT_BEFORE)
1144         {
1145           /* Search backwards for the correct data block */
1146           if (rar->cursor == 0)
1147           {
1148             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1149               "Attempt to seek past beginning of RAR data block");
1150             return (ARCHIVE_FAILED);
1151           }
1152           rar->cursor--;
1153           client_offset -= rar->dbo[rar->cursor+1].start_offset -
1154             rar->dbo[rar->cursor].end_offset;
1155           if (client_offset < rar->dbo[rar->cursor].start_offset)
1156             continue;
1157           ret = __archive_read_seek(a, rar->dbo[rar->cursor].start_offset -
1158             rar->dbo[rar->cursor].header_size, SEEK_SET);
1159           if (ret < (ARCHIVE_OK))
1160             return ret;
1161           ret = archive_read_format_rar_read_header(a, a->entry);
1162           if (ret != (ARCHIVE_OK))
1163           {
1164             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1165               "Error during seek of RAR file");
1166             return (ARCHIVE_FAILED);
1167           }
1168           rar->cursor--;
1169           break;
1170         }
1171         else if (client_offset > rar->dbo[rar->cursor].end_offset &&
1172           rar->file_flags & FHD_SPLIT_AFTER)
1173         {
1174           /* Search forward for the correct data block */
1175           rar->cursor++;
1176           if (rar->cursor < rar->nodes &&
1177             client_offset > rar->dbo[rar->cursor].end_offset)
1178           {
1179             client_offset += rar->dbo[rar->cursor].start_offset -
1180               rar->dbo[rar->cursor-1].end_offset;
1181             continue;
1182           }
1183           rar->cursor--;
1184           ret = __archive_read_seek(a, rar->dbo[rar->cursor].end_offset,
1185                                     SEEK_SET);
1186           if (ret < (ARCHIVE_OK))
1187             return ret;
1188           ret = archive_read_format_rar_read_header(a, a->entry);
1189           if (ret == (ARCHIVE_EOF))
1190           {
1191             rar->has_endarc_header = 1;
1192             ret = archive_read_format_rar_read_header(a, a->entry);
1193           }
1194           if (ret != (ARCHIVE_OK))
1195           {
1196             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1197               "Error during seek of RAR file");
1198             return (ARCHIVE_FAILED);
1199           }
1200           client_offset += rar->dbo[rar->cursor].start_offset -
1201             rar->dbo[rar->cursor-1].end_offset;
1202           continue;
1203         }
1204         break;
1205       }
1206     }
1207
1208     ret = __archive_read_seek(a, client_offset, SEEK_SET);
1209     if (ret < (ARCHIVE_OK))
1210       return ret;
1211     rar->bytes_remaining = rar->dbo[rar->cursor].end_offset - ret;
1212     i = rar->cursor;
1213     while (i > 0)
1214     {
1215       i--;
1216       ret -= rar->dbo[i+1].start_offset - rar->dbo[i].end_offset;
1217     }
1218     ret -= rar->dbo[0].start_offset;
1219
1220     /* Always restart reading the file after a seek */
1221     __archive_reset_read_data(&a->archive);
1222
1223     rar->bytes_unconsumed = 0;
1224     rar->offset = 0;
1225
1226     /*
1227      * If a seek past the end of file was requested, return the requested
1228      * offset.
1229      */
1230     if (ret == rar->unp_size && rar->offset_seek > rar->unp_size)
1231       return rar->offset_seek;
1232
1233     /* Return the new offset */
1234     rar->offset_seek = ret;
1235     return rar->offset_seek;
1236   }
1237   else
1238   {
1239     archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1240       "Seeking of compressed RAR files is unsupported");
1241   }
1242   return (ARCHIVE_FAILED);
1243 }
1244
1245 static int
1246 archive_read_format_rar_cleanup(struct archive_read *a)
1247 {
1248   struct rar *rar;
1249
1250   rar = (struct rar *)(a->format->data);
1251   free_codes(a);
1252   free(rar->filename);
1253   free(rar->filename_save);
1254   free(rar->dbo);
1255   free(rar->unp_buffer);
1256   free(rar->lzss.window);
1257   __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1258   free(rar);
1259   (a->format->data) = NULL;
1260   return (ARCHIVE_OK);
1261 }
1262
1263 static int
1264 read_header(struct archive_read *a, struct archive_entry *entry,
1265             char head_type)
1266 {
1267   const void *h;
1268   const char *p, *endp;
1269   struct rar *rar;
1270   struct rar_header rar_header;
1271   struct rar_file_header file_header;
1272   int64_t header_size;
1273   unsigned filename_size, end;
1274   char *filename;
1275   char *strp;
1276   char packed_size[8];
1277   char unp_size[8];
1278   int ttime;
1279   struct archive_string_conv *sconv, *fn_sconv;
1280   unsigned long crc32_val;
1281   int ret = (ARCHIVE_OK), ret2;
1282
1283   rar = (struct rar *)(a->format->data);
1284
1285   /* Setup a string conversion object for non-rar-unicode filenames. */
1286   sconv = rar->opt_sconv;
1287   if (sconv == NULL) {
1288     if (!rar->init_default_conversion) {
1289       rar->sconv_default =
1290           archive_string_default_conversion_for_read(
1291             &(a->archive));
1292       rar->init_default_conversion = 1;
1293     }
1294     sconv = rar->sconv_default;
1295   }
1296
1297
1298   if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
1299     return (ARCHIVE_FATAL);
1300   p = h;
1301   memcpy(&rar_header, p, sizeof(rar_header));
1302   rar->file_flags = archive_le16dec(rar_header.flags);
1303   header_size = archive_le16dec(rar_header.size);
1304   if (header_size < (int64_t)sizeof(file_header) + 7) {
1305     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1306       "Invalid header size");
1307     return (ARCHIVE_FATAL);
1308   }
1309   crc32_val = crc32(0, (const unsigned char *)p + 2, 7 - 2);
1310   __archive_read_consume(a, 7);
1311
1312   if (!(rar->file_flags & FHD_SOLID))
1313   {
1314     rar->compression_method = 0;
1315     rar->packed_size = 0;
1316     rar->unp_size = 0;
1317     rar->mtime = 0;
1318     rar->ctime = 0;
1319     rar->atime = 0;
1320     rar->arctime = 0;
1321     rar->mode = 0;
1322     memset(&rar->salt, 0, sizeof(rar->salt));
1323     rar->atime = 0;
1324     rar->ansec = 0;
1325     rar->ctime = 0;
1326     rar->cnsec = 0;
1327     rar->mtime = 0;
1328     rar->mnsec = 0;
1329     rar->arctime = 0;
1330     rar->arcnsec = 0;
1331   }
1332   else
1333   {
1334     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1335                       "RAR solid archive support unavailable.");
1336     return (ARCHIVE_FATAL);
1337   }
1338
1339   if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1340     return (ARCHIVE_FATAL);
1341
1342   /* File Header CRC check. */
1343   crc32_val = crc32(crc32_val, h, (unsigned)(header_size - 7));
1344   if ((crc32_val & 0xffff) != archive_le16dec(rar_header.crc)) {
1345     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1346       "Header CRC error");
1347     return (ARCHIVE_FATAL);
1348   }
1349   /* If no CRC error, Go on parsing File Header. */
1350   p = h;
1351   endp = p + header_size - 7;
1352   memcpy(&file_header, p, sizeof(file_header));
1353   p += sizeof(file_header);
1354
1355   rar->compression_method = file_header.method;
1356
1357   ttime = archive_le32dec(file_header.file_time);
1358   rar->mtime = get_time(ttime);
1359
1360   rar->file_crc = archive_le32dec(file_header.file_crc);
1361
1362   if (rar->file_flags & FHD_PASSWORD)
1363   {
1364         archive_entry_set_is_data_encrypted(entry, 1);
1365         rar->has_encrypted_entries = 1;
1366     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1367                       "RAR encryption support unavailable.");
1368     /* Since it is only the data part itself that is encrypted we can at least
1369        extract information about the currently processed entry and don't need
1370        to return ARCHIVE_FATAL here. */
1371     /*return (ARCHIVE_FATAL);*/
1372   }
1373
1374   if (rar->file_flags & FHD_LARGE)
1375   {
1376     memcpy(packed_size, file_header.pack_size, 4);
1377     memcpy(packed_size + 4, p, 4); /* High pack size */
1378     p += 4;
1379     memcpy(unp_size, file_header.unp_size, 4);
1380     memcpy(unp_size + 4, p, 4); /* High unpack size */
1381     p += 4;
1382     rar->packed_size = archive_le64dec(&packed_size);
1383     rar->unp_size = archive_le64dec(&unp_size);
1384   }
1385   else
1386   {
1387     rar->packed_size = archive_le32dec(file_header.pack_size);
1388     rar->unp_size = archive_le32dec(file_header.unp_size);
1389   }
1390
1391   if (rar->packed_size < 0 || rar->unp_size < 0)
1392   {
1393     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1394                       "Invalid sizes specified.");
1395     return (ARCHIVE_FATAL);
1396   }
1397
1398   rar->bytes_remaining = rar->packed_size;
1399
1400   /* TODO: RARv3 subblocks contain comments. For now the complete block is
1401    * consumed at the end.
1402    */
1403   if (head_type == NEWSUB_HEAD) {
1404     size_t distance = p - (const char *)h;
1405     header_size += rar->packed_size;
1406     /* Make sure we have the extended data. */
1407     if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1408         return (ARCHIVE_FATAL);
1409     p = h;
1410     endp = p + header_size - 7;
1411     p += distance;
1412   }
1413
1414   filename_size = archive_le16dec(file_header.name_size);
1415   if (p + filename_size > endp) {
1416     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1417       "Invalid filename size");
1418     return (ARCHIVE_FATAL);
1419   }
1420   if (rar->filename_allocated < filename_size * 2 + 2) {
1421     char *newptr;
1422     size_t newsize = filename_size * 2 + 2;
1423     newptr = realloc(rar->filename, newsize);
1424     if (newptr == NULL) {
1425       archive_set_error(&a->archive, ENOMEM,
1426                         "Couldn't allocate memory.");
1427       return (ARCHIVE_FATAL);
1428     }
1429     rar->filename = newptr;
1430     rar->filename_allocated = newsize;
1431   }
1432   filename = rar->filename;
1433   memcpy(filename, p, filename_size);
1434   filename[filename_size] = '\0';
1435   if (rar->file_flags & FHD_UNICODE)
1436   {
1437     if (filename_size != strlen(filename))
1438     {
1439       unsigned char highbyte, flagbits, flagbyte;
1440       unsigned fn_end, offset;
1441
1442       end = filename_size;
1443       fn_end = filename_size * 2;
1444       filename_size = 0;
1445       offset = (unsigned)strlen(filename) + 1;
1446       highbyte = *(p + offset++);
1447       flagbits = 0;
1448       flagbyte = 0;
1449       while (offset < end && filename_size < fn_end)
1450       {
1451         if (!flagbits)
1452         {
1453           flagbyte = *(p + offset++);
1454           flagbits = 8;
1455         }
1456
1457         flagbits -= 2;
1458         switch((flagbyte >> flagbits) & 3)
1459         {
1460           case 0:
1461             filename[filename_size++] = '\0';
1462             filename[filename_size++] = *(p + offset++);
1463             break;
1464           case 1:
1465             filename[filename_size++] = highbyte;
1466             filename[filename_size++] = *(p + offset++);
1467             break;
1468           case 2:
1469             filename[filename_size++] = *(p + offset + 1);
1470             filename[filename_size++] = *(p + offset);
1471             offset += 2;
1472             break;
1473           case 3:
1474           {
1475             char extra, high;
1476             uint8_t length = *(p + offset++);
1477
1478             if (length & 0x80) {
1479               extra = *(p + offset++);
1480               high = (char)highbyte;
1481             } else
1482               extra = high = 0;
1483             length = (length & 0x7f) + 2;
1484             while (length && filename_size < fn_end) {
1485               unsigned cp = filename_size >> 1;
1486               filename[filename_size++] = high;
1487               filename[filename_size++] = p[cp] + extra;
1488               length--;
1489             }
1490           }
1491           break;
1492         }
1493       }
1494       if (filename_size > fn_end) {
1495         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1496           "Invalid filename");
1497         return (ARCHIVE_FATAL);
1498       }
1499       filename[filename_size++] = '\0';
1500       filename[filename_size++] = '\0';
1501
1502       /* Decoded unicode form is UTF-16BE, so we have to update a string
1503        * conversion object for it. */
1504       if (rar->sconv_utf16be == NULL) {
1505         rar->sconv_utf16be = archive_string_conversion_from_charset(
1506            &a->archive, "UTF-16BE", 1);
1507         if (rar->sconv_utf16be == NULL)
1508           return (ARCHIVE_FATAL);
1509       }
1510       fn_sconv = rar->sconv_utf16be;
1511
1512       strp = filename;
1513       while (memcmp(strp, "\x00\x00", 2))
1514       {
1515         if (!memcmp(strp, "\x00\\", 2))
1516           *(strp + 1) = '/';
1517         strp += 2;
1518       }
1519       p += offset;
1520     } else {
1521       /*
1522        * If FHD_UNICODE is set but no unicode data, this file name form
1523        * is UTF-8, so we have to update a string conversion object for
1524        * it accordingly.
1525        */
1526       if (rar->sconv_utf8 == NULL) {
1527         rar->sconv_utf8 = archive_string_conversion_from_charset(
1528            &a->archive, "UTF-8", 1);
1529         if (rar->sconv_utf8 == NULL)
1530           return (ARCHIVE_FATAL);
1531       }
1532       fn_sconv = rar->sconv_utf8;
1533       while ((strp = strchr(filename, '\\')) != NULL)
1534         *strp = '/';
1535       p += filename_size;
1536     }
1537   }
1538   else
1539   {
1540     fn_sconv = sconv;
1541     while ((strp = strchr(filename, '\\')) != NULL)
1542       *strp = '/';
1543     p += filename_size;
1544   }
1545
1546   /* Split file in multivolume RAR. No more need to process header. */
1547   if (rar->filename_save &&
1548     filename_size == rar->filename_save_size &&
1549     !memcmp(rar->filename, rar->filename_save, filename_size + 1))
1550   {
1551     __archive_read_consume(a, header_size - 7);
1552     rar->cursor++;
1553     if (rar->cursor >= rar->nodes)
1554     {
1555       rar->nodes++;
1556       if ((rar->dbo =
1557         realloc(rar->dbo, sizeof(*rar->dbo) * rar->nodes)) == NULL)
1558       {
1559         archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1560         return (ARCHIVE_FATAL);
1561       }
1562       rar->dbo[rar->cursor].header_size = header_size;
1563       rar->dbo[rar->cursor].start_offset = -1;
1564       rar->dbo[rar->cursor].end_offset = -1;
1565     }
1566     if (rar->dbo[rar->cursor].start_offset < 0)
1567     {
1568       rar->dbo[rar->cursor].start_offset = a->filter->position;
1569       rar->dbo[rar->cursor].end_offset = rar->dbo[rar->cursor].start_offset +
1570         rar->packed_size;
1571     }
1572     return ret;
1573   }
1574
1575   rar->filename_save = (char*)realloc(rar->filename_save,
1576                                       filename_size + 1);
1577   memcpy(rar->filename_save, rar->filename, filename_size + 1);
1578   rar->filename_save_size = filename_size;
1579
1580   /* Set info for seeking */
1581   free(rar->dbo);
1582   if ((rar->dbo = calloc(1, sizeof(*rar->dbo))) == NULL)
1583   {
1584     archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1585     return (ARCHIVE_FATAL);
1586   }
1587   rar->dbo[0].header_size = header_size;
1588   rar->dbo[0].start_offset = -1;
1589   rar->dbo[0].end_offset = -1;
1590   rar->cursor = 0;
1591   rar->nodes = 1;
1592
1593   if (rar->file_flags & FHD_SALT)
1594   {
1595     if (p + 8 > endp) {
1596       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1597         "Invalid header size");
1598       return (ARCHIVE_FATAL);
1599     }
1600     memcpy(rar->salt, p, 8);
1601     p += 8;
1602   }
1603
1604   if (rar->file_flags & FHD_EXTTIME) {
1605     if (read_exttime(p, rar, endp) < 0) {
1606       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1607         "Invalid header size");
1608       return (ARCHIVE_FATAL);
1609     }
1610   }
1611
1612   __archive_read_consume(a, header_size - 7);
1613   rar->dbo[0].start_offset = a->filter->position;
1614   rar->dbo[0].end_offset = rar->dbo[0].start_offset + rar->packed_size;
1615
1616   switch(file_header.host_os)
1617   {
1618   case OS_MSDOS:
1619   case OS_OS2:
1620   case OS_WIN32:
1621     rar->mode = archive_le32dec(file_header.file_attr);
1622     if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
1623       rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
1624     else
1625       rar->mode = AE_IFREG;
1626     rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1627     break;
1628
1629   case OS_UNIX:
1630   case OS_MAC_OS:
1631   case OS_BEOS:
1632     rar->mode = archive_le32dec(file_header.file_attr);
1633     break;
1634
1635   default:
1636     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1637                       "Unknown file attributes from RAR file's host OS");
1638     return (ARCHIVE_FATAL);
1639   }
1640
1641   rar->bytes_uncopied = rar->bytes_unconsumed = 0;
1642   rar->lzss.position = rar->offset = 0;
1643   rar->offset_seek = 0;
1644   rar->dictionary_size = 0;
1645   rar->offset_outgoing = 0;
1646   rar->br.cache_avail = 0;
1647   rar->br.avail_in = 0;
1648   rar->crc_calculated = 0;
1649   rar->entry_eof = 0;
1650   rar->valid = 1;
1651   rar->is_ppmd_block = 0;
1652   rar->start_new_table = 1;
1653   free(rar->unp_buffer);
1654   rar->unp_buffer = NULL;
1655   rar->unp_offset = 0;
1656   rar->unp_buffer_size = UNP_BUFFER_SIZE;
1657   memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1658   __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1659   rar->ppmd_valid = rar->ppmd_eod = 0;
1660
1661   /* Don't set any archive entries for non-file header types */
1662   if (head_type == NEWSUB_HEAD)
1663     return ret;
1664
1665   archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
1666   archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
1667   archive_entry_set_atime(entry, rar->atime, rar->ansec);
1668   archive_entry_set_size(entry, rar->unp_size);
1669   archive_entry_set_mode(entry, rar->mode);
1670
1671   if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
1672   {
1673     if (errno == ENOMEM)
1674     {
1675       archive_set_error(&a->archive, ENOMEM,
1676                         "Can't allocate memory for Pathname");
1677       return (ARCHIVE_FATAL);
1678     }
1679     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1680                       "Pathname cannot be converted from %s to current locale.",
1681                       archive_string_conversion_charset_name(fn_sconv));
1682     ret = (ARCHIVE_WARN);
1683   }
1684
1685   if (((rar->mode) & AE_IFMT) == AE_IFLNK)
1686   {
1687     /* Make sure a symbolic-link file does not have its body. */
1688     rar->bytes_remaining = 0;
1689     archive_entry_set_size(entry, 0);
1690
1691     /* Read a symbolic-link name. */
1692     if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
1693       return ret2;
1694     if (ret > ret2)
1695       ret = ret2;
1696   }
1697
1698   if (rar->bytes_remaining == 0)
1699     rar->entry_eof = 1;
1700
1701   return ret;
1702 }
1703
1704 static time_t
1705 get_time(int ttime)
1706 {
1707   struct tm tm;
1708   tm.tm_sec = 2 * (ttime & 0x1f);
1709   tm.tm_min = (ttime >> 5) & 0x3f;
1710   tm.tm_hour = (ttime >> 11) & 0x1f;
1711   tm.tm_mday = (ttime >> 16) & 0x1f;
1712   tm.tm_mon = ((ttime >> 21) & 0x0f) - 1;
1713   tm.tm_year = ((ttime >> 25) & 0x7f) + 80;
1714   tm.tm_isdst = -1;
1715   return mktime(&tm);
1716 }
1717
1718 static int
1719 read_exttime(const char *p, struct rar *rar, const char *endp)
1720 {
1721   unsigned rmode, flags, rem, j, count;
1722   int ttime, i;
1723   struct tm *tm;
1724   time_t t;
1725   long nsec;
1726
1727   if (p + 2 > endp)
1728     return (-1);
1729   flags = archive_le16dec(p);
1730   p += 2;
1731
1732   for (i = 3; i >= 0; i--)
1733   {
1734     t = 0;
1735     if (i == 3)
1736       t = rar->mtime;
1737     rmode = flags >> i * 4;
1738     if (rmode & 8)
1739     {
1740       if (!t)
1741       {
1742         if (p + 4 > endp)
1743           return (-1);
1744         ttime = archive_le32dec(p);
1745         t = get_time(ttime);
1746         p += 4;
1747       }
1748       rem = 0;
1749       count = rmode & 3;
1750       if (p + count > endp)
1751         return (-1);
1752       for (j = 0; j < count; j++)
1753       {
1754         rem = ((*p) << 16) | (rem >> 8);
1755         p++;
1756       }
1757       tm = localtime(&t);
1758       nsec = tm->tm_sec + rem / NS_UNIT;
1759       if (rmode & 4)
1760       {
1761         tm->tm_sec++;
1762         t = mktime(tm);
1763       }
1764       if (i == 3)
1765       {
1766         rar->mtime = t;
1767         rar->mnsec = nsec;
1768       }
1769       else if (i == 2)
1770       {
1771         rar->ctime = t;
1772         rar->cnsec = nsec;
1773       }
1774       else if (i == 1)
1775       {
1776         rar->atime = t;
1777         rar->ansec = nsec;
1778       }
1779       else
1780       {
1781         rar->arctime = t;
1782         rar->arcnsec = nsec;
1783       }
1784     }
1785   }
1786   return (0);
1787 }
1788
1789 static int
1790 read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
1791                     struct archive_string_conv *sconv)
1792 {
1793   const void *h;
1794   const char *p;
1795   struct rar *rar;
1796   int ret = (ARCHIVE_OK);
1797
1798   rar = (struct rar *)(a->format->data);
1799   if ((h = rar_read_ahead(a, (size_t)rar->packed_size, NULL)) == NULL)
1800     return (ARCHIVE_FATAL);
1801   p = h;
1802
1803   if (archive_entry_copy_symlink_l(entry,
1804       p, (size_t)rar->packed_size, sconv))
1805   {
1806     if (errno == ENOMEM)
1807     {
1808       archive_set_error(&a->archive, ENOMEM,
1809                         "Can't allocate memory for link");
1810       return (ARCHIVE_FATAL);
1811     }
1812     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1813                       "link cannot be converted from %s to current locale.",
1814                       archive_string_conversion_charset_name(sconv));
1815     ret = (ARCHIVE_WARN);
1816   }
1817   __archive_read_consume(a, rar->packed_size);
1818   return ret;
1819 }
1820
1821 static int
1822 read_data_stored(struct archive_read *a, const void **buff, size_t *size,
1823                  int64_t *offset)
1824 {
1825   struct rar *rar;
1826   ssize_t bytes_avail;
1827
1828   rar = (struct rar *)(a->format->data);
1829   if (rar->bytes_remaining == 0 &&
1830     !(rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER))
1831   {
1832     *buff = NULL;
1833     *size = 0;
1834     *offset = rar->offset;
1835     if (rar->file_crc != rar->crc_calculated) {
1836       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1837                         "File CRC error");
1838       return (ARCHIVE_FATAL);
1839     }
1840     rar->entry_eof = 1;
1841     return (ARCHIVE_EOF);
1842   }
1843
1844   *buff = rar_read_ahead(a, 1, &bytes_avail);
1845   if (bytes_avail <= 0)
1846   {
1847     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1848                       "Truncated RAR file data");
1849     return (ARCHIVE_FATAL);
1850   }
1851
1852   *size = bytes_avail;
1853   *offset = rar->offset;
1854   rar->offset += bytes_avail;
1855   rar->offset_seek += bytes_avail;
1856   rar->bytes_remaining -= bytes_avail;
1857   rar->bytes_unconsumed = bytes_avail;
1858   /* Calculate File CRC. */
1859   rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1860     (unsigned)bytes_avail);
1861   return (ARCHIVE_OK);
1862 }
1863
1864 static int
1865 read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
1866                int64_t *offset)
1867 {
1868   struct rar *rar;
1869   int64_t start, end, actualend;
1870   size_t bs;
1871   int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
1872
1873   rar = (struct rar *)(a->format->data);
1874
1875   do {
1876     if (!rar->valid)
1877       return (ARCHIVE_FATAL);
1878     if (rar->ppmd_eod ||
1879        (rar->dictionary_size && rar->offset >= rar->unp_size))
1880     {
1881       if (rar->unp_offset > 0) {
1882         /*
1883          * We have unprocessed extracted data. write it out.
1884          */
1885         *buff = rar->unp_buffer;
1886         *size = rar->unp_offset;
1887         *offset = rar->offset_outgoing;
1888         rar->offset_outgoing += *size;
1889         /* Calculate File CRC. */
1890         rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1891           (unsigned)*size);
1892         rar->unp_offset = 0;
1893         return (ARCHIVE_OK);
1894       }
1895       *buff = NULL;
1896       *size = 0;
1897       *offset = rar->offset;
1898       if (rar->file_crc != rar->crc_calculated) {
1899         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1900                           "File CRC error");
1901         return (ARCHIVE_FATAL);
1902       }
1903       rar->entry_eof = 1;
1904       return (ARCHIVE_EOF);
1905     }
1906
1907     if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
1908     {
1909       if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
1910         bs = rar->unp_buffer_size - rar->unp_offset;
1911       else
1912         bs = (size_t)rar->bytes_uncopied;
1913       ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
1914       if (ret != ARCHIVE_OK)
1915         return (ret);
1916       rar->offset += bs;
1917       rar->bytes_uncopied -= bs;
1918       if (*buff != NULL) {
1919         rar->unp_offset = 0;
1920         *size = rar->unp_buffer_size;
1921         *offset = rar->offset_outgoing;
1922         rar->offset_outgoing += *size;
1923         /* Calculate File CRC. */
1924         rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1925           (unsigned)*size);
1926         return (ret);
1927       }
1928       continue;
1929     }
1930
1931     if (!rar->br.next_in &&
1932       (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
1933       return (ret);
1934     if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
1935       return (ret);
1936
1937     if (rar->is_ppmd_block)
1938     {
1939       if ((sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1940         &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1941       {
1942         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1943                           "Invalid symbol");
1944         return (ARCHIVE_FATAL);
1945       }
1946       if(sym != rar->ppmd_escape)
1947       {
1948         lzss_emit_literal(rar, sym);
1949         rar->bytes_uncopied++;
1950       }
1951       else
1952       {
1953         if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1954           &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1955         {
1956           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1957                             "Invalid symbol");
1958           return (ARCHIVE_FATAL);
1959         }
1960
1961         switch(code)
1962         {
1963           case 0:
1964             rar->start_new_table = 1;
1965             return read_data_compressed(a, buff, size, offset);
1966
1967           case 2:
1968             rar->ppmd_eod = 1;/* End Of ppmd Data. */
1969             continue;
1970
1971           case 3:
1972             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1973                               "Parsing filters is unsupported.");
1974             return (ARCHIVE_FAILED);
1975
1976           case 4:
1977             lzss_offset = 0;
1978             for (i = 2; i >= 0; i--)
1979             {
1980               if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1981                 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1982               {
1983                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1984                                   "Invalid symbol");
1985                 return (ARCHIVE_FATAL);
1986               }
1987               lzss_offset |= code << (i * 8);
1988             }
1989             if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1990               &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1991             {
1992               archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1993                                 "Invalid symbol");
1994               return (ARCHIVE_FATAL);
1995             }
1996             lzss_emit_match(rar, lzss_offset + 2, length + 32);
1997             rar->bytes_uncopied += length + 32;
1998             break;
1999
2000           case 5:
2001             if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2002               &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2003             {
2004               archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2005                                 "Invalid symbol");
2006               return (ARCHIVE_FATAL);
2007             }
2008             lzss_emit_match(rar, 1, length + 4);
2009             rar->bytes_uncopied += length + 4;
2010             break;
2011
2012          default:
2013            lzss_emit_literal(rar, sym);
2014            rar->bytes_uncopied++;
2015         }
2016       }
2017     }
2018     else
2019     {
2020       start = rar->offset;
2021       end = start + rar->dictionary_size;
2022       rar->filterstart = INT64_MAX;
2023
2024       if ((actualend = expand(a, end)) < 0)
2025         return ((int)actualend);
2026
2027       rar->bytes_uncopied = actualend - start;
2028       if (rar->bytes_uncopied == 0) {
2029           /* Broken RAR files cause this case.
2030           * NOTE: If this case were possible on a normal RAR file
2031           * we would find out where it was actually bad and
2032           * what we would do to solve it. */
2033           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2034                             "Internal error extracting RAR file");
2035           return (ARCHIVE_FATAL);
2036       }
2037     }
2038     if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
2039       bs = rar->unp_buffer_size - rar->unp_offset;
2040     else
2041       bs = (size_t)rar->bytes_uncopied;
2042     ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
2043     if (ret != ARCHIVE_OK)
2044       return (ret);
2045     rar->offset += bs;
2046     rar->bytes_uncopied -= bs;
2047     /*
2048      * If *buff is NULL, it means unp_buffer is not full.
2049      * So we have to continue extracting a RAR file.
2050      */
2051   } while (*buff == NULL);
2052
2053   rar->unp_offset = 0;
2054   *size = rar->unp_buffer_size;
2055   *offset = rar->offset_outgoing;
2056   rar->offset_outgoing += *size;
2057   /* Calculate File CRC. */
2058   rar->crc_calculated = crc32(rar->crc_calculated, *buff, (unsigned)*size);
2059   return ret;
2060 }
2061
2062 static int
2063 parse_codes(struct archive_read *a)
2064 {
2065   int i, j, val, n, r;
2066   unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
2067   unsigned int maxorder;
2068   struct huffman_code precode;
2069   struct rar *rar = (struct rar *)(a->format->data);
2070   struct rar_br *br = &(rar->br);
2071
2072   free_codes(a);
2073
2074   /* Skip to the next byte */
2075   rar_br_consume_unalined_bits(br);
2076
2077   /* PPMd block flag */
2078   if (!rar_br_read_ahead(a, br, 1))
2079     goto truncated_data;
2080   if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
2081   {
2082     rar_br_consume(br, 1);
2083     if (!rar_br_read_ahead(a, br, 7))
2084       goto truncated_data;
2085     ppmd_flags = rar_br_bits(br, 7);
2086     rar_br_consume(br, 7);
2087
2088     /* Memory is allocated in MB */
2089     if (ppmd_flags & 0x20)
2090     {
2091       if (!rar_br_read_ahead(a, br, 8))
2092         goto truncated_data;
2093       rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
2094       rar_br_consume(br, 8);
2095     }
2096
2097     if (ppmd_flags & 0x40)
2098     {
2099       if (!rar_br_read_ahead(a, br, 8))
2100         goto truncated_data;
2101       rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
2102       rar_br_consume(br, 8);
2103     }
2104     else
2105       rar->ppmd_escape = 2;
2106
2107     if (ppmd_flags & 0x20)
2108     {
2109       maxorder = (ppmd_flags & 0x1F) + 1;
2110       if(maxorder > 16)
2111         maxorder = 16 + (maxorder - 16) * 3;
2112
2113       if (maxorder == 1)
2114       {
2115         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2116                           "Truncated RAR file data");
2117         return (ARCHIVE_FATAL);
2118       }
2119
2120       /* Make sure ppmd7_contest is freed before Ppmd7_Construct
2121        * because reading a broken file cause this abnormal sequence. */
2122       __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
2123
2124       rar->bytein.a = a;
2125       rar->bytein.Read = &ppmd_read;
2126       __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
2127       rar->range_dec.Stream = &rar->bytein;
2128       __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
2129
2130       if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
2131         rar->dictionary_size, &g_szalloc))
2132       {
2133         archive_set_error(&a->archive, ENOMEM,
2134                           "Out of memory");
2135         return (ARCHIVE_FATAL);
2136       }
2137       if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2138       {
2139         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2140                           "Unable to initialize PPMd range decoder");
2141         return (ARCHIVE_FATAL);
2142       }
2143       __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
2144       rar->ppmd_valid = 1;
2145     }
2146     else
2147     {
2148       if (!rar->ppmd_valid) {
2149         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2150                           "Invalid PPMd sequence");
2151         return (ARCHIVE_FATAL);
2152       }
2153       if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2154       {
2155         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2156                           "Unable to initialize PPMd range decoder");
2157         return (ARCHIVE_FATAL);
2158       }
2159     }
2160   }
2161   else
2162   {
2163     rar_br_consume(br, 1);
2164
2165     /* Keep existing table flag */
2166     if (!rar_br_read_ahead(a, br, 1))
2167       goto truncated_data;
2168     if (!rar_br_bits(br, 1))
2169       memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
2170     rar_br_consume(br, 1);
2171
2172     memset(&bitlengths, 0, sizeof(bitlengths));
2173     for (i = 0; i < MAX_SYMBOLS;)
2174     {
2175       if (!rar_br_read_ahead(a, br, 4))
2176         goto truncated_data;
2177       bitlengths[i++] = rar_br_bits(br, 4);
2178       rar_br_consume(br, 4);
2179       if (bitlengths[i-1] == 0xF)
2180       {
2181         if (!rar_br_read_ahead(a, br, 4))
2182           goto truncated_data;
2183         zerocount = rar_br_bits(br, 4);
2184         rar_br_consume(br, 4);
2185         if (zerocount)
2186         {
2187           i--;
2188           for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
2189             bitlengths[i++] = 0;
2190         }
2191       }
2192     }
2193
2194     memset(&precode, 0, sizeof(precode));
2195     r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
2196     if (r != ARCHIVE_OK) {
2197       free(precode.tree);
2198       free(precode.table);
2199       return (r);
2200     }
2201
2202     for (i = 0; i < HUFFMAN_TABLE_SIZE;)
2203     {
2204       if ((val = read_next_symbol(a, &precode)) < 0) {
2205         free(precode.tree);
2206         free(precode.table);
2207         return (ARCHIVE_FATAL);
2208       }
2209       if (val < 16)
2210       {
2211         rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
2212         i++;
2213       }
2214       else if (val < 18)
2215       {
2216         if (i == 0)
2217         {
2218           free(precode.tree);
2219           free(precode.table);
2220           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2221                             "Internal error extracting RAR file.");
2222           return (ARCHIVE_FATAL);
2223         }
2224
2225         if(val == 16) {
2226           if (!rar_br_read_ahead(a, br, 3)) {
2227             free(precode.tree);
2228             free(precode.table);
2229             goto truncated_data;
2230           }
2231           n = rar_br_bits(br, 3) + 3;
2232           rar_br_consume(br, 3);
2233         } else {
2234           if (!rar_br_read_ahead(a, br, 7)) {
2235             free(precode.tree);
2236             free(precode.table);
2237             goto truncated_data;
2238           }
2239           n = rar_br_bits(br, 7) + 11;
2240           rar_br_consume(br, 7);
2241         }
2242
2243         for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2244         {
2245           rar->lengthtable[i] = rar->lengthtable[i-1];
2246           i++;
2247         }
2248       }
2249       else
2250       {
2251         if(val == 18) {
2252           if (!rar_br_read_ahead(a, br, 3)) {
2253             free(precode.tree);
2254             free(precode.table);
2255             goto truncated_data;
2256           }
2257           n = rar_br_bits(br, 3) + 3;
2258           rar_br_consume(br, 3);
2259         } else {
2260           if (!rar_br_read_ahead(a, br, 7)) {
2261             free(precode.tree);
2262             free(precode.table);
2263             goto truncated_data;
2264           }
2265           n = rar_br_bits(br, 7) + 11;
2266           rar_br_consume(br, 7);
2267         }
2268
2269         for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2270           rar->lengthtable[i++] = 0;
2271       }
2272     }
2273     free(precode.tree);
2274     free(precode.table);
2275
2276     r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
2277                 MAX_SYMBOL_LENGTH);
2278     if (r != ARCHIVE_OK)
2279       return (r);
2280     r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
2281                 OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2282     if (r != ARCHIVE_OK)
2283       return (r);
2284     r = create_code(a, &rar->lowoffsetcode,
2285                 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
2286                 LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2287     if (r != ARCHIVE_OK)
2288       return (r);
2289     r = create_code(a, &rar->lengthcode,
2290                 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
2291                 LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
2292     if (r != ARCHIVE_OK)
2293       return (r);
2294   }
2295
2296   if (!rar->dictionary_size || !rar->lzss.window)
2297   {
2298     /* Seems as though dictionary sizes are not used. Even so, minimize
2299      * memory usage as much as possible.
2300      */
2301     void *new_window;
2302     unsigned int new_size;
2303
2304     if (rar->unp_size >= DICTIONARY_MAX_SIZE)
2305       new_size = DICTIONARY_MAX_SIZE;
2306     else
2307       new_size = rar_fls((unsigned int)rar->unp_size) << 1;
2308     new_window = realloc(rar->lzss.window, new_size);
2309     if (new_window == NULL) {
2310       archive_set_error(&a->archive, ENOMEM,
2311                         "Unable to allocate memory for uncompressed data.");
2312       return (ARCHIVE_FATAL);
2313     }
2314     rar->lzss.window = (unsigned char *)new_window;
2315     rar->dictionary_size = new_size;
2316     memset(rar->lzss.window, 0, rar->dictionary_size);
2317     rar->lzss.mask = rar->dictionary_size - 1;
2318   }
2319
2320   rar->start_new_table = 0;
2321   return (ARCHIVE_OK);
2322 truncated_data:
2323   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2324                     "Truncated RAR file data");
2325   rar->valid = 0;
2326   return (ARCHIVE_FATAL);
2327 }
2328
2329 static void
2330 free_codes(struct archive_read *a)
2331 {
2332   struct rar *rar = (struct rar *)(a->format->data);
2333   free(rar->maincode.tree);
2334   free(rar->offsetcode.tree);
2335   free(rar->lowoffsetcode.tree);
2336   free(rar->lengthcode.tree);
2337   free(rar->maincode.table);
2338   free(rar->offsetcode.table);
2339   free(rar->lowoffsetcode.table);
2340   free(rar->lengthcode.table);
2341   memset(&rar->maincode, 0, sizeof(rar->maincode));
2342   memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
2343   memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
2344   memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
2345 }
2346
2347
2348 static int
2349 read_next_symbol(struct archive_read *a, struct huffman_code *code)
2350 {
2351   unsigned char bit;
2352   unsigned int bits;
2353   int length, value, node;
2354   struct rar *rar;
2355   struct rar_br *br;
2356
2357   if (!code->table)
2358   {
2359     if (make_table(a, code) != (ARCHIVE_OK))
2360       return -1;
2361   }
2362
2363   rar = (struct rar *)(a->format->data);
2364   br = &(rar->br);
2365
2366   /* Look ahead (peek) at bits */
2367   if (!rar_br_read_ahead(a, br, code->tablesize)) {
2368     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2369                       "Truncated RAR file data");
2370     rar->valid = 0;
2371     return -1;
2372   }
2373   bits = rar_br_bits(br, code->tablesize);
2374
2375   length = code->table[bits].length;
2376   value = code->table[bits].value;
2377
2378   if (length < 0)
2379   {
2380     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2381                       "Invalid prefix code in bitstream");
2382     return -1;
2383   }
2384
2385   if (length <= code->tablesize)
2386   {
2387     /* Skip length bits */
2388     rar_br_consume(br, length);
2389     return value;
2390   }
2391
2392   /* Skip tablesize bits */
2393   rar_br_consume(br, code->tablesize);
2394
2395   node = value;
2396   while (!(code->tree[node].branches[0] ==
2397     code->tree[node].branches[1]))
2398   {
2399     if (!rar_br_read_ahead(a, br, 1)) {
2400       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2401                         "Truncated RAR file data");
2402       rar->valid = 0;
2403       return -1;
2404     }
2405     bit = rar_br_bits(br, 1);
2406     rar_br_consume(br, 1);
2407
2408     if (code->tree[node].branches[bit] < 0)
2409     {
2410       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2411                         "Invalid prefix code in bitstream");
2412       return -1;
2413     }
2414     node = code->tree[node].branches[bit];
2415   }
2416
2417   return code->tree[node].branches[0];
2418 }
2419
2420 static int
2421 create_code(struct archive_read *a, struct huffman_code *code,
2422             unsigned char *lengths, int numsymbols, char maxlength)
2423 {
2424   int i, j, codebits = 0, symbolsleft = numsymbols;
2425
2426   code->numentries = 0;
2427   code->numallocatedentries = 0;
2428   if (new_node(code) < 0) {
2429     archive_set_error(&a->archive, ENOMEM,
2430                       "Unable to allocate memory for node data.");
2431     return (ARCHIVE_FATAL);
2432   }
2433   code->numentries = 1;
2434   code->minlength = INT_MAX;
2435   code->maxlength = INT_MIN;
2436   codebits = 0;
2437   for(i = 1; i <= maxlength; i++)
2438   {
2439     for(j = 0; j < numsymbols; j++)
2440     {
2441       if (lengths[j] != i) continue;
2442       if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
2443         return (ARCHIVE_FATAL);
2444       codebits++;
2445       if (--symbolsleft <= 0) { break; break; }
2446     }
2447     codebits <<= 1;
2448   }
2449   return (ARCHIVE_OK);
2450 }
2451
2452 static int
2453 add_value(struct archive_read *a, struct huffman_code *code, int value,
2454           int codebits, int length)
2455 {
2456   int repeatpos, lastnode, bitpos, bit, repeatnode, nextnode;
2457
2458   free(code->table);
2459   code->table = NULL;
2460
2461   if(length > code->maxlength)
2462     code->maxlength = length;
2463   if(length < code->minlength)
2464     code->minlength = length;
2465
2466   repeatpos = -1;
2467   if (repeatpos == 0 || (repeatpos >= 0
2468     && (((codebits >> (repeatpos - 1)) & 3) == 0
2469     || ((codebits >> (repeatpos - 1)) & 3) == 3)))
2470   {
2471     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2472                       "Invalid repeat position");
2473     return (ARCHIVE_FATAL);
2474   }
2475
2476   lastnode = 0;
2477   for (bitpos = length - 1; bitpos >= 0; bitpos--)
2478   {
2479     bit = (codebits >> bitpos) & 1;
2480
2481     /* Leaf node check */
2482     if (code->tree[lastnode].branches[0] ==
2483       code->tree[lastnode].branches[1])
2484     {
2485       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2486                         "Prefix found");
2487       return (ARCHIVE_FATAL);
2488     }
2489
2490     if (bitpos == repeatpos)
2491     {
2492       /* Open branch check */
2493       if (!(code->tree[lastnode].branches[bit] < 0))
2494       {
2495         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2496                           "Invalid repeating code");
2497         return (ARCHIVE_FATAL);
2498       }
2499
2500       if ((repeatnode = new_node(code)) < 0) {
2501         archive_set_error(&a->archive, ENOMEM,
2502                           "Unable to allocate memory for node data.");
2503         return (ARCHIVE_FATAL);
2504       }
2505       if ((nextnode = new_node(code)) < 0) {
2506         archive_set_error(&a->archive, ENOMEM,
2507                           "Unable to allocate memory for node data.");
2508         return (ARCHIVE_FATAL);
2509       }
2510
2511       /* Set branches */
2512       code->tree[lastnode].branches[bit] = repeatnode;
2513       code->tree[repeatnode].branches[bit] = repeatnode;
2514       code->tree[repeatnode].branches[bit^1] = nextnode;
2515       lastnode = nextnode;
2516
2517       bitpos++; /* terminating bit already handled, skip it */
2518     }
2519     else
2520     {
2521       /* Open branch check */
2522       if (code->tree[lastnode].branches[bit] < 0)
2523       {
2524         if (new_node(code) < 0) {
2525           archive_set_error(&a->archive, ENOMEM,
2526                             "Unable to allocate memory for node data.");
2527           return (ARCHIVE_FATAL);
2528         }
2529         code->tree[lastnode].branches[bit] = code->numentries++;
2530       }
2531
2532       /* set to branch */
2533       lastnode = code->tree[lastnode].branches[bit];
2534     }
2535   }
2536
2537   if (!(code->tree[lastnode].branches[0] == -1
2538     && code->tree[lastnode].branches[1] == -2))
2539   {
2540     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2541                       "Prefix found");
2542     return (ARCHIVE_FATAL);
2543   }
2544
2545   /* Set leaf value */
2546   code->tree[lastnode].branches[0] = value;
2547   code->tree[lastnode].branches[1] = value;
2548
2549   return (ARCHIVE_OK);
2550 }
2551
2552 static int
2553 new_node(struct huffman_code *code)
2554 {
2555   void *new_tree;
2556   if (code->numallocatedentries == code->numentries) {
2557     int new_num_entries = 256;
2558     if (code->numentries > 0) {
2559         new_num_entries = code->numentries * 2;
2560     }
2561     new_tree = realloc(code->tree, new_num_entries * sizeof(*code->tree));
2562     if (new_tree == NULL)
2563         return (-1);
2564     code->tree = (struct huffman_tree_node *)new_tree;
2565     code->numallocatedentries = new_num_entries;
2566   }
2567   code->tree[code->numentries].branches[0] = -1;
2568   code->tree[code->numentries].branches[1] = -2;
2569   return 1;
2570 }
2571
2572 static int
2573 make_table(struct archive_read *a, struct huffman_code *code)
2574 {
2575   if (code->maxlength < code->minlength || code->maxlength > 10)
2576     code->tablesize = 10;
2577   else
2578     code->tablesize = code->maxlength;
2579
2580   code->table =
2581     (struct huffman_table_entry *)calloc(1, sizeof(*code->table)
2582     * ((size_t)1 << code->tablesize));
2583
2584   return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
2585 }
2586
2587 static int
2588 make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
2589                    struct huffman_table_entry *table, int depth,
2590                    int maxdepth)
2591 {
2592   int currtablesize, i, ret = (ARCHIVE_OK);
2593
2594   if (!code->tree)
2595   {
2596     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2597                       "Huffman tree was not created.");
2598     return (ARCHIVE_FATAL);
2599   }
2600   if (node < 0 || node >= code->numentries)
2601   {
2602     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2603                       "Invalid location to Huffman tree specified.");
2604     return (ARCHIVE_FATAL);
2605   }
2606
2607   currtablesize = 1 << (maxdepth - depth);
2608
2609   if (code->tree[node].branches[0] ==
2610     code->tree[node].branches[1])
2611   {
2612     for(i = 0; i < currtablesize; i++)
2613     {
2614       table[i].length = depth;
2615       table[i].value = code->tree[node].branches[0];
2616     }
2617   }
2618   else if (node < 0)
2619   {
2620     for(i = 0; i < currtablesize; i++)
2621       table[i].length = -1;
2622   }
2623   else
2624   {
2625     if(depth == maxdepth)
2626     {
2627       table[0].length = maxdepth + 1;
2628       table[0].value = node;
2629     }
2630     else
2631     {
2632       ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
2633                                 depth + 1, maxdepth);
2634       ret |= make_table_recurse(a, code, code->tree[node].branches[1],
2635                          table + currtablesize / 2, depth + 1, maxdepth);
2636     }
2637   }
2638   return ret;
2639 }
2640
2641 static int64_t
2642 expand(struct archive_read *a, int64_t end)
2643 {
2644   static const unsigned char lengthbases[] =
2645     {   0,   1,   2,   3,   4,   5,   6,
2646         7,   8,  10,  12,  14,  16,  20,
2647        24,  28,  32,  40,  48,  56,  64,
2648        80,  96, 112, 128, 160, 192, 224 };
2649   static const unsigned char lengthbits[] =
2650     { 0, 0, 0, 0, 0, 0, 0,
2651       0, 1, 1, 1, 1, 2, 2,
2652       2, 2, 3, 3, 3, 3, 4,
2653       4, 4, 4, 5, 5, 5, 5 };
2654   static const unsigned int offsetbases[] =
2655     {       0,       1,       2,       3,       4,       6,
2656             8,      12,      16,      24,      32,      48,
2657            64,      96,     128,     192,     256,     384,
2658           512,     768,    1024,    1536,    2048,    3072,
2659          4096,    6144,    8192,   12288,   16384,   24576,
2660         32768,   49152,   65536,   98304,  131072,  196608,
2661        262144,  327680,  393216,  458752,  524288,  589824,
2662        655360,  720896,  786432,  851968,  917504,  983040,
2663       1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2664       2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
2665   static const unsigned char offsetbits[] =
2666     {  0,  0,  0,  0,  1,  1,  2,  2,  3,  3,  4,  4,
2667        5,  5,  6,  6,  7,  7,  8,  8,  9,  9, 10, 10,
2668       11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
2669       16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2670       18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
2671   static const unsigned char shortbases[] =
2672     { 0, 4, 8, 16, 32, 64, 128, 192 };
2673   static const unsigned char shortbits[] =
2674     { 2, 2, 3, 4, 5, 6, 6, 6 };
2675
2676   int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
2677   unsigned char newfile;
2678   struct rar *rar = (struct rar *)(a->format->data);
2679   struct rar_br *br = &(rar->br);
2680
2681   if (rar->filterstart < end)
2682     end = rar->filterstart;
2683
2684   while (1)
2685   {
2686     if (rar->output_last_match &&
2687       lzss_position(&rar->lzss) + rar->lastlength <= end)
2688     {
2689       lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
2690       rar->output_last_match = 0;
2691     }
2692
2693     if(rar->is_ppmd_block || rar->output_last_match ||
2694       lzss_position(&rar->lzss) >= end)
2695       return lzss_position(&rar->lzss);
2696
2697     if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
2698       return (ARCHIVE_FATAL);
2699     rar->output_last_match = 0;
2700
2701     if (symbol < 256)
2702     {
2703       lzss_emit_literal(rar, symbol);
2704       continue;
2705     }
2706     else if (symbol == 256)
2707     {
2708       if (!rar_br_read_ahead(a, br, 1))
2709         goto truncated_data;
2710       newfile = !rar_br_bits(br, 1);
2711       rar_br_consume(br, 1);
2712
2713       if(newfile)
2714       {
2715         rar->start_new_block = 1;
2716         if (!rar_br_read_ahead(a, br, 1))
2717           goto truncated_data;
2718         rar->start_new_table = rar_br_bits(br, 1);
2719         rar_br_consume(br, 1);
2720         return lzss_position(&rar->lzss);
2721       }
2722       else
2723       {
2724         if (parse_codes(a) != ARCHIVE_OK)
2725           return (ARCHIVE_FATAL);
2726         continue;
2727       }
2728     }
2729     else if(symbol==257)
2730     {
2731       archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2732                         "Parsing filters is unsupported.");
2733       return (ARCHIVE_FAILED);
2734     }
2735     else if(symbol==258)
2736     {
2737       if(rar->lastlength == 0)
2738         continue;
2739
2740       offs = rar->lastoffset;
2741       len = rar->lastlength;
2742     }
2743     else if (symbol <= 262)
2744     {
2745       offsindex = symbol - 259;
2746       offs = rar->oldoffset[offsindex];
2747
2748       if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
2749         goto bad_data;
2750       if (lensymbol > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2751         goto bad_data;
2752       if (lensymbol > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2753         goto bad_data;
2754       len = lengthbases[lensymbol] + 2;
2755       if (lengthbits[lensymbol] > 0) {
2756         if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
2757           goto truncated_data;
2758         len += rar_br_bits(br, lengthbits[lensymbol]);
2759         rar_br_consume(br, lengthbits[lensymbol]);
2760       }
2761
2762       for (i = offsindex; i > 0; i--)
2763         rar->oldoffset[i] = rar->oldoffset[i-1];
2764       rar->oldoffset[0] = offs;
2765     }
2766     else if(symbol<=270)
2767     {
2768       offs = shortbases[symbol-263] + 1;
2769       if(shortbits[symbol-263] > 0) {
2770         if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
2771           goto truncated_data;
2772         offs += rar_br_bits(br, shortbits[symbol-263]);
2773         rar_br_consume(br, shortbits[symbol-263]);
2774       }
2775
2776       len = 2;
2777
2778       for(i = 3; i > 0; i--)
2779         rar->oldoffset[i] = rar->oldoffset[i-1];
2780       rar->oldoffset[0] = offs;
2781     }
2782     else
2783     {
2784       if (symbol-271 > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2785         goto bad_data;
2786       if (symbol-271 > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2787         goto bad_data;
2788       len = lengthbases[symbol-271]+3;
2789       if(lengthbits[symbol-271] > 0) {
2790         if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
2791           goto truncated_data;
2792         len += rar_br_bits(br, lengthbits[symbol-271]);
2793         rar_br_consume(br, lengthbits[symbol-271]);
2794       }
2795
2796       if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
2797         goto bad_data;
2798       if (offssymbol > (int)(sizeof(offsetbases)/sizeof(offsetbases[0])))
2799         goto bad_data;
2800       if (offssymbol > (int)(sizeof(offsetbits)/sizeof(offsetbits[0])))
2801         goto bad_data;
2802       offs = offsetbases[offssymbol]+1;
2803       if(offsetbits[offssymbol] > 0)
2804       {
2805         if(offssymbol > 9)
2806         {
2807           if(offsetbits[offssymbol] > 4) {
2808             if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
2809               goto truncated_data;
2810             offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
2811             rar_br_consume(br, offsetbits[offssymbol] - 4);
2812           }
2813
2814           if(rar->numlowoffsetrepeats > 0)
2815           {
2816             rar->numlowoffsetrepeats--;
2817             offs += rar->lastlowoffset;
2818           }
2819           else
2820           {
2821             if ((lowoffsetsymbol =
2822               read_next_symbol(a, &rar->lowoffsetcode)) < 0)
2823               return (ARCHIVE_FATAL);
2824             if(lowoffsetsymbol == 16)
2825             {
2826               rar->numlowoffsetrepeats = 15;
2827               offs += rar->lastlowoffset;
2828             }
2829             else
2830             {
2831               offs += lowoffsetsymbol;
2832               rar->lastlowoffset = lowoffsetsymbol;
2833             }
2834           }
2835         }
2836         else {
2837           if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
2838             goto truncated_data;
2839           offs += rar_br_bits(br, offsetbits[offssymbol]);
2840           rar_br_consume(br, offsetbits[offssymbol]);
2841         }
2842       }
2843
2844       if (offs >= 0x40000)
2845         len++;
2846       if (offs >= 0x2000)
2847         len++;
2848
2849       for(i = 3; i > 0; i--)
2850         rar->oldoffset[i] = rar->oldoffset[i-1];
2851       rar->oldoffset[0] = offs;
2852     }
2853
2854     rar->lastoffset = offs;
2855     rar->lastlength = len;
2856     rar->output_last_match = 1;
2857   }
2858 truncated_data:
2859   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2860                     "Truncated RAR file data");
2861   rar->valid = 0;
2862   return (ARCHIVE_FATAL);
2863 bad_data:
2864   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2865                     "Bad RAR file data");
2866   return (ARCHIVE_FATAL);
2867 }
2868
2869 static int
2870 copy_from_lzss_window(struct archive_read *a, const void **buffer,
2871                         int64_t startpos, int length)
2872 {
2873   int windowoffs, firstpart;
2874   struct rar *rar = (struct rar *)(a->format->data);
2875
2876   if (!rar->unp_buffer)
2877   {
2878     if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
2879     {
2880       archive_set_error(&a->archive, ENOMEM,
2881                         "Unable to allocate memory for uncompressed data.");
2882       return (ARCHIVE_FATAL);
2883     }
2884   }
2885
2886   windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
2887   if(windowoffs + length <= lzss_size(&rar->lzss))
2888     memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
2889            length);
2890   else
2891   {
2892     firstpart = lzss_size(&rar->lzss) - windowoffs;
2893     if (firstpart < 0) {
2894       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2895                         "Bad RAR file data");
2896       return (ARCHIVE_FATAL);
2897     }
2898     if (firstpart < length) {
2899       memcpy(&rar->unp_buffer[rar->unp_offset],
2900              &rar->lzss.window[windowoffs], firstpart);
2901       memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
2902              &rar->lzss.window[0], length - firstpart);
2903     } else
2904       memcpy(&rar->unp_buffer[rar->unp_offset],
2905              &rar->lzss.window[windowoffs], length);
2906   }
2907   rar->unp_offset += length;
2908   if (rar->unp_offset >= rar->unp_buffer_size)
2909     *buffer = rar->unp_buffer;
2910   else
2911     *buffer = NULL;
2912   return (ARCHIVE_OK);
2913 }
2914
2915 static const void *
2916 rar_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
2917 {
2918   struct rar *rar = (struct rar *)(a->format->data);
2919   const void *h = __archive_read_ahead(a, min, avail);
2920   int ret;
2921   if (avail)
2922   {
2923     if (a->archive.read_data_is_posix_read && *avail > (ssize_t)a->archive.read_data_requested)
2924       *avail = a->archive.read_data_requested;
2925     if (*avail > rar->bytes_remaining)
2926       *avail = (ssize_t)rar->bytes_remaining;
2927     if (*avail < 0)
2928       return NULL;
2929     else if (*avail == 0 && rar->main_flags & MHD_VOLUME &&
2930       rar->file_flags & FHD_SPLIT_AFTER)
2931     {
2932       ret = archive_read_format_rar_read_header(a, a->entry);
2933       if (ret == (ARCHIVE_EOF))
2934       {
2935         rar->has_endarc_header = 1;
2936         ret = archive_read_format_rar_read_header(a, a->entry);
2937       }
2938       if (ret != (ARCHIVE_OK))
2939         return NULL;
2940       return rar_read_ahead(a, min, avail);
2941     }
2942   }
2943   return h;
2944 }