Browse code

Merge branch 'master' into 0.98

Conflicts:
unit_tests/check_clamav.c

Shawn Webb authored on 2013/10/15 23:47:42
Showing 40 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Oct 11 16:50:34 2013 EDT 2013 (dar)
2
+------------------------------------
3
+ * bb #9072: clamscan message separator fix
4
+
1 5
 Thu Oct 3 10:49:11 2013 EDT 2013 (dar)
2 6
 ------------------------------------
3 7
  * bb #1570: Support ADC compression in DMG
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- *  Copyright (C) 2007-2012 Sourcefire, Inc.
2
+ *  Copyright (C) 2007-2013 Sourcefire, Inc.
3 3
  *
4 4
  *  Authors: Tomasz Kojm
5 5
  *
... ...
@@ -908,8 +908,8 @@ int scanmanager(const struct optstruct *opts)
908 908
 
909 909
 	while((filename = filelist(opts, &ret)) && (file = strdup(filename))) {
910 910
 	    if(LSTAT(file, &sb) == -1) {
911
-		logg("^%s: Can't access file\n", file);
912 911
 		perror(file);
912
+		logg("^%s: Can't access file\n", file);
913 913
 		ret = 2;
914 914
 	    } else {
915 915
 		for(i = strlen(file) - 1; i > 0; i--) {
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
2
+ *  Copyright (C) 2002 - 2013 Sourcefire, Inc.
3 3
  *
4 4
  *  This program is free software; you can redistribute it and/or modify
5 5
  *  it under the terms of the GNU General Public License version 2 as
... ...
@@ -82,8 +82,8 @@ clamd_connect (const char *cfgfile, const char *option)
82 82
 
83 83
         if ((sockd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
84 84
         {
85
-            logg ("^Clamd was NOT notified: Can't create socket endpoint for %s\n", opt->strarg);
86 85
             perror ("socket()");
86
+            logg ("^Clamd was NOT notified: Can't create socket endpoint for %s\n", opt->strarg);
87 87
             optfree (opts);
88 88
             return -1;
89 89
         }
... ...
@@ -92,9 +92,9 @@ clamd_connect (const char *cfgfile, const char *option)
92 92
             (sockd, (struct sockaddr *) &server,
93 93
              sizeof (struct sockaddr_un)) < 0)
94 94
         {
95
+            perror ("connect()");
95 96
             closesocket (sockd);
96 97
             logg ("^Clamd was NOT notified: Can't connect to clamd through %s\n", opt->strarg);
97
-            perror ("connect()");
98 98
             optfree (opts);
99 99
             return -11;
100 100
         }
... ...
@@ -160,8 +160,8 @@ clamd_connect (const char *cfgfile, const char *option)
160 160
 
161 161
         if ((sockd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
162 162
         {
163
-            logg ("!%s: Can't create TCP socket\n", option);
164 163
             perror ("socket()");
164
+            logg ("!%s: Can't create TCP socket\n", option);
165 165
             optfree (opts);
166 166
             return -1;
167 167
         }
... ...
@@ -189,9 +189,9 @@ clamd_connect (const char *cfgfile, const char *option)
189 189
             (sockd, (struct sockaddr *) &server2,
190 190
              sizeof (struct sockaddr_in)) < 0)
191 191
         {
192
+            perror ("connect()");
192 193
             closesocket (sockd);
193 194
             logg ("^Clamd was NOT notified: Can't connect to clamd on %s:%d\n", inet_ntoa (server2.sin_addr), ntohs (server2.sin_port));
194
-            perror ("connect()");
195 195
             optfree (opts);
196 196
             return -1;
197 197
         }
... ...
@@ -222,8 +222,8 @@ notify (const char *cfgfile)
222 222
 
223 223
     if (sendln (sockd, "RELOAD", 7) < 0)
224 224
     {
225
-        logg ("!NotifyClamd: Could not write to clamd socket\n");
226 225
         perror ("send()");
226
+        logg ("!NotifyClamd: Could not write to clamd socket\n");
227 227
         closesocket (sockd);
228 228
         return 1;
229 229
     }
... ...
@@ -1,23 +1,22 @@
1
-/* 7zCrc.c -- CRC32 calculation
2
-2009-11-23 : Igor Pavlov : Public domain */
3
-
4
-#include "7zCrc.h"
5
-#include "CpuArch.h"
6
-
7
-const UInt32 g_CrcTable[256] = { 0x0, 0x77073096, 0xee0e612c, 0x990951ba, 0x76dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0xedb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x9b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x1db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x6b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0xf00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x86d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x3b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x4db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0xd6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0xa00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x26d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x5005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0xcb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0xbdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d };
8
-
9
-
10
-#define CRC_UPDATE_BYTE_2(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
11
-
12
-UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size)
13
-{
14
-  const Byte *p = (const Byte *)data;
15
-  for (; size > 0; size--, p++)
16
-    v = CRC_UPDATE_BYTE_2(v, *p);
17
-  return v;
18
-}
19
-
20
-UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size)
21
-{
22
-  return CrcUpdate(CRC_INIT_VAL, data, size) ^ CRC_INIT_VAL;
23
-}
1
+/* 7zCrc.c -- CRC32 calculation
2
+2009-11-23 : Igor Pavlov : Public domain */
3
+
4
+#include "7zCrc.h"
5
+#include "CpuArch.h"
6
+
7
+const UInt32 g_CrcTable[256] = { 0x0, 0x77073096, 0xee0e612c, 0x990951ba, 0x76dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0xedb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x9b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x1db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x6b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0xf00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x86d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x3b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x4db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0xd6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0xa00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x26d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x5005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0xcb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0xbdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d };
8
+
9
+#define CRC_UPDATE_BYTE_2(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
10
+
11
+UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size)
12
+{
13
+  const Byte *p = (const Byte *)data;
14
+  for (; size > 0; size--, p++)
15
+    v = CRC_UPDATE_BYTE_2(v, *p);
16
+  return v;
17
+}
18
+
19
+UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size)
20
+{
21
+  return CrcUpdate(CRC_INIT_VAL, data, size) ^ CRC_INIT_VAL;
22
+}
... ...
@@ -1,18 +1,22 @@
1
-/* 7zCrc.h -- CRC32 calculation
2
-2009-11-21 : Igor Pavlov : Public domain */
3
-
4
-#ifndef __7Z_CRC_H
5
-#define __7Z_CRC_H
6
-
7
-#include "Types.h"
8
-
9
-EXTERN_C_BEGIN
10
-
11
-#define CRC_INIT_VAL 0xFFFFFFFF
12
-
13
-UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size);
14
-UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size);
15
-
16
-EXTERN_C_END
17
-
18
-#endif
1
+/* 7zCrc.h -- CRC32 calculation
2
+2009-11-21 : Igor Pavlov : Public domain */
3
+
4
+#ifndef __7Z_CRC_H
5
+#define __7Z_CRC_H
6
+
7
+#include "Types.h"
8
+
9
+EXTERN_C_BEGIN
10
+
11
+extern const UInt32 g_CrcTable[];
12
+
13
+#define CRC_INIT_VAL 0xFFFFFFFF
14
+#define CRC_GET_DIGEST(crc) ((crc) ^ CRC_INIT_VAL)
15
+#define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
16
+
17
+UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size);
18
+UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size);
19
+
20
+EXTERN_C_END
21
+
22
+#endif
19 23
new file mode 100755
... ...
@@ -0,0 +1,34 @@
0
+/* 7zCrcOpt.c -- CRC32 calculation : optimized version
1
+2009-11-23 : Igor Pavlov : Public domain */
2
+
3
+#include "CpuArch.h"
4
+
5
+#ifdef MY_CPU_LE
6
+
7
+#define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
8
+
9
+UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
10
+{
11
+  const Byte *p = (const Byte *)data;
12
+  for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
13
+    v = CRC_UPDATE_BYTE_2(v, *p);
14
+  for (; size >= 4; size -= 4, p += 4)
15
+  {
16
+    v ^= *(const UInt32 *)p;
17
+    v =
18
+      table[0x300 + (v & 0xFF)] ^
19
+      table[0x200 + ((v >> 8) & 0xFF)] ^
20
+      table[0x100 + ((v >> 16) & 0xFF)] ^
21
+      table[0x000 + ((v >> 24))];
22
+  }
23
+  for (; size > 0; size--, p++)
24
+    v = CRC_UPDATE_BYTE_2(v, *p);
25
+  return v;
26
+}
27
+
28
+UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
29
+{
30
+  return CrcUpdateT4(v, data, size, table);
31
+}
32
+
33
+#endif
0 34
new file mode 100755
... ...
@@ -0,0 +1,38 @@
0
+/* Alloc.h -- Memory allocation functions
1
+2009-02-07 : Igor Pavlov : Public domain */
2
+
3
+#ifndef __COMMON_ALLOC_H
4
+#define __COMMON_ALLOC_H
5
+
6
+#include <stddef.h>
7
+
8
+#ifdef __cplusplus
9
+extern "C" {
10
+#endif
11
+
12
+void *MyAlloc(size_t size);
13
+void MyFree(void *address);
14
+
15
+#ifdef _WIN32
16
+
17
+void SetLargePageSize();
18
+
19
+void *MidAlloc(size_t size);
20
+void MidFree(void *address);
21
+void *BigAlloc(size_t size);
22
+void BigFree(void *address);
23
+
24
+#else
25
+
26
+#define MidAlloc(size) MyAlloc(size)
27
+#define MidFree(address) MyFree(address)
28
+#define BigAlloc(size) MyAlloc(size)
29
+#define BigFree(address) MyFree(address)
30
+
31
+#endif
32
+
33
+#ifdef __cplusplus
34
+}
35
+#endif
36
+
37
+#endif
0 38
new file mode 100755
... ...
@@ -0,0 +1,67 @@
0
+/* BraIA64.c -- Converter for IA-64 code
1
+2008-10-04 : Igor Pavlov : Public domain */
2
+
3
+#include "Bra.h"
4
+
5
+static const Byte kBranchTable[32] =
6
+{
7
+  0, 0, 0, 0, 0, 0, 0, 0,
8
+  0, 0, 0, 0, 0, 0, 0, 0,
9
+  4, 4, 6, 6, 0, 0, 7, 7,
10
+  4, 4, 0, 0, 4, 4, 0, 0
11
+};
12
+
13
+SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
14
+{
15
+  SizeT i;
16
+  if (size < 16)
17
+    return 0;
18
+  size -= 16;
19
+  for (i = 0; i <= size; i += 16)
20
+  {
21
+    UInt32 instrTemplate = data[i] & 0x1F;
22
+    UInt32 mask = kBranchTable[instrTemplate];
23
+    UInt32 bitPos = 5;
24
+    int slot;
25
+    for (slot = 0; slot < 3; slot++, bitPos += 41)
26
+    {
27
+      UInt32 bytePos, bitRes;
28
+      UInt64 instruction, instNorm;
29
+      int j;
30
+      if (((mask >> slot) & 1) == 0)
31
+        continue;
32
+      bytePos = (bitPos >> 3);
33
+      bitRes = bitPos & 0x7;
34
+      instruction = 0;
35
+      for (j = 0; j < 6; j++)
36
+        instruction += (UInt64)data[i + j + bytePos] << (8 * j);
37
+
38
+      instNorm = instruction >> bitRes;
39
+      if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0)
40
+      {
41
+        UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF);
42
+        UInt32 dest;
43
+        src |= ((UInt32)(instNorm >> 36) & 1) << 20;
44
+        
45
+        src <<= 4;
46
+        
47
+        if (encoding)
48
+          dest = ip + (UInt32)i + src;
49
+        else
50
+          dest = src - (ip + (UInt32)i);
51
+        
52
+        dest >>= 4;
53
+        
54
+        instNorm &= ~((UInt64)(0x8FFFFF) << 13);
55
+        instNorm |= ((UInt64)(dest & 0xFFFFF) << 13);
56
+        instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20));
57
+        
58
+        instruction &= (1 << bitRes) - 1;
59
+        instruction |= (instNorm << bitRes);
60
+        for (j = 0; j < 6; j++)
61
+          data[i + j + bytePos] = (Byte)(instruction >> (8 * j));
62
+      }
63
+    }
64
+  }
65
+  return i;
66
+}
0 67
new file mode 100755
... ...
@@ -0,0 +1,168 @@
0
+/* CpuArch.c -- CPU specific code
1
+2010-10-26: Igor Pavlov : Public domain */
2
+
3
+#include "CpuArch.h"
4
+
5
+#ifdef MY_CPU_X86_OR_AMD64
6
+
7
+#if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
8
+#define USE_ASM
9
+#endif
10
+
11
+#if defined(USE_ASM) && !defined(MY_CPU_AMD64)
12
+static UInt32 CheckFlag(UInt32 flag)
13
+{
14
+  #ifdef _MSC_VER
15
+  __asm pushfd;
16
+  __asm pop EAX;
17
+  __asm mov EDX, EAX;
18
+  __asm xor EAX, flag;
19
+  __asm push EAX;
20
+  __asm popfd;
21
+  __asm pushfd;
22
+  __asm pop EAX;
23
+  __asm xor EAX, EDX;
24
+  __asm push EDX;
25
+  __asm popfd;
26
+  __asm and flag, EAX;
27
+  #else
28
+  __asm__ __volatile__ (
29
+    "pushf\n\t"
30
+    "pop  %%EAX\n\t"
31
+    "movl %%EAX,%%EDX\n\t"
32
+    "xorl %0,%%EAX\n\t"
33
+    "push %%EAX\n\t"
34
+    "popf\n\t"
35
+    "pushf\n\t"
36
+    "pop  %%EAX\n\t"
37
+    "xorl %%EDX,%%EAX\n\t"
38
+    "push %%EDX\n\t"
39
+    "popf\n\t"
40
+    "andl %%EAX, %0\n\t":
41
+    "=c" (flag) : "c" (flag));
42
+  #endif
43
+  return flag;
44
+}
45
+#define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
46
+#else
47
+#define CHECK_CPUID_IS_SUPPORTED
48
+#endif
49
+
50
+static void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
51
+{
52
+  #ifdef USE_ASM
53
+
54
+  #ifdef _MSC_VER
55
+
56
+  UInt32 a2, b2, c2, d2;
57
+  __asm xor EBX, EBX;
58
+  __asm xor ECX, ECX;
59
+  __asm xor EDX, EDX;
60
+  __asm mov EAX, function;
61
+  __asm cpuid;
62
+  __asm mov a2, EAX;
63
+  __asm mov b2, EBX;
64
+  __asm mov c2, ECX;
65
+  __asm mov d2, EDX;
66
+
67
+  *a = a2;
68
+  *b = b2;
69
+  *c = c2;
70
+  *d = d2;
71
+
72
+  #else
73
+
74
+  __asm__ __volatile__ (
75
+    "cpuid"
76
+    : "=a" (*a) ,
77
+      "=b" (*b) ,
78
+      "=c" (*c) ,
79
+      "=d" (*d)
80
+    : "0" (function)) ;
81
+
82
+  #endif
83
+  
84
+  #else
85
+
86
+  int CPUInfo[4];
87
+  __cpuid(CPUInfo, function);
88
+  *a = CPUInfo[0];
89
+  *b = CPUInfo[1];
90
+  *c = CPUInfo[2];
91
+  *d = CPUInfo[3];
92
+
93
+  #endif
94
+}
95
+
96
+Bool x86cpuid_CheckAndRead(Cx86cpuid *p)
97
+{
98
+  CHECK_CPUID_IS_SUPPORTED
99
+  MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
100
+  MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
101
+  return True;
102
+}
103
+
104
+static UInt32 kVendors[][3] =
105
+{
106
+  { 0x756E6547, 0x49656E69, 0x6C65746E},
107
+  { 0x68747541, 0x69746E65, 0x444D4163},
108
+  { 0x746E6543, 0x48727561, 0x736C7561}
109
+};
110
+
111
+int x86cpuid_GetFirm(const Cx86cpuid *p)
112
+{
113
+  unsigned i;
114
+  for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
115
+  {
116
+    const UInt32 *v = kVendors[i];
117
+    if (v[0] == p->vendor[0] &&
118
+        v[1] == p->vendor[1] &&
119
+        v[2] == p->vendor[2])
120
+      return (int)i;
121
+  }
122
+  return -1;
123
+}
124
+
125
+Bool CPU_Is_InOrder()
126
+{
127
+  Cx86cpuid p;
128
+  int firm;
129
+  UInt32 family, model;
130
+  if (!x86cpuid_CheckAndRead(&p))
131
+    return True;
132
+  family = x86cpuid_GetFamily(&p);
133
+  model = x86cpuid_GetModel(&p);
134
+  firm = x86cpuid_GetFirm(&p);
135
+  switch (firm)
136
+  {
137
+    case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && model == 0x100C));
138
+    case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
139
+    case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
140
+  }
141
+  return True;
142
+}
143
+
144
+#if !defined(MY_CPU_AMD64) && defined(_WIN32)
145
+static Bool CPU_Sys_Is_SSE_Supported()
146
+{
147
+  OSVERSIONINFO vi;
148
+  vi.dwOSVersionInfoSize = sizeof(vi);
149
+  if (!GetVersionEx(&vi))
150
+    return False;
151
+  return (vi.dwMajorVersion >= 5);
152
+}
153
+#define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
154
+#else
155
+#define CHECK_SYS_SSE_SUPPORT
156
+#endif
157
+
158
+Bool CPU_Is_Aes_Supported()
159
+{
160
+  Cx86cpuid p;
161
+  CHECK_SYS_SSE_SUPPORT
162
+  if (!x86cpuid_CheckAndRead(&p))
163
+    return False;
164
+  return (p.c >> 25) & 1;
165
+}
166
+
167
+#endif
... ...
@@ -1,37 +1,37 @@
1
-/* CpuArch.h -- CPU specific code
2
-2010-10-26: Igor Pavlov : Public domain */
3
-
4
-#ifndef __CPU_ARCH_H
5
-#define __CPU_ARCH_H
6
-
7
-#include "Types.h"
8
-#include "others.h"
9
-
10
-EXTERN_C_BEGIN
11
-
12
-/*
13
-MY_CPU_LE means that CPU is LITTLE ENDIAN.
14
-If MY_CPU_LE is not defined, we don't know about that property of platform (it can be LITTLE ENDIAN).
15
-
16
-MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned memory accesses.
17
-If MY_CPU_LE_UNALIGN is not defined, we don't know about these properties of platform.
18
-*/
19
-
20
-#define GetUi16(p) (cli_readint16(p))
21
-#define GetUi32(p) (cli_readint32(p))
22
-
23
-#define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))
24
-
25
-#define SetUi16(p, d) { UInt32 _x_ = (d); \
26
-    ((Byte *)(p))[0] = (Byte)_x_; \
27
-    ((Byte *)(p))[1] = (Byte)(_x_ >> 8); }
28
-
29
-#define SetUi32(p, d) (cli_writeint32(p, d))
30
-
31
-#define SetUi64(p, d) { UInt64 _x64_ = (d); \
32
-    SetUi32(p, (UInt32)_x64_); \
33
-    SetUi32(((Byte *)(p)) + 4, (UInt32)(_x64_ >> 32)); }
34
-
35
-EXTERN_C_END
36
-
37
-#endif
1
+/* CpuArch.h -- CPU specific code
2
+2010-10-26: Igor Pavlov : Public domain */
3
+
4
+#ifndef __CPU_ARCH_H
5
+#define __CPU_ARCH_H
6
+
7
+#include "Types.h"
8
+#include "others.h"
9
+
10
+EXTERN_C_BEGIN
11
+
12
+/*
13
+MY_CPU_LE means that CPU is LITTLE ENDIAN.
14
+If MY_CPU_LE is not defined, we don't know about that property of platform (it can be LITTLE ENDIAN).
15
+
16
+MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned memory accesses.
17
+If MY_CPU_LE_UNALIGN is not defined, we don't know about these properties of platform.
18
+*/
19
+
20
+#define GetUi16(p) (cli_readint16(p))
21
+#define GetUi32(p) (cli_readint32(p))
22
+
23
+#define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))
24
+
25
+#define SetUi16(p, d) { UInt32 _x_ = (d); \
26
+    ((Byte *)(p))[0] = (Byte)_x_; \
27
+    ((Byte *)(p))[1] = (Byte)(_x_ >> 8); }
28
+
29
+#define SetUi32(p, d) (cli_writeint32(p, d))
30
+
31
+#define SetUi64(p, d) { UInt64 _x64_ = (d); \
32
+    SetUi32(p, (UInt32)_x64_); \
33
+    SetUi32(((Byte *)(p)) + 4, (UInt32)(_x64_ >> 32)); }
34
+
35
+#define GetBe16(p) (((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1])
36
+
37
+#endif
38 38
new file mode 100755
... ...
@@ -0,0 +1,62 @@
0
+/* Delta.c -- Delta converter
1
+2009-05-26 : Igor Pavlov : Public domain */
2
+
3
+#include "Delta.h"
4
+
5
+void Delta_Init(Byte *state)
6
+{
7
+  unsigned i;
8
+  for (i = 0; i < DELTA_STATE_SIZE; i++)
9
+    state[i] = 0;
10
+}
11
+
12
+static void MyMemCpy(Byte *dest, const Byte *src, unsigned size)
13
+{
14
+  unsigned i;
15
+  for (i = 0; i < size; i++)
16
+    dest[i] = src[i];
17
+}
18
+
19
+void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size)
20
+{
21
+  Byte buf[DELTA_STATE_SIZE];
22
+  unsigned j = 0;
23
+  MyMemCpy(buf, state, delta);
24
+  {
25
+    SizeT i;
26
+    for (i = 0; i < size;)
27
+    {
28
+      for (j = 0; j < delta && i < size; i++, j++)
29
+      {
30
+        Byte b = data[i];
31
+        data[i] = (Byte)(b - buf[j]);
32
+        buf[j] = b;
33
+      }
34
+    }
35
+  }
36
+  if (j == delta)
37
+    j = 0;
38
+  MyMemCpy(state, buf + j, delta - j);
39
+  MyMemCpy(state + delta - j, buf, j);
40
+}
41
+
42
+void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size)
43
+{
44
+  Byte buf[DELTA_STATE_SIZE];
45
+  unsigned j = 0;
46
+  MyMemCpy(buf, state, delta);
47
+  {
48
+    SizeT i;
49
+    for (i = 0; i < size;)
50
+    {
51
+      for (j = 0; j < delta && i < size; i++, j++)
52
+      {
53
+        buf[j] = data[i] = (Byte)(buf[j] + data[i]);
54
+      }
55
+    }
56
+  }
57
+  if (j == delta)
58
+    j = 0;
59
+  MyMemCpy(state, buf + j, delta - j);
60
+  MyMemCpy(state + delta - j, buf, j);
61
+}
0 62
new file mode 100755
... ...
@@ -0,0 +1,23 @@
0
+/* Delta.h -- Delta converter
1
+2009-04-15 : Igor Pavlov : Public domain */
2
+
3
+#ifndef __DELTA_H
4
+#define __DELTA_H
5
+
6
+#include "Types.h"
7
+
8
+#ifdef __cplusplus
9
+extern "C" {
10
+#endif
11
+
12
+#define DELTA_STATE_SIZE 256
13
+
14
+void Delta_Init(Byte *state);
15
+void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size);
16
+void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size);
17
+
18
+#ifdef __cplusplus
19
+}
20
+#endif
21
+
22
+#endif
0 23
new file mode 100755
... ...
@@ -0,0 +1,20 @@
0
+/* RotateDefs.h -- Rotate functions
1
+2009-02-07 : Igor Pavlov : Public domain */
2
+
3
+#ifndef __ROTATE_DEFS_H
4
+#define __ROTATE_DEFS_H
5
+
6
+#ifdef _MSC_VER
7
+
8
+#include <stdlib.h>
9
+#define rotlFixed(x, n) _rotl((x), (n))
10
+#define rotrFixed(x, n) _rotr((x), (n))
11
+
12
+#else
13
+
14
+#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
15
+#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
16
+
17
+#endif
18
+
19
+#endif
0 20
new file mode 100755
... ...
@@ -0,0 +1,35 @@
0
+/* Sha256.h -- SHA-256 Hash
1
+2010-06-11 : Igor Pavlov : Public domain */
2
+
3
+#ifndef __CRYPTO_SHA256_H
4
+#define __CRYPTO_SHA256_H
5
+
6
+#include "Types.h"
7
+
8
+EXTERN_C_BEGIN
9
+
10
+#define SHA256_DIGEST_SIZE 32
11
+
12
+#define __USE_CLAM_SHA256_FOR_7Z__
13
+
14
+#ifndef __USE_CLAM_SHA256_FOR_7Z__
15
+typedef struct
16
+{
17
+  UInt32 state[8];
18
+  UInt64 count;
19
+  Byte buffer[64];
20
+} CSha256;
21
+
22
+void Sha256_Init(CSha256 *p);
23
+void Sha256_Update(CSha256 *p, const Byte *data, size_t size);
24
+void Sha256_Final(CSha256 *p, Byte *digest);
25
+#else
26
+#include "../sha256.h"
27
+#define CSha256 SHA256_CTX
28
+#define Sha256_Init sha256_init
29
+#define Sha256_Update sha256_update
30
+#define Sha256_Final sha256_final
31
+#endif
32
+EXTERN_C_END
33
+
34
+#endif
0 35
new file mode 100755
... ...
@@ -0,0 +1,88 @@
0
+/* Xz.c - Xz
1
+2009-04-15 : Igor Pavlov : Public domain */
2
+
3
+#include "7zCrc.h"
4
+#include "CpuArch.h"
5
+#include "Xz.h"
6
+#include "XzCrc64.h"
7
+
8
+Byte XZ_SIG[XZ_SIG_SIZE] = { 0xFD, '7', 'z', 'X', 'Z', 0 };
9
+Byte XZ_FOOTER_SIG[XZ_FOOTER_SIG_SIZE] = { 'Y', 'Z' };
10
+
11
+unsigned Xz_WriteVarInt(Byte *buf, UInt64 v)
12
+{
13
+  unsigned i = 0;
14
+  do
15
+  {
16
+    buf[i++] = (Byte)((v & 0x7F) | 0x80);
17
+    v >>= 7;
18
+  }
19
+  while (v != 0);
20
+  buf[i - 1] &= 0x7F;
21
+  return i;
22
+}
23
+
24
+void Xz_Construct(CXzStream *p)
25
+{
26
+  p->numBlocks = p->numBlocksAllocated = 0;
27
+  p->blocks = 0;
28
+  p->flags = 0;
29
+}
30
+
31
+void Xz_Free(CXzStream *p, ISzAlloc *alloc)
32
+{
33
+  alloc->Free(alloc, p->blocks);
34
+  p->numBlocks = p->numBlocksAllocated = 0;
35
+  p->blocks = 0;
36
+}
37
+
38
+unsigned XzFlags_GetCheckSize(CXzStreamFlags f)
39
+{
40
+  int t = XzFlags_GetCheckType(f);
41
+  return (t == 0) ? 0 : (4 << ((t - 1) / 3));
42
+}
43
+
44
+void XzCheck_Init(CXzCheck *p, int mode)
45
+{
46
+  p->mode = mode;
47
+  switch (mode)
48
+  {
49
+    case XZ_CHECK_CRC32: p->crc = CRC_INIT_VAL; break;
50
+    case XZ_CHECK_CRC64: p->crc64 = CRC64_INIT_VAL; break;
51
+    case XZ_CHECK_SHA256: Sha256_Init(&p->sha); break;
52
+  }
53
+}
54
+
55
+void XzCheck_Update(CXzCheck *p, const void *data, size_t size)
56
+{
57
+  switch (p->mode)
58
+  {
59
+    case XZ_CHECK_CRC32: p->crc = CrcUpdate(p->crc, data, size); break;
60
+    case XZ_CHECK_CRC64: p->crc64 = Crc64Update(p->crc64, data, size); break;
61
+    case XZ_CHECK_SHA256: Sha256_Update(&p->sha, (const Byte *)data, size); break;
62
+  }
63
+}
64
+
65
+int XzCheck_Final(CXzCheck *p, Byte *digest)
66
+{
67
+  switch (p->mode)
68
+  {
69
+    case XZ_CHECK_CRC32:
70
+      SetUi32(digest, CRC_GET_DIGEST(p->crc));
71
+      break;
72
+    case XZ_CHECK_CRC64:
73
+    {
74
+      int i;
75
+      UInt64 v = CRC64_GET_DIGEST(p->crc64);
76
+      for (i = 0; i < 8; i++, v >>= 8)
77
+        digest[i] = (Byte)(v & 0xFF);
78
+      break;
79
+    }
80
+    case XZ_CHECK_SHA256:
81
+      Sha256_Final(&p->sha, digest);
82
+      break;
83
+    default:
84
+      return 0;
85
+  }
86
+  return 1;
87
+}
0 88
new file mode 100755
... ...
@@ -0,0 +1,252 @@
0
+/* Xz.h - Xz interface
1
+2010-09-17 : Igor Pavlov : Public domain */
2
+
3
+#ifndef __XZ_H
4
+#define __XZ_H
5
+
6
+#include "Sha256.h"
7
+
8
+EXTERN_C_BEGIN
9
+
10
+#define XZ_ID_Subblock 1
11
+#define XZ_ID_Delta 3
12
+#define XZ_ID_X86 4
13
+#define XZ_ID_PPC 5
14
+#define XZ_ID_IA64 6
15
+#define XZ_ID_ARM 7
16
+#define XZ_ID_ARMT 8
17
+#define XZ_ID_SPARC 9
18
+#define XZ_ID_LZMA2 0x21
19
+
20
+unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value);
21
+unsigned Xz_WriteVarInt(Byte *buf, UInt64 v);
22
+
23
+/* ---------- xz block ---------- */
24
+
25
+#define XZ_BLOCK_HEADER_SIZE_MAX 1024
26
+
27
+#define XZ_NUM_FILTERS_MAX 4
28
+#define XZ_BF_NUM_FILTERS_MASK 3
29
+#define XZ_BF_PACK_SIZE (1 << 6)
30
+#define XZ_BF_UNPACK_SIZE (1 << 7)
31
+
32
+#define XZ_FILTER_PROPS_SIZE_MAX 20
33
+
34
+typedef struct
35
+{
36
+  UInt64 id;
37
+  UInt32 propsSize;
38
+  Byte props[XZ_FILTER_PROPS_SIZE_MAX];
39
+} CXzFilter;
40
+
41
+typedef struct
42
+{
43
+  UInt64 packSize;
44
+  UInt64 unpackSize;
45
+  Byte flags;
46
+  CXzFilter filters[XZ_NUM_FILTERS_MAX];
47
+} CXzBlock;
48
+
49
+#define XzBlock_GetNumFilters(p) (((p)->flags & XZ_BF_NUM_FILTERS_MASK) + 1)
50
+#define XzBlock_HasPackSize(p)   (((p)->flags & XZ_BF_PACK_SIZE) != 0)
51
+#define XzBlock_HasUnpackSize(p) (((p)->flags & XZ_BF_UNPACK_SIZE) != 0)
52
+
53
+SRes XzBlock_Parse(CXzBlock *p, const Byte *header);
54
+SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, Bool *isIndex, UInt32 *headerSizeRes);
55
+
56
+/* ---------- xz stream ---------- */
57
+
58
+#define XZ_SIG_SIZE 6
59
+#define XZ_FOOTER_SIG_SIZE 2
60
+
61
+extern Byte XZ_SIG[XZ_SIG_SIZE];
62
+extern Byte XZ_FOOTER_SIG[XZ_FOOTER_SIG_SIZE];
63
+
64
+#define XZ_STREAM_FLAGS_SIZE 2
65
+#define XZ_STREAM_CRC_SIZE 4
66
+
67
+#define XZ_STREAM_HEADER_SIZE (XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE + XZ_STREAM_CRC_SIZE)
68
+#define XZ_STREAM_FOOTER_SIZE (XZ_FOOTER_SIG_SIZE + XZ_STREAM_FLAGS_SIZE + XZ_STREAM_CRC_SIZE + 4)
69
+
70
+#define XZ_CHECK_MASK 0xF
71
+#define XZ_CHECK_NO 0
72
+#define XZ_CHECK_CRC32 1
73
+#define XZ_CHECK_CRC64 4
74
+#define XZ_CHECK_SHA256 10
75
+
76
+typedef struct
77
+{
78
+  int mode;
79
+  UInt32 crc;
80
+  UInt64 crc64;
81
+  CSha256 sha;
82
+} CXzCheck;
83
+
84
+void XzCheck_Init(CXzCheck *p, int mode);
85
+void XzCheck_Update(CXzCheck *p, const void *data, size_t size);
86
+int XzCheck_Final(CXzCheck *p, Byte *digest);
87
+
88
+typedef UInt16 CXzStreamFlags;
89
+
90
+#define XzFlags_IsSupported(f) ((f) <= XZ_CHECK_MASK)
91
+#define XzFlags_GetCheckType(f) ((f) & XZ_CHECK_MASK)
92
+#define XzFlags_HasDataCrc32(f) (Xz_GetCheckType(f) == XZ_CHECK_CRC32)
93
+unsigned XzFlags_GetCheckSize(CXzStreamFlags f);
94
+
95
+SRes Xz_ParseHeader(CXzStreamFlags *p, const Byte *buf);
96
+SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream);
97
+
98
+typedef struct
99
+{
100
+  UInt64 unpackSize;
101
+  UInt64 totalSize;
102
+} CXzBlockSizes;
103
+
104
+typedef struct
105
+{
106
+  CXzStreamFlags flags;
107
+  size_t numBlocks;
108
+  size_t numBlocksAllocated;
109
+  CXzBlockSizes *blocks;
110
+  UInt64 startOffset;
111
+} CXzStream;
112
+
113
+void Xz_Construct(CXzStream *p);
114
+void Xz_Free(CXzStream *p, ISzAlloc *alloc);
115
+
116
+#define XZ_SIZE_OVERFLOW ((UInt64)(Int64)-1)
117
+
118
+UInt64 Xz_GetUnpackSize(const CXzStream *p);
119
+UInt64 Xz_GetPackSize(const CXzStream *p);
120
+
121
+typedef struct
122
+{
123
+  size_t num;
124
+  size_t numAllocated;
125
+  CXzStream *streams;
126
+} CXzs;
127
+
128
+void Xzs_Construct(CXzs *p);
129
+void Xzs_Free(CXzs *p, ISzAlloc *alloc);
130
+SRes Xzs_ReadBackward(CXzs *p, ILookInStream *inStream, Int64 *startOffset, ICompressProgress *progress, ISzAlloc *alloc);
131
+
132
+UInt64 Xzs_GetNumBlocks(const CXzs *p);
133
+UInt64 Xzs_GetUnpackSize(const CXzs *p);
134
+
135
+typedef enum
136
+{
137
+  CODER_STATUS_NOT_SPECIFIED,               /* use main error code instead */
138
+  CODER_STATUS_FINISHED_WITH_MARK,          /* stream was finished with end mark. */
139
+  CODER_STATUS_NOT_FINISHED,                /* stream was not finished */
140
+  CODER_STATUS_NEEDS_MORE_INPUT             /* you must provide more input bytes */
141
+} ECoderStatus;
142
+
143
+typedef enum
144
+{
145
+  CODER_FINISH_ANY,   /* finish at any point */
146
+  CODER_FINISH_END    /* block must be finished at the end */
147
+} ECoderFinishMode;
148
+
149
+typedef struct _IStateCoder
150
+{
151
+  void *p;
152
+  void (*Free)(void *p, ISzAlloc *alloc);
153
+  SRes (*SetProps)(void *p, const Byte *props, size_t propSize, ISzAlloc *alloc);
154
+  void (*Init)(void *p);
155
+  SRes (*Code)(void *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
156
+      int srcWasFinished, ECoderFinishMode finishMode, int *wasFinished);
157
+} IStateCoder;
158
+
159
+#define MIXCODER_NUM_FILTERS_MAX 4
160
+
161
+typedef struct
162
+{
163
+  ISzAlloc *alloc;
164
+  Byte *buf;
165
+  int numCoders;
166
+  int finished[MIXCODER_NUM_FILTERS_MAX - 1];
167
+  size_t pos[MIXCODER_NUM_FILTERS_MAX - 1];
168
+  size_t size[MIXCODER_NUM_FILTERS_MAX - 1];
169
+  UInt64 ids[MIXCODER_NUM_FILTERS_MAX];
170
+  IStateCoder coders[MIXCODER_NUM_FILTERS_MAX];
171
+} CMixCoder;
172
+
173
+void MixCoder_Construct(CMixCoder *p, ISzAlloc *alloc);
174
+void MixCoder_Free(CMixCoder *p);
175
+void MixCoder_Init(CMixCoder *p);
176
+SRes MixCoder_SetFromMethod(CMixCoder *p, int coderIndex, UInt64 methodId);
177
+SRes MixCoder_Code(CMixCoder *p, Byte *dest, SizeT *destLen,
178
+    const Byte *src, SizeT *srcLen, int srcWasFinished,
179
+    ECoderFinishMode finishMode, ECoderStatus *status);
180
+
181
+typedef enum
182
+{
183
+  XZ_STATE_STREAM_HEADER,
184
+  XZ_STATE_STREAM_INDEX,
185
+  XZ_STATE_STREAM_INDEX_CRC,
186
+  XZ_STATE_STREAM_FOOTER,
187
+  XZ_STATE_STREAM_PADDING,
188
+  XZ_STATE_BLOCK_HEADER,
189
+  XZ_STATE_BLOCK,
190
+  XZ_STATE_BLOCK_FOOTER
191
+} EXzState;
192
+
193
+typedef struct
194
+{
195
+  EXzState state;
196
+  UInt32 pos;
197
+  unsigned alignPos;
198
+  unsigned indexPreSize;
199
+
200
+  CXzStreamFlags streamFlags;
201
+  
202
+  UInt32 blockHeaderSize;
203
+  UInt64 packSize;
204
+  UInt64 unpackSize;
205
+
206
+  UInt64 numBlocks;
207
+  UInt64 indexSize;
208
+  UInt64 indexPos;
209
+  UInt64 padSize;
210
+
211
+  UInt64 numStreams;
212
+
213
+  UInt32 crc;
214
+  CMixCoder decoder;
215
+  CXzBlock block;
216
+  CXzCheck check;
217
+  CSha256 sha;
218
+  Byte shaDigest[SHA256_DIGEST_SIZE];
219
+  Byte buf[XZ_BLOCK_HEADER_SIZE_MAX];
220
+} CXzUnpacker;
221
+
222
+SRes XzUnpacker_Create(CXzUnpacker *p, ISzAlloc *alloc);
223
+void XzUnpacker_Free(CXzUnpacker *p);
224
+
225
+/*
226
+finishMode:
227
+  It has meaning only if the decoding reaches output limit (*destLen).
228
+  LZMA_FINISH_ANY - use smallest number of input bytes
229
+  LZMA_FINISH_END - read EndOfStream marker after decoding
230
+
231
+Returns:
232
+  SZ_OK
233
+    status:
234
+      LZMA_STATUS_FINISHED_WITH_MARK
235
+      LZMA_STATUS_NOT_FINISHED
236
+  SZ_ERROR_DATA - Data error
237
+  SZ_ERROR_MEM  - Memory allocation error
238
+  SZ_ERROR_UNSUPPORTED - Unsupported properties
239
+  SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
240
+*/
241
+
242
+
243
+SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
244
+    const Byte *src, SizeT *srcLen, /* int srcWasFinished, */ int finishMode,
245
+    ECoderStatus *status);
246
+
247
+Bool XzUnpacker_IsStreamWasFinished(CXzUnpacker *p);
248
+
249
+EXTERN_C_END
250
+
251
+#endif
0 252
new file mode 100755
... ...
@@ -0,0 +1,33 @@
0
+/* XzCrc64.c -- CRC64 calculation
1
+2010-04-16 : Igor Pavlov : Public domain */
2
+
3
+#include "XzCrc64.h"
4
+
5
+#define kCrc64Poly UINT64_CONST(0xC96C5795D7870F42)
6
+UInt64 g_Crc64Table[256];
7
+
8
+void MY_FAST_CALL Crc64GenerateTable(void)
9
+{
10
+  UInt32 i;
11
+  for (i = 0; i < 256; i++)
12
+  {
13
+    UInt64 r = i;
14
+    int j;
15
+    for (j = 0; j < 8; j++)
16
+      r = (r >> 1) ^ ((UInt64)kCrc64Poly & ~((r & 1) - 1));
17
+    g_Crc64Table[i] = r;
18
+  }
19
+}
20
+
21
+UInt64 MY_FAST_CALL Crc64Update(UInt64 v, const void *data, size_t size)
22
+{
23
+  const Byte *p = (const Byte *)data;
24
+  for (; size > 0 ; size--, p++)
25
+    v = CRC64_UPDATE_BYTE(v, *p);
26
+  return v;
27
+}
28
+
29
+UInt64 MY_FAST_CALL Crc64Calc(const void *data, size_t size)
30
+{
31
+  return CRC64_GET_DIGEST(Crc64Update(CRC64_INIT_VAL, data, size));
32
+}
0 33
new file mode 100755
... ...
@@ -0,0 +1,26 @@
0
+/* XzCrc64.h -- CRC64 calculation
1
+2010-04-16 : Igor Pavlov : Public domain */
2
+
3
+#ifndef __XZ_CRC64_H
4
+#define __XZ_CRC64_H
5
+
6
+#include <stddef.h>
7
+
8
+#include "Types.h"
9
+
10
+EXTERN_C_BEGIN
11
+
12
+extern UInt64 g_Crc64Table[];
13
+
14
+void MY_FAST_CALL Crc64GenerateTable(void);
15
+
16
+#define CRC64_INIT_VAL UINT64_CONST(0xFFFFFFFFFFFFFFFF)
17
+#define CRC64_GET_DIGEST(crc) ((crc) ^ CRC64_INIT_VAL)
18
+#define CRC64_UPDATE_BYTE(crc, b) (g_Crc64Table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
19
+
20
+UInt64 MY_FAST_CALL Crc64Update(UInt64 crc, const void *data, size_t size);
21
+UInt64 MY_FAST_CALL Crc64Calc(const void *data, size_t size);
22
+
23
+EXTERN_C_END
24
+
25
+#endif
0 26
new file mode 100755
... ...
@@ -0,0 +1,875 @@
0
+/* XzDec.c -- Xz Decode
1
+2010-04-16 : Igor Pavlov : Public domain */
2
+
3
+/* #define XZ_DUMP */
4
+
5
+#ifdef XZ_DUMP
6
+#include <stdio.h>
7
+#endif
8
+
9
+#include <stdlib.h>
10
+#include <string.h>
11
+
12
+#include "7zCrc.h"
13
+#include "Alloc.h"
14
+#include "Bra.h"
15
+#include "CpuArch.h"
16
+#include "Delta.h"
17
+#include "Lzma2Dec.h"
18
+
19
+#ifdef USE_SUBBLOCK
20
+#include "SbDec.h"
21
+#endif
22
+
23
+#include "Xz.h"
24
+
25
+#define XZ_CHECK_SIZE_MAX 64
26
+
27
+#define CODER_BUF_SIZE (1 << 17)
28
+
29
+unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value)
30
+{
31
+  int i, limit;
32
+  *value = 0;
33
+  limit = (maxSize > 9) ? 9 : (int)maxSize;
34
+
35
+  for (i = 0; i < limit;)
36
+  {
37
+    Byte b = p[i];
38
+    *value |= (UInt64)(b & 0x7F) << (7 * i++);
39
+    if ((b & 0x80) == 0)
40
+      return (b == 0 && i != 1) ? 0 : i;
41
+  }
42
+  return 0;
43
+}
44
+
45
+/* ---------- BraState ---------- */
46
+
47
+#define BRA_BUF_SIZE (1 << 14)
48
+
49
+typedef struct
50
+{
51
+  size_t bufPos;
52
+  size_t bufConv;
53
+  size_t bufTotal;
54
+
55
+  UInt32 methodId;
56
+  int encodeMode;
57
+  UInt32 delta;
58
+  UInt32 ip;
59
+  UInt32 x86State;
60
+  Byte deltaState[DELTA_STATE_SIZE];
61
+
62
+  Byte buf[BRA_BUF_SIZE];
63
+} CBraState;
64
+
65
+void BraState_Free(void *pp, ISzAlloc *alloc)
66
+{
67
+  alloc->Free(alloc, pp);
68
+}
69
+
70
+SRes BraState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAlloc *alloc)
71
+{
72
+  CBraState *p = ((CBraState *)pp);
73
+  alloc = alloc;
74
+  p->encodeMode = 0;
75
+  p->ip = 0;
76
+  if (p->methodId == XZ_ID_Delta)
77
+  {
78
+    if (propSize != 1)
79
+      return SZ_ERROR_UNSUPPORTED;
80
+    p->delta = (unsigned)props[0] + 1;
81
+  }
82
+  else
83
+  {
84
+    if (propSize == 4)
85
+    {
86
+      UInt32 v = GetUi32(props);
87
+      switch(p->methodId)
88
+      {
89
+        case XZ_ID_PPC:
90
+        case XZ_ID_ARM:
91
+        case XZ_ID_SPARC:
92
+          if ((v & 3) != 0)
93
+            return SZ_ERROR_UNSUPPORTED;
94
+          break;
95
+        case XZ_ID_ARMT:
96
+          if ((v & 1) != 0)
97
+            return SZ_ERROR_UNSUPPORTED;
98
+          break;
99
+        case XZ_ID_IA64:
100
+          if ((v & 0xF) != 0)
101
+            return SZ_ERROR_UNSUPPORTED;
102
+          break;
103
+      }
104
+      p->ip = v;
105
+    }
106
+    else if (propSize != 0)
107
+      return SZ_ERROR_UNSUPPORTED;
108
+  }
109
+  return SZ_OK;
110
+}
111
+
112
+void BraState_Init(void *pp)
113
+{
114
+  CBraState *p = ((CBraState *)pp);
115
+  p->bufPos = p->bufConv = p->bufTotal = 0;
116
+  x86_Convert_Init(p->x86State);
117
+  if (p->methodId == XZ_ID_Delta)
118
+    Delta_Init(p->deltaState);
119
+}
120
+
121
+#define CASE_BRA_CONV(isa) case XZ_ID_ ## isa: p->bufConv = isa ## _Convert(p->buf, p->bufTotal, p->ip, p->encodeMode); break;
122
+
123
+static SRes BraState_Code(void *pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
124
+    int srcWasFinished, ECoderFinishMode finishMode, int *wasFinished)
125
+{
126
+  CBraState *p = ((CBraState *)pp);
127
+  SizeT destLenOrig = *destLen;
128
+  SizeT srcLenOrig = *srcLen;
129
+  *destLen = 0;
130
+  *srcLen = 0;
131
+  finishMode = finishMode;
132
+  *wasFinished = 0;
133
+  while (destLenOrig > 0)
134
+  {
135
+    if (p->bufPos != p->bufConv)
136
+    {
137
+      size_t curSize = p->bufConv - p->bufPos;
138
+      if (curSize > destLenOrig)
139
+        curSize = destLenOrig;
140
+      memcpy(dest, p->buf + p->bufPos, curSize);
141
+      p->bufPos += curSize;
142
+      *destLen += curSize;
143
+      dest += curSize;
144
+      destLenOrig -= curSize;
145
+      continue;
146
+    }
147
+    p->bufTotal -= p->bufPos;
148
+    memmove(p->buf, p->buf + p->bufPos, p->bufTotal);
149
+    p->bufPos = 0;
150
+    p->bufConv = 0;
151
+    {
152
+      size_t curSize = BRA_BUF_SIZE - p->bufTotal;
153
+      if (curSize > srcLenOrig)
154
+        curSize = srcLenOrig;
155
+      memcpy(p->buf + p->bufTotal, src, curSize);
156
+      *srcLen += curSize;
157
+      src += curSize;
158
+      srcLenOrig -= curSize;
159
+      p->bufTotal += curSize;
160
+    }
161
+    if (p->bufTotal == 0)
162
+      break;
163
+    switch(p->methodId)
164
+    {
165
+      case XZ_ID_Delta:
166
+        if (p->encodeMode)
167
+          Delta_Encode(p->deltaState, p->delta, p->buf, p->bufTotal);
168
+        else
169
+          Delta_Decode(p->deltaState, p->delta, p->buf, p->bufTotal);
170
+        p->bufConv = p->bufTotal;
171
+        break;
172
+      case XZ_ID_X86:
173
+        p->bufConv = x86_Convert(p->buf, p->bufTotal, p->ip, &p->x86State, p->encodeMode);
174
+        break;
175
+      CASE_BRA_CONV(PPC)
176
+      CASE_BRA_CONV(IA64)
177
+      CASE_BRA_CONV(ARM)
178
+      CASE_BRA_CONV(ARMT)
179
+      CASE_BRA_CONV(SPARC)
180
+      default:
181
+        return SZ_ERROR_UNSUPPORTED;
182
+    }
183
+    p->ip += (UInt32)p->bufConv;
184
+
185
+    if (p->bufConv == 0)
186
+    {
187
+      if (!srcWasFinished)
188
+        break;
189
+      p->bufConv = p->bufTotal;
190
+    }
191
+  }
192
+  if (p->bufTotal == p->bufPos && srcLenOrig == 0 && srcWasFinished)
193
+    *wasFinished = 1;
194
+  return SZ_OK;
195
+}
196
+
197
+SRes BraState_SetFromMethod(IStateCoder *p, UInt64 id, ISzAlloc *alloc)
198
+{
199
+  CBraState *decoder;
200
+  if (id != XZ_ID_Delta &&
201
+      id != XZ_ID_X86 &&
202
+      id != XZ_ID_PPC &&
203
+      id != XZ_ID_IA64 &&
204
+      id != XZ_ID_ARM &&
205
+      id != XZ_ID_ARMT &&
206
+      id != XZ_ID_SPARC)
207
+    return SZ_ERROR_UNSUPPORTED;
208
+  p->p = 0;
209
+  decoder = alloc->Alloc(alloc, sizeof(CBraState));
210
+  if (decoder == 0)
211
+    return SZ_ERROR_MEM;
212
+  decoder->methodId = (UInt32)id;
213
+  p->p = decoder;
214
+  p->Free = BraState_Free;
215
+  p->SetProps = BraState_SetProps;
216
+  p->Init = BraState_Init;
217
+  p->Code = BraState_Code;
218
+  return SZ_OK;
219
+}
220
+
221
+/* ---------- SbState ---------- */
222
+
223
+#ifdef USE_SUBBLOCK
224
+
225
+static void SbState_Free(void *pp, ISzAlloc *alloc)
226
+{
227
+  CSubblockDec *p = (CSubblockDec *)pp;
228
+  SubblockDec_Free(p, alloc);
229
+  alloc->Free(alloc, pp);
230
+}
231
+
232
+static SRes SbState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAlloc *alloc)
233
+{
234
+  pp = pp;
235
+  props = props;
236
+  alloc = alloc;
237
+  return (propSize == 0) ? SZ_OK : SZ_ERROR_UNSUPPORTED;
238
+}
239
+
240
+static void SbState_Init(void *pp)
241
+{
242
+  SubblockDec_Init((CSubblockDec *)pp);
243
+}
244
+
245
+static SRes SbState_Code(void *pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
246
+    int srcWasFinished, ECoderFinishMode finishMode, int *wasFinished)
247
+{
248
+  ECoderStatus status;
249
+  SRes res = SubblockDec_Decode((CSubblockDec *)pp, dest, destLen, src, srcLen, finishMode, &status);
250
+  srcWasFinished = srcWasFinished;
251
+  *wasFinished = (status == LZMA_STATUS_FINISHED_WITH_MARK);
252
+  return res;
253
+}
254
+
255
+SRes SbState_SetFromMethod(IStateCoder *p, ISzAlloc *alloc)
256
+{
257
+  CSubblockDec *decoder;
258
+  p->p = 0;
259
+  decoder = alloc->Alloc(alloc, sizeof(CSubblockDec));
260
+  if (decoder == 0)
261
+    return SZ_ERROR_MEM;
262
+  p->p = decoder;
263
+  p->Free = SbState_Free;
264
+  p->SetProps = SbState_SetProps;
265
+  p->Init = SbState_Init;
266
+  p->Code = SbState_Code;
267
+  SubblockDec_Construct(decoder);
268
+  return SZ_OK;
269
+}
270
+#endif
271
+
272
+/* ---------- Lzma2State ---------- */
273
+
274
+static void Lzma2State_Free(void *pp, ISzAlloc *alloc)
275
+{
276
+  Lzma2Dec_Free((CLzma2Dec *)pp, alloc);
277
+  alloc->Free(alloc, pp);
278
+}
279
+
280
+static SRes Lzma2State_SetProps(void *pp, const Byte *props, size_t propSize, ISzAlloc *alloc)
281
+{
282
+  if (propSize != 1)
283
+    return SZ_ERROR_UNSUPPORTED;
284
+  return Lzma2Dec_Allocate((CLzma2Dec *)pp, props[0], alloc);
285
+}
286
+
287
+static void Lzma2State_Init(void *pp)
288
+{
289
+  Lzma2Dec_Init((CLzma2Dec *)pp);
290
+}
291
+
292
+static SRes Lzma2State_Code(void *pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
293
+    int srcWasFinished, ECoderFinishMode finishMode, int *wasFinished)
294
+{
295
+  ELzmaStatus status;
296
+  /* ELzmaFinishMode fm = (finishMode == LZMA_FINISH_ANY) ? LZMA_FINISH_ANY : LZMA_FINISH_END; */
297
+  SRes res = Lzma2Dec_DecodeToBuf((CLzma2Dec *)pp, dest, destLen, src, srcLen, finishMode, &status);
298
+  srcWasFinished = srcWasFinished;
299
+  *wasFinished = (status == LZMA_STATUS_FINISHED_WITH_MARK);
300
+  return res;
301
+}
302
+
303
+static SRes Lzma2State_SetFromMethod(IStateCoder *p, ISzAlloc *alloc)
304
+{
305
+  CLzma2Dec *decoder = alloc->Alloc(alloc, sizeof(CLzma2Dec));
306
+  p->p = decoder;
307
+  if (decoder == 0)
308
+    return SZ_ERROR_MEM;
309
+  p->Free = Lzma2State_Free;
310
+  p->SetProps = Lzma2State_SetProps;
311
+  p->Init = Lzma2State_Init;
312
+  p->Code = Lzma2State_Code;
313
+  Lzma2Dec_Construct(decoder);
314
+  return SZ_OK;
315
+}
316
+
317
+
318
+void MixCoder_Construct(CMixCoder *p, ISzAlloc *alloc)
319
+{
320
+  int i;
321
+  p->alloc = alloc;
322
+  p->buf = 0;
323
+  p->numCoders = 0;
324
+  for (i = 0; i < MIXCODER_NUM_FILTERS_MAX; i++)
325
+    p->coders[i].p = NULL;
326
+}
327
+
328
+void MixCoder_Free(CMixCoder *p)
329
+{
330
+  int i;
331
+  for (i = 0; i < p->numCoders; i++)
332
+  {
333
+    IStateCoder *sc = &p->coders[i];
334
+    if (p->alloc && sc->p)
335
+      sc->Free(sc->p, p->alloc);
336
+  }
337
+  p->numCoders = 0;
338
+  if (p->buf)
339
+    p->alloc->Free(p->alloc, p->buf);
340
+}
341
+
342
+void MixCoder_Init(CMixCoder *p)
343
+{
344
+  int i;
345
+  for (i = 0; i < p->numCoders - 1; i++)
346
+  {
347
+    p->size[i] = 0;
348
+    p->pos[i] = 0;
349
+    p->finished[i] = 0;
350
+  }
351
+  for (i = 0; i < p->numCoders; i++)
352
+  {
353
+    IStateCoder *coder = &p->coders[i];
354
+    coder->Init(coder->p);
355
+  }
356
+}
357
+
358
+SRes MixCoder_SetFromMethod(CMixCoder *p, int coderIndex, UInt64 methodId)
359
+{
360
+  IStateCoder *sc = &p->coders[coderIndex];
361
+  p->ids[coderIndex] = methodId;
362
+  switch(methodId)
363
+  {
364
+    case XZ_ID_LZMA2: return Lzma2State_SetFromMethod(sc, p->alloc);
365
+    #ifdef USE_SUBBLOCK
366
+    case XZ_ID_Subblock: return SbState_SetFromMethod(sc, p->alloc);
367
+    #endif
368
+  }
369
+  if (coderIndex == 0)
370
+    return SZ_ERROR_UNSUPPORTED;
371
+  return BraState_SetFromMethod(sc, methodId, p->alloc);
372
+}
373
+
374
+SRes MixCoder_Code(CMixCoder *p, Byte *dest, SizeT *destLen,
375
+    const Byte *src, SizeT *srcLen, int srcWasFinished,
376
+    ECoderFinishMode finishMode, ECoderStatus *status)
377
+{
378
+  SizeT destLenOrig = *destLen;
379
+  SizeT srcLenOrig = *srcLen;
380
+  Bool allFinished = True;
381
+  *destLen = 0;
382
+  *srcLen = 0;
383
+  *status = CODER_STATUS_NOT_FINISHED;
384
+
385
+  if (p->buf == 0)
386
+  {
387
+    p->buf = p->alloc->Alloc(p->alloc, CODER_BUF_SIZE * (MIXCODER_NUM_FILTERS_MAX - 1));
388
+    if (p->buf == 0)
389
+      return SZ_ERROR_MEM;
390
+  }
391
+
392
+  if (p->numCoders != 1)
393
+    finishMode = CODER_FINISH_ANY;
394
+
395
+  for (;;)
396
+  {
397
+    Bool processed = False;
398
+    int i;
399
+    /*
400
+    if (p->numCoders == 1 && *destLen == destLenOrig && finishMode == LZMA_FINISH_ANY)
401
+      break;
402
+    */
403
+
404
+    for (i = 0; i < p->numCoders; i++)
405
+    {
406
+      SRes res;
407
+      IStateCoder *coder = &p->coders[i];
408
+      Byte *destCur;
409
+      SizeT destLenCur, srcLenCur;
410
+      const Byte *srcCur;
411
+      int srcFinishedCur;
412
+      int encodingWasFinished;
413
+      
414
+      if (i == 0)
415
+      {
416
+        srcCur = src;
417
+        srcLenCur = srcLenOrig - *srcLen;
418
+        srcFinishedCur = srcWasFinished;
419
+      }
420
+      else
421
+      {
422
+        srcCur = p->buf + (CODER_BUF_SIZE * (i - 1)) + p->pos[i - 1];
423
+        srcLenCur = p->size[i - 1] - p->pos[i - 1];
424
+        srcFinishedCur = p->finished[i - 1];
425
+      }
426
+      
427
+      if (i == p->numCoders - 1)
428
+      {
429
+        destCur = dest;
430
+        destLenCur = destLenOrig - *destLen;
431
+      }
432
+      else
433
+      {
434
+        if (p->pos[i] != p->size[i])
435
+          continue;
436
+        destCur = p->buf + (CODER_BUF_SIZE * i);
437
+        destLenCur = CODER_BUF_SIZE;
438
+      }
439
+      
440
+      res = coder->Code(coder->p, destCur, &destLenCur, srcCur, &srcLenCur, srcFinishedCur, finishMode, &encodingWasFinished);
441
+
442
+      if (!encodingWasFinished)
443
+        allFinished = False;
444
+
445
+      if (i == 0)
446
+      {
447
+        *srcLen += srcLenCur;
448
+        src += srcLenCur;
449
+      }
450
+      else
451
+      {
452
+        p->pos[i - 1] += srcLenCur;
453
+      }
454
+
455
+      if (i == p->numCoders - 1)
456
+      {
457
+        *destLen += destLenCur;
458
+        dest += destLenCur;
459
+      }
460
+      else
461
+      {
462
+        p->size[i] = destLenCur;
463
+        p->pos[i] = 0;
464
+        p->finished[i] = encodingWasFinished;
465
+      }
466
+      
467
+      if (res != SZ_OK)
468
+        return res;
469
+
470
+      if (destLenCur != 0 || srcLenCur != 0)
471
+        processed = True;
472
+    }
473
+    if (!processed)
474
+      break;
475
+  }
476
+  if (allFinished)
477
+    *status = CODER_STATUS_FINISHED_WITH_MARK;
478
+  return SZ_OK;
479
+}
480
+
481
+SRes Xz_ParseHeader(CXzStreamFlags *p, const Byte *buf)
482
+{
483
+  *p = (CXzStreamFlags)GetBe16(buf + XZ_SIG_SIZE);
484
+  if (CrcCalc(buf + XZ_SIG_SIZE, XZ_STREAM_FLAGS_SIZE) !=
485
+      GetUi32(buf + XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE))
486
+    return SZ_ERROR_NO_ARCHIVE;
487
+  return XzFlags_IsSupported(*p) ? SZ_OK : SZ_ERROR_UNSUPPORTED;
488
+}
489
+
490
+static Bool Xz_CheckFooter(CXzStreamFlags flags, UInt64 indexSize, const Byte *buf)
491
+{
492
+  return
493
+      indexSize == (((UInt64)GetUi32(buf + 4) + 1) << 2) &&
494
+      (GetUi32(buf) == CrcCalc(buf + 4, 6) &&
495
+      flags == GetBe16(buf + 8) &&
496
+      memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) == 0);
497
+}
498
+
499
+#define READ_VARINT_AND_CHECK(buf, pos, size, res) \
500
+  { unsigned s = Xz_ReadVarInt(buf + pos, size - pos, res); \
501
+  if (s == 0) return SZ_ERROR_ARCHIVE; pos += s; }
502
+
503
+
504
+SRes XzBlock_Parse(CXzBlock *p, const Byte *header)
505
+{
506
+  unsigned pos;
507
+  int numFilters, i;
508
+  UInt32 headerSize = (UInt32)header[0] << 2;
509
+
510
+  if (CrcCalc(header, headerSize) != GetUi32(header + headerSize))
511
+    return SZ_ERROR_ARCHIVE;
512
+
513
+  pos = 1;
514
+  if (pos == headerSize)
515
+    return SZ_ERROR_ARCHIVE;
516
+  p->flags = header[pos++];
517
+
518
+  if (XzBlock_HasPackSize(p))
519
+  {
520
+    READ_VARINT_AND_CHECK(header, pos, headerSize, &p->packSize);
521
+    if (p->packSize == 0 || p->packSize + headerSize >= (UInt64)1 << 63)
522
+      return SZ_ERROR_ARCHIVE;
523
+  }
524
+
525
+  if (XzBlock_HasUnpackSize(p))
526
+    READ_VARINT_AND_CHECK(header, pos, headerSize, &p->unpackSize);
527
+
528
+  numFilters = XzBlock_GetNumFilters(p);
529
+  for (i = 0; i < numFilters; i++)
530
+  {
531
+    CXzFilter *filter = p->filters + i;
532
+    UInt64 size;
533
+    READ_VARINT_AND_CHECK(header, pos, headerSize, &filter->id);
534
+    READ_VARINT_AND_CHECK(header, pos, headerSize, &size);
535
+    if (size > headerSize - pos || size > XZ_FILTER_PROPS_SIZE_MAX)
536
+      return SZ_ERROR_ARCHIVE;
537
+    filter->propsSize = (UInt32)size;
538
+    memcpy(filter->props, header + pos, (size_t)size);
539
+    pos += (unsigned)size;
540
+
541
+    #ifdef XZ_DUMP
542
+    printf("\nf[%d] = %2X: ", i, filter->id);
543
+    {
544
+      int i;
545
+      for (i = 0; i < size; i++)
546
+        printf(" %2X", filter->props[i]);
547
+    }
548
+    #endif
549
+  }
550
+
551
+  while (pos < headerSize)
552
+    if (header[pos++] != 0)
553
+      return SZ_ERROR_ARCHIVE;
554
+  return SZ_OK;
555
+}
556
+
557
+SRes XzDec_Init(CMixCoder *p, const CXzBlock *block)
558
+{
559
+  int i;
560
+  Bool needReInit = True;
561
+  int numFilters = XzBlock_GetNumFilters(block);
562
+  if (numFilters == p->numCoders)
563
+  {
564
+    for (i = 0; i < numFilters; i++)
565
+      if (p->ids[i] != block->filters[numFilters - 1 - i].id)
566
+        break;
567
+    needReInit = (i != numFilters);
568
+  }
569
+  if (needReInit)
570
+  {
571
+    MixCoder_Free(p);
572
+    p->numCoders = numFilters;
573
+    for (i = 0; i < numFilters; i++)
574
+    {
575
+      const CXzFilter *f = &block->filters[numFilters - 1 - i];
576
+      RINOK(MixCoder_SetFromMethod(p, i, f->id));
577
+    }
578
+  }
579
+  for (i = 0; i < numFilters; i++)
580
+  {
581
+    const CXzFilter *f = &block->filters[numFilters - 1 - i];
582
+    IStateCoder *sc = &p->coders[i];
583
+    RINOK(sc->SetProps(sc->p, f->props, f->propsSize, p->alloc));
584
+  }
585
+  MixCoder_Init(p);
586
+  return SZ_OK;
587
+}
588
+
589
+SRes XzUnpacker_Create(CXzUnpacker *p, ISzAlloc *alloc)
590
+{
591
+  MixCoder_Construct(&p->decoder, alloc);
592
+  p->state = XZ_STATE_STREAM_HEADER;
593
+  p->pos = 0;
594
+  p->numStreams = 0;
595
+  return SZ_OK;
596
+}
597
+
598
+void XzUnpacker_Free(CXzUnpacker *p)
599
+{
600
+  MixCoder_Free(&p->decoder);
601
+}
602
+
603
+SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
604
+    const Byte *src, SizeT *srcLen, int finishMode, ECoderStatus *status)
605
+{
606
+  SizeT destLenOrig = *destLen;
607
+  SizeT srcLenOrig = *srcLen;
608
+  *destLen = 0;
609
+  *srcLen = 0;
610
+  *status = CODER_STATUS_NOT_SPECIFIED;
611
+  for (;;)
612
+  {
613
+    SizeT srcRem = srcLenOrig - *srcLen;
614
+
615
+    if (p->state == XZ_STATE_BLOCK)
616
+    {
617
+      SizeT destLen2 = destLenOrig - *destLen;
618
+      SizeT srcLen2 = srcLenOrig - *srcLen;
619
+      SRes res;
620
+      if (srcLen2 == 0 && destLen2 == 0)
621
+      {
622
+        *status = CODER_STATUS_NOT_FINISHED;
623
+        return SZ_OK;
624
+      }
625
+      
626
+      res = MixCoder_Code(&p->decoder, dest, &destLen2, src, &srcLen2, False, finishMode, status);
627
+      XzCheck_Update(&p->check, dest, destLen2);
628
+      
629
+      (*srcLen) += srcLen2;
630
+      src += srcLen2;
631
+      p->packSize += srcLen2;
632
+      
633
+      (*destLen) += destLen2;
634
+      dest += destLen2;
635
+      p->unpackSize += destLen2;
636
+      
637
+      RINOK(res);
638
+      
639
+      if (*status == CODER_STATUS_FINISHED_WITH_MARK)
640
+      {
641
+        Byte temp[32];
642
+        unsigned num = Xz_WriteVarInt(temp, p->packSize + p->blockHeaderSize + XzFlags_GetCheckSize(p->streamFlags));
643
+        num += Xz_WriteVarInt(temp + num, p->unpackSize);
644
+        Sha256_Update(&p->sha, temp, num);
645
+        p->indexSize += num;
646
+        p->numBlocks++;
647
+        
648
+        p->state = XZ_STATE_BLOCK_FOOTER;
649
+        p->pos = 0;
650
+        p->alignPos = 0;
651
+      }
652
+      else if (srcLen2 == 0 && destLen2 == 0)
653
+        return SZ_OK;
654
+      
655
+      continue;
656
+    }
657
+
658
+    if (srcRem == 0)
659
+    {
660
+      *status = CODER_STATUS_NEEDS_MORE_INPUT;
661
+      return SZ_OK;
662
+    }
663
+
664
+    switch(p->state)
665
+    {
666
+      case XZ_STATE_STREAM_HEADER:
667
+      {
668
+        if (p->pos < XZ_STREAM_HEADER_SIZE)
669
+        {
670
+          if (p->pos < XZ_SIG_SIZE && *src != XZ_SIG[p->pos])
671
+            return SZ_ERROR_NO_ARCHIVE;
672
+          p->buf[p->pos++] = *src++;
673
+          (*srcLen)++;
674
+        }
675
+        else
676
+        {
677
+          RINOK(Xz_ParseHeader(&p->streamFlags, p->buf));
678
+          p->state = XZ_STATE_BLOCK_HEADER;
679
+          Sha256_Init(&p->sha);
680
+          p->indexSize = 0;
681
+          p->numBlocks = 0;
682
+          p->pos = 0;
683
+        }
684
+        break;
685
+      }
686
+
687
+      case XZ_STATE_BLOCK_HEADER:
688
+      {
689
+        if (p->pos == 0)
690
+        {
691
+          p->buf[p->pos++] = *src++;
692
+          (*srcLen)++;
693
+          if (p->buf[0] == 0)
694
+          {
695
+            p->indexPreSize = 1 + Xz_WriteVarInt(p->buf + 1, p->numBlocks);
696
+            p->indexPos = p->indexPreSize;
697
+            p->indexSize += p->indexPreSize;
698
+            Sha256_Final(&p->sha, p->shaDigest);
699
+            Sha256_Init(&p->sha);
700
+            p->crc = CrcUpdate(CRC_INIT_VAL, p->buf, p->indexPreSize);
701
+            p->state = XZ_STATE_STREAM_INDEX;
702
+          }
703
+          p->blockHeaderSize = ((UInt32)p->buf[0] << 2) + 4;
704
+        }
705
+        else if (p->pos != p->blockHeaderSize)
706
+        {
707
+          UInt32 cur = p->blockHeaderSize - p->pos;
708
+          if (cur > srcRem)
709
+            cur = (UInt32)srcRem;
710
+          memcpy(p->buf + p->pos, src, cur);
711
+          p->pos += cur;
712
+          (*srcLen) += cur;
713
+          src += cur;
714
+        }
715
+        else
716
+        {
717
+          RINOK(XzBlock_Parse(&p->block, p->buf));
718
+          p->state = XZ_STATE_BLOCK;
719
+          p->packSize = 0;
720
+          p->unpackSize = 0;
721
+          XzCheck_Init(&p->check, XzFlags_GetCheckType(p->streamFlags));
722
+          RINOK(XzDec_Init(&p->decoder, &p->block));
723
+        }
724
+        break;
725
+      }
726
+
727
+      case XZ_STATE_BLOCK_FOOTER:
728
+      {
729
+        if (((p->packSize + p->alignPos) & 3) != 0)
730
+        {
731
+          (*srcLen)++;
732
+          p->alignPos++;
733
+          if (*src++ != 0)
734
+            return SZ_ERROR_CRC;
735
+        }
736
+        else
737
+        {
738
+          UInt32 checkSize = XzFlags_GetCheckSize(p->streamFlags);
739
+          UInt32 cur = checkSize - p->pos;
740
+          if (cur != 0)
741
+          {
742
+            if (cur > srcRem)
743
+              cur = (UInt32)srcRem;
744
+            memcpy(p->buf + p->pos, src, cur);
745
+            p->pos += cur;
746
+            (*srcLen) += cur;
747
+            src += cur;
748
+          }
749
+          else
750
+          {
751
+            Byte digest[XZ_CHECK_SIZE_MAX];
752
+            p->state = XZ_STATE_BLOCK_HEADER;
753
+            p->pos = 0;
754
+            if (XzCheck_Final(&p->check, digest) && memcmp(digest, p->buf, checkSize) != 0)
755
+              return SZ_ERROR_CRC;
756
+          }
757
+        }
758
+        break;
759
+      }
760
+
761
+      case XZ_STATE_STREAM_INDEX:
762
+      {
763
+        if (p->pos < p->indexPreSize)
764
+        {
765
+          (*srcLen)++;
766
+          if (*src++ != p->buf[p->pos++])
767
+            return SZ_ERROR_CRC;
768
+        }
769
+        else
770
+        {
771
+          if (p->indexPos < p->indexSize)
772
+          {
773
+            UInt64 cur = p->indexSize - p->indexPos;
774
+            if (srcRem > cur)
775
+              srcRem = (SizeT)cur;
776
+            p->crc = CrcUpdate(p->crc, src, srcRem);
777
+            Sha256_Update(&p->sha, src, srcRem);
778
+            (*srcLen) += srcRem;
779
+            src += srcRem;
780
+            p->indexPos += srcRem;
781
+          }
782
+          else if ((p->indexPos & 3) != 0)
783
+          {
784
+            Byte b = *src++;
785
+            p->crc = CRC_UPDATE_BYTE(p->crc, b);
786
+            (*srcLen)++;
787
+            p->indexPos++;
788
+            p->indexSize++;
789
+            if (b != 0)
790
+              return SZ_ERROR_CRC;
791
+          }
792
+          else
793
+          {
794
+            Byte digest[SHA256_DIGEST_SIZE];
795
+            p->state = XZ_STATE_STREAM_INDEX_CRC;
796
+            p->indexSize += 4;
797
+            p->pos = 0;
798
+            Sha256_Final(&p->sha, digest);
799
+            if (memcmp(digest, p->shaDigest, SHA256_DIGEST_SIZE) != 0)
800
+              return SZ_ERROR_CRC;
801
+          }
802
+        }
803
+        break;
804
+      }
805
+
806
+      case XZ_STATE_STREAM_INDEX_CRC:
807
+      {
808
+        if (p->pos < 4)
809
+        {
810
+          (*srcLen)++;
811
+          p->buf[p->pos++] = *src++;
812
+        }
813
+        else
814
+        {
815
+          p->state = XZ_STATE_STREAM_FOOTER;
816
+          p->pos = 0;
817
+          if (CRC_GET_DIGEST(p->crc) != GetUi32(p->buf))
818
+            return SZ_ERROR_CRC;
819
+        }
820
+        break;
821
+      }
822
+
823
+      case XZ_STATE_STREAM_FOOTER:
824
+      {
825
+        UInt32 cur = XZ_STREAM_FOOTER_SIZE - p->pos;
826
+        if (cur > srcRem)
827
+          cur = (UInt32)srcRem;
828
+        memcpy(p->buf + p->pos, src, cur);
829
+        p->pos += cur;
830
+        (*srcLen) += cur;
831
+        src += cur;
832
+        if (p->pos == XZ_STREAM_FOOTER_SIZE)
833
+        {
834
+          p->state = XZ_STATE_STREAM_PADDING;
835
+          p->numStreams++;
836
+          p->padSize = 0;
837
+          if (!Xz_CheckFooter(p->streamFlags, p->indexSize, p->buf))
838
+            return SZ_ERROR_CRC;
839
+        }
840
+        break;
841
+      }
842
+
843
+      case XZ_STATE_STREAM_PADDING:
844
+      {
845
+        if (*src != 0)
846
+        {
847
+          if (((UInt32)p->padSize & 3) != 0)
848
+            return SZ_ERROR_NO_ARCHIVE;
849
+          p->pos = 0;
850
+          p->state = XZ_STATE_STREAM_HEADER;
851
+        }
852
+        else
853
+        {
854
+          (*srcLen)++;
855
+          src++;
856
+          p->padSize++;
857
+        }
858
+        break;
859
+      }
860
+      
861
+      case XZ_STATE_BLOCK: break; /* to disable GCC warning */
862
+    }
863
+  }
864
+  /*
865
+  if (p->state == XZ_STATE_FINISHED)
866
+    *status = CODER_STATUS_FINISHED_WITH_MARK;
867
+  return SZ_OK;
868
+  */
869
+}
870
+
871
+Bool XzUnpacker_IsStreamWasFinished(CXzUnpacker *p)
872
+{
873
+  return (p->state == XZ_STATE_STREAM_PADDING) && (((UInt32)p->padSize & 3) == 0);
874
+}
0 875
new file mode 100755
... ...
@@ -0,0 +1,306 @@
0
+/* XzIn.c - Xz input
1
+2009-06-19 : Igor Pavlov : Public domain */
2
+
3
+#include <string.h>
4
+
5
+#include "7zCrc.h"
6
+#include "CpuArch.h"
7
+#include "Xz.h"
8
+
9
+SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream)
10
+{
11
+  Byte sig[XZ_STREAM_HEADER_SIZE];
12
+  RINOK(SeqInStream_Read2(inStream, sig, XZ_STREAM_HEADER_SIZE, SZ_ERROR_NO_ARCHIVE));
13
+  if (memcmp(sig, XZ_SIG, XZ_SIG_SIZE) != 0)
14
+    return SZ_ERROR_NO_ARCHIVE;
15
+  return Xz_ParseHeader(p, sig);
16
+}
17
+
18
+#define READ_VARINT_AND_CHECK(buf, pos, size, res) \
19
+  { unsigned s = Xz_ReadVarInt(buf + pos, size - pos, res); \
20
+  if (s == 0) return SZ_ERROR_ARCHIVE; pos += s; }
21
+
22
+SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, Bool *isIndex, UInt32 *headerSizeRes)
23
+{
24
+  Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
25
+  unsigned headerSize;
26
+  *headerSizeRes = 0;
27
+  RINOK(SeqInStream_ReadByte(inStream, &header[0]));
28
+  headerSize = ((unsigned)header[0] << 2) + 4;
29
+  if (headerSize == 0)
30
+  {
31
+    *headerSizeRes = 1;
32
+    *isIndex = True;
33
+    return SZ_OK;
34
+  }
35
+
36
+  *isIndex = False;
37
+  *headerSizeRes = headerSize;
38
+  RINOK(SeqInStream_Read(inStream, header + 1, headerSize - 1));
39
+  return XzBlock_Parse(p, header);
40
+}
41
+
42
+#define ADD_SIZE_CHECH(size, val) \
43
+  { UInt64 newSize = size + (val); if (newSize < size) return XZ_SIZE_OVERFLOW; size = newSize; }
44
+
45
+UInt64 Xz_GetUnpackSize(const CXzStream *p)
46
+{
47
+  UInt64 size = 0;
48
+  size_t i;
49
+  for (i = 0; i < p->numBlocks; i++)
50
+    ADD_SIZE_CHECH(size, p->blocks[i].unpackSize);
51
+  return size;
52
+}
53
+
54
+UInt64 Xz_GetPackSize(const CXzStream *p)
55
+{
56
+  UInt64 size = 0;
57
+  size_t i;
58
+  for (i = 0; i < p->numBlocks; i++)
59
+    ADD_SIZE_CHECH(size, (p->blocks[i].totalSize + 3) & ~(UInt64)3);
60
+  return size;
61
+}
62
+
63
+/*
64
+SRes XzBlock_ReadFooter(CXzBlock *p, CXzStreamFlags f, ISeqInStream *inStream)
65
+{
66
+  return SeqInStream_Read(inStream, p->check, XzFlags_GetCheckSize(f));
67
+}
68
+*/
69
+
70
+static SRes Xz_ReadIndex2(CXzStream *p, const Byte *buf, size_t size, ISzAlloc *alloc)
71
+{
72
+  size_t i, numBlocks, crcStartPos, pos = 1;
73
+  UInt32 crc;
74
+
75
+  if (size < 5 || buf[0] != 0)
76
+    return SZ_ERROR_ARCHIVE;
77
+
78
+  size -= 4;
79
+  crc = CrcCalc(buf, size);
80
+  if (crc != GetUi32(buf + size))
81
+    return SZ_ERROR_ARCHIVE;
82
+
83
+  {
84
+    UInt64 numBlocks64;
85
+    READ_VARINT_AND_CHECK(buf, pos, size, &numBlocks64);
86
+    numBlocks = (size_t)numBlocks64;
87
+    if (numBlocks != numBlocks64 || numBlocks * 2 > size)
88
+      return SZ_ERROR_ARCHIVE;
89
+  }
90
+  
91
+  crcStartPos = pos;
92
+  Xz_Free(p, alloc);
93
+  if (numBlocks != 0)
94
+  {
95
+    p->numBlocks = numBlocks;
96
+    p->numBlocksAllocated = numBlocks;
97
+    p->blocks = alloc->Alloc(alloc, sizeof(CXzBlockSizes) * numBlocks);
98
+    if (p->blocks == 0)
99
+      return SZ_ERROR_MEM;
100
+    for (i = 0; i < numBlocks; i++)
101
+    {
102
+      CXzBlockSizes *block = &p->blocks[i];
103
+      READ_VARINT_AND_CHECK(buf, pos, size, &block->totalSize);
104
+      READ_VARINT_AND_CHECK(buf, pos, size, &block->unpackSize);
105
+      if (block->totalSize == 0)
106
+        return SZ_ERROR_ARCHIVE;
107
+    }
108
+  }
109
+  while ((pos & 3) != 0)
110
+    if (buf[pos++] != 0)
111
+      return SZ_ERROR_ARCHIVE;
112
+  return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
113
+}
114
+
115
+static SRes Xz_ReadIndex(CXzStream *p, ILookInStream *stream, UInt64 indexSize, ISzAlloc *alloc)
116
+{
117
+  SRes res;
118
+  size_t size;
119
+  Byte *buf;
120
+  if (indexSize > ((UInt32)1 << 31))
121
+    return SZ_ERROR_UNSUPPORTED;
122
+  size = (size_t)indexSize;
123
+  if (size != indexSize)
124
+    return SZ_ERROR_UNSUPPORTED;
125
+  buf = alloc->Alloc(alloc, size);
126
+  if (buf == 0)
127
+    return SZ_ERROR_MEM;
128
+  res = LookInStream_Read2(stream, buf, size, SZ_ERROR_UNSUPPORTED);
129
+  if (res == SZ_OK)
130
+    res = Xz_ReadIndex2(p, buf, size, alloc);
131
+  alloc->Free(alloc, buf);
132
+  return res;
133
+}
134
+
135
+static SRes SeekFromCur(ILookInStream *inStream, Int64 *res)
136
+{
137
+  return inStream->Seek(inStream, res, SZ_SEEK_CUR);
138
+}
139
+
140
+static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOffset, ISzAlloc *alloc)
141
+{
142
+  UInt64 indexSize;
143
+  Byte buf[XZ_STREAM_FOOTER_SIZE];
144
+
145
+  if ((*startOffset & 3) != 0 || *startOffset < XZ_STREAM_FOOTER_SIZE)
146
+    return SZ_ERROR_NO_ARCHIVE;
147
+  *startOffset = -XZ_STREAM_FOOTER_SIZE;
148
+  RINOK(SeekFromCur(stream, startOffset));
149
+
150
+  RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
151
+  
152
+  if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
153
+  {
154
+    Int64 i = 0;
155
+    *startOffset += XZ_STREAM_FOOTER_SIZE;
156
+    for (;;)
157
+    {
158
+      int j;
159
+      size_t processedSize;
160
+      #define TEMP_BUF_SIZE (1 << 10)
161
+      Byte tempBuf[TEMP_BUF_SIZE];
162
+      if (*startOffset < XZ_STREAM_FOOTER_SIZE || i > (1 << 16))
163
+        return SZ_ERROR_NO_ARCHIVE;
164
+      processedSize = (*startOffset > TEMP_BUF_SIZE) ? TEMP_BUF_SIZE : (size_t)*startOffset;
165
+      i += processedSize;
166
+      *startOffset = -(Int64)processedSize;
167
+      RINOK(SeekFromCur(stream, startOffset));
168
+      RINOK(LookInStream_Read2(stream, tempBuf, processedSize, SZ_ERROR_NO_ARCHIVE));
169
+      for (j = (int)processedSize; j >= 0; j--)
170
+        if (tempBuf[j -1] != 0)
171
+          break;
172
+      if (j != 0)
173
+      {
174
+        if ((j & 3) != 0)
175
+          return SZ_ERROR_NO_ARCHIVE;
176
+        *startOffset += j;
177
+        if (*startOffset < XZ_STREAM_FOOTER_SIZE)
178
+          return SZ_ERROR_NO_ARCHIVE;
179
+        *startOffset -= XZ_STREAM_FOOTER_SIZE;
180
+        RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
181
+        RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
182
+        if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
183
+          return SZ_ERROR_NO_ARCHIVE;
184
+        break;
185
+      }
186
+    }
187
+  }
188
+  
189
+  p->flags = (CXzStreamFlags)GetBe16(buf + 8);
190
+
191
+  if (!XzFlags_IsSupported(p->flags))
192
+    return SZ_ERROR_UNSUPPORTED;
193
+
194
+  if (GetUi32(buf) != CrcCalc(buf + 4, 6))
195
+    return SZ_ERROR_ARCHIVE;
196
+
197
+  indexSize = ((UInt64)GetUi32(buf + 4) + 1) << 2;
198
+
199
+  *startOffset = -(Int64)(indexSize + XZ_STREAM_FOOTER_SIZE);
200
+  RINOK(SeekFromCur(stream, startOffset));
201
+
202
+  RINOK(Xz_ReadIndex(p, stream, indexSize, alloc));
203
+
204
+  {
205
+    UInt64 totalSize = Xz_GetPackSize(p);
206
+    UInt64 sum = XZ_STREAM_HEADER_SIZE + totalSize + indexSize;
207
+    if (totalSize == XZ_SIZE_OVERFLOW ||
208
+      sum >= ((UInt64)1 << 63) ||
209
+      totalSize >= ((UInt64)1 << 63))
210
+      return SZ_ERROR_ARCHIVE;
211
+    *startOffset = -(Int64)sum;
212
+    RINOK(SeekFromCur(stream, startOffset));
213
+  }
214
+  {
215
+    CXzStreamFlags headerFlags;
216
+    CSecToRead secToRead;
217
+    SecToRead_CreateVTable(&secToRead);
218
+    secToRead.realStream = stream;
219
+
220
+    RINOK(Xz_ReadHeader(&headerFlags, &secToRead.s));
221
+    return (p->flags == headerFlags) ? SZ_OK : SZ_ERROR_ARCHIVE;
222
+  }
223
+}
224
+
225
+
226
+/* ---------- Xz Streams ---------- */
227
+
228
+void Xzs_Construct(CXzs *p)
229
+{
230
+  p->num = p->numAllocated = 0;
231
+  p->streams = 0;
232
+}
233
+
234
+void Xzs_Free(CXzs *p, ISzAlloc *alloc)
235
+{
236
+  size_t i;
237
+  for (i = 0; i < p->num; i++)
238
+    Xz_Free(&p->streams[i], alloc);
239
+  alloc->Free(alloc, p->streams);
240
+  p->num = p->numAllocated = 0;
241
+  p->streams = 0;
242
+}
243
+
244
+UInt64 Xzs_GetNumBlocks(const CXzs *p)
245
+{
246
+  UInt64 num = 0;
247
+  size_t i;
248
+  for (i = 0; i < p->num; i++)
249
+    num += p->streams[i].numBlocks;
250
+  return num;
251
+}
252
+
253
+UInt64 Xzs_GetUnpackSize(const CXzs *p)
254
+{
255
+  UInt64 size = 0;
256
+  size_t i;
257
+  for (i = 0; i < p->num; i++)
258
+    ADD_SIZE_CHECH(size, Xz_GetUnpackSize(&p->streams[i]));
259
+  return size;
260
+}
261
+
262
+/*
263
+UInt64 Xzs_GetPackSize(const CXzs *p)
264
+{
265
+  UInt64 size = 0;
266
+  size_t i;
267
+  for (i = 0; i < p->num; i++)
268
+    ADD_SIZE_CHECH(size, Xz_GetTotalSize(&p->streams[i]));
269
+  return size;
270
+}
271
+*/
272
+
273
+SRes Xzs_ReadBackward(CXzs *p, ILookInStream *stream, Int64 *startOffset, ICompressProgress *progress, ISzAlloc *alloc)
274
+{
275
+  Int64 endOffset = 0;
276
+  RINOK(stream->Seek(stream, &endOffset, SZ_SEEK_END));
277
+  *startOffset = endOffset;
278
+  for (;;)
279
+  {
280
+    CXzStream st;
281
+    SRes res;
282
+    Xz_Construct(&st);
283
+    res = Xz_ReadBackward(&st, stream, startOffset, alloc);
284
+    st.startOffset = *startOffset;
285
+    RINOK(res);
286
+    if (p->num == p->numAllocated)
287
+    {
288
+      size_t newNum = p->num + p->num / 4 + 1;
289
+      Byte *data = (Byte *)alloc->Alloc(alloc, newNum * sizeof(CXzStream));
290
+      if (data == 0)
291
+        return SZ_ERROR_MEM;
292
+      p->numAllocated = newNum;
293
+      memcpy(data, p->streams, p->num * sizeof(CXzStream));
294
+      alloc->Free(alloc, p->streams);
295
+      p->streams = (CXzStream *)data;
296
+    }
297
+    p->streams[p->num++] = st;
298
+    if (*startOffset == 0)
299
+      break;
300
+    RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
301
+    if (progress && progress->Progress(progress, endOffset - *startOffset, (UInt64)(Int64)-1) != SZ_OK)
302
+      return SZ_ERROR_PROGRESS;
303
+  }
304
+  return SZ_OK;
305
+}
... ...
@@ -319,6 +319,21 @@ libclamav_la_SOURCES = \
319 319
 	7z/Ppmd7.h \
