]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/archive_read_support_format_rar.c
MFH r324148:
[FreeBSD/stable/10.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 *)calloc(sizeof(*rar), 1);
651   if (rar == NULL)
652   {
653     archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
654     return (ARCHIVE_FATAL);
655   }
656
657         /*
658          * Until enough data has been read, we cannot tell about
659          * any encrypted entries yet.
660          */
661         rar->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
662
663   r = __archive_read_register_format(a,
664                                      rar,
665                                      "rar",
666                                      archive_read_format_rar_bid,
667                                      archive_read_format_rar_options,
668                                      archive_read_format_rar_read_header,
669                                      archive_read_format_rar_read_data,
670                                      archive_read_format_rar_read_data_skip,
671                                      archive_read_format_rar_seek_data,
672                                      archive_read_format_rar_cleanup,
673                                      archive_read_support_format_rar_capabilities,
674                                      archive_read_format_rar_has_encrypted_entries);
675
676   if (r != ARCHIVE_OK)
677     free(rar);
678   return (r);
679 }
680
681 static int
682 archive_read_support_format_rar_capabilities(struct archive_read * a)
683 {
684         (void)a; /* UNUSED */
685         return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
686                         | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
687 }
688
689 static int
690 archive_read_format_rar_has_encrypted_entries(struct archive_read *_a)
691 {
692         if (_a && _a->format) {
693                 struct rar * rar = (struct rar *)_a->format->data;
694                 if (rar) {
695                         return rar->has_encrypted_entries;
696                 }
697         }
698         return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
699 }
700
701
702 static int
703 archive_read_format_rar_bid(struct archive_read *a, int best_bid)
704 {
705   const char *p;
706
707   /* If there's already a bid > 30, we'll never win. */
708   if (best_bid > 30)
709           return (-1);
710
711   if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
712     return (-1);
713
714   if (memcmp(p, RAR_SIGNATURE, 7) == 0)
715     return (30);
716
717   if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
718     /* This is a PE file */
719     ssize_t offset = 0x10000;
720     ssize_t window = 4096;
721     ssize_t bytes_avail;
722     while (offset + window <= (1024 * 128)) {
723       const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
724       if (buff == NULL) {
725         /* Remaining bytes are less than window. */
726         window >>= 1;
727         if (window < 0x40)
728           return (0);
729         continue;
730       }
731       p = buff + offset;
732       while (p + 7 < buff + bytes_avail) {
733         if (memcmp(p, RAR_SIGNATURE, 7) == 0)
734           return (30);
735         p += 0x10;
736       }
737       offset = p - buff;
738     }
739   }
740   return (0);
741 }
742
743 static int
744 skip_sfx(struct archive_read *a)
745 {
746   const void *h;
747   const char *p, *q;
748   size_t skip, total;
749   ssize_t bytes, window;
750
751   total = 0;
752   window = 4096;
753   while (total + window <= (1024 * 128)) {
754     h = __archive_read_ahead(a, window, &bytes);
755     if (h == NULL) {
756       /* Remaining bytes are less than window. */
757       window >>= 1;
758       if (window < 0x40)
759         goto fatal;
760       continue;
761     }
762     if (bytes < 0x40)
763       goto fatal;
764     p = h;
765     q = p + bytes;
766
767     /*
768      * Scan ahead until we find something that looks
769      * like the RAR header.
770      */
771     while (p + 7 < q) {
772       if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
773         skip = p - (const char *)h;
774         __archive_read_consume(a, skip);
775         return (ARCHIVE_OK);
776       }
777       p += 0x10;
778     }
779     skip = p - (const char *)h;
780     __archive_read_consume(a, skip);
781         total += skip;
782   }
783 fatal:
784   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
785       "Couldn't find out RAR header");
786   return (ARCHIVE_FATAL);
787 }
788
789 static int
790 archive_read_format_rar_options(struct archive_read *a,
791     const char *key, const char *val)
792 {
793   struct rar *rar;
794   int ret = ARCHIVE_FAILED;
795
796   rar = (struct rar *)(a->format->data);
797   if (strcmp(key, "hdrcharset")  == 0) {
798     if (val == NULL || val[0] == 0)
799       archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
800           "rar: hdrcharset option needs a character-set name");
801     else {
802       rar->opt_sconv =
803           archive_string_conversion_from_charset(
804               &a->archive, val, 0);
805       if (rar->opt_sconv != NULL)
806         ret = ARCHIVE_OK;
807       else
808         ret = ARCHIVE_FATAL;
809     }
810     return (ret);
811   }
812
813   /* Note: The "warn" return is just to inform the options
814    * supervisor that we didn't handle it.  It will generate
815    * a suitable error if no one used this option. */
816   return (ARCHIVE_WARN);
817 }
818
819 static int
820 archive_read_format_rar_read_header(struct archive_read *a,
821                                     struct archive_entry *entry)
822 {
823   const void *h;
824   const char *p;
825   struct rar *rar;
826   size_t skip;
827   char head_type;
828   int ret;
829   unsigned flags;
830   unsigned long crc32_expected;
831
832   a->archive.archive_format = ARCHIVE_FORMAT_RAR;
833   if (a->archive.archive_format_name == NULL)
834     a->archive.archive_format_name = "RAR";
835
836   rar = (struct rar *)(a->format->data);
837
838   /*
839    * It should be sufficient to call archive_read_next_header() for
840    * a reader to determine if an entry is encrypted or not. If the
841    * encryption of an entry is only detectable when calling
842    * archive_read_data(), so be it. We'll do the same check there
843    * as well.
844    */
845   if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
846           rar->has_encrypted_entries = 0;
847   }
848
849   /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
850   * this fails.
851   */
852   if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
853     return (ARCHIVE_EOF);
854
855   p = h;
856   if (rar->found_first_header == 0 &&
857      ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
858     /* This is an executable ? Must be self-extracting... */
859     ret = skip_sfx(a);
860     if (ret < ARCHIVE_WARN)
861       return (ret);
862   }
863   rar->found_first_header = 1;
864
865   while (1)
866   {
867     unsigned long crc32_val;
868
869     if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
870       return (ARCHIVE_FATAL);
871     p = h;
872
873     head_type = p[2];
874     switch(head_type)
875     {
876     case MARK_HEAD:
877       if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
878         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
879           "Invalid marker header");
880         return (ARCHIVE_FATAL);
881       }
882       __archive_read_consume(a, 7);
883       break;
884
885     case MAIN_HEAD:
886       rar->main_flags = archive_le16dec(p + 3);
887       skip = archive_le16dec(p + 5);
888       if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
889         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
890           "Invalid header size");
891         return (ARCHIVE_FATAL);
892       }
893       if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
894         return (ARCHIVE_FATAL);
895       p = h;
896       memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
897       memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
898              sizeof(rar->reserved2));
899       if (rar->main_flags & MHD_ENCRYPTVER) {
900         if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
901           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
902             "Invalid header size");
903           return (ARCHIVE_FATAL);
904         }
905         rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
906                             sizeof(rar->reserved2));
907       }
908
909       /* Main header is password encrypted, so we cannot read any
910          file names or any other info about files from the header. */
911       if (rar->main_flags & MHD_PASSWORD)
912       {
913         archive_entry_set_is_metadata_encrypted(entry, 1);
914         archive_entry_set_is_data_encrypted(entry, 1);
915         rar->has_encrypted_entries = 1;
916          archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
917                           "RAR encryption support unavailable.");
918         return (ARCHIVE_FATAL);
919       }
920
921       crc32_val = crc32(0, (const unsigned char *)p + 2, (unsigned)skip - 2);
922       if ((crc32_val & 0xffff) != archive_le16dec(p)) {
923         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
924           "Header CRC error");
925         return (ARCHIVE_FATAL);
926       }
927       __archive_read_consume(a, skip);
928       break;
929
930     case FILE_HEAD:
931       return read_header(a, entry, head_type);
932
933     case COMM_HEAD:
934     case AV_HEAD:
935     case SUB_HEAD:
936     case PROTECT_HEAD:
937     case SIGN_HEAD:
938     case ENDARC_HEAD:
939       flags = archive_le16dec(p + 3);
940       skip = archive_le16dec(p + 5);
941       if (skip < 7) {
942         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
943           "Invalid header size too small");
944         return (ARCHIVE_FATAL);
945       }
946       if (flags & HD_ADD_SIZE_PRESENT)
947       {
948         if (skip < 7 + 4) {
949           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
950             "Invalid header size too small");
951           return (ARCHIVE_FATAL);
952         }
953         if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
954           return (ARCHIVE_FATAL);
955         p = h;
956         skip += archive_le32dec(p + 7);
957       }
958
959       /* Skip over the 2-byte CRC at the beginning of the header. */
960       crc32_expected = archive_le16dec(p);
961       __archive_read_consume(a, 2);
962       skip -= 2;
963
964       /* Skim the entire header and compute the CRC. */
965       crc32_val = 0;
966       while (skip > 0) {
967               size_t to_read = skip;
968               ssize_t did_read;
969               if (to_read > 32 * 1024) {
970                       to_read = 32 * 1024;
971               }
972               if ((h = __archive_read_ahead(a, to_read, &did_read)) == NULL) {
973                       return (ARCHIVE_FATAL);
974               }
975               p = h;
976               crc32_val = crc32(crc32_val, (const unsigned char *)p, (unsigned)did_read);
977               __archive_read_consume(a, did_read);
978               skip -= did_read;
979       }
980       if ((crc32_val & 0xffff) != crc32_expected) {
981               archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
982                   "Header CRC error");
983               return (ARCHIVE_FATAL);
984       }
985       if (head_type == ENDARC_HEAD)
986               return (ARCHIVE_EOF);
987       break;
988
989     case NEWSUB_HEAD:
990       if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
991         return ret;
992       break;
993
994     default:
995       archive_set_error(&a->archive,  ARCHIVE_ERRNO_FILE_FORMAT,
996                         "Bad RAR file");
997       return (ARCHIVE_FATAL);
998     }
999   }
1000 }
1001
1002 static int
1003 archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
1004                                   size_t *size, int64_t *offset)
1005 {
1006   struct rar *rar = (struct rar *)(a->format->data);
1007   int ret;
1008
1009   if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
1010           rar->has_encrypted_entries = 0;
1011   }
1012
1013   if (rar->bytes_unconsumed > 0) {
1014       /* Consume as much as the decompressor actually used. */
1015       __archive_read_consume(a, rar->bytes_unconsumed);
1016       rar->bytes_unconsumed = 0;
1017   }
1018
1019   *buff = NULL;
1020   if (rar->entry_eof || rar->offset_seek >= rar->unp_size) {
1021     *size = 0;
1022     *offset = rar->offset;
1023     if (*offset < rar->unp_size)
1024       *offset = rar->unp_size;
1025     return (ARCHIVE_EOF);
1026   }
1027
1028   switch (rar->compression_method)
1029   {
1030   case COMPRESS_METHOD_STORE:
1031     ret = read_data_stored(a, buff, size, offset);
1032     break;
1033
1034   case COMPRESS_METHOD_FASTEST:
1035   case COMPRESS_METHOD_FAST:
1036   case COMPRESS_METHOD_NORMAL:
1037   case COMPRESS_METHOD_GOOD:
1038   case COMPRESS_METHOD_BEST:
1039     ret = read_data_compressed(a, buff, size, offset);
1040     if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN)
1041       __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1042     break;
1043
1044   default:
1045     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1046                       "Unsupported compression method for RAR file.");
1047     ret = ARCHIVE_FATAL;
1048     break;
1049   }
1050   return (ret);
1051 }
1052
1053 static int
1054 archive_read_format_rar_read_data_skip(struct archive_read *a)
1055 {
1056   struct rar *rar;
1057   int64_t bytes_skipped;
1058   int ret;
1059
1060   rar = (struct rar *)(a->format->data);
1061
1062   if (rar->bytes_unconsumed > 0) {
1063       /* Consume as much as the decompressor actually used. */
1064       __archive_read_consume(a, rar->bytes_unconsumed);
1065       rar->bytes_unconsumed = 0;
1066   }
1067
1068   if (rar->bytes_remaining > 0) {
1069     bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
1070     if (bytes_skipped < 0)
1071       return (ARCHIVE_FATAL);
1072   }
1073
1074   /* Compressed data to skip must be read from each header in a multivolume
1075    * archive.
1076    */
1077   if (rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER)
1078   {
1079     ret = archive_read_format_rar_read_header(a, a->entry);
1080     if (ret == (ARCHIVE_EOF))
1081       ret = archive_read_format_rar_read_header(a, a->entry);
1082     if (ret != (ARCHIVE_OK))
1083       return ret;
1084     return archive_read_format_rar_read_data_skip(a);
1085   }
1086
1087   return (ARCHIVE_OK);
1088 }
1089
1090 static int64_t
1091 archive_read_format_rar_seek_data(struct archive_read *a, int64_t offset,
1092     int whence)
1093 {
1094   int64_t client_offset, ret;
1095   unsigned int i;
1096   struct rar *rar = (struct rar *)(a->format->data);
1097
1098   if (rar->compression_method == COMPRESS_METHOD_STORE)
1099   {
1100     /* Modify the offset for use with SEEK_SET */
1101     switch (whence)
1102     {
1103       case SEEK_CUR:
1104         client_offset = rar->offset_seek;
1105         break;
1106       case SEEK_END:
1107         client_offset = rar->unp_size;
1108         break;
1109       case SEEK_SET:
1110       default:
1111         client_offset = 0;
1112     }
1113     client_offset += offset;
1114     if (client_offset < 0)
1115     {
1116       /* Can't seek past beginning of data block */
1117       return -1;
1118     }
1119     else if (client_offset > rar->unp_size)
1120     {
1121       /*
1122        * Set the returned offset but only seek to the end of
1123        * the data block.
1124        */
1125       rar->offset_seek = client_offset;
1126       client_offset = rar->unp_size;
1127     }
1128
1129     client_offset += rar->dbo[0].start_offset;
1130     i = 0;
1131     while (i < rar->cursor)
1132     {
1133       i++;
1134       client_offset += rar->dbo[i].start_offset - rar->dbo[i-1].end_offset;
1135     }
1136     if (rar->main_flags & MHD_VOLUME)
1137     {
1138       /* Find the appropriate offset among the multivolume archive */
1139       while (1)
1140       {
1141         if (client_offset < rar->dbo[rar->cursor].start_offset &&
1142           rar->file_flags & FHD_SPLIT_BEFORE)
1143         {
1144           /* Search backwards for the correct data block */
1145           if (rar->cursor == 0)
1146           {
1147             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1148               "Attempt to seek past beginning of RAR data block");
1149             return (ARCHIVE_FAILED);
1150           }
1151           rar->cursor--;
1152           client_offset -= rar->dbo[rar->cursor+1].start_offset -
1153             rar->dbo[rar->cursor].end_offset;
1154           if (client_offset < rar->dbo[rar->cursor].start_offset)
1155             continue;
1156           ret = __archive_read_seek(a, rar->dbo[rar->cursor].start_offset -
1157             rar->dbo[rar->cursor].header_size, SEEK_SET);
1158           if (ret < (ARCHIVE_OK))
1159             return ret;
1160           ret = archive_read_format_rar_read_header(a, a->entry);
1161           if (ret != (ARCHIVE_OK))
1162           {
1163             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1164               "Error during seek of RAR file");
1165             return (ARCHIVE_FAILED);
1166           }
1167           rar->cursor--;
1168           break;
1169         }
1170         else if (client_offset > rar->dbo[rar->cursor].end_offset &&
1171           rar->file_flags & FHD_SPLIT_AFTER)
1172         {
1173           /* Search forward for the correct data block */
1174           rar->cursor++;
1175           if (rar->cursor < rar->nodes &&
1176             client_offset > rar->dbo[rar->cursor].end_offset)
1177           {
1178             client_offset += rar->dbo[rar->cursor].start_offset -
1179               rar->dbo[rar->cursor-1].end_offset;
1180             continue;
1181           }
1182           rar->cursor--;
1183           ret = __archive_read_seek(a, rar->dbo[rar->cursor].end_offset,
1184                                     SEEK_SET);
1185           if (ret < (ARCHIVE_OK))
1186             return ret;
1187           ret = archive_read_format_rar_read_header(a, a->entry);
1188           if (ret == (ARCHIVE_EOF))
1189           {
1190             rar->has_endarc_header = 1;
1191             ret = archive_read_format_rar_read_header(a, a->entry);
1192           }
1193           if (ret != (ARCHIVE_OK))
1194           {
1195             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1196               "Error during seek of RAR file");
1197             return (ARCHIVE_FAILED);
1198           }
1199           client_offset += rar->dbo[rar->cursor].start_offset -
1200             rar->dbo[rar->cursor-1].end_offset;
1201           continue;
1202         }
1203         break;
1204       }
1205     }
1206
1207     ret = __archive_read_seek(a, client_offset, SEEK_SET);
1208     if (ret < (ARCHIVE_OK))
1209       return ret;
1210     rar->bytes_remaining = rar->dbo[rar->cursor].end_offset - ret;
1211     i = rar->cursor;
1212     while (i > 0)
1213     {
1214       i--;
1215       ret -= rar->dbo[i+1].start_offset - rar->dbo[i].end_offset;
1216     }
1217     ret -= rar->dbo[0].start_offset;
1218
1219     /* Always restart reading the file after a seek */
1220     __archive_reset_read_data(&a->archive);
1221
1222     rar->bytes_unconsumed = 0;
1223     rar->offset = 0;
1224
1225     /*
1226      * If a seek past the end of file was requested, return the requested
1227      * offset.
1228      */
1229     if (ret == rar->unp_size && rar->offset_seek > rar->unp_size)
1230       return rar->offset_seek;
1231
1232     /* Return the new offset */
1233     rar->offset_seek = ret;
1234     return rar->offset_seek;
1235   }
1236   else
1237   {
1238     archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1239       "Seeking of compressed RAR files is unsupported");
1240   }
1241   return (ARCHIVE_FAILED);
1242 }
1243
1244 static int
1245 archive_read_format_rar_cleanup(struct archive_read *a)
1246 {
1247   struct rar *rar;
1248
1249   rar = (struct rar *)(a->format->data);
1250   free_codes(a);
1251   free(rar->filename);
1252   free(rar->filename_save);
1253   free(rar->dbo);
1254   free(rar->unp_buffer);
1255   free(rar->lzss.window);
1256   __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1257   free(rar);
1258   (a->format->data) = NULL;
1259   return (ARCHIVE_OK);
1260 }
1261
1262 static int
1263 read_header(struct archive_read *a, struct archive_entry *entry,
1264             char head_type)
1265 {
1266   const void *h;
1267   const char *p, *endp;
1268   struct rar *rar;
1269   struct rar_header rar_header;
1270   struct rar_file_header file_header;
1271   int64_t header_size;
1272   unsigned filename_size, end;
1273   char *filename;
1274   char *strp;
1275   char packed_size[8];
1276   char unp_size[8];
1277   int ttime;
1278   struct archive_string_conv *sconv, *fn_sconv;
1279   unsigned long crc32_val;
1280   int ret = (ARCHIVE_OK), ret2;
1281
1282   rar = (struct rar *)(a->format->data);
1283
1284   /* Setup a string conversion object for non-rar-unicode filenames. */
1285   sconv = rar->opt_sconv;
1286   if (sconv == NULL) {
1287     if (!rar->init_default_conversion) {
1288       rar->sconv_default =
1289           archive_string_default_conversion_for_read(
1290             &(a->archive));
1291       rar->init_default_conversion = 1;
1292     }
1293     sconv = rar->sconv_default;
1294   }
1295
1296
1297   if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
1298     return (ARCHIVE_FATAL);
1299   p = h;
1300   memcpy(&rar_header, p, sizeof(rar_header));
1301   rar->file_flags = archive_le16dec(rar_header.flags);
1302   header_size = archive_le16dec(rar_header.size);
1303   if (header_size < (int64_t)sizeof(file_header) + 7) {
1304     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1305       "Invalid header size");
1306     return (ARCHIVE_FATAL);
1307   }
1308   crc32_val = crc32(0, (const unsigned char *)p + 2, 7 - 2);
1309   __archive_read_consume(a, 7);
1310
1311   if (!(rar->file_flags & FHD_SOLID))
1312   {
1313     rar->compression_method = 0;
1314     rar->packed_size = 0;
1315     rar->unp_size = 0;
1316     rar->mtime = 0;
1317     rar->ctime = 0;
1318     rar->atime = 0;
1319     rar->arctime = 0;
1320     rar->mode = 0;
1321     memset(&rar->salt, 0, sizeof(rar->salt));
1322     rar->atime = 0;
1323     rar->ansec = 0;
1324     rar->ctime = 0;
1325     rar->cnsec = 0;
1326     rar->mtime = 0;
1327     rar->mnsec = 0;
1328     rar->arctime = 0;
1329     rar->arcnsec = 0;
1330   }
1331   else
1332   {
1333     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1334                       "RAR solid archive support unavailable.");
1335     return (ARCHIVE_FATAL);
1336   }
1337
1338   if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1339     return (ARCHIVE_FATAL);
1340
1341   /* File Header CRC check. */
1342   crc32_val = crc32(crc32_val, h, (unsigned)(header_size - 7));
1343   if ((crc32_val & 0xffff) != archive_le16dec(rar_header.crc)) {
1344     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1345       "Header CRC error");
1346     return (ARCHIVE_FATAL);
1347   }
1348   /* If no CRC error, Go on parsing File Header. */
1349   p = h;
1350   endp = p + header_size - 7;
1351   memcpy(&file_header, p, sizeof(file_header));
1352   p += sizeof(file_header);
1353
1354   rar->compression_method = file_header.method;
1355
1356   ttime = archive_le32dec(file_header.file_time);
1357   rar->mtime = get_time(ttime);
1358
1359   rar->file_crc = archive_le32dec(file_header.file_crc);
1360
1361   if (rar->file_flags & FHD_PASSWORD)
1362   {
1363         archive_entry_set_is_data_encrypted(entry, 1);
1364         rar->has_encrypted_entries = 1;
1365     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1366                       "RAR encryption support unavailable.");
1367     /* Since it is only the data part itself that is encrypted we can at least
1368        extract information about the currently processed entry and don't need
1369        to return ARCHIVE_FATAL here. */
1370     /*return (ARCHIVE_FATAL);*/
1371   }
1372
1373   if (rar->file_flags & FHD_LARGE)
1374   {
1375     memcpy(packed_size, file_header.pack_size, 4);
1376     memcpy(packed_size + 4, p, 4); /* High pack size */
1377     p += 4;
1378     memcpy(unp_size, file_header.unp_size, 4);
1379     memcpy(unp_size + 4, p, 4); /* High unpack size */
1380     p += 4;
1381     rar->packed_size = archive_le64dec(&packed_size);
1382     rar->unp_size = archive_le64dec(&unp_size);
1383   }
1384   else
1385   {
1386     rar->packed_size = archive_le32dec(file_header.pack_size);
1387     rar->unp_size = archive_le32dec(file_header.unp_size);
1388   }
1389
1390   if (rar->packed_size < 0 || rar->unp_size < 0)
1391   {
1392     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1393                       "Invalid sizes specified.");
1394     return (ARCHIVE_FATAL);
1395   }
1396
1397   rar->bytes_remaining = rar->packed_size;
1398
1399   /* TODO: RARv3 subblocks contain comments. For now the complete block is
1400    * consumed at the end.
1401    */
1402   if (head_type == NEWSUB_HEAD) {
1403     size_t distance = p - (const char *)h;
1404     header_size += rar->packed_size;
1405     /* Make sure we have the extended data. */
1406     if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1407         return (ARCHIVE_FATAL);
1408     p = h;
1409     endp = p + header_size - 7;
1410     p += distance;
1411   }
1412
1413   filename_size = archive_le16dec(file_header.name_size);
1414   if (p + filename_size > endp) {
1415     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1416       "Invalid filename size");
1417     return (ARCHIVE_FATAL);
1418   }
1419   if (rar->filename_allocated < filename_size * 2 + 2) {
1420     char *newptr;
1421     size_t newsize = filename_size * 2 + 2;
1422     newptr = realloc(rar->filename, newsize);
1423     if (newptr == NULL) {
1424       archive_set_error(&a->archive, ENOMEM,
1425                         "Couldn't allocate memory.");
1426       return (ARCHIVE_FATAL);
1427     }
1428     rar->filename = newptr;
1429     rar->filename_allocated = newsize;
1430   }
1431   filename = rar->filename;
1432   memcpy(filename, p, filename_size);
1433   filename[filename_size] = '\0';
1434   if (rar->file_flags & FHD_UNICODE)
1435   {
1436     if (filename_size != strlen(filename))
1437     {
1438       unsigned char highbyte, flagbits, flagbyte;
1439       unsigned fn_end, offset;
1440
1441       end = filename_size;
1442       fn_end = filename_size * 2;
1443       filename_size = 0;
1444       offset = (unsigned)strlen(filename) + 1;
1445       highbyte = *(p + offset++);
1446       flagbits = 0;
1447       flagbyte = 0;
1448       while (offset < end && filename_size < fn_end)
1449       {
1450         if (!flagbits)
1451         {
1452           flagbyte = *(p + offset++);
1453           flagbits = 8;
1454         }
1455
1456         flagbits -= 2;
1457         switch((flagbyte >> flagbits) & 3)
1458         {
1459           case 0:
1460             filename[filename_size++] = '\0';
1461             filename[filename_size++] = *(p + offset++);
1462             break;
1463           case 1:
1464             filename[filename_size++] = highbyte;
1465             filename[filename_size++] = *(p + offset++);
1466             break;
1467           case 2:
1468             filename[filename_size++] = *(p + offset + 1);
1469             filename[filename_size++] = *(p + offset);
1470             offset += 2;
1471             break;
1472           case 3:
1473           {
1474             char extra, high;
1475             uint8_t length = *(p + offset++);
1476
1477             if (length & 0x80) {
1478               extra = *(p + offset++);
1479               high = (char)highbyte;
1480             } else
1481               extra = high = 0;
1482             length = (length & 0x7f) + 2;
1483             while (length && filename_size < fn_end) {
1484               unsigned cp = filename_size >> 1;
1485               filename[filename_size++] = high;
1486               filename[filename_size++] = p[cp] + extra;
1487               length--;
1488             }
1489           }
1490           break;
1491         }
1492       }
1493       if (filename_size > fn_end) {
1494         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1495           "Invalid filename");
1496         return (ARCHIVE_FATAL);
1497       }
1498       filename[filename_size++] = '\0';
1499       /*
1500        * Do not increment filename_size here as the computations below
1501        * add the space for the terminating NUL explicitly.
1502        */
1503       filename[filename_size] = '\0';
1504
1505       /* Decoded unicode form is UTF-16BE, so we have to update a string
1506        * conversion object for it. */
1507       if (rar->sconv_utf16be == NULL) {
1508         rar->sconv_utf16be = archive_string_conversion_from_charset(
1509            &a->archive, "UTF-16BE", 1);
1510         if (rar->sconv_utf16be == NULL)
1511           return (ARCHIVE_FATAL);
1512       }
1513       fn_sconv = rar->sconv_utf16be;
1514
1515       strp = filename;
1516       while (memcmp(strp, "\x00\x00", 2))
1517       {
1518         if (!memcmp(strp, "\x00\\", 2))
1519           *(strp + 1) = '/';
1520         strp += 2;
1521       }
1522       p += offset;
1523     } else {
1524       /*
1525        * If FHD_UNICODE is set but no unicode data, this file name form
1526        * is UTF-8, so we have to update a string conversion object for
1527        * it accordingly.
1528        */
1529       if (rar->sconv_utf8 == NULL) {
1530         rar->sconv_utf8 = archive_string_conversion_from_charset(
1531            &a->archive, "UTF-8", 1);
1532         if (rar->sconv_utf8 == NULL)
1533           return (ARCHIVE_FATAL);
1534       }
1535       fn_sconv = rar->sconv_utf8;
1536       while ((strp = strchr(filename, '\\')) != NULL)
1537         *strp = '/';
1538       p += filename_size;
1539     }
1540   }
1541   else
1542   {
1543     fn_sconv = sconv;
1544     while ((strp = strchr(filename, '\\')) != NULL)
1545       *strp = '/';
1546     p += filename_size;
1547   }
1548
1549   /* Split file in multivolume RAR. No more need to process header. */
1550   if (rar->filename_save &&
1551     filename_size == rar->filename_save_size &&
1552     !memcmp(rar->filename, rar->filename_save, filename_size + 1))
1553   {
1554     __archive_read_consume(a, header_size - 7);
1555     rar->cursor++;
1556     if (rar->cursor >= rar->nodes)
1557     {
1558       rar->nodes++;
1559       if ((rar->dbo =
1560         realloc(rar->dbo, sizeof(*rar->dbo) * rar->nodes)) == NULL)
1561       {
1562         archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1563         return (ARCHIVE_FATAL);
1564       }
1565       rar->dbo[rar->cursor].header_size = header_size;
1566       rar->dbo[rar->cursor].start_offset = -1;
1567       rar->dbo[rar->cursor].end_offset = -1;
1568     }
1569     if (rar->dbo[rar->cursor].start_offset < 0)
1570     {
1571       rar->dbo[rar->cursor].start_offset = a->filter->position;
1572       rar->dbo[rar->cursor].end_offset = rar->dbo[rar->cursor].start_offset +
1573         rar->packed_size;
1574     }
1575     return ret;
1576   }
1577
1578   rar->filename_save = (char*)realloc(rar->filename_save,
1579                                       filename_size + 1);
1580   memcpy(rar->filename_save, rar->filename, filename_size + 1);
1581   rar->filename_save_size = filename_size;
1582
1583   /* Set info for seeking */
1584   free(rar->dbo);
1585   if ((rar->dbo = calloc(1, sizeof(*rar->dbo))) == NULL)
1586   {
1587     archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1588     return (ARCHIVE_FATAL);
1589   }
1590   rar->dbo[0].header_size = header_size;
1591   rar->dbo[0].start_offset = -1;
1592   rar->dbo[0].end_offset = -1;
1593   rar->cursor = 0;
1594   rar->nodes = 1;
1595
1596   if (rar->file_flags & FHD_SALT)
1597   {
1598     if (p + 8 > endp) {
1599       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1600         "Invalid header size");
1601       return (ARCHIVE_FATAL);
1602     }
1603     memcpy(rar->salt, p, 8);
1604     p += 8;
1605   }
1606
1607   if (rar->file_flags & FHD_EXTTIME) {
1608     if (read_exttime(p, rar, endp) < 0) {
1609       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1610         "Invalid header size");
1611       return (ARCHIVE_FATAL);
1612     }
1613   }
1614
1615   __archive_read_consume(a, header_size - 7);
1616   rar->dbo[0].start_offset = a->filter->position;
1617   rar->dbo[0].end_offset = rar->dbo[0].start_offset + rar->packed_size;
1618
1619   switch(file_header.host_os)
1620   {
1621   case OS_MSDOS:
1622   case OS_OS2:
1623   case OS_WIN32:
1624     rar->mode = archive_le32dec(file_header.file_attr);
1625     if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
1626       rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
1627     else
1628       rar->mode = AE_IFREG;
1629     rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1630     break;
1631
1632   case OS_UNIX:
1633   case OS_MAC_OS:
1634   case OS_BEOS:
1635     rar->mode = archive_le32dec(file_header.file_attr);
1636     break;
1637
1638   default:
1639     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1640                       "Unknown file attributes from RAR file's host OS");
1641     return (ARCHIVE_FATAL);
1642   }
1643
1644   rar->bytes_uncopied = rar->bytes_unconsumed = 0;
1645   rar->lzss.position = rar->offset = 0;
1646   rar->offset_seek = 0;
1647   rar->dictionary_size = 0;
1648   rar->offset_outgoing = 0;
1649   rar->br.cache_avail = 0;
1650   rar->br.avail_in = 0;
1651   rar->crc_calculated = 0;
1652   rar->entry_eof = 0;
1653   rar->valid = 1;
1654   rar->is_ppmd_block = 0;
1655   rar->start_new_table = 1;
1656   free(rar->unp_buffer);
1657   rar->unp_buffer = NULL;
1658   rar->unp_offset = 0;
1659   rar->unp_buffer_size = UNP_BUFFER_SIZE;
1660   memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1661   __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1662   rar->ppmd_valid = rar->ppmd_eod = 0;
1663
1664   /* Don't set any archive entries for non-file header types */
1665   if (head_type == NEWSUB_HEAD)
1666     return ret;
1667
1668   archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
1669   archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
1670   archive_entry_set_atime(entry, rar->atime, rar->ansec);
1671   archive_entry_set_size(entry, rar->unp_size);
1672   archive_entry_set_mode(entry, rar->mode);
1673
1674   if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
1675   {
1676     if (errno == ENOMEM)
1677     {
1678       archive_set_error(&a->archive, ENOMEM,
1679                         "Can't allocate memory for Pathname");
1680       return (ARCHIVE_FATAL);
1681     }
1682     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1683                       "Pathname cannot be converted from %s to current locale.",
1684                       archive_string_conversion_charset_name(fn_sconv));
1685     ret = (ARCHIVE_WARN);
1686   }
1687
1688   if (((rar->mode) & AE_IFMT) == AE_IFLNK)
1689   {
1690     /* Make sure a symbolic-link file does not have its body. */
1691     rar->bytes_remaining = 0;
1692     archive_entry_set_size(entry, 0);
1693
1694     /* Read a symbolic-link name. */
1695     if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
1696       return ret2;
1697     if (ret > ret2)
1698       ret = ret2;
1699   }
1700
1701   if (rar->bytes_remaining == 0)
1702     rar->entry_eof = 1;
1703
1704   return ret;
1705 }
1706
1707 static time_t
1708 get_time(int ttime)
1709 {
1710   struct tm tm;
1711   tm.tm_sec = 2 * (ttime & 0x1f);
1712   tm.tm_min = (ttime >> 5) & 0x3f;
1713   tm.tm_hour = (ttime >> 11) & 0x1f;
1714   tm.tm_mday = (ttime >> 16) & 0x1f;
1715   tm.tm_mon = ((ttime >> 21) & 0x0f) - 1;
1716   tm.tm_year = ((ttime >> 25) & 0x7f) + 80;
1717   tm.tm_isdst = -1;
1718   return mktime(&tm);
1719 }
1720
1721 static int
1722 read_exttime(const char *p, struct rar *rar, const char *endp)
1723 {
1724   unsigned rmode, flags, rem, j, count;
1725   int ttime, i;
1726   struct tm *tm;
1727   time_t t;
1728   long nsec;
1729
1730   if (p + 2 > endp)
1731     return (-1);
1732   flags = archive_le16dec(p);
1733   p += 2;
1734
1735   for (i = 3; i >= 0; i--)
1736   {
1737     t = 0;
1738     if (i == 3)
1739       t = rar->mtime;
1740     rmode = flags >> i * 4;
1741     if (rmode & 8)
1742     {
1743       if (!t)
1744       {
1745         if (p + 4 > endp)
1746           return (-1);
1747         ttime = archive_le32dec(p);
1748         t = get_time(ttime);
1749         p += 4;
1750       }
1751       rem = 0;
1752       count = rmode & 3;
1753       if (p + count > endp)
1754         return (-1);
1755       for (j = 0; j < count; j++)
1756       {
1757         rem = (((unsigned)(unsigned char)*p) << 16) | (rem >> 8);
1758         p++;
1759       }
1760       tm = localtime(&t);
1761       nsec = tm->tm_sec + rem / NS_UNIT;
1762       if (rmode & 4)
1763       {
1764         tm->tm_sec++;
1765         t = mktime(tm);
1766       }
1767       if (i == 3)
1768       {
1769         rar->mtime = t;
1770         rar->mnsec = nsec;
1771       }
1772       else if (i == 2)
1773       {
1774         rar->ctime = t;
1775         rar->cnsec = nsec;
1776       }
1777       else if (i == 1)
1778       {
1779         rar->atime = t;
1780         rar->ansec = nsec;
1781       }
1782       else
1783       {
1784         rar->arctime = t;
1785         rar->arcnsec = nsec;
1786       }
1787     }
1788   }
1789   return (0);
1790 }
1791
1792 static int
1793 read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
1794                     struct archive_string_conv *sconv)
1795 {
1796   const void *h;
1797   const char *p;
1798   struct rar *rar;
1799   int ret = (ARCHIVE_OK);
1800
1801   rar = (struct rar *)(a->format->data);
1802   if ((h = rar_read_ahead(a, (size_t)rar->packed_size, NULL)) == NULL)
1803     return (ARCHIVE_FATAL);
1804   p = h;
1805
1806   if (archive_entry_copy_symlink_l(entry,
1807       p, (size_t)rar->packed_size, sconv))
1808   {
1809     if (errno == ENOMEM)
1810     {
1811       archive_set_error(&a->archive, ENOMEM,
1812                         "Can't allocate memory for link");
1813       return (ARCHIVE_FATAL);
1814     }
1815     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1816                       "link cannot be converted from %s to current locale.",
1817                       archive_string_conversion_charset_name(sconv));
1818     ret = (ARCHIVE_WARN);
1819   }
1820   __archive_read_consume(a, rar->packed_size);
1821   return ret;
1822 }
1823
1824 static int
1825 read_data_stored(struct archive_read *a, const void **buff, size_t *size,
1826                  int64_t *offset)
1827 {
1828   struct rar *rar;
1829   ssize_t bytes_avail;
1830
1831   rar = (struct rar *)(a->format->data);
1832   if (rar->bytes_remaining == 0 &&
1833     !(rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER))
1834   {
1835     *buff = NULL;
1836     *size = 0;
1837     *offset = rar->offset;
1838     if (rar->file_crc != rar->crc_calculated) {
1839       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1840                         "File CRC error");
1841       return (ARCHIVE_FATAL);
1842     }
1843     rar->entry_eof = 1;
1844     return (ARCHIVE_EOF);
1845   }
1846
1847   *buff = rar_read_ahead(a, 1, &bytes_avail);
1848   if (bytes_avail <= 0)
1849   {
1850     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1851                       "Truncated RAR file data");
1852     return (ARCHIVE_FATAL);
1853   }
1854
1855   *size = bytes_avail;
1856   *offset = rar->offset;
1857   rar->offset += bytes_avail;
1858   rar->offset_seek += bytes_avail;
1859   rar->bytes_remaining -= bytes_avail;
1860   rar->bytes_unconsumed = bytes_avail;
1861   /* Calculate File CRC. */
1862   rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1863     (unsigned)bytes_avail);
1864   return (ARCHIVE_OK);
1865 }
1866
1867 static int
1868 read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
1869                int64_t *offset)
1870 {
1871   struct rar *rar;
1872   int64_t start, end, actualend;
1873   size_t bs;
1874   int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
1875
1876   rar = (struct rar *)(a->format->data);
1877
1878   do {
1879     if (!rar->valid)
1880       return (ARCHIVE_FATAL);
1881     if (rar->ppmd_eod ||
1882        (rar->dictionary_size && rar->offset >= rar->unp_size))
1883     {
1884       if (rar->unp_offset > 0) {
1885         /*
1886          * We have unprocessed extracted data. write it out.
1887          */
1888         *buff = rar->unp_buffer;
1889         *size = rar->unp_offset;
1890         *offset = rar->offset_outgoing;
1891         rar->offset_outgoing += *size;
1892         /* Calculate File CRC. */
1893         rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1894           (unsigned)*size);
1895         rar->unp_offset = 0;
1896         return (ARCHIVE_OK);
1897       }
1898       *buff = NULL;
1899       *size = 0;
1900       *offset = rar->offset;
1901       if (rar->file_crc != rar->crc_calculated) {
1902         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1903                           "File CRC error");
1904         return (ARCHIVE_FATAL);
1905       }
1906       rar->entry_eof = 1;
1907       return (ARCHIVE_EOF);
1908     }
1909
1910     if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
1911     {
1912       if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
1913         bs = rar->unp_buffer_size - rar->unp_offset;
1914       else
1915         bs = (size_t)rar->bytes_uncopied;
1916       ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
1917       if (ret != ARCHIVE_OK)
1918         return (ret);
1919       rar->offset += bs;
1920       rar->bytes_uncopied -= bs;
1921       if (*buff != NULL) {
1922         rar->unp_offset = 0;
1923         *size = rar->unp_buffer_size;
1924         *offset = rar->offset_outgoing;
1925         rar->offset_outgoing += *size;
1926         /* Calculate File CRC. */
1927         rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1928           (unsigned)*size);
1929         return (ret);
1930       }
1931       continue;
1932     }
1933
1934     if (!rar->br.next_in &&
1935       (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
1936       return (ret);
1937     if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
1938       return (ret);
1939
1940     if (rar->is_ppmd_block)
1941     {
1942       if ((sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1943         &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1944       {
1945         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1946                           "Invalid symbol");
1947         return (ARCHIVE_FATAL);
1948       }
1949       if(sym != rar->ppmd_escape)
1950       {
1951         lzss_emit_literal(rar, sym);
1952         rar->bytes_uncopied++;
1953       }
1954       else
1955       {
1956         if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1957           &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1958         {
1959           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1960                             "Invalid symbol");
1961           return (ARCHIVE_FATAL);
1962         }
1963
1964         switch(code)
1965         {
1966           case 0:
1967             rar->start_new_table = 1;
1968             return read_data_compressed(a, buff, size, offset);
1969
1970           case 2:
1971             rar->ppmd_eod = 1;/* End Of ppmd Data. */
1972             continue;
1973
1974           case 3:
1975             archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1976                               "Parsing filters is unsupported.");
1977             return (ARCHIVE_FAILED);
1978
1979           case 4:
1980             lzss_offset = 0;
1981             for (i = 2; i >= 0; i--)
1982             {
1983               if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1984                 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1985               {
1986                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1987                                   "Invalid symbol");
1988                 return (ARCHIVE_FATAL);
1989               }
1990               lzss_offset |= code << (i * 8);
1991             }
1992             if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1993               &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1994             {
1995               archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1996                                 "Invalid symbol");
1997               return (ARCHIVE_FATAL);
1998             }
1999             lzss_emit_match(rar, lzss_offset + 2, length + 32);
2000             rar->bytes_uncopied += length + 32;
2001             break;
2002
2003           case 5:
2004             if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2005               &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2006             {
2007               archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2008                                 "Invalid symbol");
2009               return (ARCHIVE_FATAL);
2010             }
2011             lzss_emit_match(rar, 1, length + 4);
2012             rar->bytes_uncopied += length + 4;
2013             break;
2014
2015          default:
2016            lzss_emit_literal(rar, sym);
2017            rar->bytes_uncopied++;
2018         }
2019       }
2020     }
2021     else
2022     {
2023       start = rar->offset;
2024       end = start + rar->dictionary_size;
2025       rar->filterstart = INT64_MAX;
2026
2027       if ((actualend = expand(a, end)) < 0)
2028         return ((int)actualend);
2029
2030       rar->bytes_uncopied = actualend - start;
2031       if (rar->bytes_uncopied == 0) {
2032           /* Broken RAR files cause this case.
2033           * NOTE: If this case were possible on a normal RAR file
2034           * we would find out where it was actually bad and
2035           * what we would do to solve it. */
2036           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2037                             "Internal error extracting RAR file");
2038           return (ARCHIVE_FATAL);
2039       }
2040     }
2041     if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
2042       bs = rar->unp_buffer_size - rar->unp_offset;
2043     else
2044       bs = (size_t)rar->bytes_uncopied;
2045     ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
2046     if (ret != ARCHIVE_OK)
2047       return (ret);
2048     rar->offset += bs;
2049     rar->bytes_uncopied -= bs;
2050     /*
2051      * If *buff is NULL, it means unp_buffer is not full.
2052      * So we have to continue extracting a RAR file.
2053      */
2054   } while (*buff == NULL);
2055
2056   rar->unp_offset = 0;
2057   *size = rar->unp_buffer_size;
2058   *offset = rar->offset_outgoing;
2059   rar->offset_outgoing += *size;
2060   /* Calculate File CRC. */
2061   rar->crc_calculated = crc32(rar->crc_calculated, *buff, (unsigned)*size);
2062   return ret;
2063 }
2064
2065 static int
2066 parse_codes(struct archive_read *a)
2067 {
2068   int i, j, val, n, r;
2069   unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
2070   unsigned int maxorder;
2071   struct huffman_code precode;
2072   struct rar *rar = (struct rar *)(a->format->data);
2073   struct rar_br *br = &(rar->br);
2074
2075   free_codes(a);
2076
2077   /* Skip to the next byte */
2078   rar_br_consume_unalined_bits(br);
2079
2080   /* PPMd block flag */
2081   if (!rar_br_read_ahead(a, br, 1))
2082     goto truncated_data;
2083   if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
2084   {
2085     rar_br_consume(br, 1);
2086     if (!rar_br_read_ahead(a, br, 7))
2087       goto truncated_data;
2088     ppmd_flags = rar_br_bits(br, 7);
2089     rar_br_consume(br, 7);
2090
2091     /* Memory is allocated in MB */
2092     if (ppmd_flags & 0x20)
2093     {
2094       if (!rar_br_read_ahead(a, br, 8))
2095         goto truncated_data;
2096       rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
2097       rar_br_consume(br, 8);
2098     }
2099
2100     if (ppmd_flags & 0x40)
2101     {
2102       if (!rar_br_read_ahead(a, br, 8))
2103         goto truncated_data;
2104       rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
2105       rar_br_consume(br, 8);
2106     }
2107     else
2108       rar->ppmd_escape = 2;
2109
2110     if (ppmd_flags & 0x20)
2111     {
2112       maxorder = (ppmd_flags & 0x1F) + 1;
2113       if(maxorder > 16)
2114         maxorder = 16 + (maxorder - 16) * 3;
2115
2116       if (maxorder == 1)
2117       {
2118         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2119                           "Truncated RAR file data");
2120         return (ARCHIVE_FATAL);
2121       }
2122
2123       /* Make sure ppmd7_contest is freed before Ppmd7_Construct
2124        * because reading a broken file cause this abnormal sequence. */
2125       __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
2126
2127       rar->bytein.a = a;
2128       rar->bytein.Read = &ppmd_read;
2129       __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
2130       rar->range_dec.Stream = &rar->bytein;
2131       __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
2132
2133       if (rar->dictionary_size == 0) {
2134               archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2135                           "Invalid zero dictionary size");
2136               return (ARCHIVE_FATAL);
2137       }
2138
2139       if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
2140         rar->dictionary_size, &g_szalloc))
2141       {
2142         archive_set_error(&a->archive, ENOMEM,
2143                           "Out of memory");
2144         return (ARCHIVE_FATAL);
2145       }
2146       if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2147       {
2148         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2149                           "Unable to initialize PPMd range decoder");
2150         return (ARCHIVE_FATAL);
2151       }
2152       __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
2153       rar->ppmd_valid = 1;
2154     }
2155     else
2156     {
2157       if (!rar->ppmd_valid) {
2158         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2159                           "Invalid PPMd sequence");
2160         return (ARCHIVE_FATAL);
2161       }
2162       if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2163       {
2164         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2165                           "Unable to initialize PPMd range decoder");
2166         return (ARCHIVE_FATAL);
2167       }
2168     }
2169   }
2170   else
2171   {
2172     rar_br_consume(br, 1);
2173
2174     /* Keep existing table flag */
2175     if (!rar_br_read_ahead(a, br, 1))
2176       goto truncated_data;
2177     if (!rar_br_bits(br, 1))
2178       memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
2179     rar_br_consume(br, 1);
2180
2181     memset(&bitlengths, 0, sizeof(bitlengths));
2182     for (i = 0; i < MAX_SYMBOLS;)
2183     {
2184       if (!rar_br_read_ahead(a, br, 4))
2185         goto truncated_data;
2186       bitlengths[i++] = rar_br_bits(br, 4);
2187       rar_br_consume(br, 4);
2188       if (bitlengths[i-1] == 0xF)
2189       {
2190         if (!rar_br_read_ahead(a, br, 4))
2191           goto truncated_data;
2192         zerocount = rar_br_bits(br, 4);
2193         rar_br_consume(br, 4);
2194         if (zerocount)
2195         {
2196           i--;
2197           for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
2198             bitlengths[i++] = 0;
2199         }
2200       }
2201     }
2202
2203     memset(&precode, 0, sizeof(precode));
2204     r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
2205     if (r != ARCHIVE_OK) {
2206       free(precode.tree);
2207       free(precode.table);
2208       return (r);
2209     }
2210
2211     for (i = 0; i < HUFFMAN_TABLE_SIZE;)
2212     {
2213       if ((val = read_next_symbol(a, &precode)) < 0) {
2214         free(precode.tree);
2215         free(precode.table);
2216         return (ARCHIVE_FATAL);
2217       }
2218       if (val < 16)
2219       {
2220         rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
2221         i++;
2222       }
2223       else if (val < 18)
2224       {
2225         if (i == 0)
2226         {
2227           free(precode.tree);
2228           free(precode.table);
2229           archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2230                             "Internal error extracting RAR file.");
2231           return (ARCHIVE_FATAL);
2232         }
2233
2234         if(val == 16) {
2235           if (!rar_br_read_ahead(a, br, 3)) {
2236             free(precode.tree);
2237             free(precode.table);
2238             goto truncated_data;
2239           }
2240           n = rar_br_bits(br, 3) + 3;
2241           rar_br_consume(br, 3);
2242         } else {
2243           if (!rar_br_read_ahead(a, br, 7)) {
2244             free(precode.tree);
2245             free(precode.table);
2246             goto truncated_data;
2247           }
2248           n = rar_br_bits(br, 7) + 11;
2249           rar_br_consume(br, 7);
2250         }
2251
2252         for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2253         {
2254           rar->lengthtable[i] = rar->lengthtable[i-1];
2255           i++;
2256         }
2257       }
2258       else
2259       {
2260         if(val == 18) {
2261           if (!rar_br_read_ahead(a, br, 3)) {
2262             free(precode.tree);
2263             free(precode.table);
2264             goto truncated_data;
2265           }
2266           n = rar_br_bits(br, 3) + 3;
2267           rar_br_consume(br, 3);
2268         } else {
2269           if (!rar_br_read_ahead(a, br, 7)) {
2270             free(precode.tree);
2271             free(precode.table);
2272             goto truncated_data;
2273           }
2274           n = rar_br_bits(br, 7) + 11;
2275           rar_br_consume(br, 7);
2276         }
2277
2278         for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2279           rar->lengthtable[i++] = 0;
2280       }
2281     }
2282     free(precode.tree);
2283     free(precode.table);
2284
2285     r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
2286                 MAX_SYMBOL_LENGTH);
2287     if (r != ARCHIVE_OK)
2288       return (r);
2289     r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
2290                 OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2291     if (r != ARCHIVE_OK)
2292       return (r);
2293     r = create_code(a, &rar->lowoffsetcode,
2294                 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
2295                 LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2296     if (r != ARCHIVE_OK)
2297       return (r);
2298     r = create_code(a, &rar->lengthcode,
2299                 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
2300                 LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
2301     if (r != ARCHIVE_OK)
2302       return (r);
2303   }
2304
2305   if (!rar->dictionary_size || !rar->lzss.window)
2306   {
2307     /* Seems as though dictionary sizes are not used. Even so, minimize
2308      * memory usage as much as possible.
2309      */
2310     void *new_window;
2311     unsigned int new_size;
2312
2313     if (rar->unp_size >= DICTIONARY_MAX_SIZE)
2314       new_size = DICTIONARY_MAX_SIZE;
2315     else
2316       new_size = rar_fls((unsigned int)rar->unp_size) << 1;
2317     new_window = realloc(rar->lzss.window, new_size);
2318     if (new_window == NULL) {
2319       archive_set_error(&a->archive, ENOMEM,
2320                         "Unable to allocate memory for uncompressed data.");
2321       return (ARCHIVE_FATAL);
2322     }
2323     rar->lzss.window = (unsigned char *)new_window;
2324     rar->dictionary_size = new_size;
2325     memset(rar->lzss.window, 0, rar->dictionary_size);
2326     rar->lzss.mask = rar->dictionary_size - 1;
2327   }
2328
2329   rar->start_new_table = 0;
2330   return (ARCHIVE_OK);
2331 truncated_data:
2332   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2333                     "Truncated RAR file data");
2334   rar->valid = 0;
2335   return (ARCHIVE_FATAL);
2336 }
2337
2338 static void
2339 free_codes(struct archive_read *a)
2340 {
2341   struct rar *rar = (struct rar *)(a->format->data);
2342   free(rar->maincode.tree);
2343   free(rar->offsetcode.tree);
2344   free(rar->lowoffsetcode.tree);
2345   free(rar->lengthcode.tree);
2346   free(rar->maincode.table);
2347   free(rar->offsetcode.table);
2348   free(rar->lowoffsetcode.table);
2349   free(rar->lengthcode.table);
2350   memset(&rar->maincode, 0, sizeof(rar->maincode));
2351   memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
2352   memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
2353   memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
2354 }
2355
2356
2357 static int
2358 read_next_symbol(struct archive_read *a, struct huffman_code *code)
2359 {
2360   unsigned char bit;
2361   unsigned int bits;
2362   int length, value, node;
2363   struct rar *rar;
2364   struct rar_br *br;
2365
2366   if (!code->table)
2367   {
2368     if (make_table(a, code) != (ARCHIVE_OK))
2369       return -1;
2370   }
2371
2372   rar = (struct rar *)(a->format->data);
2373   br = &(rar->br);
2374
2375   /* Look ahead (peek) at bits */
2376   if (!rar_br_read_ahead(a, br, code->tablesize)) {
2377     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2378                       "Truncated RAR file data");
2379     rar->valid = 0;
2380     return -1;
2381   }
2382   bits = rar_br_bits(br, code->tablesize);
2383
2384   length = code->table[bits].length;
2385   value = code->table[bits].value;
2386
2387   if (length < 0)
2388   {
2389     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2390                       "Invalid prefix code in bitstream");
2391     return -1;
2392   }
2393
2394   if (length <= code->tablesize)
2395   {
2396     /* Skip length bits */
2397     rar_br_consume(br, length);
2398     return value;
2399   }
2400
2401   /* Skip tablesize bits */
2402   rar_br_consume(br, code->tablesize);
2403
2404   node = value;
2405   while (!(code->tree[node].branches[0] ==
2406     code->tree[node].branches[1]))
2407   {
2408     if (!rar_br_read_ahead(a, br, 1)) {
2409       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2410                         "Truncated RAR file data");
2411       rar->valid = 0;
2412       return -1;
2413     }
2414     bit = rar_br_bits(br, 1);
2415     rar_br_consume(br, 1);
2416
2417     if (code->tree[node].branches[bit] < 0)
2418     {
2419       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2420                         "Invalid prefix code in bitstream");
2421       return -1;
2422     }
2423     node = code->tree[node].branches[bit];
2424   }
2425
2426   return code->tree[node].branches[0];
2427 }
2428
2429 static int
2430 create_code(struct archive_read *a, struct huffman_code *code,
2431             unsigned char *lengths, int numsymbols, char maxlength)
2432 {
2433   int i, j, codebits = 0, symbolsleft = numsymbols;
2434
2435   code->numentries = 0;
2436   code->numallocatedentries = 0;
2437   if (new_node(code) < 0) {
2438     archive_set_error(&a->archive, ENOMEM,
2439                       "Unable to allocate memory for node data.");
2440     return (ARCHIVE_FATAL);
2441   }
2442   code->numentries = 1;
2443   code->minlength = INT_MAX;
2444   code->maxlength = INT_MIN;
2445   codebits = 0;
2446   for(i = 1; i <= maxlength; i++)
2447   {
2448     for(j = 0; j < numsymbols; j++)
2449     {
2450       if (lengths[j] != i) continue;
2451       if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
2452         return (ARCHIVE_FATAL);
2453       codebits++;
2454       if (--symbolsleft <= 0) { break; break; }
2455     }
2456     codebits <<= 1;
2457   }
2458   return (ARCHIVE_OK);
2459 }
2460
2461 static int
2462 add_value(struct archive_read *a, struct huffman_code *code, int value,
2463           int codebits, int length)
2464 {
2465   int repeatpos, lastnode, bitpos, bit, repeatnode, nextnode;
2466
2467   free(code->table);
2468   code->table = NULL;
2469
2470   if(length > code->maxlength)
2471     code->maxlength = length;
2472   if(length < code->minlength)
2473     code->minlength = length;
2474
2475   repeatpos = -1;
2476   if (repeatpos == 0 || (repeatpos >= 0
2477     && (((codebits >> (repeatpos - 1)) & 3) == 0
2478     || ((codebits >> (repeatpos - 1)) & 3) == 3)))
2479   {
2480     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2481                       "Invalid repeat position");
2482     return (ARCHIVE_FATAL);
2483   }
2484
2485   lastnode = 0;
2486   for (bitpos = length - 1; bitpos >= 0; bitpos--)
2487   {
2488     bit = (codebits >> bitpos) & 1;
2489
2490     /* Leaf node check */
2491     if (code->tree[lastnode].branches[0] ==
2492       code->tree[lastnode].branches[1])
2493     {
2494       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2495                         "Prefix found");
2496       return (ARCHIVE_FATAL);
2497     }
2498
2499     if (bitpos == repeatpos)
2500     {
2501       /* Open branch check */
2502       if (!(code->tree[lastnode].branches[bit] < 0))
2503       {
2504         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2505                           "Invalid repeating code");
2506         return (ARCHIVE_FATAL);
2507       }
2508
2509       if ((repeatnode = new_node(code)) < 0) {
2510         archive_set_error(&a->archive, ENOMEM,
2511                           "Unable to allocate memory for node data.");
2512         return (ARCHIVE_FATAL);
2513       }
2514       if ((nextnode = new_node(code)) < 0) {
2515         archive_set_error(&a->archive, ENOMEM,
2516                           "Unable to allocate memory for node data.");
2517         return (ARCHIVE_FATAL);
2518       }
2519
2520       /* Set branches */
2521       code->tree[lastnode].branches[bit] = repeatnode;
2522       code->tree[repeatnode].branches[bit] = repeatnode;
2523       code->tree[repeatnode].branches[bit^1] = nextnode;
2524       lastnode = nextnode;
2525
2526       bitpos++; /* terminating bit already handled, skip it */
2527     }
2528     else
2529     {
2530       /* Open branch check */
2531       if (code->tree[lastnode].branches[bit] < 0)
2532       {
2533         if (new_node(code) < 0) {
2534           archive_set_error(&a->archive, ENOMEM,
2535                             "Unable to allocate memory for node data.");
2536           return (ARCHIVE_FATAL);
2537         }
2538         code->tree[lastnode].branches[bit] = code->numentries++;
2539       }
2540
2541       /* set to branch */
2542       lastnode = code->tree[lastnode].branches[bit];
2543     }
2544   }
2545
2546   if (!(code->tree[lastnode].branches[0] == -1
2547     && code->tree[lastnode].branches[1] == -2))
2548   {
2549     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2550                       "Prefix found");
2551     return (ARCHIVE_FATAL);
2552   }
2553
2554   /* Set leaf value */
2555   code->tree[lastnode].branches[0] = value;
2556   code->tree[lastnode].branches[1] = value;
2557
2558   return (ARCHIVE_OK);
2559 }
2560
2561 static int
2562 new_node(struct huffman_code *code)
2563 {
2564   void *new_tree;
2565   if (code->numallocatedentries == code->numentries) {
2566     int new_num_entries = 256;
2567     if (code->numentries > 0) {
2568         new_num_entries = code->numentries * 2;
2569     }
2570     new_tree = realloc(code->tree, new_num_entries * sizeof(*code->tree));
2571     if (new_tree == NULL)
2572         return (-1);
2573     code->tree = (struct huffman_tree_node *)new_tree;
2574     code->numallocatedentries = new_num_entries;
2575   }
2576   code->tree[code->numentries].branches[0] = -1;
2577   code->tree[code->numentries].branches[1] = -2;
2578   return 1;
2579 }
2580
2581 static int
2582 make_table(struct archive_read *a, struct huffman_code *code)
2583 {
2584   if (code->maxlength < code->minlength || code->maxlength > 10)
2585     code->tablesize = 10;
2586   else
2587     code->tablesize = code->maxlength;
2588
2589   code->table =
2590     (struct huffman_table_entry *)calloc(1, sizeof(*code->table)
2591     * ((size_t)1 << code->tablesize));
2592
2593   return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
2594 }
2595
2596 static int
2597 make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
2598                    struct huffman_table_entry *table, int depth,
2599                    int maxdepth)
2600 {
2601   int currtablesize, i, ret = (ARCHIVE_OK);
2602
2603   if (!code->tree)
2604   {
2605     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2606                       "Huffman tree was not created.");
2607     return (ARCHIVE_FATAL);
2608   }
2609   if (node < 0 || node >= code->numentries)
2610   {
2611     archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2612                       "Invalid location to Huffman tree specified.");
2613     return (ARCHIVE_FATAL);
2614   }
2615
2616   currtablesize = 1 << (maxdepth - depth);
2617
2618   if (code->tree[node].branches[0] ==
2619     code->tree[node].branches[1])
2620   {
2621     for(i = 0; i < currtablesize; i++)
2622     {
2623       table[i].length = depth;
2624       table[i].value = code->tree[node].branches[0];
2625     }
2626   }
2627   else if (node < 0)
2628   {
2629     for(i = 0; i < currtablesize; i++)
2630       table[i].length = -1;
2631   }
2632   else
2633   {
2634     if(depth == maxdepth)
2635     {
2636       table[0].length = maxdepth + 1;
2637       table[0].value = node;
2638     }
2639     else
2640     {
2641       ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
2642                                 depth + 1, maxdepth);
2643       ret |= make_table_recurse(a, code, code->tree[node].branches[1],
2644                          table + currtablesize / 2, depth + 1, maxdepth);
2645     }
2646   }
2647   return ret;
2648 }
2649
2650 static int64_t
2651 expand(struct archive_read *a, int64_t end)
2652 {
2653   static const unsigned char lengthbases[] =
2654     {   0,   1,   2,   3,   4,   5,   6,
2655         7,   8,  10,  12,  14,  16,  20,
2656        24,  28,  32,  40,  48,  56,  64,
2657        80,  96, 112, 128, 160, 192, 224 };
2658   static const unsigned char lengthbits[] =
2659     { 0, 0, 0, 0, 0, 0, 0,
2660       0, 1, 1, 1, 1, 2, 2,
2661       2, 2, 3, 3, 3, 3, 4,
2662       4, 4, 4, 5, 5, 5, 5 };
2663   static const unsigned int offsetbases[] =
2664     {       0,       1,       2,       3,       4,       6,
2665             8,      12,      16,      24,      32,      48,
2666            64,      96,     128,     192,     256,     384,
2667           512,     768,    1024,    1536,    2048,    3072,
2668          4096,    6144,    8192,   12288,   16384,   24576,
2669         32768,   49152,   65536,   98304,  131072,  196608,
2670        262144,  327680,  393216,  458752,  524288,  589824,
2671        655360,  720896,  786432,  851968,  917504,  983040,
2672       1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2673       2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
2674   static const unsigned char offsetbits[] =
2675     {  0,  0,  0,  0,  1,  1,  2,  2,  3,  3,  4,  4,
2676        5,  5,  6,  6,  7,  7,  8,  8,  9,  9, 10, 10,
2677       11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
2678       16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2679       18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
2680   static const unsigned char shortbases[] =
2681     { 0, 4, 8, 16, 32, 64, 128, 192 };
2682   static const unsigned char shortbits[] =
2683     { 2, 2, 3, 4, 5, 6, 6, 6 };
2684
2685   int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
2686   unsigned char newfile;
2687   struct rar *rar = (struct rar *)(a->format->data);
2688   struct rar_br *br = &(rar->br);
2689
2690   if (rar->filterstart < end)
2691     end = rar->filterstart;
2692
2693   while (1)
2694   {
2695     if (rar->output_last_match &&
2696       lzss_position(&rar->lzss) + rar->lastlength <= end)
2697     {
2698       lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
2699       rar->output_last_match = 0;
2700     }
2701
2702     if(rar->is_ppmd_block || rar->output_last_match ||
2703       lzss_position(&rar->lzss) >= end)
2704       return lzss_position(&rar->lzss);
2705
2706     if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
2707       return (ARCHIVE_FATAL);
2708     rar->output_last_match = 0;
2709
2710     if (symbol < 256)
2711     {
2712       lzss_emit_literal(rar, symbol);
2713       continue;
2714     }
2715     else if (symbol == 256)
2716     {
2717       if (!rar_br_read_ahead(a, br, 1))
2718         goto truncated_data;
2719       newfile = !rar_br_bits(br, 1);
2720       rar_br_consume(br, 1);
2721
2722       if(newfile)
2723       {
2724         rar->start_new_block = 1;
2725         if (!rar_br_read_ahead(a, br, 1))
2726           goto truncated_data;
2727         rar->start_new_table = rar_br_bits(br, 1);
2728         rar_br_consume(br, 1);
2729         return lzss_position(&rar->lzss);
2730       }
2731       else
2732       {
2733         if (parse_codes(a) != ARCHIVE_OK)
2734           return (ARCHIVE_FATAL);
2735         continue;
2736       }
2737     }
2738     else if(symbol==257)
2739     {
2740       archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2741                         "Parsing filters is unsupported.");
2742       return (ARCHIVE_FAILED);
2743     }
2744     else if(symbol==258)
2745     {
2746       if(rar->lastlength == 0)
2747         continue;
2748
2749       offs = rar->lastoffset;
2750       len = rar->lastlength;
2751     }
2752     else if (symbol <= 262)
2753     {
2754       offsindex = symbol - 259;
2755       offs = rar->oldoffset[offsindex];
2756
2757       if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
2758         goto bad_data;
2759       if (lensymbol > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2760         goto bad_data;
2761       if (lensymbol > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2762         goto bad_data;
2763       len = lengthbases[lensymbol] + 2;
2764       if (lengthbits[lensymbol] > 0) {
2765         if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
2766           goto truncated_data;
2767         len += rar_br_bits(br, lengthbits[lensymbol]);
2768         rar_br_consume(br, lengthbits[lensymbol]);
2769       }
2770
2771       for (i = offsindex; i > 0; i--)
2772         rar->oldoffset[i] = rar->oldoffset[i-1];
2773       rar->oldoffset[0] = offs;
2774     }
2775     else if(symbol<=270)
2776     {
2777       offs = shortbases[symbol-263] + 1;
2778       if(shortbits[symbol-263] > 0) {
2779         if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
2780           goto truncated_data;
2781         offs += rar_br_bits(br, shortbits[symbol-263]);
2782         rar_br_consume(br, shortbits[symbol-263]);
2783       }
2784
2785       len = 2;
2786
2787       for(i = 3; i > 0; i--)
2788         rar->oldoffset[i] = rar->oldoffset[i-1];
2789       rar->oldoffset[0] = offs;
2790     }
2791     else
2792     {
2793       if (symbol-271 > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2794         goto bad_data;
2795       if (symbol-271 > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2796         goto bad_data;
2797       len = lengthbases[symbol-271]+3;
2798       if(lengthbits[symbol-271] > 0) {
2799         if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
2800           goto truncated_data;
2801         len += rar_br_bits(br, lengthbits[symbol-271]);
2802         rar_br_consume(br, lengthbits[symbol-271]);
2803       }
2804
2805       if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
2806         goto bad_data;
2807       if (offssymbol > (int)(sizeof(offsetbases)/sizeof(offsetbases[0])))
2808         goto bad_data;
2809       if (offssymbol > (int)(sizeof(offsetbits)/sizeof(offsetbits[0])))
2810         goto bad_data;
2811       offs = offsetbases[offssymbol]+1;
2812       if(offsetbits[offssymbol] > 0)
2813       {
2814         if(offssymbol > 9)
2815         {
2816           if(offsetbits[offssymbol] > 4) {
2817             if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
2818               goto truncated_data;
2819             offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
2820             rar_br_consume(br, offsetbits[offssymbol] - 4);
2821           }
2822
2823           if(rar->numlowoffsetrepeats > 0)
2824           {
2825             rar->numlowoffsetrepeats--;
2826             offs += rar->lastlowoffset;
2827           }
2828           else
2829           {
2830             if ((lowoffsetsymbol =
2831               read_next_symbol(a, &rar->lowoffsetcode)) < 0)
2832               return (ARCHIVE_FATAL);
2833             if(lowoffsetsymbol == 16)
2834             {
2835               rar->numlowoffsetrepeats = 15;
2836               offs += rar->lastlowoffset;
2837             }
2838             else
2839             {
2840               offs += lowoffsetsymbol;
2841               rar->lastlowoffset = lowoffsetsymbol;
2842             }
2843           }
2844         }
2845         else {
2846           if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
2847             goto truncated_data;
2848           offs += rar_br_bits(br, offsetbits[offssymbol]);
2849           rar_br_consume(br, offsetbits[offssymbol]);
2850         }
2851       }
2852
2853       if (offs >= 0x40000)
2854         len++;
2855       if (offs >= 0x2000)
2856         len++;
2857
2858       for(i = 3; i > 0; i--)
2859         rar->oldoffset[i] = rar->oldoffset[i-1];
2860       rar->oldoffset[0] = offs;
2861     }
2862
2863     rar->lastoffset = offs;
2864     rar->lastlength = len;
2865     rar->output_last_match = 1;
2866   }
2867 truncated_data:
2868   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2869                     "Truncated RAR file data");
2870   rar->valid = 0;
2871   return (ARCHIVE_FATAL);
2872 bad_data:
2873   archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2874                     "Bad RAR file data");
2875   return (ARCHIVE_FATAL);
2876 }
2877
2878 static int
2879 copy_from_lzss_window(struct archive_read *a, const void **buffer,
2880                         int64_t startpos, int length)
2881 {
2882   int windowoffs, firstpart;
2883   struct rar *rar = (struct rar *)(a->format->data);
2884
2885   if (!rar->unp_buffer)
2886   {
2887     if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
2888     {
2889       archive_set_error(&a->archive, ENOMEM,
2890                         "Unable to allocate memory for uncompressed data.");
2891       return (ARCHIVE_FATAL);
2892     }
2893   }
2894
2895   windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
2896   if(windowoffs + length <= lzss_size(&rar->lzss)) {
2897     memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
2898            length);
2899   } else if (length <= lzss_size(&rar->lzss)) {
2900     firstpart = lzss_size(&rar->lzss) - windowoffs;
2901     if (firstpart < 0) {
2902       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2903                         "Bad RAR file data");
2904       return (ARCHIVE_FATAL);
2905     }
2906     if (firstpart < length) {
2907       memcpy(&rar->unp_buffer[rar->unp_offset],
2908              &rar->lzss.window[windowoffs], firstpart);
2909       memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
2910              &rar->lzss.window[0], length - firstpart);
2911     } else {
2912       memcpy(&rar->unp_buffer[rar->unp_offset],
2913              &rar->lzss.window[windowoffs], length);
2914     }
2915   } else {
2916       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2917                         "Bad RAR file data");
2918       return (ARCHIVE_FATAL);
2919   }
2920   rar->unp_offset += length;
2921   if (rar->unp_offset >= rar->unp_buffer_size)
2922     *buffer = rar->unp_buffer;
2923   else
2924     *buffer = NULL;
2925   return (ARCHIVE_OK);
2926 }
2927
2928 static const void *
2929 rar_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
2930 {
2931   struct rar *rar = (struct rar *)(a->format->data);
2932   const void *h = __archive_read_ahead(a, min, avail);
2933   int ret;
2934   if (avail)
2935   {
2936     if (a->archive.read_data_is_posix_read && *avail > (ssize_t)a->archive.read_data_requested)
2937       *avail = a->archive.read_data_requested;
2938     if (*avail > rar->bytes_remaining)
2939       *avail = (ssize_t)rar->bytes_remaining;
2940     if (*avail < 0)
2941       return NULL;
2942     else if (*avail == 0 && rar->main_flags & MHD_VOLUME &&
2943       rar->file_flags & FHD_SPLIT_AFTER)
2944     {
2945       ret = archive_read_format_rar_read_header(a, a->entry);
2946       if (ret == (ARCHIVE_EOF))
2947       {
2948         rar->has_endarc_header = 1;
2949         ret = archive_read_format_rar_read_header(a, a->entry);
2950       }
2951       if (ret != (ARCHIVE_OK))
2952         return NULL;
2953       return rar_read_ahead(a, min, avail);
2954     }
2955   }
2956   return h;
2957 }