EGG extraction support includes deflate, bzip2, and lzma decompression. AZO (LZO?) decompression not yet supported. Solid archives not yet supported. Split archives may have some limited success.
This commit also includes updates to autoconf iconv.m4 file enable detection of libiconv in alternative install locations.
... | ... |
@@ -310,6 +310,11 @@ fi |
310 | 310 |
CL_MSG_STATUS([mempool ],[$have_mempool],[$enable_mempool]) |
311 | 311 |
|
312 | 312 |
AC_MSG_NOTICE([Summary of engine detection features]) |
313 |
+if test "X$have_iconv" = "Xyes"; then |
|
314 |
+ CL_MSG_STATUS([iconv ],[yes, from $ICONV_HOME],[]) |
|
315 |
+else |
|
316 |
+ CL_MSG_STATUS([iconv ],[no],[]) |
|
317 |
+fi |
|
313 | 318 |
CL_MSG_STATUS([bzip2 ],[$bzip_check],[$want_bzip2]) |
314 | 319 |
CL_MSG_STATUS([zlib ],[$ZLIB_HOME],[yes]) |
315 | 320 |
CL_MSG_STATUS([unrar ],[$want_unrar],[$want_unrar]) |
... | ... |
@@ -304,9 +304,9 @@ am__libclamav_la_SOURCES_DIST = matcher-ac.c matcher-ac.h matcher-bm.c \ |
304 | 304 |
msdoc.h matcher-pcre.c matcher-pcre.h regex_pcre.c \ |
305 | 305 |
regex_pcre.h msxml.c msxml.h msxml_parser.c msxml_parser.h \ |
306 | 306 |
tiff.c tiff.h hwp.c hwp.h lzw/lzwdec.c lzw/lzwdec.h \ |
307 |
- matcher-byte-comp.c matcher-byte-comp.h yara_arena.c \ |
|
308 |
- yara_arena.h yara_compiler.c yara_compiler.h yara_exec.c \ |
|
309 |
- yara_exec.h yara_hash.c yara_hash.h yara_grammar.y \ |
|
307 |
+ matcher-byte-comp.c matcher-byte-comp.h egg.c egg.h \ |
|
308 |
+ yara_arena.c yara_arena.h yara_compiler.c yara_compiler.h \ |
|
309 |
+ yara_exec.c yara_exec.h yara_hash.c yara_hash.h yara_grammar.y \ |
|
310 | 310 |
yara_lexer.l yara_lexer.h yara_parser.c yara_parser.h \ |
311 | 311 |
yara_clam.h bignum.h bignum_fast.h \ |
312 | 312 |
tomsfastmath/addsub/fp_add.c tomsfastmath/addsub/fp_add_d.c \ |
... | ... |
@@ -449,14 +449,14 @@ am_libclamav_la_OBJECTS = libclamav_la-matcher-ac.lo \ |
449 | 449 |
libclamav_la-regex_pcre.lo libclamav_la-msxml.lo \ |
450 | 450 |
libclamav_la-msxml_parser.lo libclamav_la-tiff.lo \ |
451 | 451 |
libclamav_la-hwp.lo libclamav_la-lzwdec.lo \ |
452 |
- libclamav_la-matcher-byte-comp.lo $(am__objects_1) \ |
|
453 |
- libclamav_la-fp_add.lo libclamav_la-fp_add_d.lo \ |
|
454 |
- libclamav_la-fp_addmod.lo libclamav_la-fp_cmp.lo \ |
|
455 |
- libclamav_la-fp_cmp_d.lo libclamav_la-fp_cmp_mag.lo \ |
|
456 |
- libclamav_la-fp_sub.lo libclamav_la-fp_sub_d.lo \ |
|
457 |
- libclamav_la-fp_submod.lo libclamav_la-s_fp_add.lo \ |
|
458 |
- libclamav_la-s_fp_sub.lo libclamav_la-fp_radix_size.lo \ |
|
459 |
- libclamav_la-fp_read_radix.lo \ |
|
452 |
+ libclamav_la-matcher-byte-comp.lo libclamav_la-egg.lo \ |
|
453 |
+ $(am__objects_1) libclamav_la-fp_add.lo \ |
|
454 |
+ libclamav_la-fp_add_d.lo libclamav_la-fp_addmod.lo \ |
|
455 |
+ libclamav_la-fp_cmp.lo libclamav_la-fp_cmp_d.lo \ |
|
456 |
+ libclamav_la-fp_cmp_mag.lo libclamav_la-fp_sub.lo \ |
|
457 |
+ libclamav_la-fp_sub_d.lo libclamav_la-fp_submod.lo \ |
|
458 |
+ libclamav_la-s_fp_add.lo libclamav_la-s_fp_sub.lo \ |
|
459 |
+ libclamav_la-fp_radix_size.lo libclamav_la-fp_read_radix.lo \ |
|
460 | 460 |
libclamav_la-fp_read_signed_bin.lo \ |
461 | 461 |
libclamav_la-fp_read_unsigned_bin.lo \ |
462 | 462 |
libclamav_la-fp_reverse.lo libclamav_la-fp_s_rmap.lo \ |
... | ... |
@@ -1365,8 +1365,8 @@ libclamav_la_SOURCES = matcher-ac.c matcher-ac.h matcher-bm.c \ |
1365 | 1365 |
openioc.h msdoc.c msdoc.h matcher-pcre.c matcher-pcre.h \ |
1366 | 1366 |
regex_pcre.c regex_pcre.h msxml.c msxml.h msxml_parser.c \ |
1367 | 1367 |
msxml_parser.h tiff.c tiff.h hwp.c hwp.h lzw/lzwdec.c \ |
1368 |
- lzw/lzwdec.h matcher-byte-comp.c matcher-byte-comp.h \ |
|
1369 |
- $(am__append_9) bignum.h bignum_fast.h \ |
|
1368 |
+ lzw/lzwdec.h matcher-byte-comp.c matcher-byte-comp.h egg.c \ |
|
1369 |
+ egg.h $(am__append_9) bignum.h bignum_fast.h \ |
|
1370 | 1370 |
tomsfastmath/addsub/fp_add.c tomsfastmath/addsub/fp_add_d.c \ |
1371 | 1371 |
tomsfastmath/addsub/fp_addmod.c tomsfastmath/addsub/fp_cmp.c \ |
1372 | 1372 |
tomsfastmath/addsub/fp_cmp_d.c \ |
... | ... |
@@ -1618,6 +1618,7 @@ distclean-compile: |
1618 | 1618 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-dlp.Plo@am__quote@ |
1619 | 1619 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-dmg.Plo@am__quote@ |
1620 | 1620 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-dsig.Plo@am__quote@ |
1621 |
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-egg.Plo@am__quote@ |
|
1621 | 1622 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-elf.Plo@am__quote@ |
1622 | 1623 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-entconv.Plo@am__quote@ |
1623 | 1624 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-events.Plo@am__quote@ |
... | ... |
@@ -2923,6 +2924,13 @@ libclamav_la-matcher-byte-comp.lo: matcher-byte-comp.c |
2923 | 2923 |
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ |
2924 | 2924 |
@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-matcher-byte-comp.lo `test -f 'matcher-byte-comp.c' || echo '$(srcdir)/'`matcher-byte-comp.c |
2925 | 2925 |
|
2926 |
+libclamav_la-egg.lo: egg.c |
|
2927 |
+@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-egg.lo -MD -MP -MF $(DEPDIR)/libclamav_la-egg.Tpo -c -o libclamav_la-egg.lo `test -f 'egg.c' || echo '$(srcdir)/'`egg.c |
|
2928 |
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-egg.Tpo $(DEPDIR)/libclamav_la-egg.Plo |
|
2929 |
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='egg.c' object='libclamav_la-egg.lo' libtool=yes @AMDEPBACKSLASH@ |
|
2930 |
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ |
|
2931 |
+@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-egg.lo `test -f 'egg.c' || echo '$(srcdir)/'`egg.c |
|
2932 |
+ |
|
2926 | 2933 |
libclamav_la-yara_arena.lo: yara_arena.c |
2927 | 2934 |
@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-yara_arena.lo -MD -MP -MF $(DEPDIR)/libclamav_la-yara_arena.Tpo -c -o libclamav_la-yara_arena.lo `test -f 'yara_arena.c' || echo '$(srcdir)/'`yara_arena.c |
2928 | 2935 |
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-yara_arena.Tpo $(DEPDIR)/libclamav_la-yara_arena.Plo |
... | ... |
@@ -105,6 +105,7 @@ static struct dconf_module modules[] = { |
105 | 105 |
{"ARCHIVE", "MBR", ARCH_CONF_MBR, 1}, |
106 | 106 |
{"ARCHIVE", "GPT", ARCH_CONF_GPT, 1}, |
107 | 107 |
{"ARCHIVE", "APM", ARCH_CONF_APM, 1}, |
108 |
+ {"ARCHIVE", "EGG", ARCH_CONF_EGG, 1}, |
|
108 | 109 |
|
109 | 110 |
{"DOCUMENT", "HTML", DOC_CONF_HTML, 1}, |
110 | 111 |
{"DOCUMENT", "RTF", DOC_CONF_RTF, 1}, |
... | ... |
@@ -95,6 +95,7 @@ struct cli_dconf { |
95 | 95 |
#define ARCH_CONF_MBR 0x800000 |
96 | 96 |
#define ARCH_CONF_GPT 0x1000000 |
97 | 97 |
#define ARCH_CONF_APM 0x2000000 |
98 |
+#define ARCH_CONF_EGG 0x4000000 |
|
98 | 99 |
|
99 | 100 |
/* Document flags */ |
100 | 101 |
#define DOC_CONF_HTML 0x1 |
... | ... |
@@ -149,10 +150,12 @@ struct cli_dconf { |
149 | 149 |
#define BYTECODE_ENGINE_MASK (BYTECODE_INTERPRETER | BYTECODE_JIT_X86 | BYTECODE_JIT_PPC | BYTECODE_JIT_ARM) |
150 | 150 |
|
151 | 151 |
#ifdef USE_MPOOL |
152 |
-struct cli_dconf *cli_dconf_init(mpool_t *); |
|
152 |
+struct cli_dconf * |
|
153 |
+cli_dconf_init(mpool_t *); |
|
153 | 154 |
#define cli_mpool_dconf_init(a) cli_dconf_init(a) |
154 | 155 |
#else |
155 |
-struct cli_dconf *cli_dconf_init(void); |
|
156 |
+struct cli_dconf * |
|
157 |
+cli_dconf_init(void); |
|
156 | 158 |
#define cli_mpool_dconf_init(a) cli_dconf_init() |
157 | 159 |
#endif |
158 | 160 |
void cli_dconf_print(struct cli_dconf *dconf); |
159 | 161 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,2785 @@ |
0 |
+/* |
|
1 |
+ * Copyright (C) 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
|
2 |
+ * |
|
3 |
+ * EGG is an archive format created by ESTsoft used by their ALZip |
|
4 |
+ * archiving software. |
|
5 |
+ * |
|
6 |
+ * This software is written from scratch based solely from ESTsoft's |
|
7 |
+ * file format documentation and from testing with EGG format archives. |
|
8 |
+ * ESTsoft's "unEGG" module was not used in the creation of this capability |
|
9 |
+ * in order to avoid to licensing restrictions on the ESTsoft "unEGG" module. |
|
10 |
+ * |
|
11 |
+ * EGG structure: |
|
12 |
+ * |
|
13 |
+ * |-----------------------------------------------------|------| |
|
14 |
+ * | EGG Header | 1 | |
|
15 |
+ * |-----------------------------------------------------|------| |
|
16 |
+ * | Extra Field 1: Â | Â | |
|
17 |
+ * | Split Compression |  | |
|
18 |
+ * | Solid Compression | 0~N | |
|
19 |
+ * | Global Encryption Header | | |
|
20 |
+ * |---------------------------------------|------|------|------| |
|
21 |
+ * | File Header | 1 |  |  | |
|
22 |
+ * |---------------------------------------|------| | | |
|
23 |
+ * | Extra Field 2: Â | Â | | | |
|
24 |
+ * | Filename Header  |  | 1~N | | |
|
25 |
+ * | Comment Header | 0~N | | | |
|
26 |
+ * | Windows File Information | | | | |
|
27 |
+ * | Posix File Information | | | 0~N | |
|
28 |
+ * | Encrypt Header  | | | | |
|
29 |
+ * |---------------------------------------|------|------| | |
|
30 |
+ * | Block Header | 1 | | | |
|
31 |
+ * |---------------------------------------|------| | | |
|
32 |
+ * | Extra Field 3: | 0~N | 0~NÂ | | |
|
33 |
+ * |---------------------------------------|------| | | |
|
34 |
+ * | Compressed Data | 1 Â | | | |
|
35 |
+ * |---------------------------------------|------|------|------| |
|
36 |
+ * | Extra Field 4: Â Â | | |
|
37 |
+ * | Archive Comment Header | 0~N | |
|
38 |
+ * |-----------------------------------------------------|------| |
|
39 |
+ * |
|
40 |
+ * Authors: Micah Snyder |
|
41 |
+ * |
|
42 |
+ * This program is free software; you can redistribute it and/or modify |
|
43 |
+ * it under the terms of the GNU General Public License version 2 as |
|
44 |
+ * published by the Free Software Foundation. |
|
45 |
+ * |
|
46 |
+ * This program is distributed in the hope that it will be useful, |
|
47 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
48 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
49 |
+ * GNU General Public License for more details. |
|
50 |
+ * |
|
51 |
+ * You should have received a copy of the GNU General Public License |
|
52 |
+ * along with this program; if not, write to the Free Software |
|
53 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
54 |
+ * MA 02110-1301, USA. |
|
55 |
+ */ |
|
56 |
+ |
|
57 |
+#if HAVE_CONFIG_H |
|
58 |
+#include "clamav-config.h" |
|
59 |
+#endif |
|
60 |
+ |
|
61 |
+#include <stdint.h> |
|
62 |
+#include <inttypes.h> |
|
63 |
+#include <wchar.h> |
|
64 |
+#include <locale.h> |
|
65 |
+#include <zlib.h> |
|
66 |
+ |
|
67 |
+#if HAVE_BZLIB_H |
|
68 |
+#include <bzlib.h> |
|
69 |
+#endif |
|
70 |
+ |
|
71 |
+#ifdef HAVE_ICONV |
|
72 |
+#include <iconv.h> |
|
73 |
+#endif |
|
74 |
+ |
|
75 |
+#include "lzma_iface.h" |
|
76 |
+ |
|
77 |
+#include "egg.h" |
|
78 |
+#include "msdoc.h" |
|
79 |
+ |
|
80 |
+#ifndef WCHAR |
|
81 |
+typedef uint16_t WCHAR; |
|
82 |
+#endif |
|
83 |
+ |
|
84 |
+/* |
|
85 |
+ * All EGG struct variables are little-endian. |
|
86 |
+ */ |
|
87 |
+ |
|
88 |
+#ifndef HAVE_ATTRIB_PACKED |
|
89 |
+#define __attribute__(x) |
|
90 |
+#endif |
|
91 |
+ |
|
92 |
+#ifdef HAVE_PRAGMA_PACK |
|
93 |
+#pragma pack(1) |
|
94 |
+#endif |
|
95 |
+ |
|
96 |
+#ifdef HAVE_PRAGMA_PACK_HPPA |
|
97 |
+#pragma pack 1 |
|
98 |
+#endif |
|
99 |
+ |
|
100 |
+/* |
|
101 |
+ * general defines |
|
102 |
+ */ |
|
103 |
+#define EOFARC 0x08E28222 /* Signals end of each header, or end of archive. */ |
|
104 |
+//#define EOFAR_ 0x2282E208 |
|
105 |
+/* |
|
106 |
+ * egg_header |
|
107 |
+ */ |
|
108 |
+#define EGG_HEADER_MAGIC 0x41474745 |
|
109 |
+#define EGG_HEADER_VERSION 0x0100 |
|
110 |
+ |
|
111 |
+typedef uint32_t magic32_t; |
|
112 |
+ |
|
113 |
+typedef struct __attribute__((packed)) { |
|
114 |
+ magic32_t magic; /* 0x41474745 */ |
|
115 |
+ uint16_t version; /* 0x0100 */ |
|
116 |
+ uint32_t header_id; /* Random number of the program (Cannot be 0) */ |
|
117 |
+ uint32_t reserved; /* 0x00000000 */ |
|
118 |
+} egg_header; |
|
119 |
+ |
|
120 |
+/* |
|
121 |
+ * file_header |
|
122 |
+ */ |
|
123 |
+#define FILE_HEADER_MAGIC 0x0A8590E3 |
|
124 |
+ |
|
125 |
+typedef struct __attribute__((packed)) { |
|
126 |
+ magic32_t magic; /* 0x0A8590E3 */ |
|
127 |
+ uint32_t file_id; /* Unique value for each header (Includes 0) */ |
|
128 |
+ uint64_t file_length; /* Total size of the file */ |
|
129 |
+} file_header; |
|
130 |
+ |
|
131 |
+/* |
|
132 |
+ * block_header |
|
133 |
+ * Note: split block of files exceeding 4G |
|
134 |
+ */ |
|
135 |
+#define BLOCK_HEADER_MAGIC 0x02B50C13 |
|
136 |
+#define BLOCK_HEADER_COMPRESS_ALGORITHM_STORE 0 |
|
137 |
+#define BLOCK_HEADER_COMPRESS_ALGORITHM_DEFLATE 1 |
|
138 |
+#define BLOCK_HEADER_COMPRESS_ALGORITHM_BZIP2 2 |
|
139 |
+#define BLOCK_HEADER_COMPRESS_ALGORITHM_AZO 3 |
|
140 |
+#define BLOCK_HEADER_COMPRESS_ALGORITHM_LZMA 4 |
|
141 |
+ |
|
142 |
+typedef struct __attribute__((packed)) { |
|
143 |
+ magic32_t magic; /* 0x02B50C13 */ |
|
144 |
+ uint8_t compress_algorithm; /* compress method algorithm number */ |
|
145 |
+ uint8_t compress_hint; /* compress method hint */ |
|
146 |
+ uint32_t uncompress_size; /* size of the block before compressed */ |
|
147 |
+ uint32_t compress_size; /* size of the block after compressed */ |
|
148 |
+ uint32_t crc32; /* CRC value of the block */ |
|
149 |
+} block_header; |
|
150 |
+ |
|
151 |
+/* |
|
152 |
+ * extra_field |
|
153 |
+ * |
|
154 |
+ * The extra_field is followed by a uint16_t or uint32_t depending on the bit_flag. |
|
155 |
+ * This describes the size of the following data. |
|
156 |
+ * In this way, an unexpected header can still be parsed. |
|
157 |
+ * Headers that make use of the extra_field: |
|
158 |
+ * - windows_file_information header |
|
159 |
+ * - posix_file_information header |
|
160 |
+ * - encrypt header |
|
161 |
+ * - filename header |
|
162 |
+ * - comment header |
|
163 |
+ * - split_compression header |
|
164 |
+ * - solid_compression header |
|
165 |
+ */ |
|
166 |
+#define EXTRA_FIELD_FLAGS_SIZE_IS_2BYTES 0x00 |
|
167 |
+#define EXTRA_FIELD_FLAGS_SIZE_IS_4BYTES 0x01 |
|
168 |
+ |
|
169 |
+typedef struct __attribute__((packed)) { |
|
170 |
+ magic32_t magic; |
|
171 |
+ uint8_t bit_flag; /* the size field following bit_flag depends if bit_flag bit 1: */ |
|
172 |
+} extra_field; /* 0 (uint16_t) */ |
|
173 |
+ /* 1 (uint32_t) */ |
|
174 |
+ |
|
175 |
+/* |
|
176 |
+ * Extra field: encrypt |
|
177 |
+ * |
|
178 |
+ * The encrypt_header is followed by: |
|
179 |
+ * 1) dummy data (size bytes) |
|
180 |
+ * |
|
181 |
+ * Note: Inserted in Extra Field 2 (optional, depending on KeyBase, AES, or LEA) |
|
182 |
+ */ |
|
183 |
+#define ENCRYPT_HEADER_MAGIC 0x08D1470F |
|
184 |
+#define ENCRYPT_HEADER_ENCRYPT_METHOD_XOR 0x00 |
|
185 |
+#define ENCRYPT_HEADER_ENCRYPT_METHOD_AES128 0x01 |
|
186 |
+#define ENCRYPT_HEADER_ENCRYPT_METHOD_AES256 0x02 |
|
187 |
+#define ENCRYPT_HEADER_ENCRYPT_METHOD_LEA128 0x10 |
|
188 |
+#define ENCRYPT_HEADER_ENCRYPT_METHOD_LEA256 0x20 |
|
189 |
+ |
|
190 |
+typedef struct __attribute__((packed)) { |
|
191 |
+ uint8_t aes_header[10]; /* AES/LEA Header */ |
|
192 |
+ uint8_t aes_footer[10]; /* AES/LEA Footer */ |
|
193 |
+} aes_lea_128; |
|
194 |
+ |
|
195 |
+typedef struct __attribute__((packed)) { |
|
196 |
+ uint8_t aes_header[18]; /* AES/LEA header */ |
|
197 |
+ uint8_t aes_footer[10]; /* AES/LEA footer */ |
|
198 |
+} aes_lea_256; |
|
199 |
+ |
|
200 |
+typedef struct __attribute__((packed)) { |
|
201 |
+ uint8_t verify_data[12]; /* KeyBase encryption verification data */ |
|
202 |
+ uint32_t crc32; /* KeyBase partial block CRC */ |
|
203 |
+} zip2_xor_keybase; |
|
204 |
+ |
|
205 |
+typedef struct __attribute__((packed)) { |
|
206 |
+ uint8_t encrypt_method; /* See above encrypt method #defines */ |
|
207 |
+} encrypt_header; |
|
208 |
+ |
|
209 |
+/* |
|
210 |
+ * Extra field: windows_file_information |
|
211 |
+ */ |
|
212 |
+#define WINDOWS_INFO_MAGIC 0x2C86950B |
|
213 |
+#define WINDOWS_INFO_ATTRIBUTE_READONLY 0x01 |
|
214 |
+#define WINDOWS_INFO_ATTRIBUTE_HIDDEN 0x02 |
|
215 |
+#define WINDOWS_INFO_ATTRIBUTE_SYSTEM_FILE 0x04 |
|
216 |
+#define WINDOWS_INFO_ATTRIBUTE_LINK_FILE 0x10 /* junction file */ |
|
217 |
+#define WINDOWS_INFO_ATTRIBUTE_DIRECTORY 0x40 |
|
218 |
+ |
|
219 |
+typedef struct __attribute__((packed)) { |
|
220 |
+ uint64_t last_modified_time; /* "100-Nanosecond Time" since the Windows Epoch (00:00:00 UTC, January 1, 1601) */ |
|
221 |
+ uint8_t attribute; /* See above attribute #defines */ |
|
222 |
+} windows_file_information; |
|
223 |
+ |
|
224 |
+/* |
|
225 |
+ * Extra field: posix_file_information |
|
226 |
+ */ |
|
227 |
+#define POSIX_INFO_MAGIC 0x1EE922E5 |
|
228 |
+#define POSIX_INFO_MODE_FILETYPE_BITMASK 0x0170000 /* bitmask for the file type bitfields */ |
|
229 |
+#define POSIX_INFO_MODE_SOCKET 0x0140000 /* socket */ |
|
230 |
+#define POSIX_INFO_MODE_SYM_LINK 0x0120000 /* symbolic link */ |
|
231 |
+#define POSIX_INFO_MODE_REG_FILE 0x0100000 /* regular file */ |
|
232 |
+#define POSIX_INFO_MODE_BLOCK_DEVICE 0x0060000 /* block device */ |
|
233 |
+#define POSIX_INFO_MODE_DIRECTORY 0x0040000 /* directory */ |
|
234 |
+#define POSIX_INFO_MODE_CHAR_DEVICE 0x0020000 /* character device */ |
|
235 |
+#define POSIX_INFO_MODE_FIFO 0x0010000 /* FIFO */ |
|
236 |
+#define POSIX_INFO_MODE_SET_UID_BIT 0x0004000 /* set UID bit */ |
|
237 |
+#define POSIX_INFO_MODE_SET_GROUPID_BIT 0x0002000 /* set-group-ID bit (see below) */ |
|
238 |
+#define POSIX_INFO_MODE_STICKY_BIT 0x0001000 /* sticky bit (see below) */ |
|
239 |
+#define POSIX_INFO_MODE_PERM_OWNER_MASK 0x00700 /* mask for file owner permissions */ |
|
240 |
+#define POSIX_INFO_MODE_PERM_OWNER_READ 0x00400 /* owner has read permission */ |
|
241 |
+#define POSIX_INFO_MODE_PERM_OWNER_WRITE 0x00200 /* owner has write permission */ |
|
242 |
+#define POSIX_INFO_MODE_PERM_OWNER_EXECUTE 0x00100 /* owner has execute permission */ |
|
243 |
+#define POSIX_INFO_MODE_PERM_GROUP_MASK 0x00070 /* mask for group permissions */ |
|
244 |
+#define POSIX_INFO_MODE_PERM_GROUP_READ 0x00040 /* group has read permission */ |
|
245 |
+#define POSIX_INFO_MODE_PERM_GROUP_WRITE 0x00020 /* group has write permission */ |
|
246 |
+#define POSIX_INFO_MODE_PERM_GROUP_EXECUTE 0x00010 /* group has execute permission */ |
|
247 |
+#define POSIX_INFO_MODE_PERM_OTHERS_MASK 0x00007 /* mask for permissions for others (not in group) */ |
|
248 |
+#define POSIX_INFO_MODE_PERM_OTHERS_READ 0x00004 /* others have read permission */ |
|
249 |
+#define POSIX_INFO_MODE_PERM_OTHERS_WRITE 0x00002 /* others have write permission */ |
|
250 |
+#define POSIX_INFO_MODE_PERM_OTHERS_EXECUTE 0x00001 /* others have execute permission*/ |
|
251 |
+ |
|
252 |
+typedef struct __attribute__((packed)) { |
|
253 |
+ uint32_t mode; /* see above mode #defines */ |
|
254 |
+ uint32_t uid; /* */ |
|
255 |
+ uint32_t gid; /* */ |
|
256 |
+ uint64_t last_modified_time; /* "Second Time" since the Unix Epoch (00:00:00 UTC, January 1, 1970) */ |
|
257 |
+} posix_file_information; |
|
258 |
+ |
|
259 |
+/* |
|
260 |
+ * Extra field: dummy_header |
|
261 |
+ * |
|
262 |
+ * The dummy header extra_info is followed by: |
|
263 |
+ * 1) dummy data (size bytes) |
|
264 |
+ * |
|
265 |
+ * Note: No need to consider if the size is too small to fit the dummy header because it can be distinguished by size calculation. |
|
266 |
+ */ |
|
267 |
+#define DUMMY_HEADER_MAGIC 0x07463307 |
|
268 |
+ |
|
269 |
+/* |
|
270 |
+ * Extra field: filename |
|
271 |
+ * |
|
272 |
+ * The filename extra_field is followed by: |
|
273 |
+ * 1) uint16_t locale IFF bit_flag is NOT unicode (UCS-2 LE) |
|
274 |
+ * 1) uint32_t parent_path_id IFF bit_flag is relative. |
|
275 |
+ * parent_path_id will be the ID of a file possessing the parent path. |
|
276 |
+ * 2) name buffer (size bytes minus above optional fields) |
|
277 |
+ */ |
|
278 |
+#define FILENAME_HEADER_MAGIC 0x0A8591AC |
|
279 |
+#define FILENAME_HEADER_FLAGS_ENCRYPT 0x04 |
|
280 |
+#define FILENAME_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8 0x08 |
|
281 |
+#define FILENAME_HEADER_FLAGS_RELATIVE_PATH_INSTEAD_OF_ABSOLUTE 0x10 |
|
282 |
+#define FILENAME_HEADER_LOCALE_USE_SYSTEM 0 |
|
283 |
+#define FILENAME_HEADER_LOCALE_JAPANESE 932 /* Shift-JIS */ |
|
284 |
+#define FILENAME_HEADER_LOCALE_KOREAN 949 |
|
285 |
+ |
|
286 |
+// typedef struct __attribute__((packed)) { |
|
287 |
+// (optional) uint16_t locale |
|
288 |
+// (optional) uint32_t parent_path_id |
|
289 |
+// uint8_t name_data [extra_field->size - sizeof(locale) - sizeof(parent_path_id)] |
|
290 |
+// } filename_header; |
|
291 |
+ |
|
292 |
+/* |
|
293 |
+ * Extra field: comment |
|
294 |
+ * |
|
295 |
+ * The comment extra_field is followed by: |
|
296 |
+ * 1) comment of size "N", exclude NULL character. |
|
297 |
+ */ |
|
298 |
+#define COMMENT_HEADER_MAGIC 0x04C63672 |
|
299 |
+#define COMMENT_HEADER_FLAGS_ENCRYPT 0x04 |
|
300 |
+#define COMMENT_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8 0x08 |
|
301 |
+ |
|
302 |
+/* |
|
303 |
+ * Extra field: split compression |
|
304 |
+ */ |
|
305 |
+#define SPLIT_COMPRESSION_MAGIC 0x24F5A262 |
|
306 |
+ |
|
307 |
+typedef struct __attribute__((packed)) { |
|
308 |
+ uint32_t prev_file_id; /* ID of previous file, 0 if first */ |
|
309 |
+ uint32_t next_file_id; /* ID of next file, 0 if last */ |
|
310 |
+} split_compression; |
|
311 |
+ |
|
312 |
+/* |
|
313 |
+ * Extra field: solid compression |
|
314 |
+ */ |
|
315 |
+#define SOLID_COMPRESSION_MAGIC 0x24E5A060 |
|
316 |
+ |
|
317 |
+#ifdef HAVE_PRAGMA_PACK |
|
318 |
+#pragma pack() |
|
319 |
+#endif |
|
320 |
+ |
|
321 |
+#ifdef HAVE_PRAGMA_PACK_HPPA |
|
322 |
+#pragma pack |
|
323 |
+#endif |
|
324 |
+ |
|
325 |
+typedef struct { |
|
326 |
+ char* name_utf8; |
|
327 |
+ uint32_t parent_path_id; |
|
328 |
+} egg_filename; |
|
329 |
+ |
|
330 |
+typedef struct { |
|
331 |
+ encrypt_header* header; /* Global Encryption Header */ |
|
332 |
+ union { |
|
333 |
+ aes_lea_128* al128; |
|
334 |
+ aes_lea_256* al256; |
|
335 |
+ zip2_xor_keybase* xor ; |
|
336 |
+ } encrypt_al; |
|
337 |
+} egg_encrypt; |
|
338 |
+ |
|
339 |
+typedef struct { |
|
340 |
+ block_header* blockHeader; |
|
341 |
+ char* compressedData; |
|
342 |
+} egg_block; |
|
343 |
+ |
|
344 |
+typedef struct { |
|
345 |
+ file_header* file; |
|
346 |
+ egg_filename filename; |
|
347 |
+ windows_file_information* windowsFileInformation; |
|
348 |
+ posix_file_information* posixFileInformation; |
|
349 |
+ egg_encrypt* encrypt; |
|
350 |
+ uint64_t nBlocks; |
|
351 |
+ egg_block** blocks; |
|
352 |
+ uint64_t nComments; |
|
353 |
+ char** comments; |
|
354 |
+} egg_file; |
|
355 |
+ |
|
356 |
+typedef struct { |
|
357 |
+ fmap_t* map; |
|
358 |
+ size_t offset; |
|
359 |
+ uint64_t fileExtractionIndex; |
|
360 |
+ int bSolid; /* Solid == all files compressed together. */ |
|
361 |
+ int bSplit; /* Split == multiple files make up single archive. */ |
|
362 |
+ split_compression* splitInfo; |
|
363 |
+ egg_encrypt* encrypt; |
|
364 |
+ uint64_t nFiles; |
|
365 |
+ egg_file** files; |
|
366 |
+ uint64_t nBlocks; |
|
367 |
+ egg_block** blocks; |
|
368 |
+ uint64_t nComments; |
|
369 |
+ char** comments; |
|
370 |
+} egg_handle; |
|
371 |
+ |
|
372 |
+#define EGG_VALIDATE_HANDLE(h) \ |
|
373 |
+ ((!handle || !handle->map || (handle->offset > handle->map->len)) ? EGG_ERR : EGG_OK) |
|
374 |
+ |
|
375 |
+const char* getEncryptName(uint8_t method) |
|
376 |
+{ |
|
377 |
+ const char* encryptName = NULL; |
|
378 |
+ |
|
379 |
+ switch (method) { |
|
380 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_XOR: |
|
381 |
+ encryptName = "XOR"; |
|
382 |
+ break; |
|
383 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_AES128: |
|
384 |
+ encryptName = "AES 128"; |
|
385 |
+ break; |
|
386 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA128: |
|
387 |
+ encryptName = "LEA 128"; |
|
388 |
+ break; |
|
389 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_AES256: |
|
390 |
+ encryptName = "AES 256"; |
|
391 |
+ break; |
|
392 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA256: |
|
393 |
+ encryptName = "LEA 256"; |
|
394 |
+ break; |
|
395 |
+ default: |
|
396 |
+ encryptName = "<unknown method>"; |
|
397 |
+ } |
|
398 |
+ |
|
399 |
+ return encryptName; |
|
400 |
+} |
|
401 |
+ |
|
402 |
+const char* getMagicHeaderName(uint32_t magic) |
|
403 |
+{ |
|
404 |
+ const char* magicName = NULL; |
|
405 |
+ |
|
406 |
+ switch (magic) { |
|
407 |
+ case EGG_HEADER_MAGIC: |
|
408 |
+ magicName = "EGG_HEADER_MAGIC"; |
|
409 |
+ break; |
|
410 |
+ case FILE_HEADER_MAGIC: |
|
411 |
+ magicName = "FILE_HEADER_MAGIC"; |
|
412 |
+ break; |
|
413 |
+ case BLOCK_HEADER_MAGIC: |
|
414 |
+ magicName = "BLOCK_HEADER_MAGIC"; |
|
415 |
+ break; |
|
416 |
+ case ENCRYPT_HEADER_MAGIC: |
|
417 |
+ magicName = "ENCRYPT_HEADER_MAGIC"; |
|
418 |
+ break; |
|
419 |
+ case WINDOWS_INFO_MAGIC: |
|
420 |
+ magicName = "WINDOWS_INFO_MAGIC"; |
|
421 |
+ break; |
|
422 |
+ case POSIX_INFO_MAGIC: |
|
423 |
+ magicName = "POSIX_INFO_MAGIC"; |
|
424 |
+ break; |
|
425 |
+ case DUMMY_HEADER_MAGIC: |
|
426 |
+ magicName = "DUMMY_HEADER_MAGIC"; |
|
427 |
+ break; |
|
428 |
+ case FILENAME_HEADER_MAGIC: |
|
429 |
+ magicName = "FILENAME_HEADER_MAGIC"; |
|
430 |
+ break; |
|
431 |
+ case COMMENT_HEADER_MAGIC: |
|
432 |
+ magicName = "COMMENT_HEADER_MAGIC"; |
|
433 |
+ break; |
|
434 |
+ case SPLIT_COMPRESSION_MAGIC: |
|
435 |
+ magicName = "SPLIT_COMPRESSION_MAGIC"; |
|
436 |
+ break; |
|
437 |
+ case SOLID_COMPRESSION_MAGIC: |
|
438 |
+ magicName = "SOLID_COMPRESSION_MAGIC"; |
|
439 |
+ break; |
|
440 |
+ default: |
|
441 |
+ magicName = "<unknown header magic>"; |
|
442 |
+ } |
|
443 |
+ |
|
444 |
+ return magicName; |
|
445 |
+} |
|
446 |
+ |
|
447 |
+/** |
|
448 |
+ * @brief Convert string to UTF-8, given Windows codepage. |
|
449 |
+ * |
|
450 |
+ * @param in string buffer |
|
451 |
+ * @param in_size length of string buffer in bytes |
|
452 |
+ * @param codepage Windows code page https://docs.microsoft.com/en-us/windows/desktop/Intl/code-page-identifiers) |
|
453 |
+ * @param [out] out pointer to receive malloc'ed utf-8 buffer. |
|
454 |
+ * @param [out] out_size pointer to receive size of utf-8 buffer, not including null terminating character. |
|
455 |
+ * @return cl_egg_error_t EGG_OK if success. EGG_BREAK if unable to because iconv is unavailable. Other error code if outright failure. |
|
456 |
+ */ |
|
457 |
+cl_egg_error_t cli_codepage_to_utf8(char* in, size_t in_size, uint16_t codepage, char** out, size_t* out_size) |
|
458 |
+{ |
|
459 |
+ cl_egg_error_t status = EGG_BREAK; |
|
460 |
+ |
|
461 |
+ char* out_utf8 = NULL; |
|
462 |
+ size_t out_utf8_size = 0; |
|
463 |
+ |
|
464 |
+#if defined(HAVE_ICONV) |
|
465 |
+ iconv_t conv = NULL; |
|
466 |
+#elif defined(WIN32) |
|
467 |
+ LPWSTR lpWideCharStr = NULL; |
|
468 |
+ int cchWideChar = 0; |
|
469 |
+#endif |
|
470 |
+ |
|
471 |
+ if (NULL == in || in_size == 0 || NULL == out || NULL == out_size) { |
|
472 |
+ cli_dbgmsg("egg_filename_to_utf8: Invalid args.\n"); |
|
473 |
+ status = EGG_ERR; |
|
474 |
+ goto done; |
|
475 |
+ } |
|
476 |
+ |
|
477 |
+ *out = NULL; |
|
478 |
+ *out_size = 0; |
|
479 |
+ |
|
480 |
+ switch (codepage) { |
|
481 |
+ case 20127: /* US-ASCII (7-bit) */ |
|
482 |
+ case 65001: { /* Unicode (UTF-8) */ |
|
483 |
+ char* track; |
|
484 |
+ int byte_count, sigbit_count; |
|
485 |
+ |
|
486 |
+ out_utf8_size = in_size; |
|
487 |
+ out_utf8 = cli_calloc(1, out_utf8_size + 1); |
|
488 |
+ if (NULL == out_utf8) { |
|
489 |
+ cli_errmsg("egg_filename_to_utf8: Failure allocating buffer for utf8 filename.\n"); |
|
490 |
+ status = EGG_EMEM; |
|
491 |
+ goto done; |
|
492 |
+ } |
|
493 |
+ memcpy(out_utf8, in, in_size); |
|
494 |
+ |
|
495 |
+ track = out_utf8 + in_size - 1; |
|
496 |
+ if ((codepage == 65001) && (*track & 0x80)) { |
|
497 |
+ /* |
|
498 |
+ * UTF-8 with a most significant bit. |
|
499 |
+ */ |
|
500 |
+ |
|
501 |
+ /* locate the start of the last character */ |
|
502 |
+ for (byte_count = 1; (track != out_utf8); track--, byte_count++) { |
|
503 |
+ if (((uint8_t)*track & 0xC0) != 0x80) |
|
504 |
+ break; |
|
505 |
+ } |
|
506 |
+ |
|
507 |
+ /* count number of set (1) significant bits */ |
|
508 |
+ for (sigbit_count = 0; sigbit_count < (int)(sizeof(uint8_t) * 8); sigbit_count++) { |
|
509 |
+ if (((uint8_t)*track & (0x80 >> sigbit_count)) == 0) |
|
510 |
+ break; |
|
511 |
+ } |
|
512 |
+ |
|
513 |
+ if (byte_count != sigbit_count) { |
|
514 |
+ cli_dbgmsg("egg_filename_to_utf8: cleaning out %d bytes from incomplete " |
|
515 |
+ "utf-8 character length %d\n", |
|
516 |
+ byte_count, sigbit_count); |
|
517 |
+ for (; byte_count > 0; byte_count--, track++) { |
|
518 |
+ *track = '\0'; |
|
519 |
+ } |
|
520 |
+ } |
|
521 |
+ } |
|
522 |
+ break; |
|
523 |
+ } |
|
524 |
+ default: { |
|
525 |
+ |
|
526 |
+#if defined(WIN32) && !defined(HAVE_ICONV) |
|
527 |
+ |
|
528 |
+ /* |
|
529 |
+ * Do conversion using native Win32 APIs. |
|
530 |
+ */ |
|
531 |
+ |
|
532 |
+ if (1200 != codepage) { /* not already UTF16-LE (Windows Unicode) */ |
|
533 |
+ /* |
|
534 |
+ * First, Convert from codepage -> UCS-2 LE with MultiByteToWideChar(codepage) |
|
535 |
+ */ |
|
536 |
+ cchWideChar = MultiByteToWideChar( |
|
537 |
+ codepage, |
|
538 |
+ 0, |
|
539 |
+ in, |
|
540 |
+ in_size, |
|
541 |
+ NULL, |
|
542 |
+ 0); |
|
543 |
+ if (0 == cchWideChar) { |
|
544 |
+ cli_dbgmsg("egg_filename_to_utf8: failed to determine string size needed for ansi to widechar conversion.\n"); |
|
545 |
+ status = EGG_ERR; |
|
546 |
+ goto done; |
|
547 |
+ } |
|
548 |
+ |
|
549 |
+ lpWideCharStr = malloc((cchWideChar + 1) * sizeof(WCHAR)); |
|
550 |
+ if (NULL == lpWideCharStr) { |
|
551 |
+ cli_dbgmsg("egg_filename_to_utf8: failed to allocate memory for wide char string.\n"); |
|
552 |
+ status = EGG_EMEM; |
|
553 |
+ goto done; |
|
554 |
+ } |
|
555 |
+ |
|
556 |
+ cchWideChar = MultiByteToWideChar( |
|
557 |
+ codepage, |
|
558 |
+ 0, |
|
559 |
+ in, |
|
560 |
+ in_size, |
|
561 |
+ lpWideCharStr, |
|
562 |
+ cchWideChar + 1); |
|
563 |
+ if (0 == cchWideChar) { |
|
564 |
+ cli_dbgmsg("egg_filename_to_utf8: failed to convert multibyte string to widechars.\n"); |
|
565 |
+ status = EGG_ERR; |
|
566 |
+ goto done; |
|
567 |
+ } |
|
568 |
+ |
|
569 |
+ in = (char*)lpWideCharStr; |
|
570 |
+ in_size = cchWideChar; |
|
571 |
+ } |
|
572 |
+ |
|
573 |
+ /* |
|
574 |
+ * Convert from UCS-2 LE -> UTF8 with WideCharToMultiByte(CP_UTF8) |
|
575 |
+ */ |
|
576 |
+ out_utf8_size = WideCharToMultiByte( |
|
577 |
+ CP_UTF8, |
|
578 |
+ 0, |
|
579 |
+ (LPCWCH)in, |
|
580 |
+ in_size / sizeof(WCHAR), |
|
581 |
+ NULL, |
|
582 |
+ 0, |
|
583 |
+ NULL, |
|
584 |
+ NULL); |
|
585 |
+ if (0 == out_utf8_size) { |
|
586 |
+ cli_dbgmsg("egg_filename_to_utf8: failed to determine string size needed for widechar conversion.\n"); |
|
587 |
+ status = EGG_ERR; |
|
588 |
+ goto done; |
|
589 |
+ } |
|
590 |
+ |
|
591 |
+ out_utf8 = malloc(out_utf8_size + 1); |
|
592 |
+ if (NULL == lpWideCharStr) { |
|
593 |
+ cli_dbgmsg("egg_filename_to_utf8: failed to allocate memory for wide char to utf-8 string.\n"); |
|
594 |
+ status = EGG_EMEM; |
|
595 |
+ goto done; |
|
596 |
+ } |
|
597 |
+ |
|
598 |
+ out_utf8_size = WideCharToMultiByte( |
|
599 |
+ CP_UTF8, |
|
600 |
+ 0, |
|
601 |
+ (LPCWCH)in, |
|
602 |
+ in_size / sizeof(WCHAR), |
|
603 |
+ out_utf8, |
|
604 |
+ out_utf8_size, |
|
605 |
+ NULL, |
|
606 |
+ NULL); |
|
607 |
+ if (0 == out_utf8_size) { |
|
608 |
+ cli_dbgmsg("egg_filename_to_utf8: failed to convert widechar string to utf-8.\n"); |
|
609 |
+ status = EGG_ERR; |
|
610 |
+ goto done; |
|
611 |
+ } |
|
612 |
+ |
|
613 |
+#elif defined(HAVE_ICONV) |
|
614 |
+ |
|
615 |
+ uint32_t attempt, i; |
|
616 |
+ size_t inbytesleft, outbytesleft; |
|
617 |
+ const char* encoding = NULL; |
|
618 |
+ |
|
619 |
+ for (i = 0; i < NUMCODEPAGES; ++i) { |
|
620 |
+ if (codepage == codepage_entries[i].codepage) { |
|
621 |
+ encoding = codepage_entries[i].encoding; |
|
622 |
+ } else if (codepage < codepage_entries[i].codepage) { |
|
623 |
+ break; /* fail-out early, requires sorted array */ |
|
624 |
+ } |
|
625 |
+ } |
|
626 |
+ |
|
627 |
+ for (attempt = 1; attempt <= 3; attempt++) { |
|
628 |
+ /* Charset to UTF-8 should never exceed in_size * 6; |
|
629 |
+ * We can shrink final buffer after the conversion, if needed. */ |
|
630 |
+ out_utf8_size = (in_size * 2) * attempt; |
|
631 |
+ |
|
632 |
+ inbytesleft = in_size; |
|
633 |
+ outbytesleft = out_utf8_size; |
|
634 |
+ |
|
635 |
+ out_utf8 = cli_calloc(1, out_utf8_size + 1); |
|
636 |
+ if (NULL == out_utf8) { |
|
637 |
+ cli_errmsg("egg_filename_to_utf8: Failure allocating buffer for utf8 data.\n"); |
|
638 |
+ status = EGG_EMEM; |
|
639 |
+ } |
|
640 |
+ |
|
641 |
+ conv = iconv_open("UTF-8//TRANSLIT", encoding); |
|
642 |
+ if (conv == (iconv_t)-1) { |
|
643 |
+ cli_warnmsg("egg_filename_to_utf8: Failed to open iconv.\n"); |
|
644 |
+ goto done; |
|
645 |
+ } |
|
646 |
+ |
|
647 |
+ if ((size_t)-1 == iconv(conv, &in, &inbytesleft, &out_utf8, &outbytesleft)) { |
|
648 |
+ switch (errno) { |
|
649 |
+ case E2BIG: |
|
650 |
+ cli_warnmsg("egg_filename_to_utf8: iconv error: There is not sufficient room at *outbuf.\n"); |
|
651 |
+ free(out_utf8); |
|
652 |
+ out_utf8 = NULL; |
|
653 |
+ continue; /* Try again, with a larger buffer. */ |
|
654 |
+ case EILSEQ: |
|
655 |
+ cli_warnmsg("egg_filename_to_utf8: iconv error: An invalid multibyte sequence has been encountered in the input.\n"); |
|
656 |
+ break; |
|
657 |
+ case EINVAL: |
|
658 |
+ cli_warnmsg("egg_filename_to_utf8: iconv error: An incomplete multibyte sequence has been encountered in the input.\n"); |
|
659 |
+ break; |
|
660 |
+ default: |
|
661 |
+ cli_warnmsg("egg_filename_to_utf8: iconv error: Unexpected error code %d.\n", errno); |
|
662 |
+ } |
|
663 |
+ status = EGG_ERR; |
|
664 |
+ goto done; |
|
665 |
+ } |
|
666 |
+ |
|
667 |
+ /* iconv succeeded, but probably didn't use the whole buffer. Free up the extra memory. */ |
|
668 |
+ out_utf8 = cli_realloc(out_utf8, out_utf8_size - outbytesleft + 1); |
|
669 |
+ if (NULL == out_utf8) { |
|
670 |
+ cli_errmsg("egg_filename_to_utf8: failure cli_realloc'ing converted filename.\n"); |
|
671 |
+ status = EGG_EMEM; |
|
672 |
+ goto done; |
|
673 |
+ } |
|
674 |
+ out_utf8_size = out_utf8_size - outbytesleft; |
|
675 |
+ } |
|
676 |
+ |
|
677 |
+#else |
|
678 |
+ |
|
679 |
+ /* |
|
680 |
+ * No way to do the conversion. |
|
681 |
+ */ |
|
682 |
+ goto done; |
|
683 |
+ |
|
684 |
+#endif |
|
685 |
+ } |
|
686 |
+ } |
|
687 |
+ |
|
688 |
+ *out = out_utf8; |
|
689 |
+ *out_size = out_utf8_size; |
|
690 |
+ |
|
691 |
+ status = EGG_OK; |
|
692 |
+ |
|
693 |
+done: |
|
694 |
+ |
|
695 |
+#if defined(WIN32) && !defined(HAVE_ICONV) |
|
696 |
+ if (NULL != lpWideCharStr) { |
|
697 |
+ free(lpWideCharStr); |
|
698 |
+ } |
|
699 |
+#endif |
|
700 |
+ |
|
701 |
+ if (EGG_OK != status) { |
|
702 |
+ if (NULL != out_utf8) { |
|
703 |
+ free(out_utf8); |
|
704 |
+ } |
|
705 |
+ } |
|
706 |
+ |
|
707 |
+ return status; |
|
708 |
+} |
|
709 |
+ |
|
710 |
+static void egg_free_encrypt(egg_encrypt* encryptInfo) |
|
711 |
+{ |
|
712 |
+ free(encryptInfo); |
|
713 |
+} |
|
714 |
+ |
|
715 |
+static cl_egg_error_t egg_parse_encrypt_header(const uint8_t* index, size_t size, egg_encrypt** encryptInfo) |
|
716 |
+{ |
|
717 |
+ /* |
|
718 |
+ * The EGG specification (last updated 2016) for the encrypt header is not accurate. |
|
719 |
+ * The following describes my findings of the actual format for the encrypt header. |
|
720 |
+ * |
|
721 |
+ * The significant discrepancy is that the Size includes the size of the header iself, not just the data following it. |
|
722 |
+ * No other extra_field header's size field includes the size of itself. |
|
723 |
+ * This must be accounted for by the caller of this function (see the "Fudge factor" comments where this function is used). |
|
724 |
+ * |
|
725 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
726 |
+ * | Magic(ENCRYP) | 4 | 0x08D1470F | |
|
727 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
728 |
+ * | Bit flag | 1 | 0 | |
|
729 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
730 |
+ * | Size | 2 | sizeof( Magic ) + sizeof( Bit flag ) + sizeof( Size ) + sizeof( Encrypt Method ) + sizeof( Method Header ) | |
|
731 |
+ * |---------------|---------|---|--------------------------------------------------------------------------------------------------------| |
|
732 |
+ * | Encrypt | 1 | 0 | KeyBase (XOR) | |
|
733 |
+ * | Method | |---|--------------------------------------------------------------------------------------------------------| |
|
734 |
+ * | | | 1 | AES128 | |
|
735 |
+ * | | |---|--------------------------------------------------------------------------------------------------------| |
|
736 |
+ * | | | 2 | AES256 | |
|
737 |
+ * | | |---|--------------------------------------------------------------------------------------------------------| |
|
738 |
+ * | | | 5 | LEA128 | |
|
739 |
+ * | | |---|--------------------------------------------------------------------------------------------------------| |
|
740 |
+ * | | | 6 | LEA256 | |
|
741 |
+ * |---------------|---------|---|--------------------------------------------------------------------------------------------------------| |
|
742 |
+ * |
|
743 |
+ * Depending on the Method (XOR / AES/LEA128 / AES/LEA256) The above will be be followed one of the following Method Headers: |
|
744 |
+ * |
|
745 |
+ * XOR (KeyBase): |
|
746 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
747 |
+ * | verify Data | 12 | Encryption Verification Data | |
|
748 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
749 |
+ * | CRC32 | 4 | Partial Block CRC | |
|
750 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
751 |
+ * |
|
752 |
+ * AES / LEA 128 |
|
753 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
754 |
+ * | Magic(ENCRYP) | 10 | AES/LEA Header | |
|
755 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
756 |
+ * | Magic(ENCRYP) | 10 | AES/LEA Footer | |
|
757 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
758 |
+ * |
|
759 |
+ * AES / LEA 256 |
|
760 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
761 |
+ * | Magic(ENCRYP) | 18 | AES/LEA Header | |
|
762 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
763 |
+ * | Magic(ENCRYP) | 10 | AES/LEA Footer | |
|
764 |
+ * |---------------|---------|------------------------------------------------------------------------------------------------------------| |
|
765 |
+ */ |
|
766 |
+ cl_egg_error_t status = EGG_ERR; |
|
767 |
+ egg_encrypt* encrypt = NULL; |
|
768 |
+ |
|
769 |
+ if (!index || 0 == size || !encryptInfo) { |
|
770 |
+ cli_errmsg("egg_parse_encrypt_header: Invalid args.\n"); |
|
771 |
+ goto done; |
|
772 |
+ } |
|
773 |
+ |
|
774 |
+ *encryptInfo = NULL; |
|
775 |
+ |
|
776 |
+ cli_dbgmsg("egg_parse_encrypt_header: Encrypted archive.\n"); |
|
777 |
+ cli_dbgmsg("egg_parse_encrypt_header: size of encrypt extra_field data: %zu\n", size); |
|
778 |
+ |
|
779 |
+ if (size < sizeof(encrypt_header)) { |
|
780 |
+ cli_warnmsg("egg_parse_encrypt_header: Encrypt header size too small (%zu < %zu)\n", size, sizeof(encrypt_header)); |
|
781 |
+ goto done; |
|
782 |
+ } |
|
783 |
+ |
|
784 |
+ encrypt = (egg_encrypt*)cli_calloc(1, sizeof(egg_encrypt)); |
|
785 |
+ if (NULL == encrypt) { |
|
786 |
+ cli_errmsg("egg_parse_encrypt_header: Failed to allocate memory for egg_encrypt.\n"); |
|
787 |
+ status = EGG_EMEM; |
|
788 |
+ goto done; |
|
789 |
+ } |
|
790 |
+ |
|
791 |
+ encrypt->header = (encrypt_header*)index; |
|
792 |
+ |
|
793 |
+ cli_dbgmsg("egg_parse_encrypt_header: encrypt_header->encrypt_method: %02x (%s)\n", encrypt->header->encrypt_method, getEncryptName(encrypt->header->encrypt_method)); |
|
794 |
+ |
|
795 |
+ index += sizeof(encrypt_header); |
|
796 |
+ size -= sizeof(encrypt_header); |
|
797 |
+ |
|
798 |
+ if (ENCRYPT_HEADER_ENCRYPT_METHOD_XOR == encrypt->header->encrypt_method) { |
|
799 |
+ if (size != sizeof(zip2_xor_keybase)) { |
|
800 |
+ cli_warnmsg("egg_parse_encrypt_header: Encrypt header size for XOR is different than expected (%zu != %zu)\n", size, sizeof(zip2_xor_keybase)); |
|
801 |
+ goto done; |
|
802 |
+ } |
|
803 |
+ |
|
804 |
+ encrypt->encrypt_al.xor = (zip2_xor_keybase*)index; |
|
805 |
+ |
|
806 |
+ cli_dbgmsg("egg_parse_encrypt_header: encrypt_header->crc32: %08x\n", le32_to_host(encrypt->encrypt_al.xor->crc32)); |
|
807 |
+ } else { |
|
808 |
+ /* |
|
809 |
+ * For AES/LEA, the additional information is found inside of embedded extra field. |
|
810 |
+ */ |
|
811 |
+ switch (encrypt->header->encrypt_method) { |
|
812 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_AES128: |
|
813 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA128: { |
|
814 |
+ if (size < sizeof(aes_lea_128)) { |
|
815 |
+ cli_warnmsg("egg_parse_encrypt_header: Encrypt header size for AES/LEA128 is different than expected (%zu != %zu)\n", size, sizeof(aes_lea_128)); |
|
816 |
+ goto done; |
|
817 |
+ } |
|
818 |
+ |
|
819 |
+ encrypt->encrypt_al.al128 = (aes_lea_128*)index; |
|
820 |
+ |
|
821 |
+ index += sizeof(aes_lea_128); |
|
822 |
+ size -= sizeof(aes_lea_128); |
|
823 |
+ break; |
|
824 |
+ } |
|
825 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_AES256: |
|
826 |
+ case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA256: { |
|
827 |
+ if (size < sizeof(aes_lea_256)) { |
|
828 |
+ cli_warnmsg("egg_parse_encrypt_header: Encrypt header size for AES/LEA256 is different than expected (%zu != %zu)\n", size, sizeof(aes_lea_256)); |
|
829 |
+ goto done; |
|
830 |
+ } |
|
831 |
+ |
|
832 |
+ encrypt->encrypt_al.al256 = (aes_lea_256*)index; |
|
833 |
+ |
|
834 |
+ index += sizeof(aes_lea_256); |
|
835 |
+ size -= sizeof(aes_lea_256); |
|
836 |
+ break; |
|
837 |
+ } |
|
838 |
+ default: { |
|
839 |
+ cli_warnmsg("egg_parse_encrypt_header: Unknown encrypt method: %d\n", encrypt->header->encrypt_method); |
|
840 |
+ goto done; |
|
841 |
+ } |
|
842 |
+ } |
|
843 |
+ } |
|
844 |
+ |
|
845 |
+ *encryptInfo = encrypt; |
|
846 |
+ status = EGG_OK; |
|
847 |
+ |
|
848 |
+done: |
|
849 |
+ |
|
850 |
+ if (EGG_OK != status) { |
|
851 |
+ egg_free_encrypt(encrypt); |
|
852 |
+ } |
|
853 |
+ |
|
854 |
+ return status; |
|
855 |
+} |
|
856 |
+ |
|
857 |
+static cl_egg_error_t egg_parse_comment_header(const uint8_t* index, size_t size, extra_field* extraField, char** commentInfo) |
|
858 |
+{ |
|
859 |
+ cl_egg_error_t status = EGG_ERR; |
|
860 |
+ |
|
861 |
+ char* comment = NULL; |
|
862 |
+ char* comment_utf8 = NULL; |
|
863 |
+ size_t comment_utf8_size = 0; |
|
864 |
+ |
|
865 |
+ if (!index || 0 == size || !extraField || !commentInfo) { |
|
866 |
+ cli_errmsg("egg_parse_comment_headers: Invalid args!\n"); |
|
867 |
+ return EGG_ERR; |
|
868 |
+ } |
|
869 |
+ |
|
870 |
+ *commentInfo = NULL; |
|
871 |
+ |
|
872 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_ENCRYPT) { |
|
873 |
+ /* |
|
874 |
+ * comment is encrypted, nothing to be done. |
|
875 |
+ */ |
|
876 |
+ *commentInfo = cli_strdup("<encrypted>"); |
|
877 |
+ status = EGG_ENCRYPTED; |
|
878 |
+ goto done; |
|
879 |
+ } |
|
880 |
+ |
|
881 |
+ /* |
|
882 |
+ * Store comment as UTF-8 string. |
|
883 |
+ */ |
|
884 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8) { |
|
885 |
+ /* |
|
886 |
+ * Unlike with filenames, the multibyte string codepage (or "locale") is not present in comment headers. |
|
887 |
+ * Try conversion with codepage 65001. |
|
888 |
+ */ |
|
889 |
+ if (EGG_OK != cli_codepage_to_utf8((char*)index, size, 65001, &comment_utf8, &comment_utf8_size)) { |
|
890 |
+ cli_dbgmsg("egg_parse_file_extra_field: failed to convert codepage \"0\" to UTF-8\n"); |
|
891 |
+ comment_utf8 = cli_genfname(NULL); |
|
892 |
+ } |
|
893 |
+ } else { |
|
894 |
+ /* Should already be UTF-8. Use as-is.. */ |
|
895 |
+ comment_utf8 = cli_strndup((char*)index, size); |
|
896 |
+ if (NULL == comment_utf8) { |
|
897 |
+ cli_dbgmsg("egg_parse_file_extra_field: failed to allocate comment buffer.\n"); |
|
898 |
+ status = EGG_EMEM; |
|
899 |
+ goto done; |
|
900 |
+ } |
|
901 |
+ } |
|
902 |
+ comment = comment_utf8; |
|
903 |
+ |
|
904 |
+ cli_dbgmsg("egg_parse_comment_header: comment: %s\n", comment); |
|
905 |
+ |
|
906 |
+ *commentInfo = comment; |
|
907 |
+ status = EGG_OK; |
|
908 |
+ |
|
909 |
+done: |
|
910 |
+ if (EGG_OK != status) { |
|
911 |
+ if (comment) { |
|
912 |
+ free(comment); |
|
913 |
+ } |
|
914 |
+ } |
|
915 |
+ |
|
916 |
+ return status; |
|
917 |
+} |
|
918 |
+ |
|
919 |
+static void egg_free_egg_block(egg_block* block) |
|
920 |
+{ |
|
921 |
+ free(block); |
|
922 |
+} |
|
923 |
+ |
|
924 |
+static cl_egg_error_t egg_parse_block_headers(egg_handle* handle, egg_block** block) |
|
925 |
+{ |
|
926 |
+ cl_egg_error_t status = EGG_ERR; |
|
927 |
+ |
|
928 |
+ egg_block* eggBlock = NULL; |
|
929 |
+ block_header* blockHeader = NULL; |
|
930 |
+ uint32_t magic = 0; |
|
931 |
+ const uint8_t* index = 0; |
|
932 |
+ |
|
933 |
+ if (!handle || !block) { |
|
934 |
+ cli_errmsg("egg_parse_block_headers: Invalid args!\n"); |
|
935 |
+ return EGG_ERR; |
|
936 |
+ } |
|
937 |
+ |
|
938 |
+ *block = NULL; |
|
939 |
+ |
|
940 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
941 |
+ cli_errmsg("egg_parse_block_headers: Invalid handle values!\n"); |
|
942 |
+ goto done; |
|
943 |
+ } |
|
944 |
+ |
|
945 |
+ /* |
|
946 |
+ * 1st: |
|
947 |
+ * Block headers must start with the block_header. |
|
948 |
+ */ |
|
949 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(block_header)); |
|
950 |
+ if (!index) { |
|
951 |
+ cli_dbgmsg("egg_parse_block_headers: File buffer too small to contain block header.\n"); |
|
952 |
+ goto done; |
|
953 |
+ } |
|
954 |
+ |
|
955 |
+ eggBlock = (egg_block*)cli_calloc(1, sizeof(egg_block)); |
|
956 |
+ if (NULL == eggBlock) { |
|
957 |
+ cli_errmsg("egg_parse_block_headers: Failed to allocate memory for egg_block.\n"); |
|
958 |
+ status = EGG_EMEM; |
|
959 |
+ goto done; |
|
960 |
+ } |
|
961 |
+ |
|
962 |
+ blockHeader = (block_header*)index; |
|
963 |
+ eggBlock->blockHeader = blockHeader; |
|
964 |
+ |
|
965 |
+ if (BLOCK_HEADER_MAGIC != le32_to_host(blockHeader->magic)) { |
|
966 |
+ cli_dbgmsg("egg_parse_block_headers: Invalid block header magic: %08x.\n", le32_to_host(blockHeader->magic)); |
|
967 |
+ goto done; |
|
968 |
+ } |
|
969 |
+ |
|
970 |
+ cli_dbgmsg("egg_parse_block_headers: block_header->magic: %08x (%s)\n", le32_to_host(blockHeader->magic), getMagicHeaderName(le32_to_host(blockHeader->magic))); |
|
971 |
+ cli_dbgmsg("egg_parse_block_headers: block_header->compress_algorithm: %08x\n", blockHeader->compress_algorithm); |
|
972 |
+ cli_dbgmsg("egg_parse_block_headers: block_header->compress_hint: %08x\n", blockHeader->compress_hint); |
|
973 |
+ cli_dbgmsg("egg_parse_block_headers: block_header->uncompress_size: %08x\n", le32_to_host(blockHeader->uncompress_size)); |
|
974 |
+ cli_dbgmsg("egg_parse_block_headers: block_header->compress_size: %08x\n", le32_to_host(blockHeader->compress_size)); |
|
975 |
+ cli_dbgmsg("egg_parse_block_headers: block_header->crc32: %08x\n", le32_to_host(blockHeader->crc32)); |
|
976 |
+ |
|
977 |
+ if (0 == le16_to_host(blockHeader->compress_size)) { |
|
978 |
+ cli_warnmsg("egg_parse_block_headers: Empty block!\n"); |
|
979 |
+ } |
|
980 |
+ |
|
981 |
+ handle->offset += sizeof(block_header); |
|
982 |
+ |
|
983 |
+ /* |
|
984 |
+ * 2nd: |
|
985 |
+ * After the block_header, the following extra field headers may be present: |
|
986 |
+ * a) EOFARC |
|
987 |
+ */ |
|
988 |
+ |
|
989 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(magic32_t)); |
|
990 |
+ if (!index) { |
|
991 |
+ cli_dbgmsg("egg_parse_block_headers: File buffer too small to contain end of archive magic bytes.\n"); |
|
992 |
+ goto done; |
|
993 |
+ } |
|
994 |
+ |
|
995 |
+ magic = le32_to_host(*((uint32_t*)index)); |
|
996 |
+ if (EOFARC != magic) { |
|
997 |
+ cli_dbgmsg("egg_parse_block_headers: EOFARC missing after block header. Found these bytes instead: %08x. (%s)\n", magic, getMagicHeaderName(magic)); |
|
998 |
+ goto done; |
|
999 |
+ } |
|
1000 |
+ cli_dbgmsg("egg_parse_block_headers: End of block header.\n"); |
|
1001 |
+ handle->offset += sizeof(magic32_t); |
|
1002 |
+ |
|
1003 |
+ /* |
|
1004 |
+ * Compressed data should follow the Block Header. |
|
1005 |
+ */ |
|
1006 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, blockHeader->compress_size); |
|
1007 |
+ if (!index) { |
|
1008 |
+ cli_dbgmsg("egg_parse_block_headers: File buffer too small to contain block compressed data.\n"); |
|
1009 |
+ goto done; |
|
1010 |
+ } |
|
1011 |
+ eggBlock->compressedData = (char*)index; |
|
1012 |
+ handle->offset += blockHeader->compress_size; |
|
1013 |
+ |
|
1014 |
+ *block = eggBlock; |
|
1015 |
+ status = EGG_OK; |
|
1016 |
+ |
|
1017 |
+done: |
|
1018 |
+ if (EGG_OK != status) { |
|
1019 |
+ if (eggBlock) { |
|
1020 |
+ egg_free_egg_block(eggBlock); |
|
1021 |
+ } |
|
1022 |
+ } |
|
1023 |
+ |
|
1024 |
+ return status; |
|
1025 |
+} |
|
1026 |
+ |
|
1027 |
+static void egg_free_egg_file(egg_file* file) |
|
1028 |
+{ |
|
1029 |
+ uint32_t i = 0; |
|
1030 |
+ |
|
1031 |
+ if (NULL != file->filename.name_utf8) { |
|
1032 |
+ free(file->filename.name_utf8); |
|
1033 |
+ file->filename.name_utf8 = NULL; |
|
1034 |
+ } |
|
1035 |
+ if (NULL != file->blocks) { |
|
1036 |
+ for (i = 0; i < file->nBlocks; i++) { |
|
1037 |
+ egg_free_egg_block(file->blocks[i]); |
|
1038 |
+ file->blocks[i] = NULL; |
|
1039 |
+ } |
|
1040 |
+ free(file->blocks); |
|
1041 |
+ file->blocks = NULL; |
|
1042 |
+ } |
|
1043 |
+ if (NULL != file->comments) { |
|
1044 |
+ for (i = 0; i < file->nComments; i++) { |
|
1045 |
+ free(file->comments[i]); |
|
1046 |
+ file->comments[i] = NULL; |
|
1047 |
+ } |
|
1048 |
+ free(file->comments); |
|
1049 |
+ file->comments = NULL; |
|
1050 |
+ } |
|
1051 |
+ |
|
1052 |
+ free(file); |
|
1053 |
+} |
|
1054 |
+ |
|
1055 |
+static cl_egg_error_t egg_parse_archive_extra_field(egg_handle* handle) |
|
1056 |
+{ |
|
1057 |
+ cl_egg_error_t status = EGG_ERR; |
|
1058 |
+ |
|
1059 |
+ const uint8_t* index = NULL; |
|
1060 |
+ extra_field* extraField = NULL; |
|
1061 |
+ uint32_t magic = 0; |
|
1062 |
+ uint32_t size = 0; |
|
1063 |
+ |
|
1064 |
+ if (!handle) { |
|
1065 |
+ cli_errmsg("egg_parse_archive_extra_field: Invalid args!\n"); |
|
1066 |
+ return EGG_ERR; |
|
1067 |
+ } |
|
1068 |
+ |
|
1069 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
1070 |
+ cli_errmsg("egg_parse_comment_headers: Invalid handle values!\n"); |
|
1071 |
+ goto done; |
|
1072 |
+ } |
|
1073 |
+ |
|
1074 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(extra_field)); |
|
1075 |
+ if (!index) { |
|
1076 |
+ cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain extra_field header.\n"); |
|
1077 |
+ goto done; |
|
1078 |
+ } |
|
1079 |
+ |
|
1080 |
+ extraField = (extra_field*)index; |
|
1081 |
+ |
|
1082 |
+ cli_dbgmsg("egg_parse_archive_extra_field: extra_field->magic: %08x (%s)\n", le32_to_host(extraField->magic), getMagicHeaderName(le32_to_host(extraField->magic))); |
|
1083 |
+ cli_dbgmsg("egg_parse_archive_extra_field: extra_field->bit_flag: %02x\n", extraField->bit_flag); |
|
1084 |
+ |
|
1085 |
+ handle->offset += sizeof(extra_field); |
|
1086 |
+ |
|
1087 |
+ if (extraField->bit_flag & EXTRA_FIELD_FLAGS_SIZE_IS_4BYTES) { |
|
1088 |
+ /* size is uint32_t */ |
|
1089 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint32_t)); |
|
1090 |
+ if (!index) { |
|
1091 |
+ cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain extra_field header.\n"); |
|
1092 |
+ goto done; |
|
1093 |
+ } |
|
1094 |
+ |
|
1095 |
+ size = le32_to_host(*(uint32_t*)index); |
|
1096 |
+ |
|
1097 |
+ handle->offset += sizeof(uint32_t); |
|
1098 |
+ } else { |
|
1099 |
+ /* size is uint16_t */ |
|
1100 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint16_t)); |
|
1101 |
+ if (!index) { |
|
1102 |
+ cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain extra_field header.\n"); |
|
1103 |
+ goto done; |
|
1104 |
+ } |
|
1105 |
+ |
|
1106 |
+ size = le16_to_host(*(uint16_t*)index); |
|
1107 |
+ |
|
1108 |
+ handle->offset += sizeof(uint16_t); |
|
1109 |
+ } |
|
1110 |
+ |
|
1111 |
+ cli_dbgmsg("egg_parse_archive_extra_field: extra_field->size: %u\n", size); |
|
1112 |
+ |
|
1113 |
+ magic = le32_to_host(extraField->magic); |
|
1114 |
+ |
|
1115 |
+ switch (magic) { |
|
1116 |
+ case SOLID_COMPRESSION_MAGIC: { |
|
1117 |
+ /* |
|
1118 |
+ * Solid archive is an archive packed with a special compression method, |
|
1119 |
+ * which treats several or all files within the archive as one continuous data stream. |
|
1120 |
+ */ |
|
1121 |
+ cli_dbgmsg("egg_parse_archive_extra_field: Solid archive. Several or all files within the archive treated as one continuous data stream.\n"); |
|
1122 |
+ |
|
1123 |
+ if (0 != handle->bSolid) { |
|
1124 |
+ cli_warnmsg("egg_parse_archive_extra_field: Encountered more than 1 Solid extra_field!\n"); |
|
1125 |
+ goto done; |
|
1126 |
+ } |
|
1127 |
+ handle->bSolid = 1; |
|
1128 |
+ break; |
|
1129 |
+ } |
|
1130 |
+ case SPLIT_COMPRESSION_MAGIC: { |
|
1131 |
+ /* |
|
1132 |
+ * Split archives are single archives split into multiple .egg volumes. |
|
1133 |
+ * |
|
1134 |
+ * It is the first file if previous file’s ID is 0, and is the last file |
|
1135 |
+ * if next file’s ID is 0. |
|
1136 |
+ * |
|
1137 |
+ * Header and Extra Field shouldn’t be cut when split compressing. |
|
1138 |
+ * Compressed Block Data can be saved cut. |
|
1139 |
+ * If header is excluded from the split size, insert Dummy Extra Field. |
|
1140 |
+ * |
|
1141 |
+ * If file compression ratio not applied when split compressing, modify |
|
1142 |
+ * Magic of the header into Dummy Header or Skip Header (0xFFFF0000) |
|
1143 |
+ * so it can be skipped. |
|
1144 |
+ */ |
|
1145 |
+ split_compression* split = NULL; |
|
1146 |
+ |
|
1147 |
+ if (0 != handle->bSplit) { |
|
1148 |
+ cli_warnmsg("egg_parse_archive_extra_field: Encountered more than 1 Split extra_field!\n"); |
|
1149 |
+ goto done; |
|
1150 |
+ } |
|
1151 |
+ handle->bSplit = 1; |
|
1152 |
+ cli_warnmsg("egg_parse_archive_extra_field: Split archive. Split archives are single archives split into multiple .egg volumes.\n"); |
|
1153 |
+ |
|
1154 |
+ if (sizeof(split_compression) != size) { |
|
1155 |
+ cli_dbgmsg("egg_parse_archive_extra_field: size in extra_field is different than size of split_compression (%zu != %u).\n", sizeof(split_compression), size); |
|
1156 |
+ } else { |
|
1157 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(split_compression)); |
|
1158 |
+ if (!index) { |
|
1159 |
+ cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain split compression header.\n"); |
|
1160 |
+ goto done; |
|
1161 |
+ } |
|
1162 |
+ |
|
1163 |
+ split = (split_compression*)index; |
|
1164 |
+ |
|
1165 |
+ handle->splitInfo = split; |
|
1166 |
+ |
|
1167 |
+ cli_dbgmsg("egg_parse_archive_extra_field: split_compression->prev_file_id: %08x\n", le32_to_host(split->prev_file_id)); |
|
1168 |
+ cli_dbgmsg("egg_parse_archive_extra_field: split_compression->next_file_id: %08x\n", le32_to_host(split->next_file_id)); |
|
1169 |
+ } |
|
1170 |
+ break; |
|
1171 |
+ } |
|
1172 |
+ case ENCRYPT_HEADER_MAGIC: { |
|
1173 |
+ /* |
|
1174 |
+ * EGG files may have a global encryption header. |
|
1175 |
+ * It is unclear if this means each file is encrypted, or that additional |
|
1176 |
+ * data beyond the file contents is encrypted. |
|
1177 |
+ */ |
|
1178 |
+ if (NULL != handle->encrypt) { |
|
1179 |
+ cli_warnmsg("egg_parse_archive_extra_field: Encountered more than 1 encrypt_header!\n"); |
|
1180 |
+ goto done; |
|
1181 |
+ } |
|
1182 |
+ |
|
1183 |
+ /* |
|
1184 |
+ * Fudge factor. |
|
1185 |
+ * The documentation is hazy about how the encrypt header works. |
|
1186 |
+ * From testing, it seems that for encrypted files, the size in the extra_field includes the size OF the extra field. |
|
1187 |
+ */ |
|
1188 |
+ size -= sizeof(extra_field) + sizeof(uint16_t); |
|
1189 |
+ |
|
1190 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, size); |
|
1191 |
+ if (!index) { |
|
1192 |
+ cli_errmsg("egg_parse_archive_extra_field: File buffer too small to contain encryption headers.\n"); |
|
1193 |
+ goto done; |
|
1194 |
+ } |
|
1195 |
+ |
|
1196 |
+ if (EGG_OK != egg_parse_encrypt_header(index, size, &handle->encrypt)) { |
|
1197 |
+ cli_errmsg("egg_parse_archive_extra_field: Failed to parse encryption headers.\n"); |
|
1198 |
+ goto done; |
|
1199 |
+ } |
|
1200 |
+ break; |
|
1201 |
+ } |
|
1202 |
+ default: { |
|
1203 |
+ cli_dbgmsg("egg_parse_archive_extra_field: unexpected header magic: %08x (%s)\n", magic, getMagicHeaderName(magic)); |
|
1204 |
+ } |
|
1205 |
+ } |
|
1206 |
+ |
|
1207 |
+ handle->offset += size; |
|
1208 |
+ |
|
1209 |
+ status = EGG_OK; |
|
1210 |
+ |
|
1211 |
+done: |
|
1212 |
+ |
|
1213 |
+ return status; |
|
1214 |
+} |
|
1215 |
+ |
|
1216 |
+static cl_egg_error_t egg_parse_file_extra_field(egg_handle* handle, egg_file* eggFile) |
|
1217 |
+{ |
|
1218 |
+ cl_egg_error_t status = EGG_ERR; |
|
1219 |
+ |
|
1220 |
+ const uint8_t* index = NULL; |
|
1221 |
+ extra_field* extraField = NULL; |
|
1222 |
+ uint32_t magic = 0; |
|
1223 |
+ uint32_t size = 0; |
|
1224 |
+ |
|
1225 |
+ if (!handle || !eggFile) { |
|
1226 |
+ cli_errmsg("egg_parse_file_extra_field: Invalid args!\n"); |
|
1227 |
+ return EGG_ERR; |
|
1228 |
+ } |
|
1229 |
+ |
|
1230 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
1231 |
+ cli_errmsg("egg_parse_file_extra_field: Invalid handle values!\n"); |
|
1232 |
+ goto done; |
|
1233 |
+ } |
|
1234 |
+ |
|
1235 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(extra_field)); |
|
1236 |
+ if (!index) { |
|
1237 |
+ cli_dbgmsg("egg_parse_file_extra_field: File buffer too small to contain extra_field header.\n"); |
|
1238 |
+ goto done; |
|
1239 |
+ } |
|
1240 |
+ |
|
1241 |
+ extraField = (extra_field*)index; |
|
1242 |
+ |
|
1243 |
+ cli_dbgmsg("egg_parse_file_extra_field: extra_field->magic: %08x (%s)\n", le32_to_host(extraField->magic), getMagicHeaderName(le32_to_host(extraField->magic))); |
|
1244 |
+ cli_dbgmsg("egg_parse_file_extra_field: extra_field->bit_flag: %02x\n", extraField->bit_flag); |
|
1245 |
+ |
|
1246 |
+ handle->offset += sizeof(extra_field); |
|
1247 |
+ |
|
1248 |
+ if (extraField->bit_flag & EXTRA_FIELD_FLAGS_SIZE_IS_4BYTES) { |
|
1249 |
+ /* size is uint32_t */ |
|
1250 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint32_t)); |
|
1251 |
+ if (!index) { |
|
1252 |
+ cli_dbgmsg("egg_parse_file_extra_field: File buffer too small to contain extra_field header.\n"); |
|
1253 |
+ goto done; |
|
1254 |
+ } |
|
1255 |
+ |
|
1256 |
+ size = le32_to_host(*(uint32_t*)index); |
|
1257 |
+ |
|
1258 |
+ handle->offset += sizeof(uint32_t); |
|
1259 |
+ } else { |
|
1260 |
+ /* size is uint16_t */ |
|
1261 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint16_t)); |
|
1262 |
+ if (!index) { |
|
1263 |
+ cli_dbgmsg("egg_parse_file_extra_field: File buffer too small to contain extra_field header.\n"); |
|
1264 |
+ goto done; |
|
1265 |
+ } |
|
1266 |
+ |
|
1267 |
+ size = le16_to_host(*(uint16_t*)index); |
|
1268 |
+ |
|
1269 |
+ handle->offset += sizeof(uint16_t); |
|
1270 |
+ } |
|
1271 |
+ |
|
1272 |
+ cli_dbgmsg("egg_parse_file_extra_field: extra_field->size: %u\n", size); |
|
1273 |
+ |
|
1274 |
+ magic = le32_to_host(extraField->magic); |
|
1275 |
+ |
|
1276 |
+ switch (magic) { |
|
1277 |
+ case FILENAME_HEADER_MAGIC: { |
|
1278 |
+ /* |
|
1279 |
+ * File Filename Header |
|
1280 |
+ */ |
|
1281 |
+ uint16_t codepage = 0; /* Windows code page https://docs.microsoft.com/en-us/windows/desktop/Intl/code-page-identifiers) */ |
|
1282 |
+ uint32_t name_size = 0; |
|
1283 |
+ uint32_t remaining_size = size; |
|
1284 |
+ |
|
1285 |
+ char* name_utf8 = NULL; |
|
1286 |
+ size_t name_utf8_size = 0; |
|
1287 |
+ |
|
1288 |
+ if (NULL != eggFile->filename.name_utf8) { |
|
1289 |
+ cli_warnmsg("egg_parse_file_extra_field: Encountered more than 1 filename_header!\n"); |
|
1290 |
+ goto done; |
|
1291 |
+ } |
|
1292 |
+ |
|
1293 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, size); |
|
1294 |
+ if (!index) { |
|
1295 |
+ cli_dbgmsg("egg_parse_file_extra_field: File buffer too small to contain name fields.\n"); |
|
1296 |
+ goto done; |
|
1297 |
+ } |
|
1298 |
+ |
|
1299 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_ENCRYPT) |
|
1300 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->bit_flag: encrypted\n"); |
|
1301 |
+ else |
|
1302 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->bit_flag: not encrypted\n"); |
|
1303 |
+ |
|
1304 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_RELATIVE_PATH_INSTEAD_OF_ABSOLUTE) |
|
1305 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->bit_flag: relative-path\n"); |
|
1306 |
+ else |
|
1307 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->bit_flag: absolute-path\n"); |
|
1308 |
+ |
|
1309 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8) |
|
1310 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->bit_flag: Windows Multibyte + codepage\n"); |
|
1311 |
+ else |
|
1312 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->bit_flag: UTF-8\n"); |
|
1313 |
+ |
|
1314 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8) { |
|
1315 |
+ /* Utf-8 - header will include locale */ |
|
1316 |
+ /* Check that the size is big enough */ |
|
1317 |
+ if (remaining_size < sizeof(uint16_t)) { |
|
1318 |
+ cli_dbgmsg("egg_parse_file_extra_field: size too small for locale information.\n"); |
|
1319 |
+ goto done; |
|
1320 |
+ } |
|
1321 |
+ codepage = *(uint16_t*)index; |
|
1322 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->codepage: %u\n", codepage); |
|
1323 |
+ index += sizeof(uint16_t); |
|
1324 |
+ handle->offset += sizeof(uint16_t); |
|
1325 |
+ remaining_size -= sizeof(uint16_t); |
|
1326 |
+ } |
|
1327 |
+ |
|
1328 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_RELATIVE_PATH_INSTEAD_OF_ABSOLUTE) { |
|
1329 |
+ /* header will include parent_path_id */ |
|
1330 |
+ /* Check that the size is big enough */ |
|
1331 |
+ if (remaining_size < sizeof(uint32_t)) { |
|
1332 |
+ cli_dbgmsg("egg_parse_file_extra_field: size too small for parent_path_id.\n"); |
|
1333 |
+ goto done; |
|
1334 |
+ } |
|
1335 |
+ eggFile->filename.parent_path_id = *(uint16_t*)index; |
|
1336 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->parent_path_id: %u\n", eggFile->filename.parent_path_id); |
|
1337 |
+ index += sizeof(uint32_t); |
|
1338 |
+ handle->offset += sizeof(uint32_t); |
|
1339 |
+ remaining_size -= sizeof(uint32_t); |
|
1340 |
+ } |
|
1341 |
+ |
|
1342 |
+ if (remaining_size == 0) { |
|
1343 |
+ cli_dbgmsg("egg_parse_file_extra_field: size too small for name string.\n"); |
|
1344 |
+ goto done; |
|
1345 |
+ } |
|
1346 |
+ name_size = remaining_size; |
|
1347 |
+ |
|
1348 |
+ /* |
|
1349 |
+ * Store name as UTF-8 string. |
|
1350 |
+ */ |
|
1351 |
+ if (extraField->bit_flag & FILENAME_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8) { |
|
1352 |
+ /* Convert ANSI codepage to UTF-8. EGG format explicitly supports: |
|
1353 |
+ * - 949 (Korean Unified Code) |
|
1354 |
+ * - 932 (Japanese Shift-JIS) */ |
|
1355 |
+ if (0 == codepage) { |
|
1356 |
+ if (EGG_OK != cli_codepage_to_utf8((char*)index, name_size, 65001, &name_utf8, &name_utf8_size)) { |
|
1357 |
+ cli_dbgmsg("egg_parse_file_extra_field: failed to convert codepage \"0\" to UTF-8\n"); |
|
1358 |
+ name_utf8 = cli_genfname(NULL); |
|
1359 |
+ } |
|
1360 |
+ } else { |
|
1361 |
+ if (EGG_OK != cli_codepage_to_utf8((char*)index, name_size, codepage, &name_utf8, &name_utf8_size)) { |
|
1362 |
+ cli_dbgmsg("egg_parse_file_extra_field: failed to convert codepage %u to UTF-8\n", codepage); |
|
1363 |
+ name_utf8 = cli_genfname(NULL); |
|
1364 |
+ } |
|
1365 |
+ } |
|
1366 |
+ } else { |
|
1367 |
+ /* Should already be UTF-8. Use as-is.. */ |
|
1368 |
+ name_utf8 = cli_strndup((char*)index, name_size); |
|
1369 |
+ if (NULL == name_utf8) { |
|
1370 |
+ cli_dbgmsg("egg_parse_file_extra_field: failed to allocate name buffer.\n"); |
|
1371 |
+ status = EGG_EMEM; |
|
1372 |
+ goto done; |
|
1373 |
+ } |
|
1374 |
+ } |
|
1375 |
+ eggFile->filename.name_utf8 = name_utf8; |
|
1376 |
+ |
|
1377 |
+ cli_dbgmsg("egg_parse_file_extra_field: filename_header->name: %s\n", eggFile->filename.name_utf8); |
|
1378 |
+ |
|
1379 |
+ break; |
|
1380 |
+ } |
|
1381 |
+ case COMMENT_HEADER_MAGIC: { |
|
1382 |
+ /* |
|
1383 |
+ * File Comment Header |
|
1384 |
+ */ |
|
1385 |
+ cl_egg_error_t retval = EGG_ERR; |
|
1386 |
+ char* comment = NULL; |
|
1387 |
+ |
|
1388 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, size); |
|
1389 |
+ if (!index) { |
|
1390 |
+ cli_dbgmsg("egg_parse_file_extra_field: File buffer too small to contain comment fields.\n"); |
|
1391 |
+ goto done; |
|
1392 |
+ } |
|
1393 |
+ |
|
1394 |
+ if (EGG_OK != (retval = egg_parse_comment_header(index, size, extraField, &comment))) { |
|
1395 |
+ cli_dbgmsg("egg_parse_file_extra_field: Issue parsing comment header. Error code: %u\n", retval); |
|
1396 |
+ break; |
|
1397 |
+ } else { |
|
1398 |
+ /* |
|
1399 |
+ * Success? |
|
1400 |
+ */ |
|
1401 |
+ if (comment == NULL) { |
|
1402 |
+ /* Uh... no. */ |
|
1403 |
+ cli_errmsg("egg_parse_file_extra_field: Logic error! Succesfully parsed comment header," |
|
1404 |
+ " but did not return egg_comment information!\n"); |
|
1405 |
+ goto done; |
|
1406 |
+ } else { |
|
1407 |
+ /* |
|
1408 |
+ * Comment found. Add comment to our list. |
|
1409 |
+ */ |
|
1410 |
+ eggFile->comments = (char**)cli_realloc( |
|
1411 |
+ (void*)eggFile->comments, |
|
1412 |
+ sizeof(char**) * (eggFile->nComments + 1)); |
|
1413 |
+ if (NULL == eggFile->comments) { |
|
1414 |
+ status = EGG_EMEM; |
|
1415 |
+ goto done; |
|
1416 |
+ } |
|
1417 |
+ eggFile->comments[eggFile->nComments] = comment; |
|
1418 |
+ eggFile->nComments++; |
|
1419 |
+ } |
|
1420 |
+ } |
|
1421 |
+ break; |
|
1422 |
+ } |
|
1423 |
+ case ENCRYPT_HEADER_MAGIC: { |
|
1424 |
+ /* |
|
1425 |
+ * File Encryption Header. |
|
1426 |
+ */ |
|
1427 |
+ if (NULL != eggFile->encrypt) { |
|
1428 |
+ cli_warnmsg("egg_parse_file_extra_field: Encountered more than 1 encrypt_header!\n"); |
|
1429 |
+ goto done; |
|
1430 |
+ } |
|
1431 |
+ |
|
1432 |
+ /* |
|
1433 |
+ * Fudge factor. |
|
1434 |
+ * The documentation is hazy about how the encrypt header works. |
|
1435 |
+ * From testing, it seems that for encrypted files, the size in the extra_field includes the size OF the extra field. |
|
1436 |
+ */ |
|
1437 |
+ size -= sizeof(extra_field) + sizeof(uint16_t); |
|
1438 |
+ |
|
1439 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, size); |
|
1440 |
+ if (!index) { |
|
1441 |
+ cli_errmsg("egg_parse_file_extra_field: File buffer too small to contain encryption fields.\n"); |
|
1442 |
+ goto done; |
|
1443 |
+ } |
|
1444 |
+ |
|
1445 |
+ if (EGG_OK != egg_parse_encrypt_header(index, size, &eggFile->encrypt)) { |
|
1446 |
+ cli_errmsg("egg_parse_file_extra_field: Failed to parse encrypt_header.\n"); |
|
1447 |
+ goto done; |
|
1448 |
+ } |
|
1449 |
+ break; |
|
1450 |
+ } |
|
1451 |
+ case WINDOWS_INFO_MAGIC: { |
|
1452 |
+ windows_file_information* windowsFileInformation = NULL; |
|
1453 |
+ |
|
1454 |
+ if (NULL != eggFile->windowsFileInformation) { |
|
1455 |
+ cli_warnmsg("egg_parse_file_extra_field: Encountered more than 1 windows_file_information!\n"); |
|
1456 |
+ goto done; |
|
1457 |
+ } |
|
1458 |
+ |
|
1459 |
+ if (sizeof(windows_file_information) != size) { |
|
1460 |
+ cli_warnmsg("egg_parse_file_extra_field: Invalid size of windows_file_information!\n"); |
|
1461 |
+ } |
|
1462 |
+ |
|
1463 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(windows_file_information)); |
|
1464 |
+ if (!index) { |
|
1465 |
+ cli_dbgmsg("egg_parse_file_extra_field: File buffer too small to contain windows info.\n"); |
|
1466 |
+ goto done; |
|
1467 |
+ } |
|
1468 |
+ |
|
1469 |
+ windowsFileInformation = (windows_file_information*)index; |
|
1470 |
+ eggFile->windowsFileInformation = windowsFileInformation; |
|
1471 |
+ |
|
1472 |
+ cli_dbgmsg("egg_parse_file_extra_field: windows_file_information->last_modified_time: %016llx\n", le64_to_host(windowsFileInformation->last_modified_time)); |
|
1473 |
+ cli_dbgmsg("egg_parse_file_extra_field: windows_file_information->attribute: %08x\n", windowsFileInformation->attribute); |
|
1474 |
+ break; |
|
1475 |
+ } |
|
1476 |
+ case POSIX_INFO_MAGIC: { |
|
1477 |
+ posix_file_information* posixFileInformation = NULL; |
|
1478 |
+ |
|
1479 |
+ if (NULL != eggFile->posixFileInformation) { |
|
1480 |
+ cli_warnmsg("egg_parse_file_extra_field: Encountered more than 1 posix_file_information!\n"); |
|
1481 |
+ goto done; |
|
1482 |
+ } |
|
1483 |
+ |
|
1484 |
+ if (sizeof(posix_file_information) != size) { |
|
1485 |
+ cli_warnmsg("egg_parse_file_extra_field: Invalid size of posix_file_information!\n"); |
|
1486 |
+ } |
|
1487 |
+ |
|
1488 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(posix_file_information)); |
|
1489 |
+ if (!index) { |
|
1490 |
+ cli_dbgmsg("egg_parse_file_extra_field: File buffer too small to contain posix info.\n"); |
|
1491 |
+ goto done; |
|
1492 |
+ } |
|
1493 |
+ |
|
1494 |
+ posixFileInformation = (posix_file_information*)index; |
|
1495 |
+ eggFile->posixFileInformation = posixFileInformation; |
|
1496 |
+ |
|
1497 |
+ cli_dbgmsg("egg_parse_file_extra_field: posix_file_information->mode: %08x ", le32_to_host(posixFileInformation->mode)); |
|
1498 |
+ if (UNLIKELY(cli_debug_flag)) { |
|
1499 |
+ /* File type flags */ |
|
1500 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_REG_FILE) { |
|
1501 |
+ printf("-"); |
|
1502 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_DIRECTORY) { |
|
1503 |
+ printf("d"); |
|
1504 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_CHAR_DEVICE) { |
|
1505 |
+ printf("c"); |
|
1506 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_BLOCK_DEVICE) { |
|
1507 |
+ printf("s"); |
|
1508 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_SOCKET) { |
|
1509 |
+ printf("s"); |
|
1510 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_FIFO) { |
|
1511 |
+ printf("p"); |
|
1512 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_SYM_LINK) { |
|
1513 |
+ printf("l"); |
|
1514 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_SOCKET) { |
|
1515 |
+ printf("s"); |
|
1516 |
+ } |
|
1517 |
+ /* Owner/Group/Other permissions */ |
|
1518 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_OWNER_READ) { |
|
1519 |
+ printf("r"); |
|
1520 |
+ } else { |
|
1521 |
+ printf("-"); |
|
1522 |
+ } |
|
1523 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_OWNER_WRITE) { |
|
1524 |
+ printf("w"); |
|
1525 |
+ } else { |
|
1526 |
+ printf("-"); |
|
1527 |
+ } |
|
1528 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_SET_UID_BIT) { |
|
1529 |
+ printf("s"); |
|
1530 |
+ } else if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_OWNER_EXECUTE) { |
|
1531 |
+ printf("x"); |
|
1532 |
+ } else { |
|
1533 |
+ printf("-"); |
|
1534 |
+ } |
|
1535 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_GROUP_READ) { |
|
1536 |
+ printf("r"); |
|
1537 |
+ } else { |
|
1538 |
+ printf("-"); |
|
1539 |
+ } |
|
1540 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_GROUP_WRITE) { |
|
1541 |
+ printf("w"); |
|
1542 |
+ } else { |
|
1543 |
+ printf("-"); |
|
1544 |
+ } |
|
1545 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_SET_UID_BIT) { |
|
1546 |
+ printf("s"); |
|
1547 |
+ } |
|
1548 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_SET_GROUPID_BIT) { |
|
1549 |
+ printf("s"); |
|
1550 |
+ } |
|
1551 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_GROUP_EXECUTE) { |
|
1552 |
+ printf("x"); |
|
1553 |
+ } else { |
|
1554 |
+ printf("-"); |
|
1555 |
+ } |
|
1556 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_OTHERS_READ) { |
|
1557 |
+ printf("r"); |
|
1558 |
+ } else { |
|
1559 |
+ printf("-"); |
|
1560 |
+ } |
|
1561 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_OTHERS_WRITE) { |
|
1562 |
+ printf("w"); |
|
1563 |
+ } else { |
|
1564 |
+ printf("-"); |
|
1565 |
+ } |
|
1566 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_PERM_OTHERS_EXECUTE) { |
|
1567 |
+ printf("x"); |
|
1568 |
+ } else { |
|
1569 |
+ printf("-"); |
|
1570 |
+ } |
|
1571 |
+ /* Sticky Bit */ |
|
1572 |
+ if (posixFileInformation->mode & POSIX_INFO_MODE_STICKY_BIT) |
|
1573 |
+ printf("t"); |
|
1574 |
+ printf("\n"); |
|
1575 |
+ } |
|
1576 |
+ |
|
1577 |
+ cli_dbgmsg("egg_parse_file_extra_field: posix_file_information->uid: %08x\n", le32_to_host(posixFileInformation->uid)); |
|
1578 |
+ cli_dbgmsg("egg_parse_file_extra_field: posix_file_information->gid: %08x\n", le32_to_host(posixFileInformation->gid)); |
|
1579 |
+ cli_dbgmsg("egg_parse_file_extra_field: posix_file_information->last_modified_time: %016llx\n", le64_to_host(posixFileInformation->last_modified_time)); |
|
1580 |
+ break; |
|
1581 |
+ } |
|
1582 |
+ case FILE_HEADER_MAGIC: { |
|
1583 |
+ if (handle->bSolid) { |
|
1584 |
+ cli_dbgmsg("egg_parse_file_extra_field: Solid archive - on to next file header.\n"); |
|
1585 |
+ } else { |
|
1586 |
+ cli_warnmsg("egg_parse_file_extra_field: Missing EOFARC in non-solid/standard archive.\n"); |
|
1587 |
+ } |
|
1588 |
+ break; |
|
1589 |
+ } |
|
1590 |
+ default: { |
|
1591 |
+ cli_dbgmsg("egg_parse_file_extra_field: unexpected header magic: %08x (%s)\n", magic, getMagicHeaderName(magic)); |
|
1592 |
+ } |
|
1593 |
+ } |
|
1594 |
+ |
|
1595 |
+ handle->offset += size; |
|
1596 |
+ |
|
1597 |
+ status = EGG_OK; |
|
1598 |
+ |
|
1599 |
+done: |
|
1600 |
+ |
|
1601 |
+ return status; |
|
1602 |
+} |
|
1603 |
+ |
|
1604 |
+static cl_egg_error_t egg_parse_file_headers(egg_handle* handle, egg_file** file) |
|
1605 |
+{ |
|
1606 |
+ cl_egg_error_t status = EGG_ERR; |
|
1607 |
+ cl_egg_error_t retval; |
|
1608 |
+ |
|
1609 |
+ egg_file* eggFile = NULL; |
|
1610 |
+ file_header* fileHeader = NULL; |
|
1611 |
+ uint32_t magic = 0; |
|
1612 |
+ const uint8_t* index = 0; |
|
1613 |
+ |
|
1614 |
+ if (!handle || !file) { |
|
1615 |
+ cli_errmsg("egg_parse_file_headers: Invalid args!\n"); |
|
1616 |
+ return EGG_ERR; |
|
1617 |
+ } |
|
1618 |
+ |
|
1619 |
+ *file = NULL; |
|
1620 |
+ |
|
1621 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
1622 |
+ cli_errmsg("egg_parse_file_headers: Invalid handle values!\n"); |
|
1623 |
+ goto done; |
|
1624 |
+ } |
|
1625 |
+ |
|
1626 |
+ /* |
|
1627 |
+ * 1st: |
|
1628 |
+ * File headers must start with the file_header. |
|
1629 |
+ */ |
|
1630 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(file_header)); |
|
1631 |
+ if (!index) { |
|
1632 |
+ cli_dbgmsg("egg_parse_file_headers: File buffer too small to contain file header.\n"); |
|
1633 |
+ goto done; |
|
1634 |
+ } |
|
1635 |
+ |
|
1636 |
+ eggFile = (egg_file*)cli_calloc(1, sizeof(egg_file)); |
|
1637 |
+ if (NULL == eggFile) { |
|
1638 |
+ cli_errmsg("egg_parse_file_headers: Failed to allocate memory for egg_file.\n"); |
|
1639 |
+ status = EGG_EMEM; |
|
1640 |
+ goto done; |
|
1641 |
+ } |
|
1642 |
+ |
|
1643 |
+ fileHeader = (file_header*)index; |
|
1644 |
+ eggFile->file = fileHeader; |
|
1645 |
+ |
|
1646 |
+ if (FILE_HEADER_MAGIC != le32_to_host(fileHeader->magic)) { |
|
1647 |
+ cli_dbgmsg("egg_parse_file_headers: Invalid file header magic: %08x (%s).\n", le32_to_host(fileHeader->magic), getMagicHeaderName(le32_to_host(fileHeader->magic))); |
|
1648 |
+ goto done; |
|
1649 |
+ } |
|
1650 |
+ |
|
1651 |
+ cli_dbgmsg("egg_parse_file_headers: file_header->magic: %08x (%s)\n", le32_to_host(fileHeader->magic), getMagicHeaderName(le32_to_host(fileHeader->magic))); |
|
1652 |
+ cli_dbgmsg("egg_parse_file_headers: file_header->file_id: %08x\n", le32_to_host(fileHeader->file_id)); |
|
1653 |
+ cli_dbgmsg("egg_parse_file_headers: file_header->file_length: %016llx (%llu)\n", |
|
1654 |
+ le64_to_host(fileHeader->file_length), |
|
1655 |
+ le64_to_host(fileHeader->file_length)); |
|
1656 |
+ |
|
1657 |
+ if (0 == le16_to_host(fileHeader->file_length)) { |
|
1658 |
+ cli_warnmsg("egg_parse_file_headers: Empty file!\n"); |
|
1659 |
+ } |
|
1660 |
+ |
|
1661 |
+ handle->offset += sizeof(file_header); |
|
1662 |
+ |
|
1663 |
+ /* |
|
1664 |
+ * 2nd: |
|
1665 |
+ * After the file_header, the following extra field headers may be present: |
|
1666 |
+ * a) filename_header |
|
1667 |
+ * b) comment_header |
|
1668 |
+ * c) windows_file_information |
|
1669 |
+ * d) posix_file_information |
|
1670 |
+ * e) encrypt_header |
|
1671 |
+ * f) EOFARC |
|
1672 |
+ */ |
|
1673 |
+ |
|
1674 |
+ while (handle->map->len > handle->offset) { |
|
1675 |
+ |
|
1676 |
+ /* Get the next magic32_t */ |
|
1677 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(magic32_t)); |
|
1678 |
+ if (!index) { |
|
1679 |
+ cli_dbgmsg("egg_parse_file_headers: File buffer too small to contain end of archive magic bytes.\n"); |
|
1680 |
+ goto done; |
|
1681 |
+ } |
|
1682 |
+ |
|
1683 |
+ magic = le32_to_host(*((uint32_t*)index)); |
|
1684 |
+ |
|
1685 |
+ if (EOFARC == magic) { |
|
1686 |
+ /* |
|
1687 |
+ * File headers should conclude with EOFARC magic bytes. |
|
1688 |
+ */ |
|
1689 |
+ handle->offset += sizeof(magic32_t); |
|
1690 |
+ |
|
1691 |
+ cli_dbgmsg("egg_parse_file_headers: End of archive headers.\n"); |
|
1692 |
+ break; /* Break out of the loop */ |
|
1693 |
+ } else { |
|
1694 |
+ /* |
|
1695 |
+ * Parse extra fields. |
|
1696 |
+ */ |
|
1697 |
+ retval = egg_parse_file_extra_field(handle, eggFile); |
|
1698 |
+ if (EGG_OK != retval) { |
|
1699 |
+ cli_dbgmsg("egg_parse_file_headers: Failed to parse archive header, magic: %08x (%s)\n", magic, getMagicHeaderName(magic)); |
|
1700 |
+ } |
|
1701 |
+ } |
|
1702 |
+ } |
|
1703 |
+ |
|
1704 |
+ *file = eggFile; |
|
1705 |
+ status = EGG_OK; |
|
1706 |
+ |
|
1707 |
+done: |
|
1708 |
+ if (EGG_OK != status) { |
|
1709 |
+ if (eggFile) { |
|
1710 |
+ egg_free_egg_file(eggFile); |
|
1711 |
+ } |
|
1712 |
+ } |
|
1713 |
+ |
|
1714 |
+ return status; |
|
1715 |
+} |
|
1716 |
+ |
|
1717 |
+static void egg_free_egg_handle(egg_handle* handle) |
|
1718 |
+{ |
|
1719 |
+ uint32_t i = 0; |
|
1720 |
+ |
|
1721 |
+ if (NULL != handle->encrypt) { |
|
1722 |
+ egg_free_encrypt(handle->encrypt); |
|
1723 |
+ handle->encrypt = NULL; |
|
1724 |
+ } |
|
1725 |
+ if (NULL != handle->files) { |
|
1726 |
+ for (i = 0; i < handle->nFiles; i++) { |
|
1727 |
+ egg_free_egg_file(handle->files[i]); |
|
1728 |
+ handle->files[i] = NULL; |
|
1729 |
+ } |
|
1730 |
+ free(handle->files); |
|
1731 |
+ handle->files = NULL; |
|
1732 |
+ } |
|
1733 |
+ if (NULL != handle->blocks) { |
|
1734 |
+ for (i = 0; i < handle->nBlocks; i++) { |
|
1735 |
+ egg_free_egg_block(handle->blocks[i]); |
|
1736 |
+ handle->blocks[i] = NULL; |
|
1737 |
+ } |
|
1738 |
+ free(handle->blocks); |
|
1739 |
+ handle->blocks = NULL; |
|
1740 |
+ } |
|
1741 |
+ if (NULL != handle->comments) { |
|
1742 |
+ for (i = 0; i < handle->nComments; i++) { |
|
1743 |
+ free(handle->comments[i]); |
|
1744 |
+ handle->comments[i] = NULL; |
|
1745 |
+ } |
|
1746 |
+ free(handle->comments); |
|
1747 |
+ handle->comments = NULL; |
|
1748 |
+ } |
|
1749 |
+} |
|
1750 |
+ |
|
1751 |
+static cl_egg_error_t egg_parse_archive_headers(egg_handle* handle) |
|
1752 |
+{ |
|
1753 |
+ cl_egg_error_t status = EGG_ERR; |
|
1754 |
+ cl_egg_error_t retval; |
|
1755 |
+ |
|
1756 |
+ egg_header* eggHeader = NULL; |
|
1757 |
+ uint32_t magic = 0; |
|
1758 |
+ const uint8_t* index = 0; |
|
1759 |
+ |
|
1760 |
+ if (!handle) { |
|
1761 |
+ cli_errmsg("egg_parse_archive_headers: Invalid args!\n"); |
|
1762 |
+ return EGG_ERR; |
|
1763 |
+ } |
|
1764 |
+ |
|
1765 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
1766 |
+ cli_errmsg("egg_parse_archive_headers: Invalid handle values!\n"); |
|
1767 |
+ goto done; |
|
1768 |
+ } |
|
1769 |
+ |
|
1770 |
+ /* |
|
1771 |
+ * 1st: |
|
1772 |
+ * Archive headers begins with the egg_header. |
|
1773 |
+ */ |
|
1774 |
+ |
|
1775 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(egg_header)); |
|
1776 |
+ if (!index) { |
|
1777 |
+ cli_dbgmsg("egg_parse_archive_headers: File buffer too small to contain egg_header.\n"); |
|
1778 |
+ goto done; |
|
1779 |
+ } |
|
1780 |
+ |
|
1781 |
+ eggHeader = (egg_header*)index; |
|
1782 |
+ |
|
1783 |
+ if (EGG_HEADER_MAGIC != le32_to_host(eggHeader->magic)) { |
|
1784 |
+ cli_dbgmsg("egg_parse_archive_headers: Invalid egg header magic: %08x.\n", le32_to_host(eggHeader->magic)); |
|
1785 |
+ goto done; |
|
1786 |
+ } |
|
1787 |
+ |
|
1788 |
+ cli_dbgmsg("egg_parse_archive_headers: egg_header->magic: %08x (%s)\n", le32_to_host(eggHeader->magic), getMagicHeaderName(le32_to_host(eggHeader->magic))); |
|
1789 |
+ cli_dbgmsg("egg_parse_archive_headers: egg_header->version: %04x\n", le16_to_host(eggHeader->version)); |
|
1790 |
+ cli_dbgmsg("egg_parse_archive_headers: egg_header->header_id: %08x\n", le32_to_host(eggHeader->header_id)); |
|
1791 |
+ cli_dbgmsg("egg_parse_archive_headers: egg_header->reserved: %08x\n", le32_to_host(eggHeader->reserved)); |
|
1792 |
+ |
|
1793 |
+ if (EGG_HEADER_VERSION != le16_to_host(eggHeader->version)) { |
|
1794 |
+ cli_dbgmsg("egg_parse_archive_headers: Unexpected EGG archive version #: %04x.\n", |
|
1795 |
+ le16_to_host(eggHeader->version)); |
|
1796 |
+ } |
|
1797 |
+ |
|
1798 |
+ handle->offset += sizeof(egg_header); |
|
1799 |
+ |
|
1800 |
+ /* |
|
1801 |
+ * 2nd: |
|
1802 |
+ * Egg Header may be followed by: |
|
1803 |
+ * a) split_compression header and/or |
|
1804 |
+ * b) solid_compression |
|
1805 |
+ * c) global encryption header |
|
1806 |
+ * d) EOFARC |
|
1807 |
+ */ |
|
1808 |
+ |
|
1809 |
+ while (handle->map->len > handle->offset) { |
|
1810 |
+ |
|
1811 |
+ /* Get the next magic32_t */ |
|
1812 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(magic32_t)); |
|
1813 |
+ if (!index) { |
|
1814 |
+ cli_dbgmsg("egg_parse_archive_headers: File buffer too small to contain end of archive magic bytes.\n"); |
|
1815 |
+ goto done; |
|
1816 |
+ } |
|
1817 |
+ |
|
1818 |
+ magic = le32_to_host(*((uint32_t*)index)); |
|
1819 |
+ |
|
1820 |
+ if (EOFARC == magic) { |
|
1821 |
+ /* |
|
1822 |
+ * Archive headers should conclude with EOFARC magic bytes. |
|
1823 |
+ */ |
|
1824 |
+ handle->offset += sizeof(magic32_t); |
|
1825 |
+ |
|
1826 |
+ cli_dbgmsg("egg_parse_archive_headers: End of archive headers.\n"); |
|
1827 |
+ break; /* Break out of the loop */ |
|
1828 |
+ } else { |
|
1829 |
+ /* |
|
1830 |
+ * Parse extra fields. |
|
1831 |
+ */ |
|
1832 |
+ retval = egg_parse_archive_extra_field(handle); |
|
1833 |
+ if (EGG_OK != retval) { |
|
1834 |
+ cli_dbgmsg("egg_parse_archive_headers: Failed to parse archive header, magic: %08x (%s)\n", magic, getMagicHeaderName(magic)); |
|
1835 |
+ break; /* Break out of the loop */ |
|
1836 |
+ } |
|
1837 |
+ } |
|
1838 |
+ } |
|
1839 |
+ |
|
1840 |
+ status = EGG_OK; |
|
1841 |
+ |
|
1842 |
+done: |
|
1843 |
+ return status; |
|
1844 |
+} |
|
1845 |
+ |
|
1846 |
+cl_egg_error_t cli_egg_open(fmap_t* map, size_t sfx_offset, void** hArchive, char** comment, uint32_t* comment_size) |
|
1847 |
+{ |
|
1848 |
+ cl_egg_error_t status = EGG_ERR; |
|
1849 |
+ cl_egg_error_t retval = EGG_ERR; |
|
1850 |
+ egg_handle* handle = NULL; |
|
1851 |
+ uint32_t magic = 0; |
|
1852 |
+ const uint8_t* index = 0; |
|
1853 |
+ |
|
1854 |
+ UNUSEDPARAM(comment); |
|
1855 |
+ UNUSEDPARAM(comment_size); |
|
1856 |
+ |
|
1857 |
+ if (!map || !hArchive) { |
|
1858 |
+ cli_errmsg("cli_egg_open: Invalid args!\n"); |
|
1859 |
+ return EGG_ERR; |
|
1860 |
+ } |
|
1861 |
+ |
|
1862 |
+ handle = (egg_handle*)cli_calloc(1, sizeof(egg_handle)); |
|
1863 |
+ if (NULL == handle) { |
|
1864 |
+ cli_errmsg("cli_egg_open: Failed to allocate memory for egg_handle.\n"); |
|
1865 |
+ status = EGG_EMEM; |
|
1866 |
+ goto done; |
|
1867 |
+ } |
|
1868 |
+ handle->map = map; |
|
1869 |
+ handle->offset = sfx_offset; |
|
1870 |
+ |
|
1871 |
+ /* |
|
1872 |
+ * 1st: |
|
1873 |
+ * Parse the archive headers. |
|
1874 |
+ */ |
|
1875 |
+ if (EGG_OK != (retval = egg_parse_archive_headers(handle))) { |
|
1876 |
+ cli_warnmsg("cli_egg_open: Failed to parse archive headers!\n"); |
|
1877 |
+ goto done; |
|
1878 |
+ } |
|
1879 |
+ |
|
1880 |
+ /* |
|
1881 |
+ * 2nd: |
|
1882 |
+ * Archive headers may be followed by: |
|
1883 |
+ * a) 0+ file headers |
|
1884 |
+ * b) 0+ block headers |
|
1885 |
+ * c) 0+ archive comment headers |
|
1886 |
+ */ |
|
1887 |
+ while (EGG_OK == retval) { |
|
1888 |
+ |
|
1889 |
+ /* Get the next magic32_t */ |
|
1890 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(magic32_t)); |
|
1891 |
+ if (!index) { |
|
1892 |
+ cli_dbgmsg("cli_egg_open: No more data in archive.\n"); |
|
1893 |
+ break; |
|
1894 |
+ } |
|
1895 |
+ |
|
1896 |
+ magic = le32_to_host(*((uint32_t*)index)); |
|
1897 |
+ |
|
1898 |
+ if (EOFARC == magic) { |
|
1899 |
+ /* |
|
1900 |
+ * Archive headers should conclude with EOFARC magic bytes. |
|
1901 |
+ */ |
|
1902 |
+ handle->offset += sizeof(magic32_t); |
|
1903 |
+ |
|
1904 |
+ if (handle->map->len > handle->offset) { |
|
1905 |
+ cli_warnmsg("Apparent end to EGG archive, but an addition %zu bytes of data exists in the file!\n", |
|
1906 |
+ handle->map->len - handle->offset); |
|
1907 |
+ } else { |
|
1908 |
+ cli_dbgmsg("cli_egg_open: Successfully indexed EGG archive!\n"); |
|
1909 |
+ } |
|
1910 |
+ |
|
1911 |
+ break; /* Break out of the loop */ |
|
1912 |
+ } else if (FILE_HEADER_MAGIC == magic) { |
|
1913 |
+ /* |
|
1914 |
+ * Archive File Header |
|
1915 |
+ */ |
|
1916 |
+ egg_file* found_file = NULL; |
|
1917 |
+ if (EGG_OK != (retval = egg_parse_file_headers(handle, &found_file))) { |
|
1918 |
+ cli_dbgmsg("cli_egg_open: Issue parsing file header. Error code: %u\n", retval); |
|
1919 |
+ goto done; |
|
1920 |
+ } else if (found_file == NULL) { |
|
1921 |
+ cli_errmsg("cli_egg_open: Logic error! Succesfully parsed file headers," |
|
1922 |
+ " but did not return egg_file information!\n"); |
|
1923 |
+ goto done; |
|
1924 |
+ } else { |
|
1925 |
+ /* Add file to list. */ |
|
1926 |
+ handle->files = (egg_file**)cli_realloc( |
|
1927 |
+ (void*)handle->files, |
|
1928 |
+ sizeof(egg_file*) * (handle->nFiles + 1)); |
|
1929 |
+ if (NULL == handle->files) { |
|
1930 |
+ status = EGG_EMEM; |
|
1931 |
+ goto done; |
|
1932 |
+ } |
|
1933 |
+ handle->files[handle->nFiles] = found_file; |
|
1934 |
+ handle->nFiles++; |
|
1935 |
+ } |
|
1936 |
+ } else if (BLOCK_HEADER_MAGIC == magic) { |
|
1937 |
+ /* |
|
1938 |
+ * Archive Block Header |
|
1939 |
+ */ |
|
1940 |
+ egg_block* found_block = NULL; |
|
1941 |
+ if (EGG_OK != (retval = egg_parse_block_headers(handle, &found_block))) { |
|
1942 |
+ cli_dbgmsg("cli_egg_open: Issue parsing block header. Error code: %u\n", retval); |
|
1943 |
+ goto done; |
|
1944 |
+ } else if (found_block == NULL) { |
|
1945 |
+ cli_errmsg("cli_egg_open: Logic error! Succesfully parsed block headers," |
|
1946 |
+ " but did not return egg_block information!\n"); |
|
1947 |
+ goto done; |
|
1948 |
+ } else { |
|
1949 |
+ /* Add block to list. */ |
|
1950 |
+ if (handle->bSolid) { |
|
1951 |
+ handle->blocks = (egg_block**)cli_realloc( |
|
1952 |
+ (void*)handle->blocks, |
|
1953 |
+ sizeof(egg_block*) * (handle->nBlocks + 1)); |
|
1954 |
+ if (NULL == handle->blocks) { |
|
1955 |
+ status = EGG_EMEM; |
|
1956 |
+ goto done; |
|
1957 |
+ } |
|
1958 |
+ handle->blocks[handle->nBlocks] = found_block; |
|
1959 |
+ handle->nBlocks++; |
|
1960 |
+ } else { |
|
1961 |
+ egg_file* eggFile = NULL; |
|
1962 |
+ /* |
|
1963 |
+ * Associate block with most recently added file. |
|
1964 |
+ */ |
|
1965 |
+ if (handle->nFiles == 0) { |
|
1966 |
+ cli_dbgmsg("cli_egg_open: No file found for block in non-solid archive.\n"); |
|
1967 |
+ // TODO: create an unamed block. |
|
1968 |
+ } else { |
|
1969 |
+ eggFile = handle->files[handle->nFiles - 1]; |
|
1970 |
+ |
|
1971 |
+ /* Add block to list. */ |
|
1972 |
+ eggFile->blocks = (egg_block**)cli_realloc( |
|
1973 |
+ (void*)eggFile->blocks, |
|
1974 |
+ sizeof(egg_block*) * (eggFile->nBlocks + 1)); |
|
1975 |
+ if (NULL == eggFile->blocks) { |
|
1976 |
+ status = EGG_EMEM; |
|
1977 |
+ goto done; |
|
1978 |
+ } |
|
1979 |
+ eggFile->blocks[eggFile->nBlocks] = found_block; |
|
1980 |
+ eggFile->nBlocks++; |
|
1981 |
+ } |
|
1982 |
+ } |
|
1983 |
+ } |
|
1984 |
+ } else if (COMMENT_HEADER_MAGIC == magic) { |
|
1985 |
+ /* |
|
1986 |
+ * Parse extra field for archive comment header. |
|
1987 |
+ */ |
|
1988 |
+ extra_field* extraField = NULL; |
|
1989 |
+ char* comment = NULL; |
|
1990 |
+ uint32_t size = 0; |
|
1991 |
+ |
|
1992 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(extra_field)); |
|
1993 |
+ if (!index) { |
|
1994 |
+ cli_dbgmsg("cli_egg_open: File buffer too small to contain extra_field header.\n"); |
|
1995 |
+ goto done; |
|
1996 |
+ } |
|
1997 |
+ |
|
1998 |
+ extraField = (extra_field*)index; |
|
1999 |
+ |
|
2000 |
+ cli_dbgmsg("cli_egg_open: archive comment extra_field->magic: %08x (%s)\n", le32_to_host(extraField->magic), getMagicHeaderName(le32_to_host(extraField->magic))); |
|
2001 |
+ cli_dbgmsg("cli_egg_open: archive comment extra_field->bit_flag: %02x\n", extraField->bit_flag); |
|
2002 |
+ |
|
2003 |
+ handle->offset += sizeof(extra_field); |
|
2004 |
+ |
|
2005 |
+ if (extraField->bit_flag & EXTRA_FIELD_FLAGS_SIZE_IS_4BYTES) { |
|
2006 |
+ /* size is uint32_t */ |
|
2007 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint32_t)); |
|
2008 |
+ if (!index) { |
|
2009 |
+ cli_dbgmsg("cli_egg_open: File buffer too small to contain archive comment extra_field header.\n"); |
|
2010 |
+ goto done; |
|
2011 |
+ } |
|
2012 |
+ |
|
2013 |
+ size = le32_to_host(*(uint32_t*)index); |
|
2014 |
+ |
|
2015 |
+ handle->offset += sizeof(uint32_t); |
|
2016 |
+ } else { |
|
2017 |
+ /* size is uint16_t */ |
|
2018 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint16_t)); |
|
2019 |
+ if (!index) { |
|
2020 |
+ cli_dbgmsg("cli_egg_open: File buffer too small to contain archive comment extra_field header.\n"); |
|
2021 |
+ goto done; |
|
2022 |
+ } |
|
2023 |
+ |
|
2024 |
+ size = le16_to_host(*(uint16_t*)index); |
|
2025 |
+ |
|
2026 |
+ handle->offset += sizeof(uint16_t); |
|
2027 |
+ } |
|
2028 |
+ |
|
2029 |
+ cli_dbgmsg("cli_egg_open: archive comment extra_field->size: %u\n", size); |
|
2030 |
+ |
|
2031 |
+ index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, size); |
|
2032 |
+ if (!index) { |
|
2033 |
+ cli_dbgmsg("cli_egg_open: File buffer too small to contain extra_field header.\n"); |
|
2034 |
+ goto done; |
|
2035 |
+ } |
|
2036 |
+ |
|
2037 |
+ retval = egg_parse_comment_header(index, size, extraField, &comment); |
|
2038 |
+ if (EGG_OK != retval) { |
|
2039 |
+ cli_dbgmsg("cli_egg_open: Failed to parse archive comment extra_field data.\n"); |
|
2040 |
+ goto done; |
|
2041 |
+ } |
|
2042 |
+ |
|
2043 |
+ handle->comments = (char**)cli_realloc( |
|
2044 |
+ (void*)handle->comments, |
|
2045 |
+ sizeof(char**) * (handle->nComments + 1)); |
|
2046 |
+ if (NULL == handle->comments) { |
|
2047 |
+ status = EGG_EMEM; |
|
2048 |
+ goto done; |
|
2049 |
+ } |
|
2050 |
+ handle->comments[handle->nComments] = comment; |
|
2051 |
+ handle->nComments++; |
|
2052 |
+ } else { |
|
2053 |
+ cli_dbgmsg("cli_egg_open: unexpected header magic: %08x (%s)\n", magic, getMagicHeaderName(magic)); |
|
2054 |
+ status = EGG_ERR; |
|
2055 |
+ } |
|
2056 |
+ } |
|
2057 |
+ |
|
2058 |
+ if (EGG_OK != retval) { |
|
2059 |
+ if (EGG_BREAK == retval) { |
|
2060 |
+ /* End of archive. */ |
|
2061 |
+ if ((handle->bSplit) && (handle->splitInfo->next_file_id != 0)) |
|
2062 |
+ cli_warnmsg("cli_egg_open: Abrupt end to EGG volume!\n"); |
|
2063 |
+ else |
|
2064 |
+ cli_dbgmsg("cli_egg_open: End of EGG volume in split archive.\n"); |
|
2065 |
+ } else { |
|
2066 |
+ /* Something went wrong. */ |
|
2067 |
+ cli_warnmsg("cli_egg_open: Failed to parse file headers!\n"); |
|
2068 |
+ } |
|
2069 |
+ } |
|
2070 |
+ |
|
2071 |
+ *hArchive = handle; |
|
2072 |
+ status = EGG_OK; |
|
2073 |
+ |
|
2074 |
+done: |
|
2075 |
+ if (EGG_OK != status) { |
|
2076 |
+ if (handle) |
|
2077 |
+ egg_free_egg_handle(handle); |
|
2078 |
+ *hArchive = NULL; |
|
2079 |
+ } |
|
2080 |
+ return status; |
|
2081 |
+} |
|
2082 |
+ |
|
2083 |
+cl_egg_error_t cli_egg_peek_file_header(void* hArchive, cl_egg_metadata* file_metadata) |
|
2084 |
+{ |
|
2085 |
+ cl_egg_error_t status = EGG_ERR; |
|
2086 |
+ egg_handle* handle = NULL; |
|
2087 |
+ egg_file* currFile = NULL; |
|
2088 |
+ |
|
2089 |
+ if (!hArchive || !file_metadata) { |
|
2090 |
+ cli_errmsg("cli_egg_peek_file_header: Invalid args!\n"); |
|
2091 |
+ return EGG_ERR; |
|
2092 |
+ } |
|
2093 |
+ |
|
2094 |
+ handle = (egg_handle*)hArchive; |
|
2095 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
2096 |
+ cli_errmsg("cli_egg_peek_file_header: Invalid handle values!\n"); |
|
2097 |
+ goto done; |
|
2098 |
+ } |
|
2099 |
+ |
|
2100 |
+ memset(file_metadata, 0, sizeof(cl_egg_metadata)); |
|
2101 |
+ |
|
2102 |
+ if (handle->fileExtractionIndex >= handle->nFiles) { |
|
2103 |
+ status = EGG_BREAK; |
|
2104 |
+ goto done; |
|
2105 |
+ } |
|
2106 |
+ |
|
2107 |
+ currFile = handle->files[handle->fileExtractionIndex]; |
|
2108 |
+ if (NULL == currFile) { |
|
2109 |
+ cli_errmsg("cli_egg_peek_file_header: invalid egg_file pointer!\n"); |
|
2110 |
+ goto done; |
|
2111 |
+ } |
|
2112 |
+ |
|
2113 |
+ if (NULL == currFile->file) { |
|
2114 |
+ cli_errmsg("cli_egg_peek_file_header: egg_file is missing file header!\n"); |
|
2115 |
+ goto done; |
|
2116 |
+ } |
|
2117 |
+ |
|
2118 |
+ if (handle->bSolid) { |
|
2119 |
+ /* |
|
2120 |
+ * TODO: no idea. |
|
2121 |
+ */ |
|
2122 |
+ file_metadata->pack_size = 0; |
|
2123 |
+ file_metadata->unpack_size = currFile->file->file_length; |
|
2124 |
+ } else { |
|
2125 |
+ uint64_t i = 0; |
|
2126 |
+ if (!currFile->blocks) { |
|
2127 |
+ cli_errmsg("cli_egg_peek_file_header: empty file!\n"); |
|
2128 |
+ } |
|
2129 |
+ for (i = 0; i < currFile->nBlocks; i++) { |
|
2130 |
+ egg_block* currBlock = currFile->blocks[i]; |
|
2131 |
+ |
|
2132 |
+ if (!currBlock->blockHeader) { |
|
2133 |
+ cli_errmsg("cli_egg_peek_file_header: egg_block missing block_header!\n"); |
|
2134 |
+ goto done; |
|
2135 |
+ } |
|
2136 |
+ file_metadata->pack_size += currBlock->blockHeader->compress_size; |
|
2137 |
+ file_metadata->unpack_size += currBlock->blockHeader->uncompress_size; |
|
2138 |
+ } |
|
2139 |
+ if (file_metadata->unpack_size != currFile->file->file_length) { |
|
2140 |
+ cli_warnmsg("cli_egg_peek_file_header: sum of block uncompress_size's does not match listed file_length!\n"); |
|
2141 |
+ } |
|
2142 |
+ } |
|
2143 |
+ |
|
2144 |
+ file_metadata->filename = strdup(currFile->filename.name_utf8); |
|
2145 |
+ |
|
2146 |
+ if (NULL != currFile->encrypt) |
|
2147 |
+ file_metadata->encrypted = 1; |
|
2148 |
+ |
|
2149 |
+ if (currFile->posixFileInformation && currFile->posixFileInformation->mode & POSIX_INFO_MODE_DIRECTORY) |
|
2150 |
+ file_metadata->is_dir = 1; |
|
2151 |
+ else if (currFile->windowsFileInformation && currFile->windowsFileInformation->attribute & WINDOWS_INFO_ATTRIBUTE_DIRECTORY) |
|
2152 |
+ file_metadata->is_dir = 1; |
|
2153 |
+ |
|
2154 |
+ status = EGG_OK; |
|
2155 |
+done: |
|
2156 |
+ return status; |
|
2157 |
+} |
|
2158 |
+ |
|
2159 |
+cl_egg_error_t cli_egg_deflate_decompress(char* compressed, size_t compressed_size, char** decompressed, size_t* decompressed_size) |
|
2160 |
+{ |
|
2161 |
+ cl_egg_error_t status = EGG_ERR; |
|
2162 |
+ |
|
2163 |
+ uint8_t* temp = NULL; |
|
2164 |
+ uint8_t* decoded = NULL; |
|
2165 |
+ uint32_t declen = 0, capacity = 0; |
|
2166 |
+ |
|
2167 |
+ z_stream stream; |
|
2168 |
+ int zstat; |
|
2169 |
+ |
|
2170 |
+ if (NULL == compressed || compressed_size == 0 || NULL == decompressed || NULL == decompressed_size) { |
|
2171 |
+ cli_errmsg("cli_egg_deflate_decompress: Invalid args!\n"); |
|
2172 |
+ status = EGG_ERR; |
|
2173 |
+ goto done; |
|
2174 |
+ } |
|
2175 |
+ |
|
2176 |
+ *decompressed = NULL; |
|
2177 |
+ *decompressed_size = 0; |
|
2178 |
+ |
|
2179 |
+ if (!(decoded = (uint8_t*)cli_calloc(BUFSIZ, sizeof(uint8_t)))) { |
|
2180 |
+ cli_errmsg("cli_egg_deflate_decompress: cannot allocate memory for decompressed output\n"); |
|
2181 |
+ status = EGG_EMEM; |
|
2182 |
+ goto done; |
|
2183 |
+ } |
|
2184 |
+ |
|
2185 |
+ capacity = BUFSIZ; |
|
2186 |
+ |
|
2187 |
+ memset(&stream, 0, sizeof(stream)); |
|
2188 |
+ stream.next_in = (Bytef*)compressed; |
|
2189 |
+ stream.avail_in = compressed_size; |
|
2190 |
+ stream.next_out = (Bytef*)decoded; |
|
2191 |
+ stream.avail_out = BUFSIZ; |
|
2192 |
+ |
|
2193 |
+ zstat = inflateInit2(&stream, -15); |
|
2194 |
+ if (zstat != Z_OK) { |
|
2195 |
+ cli_warnmsg("cli_egg_deflate_decompress: inflateInit failed\n"); |
|
2196 |
+ status = EGG_EMEM; |
|
2197 |
+ goto done; |
|
2198 |
+ } |
|
2199 |
+ |
|
2200 |
+ /* initial inflate */ |
|
2201 |
+ zstat = inflate(&stream, Z_NO_FLUSH); |
|
2202 |
+ |
|
2203 |
+ /* check if nothing written whatsoever */ |
|
2204 |
+ if ((zstat != Z_OK) && (stream.avail_out == BUFSIZ)) { |
|
2205 |
+ /* Inflation failed */ |
|
2206 |
+ cli_errmsg("cli_egg_deflate_decompress: failed to decompress data\n"); |
|
2207 |
+ status = EGG_ERR; |
|
2208 |
+ goto done; |
|
2209 |
+ } |
|
2210 |
+ |
|
2211 |
+ while (zstat == Z_OK && stream.avail_in) { |
|
2212 |
+ /* extend output capacity if needed,*/ |
|
2213 |
+ if (stream.avail_out == 0) { |
|
2214 |
+ if (!(temp = cli_realloc(decoded, capacity + BUFSIZ))) { |
|
2215 |
+ cli_errmsg("cli_egg_deflate_decompress: cannot reallocate memory for decompressed output\n"); |
|
2216 |
+ status = EGG_EMEM; |
|
2217 |
+ goto done; |
|
2218 |
+ } |
|
2219 |
+ decoded = temp; |
|
2220 |
+ stream.next_out = decoded + capacity; |
|
2221 |
+ stream.avail_out = BUFSIZ; |
|
2222 |
+ declen += BUFSIZ; |
|
2223 |
+ capacity += BUFSIZ; |
|
2224 |
+ } |
|
2225 |
+ |
|
2226 |
+ /* continue inflation */ |
|
2227 |
+ zstat = inflate(&stream, Z_NO_FLUSH); |
|
2228 |
+ } |
|
2229 |
+ |
|
2230 |
+ /* add end fragment to decoded length */ |
|
2231 |
+ declen += (BUFSIZ - stream.avail_out); |
|
2232 |
+ |
|
2233 |
+ /* error handling */ |
|
2234 |
+ switch (zstat) { |
|
2235 |
+ case Z_OK: |
|
2236 |
+ cli_dbgmsg("cli_egg_deflate_decompress: Z_OK on stream decompression\n"); |
|
2237 |
+ /* intentional fall-through */ |
|
2238 |
+ case Z_STREAM_END: |
|
2239 |
+ cli_dbgmsg("cli_egg_deflate_decompress: decompressed %lu bytes from %lu total bytes (%lu bytes remaining)\n", |
|
2240 |
+ (unsigned long)declen, (unsigned long)(compressed_size), (unsigned long)(stream.avail_in)); |
|
2241 |
+ break; |
|
2242 |
+ |
|
2243 |
+ /* potentially fatal - *mostly* ignored as per older version */ |
|
2244 |
+ case Z_STREAM_ERROR: |
|
2245 |
+ case Z_NEED_DICT: |
|
2246 |
+ case Z_DATA_ERROR: |
|
2247 |
+ case Z_MEM_ERROR: |
|
2248 |
+ default: |
|
2249 |
+ if (stream.msg) |
|
2250 |
+ cli_dbgmsg("cli_egg_deflate_decompress: after decompressing %lu bytes, got error \"%s\"\n", |
|
2251 |
+ (unsigned long)declen, stream.msg); |
|
2252 |
+ else |
|
2253 |
+ cli_dbgmsg("cli_egg_deflate_decompress: after decompressing %lu bytes, got error %d\n", |
|
2254 |
+ (unsigned long)declen, zstat); |
|
2255 |
+ |
|
2256 |
+ if (declen == 0) { |
|
2257 |
+ cli_dbgmsg("cli_egg_deflate_decompress: no bytes were decompressed.\n"); |
|
2258 |
+ |
|
2259 |
+ status = EGG_ERR; |
|
2260 |
+ } |
|
2261 |
+ break; |
|
2262 |
+ } |
|
2263 |
+ |
|
2264 |
+ *decompressed = (char*)decoded; |
|
2265 |
+ *decompressed_size = declen; |
|
2266 |
+ |
|
2267 |
+ status = EGG_OK; |
|
2268 |
+ |
|
2269 |
+done: |
|
2270 |
+ |
|
2271 |
+ (void)inflateEnd(&stream); |
|
2272 |
+ |
|
2273 |
+ if (EGG_OK != status) { |
|
2274 |
+ free(decoded); |
|
2275 |
+ } |
|
2276 |
+ |
|
2277 |
+ return status; |
|
2278 |
+} |
|
2279 |
+ |
|
2280 |
+#ifdef HAVE_BZLIB_H |
|
2281 |
+cl_egg_error_t cli_egg_bzip2_decompress(char* compressed, size_t compressed_size, char** decompressed, size_t* decompressed_size) |
|
2282 |
+{ |
|
2283 |
+ cl_egg_error_t status = EGG_ERR; |
|
2284 |
+ |
|
2285 |
+ char* temp = NULL; |
|
2286 |
+ char* decoded = NULL; |
|
2287 |
+ uint32_t declen = 0, capacity = 0; |
|
2288 |
+ |
|
2289 |
+ bz_stream stream; |
|
2290 |
+ int bzstat; |
|
2291 |
+ |
|
2292 |
+ if (NULL == compressed || compressed_size == 0 || NULL == decompressed || NULL == decompressed_size) { |
|
2293 |
+ cli_errmsg("cli_egg_bzip2_decompress: Invalid args!\n"); |
|
2294 |
+ status = EGG_ERR; |
|
2295 |
+ goto done; |
|
2296 |
+ } |
|
2297 |
+ |
|
2298 |
+ *decompressed = NULL; |
|
2299 |
+ *decompressed_size = 0; |
|
2300 |
+ |
|
2301 |
+ if (!(decoded = (char*)cli_calloc(BUFSIZ, sizeof(Bytef)))) { |
|
2302 |
+ cli_errmsg("cli_egg_bzip2_decompress: cannot allocate memory for decompressed output\n"); |
|
2303 |
+ status = EGG_EMEM; |
|
2304 |
+ goto done; |
|
2305 |
+ } |
|
2306 |
+ |
|
2307 |
+ capacity = BUFSIZ; |
|
2308 |
+ |
|
2309 |
+ memset(&stream, 0, sizeof(stream)); |
|
2310 |
+ stream.next_in = compressed; |
|
2311 |
+ stream.avail_in = compressed_size; |
|
2312 |
+ stream.next_out = decoded; |
|
2313 |
+ stream.avail_out = BUFSIZ; |
|
2314 |
+ |
|
2315 |
+ if (BZ_OK != (bzstat = BZ2_bzDecompressInit(&stream, 0, 0))) { |
|
2316 |
+ cli_warnmsg("cli_egg_bzip2_decompress: bzinit failed\n"); |
|
2317 |
+ status = EGG_EMEM; |
|
2318 |
+ goto done; |
|
2319 |
+ } |
|
2320 |
+ |
|
2321 |
+ /* initial inflate */ |
|
2322 |
+ bzstat = BZ2_bzDecompress(&stream); |
|
2323 |
+ |
|
2324 |
+ /* check if nothing written whatsoever */ |
|
2325 |
+ if ((bzstat != BZ_OK) && (stream.avail_out == BUFSIZ)) { |
|
2326 |
+ /* Inflation failed */ |
|
2327 |
+ cli_errmsg("cli_egg_bzip2_decompress: failed to decompress data\n"); |
|
2328 |
+ status = EGG_ERR; |
|
2329 |
+ goto done; |
|
2330 |
+ } |
|
2331 |
+ |
|
2332 |
+ while (bzstat == BZ_OK && stream.avail_in) { |
|
2333 |
+ /* extend output capacity if needed,*/ |
|
2334 |
+ if (stream.avail_out == 0) { |
|
2335 |
+ if (!(temp = cli_realloc(decoded, capacity + BUFSIZ))) { |
|
2336 |
+ cli_errmsg("cli_egg_bzip2_decompress: cannot reallocate memory for decompressed output\n"); |
|
2337 |
+ status = EGG_EMEM; |
|
2338 |
+ goto done; |
|
2339 |
+ } |
|
2340 |
+ decoded = temp; |
|
2341 |
+ stream.next_out = decoded + capacity; |
|
2342 |
+ stream.avail_out = BUFSIZ; |
|
2343 |
+ declen += BUFSIZ; |
|
2344 |
+ capacity += BUFSIZ; |
|
2345 |
+ } |
|
2346 |
+ |
|
2347 |
+ /* continue inflation */ |
|
2348 |
+ bzstat = BZ2_bzDecompress(&stream); |
|
2349 |
+ } |
|
2350 |
+ |
|
2351 |
+ /* add end fragment to decoded length */ |
|
2352 |
+ declen += (BUFSIZ - stream.avail_out); |
|
2353 |
+ |
|
2354 |
+ /* error handling */ |
|
2355 |
+ switch (bzstat) { |
|
2356 |
+ case BZ_OK: |
|
2357 |
+ cli_dbgmsg("cli_egg_bzip2_decompress: BZ_OK on stream decompression\n"); |
|
2358 |
+ /* intentional fall-through */ |
|
2359 |
+ case BZ_STREAM_END: |
|
2360 |
+ cli_dbgmsg("cli_egg_bzip2_decompress: decompressed %lu bytes from %lu total bytes (%lu bytes remaining)\n", |
|
2361 |
+ (unsigned long)declen, (unsigned long)(compressed_size), (unsigned long)(stream.avail_in)); |
|
2362 |
+ break; |
|
2363 |
+ |
|
2364 |
+ /* potentially fatal */ |
|
2365 |
+ case BZ_DATA_ERROR: |
|
2366 |
+ case BZ_MEM_ERROR: |
|
2367 |
+ default: |
|
2368 |
+ cli_dbgmsg("cli_egg_bzip2_decompress: after decompressing %lu bytes, got error %d\n", |
|
2369 |
+ (unsigned long)declen, bzstat); |
|
2370 |
+ |
|
2371 |
+ if (declen == 0) { |
|
2372 |
+ cli_dbgmsg("cli_egg_bzip2_decompress: no bytes were decompressed.\n"); |
|
2373 |
+ |
|
2374 |
+ status = EGG_ERR; |
|
2375 |
+ } |
|
2376 |
+ break; |
|
2377 |
+ } |
|
2378 |
+ |
|
2379 |
+ *decompressed = (char*)decoded; |
|
2380 |
+ *decompressed_size = declen; |
|
2381 |
+ |
|
2382 |
+ status = EGG_OK; |
|
2383 |
+ |
|
2384 |
+done: |
|
2385 |
+ |
|
2386 |
+ (void)BZ2_bzDecompressEnd(&stream); |
|
2387 |
+ |
|
2388 |
+ if (EGG_OK != status) { |
|
2389 |
+ free(decoded); |
|
2390 |
+ } |
|
2391 |
+ |
|
2392 |
+ return status; |
|
2393 |
+} |
|
2394 |
+#endif |
|
2395 |
+ |
|
2396 |
+cl_egg_error_t cli_egg_lzma_decompress(char* compressed, size_t compressed_size, char** decompressed, size_t* decompressed_size) |
|
2397 |
+{ |
|
2398 |
+ cl_egg_error_t status = EGG_ERR; |
|
2399 |
+ |
|
2400 |
+ uint8_t* temp = NULL; |
|
2401 |
+ uint8_t* decoded = NULL; |
|
2402 |
+ uint32_t declen = 0, capacity = 0; |
|
2403 |
+ |
|
2404 |
+ struct CLI_LZMA stream; |
|
2405 |
+ int lzmastat; |
|
2406 |
+ |
|
2407 |
+ if (NULL == compressed || compressed_size == 0 || NULL == decompressed || NULL == decompressed_size) { |
|
2408 |
+ cli_errmsg("cli_egg_lzma_decompress: Invalid args!\n"); |
|
2409 |
+ status = EGG_ERR; |
|
2410 |
+ goto done; |
|
2411 |
+ } |
|
2412 |
+ |
|
2413 |
+ *decompressed = NULL; |
|
2414 |
+ *decompressed_size = 0; |
|
2415 |
+ |
|
2416 |
+ if (!(decoded = (uint8_t*)cli_calloc(BUFSIZ, sizeof(char)))) { |
|
2417 |
+ cli_errmsg("cli_egg_lzma_decompress: cannot allocate memory for decompressed output\n"); |
|
2418 |
+ status = EGG_EMEM; |
|
2419 |
+ goto done; |
|
2420 |
+ } |
|
2421 |
+ |
|
2422 |
+ capacity = BUFSIZ; |
|
2423 |
+ |
|
2424 |
+ memset(&stream, 0, sizeof(stream)); |
|
2425 |
+ stream.next_in = (Bytef*)compressed; |
|
2426 |
+ stream.avail_in = compressed_size; |
|
2427 |
+ stream.next_out = (Bytef*)decoded; |
|
2428 |
+ stream.avail_out = BUFSIZ; |
|
2429 |
+ |
|
2430 |
+ lzmastat = cli_LzmaInit(&stream, 0); |
|
2431 |
+ if (lzmastat != LZMA_RESULT_OK) { |
|
2432 |
+ cli_warnmsg("cli_egg_lzma_decompress: inflateInit failed\n"); |
|
2433 |
+ status = EGG_EMEM; |
|
2434 |
+ goto done; |
|
2435 |
+ } |
|
2436 |
+ |
|
2437 |
+ /* initial inflate */ |
|
2438 |
+ lzmastat = cli_LzmaDecode(&stream); |
|
2439 |
+ |
|
2440 |
+ /* check if nothing written whatsoever */ |
|
2441 |
+ if ((lzmastat != LZMA_RESULT_OK) && (stream.avail_out == BUFSIZ)) { |
|
2442 |
+ /* Inflation failed */ |
|
2443 |
+ cli_errmsg("cli_egg_lzma_decompress: failed to decompress data\n"); |
|
2444 |
+ status = EGG_ERR; |
|
2445 |
+ goto done; |
|
2446 |
+ } |
|
2447 |
+ |
|
2448 |
+ while (lzmastat == LZMA_RESULT_OK && stream.avail_in) { |
|
2449 |
+ /* extend output capacity if needed,*/ |
|
2450 |
+ if (stream.avail_out == 0) { |
|
2451 |
+ if (!(temp = cli_realloc(decoded, capacity + BUFSIZ))) { |
|
2452 |
+ cli_errmsg("cli_egg_lzma_decompress: cannot reallocate memory for decompressed output\n"); |
|
2453 |
+ status = EGG_EMEM; |
|
2454 |
+ goto done; |
|
2455 |
+ } |
|
2456 |
+ decoded = temp; |
|
2457 |
+ stream.next_out = decoded + capacity; |
|
2458 |
+ stream.avail_out = BUFSIZ; |
|
2459 |
+ declen += BUFSIZ; |
|
2460 |
+ capacity += BUFSIZ; |
|
2461 |
+ } |
|
2462 |
+ |
|
2463 |
+ /* continue inflation */ |
|
2464 |
+ lzmastat = cli_LzmaDecode(&stream); |
|
2465 |
+ } |
|
2466 |
+ |
|
2467 |
+ /* add end fragment to decoded length */ |
|
2468 |
+ declen += (BUFSIZ - stream.avail_out); |
|
2469 |
+ |
|
2470 |
+ /* error handling */ |
|
2471 |
+ switch (lzmastat) { |
|
2472 |
+ case LZMA_RESULT_OK: |
|
2473 |
+ cli_dbgmsg("cli_egg_lzma_decompress: Z_OK on stream decompression\n"); |
|
2474 |
+ /* intentional fall-through */ |
|
2475 |
+ case LZMA_STREAM_END: |
|
2476 |
+ cli_dbgmsg("cli_egg_lzma_decompress: decompressed %lu bytes from %lu total bytes (%lu bytes remaining)\n", |
|
2477 |
+ (unsigned long)declen, (unsigned long)(compressed_size), (unsigned long)(stream.avail_in)); |
|
2478 |
+ break; |
|
2479 |
+ |
|
2480 |
+ /* potentially fatal */ |
|
2481 |
+ case LZMA_RESULT_DATA_ERROR: |
|
2482 |
+ default: |
|
2483 |
+ cli_dbgmsg("cli_egg_lzma_decompress: after decompressing %lu bytes, got error %d\n", |
|
2484 |
+ (unsigned long)declen, lzmastat); |
|
2485 |
+ |
|
2486 |
+ if (declen == 0) { |
|
2487 |
+ cli_dbgmsg("cli_egg_lzma_decompress: no bytes were decompressed.\n"); |
|
2488 |
+ |
|
2489 |
+ status = EGG_ERR; |
|
2490 |
+ } |
|
2491 |
+ break; |
|
2492 |
+ } |
|
2493 |
+ |
|
2494 |
+ *decompressed = (char*)decoded; |
|
2495 |
+ *decompressed_size = declen; |
|
2496 |
+ |
|
2497 |
+ status = EGG_OK; |
|
2498 |
+ |
|
2499 |
+done: |
|
2500 |
+ |
|
2501 |
+ (void)cli_LzmaShutdown(&stream); |
|
2502 |
+ |
|
2503 |
+ if (EGG_OK != status) { |
|
2504 |
+ free(decoded); |
|
2505 |
+ } |
|
2506 |
+ |
|
2507 |
+ return status; |
|
2508 |
+} |
|
2509 |
+ |
|
2510 |
+cl_egg_error_t cli_egg_extract_file(void* hArchive, const char** filename, const char** output_buffer, size_t* output_buffer_length) |
|
2511 |
+{ |
|
2512 |
+ cl_egg_error_t status = EGG_ERR; |
|
2513 |
+ egg_handle* handle = NULL; |
|
2514 |
+ egg_file* currFile = NULL; |
|
2515 |
+ char* decompressed = NULL; |
|
2516 |
+ uint64_t decompressed_size = 0; |
|
2517 |
+ uint64_t i = 0; |
|
2518 |
+ |
|
2519 |
+ if (!hArchive || !filename || !output_buffer || !output_buffer_length) { |
|
2520 |
+ cli_errmsg("cli_egg_extract_file: Invalid args!\n"); |
|
2521 |
+ goto done; |
|
2522 |
+ } |
|
2523 |
+ |
|
2524 |
+ *output_buffer = NULL; |
|
2525 |
+ *output_buffer_length = 0; |
|
2526 |
+ |
|
2527 |
+ handle = (egg_handle*)hArchive; |
|
2528 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
2529 |
+ cli_errmsg("cli_egg_extract_file: Invalid handle values!\n"); |
|
2530 |
+ goto done; |
|
2531 |
+ } |
|
2532 |
+ |
|
2533 |
+ if (handle->fileExtractionIndex >= handle->nFiles) { |
|
2534 |
+ cli_errmsg("cli_egg_extract_file: File index exceeds number of files in archive!\n"); |
|
2535 |
+ goto done; |
|
2536 |
+ } |
|
2537 |
+ |
|
2538 |
+ currFile = handle->files[handle->fileExtractionIndex]; |
|
2539 |
+ if (NULL == currFile) { |
|
2540 |
+ cli_errmsg("cli_egg_extract_file: invalid egg_file pointer!\n"); |
|
2541 |
+ goto done; |
|
2542 |
+ } |
|
2543 |
+ |
|
2544 |
+ if (NULL == currFile->file) { |
|
2545 |
+ cli_errmsg("cli_egg_extract_file: egg_file is missing file header!\n"); |
|
2546 |
+ goto done; |
|
2547 |
+ } |
|
2548 |
+ |
|
2549 |
+ if (handle->bSolid) { |
|
2550 |
+ /* |
|
2551 |
+ * TODO: no idea. |
|
2552 |
+ */ |
|
2553 |
+ } else { |
|
2554 |
+ if (currFile->nBlocks == 0 || currFile->blocks == NULL) { |
|
2555 |
+ cli_errmsg("cli_egg_extract_file: empty file!\n"); |
|
2556 |
+ } |
|
2557 |
+ |
|
2558 |
+ for (i = 0; i < currFile->nBlocks; i++) { |
|
2559 |
+ egg_block* currBlock = currFile->blocks[i]; |
|
2560 |
+ cl_egg_error_t retval = EGG_ERR; |
|
2561 |
+ |
|
2562 |
+ if (NULL == currBlock->blockHeader) { |
|
2563 |
+ cli_errmsg("cli_egg_extract_file: current egg_block missing header!\n"); |
|
2564 |
+ break; |
|
2565 |
+ } |
|
2566 |
+ switch (currBlock->blockHeader->compress_algorithm) { |
|
2567 |
+ case BLOCK_HEADER_COMPRESS_ALGORITHM_STORE: { |
|
2568 |
+ /* |
|
2569 |
+ * No compression. Woohoo! |
|
2570 |
+ */ |
|
2571 |
+ if (currBlock->blockHeader->compress_size == 0) { |
|
2572 |
+ cli_warnmsg("cli_egg_extract_file: blockHeader compress_size is 0!\n"); |
|
2573 |
+ break; |
|
2574 |
+ } else if (currBlock->blockHeader->compress_size != currBlock->blockHeader->uncompress_size) { |
|
2575 |
+ cli_warnmsg("cli_egg_extract_file: blockHeader compress_size != uncompress_size!\n"); |
|
2576 |
+ break; |
|
2577 |
+ } |
|
2578 |
+ decompressed = cli_realloc(decompressed, (size_t)decompressed_size + currBlock->blockHeader->compress_size); |
|
2579 |
+ if (NULL == decompressed) { |
|
2580 |
+ cli_errmsg("cli_egg_extract_file: Failed to allocate %llu bytes for decompressed file!\n", |
|
2581 |
+ decompressed_size); |
|
2582 |
+ status = EGG_EMEM; |
|
2583 |
+ goto done; |
|
2584 |
+ } |
|
2585 |
+ |
|
2586 |
+ memcpy(decompressed + decompressed_size, currBlock->compressedData, currBlock->blockHeader->compress_size); |
|
2587 |
+ decompressed_size += currBlock->blockHeader->compress_size; |
|
2588 |
+ |
|
2589 |
+ retval = EGG_OK; |
|
2590 |
+ break; |
|
2591 |
+ } |
|
2592 |
+ case BLOCK_HEADER_COMPRESS_ALGORITHM_DEFLATE: { |
|
2593 |
+ char* decompressed_block = NULL; |
|
2594 |
+ size_t decompressed_block_size = 0; |
|
2595 |
+ |
|
2596 |
+ if (EGG_OK != cli_egg_deflate_decompress(currBlock->compressedData, |
|
2597 |
+ currBlock->blockHeader->compress_size, |
|
2598 |
+ &decompressed_block, |
|
2599 |
+ &decompressed_block_size)) { |
|
2600 |
+ /* Failed to decompress block */ |
|
2601 |
+ cli_warnmsg("Failed to decompress RFC 1951 deflate compressed block\n"); |
|
2602 |
+ goto done; |
|
2603 |
+ } |
|
2604 |
+ /* Decompressed block. Add it to the file data */ |
|
2605 |
+ decompressed = cli_realloc(decompressed, (size_t)decompressed_size + decompressed_block_size); |
|
2606 |
+ if (NULL == decompressed) { |
|
2607 |
+ cli_errmsg("cli_egg_extract_file: Failed to allocate %llu bytes for decompressed file!\n", |
|
2608 |
+ decompressed_size); |
|
2609 |
+ free(decompressed_block); |
|
2610 |
+ status = EGG_EMEM; |
|
2611 |
+ goto done; |
|
2612 |
+ } |
|
2613 |
+ |
|
2614 |
+ memcpy(decompressed + decompressed_size, decompressed_block, decompressed_block_size); |
|
2615 |
+ decompressed_size += decompressed_block_size; |
|
2616 |
+ |
|
2617 |
+ free(decompressed_block); |
|
2618 |
+ |
|
2619 |
+ retval = EGG_OK; |
|
2620 |
+ break; |
|
2621 |
+ } |
|
2622 |
+ case BLOCK_HEADER_COMPRESS_ALGORITHM_BZIP2: { |
|
2623 |
+#if HAVE_BZLIB_H |
|
2624 |
+ char* decompressed_block = NULL; |
|
2625 |
+ size_t decompressed_block_size = 0; |
|
2626 |
+ |
|
2627 |
+ if (EGG_OK != cli_egg_bzip2_decompress(currBlock->compressedData, |
|
2628 |
+ currBlock->blockHeader->compress_size, |
|
2629 |
+ &decompressed_block, |
|
2630 |
+ &decompressed_block_size)) { |
|
2631 |
+ /* Failed to decompress block */ |
|
2632 |
+ cli_warnmsg("Failed to decompress BZIP2 compressed block\n"); |
|
2633 |
+ goto done; |
|
2634 |
+ } |
|
2635 |
+ /* Decompressed block. Add it to the file data */ |
|
2636 |
+ decompressed = cli_realloc(decompressed, (size_t)decompressed_size + decompressed_block_size); |
|
2637 |
+ if (NULL == decompressed) { |
|
2638 |
+ cli_errmsg("cli_egg_extract_file: Failed to allocate %llu bytes for decompressed file!\n", |
|
2639 |
+ decompressed_size); |
|
2640 |
+ free(decompressed_block); |
|
2641 |
+ status = EGG_EMEM; |
|
2642 |
+ goto done; |
|
2643 |
+ } |
|
2644 |
+ |
|
2645 |
+ memcpy(decompressed + decompressed_size, decompressed_block, decompressed_block_size); |
|
2646 |
+ decompressed_size += decompressed_block_size; |
|
2647 |
+ |
|
2648 |
+ free(decompressed_block); |
|
2649 |
+ |
|
2650 |
+ retval = EGG_OK; |
|
2651 |
+ break; |
|
2652 |
+#else |
|
2653 |
+ cli_warnmsg("cli_egg_extract_file: BZIP2 decompression support not available.\n"); |
|
2654 |
+ goto done; |
|
2655 |
+#endif |
|
2656 |
+ } |
|
2657 |
+ case BLOCK_HEADER_COMPRESS_ALGORITHM_AZO: { |
|
2658 |
+ cli_warnmsg("cli_egg_extract_file: AZO decompression not yet supported.\n"); |
|
2659 |
+ goto done; |
|
2660 |
+ //break; |
|
2661 |
+ } |
|
2662 |
+ case BLOCK_HEADER_COMPRESS_ALGORITHM_LZMA: { |
|
2663 |
+ char* decompressed_block = NULL; |
|
2664 |
+ size_t decompressed_block_size = 0; |
|
2665 |
+ |
|
2666 |
+ if (EGG_OK != cli_egg_lzma_decompress(currBlock->compressedData, |
|
2667 |
+ currBlock->blockHeader->compress_size, |
|
2668 |
+ &decompressed_block, |
|
2669 |
+ &decompressed_block_size)) { |
|
2670 |
+ /* Failed to decompress block */ |
|
2671 |
+ cli_warnmsg("Failed to decompress LZMA compressed block\n"); |
|
2672 |
+ goto done; |
|
2673 |
+ } |
|
2674 |
+ /* Decompressed block. Add it to the file data */ |
|
2675 |
+ decompressed = cli_realloc(decompressed, (size_t)decompressed_size + decompressed_block_size); |
|
2676 |
+ if (NULL == decompressed) { |
|
2677 |
+ cli_errmsg("cli_egg_extract_file: Failed to allocate %llu bytes for decompressed file!\n", |
|
2678 |
+ decompressed_size); |
|
2679 |
+ free(decompressed_block); |
|
2680 |
+ status = EGG_EMEM; |
|
2681 |
+ goto done; |
|
2682 |
+ } |
|
2683 |
+ |
|
2684 |
+ memcpy(decompressed + decompressed_size, decompressed_block, decompressed_block_size); |
|
2685 |
+ decompressed_size += decompressed_block_size; |
|
2686 |
+ |
|
2687 |
+ free(decompressed_block); |
|
2688 |
+ |
|
2689 |
+ retval = EGG_OK; |
|
2690 |
+ break; |
|
2691 |
+ } |
|
2692 |
+ default: { |
|
2693 |
+ cli_errmsg("cli_egg_extract_file: unknown compression algorithm: %d!\n", |
|
2694 |
+ currBlock->blockHeader->compress_algorithm); |
|
2695 |
+ goto done; |
|
2696 |
+ } |
|
2697 |
+ } |
|
2698 |
+ |
|
2699 |
+ if (EGG_OK != retval) { |
|
2700 |
+ cli_warnmsg("cli_egg_extract_file: Unable to decompress file: %s\n", |
|
2701 |
+ currFile->filename.name_utf8); |
|
2702 |
+ } |
|
2703 |
+ |
|
2704 |
+ if ((i == currFile->nBlocks - 1) && // last block ? |
|
2705 |
+ (decompressed_size != currFile->file->file_length)) { // right amount of data ? |
|
2706 |
+ cli_warnmsg("cli_egg_extract_file: alleged filesize (%llu) != actual filesize (%llu)!\n", |
|
2707 |
+ currFile->file->file_length, |
|
2708 |
+ decompressed_size); |
|
2709 |
+ } |
|
2710 |
+ } |
|
2711 |
+ } |
|
2712 |
+ |
|
2713 |
+ cli_dbgmsg("cli_egg_extract_file: File extracted: %s\n", currFile->filename.name_utf8); |
|
2714 |
+ *filename = strdup(currFile->filename.name_utf8); |
|
2715 |
+ *output_buffer = decompressed; |
|
2716 |
+ *output_buffer_length = decompressed_size; |
|
2717 |
+ status = EGG_OK; |
|
2718 |
+ |
|
2719 |
+done: |
|
2720 |
+ handle->fileExtractionIndex += 1; |
|
2721 |
+ |
|
2722 |
+ if (EGG_OK != status) { |
|
2723 |
+ /* Free buffer */ |
|
2724 |
+ if (NULL != decompressed) { |
|
2725 |
+ free(decompressed); |
|
2726 |
+ } |
|
2727 |
+ } |
|
2728 |
+ |
|
2729 |
+ return status; |
|
2730 |
+} |
|
2731 |
+ |
|
2732 |
+cl_egg_error_t cli_egg_skip_file(void* hArchive) |
|
2733 |
+{ |
|
2734 |
+ cl_egg_error_t status = EGG_ERR; |
|
2735 |
+ egg_handle* handle = NULL; |
|
2736 |
+ |
|
2737 |
+ if (!hArchive) { |
|
2738 |
+ cli_errmsg("cli_egg_skip_file: Invalid args!\n"); |
|
2739 |
+ return EGG_ERR; |
|
2740 |
+ } |
|
2741 |
+ |
|
2742 |
+ handle = (egg_handle*)hArchive; |
|
2743 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
2744 |
+ cli_errmsg("cli_egg_skip_file: Invalid handle values!\n"); |
|
2745 |
+ goto done; |
|
2746 |
+ } |
|
2747 |
+ |
|
2748 |
+ if (handle->fileExtractionIndex >= handle->nFiles) { |
|
2749 |
+ cli_warnmsg("cli_egg_skip_file: File index exceeds number of files in archive!\n"); |
|
2750 |
+ status = EGG_BREAK; |
|
2751 |
+ goto done; |
|
2752 |
+ } |
|
2753 |
+ |
|
2754 |
+ handle->fileExtractionIndex += 1; |
|
2755 |
+ if (handle->fileExtractionIndex >= handle->nFiles) { |
|
2756 |
+ status = EGG_BREAK; |
|
2757 |
+ } |
|
2758 |
+ |
|
2759 |
+ cli_dbgmsg("cli_egg_skip_file: File skipped.\n"); |
|
2760 |
+ |
|
2761 |
+ status = EGG_OK; |
|
2762 |
+done: |
|
2763 |
+ return status; |
|
2764 |
+} |
|
2765 |
+ |
|
2766 |
+void cli_egg_close(void* hArchive) |
|
2767 |
+{ |
|
2768 |
+ egg_handle* handle = NULL; |
|
2769 |
+ |
|
2770 |
+ if (!hArchive) { |
|
2771 |
+ cli_errmsg("cli_egg_close: Invalid args.\n"); |
|
2772 |
+ return; |
|
2773 |
+ } |
|
2774 |
+ |
|
2775 |
+ handle = (egg_handle*)hArchive; |
|
2776 |
+ if (EGG_OK != EGG_VALIDATE_HANDLE(handle)) { |
|
2777 |
+ cli_errmsg("cli_egg_close: Invalid handle values!\n"); |
|
2778 |
+ return; |
|
2779 |
+ } |
|
2780 |
+ |
|
2781 |
+ egg_free_egg_handle(handle); |
|
2782 |
+ |
|
2783 |
+ return; |
|
2784 |
+} |
0 | 2785 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,58 @@ |
0 |
+/* |
|
1 |
+ * Copyright (C) 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
|
2 |
+ * |
|
3 |
+ * EGG is an archive format created by ESTsoft used by their ALZip |
|
4 |
+ * archiving software. |
|
5 |
+ * |
|
6 |
+ * This software is written from scratch based solely from ESTsoft's |
|
7 |
+ * file format documentation and from testing with EGG format archives. |
|
8 |
+ * ESTsoft's "unEGG" module was not used in the creation of this capability |
|
9 |
+ * in order to avoid to licensing restrictions on the ESTsoft "unEGG" module. |
|
10 |
+ * |
|
11 |
+ * Authors: Micah Snyder |
|
12 |
+ * |
|
13 |
+ * This program is free software; you can redistribute it and/or modify |
|
14 |
+ * it under the terms of the GNU General Public License version 2 as |
|
15 |
+ * published by the Free Software Foundation. |
|
16 |
+ * |
|
17 |
+ * This program is distributed in the hope that it will be useful, |
|
18 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 |
+ * GNU General Public License for more details. |
|
21 |
+ * |
|
22 |
+ * You should have received a copy of the GNU General Public License |
|
23 |
+ * along with this program; if not, write to the Free Software |
|
24 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 |
+ * MA 02110-1301, USA. |
|
26 |
+ */ |
|
27 |
+ |
|
28 |
+#ifndef _EGG_H |
|
29 |
+#define _EGG_H |
|
30 |
+ |
|
31 |
+#include <clamav.h> |
|
32 |
+#include <others.h> |
|
33 |
+ |
|
34 |
+typedef enum { |
|
35 |
+ EGG_OK = 0, |
|
36 |
+ EGG_BREAK, |
|
37 |
+ EGG_ENCRYPTED, |
|
38 |
+ EGG_EMEM, |
|
39 |
+ EGG_ERR |
|
40 |
+} cl_egg_error_t; |
|
41 |
+ |
|
42 |
+typedef struct cl_egg_metadata { |
|
43 |
+ uint64_t pack_size; |
|
44 |
+ uint64_t unpack_size; |
|
45 |
+ char* filename; |
|
46 |
+ struct cl_egg_metadata* next; |
|
47 |
+ unsigned int encrypted; |
|
48 |
+ uint32_t is_dir; |
|
49 |
+} cl_egg_metadata; |
|
50 |
+ |
|
51 |
+cl_egg_error_t cli_egg_open(fmap_t* map, size_t sfx_offset, void** hArchive, char** comment, uint32_t* comment_size); |
|
52 |
+cl_egg_error_t cli_egg_peek_file_header(void* hArchive, cl_egg_metadata* file_metadata); |
|
53 |
+cl_egg_error_t cli_egg_extract_file(void* hArchive, const char** filename, const char** output_buffer, size_t* output_buffer_length); |
|
54 |
+cl_egg_error_t cli_egg_skip_file(void* hArchive); |
|
55 |
+void cli_egg_close(void* hArchive); |
|
56 |
+ |
|
57 |
+#endif // _EGG_H |
... | ... |
@@ -130,6 +130,8 @@ static const struct ftmap_s { |
130 | 130 |
{ "CL_TYPE_PS", CL_TYPE_PS }, |
131 | 131 |
{ "CL_TYPE_MHTML", CL_TYPE_MHTML }, |
132 | 132 |
{ "CL_TYPE_LNK", CL_TYPE_LNK }, |
133 |
+ { "CL_TYPE_EGG", CL_TYPE_EGG }, |
|
134 |
+ { "CL_TYPE_EGGSFX", CL_TYPE_EGGSFX }, |
|
133 | 135 |
{ NULL, CL_TYPE_IGNORED } |
134 | 136 |
}; |
135 | 137 |
// clang-format on |
... | ... |
@@ -87,6 +87,7 @@ typedef enum { |
87 | 87 |
CL_TYPE_HWP3, |
88 | 88 |
CL_TYPE_OOXML_HWP, |
89 | 89 |
CL_TYPE_PS, |
90 |
+ CL_TYPE_EGG, |
|
90 | 91 |
|
91 | 92 |
/* Section for partition types */ |
92 | 93 |
CL_TYPE_PART_ANY, /* unknown partition type */ |
... | ... |
@@ -102,6 +103,7 @@ typedef enum { |
102 | 102 |
CL_TYPE_7ZSFX, |
103 | 103 |
CL_TYPE_CABSFX, |
104 | 104 |
CL_TYPE_ARJSFX, |
105 |
+ CL_TYPE_EGGSFX, |
|
105 | 106 |
CL_TYPE_NULSFT, /* on the fly */ |
106 | 107 |
CL_TYPE_AUTOIT, |
107 | 108 |
CL_TYPE_ISHIELD_MSI, |
... | ... |
@@ -196,6 +196,6 @@ static const char *ftypes_int[] = { |
196 | 196 |
"1:0:(4d|6d)(49|69)(4d|6d)(45|65)2d(56|76)657273696f6e3a20{-1024}0a(43|63)6f6e74656e742d(54|74)7970653a20:MHTML file:CL_TYPE_ANY:CL_TYPE_MHTML:83", |
197 | 197 |
"1:*:0a(4d|6d)(49|69)(4d|6d)(45|65)2d(56|76)657273696f6e3a20{-1024}0a(43|63)6f6e74656e742d(54|74)7970653a20:MHTML file:CL_TYPE_ANY:CL_TYPE_MHTML:83", |
198 | 198 |
"0:0:4C0000000114020000000000C000000000000046:Microsoft Windows Shortcut File:CL_TYPE_ANY:CL_TYPE_LNK:100", |
199 |
+ "0:0:45474741:Egg Archive:CL_TYPE_ANY:CL_TYPE_EGG:100", |
|
199 | 200 |
NULL}; |
200 |
- |
|
201 | 201 |
#endif |
... | ... |
@@ -27,154 +27,13 @@ |
27 | 27 |
#include "others.h" |
28 | 28 |
#include "uniq.h" |
29 | 29 |
|
30 |
-/* Summary and Document Information Parsing to JSON */ |
|
31 |
-#if HAVE_JSON |
|
32 |
- |
|
33 |
-#define PROPCNTLIMIT 25 |
|
34 |
-#define PROPSTRLIMIT 256 /* affects property strs, NOT sanitized strs (may result in a buffer allocating PROPSTRLIMIT*6) */ |
|
35 |
-#define UTF16_MS "UTF-16LE" |
|
36 |
- |
|
37 |
-#define sum16_endian_convert(v) le16_to_host((uint16_t)(v)) |
|
38 |
-#define sum32_endian_convert(v) le32_to_host((uint32_t)(v)) |
|
39 |
-#define sum64_endian_convert(v) le64_to_host((uint64_t)(v)) |
|
40 |
- |
|
41 |
-enum summary_pidsi { |
|
42 |
- SPID_CODEPAGE = 0x00000001, |
|
43 |
- SPID_TITLE = 0x00000002, |
|
44 |
- SPID_SUBJECT = 0x00000003, |
|
45 |
- SPID_AUTHOR = 0x00000004, |
|
46 |
- SPID_KEYWORDS = 0x00000005, |
|
47 |
- SPID_COMMENTS = 0x00000006, |
|
48 |
- SPID_TEMPLATE = 0x00000007, |
|
49 |
- SPID_LASTAUTHOR = 0x00000008, |
|
50 |
- SPID_REVNUMBER = 0x00000009, |
|
51 |
- SPID_EDITTIME = 0x0000000A, |
|
52 |
- SPID_LASTPRINTED = 0x0000000B, |
|
53 |
- SPID_CREATEDTIME = 0x0000000C, |
|
54 |
- SPID_MODIFIEDTIME = 0x0000000D, |
|
55 |
- SPID_PAGECOUNT = 0x0000000E, |
|
56 |
- SPID_WORDCOUNT = 0x0000000F, |
|
57 |
- SPID_CHARCOUNT = 0x00000010, |
|
58 |
- SPID_THUMBNAIL = 0x00000011, |
|
59 |
- SPID_APPNAME = 0x00000012, |
|
60 |
- SPID_SECURITY = 0x00000013 |
|
61 |
-}; |
|
62 |
- |
|
63 |
-enum docsum_pidsi { |
|
64 |
- DSPID_CODEPAGE = 0x00000001, |
|
65 |
- DSPID_CATEGORY = 0x00000002, |
|
66 |
- DSPID_PRESFORMAT = 0x00000003, |
|
67 |
- DSPID_BYTECOUNT = 0x00000004, |
|
68 |
- DSPID_LINECOUNT = 0x00000005, |
|
69 |
- DSPID_PARCOUNT = 0x00000006, |
|
70 |
- DSPID_SLIDECOUNT = 0x00000007, |
|
71 |
- DSPID_NOTECOUNT = 0x00000008, |
|
72 |
- DSPID_HIDDENCOUNT = 0x00000009, |
|
73 |
- DSPID_MMCLIPCOUNT = 0x0000000A, |
|
74 |
- DSPID_SCALE = 0x0000000B, |
|
75 |
- DSPID_HEADINGPAIR = 0x0000000C, /* VT_VARIANT | VT_VECTOR */ |
|
76 |
- DSPID_DOCPARTS = 0x0000000D, /* VT_VECTOR | VT_LPSTR */ |
|
77 |
- DSPID_MANAGER = 0x0000000E, |
|
78 |
- DSPID_COMPANY = 0x0000000F, |
|
79 |
- DSPID_LINKSDIRTY = 0x00000010, |
|
80 |
- DSPID_CCHWITHSPACES = 0x00000011, |
|
81 |
- DSPID_SHAREDDOC = 0x00000013, /* must be false */ |
|
82 |
- DSPID_LINKBASE = 0x00000014, /* moved to user-defined */ |
|
83 |
- DSPID_HLINKS = 0x00000015, /* moved to user-defined */ |
|
84 |
- DSPID_HYPERLINKSCHANGED = 0x00000016, |
|
85 |
- DSPID_VERSION = 0x00000017, |
|
86 |
- DSPID_DIGSIG = 0x00000018, |
|
87 |
- DSPID_CONTENTTYPE = 0x0000001A, |
|
88 |
- DSPID_CONTENTSTATUS = 0x0000001B, |
|
89 |
- DSPID_LANGUAGE = 0x0000001C, |
|
90 |
- DSPID_DOCVERSION = 0x0000001D |
|
91 |
-}; |
|
92 |
- |
|
93 |
-enum property_type { |
|
94 |
- PT_EMPTY = 0x0000, |
|
95 |
- PT_NULL = 0x0001, |
|
96 |
- PT_INT16 = 0x0002, |
|
97 |
- PT_INT32 = 0x0003, |
|
98 |
- PT_FLOAT32 = 0x0004, |
|
99 |
- PT_DOUBLE64 = 0x0005, |
|
100 |
- PT_DATE = 0x0007, |
|
101 |
- PT_BSTR = 0x0008, |
|
102 |
- PT_BOOL = 0x000B, |
|
103 |
- PT_INT8v1 = 0x0010, |
|
104 |
- PT_UINT8 = 0x0011, |
|
105 |
- PT_UINT16 = 0x0012, |
|
106 |
- PT_UINT32 = 0x0013, |
|
107 |
- PT_INT64 = 0x0014, |
|
108 |
- PT_UINT64 = 0x0015, |
|
109 |
- PT_INT32v1 = 0x0016, |
|
110 |
- PT_UINT32v1 = 0x0017, |
|
111 |
- PT_LPSTR = 0x001E, |
|
112 |
- PT_LPWSTR = 0x001F, |
|
113 |
- PT_FILETIME = 0x0040, |
|
114 |
- |
|
115 |
- /* More Types not currently handled */ |
|
116 |
-}; |
|
117 |
- |
|
118 |
-typedef struct summary_stub { |
|
119 |
- uint16_t byte_order; |
|
120 |
- uint16_t version; |
|
121 |
- uint32_t system; /* implementation-specific */ |
|
122 |
- uint8_t CLSID[16]; |
|
123 |
- |
|
124 |
- uint32_t num_propsets; /* 1 or 2 */ |
|
125 |
-} summary_stub_t; |
|
126 |
- |
|
127 |
-typedef struct propset_summary_entry { |
|
128 |
- uint8_t FMTID[16]; |
|
129 |
- uint32_t offset; |
|
130 |
-} propset_entry_t; |
|
131 |
- |
|
132 |
-/* error codes */ |
|
133 |
-#define OLE2_SUMMARY_ERROR_TOOSMALL 0x00000001 |
|
134 |
-#define OLE2_SUMMARY_ERROR_OOB 0x00000002 |
|
135 |
-#define OLE2_SUMMARY_ERROR_DATABUF 0x00000004 |
|
136 |
-#define OLE2_SUMMARY_ERROR_INVALID_ENTRY 0x00000008 |
|
137 |
-#define OLE2_SUMMARY_LIMIT_PROPS 0x00000010 |
|
138 |
-#define OLE2_SUMMARY_FLAG_TIMEOUT 0x00000020 |
|
139 |
-#define OLE2_SUMMARY_FLAG_CODEPAGE 0x00000040 |
|
140 |
-#define OLE2_SUMMARY_FLAG_UNKNOWN_PROPID 0x00000080 |
|
141 |
-#define OLE2_SUMMARY_FLAG_UNHANDLED_PROPTYPE 0x00000100 |
|
142 |
-#define OLE2_SUMMARY_FLAG_TRUNC_STR 0x00000200 |
|
143 |
- |
|
144 |
-#define OLE2_CODEPAGE_ERROR_NOTFOUND 0x00000400 |
|
145 |
-#define OLE2_CODEPAGE_ERROR_UNINITED 0x00000800 |
|
146 |
-#define OLE2_CODEPAGE_ERROR_INVALID 0x00001000 |
|
147 |
-#define OLE2_CODEPAGE_ERROR_INCOMPLETE 0x00002000 |
|
148 |
-#define OLE2_CODEPAGE_ERROR_OUTBUFTOOSMALL 0x00002000 |
|
149 |
- |
|
150 |
-/* metadata structures */ |
|
151 |
-typedef struct summary_ctx { |
|
152 |
- cli_ctx *ctx; |
|
153 |
- int mode; |
|
154 |
- fmap_t *sfmap; |
|
155 |
- json_object *summary; |
|
156 |
- size_t maplen; |
|
157 |
- uint32_t flags; |
|
158 |
- |
|
159 |
- /* propset metadata */ |
|
160 |
- uint32_t pssize; /* track from propset start, not tail start */ |
|
161 |
- uint16_t codepage; |
|
162 |
- int writecp; |
|
163 |
- |
|
164 |
- /* property metadata */ |
|
165 |
- const char *propname; |
|
166 |
- |
|
167 |
- /* timeout meta */ |
|
168 |
- int toval; |
|
169 |
-} summary_ctx_t; |
|
170 |
- |
|
171 | 30 |
/* string conversion */ |
172 | 31 |
struct codepage_entry { |
173 | 32 |
uint16_t codepage; |
174 | 33 |
const char *encoding; |
175 | 34 |
}; |
176 | 35 |
|
177 |
-#define NUMCODEPAGES sizeof(codepage_entries) / sizeof(struct codepage_entry) |
|
36 |
+#define NUMCODEPAGES (sizeof(codepage_entries) / sizeof(struct codepage_entry)) |
|
178 | 37 |
/* MAINTAIN - the array in codepage value sorted order */ |
179 | 38 |
static const struct codepage_entry codepage_entries[] = { |
180 | 39 |
{37, "IBM037"}, /* IBM EBCDIC US-Canada */ |
... | ... |
@@ -204,7 +63,7 @@ static const struct codepage_entry codepage_entries[] = { |
204 | 204 |
{875, "CP875"}, /* IBM EBCDIC Greek Modern */ |
205 | 205 |
{932, "SHIFT_JIS"}, /* ANSI/OEM Japanese; Japanese (Shift-JIS) */ |
206 | 206 |
{936, "GB2312"}, /* ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312) */ |
207 |
- {949, NULL}, /* ANSI/OEM Korean (Unified Hangul Code) */ |
|
207 |
+ {949, "CP949"}, /* ANSI/OEM Korean (Unified Hangul Code) */ |
|
208 | 208 |
{950, "BIG5"}, /* ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5) */ |
209 | 209 |
{1026, "IBM1026"}, /* IBM EBCDIC Turkish (Latin 5) */ |
210 | 210 |
{1047, NULL}, /* IBM EBCDIC Latin 1/Open System */ |
... | ... |
@@ -331,7 +190,149 @@ static const struct codepage_entry codepage_entries[] = { |
331 | 331 |
{65001, "UTF-8"} /* Unicode (UTF-8) */ |
332 | 332 |
}; |
333 | 333 |
|
334 |
+#if HAVE_JSON |
|
335 |
+ |
|
336 |
+#define PROPCNTLIMIT 25 |
|
337 |
+#define PROPSTRLIMIT 256 /* affects property strs, NOT sanitized strs (may result in a buffer allocating PROPSTRLIMIT*6) */ |
|
338 |
+#define UTF16_MS "UTF-16LE" |
|
339 |
+ |
|
340 |
+#define sum16_endian_convert(v) le16_to_host((uint16_t)(v)) |
|
341 |
+#define sum32_endian_convert(v) le32_to_host((uint32_t)(v)) |
|
342 |
+#define sum64_endian_convert(v) le64_to_host((uint64_t)(v)) |
|
343 |
+ |
|
344 |
+enum summary_pidsi { |
|
345 |
+ SPID_CODEPAGE = 0x00000001, |
|
346 |
+ SPID_TITLE = 0x00000002, |
|
347 |
+ SPID_SUBJECT = 0x00000003, |
|
348 |
+ SPID_AUTHOR = 0x00000004, |
|
349 |
+ SPID_KEYWORDS = 0x00000005, |
|
350 |
+ SPID_COMMENTS = 0x00000006, |
|
351 |
+ SPID_TEMPLATE = 0x00000007, |
|
352 |
+ SPID_LASTAUTHOR = 0x00000008, |
|
353 |
+ SPID_REVNUMBER = 0x00000009, |
|
354 |
+ SPID_EDITTIME = 0x0000000A, |
|
355 |
+ SPID_LASTPRINTED = 0x0000000B, |
|
356 |
+ SPID_CREATEDTIME = 0x0000000C, |
|
357 |
+ SPID_MODIFIEDTIME = 0x0000000D, |
|
358 |
+ SPID_PAGECOUNT = 0x0000000E, |
|
359 |
+ SPID_WORDCOUNT = 0x0000000F, |
|
360 |
+ SPID_CHARCOUNT = 0x00000010, |
|
361 |
+ SPID_THUMBNAIL = 0x00000011, |
|
362 |
+ SPID_APPNAME = 0x00000012, |
|
363 |
+ SPID_SECURITY = 0x00000013 |
|
364 |
+}; |
|
365 |
+ |
|
366 |
+enum docsum_pidsi { |
|
367 |
+ DSPID_CODEPAGE = 0x00000001, |
|
368 |
+ DSPID_CATEGORY = 0x00000002, |
|
369 |
+ DSPID_PRESFORMAT = 0x00000003, |
|
370 |
+ DSPID_BYTECOUNT = 0x00000004, |
|
371 |
+ DSPID_LINECOUNT = 0x00000005, |
|
372 |
+ DSPID_PARCOUNT = 0x00000006, |
|
373 |
+ DSPID_SLIDECOUNT = 0x00000007, |
|
374 |
+ DSPID_NOTECOUNT = 0x00000008, |
|
375 |
+ DSPID_HIDDENCOUNT = 0x00000009, |
|
376 |
+ DSPID_MMCLIPCOUNT = 0x0000000A, |
|
377 |
+ DSPID_SCALE = 0x0000000B, |
|
378 |
+ DSPID_HEADINGPAIR = 0x0000000C, /* VT_VARIANT | VT_VECTOR */ |
|
379 |
+ DSPID_DOCPARTS = 0x0000000D, /* VT_VECTOR | VT_LPSTR */ |
|
380 |
+ DSPID_MANAGER = 0x0000000E, |
|
381 |
+ DSPID_COMPANY = 0x0000000F, |
|
382 |
+ DSPID_LINKSDIRTY = 0x00000010, |
|
383 |
+ DSPID_CCHWITHSPACES = 0x00000011, |
|
384 |
+ DSPID_SHAREDDOC = 0x00000013, /* must be false */ |
|
385 |
+ DSPID_LINKBASE = 0x00000014, /* moved to user-defined */ |
|
386 |
+ DSPID_HLINKS = 0x00000015, /* moved to user-defined */ |
|
387 |
+ DSPID_HYPERLINKSCHANGED = 0x00000016, |
|
388 |
+ DSPID_VERSION = 0x00000017, |
|
389 |
+ DSPID_DIGSIG = 0x00000018, |
|
390 |
+ DSPID_CONTENTTYPE = 0x0000001A, |
|
391 |
+ DSPID_CONTENTSTATUS = 0x0000001B, |
|
392 |
+ DSPID_LANGUAGE = 0x0000001C, |
|
393 |
+ DSPID_DOCVERSION = 0x0000001D |
|
394 |
+}; |
|
395 |
+ |
|
396 |
+enum property_type { |
|
397 |
+ PT_EMPTY = 0x0000, |
|
398 |
+ PT_NULL = 0x0001, |
|
399 |
+ PT_INT16 = 0x0002, |
|
400 |
+ PT_INT32 = 0x0003, |
|
401 |
+ PT_FLOAT32 = 0x0004, |
|
402 |
+ PT_DOUBLE64 = 0x0005, |
|
403 |
+ PT_DATE = 0x0007, |
|
404 |
+ PT_BSTR = 0x0008, |
|
405 |
+ PT_BOOL = 0x000B, |
|
406 |
+ PT_INT8v1 = 0x0010, |
|
407 |
+ PT_UINT8 = 0x0011, |
|
408 |
+ PT_UINT16 = 0x0012, |
|
409 |
+ PT_UINT32 = 0x0013, |
|
410 |
+ PT_INT64 = 0x0014, |
|
411 |
+ PT_UINT64 = 0x0015, |
|
412 |
+ PT_INT32v1 = 0x0016, |
|
413 |
+ PT_UINT32v1 = 0x0017, |
|
414 |
+ PT_LPSTR = 0x001E, |
|
415 |
+ PT_LPWSTR = 0x001F, |
|
416 |
+ PT_FILETIME = 0x0040, |
|
417 |
+ |
|
418 |
+ /* More Types not currently handled */ |
|
419 |
+}; |
|
420 |
+ |
|
421 |
+typedef struct summary_stub { |
|
422 |
+ uint16_t byte_order; |
|
423 |
+ uint16_t version; |
|
424 |
+ uint32_t system; /* implementation-specific */ |
|
425 |
+ uint8_t CLSID[16]; |
|
426 |
+ |
|
427 |
+ uint32_t num_propsets; /* 1 or 2 */ |
|
428 |
+} summary_stub_t; |
|
429 |
+ |
|
430 |
+typedef struct propset_summary_entry { |
|
431 |
+ uint8_t FMTID[16]; |
|
432 |
+ uint32_t offset; |
|
433 |
+} propset_entry_t; |
|
434 |
+ |
|
435 |
+/* error codes */ |
|
436 |
+#define OLE2_SUMMARY_ERROR_TOOSMALL 0x00000001 |
|
437 |
+#define OLE2_SUMMARY_ERROR_OOB 0x00000002 |
|
438 |
+#define OLE2_SUMMARY_ERROR_DATABUF 0x00000004 |
|
439 |
+#define OLE2_SUMMARY_ERROR_INVALID_ENTRY 0x00000008 |
|
440 |
+#define OLE2_SUMMARY_LIMIT_PROPS 0x00000010 |
|
441 |
+#define OLE2_SUMMARY_FLAG_TIMEOUT 0x00000020 |
|
442 |
+#define OLE2_SUMMARY_FLAG_CODEPAGE 0x00000040 |
|
443 |
+#define OLE2_SUMMARY_FLAG_UNKNOWN_PROPID 0x00000080 |
|
444 |
+#define OLE2_SUMMARY_FLAG_UNHANDLED_PROPTYPE 0x00000100 |
|
445 |
+#define OLE2_SUMMARY_FLAG_TRUNC_STR 0x00000200 |
|
446 |
+ |
|
447 |
+#define OLE2_CODEPAGE_ERROR_NOTFOUND 0x00000400 |
|
448 |
+#define OLE2_CODEPAGE_ERROR_UNINITED 0x00000800 |
|
449 |
+#define OLE2_CODEPAGE_ERROR_INVALID 0x00001000 |
|
450 |
+#define OLE2_CODEPAGE_ERROR_INCOMPLETE 0x00002000 |
|
451 |
+#define OLE2_CODEPAGE_ERROR_OUTBUFTOOSMALL 0x00002000 |
|
452 |
+ |
|
453 |
+/* metadata structures */ |
|
454 |
+typedef struct summary_ctx { |
|
455 |
+ cli_ctx *ctx; |
|
456 |
+ int mode; |
|
457 |
+ fmap_t *sfmap; |
|
458 |
+ json_object *summary; |
|
459 |
+ size_t maplen; |
|
460 |
+ uint32_t flags; |
|
461 |
+ |
|
462 |
+ /* propset metadata */ |
|
463 |
+ uint32_t pssize; /* track from propset start, not tail start */ |
|
464 |
+ uint16_t codepage; |
|
465 |
+ int writecp; |
|
466 |
+ |
|
467 |
+ /* property metadata */ |
|
468 |
+ const char *propname; |
|
469 |
+ |
|
470 |
+ /* timeout meta */ |
|
471 |
+ int toval; |
|
472 |
+} summary_ctx_t; |
|
473 |
+ |
|
474 |
+/* Summary and Document Information Parsing to JSON */ |
|
334 | 475 |
int cli_ole2_summary_json(cli_ctx *ctx, int fd, int mode); |
476 |
+ |
|
335 | 477 |
#endif /* HAVE_JSON */ |
336 | 478 |
|
337 | 479 |
#endif /* __MSDOC_H_ */ |
... | ... |
@@ -110,6 +110,7 @@ |
110 | 110 |
#include "hwp.h" |
111 | 111 |
#include "msdoc.h" |
112 | 112 |
#include "execs.h" |
113 |
+#include "egg.h" |
|
113 | 114 |
|
114 | 115 |
#ifdef HAVE_BZLIB_H |
115 | 116 |
#include <bzlib.h> |
... | ... |
@@ -573,6 +574,398 @@ done: |
573 | 573 |
return status; |
574 | 574 |
} |
575 | 575 |
|
576 |
+/** |
|
577 |
+ * @brief Scan the metadata using cli_matchmeta() |
|
578 |
+ * |
|
579 |
+ * @param metadata egg metadata structure |
|
580 |
+ * @param ctx scanning context structure |
|
581 |
+ * @param files number of files |
|
582 |
+ * @return cl_error_t Returns CL_CLEAN if nothing found, CL_VIRUS if something found, CL_EUNPACK if encrypted. |
|
583 |
+ */ |
|
584 |
+static cl_error_t cli_egg_scanmetadata(cl_egg_metadata *metadata, cli_ctx *ctx, unsigned int files) |
|
585 |
+{ |
|
586 |
+ cl_error_t status = CL_CLEAN; |
|
587 |
+ int virus_found = 0; |
|
588 |
+ |
|
589 |
+ cli_dbgmsg("EGG: %s, encrypted: %u, compressed: %u, normal: %u, ratio: %u\n", |
|
590 |
+ metadata->filename, metadata->encrypted, (unsigned int)metadata->pack_size, |
|
591 |
+ (unsigned int)metadata->unpack_size, |
|
592 |
+ metadata->pack_size ? (unsigned int)(metadata->unpack_size / metadata->pack_size) : 0); |
|
593 |
+ |
|
594 |
+ if (CL_VIRUS == cli_matchmeta(ctx, metadata->filename, metadata->pack_size, metadata->unpack_size, metadata->encrypted, files, 0, NULL)) { |
|
595 |
+ status = CL_VIRUS; |
|
596 |
+ } else if (SCAN_HEURISTIC_ENCRYPTED_ARCHIVE && metadata->encrypted) { |
|
597 |
+ cli_dbgmsg("EGG: Encrypted files found in archive.\n"); |
|
598 |
+ status = CL_EUNPACK; |
|
599 |
+ } |
|
600 |
+ |
|
601 |
+done: |
|
602 |
+ |
|
603 |
+ return status; |
|
604 |
+} |
|
605 |
+ |
|
606 |
+static cl_error_t cli_scanegg(cli_ctx *ctx, size_t sfx_offset) |
|
607 |
+{ |
|
608 |
+ cl_error_t status = CL_EPARSE; |
|
609 |
+ cl_egg_error_t egg_ret = EGG_ERR; |
|
610 |
+ |
|
611 |
+ char *buffer = NULL; |
|
612 |
+ size_t buffer_len = 0; |
|
613 |
+ |
|
614 |
+ char *extract_dir = NULL; /* temp dir to write extracted files to */ |
|
615 |
+ unsigned int file_count = 0; |
|
616 |
+ unsigned int viruses_found = 0; |
|
617 |
+ |
|
618 |
+ uint32_t nEncryptedFilesFound = 0; |
|
619 |
+ uint32_t nTooLargeFilesFound = 0; |
|
620 |
+ |
|
621 |
+ void *hArchive = NULL; |
|
622 |
+ |
|
623 |
+ char *comment = NULL; |
|
624 |
+ uint32_t comment_size = 0; |
|
625 |
+ |
|
626 |
+ cl_egg_metadata metadata; |
|
627 |
+ char *filename_base = NULL; |
|
628 |
+ char *extract_fullpath = NULL; |
|
629 |
+ char *comment_fullpath = NULL; |
|
630 |
+ |
|
631 |
+ if (ctx == NULL) { |
|
632 |
+ cli_dbgmsg("EGG: Invalid arguments!\n"); |
|
633 |
+ return CL_EARG; |
|
634 |
+ } |
|
635 |
+ |
|
636 |
+ cli_dbgmsg("in scanegg()\n"); |
|
637 |
+ |
|
638 |
+ /* Zero out the metadata struct before we read the header */ |
|
639 |
+ memset(&metadata, 0, sizeof(cl_egg_metadata)); |
|
640 |
+ |
|
641 |
+ /* Determine file basename */ |
|
642 |
+ if (NULL != ctx->sub_filepath) { |
|
643 |
+ if (CL_SUCCESS != cli_basename(ctx->sub_filepath, strlen(ctx->sub_filepath), &filename_base)) { |
|
644 |
+ status = CL_EARG; |
|
645 |
+ goto done; |
|
646 |
+ } |
|
647 |
+ } |
|
648 |
+ |
|
649 |
+ if (ctx->engine->keeptmp) { |
|
650 |
+ /* generate the temporary directory for extracted files. */ |
|
651 |
+ if (!(extract_dir = cli_gentemp_with_prefix(ctx->engine->tmpdir, filename_base))) { |
|
652 |
+ status = CL_EMEM; |
|
653 |
+ goto done; |
|
654 |
+ } |
|
655 |
+ if (mkdir(extract_dir, 0700)) { |
|
656 |
+ cli_dbgmsg("EGG: Can't create temporary directory for extracted files %s\n", extract_dir); |
|
657 |
+ status = CL_ETMPDIR; |
|
658 |
+ goto done; |
|
659 |
+ } |
|
660 |
+ } |
|
661 |
+ /* |
|
662 |
+ * Open the archive. |
|
663 |
+ */ |
|
664 |
+ if (CL_SUCCESS != (egg_ret = cli_egg_open(*ctx->fmap, sfx_offset, &hArchive, &comment, &comment_size))) { |
|
665 |
+ if (egg_ret == EGG_ENCRYPTED) { |
|
666 |
+ cli_dbgmsg("EGG: Encrypted main header\n"); |
|
667 |
+ status = CL_EUNPACK; |
|
668 |
+ goto done; |
|
669 |
+ } |
|
670 |
+ if (egg_ret == EGG_EMEM) { |
|
671 |
+ status = CL_EMEM; |
|
672 |
+ goto done; |
|
673 |
+ } else { |
|
674 |
+ status = CL_EFORMAT; |
|
675 |
+ goto done; |
|
676 |
+ } |
|
677 |
+ } |
|
678 |
+ |
|
679 |
+ /* If the archive header had a comment, write it to the comment dir. */ |
|
680 |
+ if ((comment != NULL) && (comment_size > 0)) { |
|
681 |
+ /* |
|
682 |
+ * Drop the comment to a temp file, if requested |
|
683 |
+ */ |
|
684 |
+ if (ctx->engine->keeptmp) { |
|
685 |
+ int comment_fd = -1; |
|
686 |
+ if (!(comment_fullpath = cli_gentemp_with_prefix(extract_dir, "comments"))) { |
|
687 |
+ status = CL_EMEM; |
|
688 |
+ goto done; |
|
689 |
+ } |
|
690 |
+ |
|
691 |
+ comment_fd = open(comment_fullpath, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600); |
|
692 |
+ if (comment_fd < 0) { |
|
693 |
+ cli_dbgmsg("EGG: ERROR: Failed to open output file\n"); |
|
694 |
+ } else { |
|
695 |
+ cli_dbgmsg("EGG: Writing the archive comment to temp file: %s\n", comment_fullpath); |
|
696 |
+ if (0 == write(comment_fd, comment, comment_size)) { |
|
697 |
+ cli_dbgmsg("EGG: ERROR: Failed to write to output file\n"); |
|
698 |
+ } else { |
|
699 |
+ close(comment_fd); |
|
700 |
+ comment_fd = -1; |
|
701 |
+ } |
|
702 |
+ } |
|
703 |
+ } |
|
704 |
+ |
|
705 |
+ /* |
|
706 |
+ * Scan the comment. |
|
707 |
+ */ |
|
708 |
+ status = cli_mem_scandesc(comment, comment_size, ctx); |
|
709 |
+ |
|
710 |
+ if ((status == CL_VIRUS) && SCAN_ALLMATCHES) { |
|
711 |
+ status = CL_CLEAN; |
|
712 |
+ viruses_found++; |
|
713 |
+ } |
|
714 |
+ if ((status == CL_VIRUS) || (status == CL_BREAK)) { |
|
715 |
+ goto done; |
|
716 |
+ } |
|
717 |
+ } |
|
718 |
+ |
|
719 |
+ /* |
|
720 |
+ * Read & scan each file header. |
|
721 |
+ * Extract & scan each file. |
|
722 |
+ * |
|
723 |
+ * Skip files if they will exceed max filesize or max scansize. |
|
724 |
+ * Count the number of encrypted file headers and encrypted files. |
|
725 |
+ * - Alert if there are encrypted files, |
|
726 |
+ * if the Heuristic for encrypted archives is enabled, |
|
727 |
+ * and if we have not detected a signature match. |
|
728 |
+ */ |
|
729 |
+ do { |
|
730 |
+ status = CL_CLEAN; |
|
731 |
+ |
|
732 |
+ /* Zero out the metadata struct before we read the header */ |
|
733 |
+ memset(&metadata, 0, sizeof(unrar_metadata_t)); |
|
734 |
+ |
|
735 |
+ /* |
|
736 |
+ * Get the header information for the next file in the archive. |
|
737 |
+ */ |
|
738 |
+ egg_ret = cli_egg_peek_file_header(hArchive, &metadata); |
|
739 |
+ if (egg_ret != EGG_OK) { |
|
740 |
+ if (egg_ret == EGG_ENCRYPTED) { |
|
741 |
+ /* Found an encrypted file header, must skip. */ |
|
742 |
+ cli_dbgmsg("EGG: Encrypted file header, unable to reading file metadata and file contents. Skipping file...\n"); |
|
743 |
+ nEncryptedFilesFound += 1; |
|
744 |
+ |
|
745 |
+ if (EGG_OK != cli_egg_skip_file(hArchive)) { |
|
746 |
+ /* Failed to skip! Break extraction loop. */ |
|
747 |
+ cli_dbgmsg("EGG: Failed to skip file. EGG archive extraction has failed.\n"); |
|
748 |
+ break; |
|
749 |
+ } |
|
750 |
+ } else if (egg_ret == EGG_BREAK) { |
|
751 |
+ /* No more files. Break extraction loop. */ |
|
752 |
+ cli_dbgmsg("EGG: No more files in archive.\n"); |
|
753 |
+ break; |
|
754 |
+ } else { |
|
755 |
+ /* Memory error or some other error reading the header info. */ |
|
756 |
+ cli_dbgmsg("EGG: Error (%u) reading file header!\n", egg_ret); |
|
757 |
+ break; |
|
758 |
+ } |
|
759 |
+ } else { |
|
760 |
+ file_count += 1; |
|
761 |
+ |
|
762 |
+ /* |
|
763 |
+ * Scan the metadata for the file in question since the content was clean, or we're running in all-match. |
|
764 |
+ */ |
|
765 |
+ status = cli_egg_scanmetadata(&metadata, ctx, file_count); |
|
766 |
+ if ((status == CL_VIRUS) && SCAN_ALLMATCHES) { |
|
767 |
+ status = CL_CLEAN; |
|
768 |
+ viruses_found++; |
|
769 |
+ } |
|
770 |
+ if ((status == CL_VIRUS) || (status == CL_BREAK)) { |
|
771 |
+ break; |
|
772 |
+ } |
|
773 |
+ |
|
774 |
+ /* Check if we've already exceeded the scan limit */ |
|
775 |
+ if (cli_checklimits("EGG", ctx, 0, 0, 0)) |
|
776 |
+ break; |
|
777 |
+ |
|
778 |
+ if (metadata.is_dir) { |
|
779 |
+ /* Entry is a directory. Skip. */ |
|
780 |
+ cli_dbgmsg("EGG: Found directory. Skipping to next file.\n"); |
|
781 |
+ |
|
782 |
+ if (EGG_OK != cli_egg_skip_file(hArchive)) { |
|
783 |
+ /* Failed to skip! Break extraction loop. */ |
|
784 |
+ cli_dbgmsg("EGG: Failed to skip directory. EGG archive extraction has failed.\n"); |
|
785 |
+ break; |
|
786 |
+ } |
|
787 |
+ } else if (cli_checklimits("EGG", ctx, metadata.unpack_size, 0, 0)) { |
|
788 |
+ /* File size exceeds maxfilesize, must skip extraction. |
|
789 |
+ * Although we may be able to scan the metadata */ |
|
790 |
+ nTooLargeFilesFound += 1; |
|
791 |
+ |
|
792 |
+ cli_dbgmsg("EGG: Next file is too large (%" PRIu64 " bytes); it would exceed max scansize. Skipping to next file.\n", metadata.unpack_size); |
|
793 |
+ |
|
794 |
+ if (EGG_OK != cli_egg_skip_file(hArchive)) { |
|
795 |
+ /* Failed to skip! Break extraction loop. */ |
|
796 |
+ cli_dbgmsg("EGG: Failed to skip file. EGG archive extraction has failed.\n"); |
|
797 |
+ break; |
|
798 |
+ } |
|
799 |
+ } else if (metadata.encrypted != 0) { |
|
800 |
+ /* Found an encrypted file, must skip. */ |
|
801 |
+ cli_dbgmsg("EGG: Encrypted file, unable to extract file contents. Skipping file...\n"); |
|
802 |
+ nEncryptedFilesFound += 1; |
|
803 |
+ |
|
804 |
+ if (EGG_OK != cli_egg_skip_file(hArchive)) { |
|
805 |
+ /* Failed to skip! Break extraction loop. */ |
|
806 |
+ cli_dbgmsg("EGG: Failed to skip file. EGG archive extraction has failed.\n"); |
|
807 |
+ break; |
|
808 |
+ } |
|
809 |
+ } else { |
|
810 |
+ /* |
|
811 |
+ * Extract the file... |
|
812 |
+ */ |
|
813 |
+ char *extract_filename = NULL; |
|
814 |
+ char *extract_buffer = NULL; |
|
815 |
+ size_t extract_buffer_len = 0; |
|
816 |
+ |
|
817 |
+ cli_dbgmsg("EGG: Extracting file: %s\n", metadata.filename); |
|
818 |
+ |
|
819 |
+ egg_ret = cli_egg_extract_file(hArchive, (const char **)&extract_filename, (const char **)&extract_buffer, &extract_buffer_len); |
|
820 |
+ if (egg_ret != EGG_OK) { |
|
821 |
+ /* |
|
822 |
+ * Some other error extracting the file |
|
823 |
+ */ |
|
824 |
+ cli_dbgmsg("EGG: Error extracting file: %s\n", metadata.filename); |
|
825 |
+ } else if (!extract_buffer || 0 == extract_buffer_len) { |
|
826 |
+ /* |
|
827 |
+ * Empty file. Skip. |
|
828 |
+ */ |
|
829 |
+ cli_dbgmsg("EGG: Skipping empty file: %s\n", metadata.filename); |
|
830 |
+ } else { |
|
831 |
+ /* |
|
832 |
+ * Drop to a temp file, if requested. |
|
833 |
+ */ |
|
834 |
+ if (ctx->engine->keeptmp) { |
|
835 |
+ int extracted_fd = -1; |
|
836 |
+ if (!(extract_fullpath = cli_gentemp(extract_dir))) { |
|
837 |
+ status = CL_EMEM; |
|
838 |
+ break; |
|
839 |
+ } |
|
840 |
+ |
|
841 |
+ extracted_fd = open(extract_fullpath, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600); |
|
842 |
+ if (extracted_fd < 0) { |
|
843 |
+ cli_dbgmsg("EGG: ERROR: Failed to open output file\n"); |
|
844 |
+ } else { |
|
845 |
+ cli_dbgmsg("EGG: Writing the extracted file contents to temp file: %s\n", extract_fullpath); |
|
846 |
+ if (0 == write(extracted_fd, extract_buffer, extract_buffer_len)) { |
|
847 |
+ cli_dbgmsg("EGG: ERROR: Failed to write to output file\n"); |
|
848 |
+ } else { |
|
849 |
+ close(extracted_fd); |
|
850 |
+ extracted_fd = -1; |
|
851 |
+ } |
|
852 |
+ } |
|
853 |
+ } |
|
854 |
+ |
|
855 |
+ /* |
|
856 |
+ * Scan the extracted file (buffer)... |
|
857 |
+ */ |
|
858 |
+ cli_dbgmsg("EGG: Extraction complete. Scanning now...\n"); |
|
859 |
+ status = cli_mem_scandesc(extract_buffer, extract_buffer_len, ctx); |
|
860 |
+ if (status == CL_VIRUS) { |
|
861 |
+ cli_dbgmsg("EGG: infected with %s\n", cli_get_last_virus(ctx)); |
|
862 |
+ status = CL_VIRUS; |
|
863 |
+ viruses_found++; |
|
864 |
+ } |
|
865 |
+ |
|
866 |
+ if (NULL != extract_filename) { |
|
867 |
+ free(extract_filename); |
|
868 |
+ extract_filename = NULL; |
|
869 |
+ } |
|
870 |
+ if (NULL != extract_buffer) { |
|
871 |
+ free(extract_buffer); |
|
872 |
+ extract_buffer = NULL; |
|
873 |
+ } |
|
874 |
+ } |
|
875 |
+ |
|
876 |
+ /* Free up that the filepath */ |
|
877 |
+ if (NULL != extract_fullpath) { |
|
878 |
+ free(extract_fullpath); |
|
879 |
+ extract_fullpath = NULL; |
|
880 |
+ } |
|
881 |
+ } |
|
882 |
+ } |
|
883 |
+ |
|
884 |
+ if (status == CL_VIRUS) { |
|
885 |
+ if (SCAN_ALLMATCHES) |
|
886 |
+ status = CL_SUCCESS; |
|
887 |
+ else |
|
888 |
+ break; |
|
889 |
+ } |
|
890 |
+ |
|
891 |
+ if (ctx->engine->maxscansize && ctx->scansize >= ctx->engine->maxscansize) { |
|
892 |
+ status = CL_CLEAN; |
|
893 |
+ break; |
|
894 |
+ } |
|
895 |
+ |
|
896 |
+ /* |
|
897 |
+ * TODO: Free up any malloced metadata... |
|
898 |
+ */ |
|
899 |
+ if (metadata.filename != NULL) { |
|
900 |
+ free(metadata.filename); |
|
901 |
+ metadata.filename = NULL; |
|
902 |
+ } |
|
903 |
+ |
|
904 |
+ } while (status == CL_CLEAN); |
|
905 |
+ |
|
906 |
+ if (status == CL_BREAK) |
|
907 |
+ status = CL_CLEAN; |
|
908 |
+ |
|
909 |
+done: |
|
910 |
+ if (NULL != comment) { |
|
911 |
+ free(comment); |
|
912 |
+ comment = NULL; |
|
913 |
+ } |
|
914 |
+ |
|
915 |
+ if (NULL != comment_fullpath) { |
|
916 |
+ free(comment_fullpath); |
|
917 |
+ comment_fullpath = NULL; |
|
918 |
+ } |
|
919 |
+ |
|
920 |
+ if (NULL != hArchive) { |
|
921 |
+ cli_egg_close(hArchive); |
|
922 |
+ hArchive = NULL; |
|
923 |
+ } |
|
924 |
+ |
|
925 |
+ if (NULL != filename_base) { |
|
926 |
+ free(filename_base); |
|
927 |
+ filename_base = NULL; |
|
928 |
+ } |
|
929 |
+ |
|
930 |
+ if (metadata.filename != NULL) { |
|
931 |
+ free(metadata.filename); |
|
932 |
+ metadata.filename = NULL; |
|
933 |
+ } |
|
934 |
+ |
|
935 |
+ if (NULL != extract_fullpath) { |
|
936 |
+ free(extract_fullpath); |
|
937 |
+ extract_fullpath = NULL; |
|
938 |
+ } |
|
939 |
+ |
|
940 |
+ if (NULL != extract_dir) { |
|
941 |
+ free(extract_dir); |
|
942 |
+ extract_dir = NULL; |
|
943 |
+ } |
|
944 |
+ |
|
945 |
+ /* If return value was a failure due to encryption, scan the un-extracted archive just in case... */ |
|
946 |
+ if ((CL_VIRUS != status) && ((CL_EUNPACK == status) || (nEncryptedFilesFound > 0))) { |
|
947 |
+ status = cli_mem_scandesc(buffer, buffer_len, ctx); |
|
948 |
+ |
|
949 |
+ /* If no virus, and user requests enabled the Heuristic for encrypted archives... */ |
|
950 |
+ if ((status != CL_VIRUS) && SCAN_HEURISTIC_ENCRYPTED_ARCHIVE) { |
|
951 |
+ if (CL_VIRUS == cli_append_virus(ctx, "Heuristics.Encrypted.EGG")) { |
|
952 |
+ status = CL_VIRUS; |
|
953 |
+ } |
|
954 |
+ } |
|
955 |
+ if (status != CL_VIRUS) { |
|
956 |
+ status = CL_CLEAN; |
|
957 |
+ } |
|
958 |
+ } |
|
959 |
+ |
|
960 |
+ cli_dbgmsg("EGG: Exit code: %d\n", status); |
|
961 |
+ |
|
962 |
+ if (SCAN_ALLMATCHES && viruses_found) |
|
963 |
+ status = CL_VIRUS; |
|
964 |
+ |
|
965 |
+ return status; |
|
966 |
+} |
|
967 |
+ |
|
576 | 968 |
static int cli_scanarj(cli_ctx *ctx, off_t sfx_offset) |
577 | 969 |
{ |
578 | 970 |
int ret = CL_CLEAN, rc, file = 0; |
... | ... |
@@ -2500,6 +2893,15 @@ static int cli_scanraw(cli_ctx *ctx, cli_file_t type, uint8_t typercg, cli_file_ |
2500 | 2500 |
} |
2501 | 2501 |
break; |
2502 | 2502 |
|
2503 |
+ case CL_TYPE_EGGSFX: |
|
2504 |
+ if (type != CL_TYPE_EGG && SCAN_PARSE_ARCHIVE && (DCONF_ARCH & ARCH_CONF_EGG)) { |
|
2505 |
+ size_t csize = map->len - fpt->offset; /* not precise */ |
|
2506 |
+ cli_set_container(ctx, CL_TYPE_EGG, csize); |
|
2507 |
+ cli_dbgmsg("EGG/EGG-SFX signature found at %u\n", (unsigned int)fpt->offset); |
|
2508 |
+ nret = cli_scanegg(ctx, fpt->offset); |
|
2509 |
+ } |
|
2510 |
+ break; |
|
2511 |
+ |
|
2503 | 2512 |
case CL_TYPE_ZIPSFX: |
2504 | 2513 |
if (type != CL_TYPE_ZIP && SCAN_PARSE_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ZIP)) { |
2505 | 2514 |
size_t csize = map->len - fpt->offset; /* not precise */ |
... | ... |
@@ -3135,6 +3537,11 @@ static int magic_scandesc(cli_ctx *ctx, cli_file_t type) |
3135 | 3135 |
} |
3136 | 3136 |
break; |
3137 | 3137 |
|
3138 |
+ case CL_TYPE_EGG: |
|
3139 |
+ if (SCAN_PARSE_ARCHIVE && (DCONF_ARCH & ARCH_CONF_EGG)) |
|
3140 |
+ ret = cli_scanegg(ctx, 0); |
|
3141 |
+ break; |
|
3142 |
+ |
|
3138 | 3143 |
case CL_TYPE_OOXML_WORD: |
3139 | 3144 |
case CL_TYPE_OOXML_PPT: |
3140 | 3145 |
case CL_TYPE_OOXML_XL: |
... | ... |
@@ -1,52 +1,123 @@ |
1 |
-dnl we need to try to link with iconv, otherwise there could be a |
|
1 |
+dnl we need to try to link with iconv, otherwise there could be a |
|
2 | 2 |
dnl mismatch between a 32-bit and 64-bit lib. Detect this at configure time. |
3 | 3 |
dnl we need to check after zlib/bzip2, because they can change the include path |
4 |
-AC_ARG_WITH([iconv], [AS_HELP_STRING([--with-iconv], [supports iconv() @<:@default=auto@:>@])], |
|
5 |
-[ |
|
6 |
- case "$withval" in |
|
7 |
- yes|no) wiconv="$withval";; |
|
8 |
- *) AC_MSG_ERROR([--with-iconv does not take an argument]);; |
|
9 |
- esac], |
|
10 |
-[ wiconv=auto ]) |
|
11 |
-if test "X$wiconv" != "Xno"; then |
|
12 |
- save_LDFLAGS="$LDFLAGS" |
|
4 |
+ |
|
5 |
+want_iconv="sure" |
|
6 |
+have_iconv="no" |
|
7 |
+have_iconv_lib="no" |
|
8 |
+ |
|
9 |
+save_LDFLAGS="$LDFLAGS" |
|
10 |
+save_LIBS="$LIBS" |
|
11 |
+save_CPPFLAGS="$CPPFLAGS" |
|
12 |
+ |
|
13 |
+ICONV_HOME="" |
|
14 |
+ |
|
15 |
+AC_ARG_WITH( |
|
16 |
+ [iconv], |
|
17 |
+ [AS_HELP_STRING([--with-iconv@<:@=DIR@:>@], [path to directory containing libiconv |
|
18 |
+ @<:@default=/usr/local or /usr if not found in /usr/local@:>@])], |
|
19 |
+ [ |
|
20 |
+ if test "X$withval" = "Xno"; then |
|
21 |
+ want_iconv="no" |
|
22 |
+ else |
|
23 |
+ want_iconv="yes" |
|
24 |
+ |
|
25 |
+ if test "X$withval" = "Xyes"; then |
|
26 |
+ find_iconv="yes" |
|
27 |
+ else |
|
28 |
+ LDFLAGS="-L${withval}/lib -liconv" |
|
29 |
+ AC_CHECK_LIB( |
|
30 |
+ [iconv], |
|
31 |
+ [libiconv_open], |
|
32 |
+ [ |
|
33 |
+ ICONV_HOME="${withval}" |
|
34 |
+ have_iconv_lib="yes" |
|
35 |
+ ], |
|
36 |
+ [ |
|
37 |
+ AC_MSG_ERROR([Failed to find iconv (libiconv) in ${withval}]) |
|
38 |
+ ]) |
|
39 |
+ fi |
|
40 |
+ fi |
|
41 |
+ ], |
|
42 |
+ [ |
|
43 |
+ find_iconv="yes" |
|
44 |
+ ]) |
|
45 |
+ |
|
46 |
+if test "X$want_iconv" != "Xno"; then |
|
47 |
+ if test "X$find_iconv" = "Xyes"; then |
|
13 | 48 |
LDFLAGS="-L/usr/local/lib -liconv" |
14 |
- ICONV_HOME="" |
|
15 |
- AC_CHECK_LIB([iconv], [libiconv_open], [ICONV_HOME="/usr/local"], |
|
16 |
- [ |
|
17 |
- LDFLAGS="-L/usr/lib -liconv" |
|
18 |
- AC_CHECK_LIB([iconv], [libiconv_open], [ICONV_HOME="/usr"], [LDFLAGS="$save_LDFLAGS"]) |
|
19 |
- ]) |
|
20 |
- AC_MSG_CHECKING([for iconv]) |
|
21 |
- save_LIBS="$LIBS" |
|
22 |
- save_CPPFLAGS="$CPPFLAGS" |
|
49 |
+ AC_CHECK_LIB( |
|
50 |
+ [iconv], |
|
51 |
+ [libiconv_open], |
|
52 |
+ [ |
|
53 |
+ ICONV_HOME="/usr/local" |
|
54 |
+ have_iconv_lib="yes" |
|
55 |
+ ], |
|
56 |
+ [ |
|
57 |
+ LDFLAGS="-L/usr/lib -liconv" |
|
58 |
+ AC_CHECK_LIB( |
|
59 |
+ [iconv], |
|
60 |
+ [libiconv_open], |
|
61 |
+ [ |
|
62 |
+ ICONV_HOME="/usr" |
|
63 |
+ have_iconv_lib="yes" |
|
64 |
+ ], |
|
65 |
+ [ |
|
66 |
+ if test "X$want_iconv" = "Xyes"; then |
|
67 |
+ AC_MSG_ERROR([Failed to find iconv (libiconv) in /usr or /usr/local]) |
|
68 |
+ fi |
|
69 |
+ ]) |
|
70 |
+ ]) |
|
71 |
+ fi |
|
72 |
+ |
|
73 |
+ if test "X$have_iconv_lib" = "Xyes"; then |
|
23 | 74 |
LIBS="$LIBCLAMAV_LIBS" |
24 | 75 |
if test "X$ICONV_HOME" != "X"; then |
25 |
- ICONV_LDFLAGS="$LDFLAGS" |
|
26 |
- ICONV_CPPFLAGS="-I$ICONV_HOME/include" |
|
76 |
+ ICONV_LDFLAGS="$LDFLAGS" |
|
77 |
+ ICONV_CPPFLAGS="-I$ICONV_HOME/include" |
|
27 | 78 |
else |
28 |
- ICONV_LDFLAGS="" |
|
29 |
- ICONV_CPPFLAGS="" |
|
79 |
+ ICONV_LDFLAGS="" |
|
80 |
+ ICONV_CPPFLAGS="" |
|
30 | 81 |
fi |
31 | 82 |
CPPFLAGS="$ICONV_CPPFLAGS $LIBCLAMAV_CPPFLAGS" |
32 |
- AC_TRY_LINK([ |
|
33 |
- #include <iconv.h> |
|
34 |
- ],[ |
|
35 |
- char** xin,**xout; |
|
36 |
- unsigned long il,ol; |
|
37 |
- int rc; |
|
38 |
- iconv_t iconv_struct = iconv_open("UTF-16BE","UTF-8"); |
|
39 |
- rc = iconv(iconv_struct,xin,&il,xout,&ol); |
|
40 |
- iconv_close(iconv_struct); |
|
41 |
- ],[ |
|
42 |
- AC_MSG_RESULT(yes) |
|
43 |
- AC_DEFINE([HAVE_ICONV], 1, [iconv() available]) |
|
44 |
- AC_SUBST(ICONV_LDFLAGS) |
|
45 |
- AC_SUBST(ICONV_CPPFLAGS) |
|
46 |
- ],[ |
|
47 |
- AC_MSG_RESULT(no) |
|
48 |
- ]) |
|
49 |
- LIBS="$save_LIBS" |
|
50 |
- LDFLAGS="$save_LDFLAGS" |
|
51 |
- CPPFLAGS="$save_CPPFLAGS" |
|
83 |
+ |
|
84 |
+ AC_TRY_LINK( |
|
85 |
+ [ |
|
86 |
+ #include <iconv.h> |
|
87 |
+ ], |
|
88 |
+ [ |
|
89 |
+ char** xin,**xout; |
|
90 |
+ unsigned long il,ol; |
|
91 |
+ int rc; |
|
92 |
+ iconv_t iconv_struct = iconv_open("UTF-16BE","UTF-8"); |
|
93 |
+ rc = iconv(iconv_struct,xin,&il,xout,&ol); |
|
94 |
+ iconv_close(iconv_struct); |
|
95 |
+ ], |
|
96 |
+ [ |
|
97 |
+ have_iconv="yes" |
|
98 |
+ ], |
|
99 |
+ [ |
|
100 |
+ if test "X$want_iconv" = "Xyes"; then |
|
101 |
+ AC_MSG_ERROR([The libiconv link test failed. Your libiconv installation may be misconfigured.]) |
|
102 |
+ else |
|
103 |
+ AC_MSG_WARN([The libiconv found, but link test failed. Your libiconv installation may be misconfigured. iconv will not be available.]) |
|
104 |
+ fi |
|
105 |
+ ]) |
|
106 |
+ fi |
|
107 |
+fi |
|
108 |
+ |
|
109 |
+ |
|
110 |
+AC_MSG_CHECKING([for libiconv installation]) |
|
111 |
+ |
|
112 |
+if test "X$have_iconv" = "Xno"; then |
|
113 |
+ AC_MSG_RESULT(no) |
|
114 |
+else |
|
115 |
+ AC_MSG_RESULT([$ICONV_HOME]) |
|
116 |
+ AC_DEFINE([HAVE_ICONV], 1, [iconv() available]) |
|
117 |
+ AC_SUBST(ICONV_LDFLAGS) |
|
118 |
+ AC_SUBST(ICONV_CPPFLAGS) |
|
52 | 119 |
fi |
120 |
+ |
|
121 |
+LIBS="$save_LIBS" |
|
122 |
+LDFLAGS="$save_LDFLAGS" |
|
123 |
+CPPFLAGS="$save_CPPFLAGS" |
... | ... |
@@ -180,6 +180,7 @@ |
180 | 180 |
<ClCompile Include="..\libclamav\7z\XzIn.c" /> |
181 | 181 |
<ClCompile Include="..\libclamav\conv.c" /> |
182 | 182 |
<ClCompile Include="..\libclamav\crypto.c" /> |
183 |
+ <ClCompile Include="..\libclamav\egg.c" /> |
|
183 | 184 |
<ClCompile Include="..\libclamav\execs.c" /> |
184 | 185 |
<ClCompile Include="..\libclamav\fpu.c" /> |
185 | 186 |
<ClCompile Include="..\libclamav\hostid.c" /> |