320 320
 	7z/Ppmd7Dec.c \
321 321
 	7z/Types.h \
322
+	7z/Xz.c \
323
+	7z/Xz.h \
324
+	7z/XzCrc64.c \
325
+	7z/XzCrc64.h \
326
+	7z/XzDec.c \
327
+	7z/XzIn.c \
328
+	7z/Sha256.h \
329
+	7z/Delta.c \
330
+	7z/Delta.h \
331
+	7z/Alloc.h \
332
+	7z/BraIA64.c \
333
+	7z/CpuArch.c \
334
+	7z/CpuArch.h \
335
+	7z/7zCrcOpt.c \
336
+	7z/RotateDefs.h \
322 337
 	explode.c \
323 338
 	explode.h \
324 339
 	textnorm.c \
... ...
@@ -377,6 +392,8 @@ libclamav_la_SOURCES = \
377 377
 	dmg.h \
378 378
 	xar.c \
379 379
 	xar.h \
380
+	xz_iface.c \
381
+	xz_iface.h \
380 382
 	sf_base64decode.c \
381 383
 	sf_base64decode.h \
382 384
 	hfsplus.c \
... ...
@@ -174,6 +174,10 @@ am_libclamav_la_OBJECTS = libclamav_la-matcher-ac.lo \
174 174
 	libclamav_la-Bcj2.lo libclamav_la-Bra.lo libclamav_la-Bra86.lo \
