-
Notifications
You must be signed in to change notification settings - Fork 715
/
egg.c
2554 lines (2162 loc) · 94.3 KB
/
egg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019-2024 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
*
* EGG is an archive format created by ESTsoft used by their ALZip
* archiving software.
*
* This software is written from scratch based solely from ESTsoft's
* file format documentation and from testing with EGG format archives.
* ESTsoft's "unEGG" module was not used in the creation of this capability
* in order to avoid to licensing restrictions on the ESTsoft "unEGG" module.
*
* EGG structure:
*
* |-----------------------------------------------------|------|
* | EGG Header | 1 |
* |-----------------------------------------------------|------|
* | Extra Field 1: | |
* | Split Compression | |
* | Solid Compression | 0~N |
* | Global Encryption Header | |
* |---------------------------------------|------|------|------|
* | File Header | 1 | | |
* |---------------------------------------|------| | |
* | Extra Field 2: | | | |
* | Filename Header | | 1~N | |
* | Comment Header | 0~N | | |
* | Windows File Information | | | |
* | Posix File Information | | | 0~N |
* | Encrypt Header | | | |
* |---------------------------------------|------|------| |
* | Block Header | 1 | | |
* |---------------------------------------|------| | |
* | Extra Field 3: | 0~N | 0~N | |
* |---------------------------------------|------| | |
* | Compressed Data | 1 | | |
* |---------------------------------------|------|------|------|
* | Extra Field 4: | |
* | Archive Comment Header | 0~N |
* |-----------------------------------------------------|------|
*
* Authors: Micah Snyder
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdint.h>
#include <inttypes.h>
#include <wchar.h>
#include <locale.h>
#include <zlib.h>
#include <bzlib.h>
#include "lzma_iface.h"
#include "egg.h"
#include "entconv.h"
#include "str.h"
#ifndef WCHAR
typedef uint16_t WCHAR;
#endif
/*
* All EGG struct variables are little-endian.
*/
#ifndef HAVE_ATTRIB_PACKED
#define __attribute__(x)
#endif
#ifdef HAVE_PRAGMA_PACK
#pragma pack(1)
#endif
#ifdef HAVE_PRAGMA_PACK_HPPA
#pragma pack 1
#endif
/*
* general defines
*/
#define EOFARC 0x08E28222 /* Signals end of each header, or end of archive. */
// #define EOFAR_ 0x2282E208
/*
* egg_header
*/
#define EGG_HEADER_MAGIC 0x41474745
#define EGG_HEADER_VERSION 0x0100
typedef uint32_t magic32_t;
typedef struct __attribute__((packed)) {
magic32_t magic; /* 0x41474745 */
uint16_t version; /* 0x0100 */
uint32_t header_id; /* Random number of the program (Cannot be 0) */
uint32_t reserved; /* 0x00000000 */
} egg_header;
/*
* file_header
*/
#define FILE_HEADER_MAGIC 0x0A8590E3
typedef struct __attribute__((packed)) {
magic32_t magic; /* 0x0A8590E3 */
uint32_t file_id; /* Unique value for each header (Includes 0) */
uint64_t file_length; /* Total size of the file */
} file_header;
/*
* block_header
* Note: split block of files exceeding 4G
*/
#define BLOCK_HEADER_MAGIC 0x02B50C13
#define BLOCK_HEADER_COMPRESS_ALGORITHM_STORE 0
#define BLOCK_HEADER_COMPRESS_ALGORITHM_DEFLATE 1
#define BLOCK_HEADER_COMPRESS_ALGORITHM_BZIP2 2
#define BLOCK_HEADER_COMPRESS_ALGORITHM_AZO 3
#define BLOCK_HEADER_COMPRESS_ALGORITHM_LZMA 4
typedef struct __attribute__((packed)) {
magic32_t magic; /* 0x02B50C13 */
uint8_t compress_algorithm; /* compress method algorithm number */
uint8_t compress_hint; /* compress method hint */
uint32_t uncompress_size; /* size of the block before compressed */
uint32_t compress_size; /* size of the block after compressed */
uint32_t crc32; /* CRC value of the block */
} block_header;
/*
* extra_field
*
* The extra_field is followed by a uint16_t or uint32_t depending on the bit_flag.
* This describes the size of the following data.
* In this way, an unexpected header can still be parsed.
* Headers that make use of the extra_field:
* - windows_file_information header
* - posix_file_information header
* - encrypt header
* - filename header
* - comment header
* - split_compression header
* - solid_compression header
*/
#define EXTRA_FIELD_FLAGS_SIZE_IS_2BYTES 0x00
#define EXTRA_FIELD_FLAGS_SIZE_IS_4BYTES 0x01
typedef struct __attribute__((packed)) {
magic32_t magic;
uint8_t bit_flag; /* the size field following bit_flag depends if bit_flag bit 1: */
} extra_field; /* 0 (uint16_t) */
/* 1 (uint32_t) */
/*
* Extra field: encrypt
*
* The encrypt_header is followed by:
* 1) dummy data (size bytes)
*
* Note: Inserted in Extra Field 2 (optional, depending on KeyBase, AES, or LEA)
*/
#define ENCRYPT_HEADER_MAGIC 0x08D1470F
#define ENCRYPT_HEADER_ENCRYPT_METHOD_XOR 0x00
#define ENCRYPT_HEADER_ENCRYPT_METHOD_AES128 0x01
#define ENCRYPT_HEADER_ENCRYPT_METHOD_AES256 0x02
#define ENCRYPT_HEADER_ENCRYPT_METHOD_LEA128 0x10
#define ENCRYPT_HEADER_ENCRYPT_METHOD_LEA256 0x20
typedef struct __attribute__((packed)) {
uint8_t aes_header[10]; /* AES/LEA Header */
uint8_t aes_footer[10]; /* AES/LEA Footer */
} aes_lea_128;
typedef struct __attribute__((packed)) {
uint8_t aes_header[18]; /* AES/LEA header */
uint8_t aes_footer[10]; /* AES/LEA footer */
} aes_lea_256;
typedef struct __attribute__((packed)) {
uint8_t verify_data[12]; /* KeyBase encryption verification data */
uint32_t crc32; /* KeyBase partial block CRC */
} zip2_xor_keybase;
typedef struct __attribute__((packed)) {
uint8_t encrypt_method; /* See above encrypt method #defines */
} encrypt_header;
/*
* Extra field: windows_file_information
*/
#define WINDOWS_INFO_MAGIC 0x2C86950B
#define WINDOWS_INFO_ATTRIBUTE_READONLY 0x01
#define WINDOWS_INFO_ATTRIBUTE_HIDDEN 0x02
#define WINDOWS_INFO_ATTRIBUTE_SYSTEM_FILE 0x04
#define WINDOWS_INFO_ATTRIBUTE_LINK_FILE 0x10 /* junction file */
#define WINDOWS_INFO_ATTRIBUTE_DIRECTORY 0x40
typedef struct __attribute__((packed)) {
uint64_t last_modified_time; /* "100-Nanosecond Time" since the Windows Epoch (00:00:00 UTC, January 1, 1601) */
uint8_t attribute; /* See above attribute #defines */
} windows_file_information;
/*
* Extra field: posix_file_information
*/
#define POSIX_INFO_MAGIC 0x1EE922E5
#define POSIX_INFO_MODE_FILETYPE_BITMASK 0x0170000 /* bitmask for the file type bitfields */
#define POSIX_INFO_MODE_SOCKET 0x0140000 /* socket */
#define POSIX_INFO_MODE_SYM_LINK 0x0120000 /* symbolic link */
#define POSIX_INFO_MODE_REG_FILE 0x0100000 /* regular file */
#define POSIX_INFO_MODE_BLOCK_DEVICE 0x0060000 /* block device */
#define POSIX_INFO_MODE_DIRECTORY 0x0040000 /* directory */
#define POSIX_INFO_MODE_CHAR_DEVICE 0x0020000 /* character device */
#define POSIX_INFO_MODE_FIFO 0x0010000 /* FIFO */
#define POSIX_INFO_MODE_SET_UID_BIT 0x0004000 /* set UID bit */
#define POSIX_INFO_MODE_SET_GROUPID_BIT 0x0002000 /* set-group-ID bit (see below) */
#define POSIX_INFO_MODE_STICKY_BIT 0x0001000 /* sticky bit (see below) */
#define POSIX_INFO_MODE_PERM_OWNER_MASK 0x00700 /* mask for file owner permissions */
#define POSIX_INFO_MODE_PERM_OWNER_READ 0x00400 /* owner has read permission */
#define POSIX_INFO_MODE_PERM_OWNER_WRITE 0x00200 /* owner has write permission */
#define POSIX_INFO_MODE_PERM_OWNER_EXECUTE 0x00100 /* owner has execute permission */
#define POSIX_INFO_MODE_PERM_GROUP_MASK 0x00070 /* mask for group permissions */
#define POSIX_INFO_MODE_PERM_GROUP_READ 0x00040 /* group has read permission */
#define POSIX_INFO_MODE_PERM_GROUP_WRITE 0x00020 /* group has write permission */
#define POSIX_INFO_MODE_PERM_GROUP_EXECUTE 0x00010 /* group has execute permission */
#define POSIX_INFO_MODE_PERM_OTHERS_MASK 0x00007 /* mask for permissions for others (not in group) */
#define POSIX_INFO_MODE_PERM_OTHERS_READ 0x00004 /* others have read permission */
#define POSIX_INFO_MODE_PERM_OTHERS_WRITE 0x00002 /* others have write permission */
#define POSIX_INFO_MODE_PERM_OTHERS_EXECUTE 0x00001 /* others have execute permission*/
typedef struct __attribute__((packed)) {
uint32_t mode; /* see above mode #defines */
uint32_t uid; /* */
uint32_t gid; /* */
uint64_t last_modified_time; /* "Second Time" since the Unix Epoch (00:00:00 UTC, January 1, 1970) */
} posix_file_information;
/*
* Extra field: dummy_header
*
* The dummy header extra_info is followed by:
* 1) dummy data (size bytes)
*
* Note: No need to consider if the size is too small to fit the dummy header because it can be distinguished by size calculation.
*/
#define DUMMY_HEADER_MAGIC 0x07463307
/*
* Extra field: filename
*
* The filename extra_field is followed by:
* 1) uint16_t locale IFF bit_flag is NOT unicode (UCS-2 LE)
* 1) uint32_t parent_path_id IFF bit_flag is relative.
* parent_path_id will be the ID of a file possessing the parent path.
* 2) name buffer (size bytes minus above optional fields)
*/
#define FILENAME_HEADER_MAGIC 0x0A8591AC
#define FILENAME_HEADER_FLAGS_ENCRYPT 0x04
#define FILENAME_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8 0x08
#define FILENAME_HEADER_FLAGS_RELATIVE_PATH_INSTEAD_OF_ABSOLUTE 0x10
#define FILENAME_HEADER_LOCALE_USE_SYSTEM 0
#define FILENAME_HEADER_LOCALE_JAPANESE 932 /* Shift-JIS */
#define FILENAME_HEADER_LOCALE_KOREAN 949
// typedef struct __attribute__((packed)) {
// (optional) uint16_t locale
// (optional) uint32_t parent_path_id
// uint8_t name_data [extra_field->size - sizeof(locale) - sizeof(parent_path_id)]
// } filename_header;
/*
* Extra field: comment
*
* The comment extra_field is followed by:
* 1) comment of size "N", exclude NULL character.
*/
#define COMMENT_HEADER_MAGIC 0x04C63672
#define COMMENT_HEADER_FLAGS_ENCRYPT 0x04
#define COMMENT_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8 0x08
/*
* Extra field: split compression
*/
#define SPLIT_COMPRESSION_MAGIC 0x24F5A262
typedef struct __attribute__((packed)) {
uint32_t prev_file_id; /* ID of previous file, 0 if first */
uint32_t next_file_id; /* ID of next file, 0 if last */
} split_compression;
/*
* Extra field: solid compression
*/
#define SOLID_COMPRESSION_MAGIC 0x24E5A060
#ifdef HAVE_PRAGMA_PACK
#pragma pack()
#endif
#ifdef HAVE_PRAGMA_PACK_HPPA
#pragma pack
#endif
typedef struct {
char* name_utf8;
uint32_t parent_path_id;
} egg_filename;
typedef struct {
encrypt_header* header; /* Global Encryption Header */
union {
aes_lea_128* al128;
aes_lea_256* al256;
zip2_xor_keybase* xor ;
} encrypt_al;
} egg_encrypt;
typedef struct {
block_header* blockHeader;
char* compressedData;
} egg_block;
typedef struct {
file_header* file;
egg_filename filename;
windows_file_information* windowsFileInformation;
posix_file_information* posixFileInformation;
egg_encrypt* encrypt;
uint64_t nBlocks;
egg_block** blocks;
uint64_t nComments;
char** comments;
} egg_file;
typedef struct {
fmap_t* map;
size_t offset;
uint64_t fileExtractionIndex;
int bSolid; /* Solid == all files compressed together. */
int bSplit; /* Split == multiple files make up single archive. */
split_compression* splitInfo;
egg_encrypt* encrypt;
uint64_t nFiles;
egg_file** files;
uint64_t nBlocks;
egg_block** blocks;
uint64_t nComments;
char** comments;
} egg_handle;
#define EGG_VALIDATE_HANDLE(h) \
((!handle || !handle->map || (handle->offset > handle->map->len)) ? CL_EARG : CL_SUCCESS)
const char* getEncryptName(uint8_t method)
{
const char* encryptName = NULL;
switch (method) {
case ENCRYPT_HEADER_ENCRYPT_METHOD_XOR:
encryptName = "XOR";
break;
case ENCRYPT_HEADER_ENCRYPT_METHOD_AES128:
encryptName = "AES 128";
break;
case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA128:
encryptName = "LEA 128";
break;
case ENCRYPT_HEADER_ENCRYPT_METHOD_AES256:
encryptName = "AES 256";
break;
case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA256:
encryptName = "LEA 256";
break;
default:
encryptName = "<unknown method>";
}
return encryptName;
}
const char* getMagicHeaderName(uint32_t magic)
{
const char* magicName = NULL;
switch (magic) {
case EGG_HEADER_MAGIC:
magicName = "EGG_HEADER_MAGIC";
break;
case FILE_HEADER_MAGIC:
magicName = "FILE_HEADER_MAGIC";
break;
case BLOCK_HEADER_MAGIC:
magicName = "BLOCK_HEADER_MAGIC";
break;
case ENCRYPT_HEADER_MAGIC:
magicName = "ENCRYPT_HEADER_MAGIC";
break;
case WINDOWS_INFO_MAGIC:
magicName = "WINDOWS_INFO_MAGIC";
break;
case POSIX_INFO_MAGIC:
magicName = "POSIX_INFO_MAGIC";
break;
case DUMMY_HEADER_MAGIC:
magicName = "DUMMY_HEADER_MAGIC";
break;
case FILENAME_HEADER_MAGIC:
magicName = "FILENAME_HEADER_MAGIC";
break;
case COMMENT_HEADER_MAGIC:
magicName = "COMMENT_HEADER_MAGIC";
break;
case SPLIT_COMPRESSION_MAGIC:
magicName = "SPLIT_COMPRESSION_MAGIC";
break;
case SOLID_COMPRESSION_MAGIC:
magicName = "SOLID_COMPRESSION_MAGIC";
break;
default:
magicName = "<unknown header magic>";
}
return magicName;
}
static void egg_free_encrypt(egg_encrypt* encryptInfo)
{
free(encryptInfo);
}
static cl_error_t egg_parse_encrypt_header(const uint8_t* index, size_t size, egg_encrypt** encryptInfo)
{
/*
* The EGG specification (last updated 2016) for the encrypt header is not accurate.
* The following describes my findings of the actual format for the encrypt header.
*
* The significant discrepancy is that the Size includes the size of the header itself, not just the data following it.
* No other extra_field header's size field includes the size of itself.
* This must be accounted for by the caller of this function (see the "Fudge factor" comments where this function is used).
*
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | Magic(ENCRYP) | 4 | 0x08D1470F |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | Bit flag | 1 | 0 |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | Size | 2 | sizeof( Magic ) + sizeof( Bit flag ) + sizeof( Size ) + sizeof( Encrypt Method ) + sizeof( Method Header ) |
* |---------------|---------|---|--------------------------------------------------------------------------------------------------------|
* | Encrypt | 1 | 0 | KeyBase (XOR) |
* | Method | |---|--------------------------------------------------------------------------------------------------------|
* | | | 1 | AES128 |
* | | |---|--------------------------------------------------------------------------------------------------------|
* | | | 2 | AES256 |
* | | |---|--------------------------------------------------------------------------------------------------------|
* | | | 5 | LEA128 |
* | | |---|--------------------------------------------------------------------------------------------------------|
* | | | 6 | LEA256 |
* |---------------|---------|---|--------------------------------------------------------------------------------------------------------|
*
* Depending on the Method (XOR / AES/LEA128 / AES/LEA256) The above will be followed by one of the following Method Headers:
*
* XOR (KeyBase):
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | verify Data | 12 | Encryption Verification Data |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | CRC32 | 4 | Partial Block CRC |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
*
* AES / LEA 128
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | Magic(ENCRYP) | 10 | AES/LEA Header |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | Magic(ENCRYP) | 10 | AES/LEA Footer |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
*
* AES / LEA 256
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | Magic(ENCRYP) | 18 | AES/LEA Header |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
* | Magic(ENCRYP) | 10 | AES/LEA Footer |
* |---------------|---------|------------------------------------------------------------------------------------------------------------|
*/
cl_error_t status = CL_EPARSE;
egg_encrypt* encrypt = NULL;
if (!index || 0 == size || !encryptInfo) {
cli_errmsg("egg_parse_encrypt_header: Invalid args.\n");
status = CL_EARG;
goto done;
}
*encryptInfo = NULL;
cli_dbgmsg("egg_parse_encrypt_header: Encrypted archive.\n");
cli_dbgmsg("egg_parse_encrypt_header: size of encrypt extra_field data: %zu\n", size);
if (size < sizeof(encrypt_header)) {
cli_warnmsg("egg_parse_encrypt_header: Encrypt header size too small (%zu < %zu)\n", size, sizeof(encrypt_header));
goto done;
}
encrypt = (egg_encrypt*)calloc(1, sizeof(egg_encrypt));
if (NULL == encrypt) {
cli_errmsg("egg_parse_encrypt_header: Failed to allocate memory for egg_encrypt.\n");
status = CL_EMEM;
goto done;
}
encrypt->header = (encrypt_header*)index;
cli_dbgmsg("egg_parse_encrypt_header: encrypt_header->encrypt_method: %02x (%s)\n", encrypt->header->encrypt_method, getEncryptName(encrypt->header->encrypt_method));
index += sizeof(encrypt_header);
size -= sizeof(encrypt_header);
if (ENCRYPT_HEADER_ENCRYPT_METHOD_XOR == encrypt->header->encrypt_method) {
if (size != sizeof(zip2_xor_keybase)) {
cli_warnmsg("egg_parse_encrypt_header: Encrypt header size for XOR is different than expected (%zu != %zu)\n", size, sizeof(zip2_xor_keybase));
goto done;
}
encrypt->encrypt_al.xor = (zip2_xor_keybase*)index;
cli_dbgmsg("egg_parse_encrypt_header: encrypt_header->crc32: %08x\n", le32_to_host(encrypt->encrypt_al.xor->crc32));
} else {
/*
* For AES/LEA, the additional information is found inside of embedded extra field.
*/
switch (encrypt->header->encrypt_method) {
case ENCRYPT_HEADER_ENCRYPT_METHOD_AES128:
case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA128: {
if (size < sizeof(aes_lea_128)) {
cli_warnmsg("egg_parse_encrypt_header: Encrypt header size for AES/LEA128 is different than expected (%zu != %zu)\n", size, sizeof(aes_lea_128));
goto done;
}
encrypt->encrypt_al.al128 = (aes_lea_128*)index;
index += sizeof(aes_lea_128);
size -= sizeof(aes_lea_128);
break;
}
case ENCRYPT_HEADER_ENCRYPT_METHOD_AES256:
case ENCRYPT_HEADER_ENCRYPT_METHOD_LEA256: {
if (size < sizeof(aes_lea_256)) {
cli_warnmsg("egg_parse_encrypt_header: Encrypt header size for AES/LEA256 is different than expected (%zu != %zu)\n", size, sizeof(aes_lea_256));
goto done;
}
encrypt->encrypt_al.al256 = (aes_lea_256*)index;
index += sizeof(aes_lea_256);
size -= sizeof(aes_lea_256);
break;
}
default: {
cli_warnmsg("egg_parse_encrypt_header: Unknown encrypt method: %d\n", encrypt->header->encrypt_method);
goto done;
}
}
}
*encryptInfo = encrypt;
encrypt = NULL;
status = CL_SUCCESS;
done:
if (NULL != encrypt) {
egg_free_encrypt(encrypt);
}
return status;
}
static cl_error_t egg_parse_comment_header(const uint8_t* index, size_t size, extra_field* extraField, char** commentInfo)
{
cl_error_t status = CL_EPARSE;
char* comment_utf8 = NULL;
size_t comment_utf8_size = 0;
if (!index || 0 == size || !extraField || !commentInfo) {
cli_errmsg("egg_parse_comment_headers: Invalid args!\n");
return CL_EARG;
}
*commentInfo = NULL;
if (extraField->bit_flag & COMMENT_HEADER_FLAGS_ENCRYPT) {
/*
* comment is encrypted, nothing to be done.
*/
status = CL_EUNPACK;
goto done;
}
/*
* Store comment as UTF-8 string.
*/
if (extraField->bit_flag & COMMENT_HEADER_FLAGS_MULTIBYTE_CODEPAGE_INSTEAD_OF_UTF8) {
/*
* Unlike with filenames, the multibyte string codepage (or "locale") is not present in comment headers.
* Try conversion with CODEPAGE_UTF8.
*/
if (CL_SUCCESS != cli_codepage_to_utf8((char*)index, size, CODEPAGE_UTF8, &comment_utf8, &comment_utf8_size)) {
cli_dbgmsg("egg_parse_comment_header: failed to convert codepage \"0\" to UTF-8\n");
status = CL_EUNPACK;
goto done;
}
} else {
/* Should already be UTF-8. Use as-is.. */
comment_utf8 = CLI_STRNDUP((char*)index, size);
if (NULL == comment_utf8) {
cli_dbgmsg("egg_parse_comment_header: failed to allocate comment buffer.\n");
status = CL_EMEM;
goto done;
}
}
cli_dbgmsg("egg_parse_comment_header: comment: %s\n", comment_utf8);
*commentInfo = comment_utf8;
comment_utf8 = NULL;
status = CL_SUCCESS;
done:
if (NULL != comment_utf8) {
free(comment_utf8);
}
return status;
}
static void egg_free_egg_block(egg_block* block)
{
free(block);
}
static cl_error_t egg_parse_block_headers(egg_handle* handle, egg_block** block)
{
cl_error_t status = CL_EPARSE;
egg_block* eggBlock = NULL;
block_header* blockHeader = NULL;
uint32_t magic = 0;
const uint8_t* index = 0;
if (!handle || !block) {
cli_errmsg("egg_parse_block_headers: Invalid args!\n");
return CL_EARG;
}
*block = NULL;
if (CL_SUCCESS != EGG_VALIDATE_HANDLE(handle)) {
cli_errmsg("egg_parse_block_headers: Invalid handle values!\n");
status = CL_EARG;
goto done;
}
/*
* 1st:
* Block headers must start with the block_header.
*/
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(block_header));
if (!index) {
cli_dbgmsg("egg_parse_block_headers: File buffer too small to contain block header.\n");
goto done;
}
eggBlock = (egg_block*)calloc(1, sizeof(egg_block));
if (NULL == eggBlock) {
cli_errmsg("egg_parse_block_headers: Failed to allocate memory for egg_block.\n");
status = CL_EMEM;
goto done;
}
blockHeader = (block_header*)index;
eggBlock->blockHeader = blockHeader;
if (BLOCK_HEADER_MAGIC != le32_to_host(blockHeader->magic)) {
cli_dbgmsg("egg_parse_block_headers: Invalid block header magic: %08x.\n", le32_to_host(blockHeader->magic));
goto done;
}
cli_dbgmsg("egg_parse_block_headers: block_header->magic: %08x (%s)\n", le32_to_host(blockHeader->magic), getMagicHeaderName(le32_to_host(blockHeader->magic)));
cli_dbgmsg("egg_parse_block_headers: block_header->compress_algorithm: %08x\n", blockHeader->compress_algorithm);
cli_dbgmsg("egg_parse_block_headers: block_header->compress_hint: %08x\n", blockHeader->compress_hint);
cli_dbgmsg("egg_parse_block_headers: block_header->uncompress_size: %08x\n", le32_to_host(blockHeader->uncompress_size));
cli_dbgmsg("egg_parse_block_headers: block_header->compress_size: %08x\n", le32_to_host(blockHeader->compress_size));
cli_dbgmsg("egg_parse_block_headers: block_header->crc32: %08x\n", le32_to_host(blockHeader->crc32));
if (0 == le16_to_host(blockHeader->compress_size)) {
cli_warnmsg("egg_parse_block_headers: Empty block!\n");
}
handle->offset += sizeof(block_header);
/*
* 2nd:
* After the block_header, the following extra field headers may be present:
* a) EOFARC
*/
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(magic32_t));
if (!index) {
cli_dbgmsg("egg_parse_block_headers: File buffer too small to contain end of archive magic bytes.\n");
goto done;
}
magic = le32_to_host(*((uint32_t*)index));
if (EOFARC != magic) {
cli_dbgmsg("egg_parse_block_headers: EOFARC missing after block header. Found these bytes instead: %08x. (%s)\n", magic, getMagicHeaderName(magic));
goto done;
}
cli_dbgmsg("egg_parse_block_headers: End of block header.\n");
handle->offset += sizeof(magic32_t);
/*
* Compressed data should follow the Block Header.
*/
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, blockHeader->compress_size);
if (!index) {
cli_dbgmsg("egg_parse_block_headers: File buffer too small to contain block compressed data.\n");
goto done;
}
eggBlock->compressedData = (char*)index;
handle->offset += blockHeader->compress_size;
*block = eggBlock;
eggBlock = NULL;
status = CL_SUCCESS;
done:
if (NULL != eggBlock) {
egg_free_egg_block(eggBlock);
}
return status;
}
static void egg_free_egg_file(egg_file* file)
{
uint32_t i = 0;
if (NULL != file->filename.name_utf8) {
free(file->filename.name_utf8);
file->filename.name_utf8 = NULL;
}
if (NULL != file->encrypt) {
free(file->encrypt);
file->encrypt = NULL;
}
if (NULL != file->blocks) {
for (i = 0; i < file->nBlocks; i++) {
egg_free_egg_block(file->blocks[i]);
file->blocks[i] = NULL;
}
free(file->blocks);
file->blocks = NULL;
}
if (NULL != file->comments) {
for (i = 0; i < file->nComments; i++) {
free(file->comments[i]);
file->comments[i] = NULL;
}
free(file->comments);
file->comments = NULL;
}
free(file);
}
static cl_error_t egg_parse_archive_extra_field(egg_handle* handle)
{
cl_error_t status = CL_EPARSE;
const uint8_t* index = NULL;
extra_field* extraField = NULL;
uint32_t magic = 0;
uint32_t size = 0;
if (!handle) {
cli_errmsg("egg_parse_archive_extra_field: Invalid args!\n");
return CL_EARG;
}
if (CL_SUCCESS != EGG_VALIDATE_HANDLE(handle)) {
cli_errmsg("egg_parse_comment_headers: Invalid handle values!\n");
status = CL_EARG;
goto done;
}
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(extra_field));
if (!index) {
cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain extra_field header.\n");
goto done;
}
extraField = (extra_field*)index;
cli_dbgmsg("egg_parse_archive_extra_field: extra_field->magic: %08x (%s)\n", le32_to_host(extraField->magic), getMagicHeaderName(le32_to_host(extraField->magic)));
cli_dbgmsg("egg_parse_archive_extra_field: extra_field->bit_flag: %02x\n", extraField->bit_flag);
handle->offset += sizeof(extra_field);
if (extraField->bit_flag & EXTRA_FIELD_FLAGS_SIZE_IS_4BYTES) {
/* size is uint32_t */
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint32_t));
if (!index) {
cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain extra_field header.\n");
goto done;
}
size = le32_to_host(*(uint32_t*)index);
handle->offset += sizeof(uint32_t);
} else {
/* size is uint16_t */
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(uint16_t));
if (!index) {
cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain extra_field header.\n");
goto done;
}
size = le16_to_host(*(uint16_t*)index);
handle->offset += sizeof(uint16_t);
}
cli_dbgmsg("egg_parse_archive_extra_field: extra_field->size: %u\n", size);
magic = le32_to_host(extraField->magic);
switch (magic) {
case SOLID_COMPRESSION_MAGIC: {
/*
* Solid archive is an archive packed with a special compression method,
* which treats several or all files within the archive as one continuous data stream.
*/
cli_dbgmsg("egg_parse_archive_extra_field: Solid archive. Several or all files within the archive treated as one continuous data stream.\n");
if (0 != handle->bSolid) {
cli_warnmsg("egg_parse_archive_extra_field: Encountered more than 1 Solid extra_field!\n");
goto done;
}
handle->bSolid = 1;
break;
}
case SPLIT_COMPRESSION_MAGIC: {
/*
* Split archives are single archives split into multiple .egg volumes.
*
* It is the first file if previous file’s ID is 0, and is the last file
* if next file’s ID is 0.
*
* Header and Extra Field shouldn’t be cut when split compressing.
* Compressed Block Data can be saved cut.
* If header is excluded from the split size, insert Dummy Extra Field.
*
* If file compression ratio not applied when split compressing, modify
* Magic of the header into Dummy Header or Skip Header (0xFFFF0000)
* so it can be skipped.
*/
split_compression* split = NULL;
if (0 != handle->bSplit) {
cli_warnmsg("egg_parse_archive_extra_field: Encountered more than 1 Split extra_field!\n");
goto done;
}
handle->bSplit = 1;
cli_warnmsg("egg_parse_archive_extra_field: Split archive. Split archives are single archives split into multiple .egg volumes.\n");
if (sizeof(split_compression) != size) {
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);
} else {
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, sizeof(split_compression));
if (!index) {
cli_dbgmsg("egg_parse_archive_extra_field: File buffer too small to contain split compression header.\n");
goto done;
}
split = (split_compression*)index;
handle->splitInfo = split;
cli_dbgmsg("egg_parse_archive_extra_field: split_compression->prev_file_id: %08x\n", le32_to_host(split->prev_file_id));
cli_dbgmsg("egg_parse_archive_extra_field: split_compression->next_file_id: %08x\n", le32_to_host(split->next_file_id));
}
break;
}
case ENCRYPT_HEADER_MAGIC: {
/*
* EGG files may have a global encryption header.
* It is unclear if this means each file is encrypted, or that additional
* data beyond the file contents is encrypted.
*/
if (NULL != handle->encrypt) {
cli_warnmsg("egg_parse_archive_extra_field: Encountered more than 1 encrypt_header!\n");
goto done;
}
/*
* Fudge factor.
* The documentation is hazy about how the encrypt header works.
* From testing, it seems that for encrypted files, the size in the extra_field includes the size OF the extra field.
*/
size -= sizeof(extra_field) + sizeof(uint16_t);
index = (const uint8_t*)fmap_need_off_once(handle->map, handle->offset, size);
if (!index) {
cli_errmsg("egg_parse_archive_extra_field: File buffer too small to contain encryption headers.\n");
goto done;
}
if (CL_SUCCESS != egg_parse_encrypt_header(index, size, &handle->encrypt)) {
cli_errmsg("egg_parse_archive_extra_field: Failed to parse encryption headers.\n");
goto done;
}
break;
}
default: {
cli_dbgmsg("egg_parse_archive_extra_field: unexpected header magic: %08x (%s)\n", magic, getMagicHeaderName(magic));
}
}
handle->offset += size;
status = CL_SUCCESS;
done:
return status;
}
static void print_posix_info_mode(uint32_t mode)
{
/* File type flags */
if (mode & POSIX_INFO_MODE_REG_FILE) {
printf("-");
} else if (mode & POSIX_INFO_MODE_DIRECTORY) {
printf("d");
} else if (mode & POSIX_INFO_MODE_CHAR_DEVICE) {
printf("c");
} else if (mode & POSIX_INFO_MODE_BLOCK_DEVICE) {
printf("s");
} else if (mode & POSIX_INFO_MODE_SOCKET) {
printf("s");
} else if (mode & POSIX_INFO_MODE_FIFO) {
printf("p");
} else if (mode & POSIX_INFO_MODE_SYM_LINK) {
printf("l");
}
/* Owner/Group/Other permissions */
if (mode & POSIX_INFO_MODE_PERM_OWNER_READ) {
printf("r");
} else {
printf("-");
}
if (mode & POSIX_INFO_MODE_PERM_OWNER_WRITE) {
printf("w");
} else {
printf("-");
}
if (mode & POSIX_INFO_MODE_SET_UID_BIT) {
printf("s");
} else if (mode & POSIX_INFO_MODE_PERM_OWNER_EXECUTE) {
printf("x");
} else {
printf("-");
}
if (mode & POSIX_INFO_MODE_PERM_GROUP_READ) {
printf("r");
} else {
printf("-");
}
if (mode & POSIX_INFO_MODE_PERM_GROUP_WRITE) {
printf("w");
} else {
printf("-");
}
if (mode & POSIX_INFO_MODE_SET_UID_BIT) {
printf("s");