1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
|
/*-
* Copyright (c) 2003-2007 Tim Kientzle
* Copyright (c) 2011 Andres Mejia
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "archive_platform.h"
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <time.h>
#include <limits.h>
#include "archive.h"
#include "archive_endian.h"
#include "archive_entry.h"
#include "archive_entry_locale.h"
#include "archive_ppmd7.h"
#include "archive_private.h"
#include "archive_read_private.h"
/* RAR signature, also known as the mark header */
#define RAR_SIGNATURE "\x52\x61\x72\x21\x1A\x07\x00"
/* Header types */
#define MARK_HEAD 0x72
#define MAIN_HEAD 0x73
#define FILE_HEAD 0x74
#define COMM_HEAD 0x75
#define AV_HEAD 0x76
#define SUB_HEAD 0x77
#define PROTECT_HEAD 0x78
#define SIGN_HEAD 0x79
#define NEWSUB_HEAD 0x7a
#define ENDARC_HEAD 0x7b
/* Main Header Flags */
#define MHD_VOLUME 0x0001
#define MHD_COMMENT 0x0002
#define MHD_LOCK 0x0004
#define MHD_SOLID 0x0008
#define MHD_NEWNUMBERING 0x0010
#define MHD_AV 0x0020
#define MHD_PROTECT 0x0040
#define MHD_PASSWORD 0x0080
#define MHD_FIRSTVOLUME 0x0100
#define MHD_ENCRYPTVER 0x0200
/* Flags common to all headers */
#define HD_MARKDELETION 0x4000
#define HD_ADD_SIZE_PRESENT 0x8000
/* File Header Flags */
#define FHD_SPLIT_BEFORE 0x0001
#define FHD_SPLIT_AFTER 0x0002
#define FHD_PASSWORD 0x0004
#define FHD_COMMENT 0x0008
#define FHD_SOLID 0x0010
#define FHD_LARGE 0x0100
#define FHD_UNICODE 0x0200
#define FHD_SALT 0x0400
#define FHD_VERSION 0x0800
#define FHD_EXTTIME 0x1000
#define FHD_EXTFLAGS 0x2000
/* File dictionary sizes */
#define DICTIONARY_SIZE_64 0x00
#define DICTIONARY_SIZE_128 0x20
#define DICTIONARY_SIZE_256 0x40
#define DICTIONARY_SIZE_512 0x60
#define DICTIONARY_SIZE_1024 0x80
#define DICTIONARY_SIZE_2048 0xA0
#define DICTIONARY_SIZE_4096 0xC0
#define FILE_IS_DIRECTORY 0xE0
#define DICTIONARY_MASK FILE_IS_DIRECTORY
/* OS Flags */
#define OS_MSDOS 0
#define OS_OS2 1
#define OS_WIN32 2
#define OS_UNIX 3
#define OS_MAC_OS 4
#define OS_BEOS 5
/* Compression Methods */
#define COMPRESS_METHOD_STORE 0x30
/* LZSS */
#define COMPRESS_METHOD_FASTEST 0x31
#define COMPRESS_METHOD_FAST 0x32
#define COMPRESS_METHOD_NORMAL 0x33
/* PPMd Variant H */
#define COMPRESS_METHOD_GOOD 0x34
#define COMPRESS_METHOD_BEST 0x35
#define CRC_POLYNOMIAL 0xEDB88320
#define NS_UNIT 10000000
#define DICTIONARY_MAX_SIZE 0x400000
#define MAINCODE_SIZE 299
#define OFFSETCODE_SIZE 60
#define LOWOFFSETCODE_SIZE 17
#define LENGTHCODE_SIZE 28
#define HUFFMAN_TABLE_SIZE \
MAINCODE_SIZE + OFFSETCODE_SIZE + LOWOFFSETCODE_SIZE + LENGTHCODE_SIZE
#define MAX_SYMBOL_LENGTH 0xF
#define MAX_SYMBOLS 20
/* Define this here for non-Windows platforms */
#if !((defined(__WIN32__) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__))
#define FILE_ATTRIBUTE_DIRECTORY 0x10
#endif
/* Fields common to all headers */
struct rar_header
{
char crc[2];
char type;
char flags[2];
char size[2];
};
/* Fields common to all file headers */
struct rar_file_header
{
char pack_size[4];
char unp_size[4];
char host_os;
char file_crc[4];
char file_time[4];
char unp_ver;
char method;
char name_size[2];
char file_attr[4];
};
struct huffman_tree_node
{
int branches[2];
};
struct huffman_table_entry
{
unsigned int length;
int value;
};
struct huffman_code
{
struct huffman_tree_node *tree;
int numentries;
int minlength;
int maxlength;
int tablesize;
struct huffman_table_entry *table;
};
struct lzss
{
unsigned char *window;
int mask;
int64_t position;
};
struct rar
{
/* Entries from main RAR header */
unsigned main_flags;
char reserved1[2];
char reserved2[4];
char encryptver;
/* File header entries */
char compression_method;
unsigned file_flags;
int64_t packed_size;
int64_t unp_size;
time_t mtime;
long mnsec;
mode_t mode;
/* File header optional entries */
char salt[8];
time_t atime;
long ansec;
time_t ctime;
long cnsec;
time_t arctime;
long arcnsec;
/* Fields to help with tracking decompression of files. */
int64_t bytes_unconsumed;
int64_t bytes_remaining;
int64_t bytes_uncopied;
int64_t offset;
char valid;
unsigned char *unp_buffer;
unsigned int dictionary_size;
char start_new_block;
/* LZSS members */
struct huffman_code maincode;
struct huffman_code offsetcode;
struct huffman_code lowoffsetcode;
struct huffman_code lengthcode;
unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
struct lzss lzss;
char output_last_match;
unsigned int lastlength;
unsigned int lastoffset;
unsigned int oldoffset[4];
unsigned int lastlowoffset;
unsigned int numlowoffsetrepeats;
int64_t filterstart;
char start_new_table;
/* PPMd Variant H members */
char is_ppmd_block;
int ppmd_escape;
CPpmd7 ppmd7_context;
CPpmd7z_RangeDec range_dec;
IByteIn bytein;
/*
* Bit stream reader.
*/
struct rar_br {
#define CACHE_TYPE uint64_t
#define CACHE_BITS (8 * sizeof(CACHE_TYPE))
/* Cache buffer. */
CACHE_TYPE cache_buffer;
/* Indicates how many bits avail in cache_buffer. */
int cache_avail;
ssize_t avail_in;
const unsigned char *next_in;
} br;
};
static int archive_read_format_rar_bid(struct archive_read *);
static int archive_read_format_rar_read_header(struct archive_read *,
struct archive_entry *);
static int archive_read_format_rar_read_data(struct archive_read *,
const void **, size_t *, int64_t *);
static int archive_read_format_rar_read_data_skip(struct archive_read *a);
static int archive_read_format_rar_cleanup(struct archive_read *);
/* Support functions */
static int read_header(struct archive_read *, struct archive_entry *, char);
static time_t get_time(int time);
static int read_exttime(const char *, struct rar *, const char *);
static int read_symlink_stored(struct archive_read *, struct archive_entry *,
struct archive_string_conv *);
static int read_data_stored(struct archive_read *, const void **, size_t *,
int64_t *);
static int read_data_compressed(struct archive_read *, const void **, size_t *,
int64_t *);
static int rar_br_preparation(struct archive_read *, struct rar_br *);
static int parse_codes(struct archive_read *);
static void free_codes(struct archive_read *);
static int read_next_symbol(struct archive_read *, struct huffman_code *);
static int create_code(struct archive_read *, struct huffman_code *,
unsigned char *, int, char);
static int add_value(struct archive_read *, struct huffman_code *, int, int,
int);
static int new_node(struct huffman_code *);
static int make_table(struct huffman_code *);
static int make_table_recurse(struct huffman_code *, int,
struct huffman_table_entry *, int, int);
static int64_t expand(struct archive_read *, int64_t);
static int copy_from_lzss_window(struct archive_read *, const void **,
int64_t, int);
/*
* Bit stream reader.
*/
/* Check that the cache buffer has enough bits. */
#define rar_br_has(br, n) ((br)->cache_avail >= n)
/* Get compressed data by bit. */
#define rar_br_bits(br, n) \
(((uint32_t)((br)->cache_buffer >> \
((br)->cache_avail - (n)))) & cache_masks[n])
#define rar_br_bits_forced(br, n) \
(((uint32_t)((br)->cache_buffer << \
((n) - (br)->cache_avail))) & cache_masks[n])
/* Read ahead to make sure the cache buffer has enough compressed data we
* will use.
* True : completed, there is enough data in the cache buffer.
* False : there is no data in the stream. */
#define rar_br_read_ahead(a, br, n) \
((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
/* Notify how many bits we consumed. */
#define rar_br_consume(br, n) ((br)->cache_avail -= (n))
#define rar_br_consume_unalined_bits(br) ((br)->cache_avail &= ~7)
static const uint32_t cache_masks[] = {
0x00000000, 0x00000001, 0x00000003, 0x00000007,
0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
};
/*
* Shift away used bits in the cache data and fill it up with following bits.
* Call this when cache buffer does not have enough bits you need.
*
* Returns 1 if the cache buffer is full.
* Returns 0 if the cache buffer is not full; input buffer is empty.
*/
static int
rar_br_fillup(struct archive_read *a, struct rar_br *br)
{
struct rar *rar = (struct rar *)(a->format->data);
int n = CACHE_BITS - br->cache_avail;
for (;;) {
switch (n >> 3) {
case 8:
if (br->avail_in >= 8) {
br->cache_buffer =
((uint64_t)br->next_in[0]) << 56 |
((uint64_t)br->next_in[1]) << 48 |
((uint64_t)br->next_in[2]) << 40 |
((uint64_t)br->next_in[3]) << 32 |
((uint32_t)br->next_in[4]) << 24 |
((uint32_t)br->next_in[5]) << 16 |
((uint32_t)br->next_in[6]) << 8 |
(uint32_t)br->next_in[7];
br->next_in += 8;
br->avail_in -= 8;
br->cache_avail += 8 * 8;
rar->bytes_unconsumed += 8;
rar->bytes_remaining -= 8;
return (1);
}
break;
case 7:
if (br->avail_in >= 7) {
br->cache_buffer =
(br->cache_buffer << 56) |
((uint64_t)br->next_in[0]) << 48 |
((uint64_t)br->next_in[1]) << 40 |
((uint64_t)br->next_in[2]) << 32 |
((uint32_t)br->next_in[3]) << 24 |
((uint32_t)br->next_in[4]) << 16 |
((uint32_t)br->next_in[5]) << 8 |
(uint32_t)br->next_in[6];
br->next_in += 7;
br->avail_in -= 7;
br->cache_avail += 7 * 8;
rar->bytes_unconsumed += 7;
rar->bytes_remaining -= 7;
return (1);
}
break;
case 6:
if (br->avail_in >= 6) {
br->cache_buffer =
(br->cache_buffer << 48) |
((uint64_t)br->next_in[0]) << 40 |
((uint64_t)br->next_in[1]) << 32 |
((uint32_t)br->next_in[2]) << 24 |
((uint32_t)br->next_in[3]) << 16 |
((uint32_t)br->next_in[4]) << 8 |
(uint32_t)br->next_in[5];
br->next_in += 6;
br->avail_in -= 6;
br->cache_avail += 6 * 8;
rar->bytes_unconsumed += 6;
rar->bytes_remaining -= 6;
return (1);
}
break;
case 0:
/* We have enough compressed data in
* the cache buffer.*/
return (1);
default:
break;
}
if (br->avail_in <= 0) {
if (rar->bytes_unconsumed > 0) {
/* Consume as much as the decompressor
* actually used. */
__archive_read_consume(a, rar->bytes_unconsumed);
rar->bytes_unconsumed = 0;
}
br->next_in = __archive_read_ahead(a, 1, &(br->avail_in));
if (br->next_in == NULL)
return (0);
if (br->avail_in > rar->bytes_remaining)
br->avail_in = rar->bytes_remaining;
if (br->avail_in == 0)
return (0);
}
br->cache_buffer =
(br->cache_buffer << 8) | *br->next_in++;
br->avail_in--;
br->cache_avail += 8;
n -= 8;
rar->bytes_unconsumed++;
rar->bytes_remaining--;
}
}
static int
rar_br_preparation(struct archive_read *a, struct rar_br *br)
{
struct rar *rar = (struct rar *)(a->format->data);
if (rar->bytes_remaining > 0) {
br->next_in = __archive_read_ahead(a, 1, &(br->avail_in));
if (br->next_in == NULL) {
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
return (ARCHIVE_FATAL);
}
if (br->avail_in > rar->bytes_remaining)
br->avail_in = rar->bytes_remaining;
if (br->cache_avail == 0)
(void)rar_br_fillup(a, br);
}
return (ARCHIVE_OK);
}
/* Find last bit set */
static inline int
rar_fls(unsigned int word)
{
word |= (word >> 1);
word |= (word >> 2);
word |= (word >> 4);
word |= (word >> 8);
word |= (word >> 16);
return word - (word >> 1);
}
/* LZSS functions */
static inline int64_t
lzss_position(struct lzss *lzss)
{
return lzss->position;
}
static inline int
lzss_mask(struct lzss *lzss)
{
return lzss->mask;
}
static inline int
lzss_size(struct lzss *lzss)
{
return lzss->mask + 1;
}
static inline int
lzss_offset_for_position(struct lzss *lzss, int64_t pos)
{
return pos & lzss->mask;
}
static inline unsigned char *
lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
{
return &lzss->window[lzss_offset_for_position(lzss, pos)];
}
static inline int
lzss_current_offset(struct lzss *lzss)
{
return lzss_offset_for_position(lzss, lzss->position);
}
static inline uint8_t *
lzss_current_pointer(struct lzss *lzss)
{
return lzss_pointer_for_position(lzss, lzss->position);
}
static inline void
lzss_emit_literal(struct rar *rar, uint8_t literal)
{
*lzss_current_pointer(&rar->lzss) = literal;
rar->lzss.position++;
}
static inline void
lzss_emit_match(struct rar *rar, int offset, int length)
{
int i, windowoffs = lzss_current_offset(&rar->lzss);
for(i = 0; i < length; i++)
{
rar->lzss.window[(windowoffs + i) & lzss_mask(&rar->lzss)] =
rar->lzss.window[(windowoffs + i - offset) & lzss_mask(&rar->lzss)];
}
rar->lzss.position += length;
}
static void *
ppmd_alloc(void *p, size_t size)
{
(void)p;
return malloc(size);
}
static void
ppmd_free(void *p, void *address)
{
(void)p;
free(address);
}
static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
static Byte
ppmd_read(void *p)
{
struct archive_read *a = ((IByteIn*)p)->a;
struct rar *rar = (struct rar *)(a->format->data);
struct rar_br *br = &(rar->br);
Byte b;
if (!rar_br_read_ahead(a, br, 1))
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
rar->valid = 0;
return 0;
}
b = rar_br_bits(br, 8);
rar_br_consume(br, 8);
return b;
}
int
archive_read_support_format_rar(struct archive *_a)
{
struct archive_read *a = (struct archive_read *)_a;
struct rar *rar;
int r;
archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
"archive_read_support_format_rar");
rar = (struct rar *)malloc(sizeof(*rar));
if (rar == NULL)
{
archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
return (ARCHIVE_FATAL);
}
memset(rar, 0, sizeof(*rar));
r = __archive_read_register_format(a,
rar,
"rar",
archive_read_format_rar_bid,
NULL,
archive_read_format_rar_read_header,
archive_read_format_rar_read_data,
archive_read_format_rar_read_data_skip,
archive_read_format_rar_cleanup);
if (r != ARCHIVE_OK)
free(rar);
return (r);
}
static int
archive_read_format_rar_bid(struct archive_read *a)
{
const char *p;
if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
return (-1);
if (memcmp(p, RAR_SIGNATURE, 7) == 0)
return (30);
return (0);
}
static int
archive_read_format_rar_read_header(struct archive_read *a,
struct archive_entry *entry)
{
const void *h;
const char *p;
struct rar *rar;
size_t skip;
char head_type;
int ret;
unsigned flags;
a->archive.archive_format = ARCHIVE_FORMAT_RAR;
if (a->archive.archive_format_name == NULL)
a->archive.archive_format_name = "RAR";
rar = (struct rar *)(a->format->data);
/* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
* this fails.
*/
if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
return (ARCHIVE_EOF);
p = h;
while (1)
{
head_type = p[2];
switch(head_type)
{
case MARK_HEAD:
__archive_read_consume(a, 7);
if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
continue;
case MAIN_HEAD:
rar->main_flags = archive_le16dec(p + 3);
skip = archive_le16dec(p + 5);
if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid header size");
return (ARCHIVE_FATAL);
}
if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
sizeof(rar->reserved2));
if (rar->main_flags & MHD_ENCRYPTVER) {
if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid header size");
return (ARCHIVE_FATAL);
}
rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
sizeof(rar->reserved2));
}
if (rar->main_flags & MHD_VOLUME ||
rar->main_flags & MHD_FIRSTVOLUME)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"RAR volume support unavailable.");
return (ARCHIVE_FATAL);
}
if (rar->main_flags & MHD_PASSWORD)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"RAR encryption support unavailable.");
return (ARCHIVE_FATAL);
}
__archive_read_consume(a, skip);
if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
continue;
case FILE_HEAD:
return read_header(a, entry, head_type);
case COMM_HEAD:
case AV_HEAD:
case SUB_HEAD:
case PROTECT_HEAD:
case SIGN_HEAD:
flags = archive_le16dec(p + 3);
skip = archive_le16dec(p + 5);
if (skip < 7) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid header size");
return (ARCHIVE_FATAL);
}
if (skip > 7) {
if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
}
if (flags & HD_ADD_SIZE_PRESENT)
{
if (skip < 7 + 4) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid header size");
return (ARCHIVE_FATAL);
}
skip += archive_le32dec(p + 7);
if (__archive_read_ahead(a, skip, NULL) == NULL)
return (ARCHIVE_FATAL);
}
__archive_read_consume(a, skip);
if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
continue;
case NEWSUB_HEAD:
if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
return ret;
if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
continue;
case ENDARC_HEAD:
return (ARCHIVE_EOF);
}
break;
}
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Bad RAR file");
return (ARCHIVE_FATAL);
}
static int
archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
size_t *size, int64_t *offset)
{
struct rar *rar = (struct rar *)(a->format->data);
if (rar->bytes_unconsumed > 0) {
/* Consume as much as the decompressor actually used. */
__archive_read_consume(a, rar->bytes_unconsumed);
rar->bytes_unconsumed = 0;
}
switch (rar->compression_method)
{
case COMPRESS_METHOD_STORE:
return read_data_stored(a, buff, size, offset);
case COMPRESS_METHOD_FASTEST:
case COMPRESS_METHOD_FAST:
case COMPRESS_METHOD_NORMAL:
case COMPRESS_METHOD_GOOD:
case COMPRESS_METHOD_BEST:
return read_data_compressed(a, buff, size, offset);
default:
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Unsupported compression method for RAR file.");
}
return (ARCHIVE_FATAL);
}
static int
archive_read_format_rar_read_data_skip(struct archive_read *a)
{
struct rar *rar;
int64_t bytes_skipped;
rar = (struct rar *)(a->format->data);
if (rar->bytes_unconsumed > 0) {
/* Consume as much as the decompressor actually used. */
__archive_read_consume(a, rar->bytes_unconsumed);
rar->bytes_unconsumed = 0;
}
if (rar->bytes_remaining > 0) {
bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
if (bytes_skipped < 0)
return (ARCHIVE_FATAL);
}
return (ARCHIVE_OK);
}
static int
archive_read_format_rar_cleanup(struct archive_read *a)
{
struct rar *rar;
rar = (struct rar *)(a->format->data);
free_codes(a);
free(rar->unp_buffer);
free(rar->lzss.window);
Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
free(rar);
(a->format->data) = NULL;
return (ARCHIVE_OK);
}
static int
read_header(struct archive_read *a, struct archive_entry *entry,
char head_type)
{
const void *h;
const char *p, *endp;
struct rar *rar;
struct rar_header rar_header;
struct rar_file_header file_header;
int64_t header_size;
unsigned filename_size, end;
char *filename;
char *strp;
char packed_size[8];
char unp_size[8];
int time;
struct archive_string_conv *sconv;
int ret = (ARCHIVE_OK), ret2;
rar = (struct rar *)(a->format->data);
if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
memcpy(&rar_header, p, sizeof(rar_header));
rar->file_flags = archive_le16dec(rar_header.flags);
header_size = archive_le16dec(rar_header.size);
if (header_size < sizeof(file_header) + 7) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid header size");
return (ARCHIVE_FATAL);
}
__archive_read_consume(a, 7);
if (!(rar->file_flags & FHD_SOLID))
{
rar->compression_method = 0;
rar->packed_size = 0;
rar->unp_size = 0;
rar->mtime = 0;
rar->ctime = 0;
rar->atime = 0;
rar->arctime = 0;
rar->mode = 0;
memset(&rar->salt, 0, sizeof(rar->salt));
rar->atime = 0;
rar->ansec = 0;
rar->ctime = 0;
rar->cnsec = 0;
rar->mtime = 0;
rar->mnsec = 0;
rar->arctime = 0;
rar->arcnsec = 0;
}
else
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"RAR solid archive support unavailable.");
return (ARCHIVE_FATAL);
}
if ((h = __archive_read_ahead(a, header_size - 7, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
endp = p + header_size - 7;
memcpy(&file_header, p, sizeof(file_header));
p += sizeof(file_header);
rar->compression_method = file_header.method;
time = archive_le32dec(file_header.file_time);
rar->mtime = get_time(time);
if (rar->file_flags & FHD_PASSWORD)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"RAR encryption support unavailable.");
return (ARCHIVE_FATAL);
}
if (rar->file_flags & FHD_LARGE)
{
memcpy(packed_size, file_header.pack_size, 4);
memcpy(packed_size + 4, p, 4); /* High pack size */
p += 4;
memcpy(unp_size, file_header.unp_size, 4);
memcpy(unp_size + 4, p, 4); /* High unpack size */
p += 4;
rar->packed_size = archive_le64dec(&packed_size);
rar->unp_size = archive_le64dec(&unp_size);
}
else
{
rar->packed_size = archive_le32dec(file_header.pack_size);
rar->unp_size = archive_le32dec(file_header.unp_size);
}
/* TODO: Need to use CRC check for these kind of cases.
* For now, check if sizes are not < 0.
*/
if (rar->packed_size < 0 || rar->unp_size < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid sizes specified.");
return (ARCHIVE_FATAL);
}
/* TODO: RARv3 subblocks contain comments. For now the complete block is
* consumed at the end.
*/
if (head_type == NEWSUB_HEAD) {
size_t distance = p - (const char *)h;
header_size += rar->packed_size;
/* Make sure we have the extended data. */
if ((h = __archive_read_ahead(a, header_size - 7, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
endp = p + header_size - 7;
p += distance;
}
filename_size = archive_le16dec(file_header.name_size);
if (p + filename_size > endp) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid filename size");
return (ARCHIVE_FATAL);
}
if ((filename = malloc(filename_size+2)) == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Couldn't allocate memory.");
return (ARCHIVE_FATAL);
}
memcpy(filename, p, filename_size);
filename[filename_size] = '\0';
if (rar->file_flags & FHD_UNICODE)
{
unsigned offset;
if (filename_size != strlen(filename))
{
unsigned char highbyte, flagbits, flagbyte, length;
end = filename_size;
filename_size = 0;
offset = strlen(filename) + 1;
highbyte = *(p + offset++);
flagbits = 0;
flagbyte = 0;
while (offset < end && filename_size < end)
{
if (!flagbits)
{
flagbyte = *(p + offset++);
flagbits = 8;
}
flagbits -= 2;
switch((flagbyte >> flagbits) & 3)
{
case 0:
filename[filename_size++] = '\0';
filename[filename_size++] = *(p + offset++);
break;
case 1:
filename[filename_size++] = highbyte;
filename[filename_size++] = *(p + offset++);
break;
case 2:
filename[filename_size++] = *(p + offset + 1);
filename[filename_size++] = *(p + offset);
offset += 2;
break;
case 3:
{
length = *(p + offset++);
while (length)
{
if (filename_size >= end)
break;
filename[filename_size++] = *(p + offset);
length--;
}
}
break;
}
}
if (filename_size >= end) {
free(filename);
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid filename");
return (ARCHIVE_FAILED);
}
filename[filename_size++] = '\0';
filename[filename_size++] = '\0';
} else
offset = 0;
sconv = archive_string_conversion_from_charset(&a->archive, "UTF-16BE", 1);
if (sconv == NULL)
{
free(filename);
return (ARCHIVE_FATAL);
}
strp = filename;
while (memcmp(strp, "\x00\x00", 2))
{
if (!memcmp(strp, "\x00\\", 2))
*(strp + 1) = '/';
strp += 2;
}
p += offset;
}
else
{
sconv = archive_string_default_conversion_for_read(&(a->archive));
while ((strp = strchr(filename, '\\')) != NULL)
*strp = '/';
p += filename_size;
}
if (rar->file_flags & FHD_SALT)
{
if (p + 8 > endp) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid header size");
return (ARCHIVE_FATAL);
}
memcpy(rar->salt, p, 8);
p += 8;
}
if (rar->file_flags & FHD_EXTTIME) {
if (read_exttime(p, rar, endp) < 0) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid header size");
return (ARCHIVE_FATAL);
}
}
__archive_read_consume(a, header_size - 7);
switch(file_header.host_os)
{
case OS_WIN32:
rar->mode = archive_le32dec(file_header.file_attr);
if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
else
rar->mode = AE_IFREG;
rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
break;
case OS_UNIX:
rar->mode = archive_le32dec(file_header.file_attr);
break;
case OS_MSDOS:
case OS_OS2:
case OS_MAC_OS:
case OS_BEOS:
default:
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Unknown file attributes from RAR file's host OS");
free(filename);
return (ARCHIVE_FATAL);
}
rar->bytes_remaining = rar->packed_size;
rar->bytes_uncopied = rar->bytes_unconsumed = 0;
rar->lzss.position = rar->dictionary_size = rar->offset = 0;
rar->br.cache_avail = 0;
rar->br.avail_in = 0;
rar->valid = 1;
rar->is_ppmd_block = 0;
rar->start_new_table = 1;
free(rar->unp_buffer);
rar->unp_buffer = NULL;
memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
/* Don't set any archive entries for non-file header types */
if (head_type == NEWSUB_HEAD)
return ret;
archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
archive_entry_set_atime(entry, rar->atime, rar->ansec);
archive_entry_set_size(entry, rar->unp_size);
archive_entry_set_mode(entry, rar->mode);
if (archive_entry_copy_pathname_l(entry, filename, filename_size, sconv))
{
if (errno == ENOMEM)
{
archive_set_error(&a->archive, ENOMEM,
"Can't allocate memory for Pathname");
return (ARCHIVE_FATAL);
}
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Pathname cannot be converted from %s to current locale.",
archive_string_conversion_charset_name(sconv));
ret = (ARCHIVE_WARN);
}
free(filename);
if (((rar->mode) & AE_IFMT) == AE_IFLNK)
{
if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
return ret2;
if (ret > ret2)
ret = ret2;
}
return ret;
}
static time_t
get_time(int time)
{
struct tm tm;
tm.tm_sec = 2 * (time & 0x1f);
tm.tm_min = (time >> 5) & 0x3f;
tm.tm_hour = (time >> 11) & 0x1f;
tm.tm_mday = (time >> 16) & 0x1f;
tm.tm_mon = ((time >> 21) & 0x0f) - 1;
tm.tm_year = ((time >> 25) & 0x7f) + 80;
tm.tm_isdst = -1;
return mktime(&tm);
}
static int
read_exttime(const char *p, struct rar *rar, const char *endp)
{
unsigned rmode, flags, rem, j, count;
int time, i;
struct tm *tm;
time_t t;
long nsec;
if (p + 2 > endp)
return (-1);
flags = archive_le16dec(p);
p += 2;
for (i = 3; i >= 0; i--)
{
t = 0;
if (i == 3)
t = rar->mtime;
rmode = flags >> i * 4;
if (rmode & 8)
{
if (!t)
{
if (p + 4 > endp)
return (-1);
time = archive_le32dec(p);
t = get_time(time);
p += 4;
}
rem = 0;
count = rmode & 3;
if (p + count > endp)
return (-1);
for (j = 0; j < count; j++)
{
rem = ((*p) << 16) | (rem >> 8);
p++;
}
tm = localtime(&t);
nsec = tm->tm_sec + rem / NS_UNIT;
if (rmode & 4)
{
tm->tm_sec++;
t = mktime(tm);
}
if (i == 3)
{
rar->mtime = t;
rar->mnsec = nsec;
}
else if (i == 2)
{
rar->ctime = t;
rar->cnsec = nsec;
}
else if (i == 1)
{
rar->atime = t;
rar->ansec = nsec;
}
else
{
rar->arctime = t;
rar->arcnsec = nsec;
}
}
}
return (0);
}
static int
read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
struct archive_string_conv *sconv)
{
const void *h;
const char *p;
struct rar *rar;
char *filename;
int ret = (ARCHIVE_OK);
rar = (struct rar *)(a->format->data);
if ((h = __archive_read_ahead(a, rar->packed_size, NULL)) == NULL)
return (ARCHIVE_FATAL);
p = h;
filename = malloc(rar->packed_size+1);
memcpy(filename, p, rar->packed_size);
filename[rar->packed_size] = '\0';
if (archive_entry_copy_symlink_l(entry, filename, strlen(filename), sconv))
{
if (errno == ENOMEM)
{
archive_set_error(&a->archive, ENOMEM,
"Can't allocate memory for link");
free(filename);
return (ARCHIVE_FATAL);
}
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"link cannot be converted from %s to current locale.",
archive_string_conversion_charset_name(sconv));
ret = (ARCHIVE_WARN);
}
free(filename);
return ret;
}
static int
read_data_stored(struct archive_read *a, const void **buff, size_t *size,
int64_t *offset)
{
struct rar *rar;
ssize_t bytes_avail;
rar = (struct rar *)(a->format->data);
if (rar->bytes_remaining == 0)
{
*buff = NULL;
*size = 0;
*offset = rar->offset;
return (ARCHIVE_EOF);
}
*buff = __archive_read_ahead(a, 1, &bytes_avail);
if (bytes_avail <= 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
return (ARCHIVE_FATAL);
}
if (bytes_avail > rar->bytes_remaining)
bytes_avail = rar->bytes_remaining;
*size = bytes_avail;
*offset = rar->offset;
rar->offset += bytes_avail;
rar->bytes_remaining -= bytes_avail;
rar->bytes_unconsumed = bytes_avail;
return (ARCHIVE_OK);
}
static int
read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
int64_t *offset)
{
struct rar *rar;
int64_t start, end, actualend;
int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
rar = (struct rar *)(a->format->data);
if (!rar->valid)
return (ARCHIVE_FAILED);
if (rar->dictionary_size && rar->offset >= rar->unp_size)
{
*buff = NULL;
*size = 0;
*offset = rar->offset;
Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
return (ARCHIVE_EOF);
}
if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
{
*offset = rar->offset;
if (rar->offset + rar->bytes_uncopied > rar->unp_size)
*size = rar->unp_size - rar->offset;
else
*size = rar->bytes_uncopied;
ret = copy_from_lzss_window(a, buff, *offset, *size);
rar->offset += *size;
rar->bytes_uncopied -= *size;
return ret;
}
if ((ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
return (ret);
if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
return (ret);
if (rar->is_ppmd_block)
{
if ((sym = Ppmd7_DecodeSymbol(&rar->ppmd7_context, &rar->range_dec.p)) < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid symbol");
return (ARCHIVE_FATAL);
}
if(sym != rar->ppmd_escape)
{
lzss_emit_literal(rar, sym);
rar->bytes_uncopied++;
}
else
{
if ((code = Ppmd7_DecodeSymbol(&rar->ppmd7_context, &rar->range_dec.p)) < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid symbol");
return (ARCHIVE_FATAL);
}
switch(code)
{
case 0:
rar->start_new_table = 1;
return read_data_compressed(a, buff, size, offset);
case 2:
*buff = NULL;
*size = 0;
*offset = rar->offset;
Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
return (ARCHIVE_EOF);
case 3:
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Parsing filters is unsupported.");
return (ARCHIVE_FAILED);
case 4:
lzss_offset = 0;
for (i = 2; i >= 0; i--)
{
if ((code = Ppmd7_DecodeSymbol(&rar->ppmd7_context,
&rar->range_dec.p)) < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid symbol");
return (ARCHIVE_FATAL);
}
lzss_offset |= code << (i * 8);
}
if ((length = Ppmd7_DecodeSymbol(&rar->ppmd7_context,
&rar->range_dec.p)) < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid symbol");
return (ARCHIVE_FATAL);
}
lzss_emit_match(rar, lzss_offset + 2, length + 32);
rar->bytes_uncopied += length + 32;
break;
case 5:
if ((length = Ppmd7_DecodeSymbol(&rar->ppmd7_context,
&rar->range_dec.p)) < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid symbol");
return (ARCHIVE_FATAL);
}
lzss_emit_match(rar, 1, length + 4);
rar->bytes_uncopied += length + 4;
break;
default:
lzss_emit_literal(rar, sym);
rar->bytes_uncopied++;
}
}
}
else
{
start = rar->offset;
end = start + rar->dictionary_size;
rar->filterstart = INT64_MAX;
if ((actualend = expand(a, end)) < 0)
return ((int)actualend);
rar->bytes_uncopied = actualend - start;
if (rar->bytes_uncopied == 0) {
/* Broken RAR files cause this case.
* NOTE: If this case were possible on a normal RAR file
* we would find out where it was actually bad and
* what we would do to solve it. */
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Internal error extracting RAR file.");
return (ARCHIVE_FAILED);
}
}
*offset = rar->offset;
if (rar->offset + rar->bytes_uncopied > rar->unp_size)
*size = rar->unp_size - rar->offset;
else
*size = rar->bytes_uncopied;
ret = copy_from_lzss_window(a, buff, *offset, *size);
rar->offset += *size;
rar->bytes_uncopied -= *size;
return ret;
}
static int
parse_codes(struct archive_read *a)
{
int i, j, val, n, r;
unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
unsigned int maxorder;
struct huffman_code precode;
struct rar *rar = (struct rar *)(a->format->data);
struct rar_br *br = &(rar->br);
free_codes(a);
/* Skip to the next byte */
rar_br_consume_unalined_bits(br);
/* PPMd block flag */
if (!rar_br_read_ahead(a, br, 1))
goto truncated_data;
if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
{
rar_br_consume(br, 1);
if (!rar_br_read_ahead(a, br, 7))
goto truncated_data;
ppmd_flags = rar_br_bits(br, 7);
rar_br_consume(br, 7);
/* Memory is allocated in MB */
if (ppmd_flags & 0x20)
{
if (!rar_br_read_ahead(a, br, 8))
goto truncated_data;
rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
rar_br_consume(br, 8);
}
if (ppmd_flags & 0x40)
{
if (!rar_br_read_ahead(a, br, 8))
goto truncated_data;
rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
rar_br_consume(br, 8);
}
else
rar->ppmd_escape = 2;
if (ppmd_flags & 0x20)
{
maxorder = (ppmd_flags & 0x1F) + 1;
if(maxorder > 16)
maxorder = 16 + (maxorder - 16) * 3;
if (maxorder == 1)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
return (ARCHIVE_FATAL);
}
rar->bytein.a = a;
rar->bytein.Read = &ppmd_read;
PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
rar->range_dec.Stream = &rar->bytein;
Ppmd7_Construct(&rar->ppmd7_context);
if (!Ppmd7_Alloc(&rar->ppmd7_context, rar->dictionary_size, &g_szalloc))
{
archive_set_error(&a->archive, ENOMEM,
"Out of memory");
return (ARCHIVE_FATAL);
}
if (!PpmdRAR_RangeDec_Init(&rar->range_dec))
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Unable to initialize PPMd range decoder");
return (ARCHIVE_FATAL);
}
Ppmd7_Init(&rar->ppmd7_context, maxorder);
}
else
{
if (!PpmdRAR_RangeDec_Init(&rar->range_dec))
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Unable to initialize PPMd range decoder");
return (ARCHIVE_FATAL);
}
}
}
else
{
rar_br_consume(br, 1);
/* Keep existing table flag */
if (!rar_br_read_ahead(a, br, 1))
goto truncated_data;
if (!rar_br_bits(br, 1))
memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
rar_br_consume(br, 1);
memset(&bitlengths, 0, sizeof(bitlengths));
for (i = 0; i < MAX_SYMBOLS;)
{
if (!rar_br_read_ahead(a, br, 4))
goto truncated_data;
bitlengths[i++] = rar_br_bits(br, 4);
rar_br_consume(br, 4);
if (bitlengths[i-1] == 0xF)
{
if (!rar_br_read_ahead(a, br, 4))
goto truncated_data;
zerocount = rar_br_bits(br, 4);
rar_br_consume(br, 4);
if (zerocount)
{
i--;
for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
bitlengths[i++] = 0;
}
}
}
memset(&precode, 0, sizeof(precode));
r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
if (r != ARCHIVE_OK)
return (r);
for (i = 0; i < HUFFMAN_TABLE_SIZE;)
{
if ((val = read_next_symbol(a, &precode)) < 0)
return (ARCHIVE_FAILED);
if (val < 16)
{
rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
i++;
}
else if (val < 18)
{
if (i == 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Internal error extracting RAR file.");
return (ARCHIVE_FAILED);
}
if(val == 16) {
if (!rar_br_read_ahead(a, br, 3))
goto truncated_data;
n = rar_br_bits(br, 3) + 3;
rar_br_consume(br, 3);
} else {
if (!rar_br_read_ahead(a, br, 7))
goto truncated_data;
n = rar_br_bits(br, 7) + 11;
rar_br_consume(br, 7);
}
for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
{
rar->lengthtable[i] = rar->lengthtable[i-1];
i++;
}
}
else
{
if(val == 18) {
if (!rar_br_read_ahead(a, br, 3))
goto truncated_data;
n = rar_br_bits(br, 3) + 3;
rar_br_consume(br, 3);
} else {
if (!rar_br_read_ahead(a, br, 7))
goto truncated_data;
n = rar_br_bits(br, 7) + 11;
rar_br_consume(br, 7);
}
for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
rar->lengthtable[i++] = 0;
}
}
r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
MAX_SYMBOL_LENGTH);
if (r != ARCHIVE_OK)
return (r);
r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
if (r != ARCHIVE_OK)
return (r);
r = create_code(a, &rar->lowoffsetcode,
&rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
if (r != ARCHIVE_OK)
return (r);
r = create_code(a, &rar->lengthcode,
&rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
if (r != ARCHIVE_OK)
return (r);
}
if (!rar->dictionary_size || !rar->lzss.window)
{
/* Seems as though dictionary sizes are not used. Even so, minimize
* memory usage as much as possible.
*/
if (rar->unp_size >= DICTIONARY_MAX_SIZE)
rar->dictionary_size = DICTIONARY_MAX_SIZE;
else
rar->dictionary_size = rar_fls(rar->unp_size) << 1;
rar->lzss.window = (unsigned char *)realloc(rar->lzss.window,
rar->dictionary_size);
memset(rar->lzss.window, 0, rar->dictionary_size);
rar->lzss.mask = rar->dictionary_size - 1;
}
rar->start_new_table = 0;
return (ARCHIVE_OK);
truncated_data:
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
rar->valid = 0;
return (ARCHIVE_FAILED);
}
static void
free_codes(struct archive_read *a)
{
struct rar *rar = (struct rar *)(a->format->data);
free(rar->maincode.tree);
free(rar->offsetcode.tree);
free(rar->lowoffsetcode.tree);
free(rar->lengthcode.tree);
free(rar->maincode.table);
free(rar->offsetcode.table);
free(rar->lowoffsetcode.table);
free(rar->lengthcode.table);
memset(&rar->maincode, 0, sizeof(rar->maincode));
memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
}
static int
read_next_symbol(struct archive_read *a, struct huffman_code *code)
{
unsigned char bit;
unsigned int bits;
int length, value, node;
struct rar *rar;
struct rar_br *br;
if (!code->table)
{
if (make_table(code) != (ARCHIVE_OK))
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Error in generating table.");
return -1;
}
}
rar = (struct rar *)(a->format->data);
br = &(rar->br);
/* Look ahead (peek) at bits */
if (!rar_br_read_ahead(a, br, code->tablesize)) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
rar->valid = 0;
return -1;
}
bits = rar_br_bits(br, code->tablesize);
length = code->table[bits].length;
value = code->table[bits].value;
if (length < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid prefix code in bitstream");
return -1;
}
if (length <= code->tablesize)
{
/* Skip length bits */
rar_br_consume(br, length);
return value;
}
/* Skip tablesize bits */
rar_br_consume(br, code->tablesize);
node = value;
while (!(code->tree[node].branches[0] ==
code->tree[node].branches[1]))
{
if (!rar_br_read_ahead(a, br, 1)) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
rar->valid = 0;
return -1;
}
bit = rar_br_bits(br, 1);
rar_br_consume(br, 1);
if (code->tree[node].branches[bit] < 0)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid prefix code in bitstream");
return -1;
}
node = code->tree[node].branches[bit];
}
return code->tree[node].branches[0];
}
static int
create_code(struct archive_read *a, struct huffman_code *code,
unsigned char *lengths, int numsymbols, char maxlength)
{
int i, j, codebits = 0, symbolsleft = numsymbols;
new_node(code);
code->numentries = 1;
code->minlength = INT_MAX;
code->maxlength = INT_MIN;
codebits = 0;
for(i = 1; i <= maxlength; i++)
{
for(j = 0; j < numsymbols; j++)
{
if (lengths[j] != i) continue;
if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
return (ARCHIVE_FAILED);
codebits++;
if (--symbolsleft <= 0) { break; break; }
}
codebits <<= 1;
}
return (ARCHIVE_OK);
}
static int
add_value(struct archive_read *a, struct huffman_code *code, int value,
int codebits, int length)
{
int repeatpos, lastnode, bitpos, bit, repeatnode, nextnode;
free(code->table);
code->table = NULL;
if(length > code->maxlength)
code->maxlength = length;
if(length < code->minlength)
code->minlength = length;
repeatpos = -1;
if (repeatpos == 0 || (repeatpos >= 0
&& (((codebits >> (repeatpos - 1)) & 3) == 0
|| ((codebits >> (repeatpos - 1)) & 3) == 3)))
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid repeat position");
return (ARCHIVE_FAILED);
}
lastnode = 0;
for (bitpos = length - 1; bitpos >= 0; bitpos--)
{
bit = (codebits >> bitpos) & 1;
/* Leaf node check */
if (code->tree[lastnode].branches[0] ==
code->tree[lastnode].branches[1])
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Prefix found");
return (ARCHIVE_FAILED);
}
if (bitpos == repeatpos)
{
/* Open branch check */
if (!(code->tree[lastnode].branches[bit] < 0))
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Invalid repeating code");
return (ARCHIVE_FAILED);
}
repeatnode = new_node(code);
nextnode = new_node(code);
/* Set branches */
code->tree[lastnode].branches[bit] = repeatnode;
code->tree[repeatnode].branches[bit] = repeatnode;
code->tree[repeatnode].branches[bit^1] = nextnode;
lastnode = nextnode;
bitpos++; /* terminating bit already handled, skip it */
}
else
{
/* Open branch check */
if (code->tree[lastnode].branches[bit] < 0)
{
new_node(code);
code->tree[lastnode].branches[bit] = code->numentries++;
}
/* set to branch */
lastnode = code->tree[lastnode].branches[bit];
}
}
if (!(code->tree[lastnode].branches[0] == -1
&& code->tree[lastnode].branches[1] == -2))
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Prefix found");
return (ARCHIVE_FAILED);
}
/* Set leaf value */
code->tree[lastnode].branches[0] = value;
code->tree[lastnode].branches[1] = value;
return (ARCHIVE_OK);
}
static int new_node(struct huffman_code *code)
{
code->tree = (struct huffman_tree_node *)realloc(code->tree,
(code->numentries + 1) * sizeof(*code->tree));
code->tree[code->numentries].branches[0] = -1;
code->tree[code->numentries].branches[1] = -2;
return 1;
}
static int
make_table(struct huffman_code *code)
{
if (code->maxlength < code->minlength || code->maxlength > 10)
code->tablesize = 10;
else
code->tablesize = code->maxlength;
code->table =
(struct huffman_table_entry *)malloc(sizeof(*code->table)
* (1 << code->tablesize));
return make_table_recurse(code, 0, code->table, 0, code->tablesize);
}
static int
make_table_recurse(struct huffman_code *code, int node,
struct huffman_table_entry *table, int depth,
int maxdepth)
{
int currtablesize, i, ret = (ARCHIVE_OK);
currtablesize = 1 << (maxdepth - depth);
if (!code->tree)
return (ARCHIVE_FATAL);
if (code->tree[node].branches[0] ==
code->tree[node].branches[1])
{
for(i = 0; i < currtablesize; i++)
{
table[i].length = depth;
table[i].value = code->tree[node].branches[0];
}
}
else if (node < 0)
{
for(i = 0; i < currtablesize; i++)
table[i].length = -1;
}
else
{
if(depth == maxdepth)
{
table[0].length = maxdepth + 1;
table[0].value = node;
}
else
{
ret |= make_table_recurse(code, code->tree[node].branches[0], table,
depth + 1, maxdepth);
ret |= make_table_recurse(code, code->tree[node].branches[1],
table + currtablesize / 2, depth + 1, maxdepth);
}
}
return ret;
}
static int64_t
expand(struct archive_read *a, int64_t end)
{
static const unsigned char lengthbases[] =
{ 0, 1, 2, 3, 4, 5, 6,
7, 8, 10, 12, 14, 16, 20,
24, 28, 32, 40, 48, 56, 64,
80, 96, 112, 128, 160, 192, 224 };
static const unsigned char lengthbits[] =
{ 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 2, 2,
2, 2, 3, 3, 3, 3, 4,
4, 4, 4, 5, 5, 5, 5 };
static const unsigned int offsetbases[] =
{ 0, 1, 2, 3, 4, 6,
8, 12, 16, 24, 32, 48,
64, 96, 128, 192, 256, 384,
512, 768, 1024, 1536, 2048, 3072,
4096, 6144, 8192, 12288, 16384, 24576,
32768, 49152, 65536, 98304, 131072, 196608,
262144, 327680, 393216, 458752, 524288, 589824,
655360, 720896, 786432, 851968, 917504, 983040,
1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
static const unsigned char offsetbits[] =
{ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
static const unsigned char shortbases[] =
{ 0, 4, 8, 16, 32, 64, 128, 192 };
static const unsigned char shortbits[] =
{ 2, 2, 3, 4, 5, 6, 6, 6 };
int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
unsigned char newfile;
struct rar *rar = (struct rar *)(a->format->data);
struct rar_br *br = &(rar->br);
if (rar->filterstart < end)
end = rar->filterstart;
while (1)
{
if (rar->output_last_match &&
lzss_position(&rar->lzss) + rar->lastlength <= end)
{
lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
rar->output_last_match = 0;
}
if(rar->output_last_match || lzss_position(&rar->lzss) >= end)
return lzss_position(&rar->lzss);
if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
return (ARCHIVE_FAILED);
rar->output_last_match = 0;
if (symbol < 256)
{
lzss_emit_literal(rar, symbol);
continue;
}
else if (symbol == 256)
{
if (!rar_br_read_ahead(a, br, 1))
goto truncated_data;
newfile = !rar_br_bits(br, 1);
rar_br_consume(br, 1);
if(newfile)
{
rar->start_new_block = 1;
if (!rar_br_read_ahead(a, br, 1))
goto truncated_data;
rar->start_new_table = rar_br_bits(br, 1);
rar_br_consume(br, 1);
return lzss_position(&rar->lzss);
}
else
{
if (parse_codes(a) != ARCHIVE_OK)
return (ARCHIVE_FAILED);
continue;
}
}
else if(symbol==257)
{
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Parsing filters is unsupported.");
return (ARCHIVE_FAILED);
}
else if(symbol==258)
{
if(rar->lastlength == 0)
continue;
offs = rar->lastoffset;
len = rar->lastlength;
}
else if (symbol <= 262)
{
offsindex = symbol - 259;
offs = rar->oldoffset[offsindex];
if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
goto bad_data;
if (lensymbol > sizeof(lengthbases)/sizeof(lengthbases[0]))
goto bad_data;
if (lensymbol > sizeof(lengthbits)/sizeof(lengthbits[0]))
goto bad_data;
len = lengthbases[lensymbol] + 2;
if (lengthbits[lensymbol] > 0) {
if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
goto truncated_data;
len += rar_br_bits(br, lengthbits[lensymbol]);
rar_br_consume(br, lengthbits[lensymbol]);
}
for (i = offsindex; i > 0; i--)
rar->oldoffset[i] = rar->oldoffset[i-1];
rar->oldoffset[0] = offs;
}
else if(symbol<=270)
{
offs = shortbases[symbol-263] + 1;
if(shortbits[symbol-263] > 0) {
if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
goto truncated_data;
offs += rar_br_bits(br, shortbits[symbol-263]);
rar_br_consume(br, shortbits[symbol-263]);
}
len = 2;
for(i = 3; i > 0; i--)
rar->oldoffset[i] = rar->oldoffset[i-1];
rar->oldoffset[0] = offs;
}
else
{
if (symbol-271 > sizeof(lengthbases)/sizeof(lengthbases[0]))
goto bad_data;
if (symbol-271 > sizeof(lengthbits)/sizeof(lengthbits[0]))
goto bad_data;
len = lengthbases[symbol-271]+3;
if(lengthbits[symbol-271] > 0) {
if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
goto truncated_data;
len += rar_br_bits(br, lengthbits[symbol-271]);
rar_br_consume(br, lengthbits[symbol-271]);
}
if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
goto bad_data;
if (offssymbol > sizeof(offsetbases)/sizeof(offsetbases[0]))
goto bad_data;
if (offssymbol > sizeof(offsetbits)/sizeof(offsetbits[0]))
goto bad_data;
offs = offsetbases[offssymbol]+1;
if(offsetbits[offssymbol] > 0)
{
if(offssymbol > 9)
{
if(offsetbits[offssymbol] > 4) {
if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
goto truncated_data;
offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
rar_br_consume(br, offsetbits[offssymbol] - 4);
}
if(rar->numlowoffsetrepeats > 0)
{
rar->numlowoffsetrepeats--;
offs += rar->lastlowoffset;
}
else
{
if ((lowoffsetsymbol =
read_next_symbol(a, &rar->lowoffsetcode)) < 0)
return (ARCHIVE_FAILED);
if(lowoffsetsymbol == 16)
{
rar->numlowoffsetrepeats = 15;
offs += rar->lastlowoffset;
}
else
{
offs += lowoffsetsymbol;
rar->lastlowoffset = lowoffsetsymbol;
}
}
}
else {
if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
goto truncated_data;
offs += rar_br_bits(br, offsetbits[offssymbol]);
rar_br_consume(br, offsetbits[offssymbol]);
}
}
if (offs >= 0x40000)
len++;
if (offs >= 0x2000)
len++;
for(i = 3; i > 0; i--)
rar->oldoffset[i] = rar->oldoffset[i-1];
rar->oldoffset[0] = offs;
}
rar->lastoffset = offs;
rar->lastlength = len;
rar->output_last_match = 1;
}
truncated_data:
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated RAR file data");
rar->valid = 0;
return (ARCHIVE_FAILED);
bad_data:
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Bad RAR file data");
return (ARCHIVE_FAILED);
}
static int
copy_from_lzss_window(struct archive_read *a, const void **buffer,
int64_t startpos, int length)
{
int windowoffs, firstpart;
struct rar *rar = (struct rar *)(a->format->data);
if (!rar->unp_buffer)
{
if ((rar->unp_buffer = malloc(rar->dictionary_size)) == NULL)
{
archive_set_error(&a->archive, ENOMEM,
"Unable to allocate memory for uncompressed data.");
return (ARCHIVE_FATAL);
}
}
windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
if(windowoffs + length <= lzss_size(&rar->lzss))
*buffer = &rar->lzss.window[windowoffs];
else
{
firstpart = lzss_size(&rar->lzss) - windowoffs;
memcpy(&rar->unp_buffer[0], &rar->lzss.window[windowoffs], firstpart);
memcpy(&rar->unp_buffer[firstpart], &rar->lzss.window[0],
length - firstpart);
*buffer = rar->unp_buffer;
}
return (ARCHIVE_OK);
}
|