175 175
 	libclamav_la-Lzma2Dec.lo libclamav_la-LzmaDec.lo \
176 176
 	libclamav_la-Ppmd7.lo libclamav_la-Ppmd7Dec.lo \
177
+	libclamav_la-Xz.lo libclamav_la-XzCrc64.lo \
178
+	libclamav_la-XzDec.lo libclamav_la-XzIn.lo \
179
+	libclamav_la-Delta.lo libclamav_la-BraIA64.lo \
180
+	libclamav_la-CpuArch.lo libclamav_la-7zCrcOpt.lo \
177 181
 	libclamav_la-explode.lo libclamav_la-textnorm.lo \
178 182
 	libclamav_la-dlp.lo libclamav_la-js-norm.lo \
179 183
 	libclamav_la-uniq.lo libclamav_la-version.lo \
... ...
@@ -186,8 +190,9 @@ am_libclamav_la_OBJECTS = libclamav_la-matcher-ac.lo \
186 186
 	libclamav_la-bytecode_api_decl.lo libclamav_la-cache.lo \
187 187
 	libclamav_la-bytecode_detect.lo libclamav_la-events.lo \
188 188
 	libclamav_la-adc.lo libclamav_la-dmg.lo libclamav_la-xar.lo \
189
-	libclamav_la-sf_base64decode.lo libclamav_la-hfsplus.lo \
190
-	libclamav_la-swf.lo libclamav_la-jpeg.lo libclamav_la-png.lo \
189
+	libclamav_la-xz_iface.lo libclamav_la-sf_base64decode.lo \
190
+	libclamav_la-hfsplus.lo libclamav_la-swf.lo \
191
+	libclamav_la-jpeg.lo libclamav_la-png.lo \
191 192
 	libclamav_la-iso9660.lo libclamav_la-arc4.lo \
