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