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