192 193
 	libclamav_la-rijndael.lo libclamav_la-crtmgr.lo \
193 194
 	libclamav_la-asn1.lo libclamav_la-fp_add.lo \
... ...
@@ -700,7 +705,10 @@ libclamav_la_SOURCES = clamav.h matcher-ac.c matcher-ac.h matcher-bm.c \
700 700
 	7z/7zIn.c 7z/7zStream.c 7z/7zVersion.h 7z/Bcj2.c 7z/Bcj2.h \
701 701
 	7z/Bra.c 7z/Bra.h 7z/Bra86.c 7z/CpuArch.h 7z/Lzma2Dec.c \
702 702
 	7z/Lzma2Dec.h 7z/LzmaDec.c 7z/LzmaDec.h 7z/Ppmd.h 7z/Ppmd7.c \
703
-	7z/Ppmd7.h 7z/Ppmd7Dec.c 7z/Types.h explode.c explode.h \
703
+	7z/Ppmd7.h 7z/Ppmd7Dec.c 7z/Types.h 7z/Xz.c 7z/Xz.h \
704
+	7z/XzCrc64.c 7z/XzCrc64.h 7z/XzDec.c 7z/XzIn.c 7z/Sha256.h \
705
+	7z/Delta.c 7z/Delta.h 7z/Alloc.h 7z/BraIA64.c 7z/CpuArch.c \
706
+	7z/CpuArch.h 7z/7zCrcOpt.c 7z/RotateDefs.h explode.c explode.h \
704 707
 	textnorm.c textnorm.h dlp.c dlp.h jsparse/js-norm.c \
705 708
 	jsparse/js-norm.h jsparse/lexglobal.h jsparse/textbuf.h uniq.c \
706 709
 	uniq.h version.c version.h mpool.c mpool.h filtering.h \
... ...
@@ -712,12 +720,13 @@ libclamav_la_SOURCES = clamav.h matcher-ac.c matcher-ac.h matcher-bm.c \
712 712
 	bytecode_api_impl.h bytecode_hooks.h cache.c cache.h \
713 713
 	bytecode_detect.c bytecode_detect.h builtin_bytecodes.h \
714 714
 	events.c events.h adc.c adc.h dmg.c dmg.h xar.c xar.h \
715
-	sf_base64decode.c sf_base64decode.h hfsplus.c hfsplus.h swf.c \
716
-	swf.h jpeg.c jpeg.h png.c png.h iso9660.c iso9660.h arc4.c \
717
-	arc4.h rijndael.c rijndael.h crtmgr.c crtmgr.h asn1.c asn1.h \
718
-	bignum.h bignum_fast.h tomsfastmath/addsub/fp_add.c \
719
-	tomsfastmath/addsub/fp_add_d.c tomsfastmath/addsub/fp_addmod.c \
720
-	tomsfastmath/addsub/fp_cmp.c tomsfastmath/addsub/fp_cmp_d.c \
715
+	xz_iface.c xz_iface.h sf_base64decode.c sf_base64decode.h \
716
+	hfsplus.c hfsplus.h swf.c swf.h jpeg.c jpeg.h png.c png.h \
717
+	iso9660.c iso9660.h arc4.c arc4.h rijndael.c rijndael.h \
718
+	crtmgr.c crtmgr.h asn1.c asn1.h bignum.h bignum_fast.h \
719
+	tomsfastmath/addsub/fp_add.c tomsfastmath/addsub/fp_add_d.c \
720
+	tomsfastmath/addsub/fp_addmod.c tomsfastmath/addsub/fp_cmp.c \
721
+	tomsfastmath/addsub/fp_cmp_d.c \
721 722
 	tomsfastmath/addsub/fp_cmp_mag.c tomsfastmath/addsub/fp_sub.c \
722 723
 	tomsfastmath/addsub/fp_sub_d.c tomsfastmath/addsub/fp_submod.c \
723 724
 	tomsfastmath/addsub/s_fp_add.c tomsfastmath/addsub/s_fp_sub.c \
... ...
@@ -904,6 +913,7 @@ distclean-compile:
904 904
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-7zBuf.Plo@am__quote@
905 905
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-7zBuf2.Plo@am__quote@
906 906
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-7zCrc.Plo@am__quote@
907
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-7zCrcOpt.Plo@am__quote@
907 908
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-7zDec.Plo@am__quote@
908 909
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-7zFile.Plo@am__quote@
909 910
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-7zIn.Plo@am__quote@
... ...
@@ -912,10 +922,17 @@ distclean-compile:
912 912
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Bcj2.Plo@am__quote@
913 913
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Bra.Plo@am__quote@
914 914
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Bra86.Plo@am__quote@
915
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-BraIA64.Plo@am__quote@
916
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-CpuArch.Plo@am__quote@
917
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Delta.Plo@am__quote@
915 918
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Lzma2Dec.Plo@am__quote@
916 919
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-LzmaDec.Plo@am__quote@
917 920
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Ppmd7.Plo@am__quote@
918 921
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Ppmd7Dec.Plo@am__quote@
922
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-Xz.Plo@am__quote@
923
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-XzCrc64.Plo@am__quote@
924
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-XzDec.Plo@am__quote@
925
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-XzIn.Plo@am__quote@
919 926
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-adc.Plo@am__quote@
920 927
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-arc4.Plo@am__quote@
921 928
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-asn1.Plo@am__quote@
... ...
@@ -1096,6 +1113,7 @@ distclean-compile:
1096 1096
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-version.Plo@am__quote@
1097 1097
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-wwunpack.Plo@am__quote@
1098 1098
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-xar.Plo@am__quote@
1099
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-xz_iface.Plo@am__quote@
1099 1100
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-yc.Plo@am__quote@
1100 1101
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unrar.Plo@am__quote@
1101 1102
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unrar15.Plo@am__quote@
... ...
@@ -1695,6 +1713,62 @@ libclamav_la-Ppmd7Dec.lo: 7z/Ppmd7Dec.c
1695 1695
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1696 1696
 @am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-Ppmd7Dec.lo `test -f '7z/Ppmd7Dec.c' || echo '$(srcdir)/'`7z/Ppmd7Dec.c
1697 1697
 
1698
+libclamav_la-Xz.lo: 7z/Xz.c
1699
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-Xz.lo -MD -MP -MF $(DEPDIR)/libclamav_la-Xz.Tpo -c -o libclamav_la-Xz.lo `test -f '7z/Xz.c' || echo '$(srcdir)/'`7z/Xz.c
1700
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-Xz.Tpo $(DEPDIR)/libclamav_la-Xz.Plo
1701
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/Xz.c' object='libclamav_la-Xz.lo' libtool=yes @AMDEPBACKSLASH@
1702
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1703
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-Xz.lo `test -f '7z/Xz.c' || echo '$(srcdir)/'`7z/Xz.c
1704
+
1705
+libclamav_la-XzCrc64.lo: 7z/XzCrc64.c
1706
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-XzCrc64.lo -MD -MP -MF $(DEPDIR)/libclamav_la-XzCrc64.Tpo -c -o libclamav_la-XzCrc64.lo `test -f '7z/XzCrc64.c' || echo '$(srcdir)/'`7z/XzCrc64.c
1707
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-XzCrc64.Tpo $(DEPDIR)/libclamav_la-XzCrc64.Plo
1708
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/XzCrc64.c' object='libclamav_la-XzCrc64.lo' libtool=yes @AMDEPBACKSLASH@
1709
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1710
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-XzCrc64.lo `test -f '7z/XzCrc64.c' || echo '$(srcdir)/'`7z/XzCrc64.c
1711
+
1712
+libclamav_la-XzDec.lo: 7z/XzDec.c
1713
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-XzDec.lo -MD -MP -MF $(DEPDIR)/libclamav_la-XzDec.Tpo -c -o libclamav_la-XzDec.lo `test -f '7z/XzDec.c' || echo '$(srcdir)/'`7z/XzDec.c
1714
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-XzDec.Tpo $(DEPDIR)/libclamav_la-XzDec.Plo
1715
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/XzDec.c' object='libclamav_la-XzDec.lo' libtool=yes @AMDEPBACKSLASH@
1716
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1717
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-XzDec.lo `test -f '7z/XzDec.c' || echo '$(srcdir)/'`7z/XzDec.c
1718
+
1719
+libclamav_la-XzIn.lo: 7z/XzIn.c
1720
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-XzIn.lo -MD -MP -MF $(DEPDIR)/libclamav_la-XzIn.Tpo -c -o libclamav_la-XzIn.lo `test -f '7z/XzIn.c' || echo '$(srcdir)/'`7z/XzIn.c
1721
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-XzIn.Tpo $(DEPDIR)/libclamav_la-XzIn.Plo
1722
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/XzIn.c' object='libclamav_la-XzIn.lo' libtool=yes @AMDEPBACKSLASH@
1723
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1724
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-XzIn.lo `test -f '7z/XzIn.c' || echo '$(srcdir)/'`7z/XzIn.c
1725
+
1726
+libclamav_la-Delta.lo: 7z/Delta.c
1727
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-Delta.lo -MD -MP -MF $(DEPDIR)/libclamav_la-Delta.Tpo -c -o libclamav_la-Delta.lo `test -f '7z/Delta.c' || echo '$(srcdir)/'`7z/Delta.c
1728
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-Delta.Tpo $(DEPDIR)/libclamav_la-Delta.Plo
1729
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/Delta.c' object='libclamav_la-Delta.lo' libtool=yes @AMDEPBACKSLASH@
1730
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1731
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-Delta.lo `test -f '7z/Delta.c' || echo '$(srcdir)/'`7z/Delta.c
1732
+
1733
+libclamav_la-BraIA64.lo: 7z/BraIA64.c
1734
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-BraIA64.lo -MD -MP -MF $(DEPDIR)/libclamav_la-BraIA64.Tpo -c -o libclamav_la-BraIA64.lo `test -f '7z/BraIA64.c' || echo '$(srcdir)/'`7z/BraIA64.c
1735
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-BraIA64.Tpo $(DEPDIR)/libclamav_la-BraIA64.Plo
1736
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/BraIA64.c' object='libclamav_la-BraIA64.lo' libtool=yes @AMDEPBACKSLASH@
1737
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1738
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-BraIA64.lo `test -f '7z/BraIA64.c' || echo '$(srcdir)/'`7z/BraIA64.c
1739
+
1740
+libclamav_la-CpuArch.lo: 7z/CpuArch.c
1741
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-CpuArch.lo -MD -MP -MF $(DEPDIR)/libclamav_la-CpuArch.Tpo -c -o libclamav_la-CpuArch.lo `test -f '7z/CpuArch.c' || echo '$(srcdir)/'`7z/CpuArch.c
1742
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-CpuArch.Tpo $(DEPDIR)/libclamav_la-CpuArch.Plo
1743
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/CpuArch.c' object='libclamav_la-CpuArch.lo' libtool=yes @AMDEPBACKSLASH@
1744
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1745
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-CpuArch.lo `test -f '7z/CpuArch.c' || echo '$(srcdir)/'`7z/CpuArch.c
1746
+
1747
+libclamav_la-7zCrcOpt.lo: 7z/7zCrcOpt.c
1748
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-7zCrcOpt.lo -MD -MP -MF $(DEPDIR)/libclamav_la-7zCrcOpt.Tpo -c -o libclamav_la-7zCrcOpt.lo `test -f '7z/7zCrcOpt.c' || echo '$(srcdir)/'`7z/7zCrcOpt.c
1749
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-7zCrcOpt.Tpo $(DEPDIR)/libclamav_la-7zCrcOpt.Plo
1750
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='7z/7zCrcOpt.c' object='libclamav_la-7zCrcOpt.lo' libtool=yes @AMDEPBACKSLASH@
1751
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1752
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-7zCrcOpt.lo `test -f '7z/7zCrcOpt.c' || echo '$(srcdir)/'`7z/7zCrcOpt.c
1753
+
1698 1754
 libclamav_la-explode.lo: explode.c
1699 1755
 @am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-explode.lo -MD -MP -MF $(DEPDIR)/libclamav_la-explode.Tpo -c -o libclamav_la-explode.lo `test -f 'explode.c' || echo '$(srcdir)/'`explode.c
1700 1756
 @am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-explode.Tpo $(DEPDIR)/libclamav_la-explode.Plo
... ...
@@ -1870,6 +1944,13 @@ libclamav_la-xar.lo: xar.c
1870 1870
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1871 1871
 @am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-xar.lo `test -f 'xar.c' || echo '$(srcdir)/'`xar.c
1872 1872
 
1873
+libclamav_la-xz_iface.lo: xz_iface.c
1874
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-xz_iface.lo -MD -MP -MF $(DEPDIR)/libclamav_la-xz_iface.Tpo -c -o libclamav_la-xz_iface.lo `test -f 'xz_iface.c' || echo '$(srcdir)/'`xz_iface.c
1875
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-xz_iface.Tpo $(DEPDIR)/libclamav_la-xz_iface.Plo
1876
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='xz_iface.c' object='libclamav_la-xz_iface.lo' libtool=yes @AMDEPBACKSLASH@
1877
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
1878
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-xz_iface.lo `test -f 'xz_iface.c' || echo '$(srcdir)/'`xz_iface.c
1879
+
1873 1880
 libclamav_la-sf_base64decode.lo: sf_base64decode.c
1874 1881
 @am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-sf_base64decode.lo -MD -MP -MF $(DEPDIR)/libclamav_la-sf_base64decode.Tpo -c -o libclamav_la-sf_base64decode.lo `test -f 'sf_base64decode.c' || echo '$(srcdir)/'`sf_base64decode.c
1875 1882
 @am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-sf_base64decode.Tpo $(DEPDIR)/libclamav_la-sf_base64decode.Plo
... ...
@@ -99,6 +99,7 @@ static struct dconf_module modules[] = {
99 99
     { "ARCHIVE",    "DMG",	    ARCH_CONF_DMG,	    1 },
100 100
     { "ARCHIVE",    "XAR",	    ARCH_CONF_XAR,	    1 },
101 101
     { "ARCHIVE",    "HFSPLUS",	    ARCH_CONF_HFSPLUS,	    1 },
102
+    { "ARCHIVE",    "XZ",	    ARCH_CONF_XZ,	    1 },
102 103
 
103 104
     { "DOCUMENT",   "HTML",	    DOC_CONF_HTML,	    1 },
104 105
     { "DOCUMENT",   "RTF",	    DOC_CONF_RTF,	    1 },
... ...
@@ -85,6 +85,7 @@ struct cli_dconf {
85 85
 #define ARCH_CONF_DMG       0x40000
86 86
 #define ARCH_CONF_XAR       0x80000
87 87
 #define ARCH_CONF_HFSPLUS   0x100000
88
+#define ARCH_CONF_XZ        0x200000
88 89
 
89 90
 /* Document flags */
90 91
 #define DOC_CONF_HTML		0x1
... ...
@@ -105,6 +105,7 @@ static const struct ftmap_s {
105 105
     { "CL_TYPE_XAR",		CL_TYPE_XAR		},
106 106
     { "CL_TYPE_PART_ANY",	CL_TYPE_PART_ANY	},
107 107
     { "CL_TYPE_PART_HFSPLUS",	CL_TYPE_PART_HFSPLUS	},
108
+    { "CL_TYPE_XZ",     	CL_TYPE_XZ      	},
108 109
     { NULL,			CL_TYPE_IGNORED		}
109 110
 };
110 111
 
... ...
@@ -79,6 +79,7 @@ typedef enum {
79 79
     CL_TYPE_SWF,
80 80
     CL_TYPE_JAVA,
81 81
     CL_TYPE_XAR,
82
+    CL_TYPE_XZ,
82 83
 
83 84
     /* Section for partition types */
84 85
     CL_TYPE_PART_ANY, /* unknown partition type */
... ...
@@ -176,6 +176,7 @@ static const char *ftypes_int[] = {
176 176
   "0:0:78617221:XAR container file:CL_TYPE_ANY:CL_TYPE_XAR:75",
177 177
   "4:1024:482B0004:HFS+ partition:CL_TYPE_PART_ANY:CL_TYPE_PART_HFSPLUS:75",
178 178
   "4:1024:48580005:HFSX partition:CL_TYPE_PART_ANY:CL_TYPE_PART_HFSPLUS:75",
179
+  "0:0:FD377A585A00:XZ container file:CL_TYPE_ANY:CL_TYPE_XZ:75",
179 180
   NULL
180 181
 };
181 182
 
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- *  Copyright (C) 2007-2008 Sourcefire, Inc.
2
+ *  Copyright (C) 2007-2013 Sourcefire, Inc.
3 3
  *
4 4
  *  Authors: Tomasz Kojm, Trog
5 5
  *
... ...
@@ -195,8 +195,8 @@ void *cli_malloc(size_t size)
195 195
     alloc = malloc(size);
196 196
 
197 197
     if(!alloc) {
198
-	cli_errmsg("cli_malloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int) size);
199 198
 	perror("malloc_problem");
199
+	cli_errmsg("cli_malloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int) size);
200 200
 	return NULL;
201 201
     } else return alloc;
202 202
 }
... ...
@@ -215,8 +215,8 @@ void *cli_calloc(size_t nmemb, size_t size)
215 215
     alloc = calloc(nmemb, size);
216 216
 
217 217
     if(!alloc) {
218
-	cli_errmsg("cli_calloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int) (nmemb * size));
219 218
 	perror("calloc_problem");
219
+	cli_errmsg("cli_calloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int) (nmemb * size));
220 220
 	return NULL;
221 221
     } else return alloc;
222 222
 }
... ...
@@ -234,8 +234,8 @@ void *cli_realloc(void *ptr, size_t size)
234 234
     alloc = realloc(ptr, size);
235 235
 
236 236
     if(!alloc) {
237
-	cli_errmsg("cli_realloc(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int) size);
238 237
 	perror("realloc_problem");
238
+	cli_errmsg("cli_realloc(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int) size);
239 239
 	return NULL;
240 240
     } else return alloc;
241 241
 }
... ...
@@ -253,8 +253,8 @@ void *cli_realloc2(void *ptr, size_t size)
253 253
     alloc = realloc(ptr, size);
254 254
 
255 255
     if(!alloc) {
256
-	cli_errmsg("cli_realloc2(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int) size);
257 256
 	perror("realloc_problem");
257
+	cli_errmsg("cli_realloc2(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int) size);
258 258
 	if(ptr)
259 259
 	    free(ptr);
260 260
 	return NULL;
... ...
@@ -274,8 +274,8 @@ char *cli_strdup(const char *s)
274 274
     alloc = strdup(s);
275 275
 
276 276
     if(!alloc) {
277
-        cli_errmsg("cli_strdup(): Can't allocate memory (%u bytes).\n", (unsigned int) strlen(s));
278 277
         perror("strdup_problem");
278
+        cli_errmsg("cli_strdup(): Can't allocate memory (%u bytes).\n", (unsigned int) strlen(s));
279 279
         return NULL;
280 280
     }
281 281
 
... ...
@@ -2747,7 +2747,8 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2747 2747
 	} result;
2748 2748
 #endif
2749 2749
 	char *dbfile;
2750
-	int ret = CL_EOPEN, have_cld;
2750
+	int ret = CL_EOPEN, have_cld, ends_with_sep = 0;
2751
+	size_t dirname_len;
2751 2752
 	struct cl_cvd *daily_cld, *daily_cvd;
2752 2753
 
2753 2754
 
... ...
@@ -2758,6 +2759,14 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2758 2758
         return CL_EOPEN;
2759 2759
     }
2760 2760
 
2761
+    dirname_len = strlen(dirname);
2762
+    if(dirname_len >= strlen(PATHSEP)) {
2763
+        if(strcmp(dirname + dirname_len - strlen(PATHSEP), PATHSEP) == 0) {
2764
+            cli_dbgmsg("cli_loaddbdir(): dirname ends with separator\n");
2765
+            ends_with_sep = 1;
2766
+        }
2767
+    }
2768
+
2761 2769
     /* first round - load .ign and .ign2 files */
2762 2770
 #ifdef HAVE_READDIR_R_3
2763 2771
     while(!readdir_r(dd, &result.d, &dent) && dent) {
... ...
@@ -2769,13 +2778,16 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2769 2769
 	if(dent->d_ino)
2770 2770
 	{
2771 2771
 	    if(cli_strbcasestr(dent->d_name, ".ign") || cli_strbcasestr(dent->d_name, ".ign2")) {
2772
-		dbfile = (char *) cli_malloc(strlen(dent->d_name) + strlen(dirname) + 2);
2772
+		dbfile = (char *) cli_malloc(strlen(dent->d_name) + dirname_len + 2);
2773 2773
 		if(!dbfile) {
2774 2774
 		    cli_errmsg("cli_loaddbdir(): dbfile == NULL\n");
2775 2775
 		    closedir(dd);
2776 2776
 		    return CL_EMEM;
2777 2777
 		}
2778
-		sprintf(dbfile, "%s"PATHSEP"%s", dirname, dent->d_name);
2778
+		if(ends_with_sep)
2779
+		    sprintf(dbfile, "%s%s", dirname, dent->d_name);
2780
+		else
2781
+                    sprintf(dbfile, "%s"PATHSEP"%s", dirname, dent->d_name);
2779 2782
 		ret = cli_load(dbfile, engine, signo, options, NULL);
2780 2783
 		if(ret) {
2781 2784
 		    cli_errmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
... ...
@@ -2789,14 +2801,17 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2789 2789
     }
2790 2790
 
2791 2791
     /* the daily db must be loaded before main */
2792
-    dbfile = (char *) cli_malloc(strlen(dirname) + 20);
2792
+    dbfile = (char *) cli_malloc(dirname_len + 20);
2793 2793
     if(!dbfile) {
2794 2794
 	closedir(dd);
2795 2795
     cli_errmsg("cli_loaddbdir: Can't allocate memory for dbfile\n");
2796 2796
 	return CL_EMEM;
2797 2797
     }
2798 2798
 
2799
-    sprintf(dbfile, "%s"PATHSEP"daily.cld", dirname);
2799
+    if(ends_with_sep)
2800
+        sprintf(dbfile, "%sdaily.cld", dirname);
2801
+    else
2802
+        sprintf(dbfile, "%s"PATHSEP"daily.cld", dirname);
2800 2803
     have_cld = !access(dbfile, R_OK);
2801 2804
     if(have_cld) {
2802 2805
 	daily_cld = cl_cvdhead(dbfile);
... ...
@@ -2807,7 +2822,10 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2807 2807
 	    return CL_EMALFDB;
2808 2808
 	}
2809 2809
     }
2810
-    sprintf(dbfile, "%s"PATHSEP"daily.cvd", dirname); 
2810
+    if(ends_with_sep)
2811
+        sprintf(dbfile, "%sdaily.cvd", dirname);
2812
+    else
2813
+        sprintf(dbfile, "%s"PATHSEP"daily.cvd", dirname);
2811 2814
     if(!access(dbfile, R_OK)) {
2812 2815
 	if(have_cld) {
2813 2816
 	    daily_cvd = cl_cvdhead(dbfile);
... ...
@@ -2818,12 +2836,19 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2818 2818
 		closedir(dd);
2819 2819
 		return CL_EMALFDB;
2820 2820
 	    }
2821
-	    if(daily_cld->version > daily_cvd->version)
2822
-		sprintf(dbfile, "%s"PATHSEP"daily.cld", dirname);
2821
+	    if(daily_cld->version > daily_cvd->version) {
2822
+		if(ends_with_sep)
2823
+                    sprintf(dbfile, "%sdaily.cld", dirname);
2824
+		else
2825
+                    sprintf(dbfile, "%s"PATHSEP"daily.cld", dirname);
2826
+	    }
2823 2827
 	    cl_cvdfree(daily_cvd);
2824 2828
 	}
2825 2829
     } else {
2826
-	sprintf(dbfile, "%s"PATHSEP"daily.cld", dirname);
2830
+	if(ends_with_sep)
2831
+	    sprintf(dbfile, "%sdaily.cld", dirname);
2832
+	else
2833
+	    sprintf(dbfile, "%s"PATHSEP"daily.cld", dirname);
2827 2834
     }
2828 2835
     if(have_cld)
2829 2836
 	cl_cvdfree(daily_cld);
... ...
@@ -2835,7 +2860,10 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2835 2835
     }
2836 2836
 
2837 2837
     /* try to load local.gdb next */
2838
-    sprintf(dbfile, "%s"PATHSEP"local.gdb", dirname);
2838
+    if(ends_with_sep)
2839
+        sprintf(dbfile, "%slocal.gdb", dirname);
2840
+    else
2841
+        sprintf(dbfile, "%s"PATHSEP"local.gdb", dirname);
2839 2842
     if(!access(dbfile, R_OK) && (ret = cli_load(dbfile, engine, signo, options, NULL))) {
2840 2843
 	free(dbfile);
2841 2844
 	closedir(dd);
... ...
@@ -2843,7 +2871,10 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2843 2843
     }
2844 2844
 
2845 2845
     /* check for and load daily.cfg */
2846
-    sprintf(dbfile, "%s"PATHSEP"daily.cfg", dirname);
2846
+    if(ends_with_sep)
2847
+        sprintf(dbfile, "%sdaily.cfg", dirname);
2848
+    else
2849
+        sprintf(dbfile, "%s"PATHSEP"daily.cfg", dirname);
2847 2850
     if(!access(dbfile, R_OK) && (ret = cli_load(dbfile, engine, signo, options, NULL))) {
2848 2851
 	free(dbfile);
2849 2852
 	closedir(dd);
... ...
@@ -2868,13 +2899,16 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2868 2868
 		    continue;
2869 2869
 		}
2870 2870
 
2871
-		dbfile = (char *) cli_malloc(strlen(dent->d_name) + strlen(dirname) + 2);
2871
+		dbfile = (char *) cli_malloc(strlen(dent->d_name) + dirname_len + 2);
2872 2872
 		if(!dbfile) {
2873 2873
 		    cli_errmsg("cli_loaddbdir(): dbfile == NULL\n");
2874 2874
 		    closedir(dd);
2875 2875
 		    return CL_EMEM;
2876 2876
 		}
2877
-		sprintf(dbfile, "%s"PATHSEP"%s", dirname, dent->d_name);
2877
+                if(ends_with_sep)
2878
+		    sprintf(dbfile, "%s%s", dirname, dent->d_name);
2879
+                else
2880
+		    sprintf(dbfile, "%s"PATHSEP"%s", dirname, dent->d_name);
2878 2881
 		ret = cli_load(dbfile, engine, signo, options, NULL);
2879 2882
 		if(ret) {
2880 2883
 		    cli_errmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- *  Copyright (C) 2007-2008 Sourcefire, Inc.
2
+ *  Copyright (C) 2007-2013 Sourcefire, Inc.
3 3
  *
4 4
  *  Authors: Tomasz Kojm
5 5
  *
... ...
@@ -97,6 +97,7 @@
97 97
 #include "dmg.h"
98 98
 #include "xar.h"
99 99
 #include "hfsplus.h"
100
+#include "xz_iface.h"
100 101
 
101 102
 #ifdef HAVE_BZLIB_H
102 103
 #include <bzlib.h>
... ...
@@ -700,6 +701,99 @@ static int cli_scanbzip(cli_ctx *ctx)
700 700
 }
701 701
 #endif
702 702
 
703
+static int cli_scanxz(cli_ctx *ctx)
704
+{
705
+    int ret = CL_CLEAN, fd, rc;
706
+    unsigned long int size = 0;
707
+    char *tmpname;
708
+    struct CLI_XZ strm = {{0}};
709
+    size_t off = 0;
710
+    size_t avail;
711
+    unsigned char * buf = cli_malloc(CLI_XZ_OBUF_SIZE);
712
+
713
+    if (buf == NULL) {
714
+	cli_errmsg("cli_scanxz: nomemory for decompress buffer.\n");
715
+        return CL_EMEM;
716
+    }
717
+    strm.next_out = buf;
718
+    strm.avail_out = CLI_XZ_OBUF_SIZE;
719
+    rc = cli_XzInit(&strm);
720
+    if (rc != XZ_RESULT_OK) {
721
+	cli_errmsg("cli_scanxz: DecompressInit failed: %i\n", rc);
722
+        free(buf);
723
+	return CL_EOPEN;
724
+    }
725
+
726
+    if ((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd))) {
727
+	cli_errmsg("cli_scanxz: Can't generate temporary file.\n");
728
+	cli_XzShutdown(&strm);
729
+        free(buf);
730
+	return ret;
731
+    }
732
+    cli_dbgmsg("cli_scanxz: decompressing to file %s\n", tmpname);
733
+
734
+    do {
735
+        /* set up input buffer */
736
+	if (!strm.avail_in) {
737
+            strm.next_in = (void*)fmap_need_off_once_len(*ctx->fmap, off, CLI_XZ_IBUF_SIZE, &avail);
738
+	    strm.avail_in = avail;
739
+	    off += avail;
740
+	    if (!strm.avail_in) {
741
+		cli_errmsg("cli_scanxz: premature end of compressed stream\n");
742
+                ret = CL_EFORMAT;
743
+		goto xz_exit;
744
+	    }
745
+	}
746
+
747
+        /* xz decompress a chunk */
748
+	rc = cli_XzDecode(&strm);
749
+	if (XZ_RESULT_OK != rc && XZ_STREAM_END != rc) {
750
+	    cli_errmsg("cli_scanxz: decompress error: %d\n", rc);
751
+            ret = CL_EFORMAT;
752
+            goto xz_exit;
753
+	}
754
+        //cli_dbgmsg("cli_scanxz: xz decompressed %li of %li available bytes\n",
755
+        //           avail - strm.avail_in, avail);
756
+        
757
+        /* write decompress buffer */
758
+	if (!strm.avail_out || rc == XZ_STREAM_END) {            
759
+	    size_t towrite = CLI_XZ_OBUF_SIZE - strm.avail_out;
760
+	    size += towrite;
761
+
762
+            //cli_dbgmsg("Writing %li bytes to XZ decompress temp file(%li byte total)\n",
763
+            //           towrite, size);
764
+
765
+	    if(cli_writen(fd, buf, towrite) != towrite) {
766
+		cli_errmsg("cli_scanxz: Can't write to file.\n");
767
+                ret = CL_EWRITE;
768
+                goto xz_exit;
769
+	    }
770
+	    if (cli_checklimits("cli_scanxz", ctx, size, 0, 0) != CL_CLEAN) {
771
+                cli_warnmsg("cli_scanxz: decompress file size exceeds limits - "
772
+                            "only scanning %li bytes\n", size);
773
+		break;
774
+            }
775
+	    strm.next_out = buf;
776
+	    strm.avail_out = CLI_XZ_OBUF_SIZE;
777
+	}
778
+    } while (XZ_STREAM_END != rc);
779
+
780
+    /* scan decompressed file */
781
+    if ((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
782
+	cli_dbgmsg("cli_scanxz: Infected with %s\n", cli_get_last_virus(ctx));
783
+    }
784
+
785
+ xz_exit:
786
+    cli_XzShutdown(&strm);
787
+    close(fd);
788
+    if(!ctx->engine->keeptmp)
789
+	if (cli_unlink(tmpname) && ret == CL_CLEAN)
790
+            ret = CL_EUNLINK;
791
+    free(tmpname);
792
+    free(buf);
793
+    return ret;
794
+}
795
+
703 796
 static int cli_scanszdd(cli_ctx *ctx)
704 797
 {
705 798
 	int ofd, ret;
... ...
@@ -2305,7 +2399,6 @@ static int magic_scandesc(cli_ctx *ctx, cli_file_t type)
2305 2305
 	bitset_t *old_hook_lsig_matches;
2306 2306
 	const char *filetype;
2307 2307
 	int cache_clean = 0, res;
2308
-	unsigned int viruses_found = 0;
2309 2308
 
2310 2309
     if(!ctx->engine) {
2311 2310
 	cli_errmsg("CRITICAL: engine == NULL\n");
... ...
@@ -2463,6 +2556,11 @@ static int magic_scandesc(cli_ctx *ctx, cli_file_t type)
2463 2463
 		ret = cli_scanbzip(ctx);
2464 2464
 	    break;
2465 2465
 
2466
+	case CL_TYPE_XZ:
2467
+	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_XZ))
2468
+		ret = cli_scanxz(ctx);
2469
+	    break;
2470
+
2466 2471
 	case CL_TYPE_ARJ:
2467 2472
 	    ctx->container_type = CL_TYPE_ARJ;
2468 2473
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ARJ))
... ...
@@ -94,10 +94,10 @@ static int xar_get_numeric_from_xml_element(xmlTextReaderPtr reader, long * valu
94 94
     hash - pointer to int for returning checksum algorithm.
95 95
   returns - void
96 96
  */
97
-static void xar_get_checksum_values(xmlTextReaderPtr reader, char ** cksum, int * hash)
97
+static void xar_get_checksum_values(xmlTextReaderPtr reader, unsigned char ** cksum, int * hash)
98 98
 {
99 99
     xmlChar * style = xmlTextReaderGetAttribute(reader, (const xmlChar *)"style");
100
-    const char * xmlval;
100
+    const xmlChar * xmlval;
101 101
 
102 102
     *hash = XAR_CKSUM_NONE;
103 103
     if (style == NULL) {
... ...
@@ -116,7 +116,7 @@ static void xar_get_checksum_values(xmlTextReaderPtr reader, char ** cksum, int
116 116
     }
117 117
 
118 118
     if (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) {
119
-        xmlval = (const char *)xmlTextReaderConstValue(reader);
119
+        xmlval = xmlTextReaderConstValue(reader);
120 120
         if (xmlval) {
121 121
             *cksum = xmlStrdup(xmlval); 
122 122
             cli_dbgmsg("cli_scanxar: checksum value is %s.\n", *cksum);
... ...
@@ -145,7 +145,7 @@ static void xar_get_checksum_values(xmlTextReaderPtr reader, char ** cksum, int
145 145
    returns - CL_FORMAT, CL_SUCCESS, CL_BREAK. CL_BREAK indicates no more <data>/<ea> element.
146 146
  */
147 147
 static int xar_get_toc_data_values(xmlTextReaderPtr reader, long *length, long *offset, long *size, int *encoding,
148
-                                   char ** a_cksum, int * a_hash, char ** e_cksum, int * e_hash)
148
+                                   unsigned char ** a_cksum, int * a_hash, unsigned char ** e_cksum, int * e_hash)
149 149
 {
150 150
     const xmlChar *name;
151 151
     int indata = 0, inea = 0;
... ...
@@ -208,8 +208,7 @@ static int xar_get_toc_data_values(xmlTextReaderPtr reader, long *length, long *
208 208
                     *encoding = CL_TYPE_7Z;
209 209
                  } else if (xmlStrEqual(style, (const xmlChar *)"application/x-xz")) {
210 210
                     cli_dbgmsg("cli_scanxar: encoding = application/x-xz.\n");
211
-                    cli_dbgmsg("cli_scanxar: decompression of application/x-xz not supported.\n");
212
-                    *encoding = CL_TYPE_ANY;
211
+                    *encoding = CL_TYPE_XZ;
213 212
                 } else {
214 213
                     cli_errmsg("cli_scaxar: unknown style value=%s for encoding element\n", style);
215 214
                     *encoding = CL_TYPE_ANY;
... ...
@@ -292,7 +291,7 @@ static int xar_scan_subdocuments(xmlTextReaderPtr reader, cli_ctx *ctx)
292 292
             subdoc_len = xmlStrlen(subdoc);
293 293
             cli_dbgmsg("cli_scanxar: in-memory scan of xml subdocument, len %i.\n", subdoc_len);
294 294
             rc = cli_mem_scandesc(subdoc, subdoc_len, ctx);
295
-            if (rc = CL_VIRUS && SCAN_ALL)
295
+            if (rc == CL_VIRUS && SCAN_ALL)
296 296
                 rc = CL_SUCCESS;
297 297
             
298 298
             /* make a file to leave if --leave-temps in effect */
... ...
@@ -420,7 +419,7 @@ int cli_scanxar(cli_ctx *ctx)
420 420
     char *toc, *tmpname;
421 421
     xmlTextReaderPtr reader = NULL;
422 422
     int a_hash, e_hash;
423
-    char *a_cksum = NULL, *e_cksum = NULL;
423
+    unsigned char *a_cksum = NULL, *e_cksum = NULL;
424 424
 
425 425
     /* retrieve xar header */
426 426
     if (fmap_readn(*ctx->fmap, &hdr, 0, sizeof(hdr)) != sizeof(hdr)) {
... ...
@@ -531,7 +530,7 @@ int cli_scanxar(cli_ctx *ctx)
531 531
     while (CL_SUCCESS == (rc = xar_get_toc_data_values(reader, &length, &offset, &size, &encoding,
532 532
                                                        &a_cksum, &a_hash, &e_cksum, &e_hash))) {
533 533
         int do_extract_cksum = 1;
534
-        char * blockp;
534
+        unsigned char * blockp;
535 535
         SHA1Context a_sc, e_sc;
536 536
         cli_md5_ctx a_mc, e_mc;
537 537
         void *a_hash_ctx, *e_hash_ctx;
... ...
@@ -575,7 +574,7 @@ int cli_scanxar(cli_ctx *ctx)
575 575
                 void * next_in;
576 576
                 unsigned int bytes = MIN(map->len - at, map->pgsz);
577 577
                 bytes = MIN(length, bytes);
578
-                cli_dbgmsg("cli_scanxar: fmap %u bytes\n", bytes);
578
+                //cli_dbgmsg("cli_scanxar: fmap %u bytes\n", bytes);
579 579
                 if(!(strm.next_in = next_in = (void*)fmap_need_off_once(map, at, bytes))) {
580 580
                     cli_dbgmsg("cli_scanxar: Can't read %u bytes @ %lu.\n", bytes, (long unsigned)at);
581 581
                     inflateEnd(&strm);
... ...
@@ -589,7 +588,7 @@ int cli_scanxar(cli_ctx *ctx)
589 589
                     unsigned char buff[FILEBUFF];
590 590
                     strm.avail_out = sizeof(buff);
591 591
                     strm.next_out = buff;
592
-                    cli_dbgmsg("cli_scanxar: inflating.....\n");
592
+                    //cli_dbgmsg("cli_scanxar: inflating.....\n");
593 593
                     inf = inflate(&strm, Z_SYNC_FLUSH);
594 594
                     if (inf != Z_OK && inf != Z_STREAM_END && inf != Z_BUF_ERROR) {
595 595
                         cli_errmsg("cli_scanxar: inflate error %i %s.\n", inf, strm.msg?strm.msg:"");
... ...
@@ -624,15 +623,16 @@ int cli_scanxar(cli_ctx *ctx)
624 624
             inflateEnd(&strm);
625 625
             break;
626 626
         case CL_TYPE_7Z:
627
-            {
628 627
 #define CLI_LZMA_OBUF_SIZE 1024*1024
629 628
 #define CLI_LZMA_HDR_SIZE LZMA_PROPS_SIZE+8
630
-#define CLI_LZMA_CRATIO_SHIFT 2 /* estimated compression ratio 25% */
631
-                struct CLI_LZMA lz = {0};
629
+#define CLI_LZMA_IBUF_SIZE CLI_LZMA_OBUF_SIZE>>2 /* estimated compression ratio 25% */
630
+            {
631
+                struct CLI_LZMA lz;
632 632
                 unsigned long in_remaining = length;
633 633
                 unsigned long out_size = 0;
634
-                char * buff = __lzma_wrap_alloc(NULL, CLI_LZMA_OBUF_SIZE);
634
+                unsigned char * buff = __lzma_wrap_alloc(NULL, CLI_LZMA_OBUF_SIZE);
635 635
                 
636
+                memset(&lz, 0, sizeof(lz));
636 637
                 if (buff == NULL) {
637 638
                     cli_errmsg("cli_scanxar: memory request for lzma decompression buffer fails.\n");
638 639
                     rc = CL_EMEM;
... ...
@@ -672,7 +672,7 @@ int cli_scanxar(cli_ctx *ctx)
672 672
 
673 673
                     lz.next_out = buff;
674 674
                     lz.avail_out = CLI_LZMA_OBUF_SIZE;
675
-                    lz.avail_in = avail_in = MIN(CLI_LZMA_OBUF_SIZE>>CLI_LZMA_CRATIO_SHIFT, in_remaining);
675
+                    lz.avail_in = avail_in = MIN(CLI_LZMA_IBUF_SIZE, in_remaining);
676 676
                     lz.next_in = next_in = (void*)fmap_need_off_once(map, at, lz.avail_in);
677 677
                     if (lz.next_in == NULL) {
678 678
                         cli_errmsg("cli_scanxar: Can't read %li bytes @ %li, errno: %s.\n",
... ...
@@ -736,9 +736,10 @@ int cli_scanxar(cli_ctx *ctx)
736 736
         case CL_TYPE_ANY:
737 737
         default:
738 738
         case CL_TYPE_BZ:
739
+        case CL_TYPE_XZ:
740
+            /* for uncompressed, bzip2, xz, and unknown, just pull the file, cli_magic_scandesc does the rest */
739 741
             do_extract_cksum = 0;
740 742
             {
741
-                /* for uncompressed, bzip2, and unknown, just pull the file, cli_magic_scandesc does the rest */
742 743
                 unsigned long write_len;
743 744
                 
744 745
                 if (ctx->engine->maxfilesize)
... ...
@@ -766,7 +767,7 @@ int cli_scanxar(cli_ctx *ctx)
766 766
 
767 767
         xar_hash_final(a_hash_ctx, result, a_hash);
768 768
         if (a_cksum != NULL) {
769
-            expected = cli_hex2str(a_cksum);
769
+            expected = cli_hex2str((char *)a_cksum);
770 770
             if (xar_hash_check(a_hash, result, expected) != 0) {
771 771
                 cli_dbgmsg("cli_scanxar: archived-checksum missing or mismatch.\n");
772 772
                 cksum_fails++;
... ...
@@ -780,7 +781,7 @@ int cli_scanxar(cli_ctx *ctx)
780 780
         if (e_cksum != NULL) {
781 781
             if (do_extract_cksum) {
782 782
                 xar_hash_final(e_hash_ctx, result, e_hash);
783
-                expected = cli_hex2str(e_cksum);
783
+                expected = cli_hex2str((char *)e_cksum);
784 784
                 if (xar_hash_check(e_hash, result, expected) != 0) {
785 785
                     cli_dbgmsg("cli_scanxar: extracted-checksum missing or mismatch.\n");
786 786
                     cksum_fails++;
787 787
new file mode 100644
... ...
@@ -0,0 +1,78 @@
0
+/*
1
+ *  Copyright (C) 2013 Sourcefire, Inc.
2
+ *
3
+ *  Authors: Steven Morgan (smorgan@sourcefire.com)
4
+ *
5
+ *  This program is free software; you can redistribute it and/or modify
6
+ *  it under the terms of the GNU General Public License version 2 as
7
+ *  published by the Free Software Foundation.
8
+ *
9
+ *  This program is distributed in the hope that it will be useful,
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ *  GNU General Public License for more details.
13
+ *
14
+ *  You should have received a copy of the GNU General Public License
15
+ *  along with this program; if not, write to the Free Software
16
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ *  MA 02110-1301, USA.
18
+ */
19
+
20
+#if HAVE_CONFIG_H
21
+#include "clamav-config.h"
22
+#endif
23
+
24
+#include "7z/Sha256.h"
25
+#include "7z/XzCrc64.h"
26
+#include "xz_iface.h"
27
+
28
+void *__xz_wrap_alloc(void *unused, size_t size) { 
29
+    if(!size || size > CLI_MAX_ALLOCATION)
30
+	return NULL;
31
+    unused = unused;
32
+    if(!size || size > CLI_MAX_ALLOCATION) {
33
+	cli_dbgmsg("xz_iface: Attempt to allocate %lu bytes exceeds CLI_MAX_ALLOCATION.\n",
34
+                   (unsigned long int) size);
35
+	return NULL;
36
+    }
37
+    return cli_malloc(size);
38
+}
39
+void __xz_wrap_free(void *unused, void *freeme) {
40
+    unused = unused;
41
+    free(freeme);
42
+}
43
+
44
+static ISzAlloc g_Alloc = { __xz_wrap_alloc, __xz_wrap_free };
45
+    
46
+int cli_XzInit(struct CLI_XZ *XZ) {
47
+    if (SZ_OK != XzUnpacker_Create(&XZ->state, &g_Alloc))
48
+        return XZ_RESULT_DATA_ERROR;
49
+    if (g_Crc64Table[1] == 0)
50
+        Crc64GenerateTable();
51
+    return XZ_RESULT_OK;
52
+}
53
+	
54
+void cli_XzShutdown(struct CLI_XZ *XZ) {
55
+    XzUnpacker_Free(&XZ->state);
56
+}
57
+
58
+int cli_XzDecode(struct CLI_XZ *XZ) {
59
+    SRes res;
60
+    SizeT outbytes, inbytes;
61
+
62
+    inbytes = XZ->avail_in;
63
+    outbytes = XZ->avail_out;
64
+    res = XzUnpacker_Code(&XZ->state, XZ->next_out, &outbytes, 
65
+                          XZ->next_in, &inbytes, CODER_FINISH_ANY, &XZ->status);
66
+    XZ->avail_in -= inbytes;
67
+    XZ->next_in += inbytes;
68
+    XZ->avail_out -= outbytes;
69
+    XZ->next_out += outbytes;
70
+    if (XZ->status == CODER_STATUS_FINISHED_WITH_MARK || XzUnpacker_IsStreamWasFinished(&XZ->state))
71
+        return XZ_STREAM_END;
72
+    if (XZ->status == CODER_STATUS_NOT_FINISHED && XZ->avail_out == 0)
73
+        return XZ_RESULT_OK;
74
+    if (res != SZ_OK)
75
+	return XZ_RESULT_DATA_ERROR;
76
+    return XZ_RESULT_OK;
77
+}
0 78
new file mode 100644
... ...
@@ -0,0 +1,48 @@
0
+/*
1
+ *  Copyright (C) 2013 Sourcefire, Inc.
2
+ *
3
+ *  Authors: Steven Morgan (smorgan@sourcefire.com)
4
+ *
5
+ *  This program is free software; you can redistribute it and/or modify
6
+ *  it under the terms of the GNU General Public License version 2 as
7
+ *  published by the Free Software Foundation.
8
+ *
9
+ *  This program is distributed in the hope that it will be useful,
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ *  GNU General Public License for more details.
13
+ *
14
+ *  You should have received a copy of the GNU General Public License
15
+ *  along with this program; if not, write to the Free Software
16
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ *  MA 02110-1301, USA.
18
+ */
19
+
20
+#ifndef __XZ_IFACE_H
21
+#define __XZ_IFACE_H
22
+
23
+#include "7z/Xz.h"
24
+#include "cltypes.h"
25
+#include "others.h"
26
+
27
+struct CLI_XZ {
28
+    CXzUnpacker state;
29
+    ECoderStatus status;
30
+    unsigned char *next_in;
31
+    unsigned char *next_out;
32
+    SizeT avail_in;
33
+    SizeT avail_out;
34
+};
35
+
36
+int cli_XzInit(struct CLI_XZ *);
37
+void cli_XzShutdown(struct CLI_XZ *);
38
+int cli_XzDecode(struct CLI_XZ *);
39
+
40
+#define XZ_RESULT_OK 0
41
+#define XZ_RESULT_DATA_ERROR 1
42
+#define XZ_STREAM_END 2
43
+
44
+#define CLI_XZ_OBUF_SIZE 1024*1024
45
+#define CLI_XZ_IBUF_SIZE CLI_XZ_OBUF_SIZE>>2 /* compression ratio 25% */
46
+
47
+#endif /* __XZ_IFACE_H */
... ...
@@ -173,8 +173,8 @@ static int hashsig(const struct optstruct *opts, unsigned int mdb, int type)
173 173
     if(opts->filename) {
174 174
 	for(i = 0; opts->filename[i]; i++) {
175 175
 	    if(CLAMSTAT(opts->filename[i], &sb) == -1) {
176
-		mprintf("!hashsig: Can't access file %s\n", opts->filename[i]);
177 176
 		perror("hashsig");
177
+		mprintf("!hashsig: Can't access file %s\n", opts->filename[i]);
178 178
 		return -1;
179 179
 	    } else {
180 180
 		if((sb.st_mode & S_IFMT) == S_IFREG) {
... ...
@@ -339,8 +339,8 @@ static char *getdsig(const char *host, const char *user, const unsigned char *da
339 339
     server.sin_port = htons(33101);
340 340
 
341 341
     if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) {
342
-        closesocket(sockd);
343 342
 	perror("connect()");
343
+        closesocket(sockd);
344 344
 	mprintf("!getdsig: Can't connect to ClamAV Signing Service at %s\n", host);
345 345
 	memset(pass, 0, sizeof(pass));
346 346
 	return NULL;
... ...
@@ -355,7 +355,7 @@ END_TEST
355 355
 static char **testfiles = NULL;
356 356
 static unsigned testfiles_n = 0;
357 357
 
358
-#if HAVE_LIBBZ2
358
+#if HAVE_BZLIB_H
359 359
 static const int expected_testfiles = 48;
360 360
 #else
361 361
 static const int expected_testfiles = 46;
362 362
deleted file mode 100644
... ...
@@ -1,129 +0,0 @@
1
-#ifndef __LIBXML_WIN32_CONFIG__
2
-#define __LIBXML_WIN32_CONFIG__
3
-
4
-#define HAVE_CTYPE_H
5
-#define HAVE_STDARG_H
6
-#define HAVE_MALLOC_H
7
-#define HAVE_ERRNO_H
8
-
9
-#if defined(_WIN32_WCE)
10
-#undef HAVE_ERRNO_H
11
-#include <windows.h>
12
-#include "wincecompat.h"
13
-#else
14
-#define HAVE_SYS_STAT_H
15
-#define HAVE__STAT
16
-#define HAVE_STAT
17
-#define HAVE_STDLIB_H
18
-#define HAVE_TIME_H
19
-#define HAVE_FCNTL_H
20
-/* The following features are enabled for the clamav partial libxml2 port */
21
-#define LIBXML_OUTPUT_ENABLED 1
22
-#define LIBXML_PUSH_ENABLED 1
23
-#define LIBXML_WRITER_ENABLED 1
24
-#define LIBXML_TREE_ENABLED 1
25
-#include <io.h>
26
-#include <direct.h>
27
-#endif
28
-
29
-#include <libxml/xmlversion.h>
30
-
31
-#ifndef ICONV_CONST
32
-#define ICONV_CONST const
33
-#endif
34
-
35
-#ifdef NEED_SOCKETS
36
-#include <wsockcompat.h>
37
-#endif
38
-
39
-/*
40
- * Windows platforms may define except 
41
- */
42
-#undef except
43
-
44
-#define HAVE_ISINF
45
-#define HAVE_ISNAN
46
-#include <math.h>
47
-#if defined(_MSC_VER) || defined(__BORLANDC__)
48
-/* MS C-runtime has functions which can be used in order to determine if
49
-   a given floating-point variable contains NaN, (+-)INF. These are 
50
-   preferred, because floating-point technology is considered propriatary
51
-   by MS and we can assume that their functions know more about their 
52
-   oddities than we do. */
53
-#include <float.h>
54
-/* Bjorn Reese figured a quite nice construct for isinf() using the _fpclass
55
-   function. */
56
-#ifndef isinf
57
-#define isinf(d) ((_fpclass(d) == _FPCLASS_PINF) ? 1 \
58
-	: ((_fpclass(d) == _FPCLASS_NINF) ? -1 : 0))
59
-#endif
60
-/* _isnan(x) returns nonzero if (x == NaN) and zero otherwise. */
61
-#ifndef isnan
62
-#define isnan(d) (_isnan(d))
63
-#endif
64
-#else /* _MSC_VER */
65
-#ifndef isinf
66
-static int isinf (double d) {
67
-    int expon = 0;
68
-    double val = frexp (d, &expon);
69
-    if (expon == 1025) {
70
-        if (val == 0.5) {
71
-            return 1;
72
-        } else if (val == -0.5) {
73
-            return -1;
74
-        } else {
75
-            return 0;
76
-        }
77
-    } else {
78
-        return 0;
79
-    }
80
-}
81
-#endif
82
-#ifndef isnan
83
-static int isnan (double d) {
84
-    int expon = 0;
85
-    double val = frexp (d, &expon);
86
-    if (expon == 1025) {
87
-        if (val == 0.5) {
88
-            return 0;
89
-        } else if (val == -0.5) {
90
-            return 0;
91
-        } else {
92
-            return 1;
93
-        }
94
-    } else {
95
-        return 0;
96
-    }
97
-}
98
-#endif
99
-#endif /* _MSC_VER */
100
-
101
-#if defined(_MSC_VER)
102
-#define mkdir(p,m) _mkdir(p)
103
-#define snprintf _snprintf
104
-#if _MSC_VER < 1500
105
-#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
106
-#endif
107
-#elif defined(__MINGW32__)
108
-#define mkdir(p,m) _mkdir(p)
109
-#endif
110
-
111
-/* Threading API to use should be specified here for compatibility reasons.
112
-   This is however best specified on the compiler's command-line. */
113
-#if defined(LIBXML_THREAD_ENABLED)
114
-#if !defined(HAVE_PTHREAD_H) && !defined(HAVE_WIN32_THREADS) && !defined(_WIN32_WCE)
115
-#define HAVE_WIN32_THREADS
116
-#endif
117
-#endif
118
-
119
-/* Some third-party libraries far from our control assume the following
120
-   is defined, which it is not if we don't include windows.h. */
121
-#if !defined(FALSE)
122
-#define FALSE 0
123
-#endif
124
-#if !defined(TRUE)
125
-#define TRUE (!(FALSE))
126
-#endif
127
-
128
-#endif /* __LIBXML_WIN32_CONFIG__ */
129
-
130 1
new file mode 100644
... ...
@@ -0,0 +1,129 @@
0
+#ifndef __LIBXML_WIN32_CONFIG__
1
+#define __LIBXML_WIN32_CONFIG__
2
+
3
+#define HAVE_CTYPE_H
4
+#define HAVE_STDARG_H
5
+#define HAVE_MALLOC_H
6
+#define HAVE_ERRNO_H
7
+
8
+#if defined(_WIN32_WCE)
9
+#undef HAVE_ERRNO_H
10
+#include <windows.h>
11
+#include "wincecompat.h"
12
+#else
13
+#define HAVE_SYS_STAT_H
14
+#define HAVE__STAT
15
+#define HAVE_STAT
16
+#define HAVE_STDLIB_H
17
+#define HAVE_TIME_H
18
+#define HAVE_FCNTL_H
19
+/* The following features are enabled for the clamav partial libxml2 port */
20
+#define LIBXML_OUTPUT_ENABLED 1
21
+#define LIBXML_PUSH_ENABLED 1
22
+#define LIBXML_WRITER_ENABLED 1
23
+#define LIBXML_TREE_ENABLED 1
24
+#include <io.h>
25
+#include <direct.h>
26
+#endif
27
+
28
+#include <libxml/xmlversion.h>
29
+
30
+#ifndef ICONV_CONST
31
+#define ICONV_CONST const
32
+#endif
33
+
34
+#ifdef NEED_SOCKETS
35
+#include <wsockcompat.h>
36
+#endif
37
+
38
+/*
39
+ * Windows platforms may define except 
40
+ */
41
+#undef except
42
+
43
+#define HAVE_ISINF
44
+#define HAVE_ISNAN
45
+#include <math.h>
46
+#if defined(_MSC_VER) || defined(__BORLANDC__)
47
+/* MS C-runtime has functions which can be used in order to determine if
48
+   a given floating-point variable contains NaN, (+-)INF. These are 
49
+   preferred, because floating-point technology is considered propriatary
50
+   by MS and we can assume that their functions know more about their 
51
+   oddities than we do. */
52
+#include <float.h>
53
+/* Bjorn Reese figured a quite nice construct for isinf() using the _fpclass
54
+   function. */
55
+#ifndef isinf
56
+#define isinf(d) ((_fpclass(d) == _FPCLASS_PINF) ? 1 \
57
+	: ((_fpclass(d) == _FPCLASS_NINF) ? -1 : 0))
58
+#endif
59
+/* _isnan(x) returns nonzero if (x == NaN) and zero otherwise. */
60
+#ifndef isnan
61
+#define isnan(d) (_isnan(d))
62
+#endif
63
+#else /* _MSC_VER */
64
+#ifndef isinf
65
+static int isinf (double d) {
66
+    int expon = 0;
67
+    double val = frexp (d, &expon);
68
+    if (expon == 1025) {
69
+        if (val == 0.5) {
70
+            return 1;
71
+        } else if (val == -0.5) {
72
+            return -1;
73
+        } else {
74
+            return 0;
75
+        }
76
+    } else {
77
+        return 0;
78
+    }
79
+}
80
+#endif
81
+#ifndef isnan
82
+static int isnan (double d) {
83
+    int expon = 0;
84
+    double val = frexp (d, &expon);
85
+    if (expon == 1025) {
86
+        if (val == 0.5) {
87
+            return 0;
88
+        } else if (val == -0.5) {
89
+            return 0;
90
+        } else {
91
+            return 1;
92
+        }
93
+    } else {
94
+        return 0;
95
+    }
96
+}
97
+#endif
98
+#endif /* _MSC_VER */
99
+
100
+#if defined(_MSC_VER)
101
+#define mkdir(p,m) _mkdir(p)
102
+#define snprintf _snprintf
103
+#if _MSC_VER < 1500
104
+#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
105
+#endif
106
+#elif defined(__MINGW32__)
107
+#define mkdir(p,m) _mkdir(p)
108
+#endif
109
+
110
+/* Threading API to use should be specified here for compatibility reasons.
111
+   This is however best specified on the compiler's command-line. */
112
+#if defined(LIBXML_THREAD_ENABLED)
113
+#if !defined(HAVE_PTHREAD_H) && !defined(HAVE_WIN32_THREADS) && !defined(_WIN32_WCE)
114
+#define HAVE_WIN32_THREADS
115
+#endif
116
+#endif
117
+
118
+/* Some third-party libraries far from our control assume the following
119
+   is defined, which it is not if we don't include windows.h. */
120
+#if !defined(FALSE)
121
+#define FALSE 0
122
+#endif
123
+#if !defined(TRUE)
124
+#define TRUE (!(FALSE))
125
+#endif
126
+
127
+#endif /* __LIBXML_WIN32_CONFIG__ */
128
+
... ...
@@ -34,7 +34,7 @@
34 34
  * copy to config.h own "preset" configuration file.
35 35
  * As result ifdef HAVE_CONFIG_H is omited here.
36 36
  */
37
-#include "config.h"
37
+#include "libxml/config.h"
38 38
 #include <libxml/xmlversion.h>
39 39
 #endif
40 40
 
... ...
@@ -165,6 +165,14 @@
165 165
     <ResourceCompile Include="res\libclamav.rc" />
166 166
   </ItemGroup>
167 167
   <ItemGroup>
168
+    <ClCompile Include="..\libclamav\7z\7zCrcOpt.c" />
169
+    <ClCompile Include="..\libclamav\7z\BraIA64.c" />
170
+    <ClCompile Include="..\libclamav\7z\CpuArch.c" />
171
+    <ClCompile Include="..\libclamav\7z\Delta.c" />
172
+    <ClCompile Include="..\libclamav\7z\Xz.c" />
173
+    <ClCompile Include="..\libclamav\7z\XzCrc64.c" />
174
+    <ClCompile Include="..\libclamav\7z\XzDec.c" />
175
+    <ClCompile Include="..\libclamav\7z\XzIn.c" />
168 176
     <ClCompile Include="..\libclamav\sf_base64decode.c" />
169 177
     <ClCompile Include="..\libclamav\tomsfastmath\mul\fp_mul_comba_small_set.c" />
170 178
     <ClCompile Include="..\libclamav\tomsfastmath\mul\fp_mul_comba_9.c" />
... ...
@@ -376,6 +384,7 @@
376 376
     <ClCompile Include="..\libclamav\version.c" />
377 377
     <ClCompile Include="..\libclamav\wwunpack.c" />
378 378
     <ClCompile Include="..\libclamav\xar.c" />
379
+    <ClCompile Include="..\libclamav\xz_iface.c" />
379 380
     <ClCompile Include="..\libclamav\yc.c" />
380 381
     <ClCompile Include="..\shared\getopt.c" />
381 382
     <ClCompile Include="..\shared\misc.c" />
... ...
@@ -471,4 +480,4 @@
471 471
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
472 472
   <ImportGroup Label="ExtensionTargets">
473 473
   </ImportGroup>
474
-</Project>
474
+</Project>
475 475
\ No newline at end of file
... ...
@@ -843,5 +843,32 @@
843 843
     <ClCompile Include="3rdparty\libxml2\xmlstring.c">
844 844
       <Filter>Source Files\libxml2</Filter>
845 845
     </ClCompile>
846
+    <ClCompile Include="..\libclamav\xz_iface.c">
847
+      <Filter>Source Files</Filter>
848
+    </ClCompile>
849
+    <ClCompile Include="..\libclamav\7z\7zCrcOpt.c">
850
+      <Filter>Source Files\7z</Filter>
851
+    </ClCompile>
852
+    <ClCompile Include="..\libclamav\7z\BraIA64.c">
853
+      <Filter>Source Files\7z</Filter>
854
+    </ClCompile>
855
+    <ClCompile Include="..\libclamav\7z\CpuArch.c">
856
+      <Filter>Source Files\7z</Filter>
857
+    </ClCompile>
858
+    <ClCompile Include="..\libclamav\7z\Delta.c">
859
+      <Filter>Source Files\7z</Filter>
860
+    </ClCompile>
861
+    <ClCompile Include="..\libclamav\7z\Xz.c">
862
+      <Filter>Source Files\7z</Filter>
863
+    </ClCompile>
864
+    <ClCompile Include="..\libclamav\7z\XzCrc64.c">
865
+      <Filter>Source Files\7z</Filter>
866
+    </ClCompile>
867
+    <ClCompile Include="..\libclamav\7z\XzDec.c">
868
+      <Filter>Source Files\7z</Filter>
869
+    </ClCompile>
870
+    <ClCompile Include="..\libclamav\7z\XzIn.c">
871
+      <Filter>Source Files\7z</Filter>
872
+    </ClCompile>
846 873
   </ItemGroup>
847
-</Project>
874
+</Project>
848 875
\ No newline at end of file