-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathunpacker.c
1048 lines (920 loc) · 22.8 KB
/
unpacker.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) 2022 Jaroslav Hensl *
* *
* Permission is hereby granted, free of charge, to any person *
* obtaining a copy of this software and associated documentation *
* files (the "Software"), to deal in the Software without *
* restriction, including without limitation the rights to use, *
* copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be *
* included in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, *
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
* OTHER DEALINGS IN THE SOFTWARE. *
* *
*******************************************************************************/
#include "patcher9x.h"
#define cfhdrPREV_CABINET 0x0001
#define cfhdrNEXT_CABINET 0x0002
#define cfhdrRESERVE_PRESENT 0x0004
/*
* MS CAB format
*
* FROM: https://docs.microsoft.com/en-us/previous-versions//bb267310(v=vs.85)?redirectedfrom=MSDN
*/
#define CAB_MAX_STR 255
#pragma pack(push)
#pragma pack(1)
typedef struct _mscab_t
{
uint8_t signature[4]; /*inet file signature */
uint32_t reserved1; /* reserved */
uint32_t cbCabinet; /* size of this cabinet file in bytes */
uint32_t reserved2; /* reserved */
uint32_t coffFiles; /* offset of the first CFFILE entry */
uint32_t reserved3; /* reserved */
uint8_t versionMinor; /* cabinet file format version, minor */
uint8_t versionMajor; /* cabinet file format version, major */
uint16_t cFolders; /* number of CFFOLDER entries in this */
/* cabinet */
uint16_t cFiles; /* number of CFFILE entries in this cabinet */
uint16_t flags; /* cabinet file option indicators */
uint16_t setID; /* must be the same for all cabinets in a */
/* set */
uint16_t iCabinet; /* number of this cabinet file in a set */
} mscab_t;
#pragma pack(pop)
/* CAB list item */
typedef struct _cab_list_item_t
{
char *filename;
struct mscabd_cabinet *cab;
struct _cab_list_item_t *next;
} cab_list_item_t;
/* CAB list */
typedef struct _cab_list_t
{
cab_list_item_t *first;
cab_list_item_t *last;
} cab_list_t;
/* list of already scanned files */
struct scanned_files_item
{
char *filename;
struct scanned_files_item *next;
};
struct scanned_files_list
{
struct scanned_files_item *first;
struct scanned_files_item *last;
};
/* allocate and add item to the end of list */
static cab_list_item_t *cab_list_add(cab_list_t *list)
{
cab_list_item_t *item = malloc(sizeof(cab_list_item_t));
if(item)
{
memset(item, 0, sizeof(cab_list_item_t));
item->next = NULL;
if(list->last == NULL)
{
list->last = list->first = item;
}
else
{
list->last->next = item;
list->last = item;
}
return item;
}
return NULL;
}
/* free all items in list and call fs_path_free on all stored filename */
static void cab_list_clear(cab_list_t *list)
{
cab_list_item_t *item;
while(list->first != NULL)
{
item = list->first;
list->first = item->next;
if(item->filename != NULL)
{
fs_path_free(item->filename);
}
free(item);
}
list->last = NULL;
}
/* init list for first usage */
static void cab_list_init(cab_list_t *list)
{
list->first = NULL;
list->last = NULL;
}
/* init file scan list */
static void scanned_files_list_init(scanned_files_list_t *list)
{
list->first = NULL;
list->last = NULL;
}
static void scanned_files_list_add(scanned_files_list_t *list, const char *fn)
{
size_t len = strlen(fn);
struct scanned_files_item *item = malloc(sizeof(struct scanned_files_item) + len + 1);
if(item != NULL)
{
item->filename = (char *)(item+1);
strcpy(item->filename, fn);
item->next = NULL;
if(list->last == NULL)
{
list->first = item;
list->last = item;
}
else
{
list->last->next = item;
list->last = item;
}
}
}
static int scanned_files_lookup(scanned_files_list_t *list, const char *fn)
{
struct scanned_files_item *item = list->first;
while(item != NULL)
{
if(istrcmp(item->filename, fn) == 0)
{
return 1;
}
item = item->next;
}
return 0;
}
static void scanned_files_clean(scanned_files_list_t *list)
{
struct scanned_files_item *item = list->first;
while(item != NULL)
{
struct scanned_files_item *del_item = item;
item = item->next;
free(del_item);
}
list->first = NULL;
list->last = NULL;
}
/**
* Read string form CAB header
*
* @param fp: ponter to open file
* @param buf: destination to write string bytes. String will be terminated with '\0'
* Destination should be at last CAB_MAX_STR bytes len.
* Could be NULL, if value doesn't matter and you need to seek to next header.
*
**/
static void cab_read_str(FILE *fp, char *buf)
{
int c = 0;
int i = 0;
for(i = 0; i < CAB_MAX_STR; i++)
{
c = fgetc(fp);
if(c == EOF || c == '\0')
{
if(buf != NULL)
{
*buf = '\0';
}
break;
}
if(buf != NULL)
{
*buf = c;
buf++;
}
}
if(buf != NULL)
{
*buf = '\0';
}
}
/**
* Open CAB and try to find prev and next part if archive is multivolume
*
* @param srccab: patch to archive
* @param prevcab: pointer to pointer to file name of previous part of archive if the part is exists.
* If not destination will be set to NULL. Paramater could be NULL if on previous part not matter.
* Unused result should be free with 'fs_path_free'
* @param nextcab: same behavior as prevcab but for next part of archive
*
**/
static void cab_analyze(const char *srccab, char **prevcab, char **nextcab)
{
char prevfile[CAB_MAX_STR];
char nextfile[CAB_MAX_STR];
FILE *fr = fopen(srccab, "rb");
char *ptr;
if(fr != NULL)
{
mscab_t header;
fread(&header, sizeof(mscab_t), 1, fr);
if(memcmp(header.signature, "MSCF", 4) == 0)
{
if((header.flags & cfhdrRESERVE_PRESENT) != 0)
{
uint16_t cbCFHeader;
fread(&cbCFHeader, sizeof(uint16_t), 1, fr);
cab_read_str(fr, NULL); /* read (and ignore) cbCFFolder */
cab_read_str(fr, NULL); /* read (and ignore) cbCFData */
fseek(fr, cbCFHeader, SEEK_CUR); /* skip abReserve */
}
if((header.flags & cfhdrPREV_CABINET) != 0)
{
cab_read_str(fr, prevfile); /* read szCabinetPrev */
cab_read_str(fr, NULL); /* read (and ignore) szDiskPrev */
if(prevcab != NULL)
{
*prevcab = NULL;
mscab_t header_test;
FILE *test;
int valid = 0;
ptr = fs_path_get2(srccab, prevfile, NULL);
if(ptr != NULL)
{
test = fopen(ptr, "rb");
if(test)
{
fread(&header_test, sizeof(mscab_t), 1, test);
/* check if is CAB and setID match with source */
if(memcmp(header_test.signature, "MSCF", 4) == 0 &&
header_test.setID == header.setID)
{
/* check sequence */
if(header.iCabinet == header_test.iCabinet+1)
{
*prevcab = ptr;
valid = 1;
}
}
fclose(test);
}
if(!valid)
{
fs_path_free(ptr);
}
}
}
} // cfhdrPREV_CABINET
if((header.flags & cfhdrNEXT_CABINET) != 0)
{
cab_read_str(fr, nextfile); /* read szCabinetNext */
cab_read_str(fr, NULL); /* read (and ignore) szDiskNext */
if(nextcab != NULL)
{
*nextcab = NULL; /* clear */
mscab_t header_test;
FILE *test;
int valid = 0;
ptr = fs_path_get2(srccab, nextfile, NULL);
if(ptr != NULL)
{
test = fopen(ptr, "rb");
if(test)
{
fread(&header_test, sizeof(mscab_t), 1, test);
/* check if is CAB and setID match with source */
if(memcmp(header_test.signature, "MSCF", 4) == 0 &&
header_test.setID == header.setID)
{
/* check sequence */
if(header.iCabinet+1 == header_test.iCabinet)
{
*nextcab = ptr;
valid = 1;
}
}
fclose(test);
}
if(!valid)
{
fs_path_free(ptr);
}
}
}
} // cfhdrNEXT_CABINET
} // is MS CAB
fclose(fr);
} // fr != NULL
}
/**
* Unpack file form CAB archive, multivolume archives are supported too
*
* @param srccab: path to archive
* @param infilename: filename in archive to extract
* @param out: path to extact (with filename)
*
* @return: number of extracet files (should be 0 or 1)
**/
int cab_unpack(const char *srccab, const char *infilename, const char *out, scanned_files_list_t *sclist)
{
int cnt = 0;
char *search;
struct mscab_decompressor *cabd;
struct mscabd_cabinet *cab;
struct mscabd_cabinet *cabptr;
struct mscabd_file *file;
cab_list_item_t *item;
cab_list_item_t *item_first_cab = NULL;
cab_list_item_t *item_last_cab = NULL;
// printf("CAB: %s\n", srccab);
if((cabd = mspack_create_cab_decompressor(NULL)) == NULL)
{
return 0;
}
cab_list_t prevlist;
cab_list_t nextlist;
cab_list_init(&prevlist);
cab_list_init(&nextlist);
/* add file to list */
if(sclist != NULL) scanned_files_list_add(sclist, fs_basename(srccab));
/* search for archive previous parts */
search = (char*)srccab;
do
{
char *prev = NULL;
cab_analyze(search, &prev, NULL);
if(prev != NULL)
{
item = cab_list_add(&prevlist);
if(item != NULL)
{
item->filename = prev;
item->cab = cabd->open(cabd, item->filename);
if(sclist != NULL) scanned_files_list_add(sclist, item->filename);
// printf("PREV: %s\n", item->filename);
}
}
search = prev;
} while(search != NULL);
/* search for archive next parts */
search = (char*)srccab;
do
{
char *next = NULL;
cab_analyze(search, NULL, &next);
if(next != NULL)
{
item = cab_list_add(&nextlist);
if(item != NULL)
{
item->filename = next;
item->cab = cabd->open(cabd, item->filename);
if(sclist != NULL) scanned_files_list_add(sclist, item->filename);
// printf("NEXT: %s\n", item->filename);
}
}
search = next;
} while(search != NULL);
cab = cabd->open(cabd, srccab);
if(cab)
{
/* links archives - prev */
cabptr = cab;
for(item = prevlist.first; item != NULL; item = item->next)
{
if(item->cab)
{
cabd->prepend(cabd, cabptr, item->cab);
cabptr = item->cab;
}
if(item->next == NULL)
{
item_first_cab = item;
}
}
/* links archives - next */
cabptr = cab;
for(item = nextlist.first; item != NULL; item = item->next)
{
if(item->cab)
{
cabd->append(cabd, cabptr, item->cab);
cabptr = item->cab;
}
if(item->next == NULL)
{
item_last_cab = item;
}
}
cabptr = cab;
if(item_first_cab != NULL && item_first_cab->cab != NULL)
{
/* try start with first archive */
cabptr = item_first_cab->cab;
}
for (file = cabptr->files; file; file = file->next)
{
if(istrcmp(file->filename, infilename) == 0)
{
/* nice message for user */
char *cab_first = NULL;
char *cab_last = NULL;
if(item_first_cab == NULL && item_last_cab == NULL)
{
cab_first = fs_basename(srccab);
if(cab_first)
{
printf("Extracting %s from %s ... ", file->filename, cab_first);
fs_path_free(cab_first);
}
}
else
{
if(item_first_cab != NULL)
{
cab_first = fs_basename(item_first_cab->filename);
}
else
{
cab_first = fs_basename(srccab);
}
if(item_last_cab != NULL)
{
cab_last = fs_basename(item_last_cab->filename);
}
else
{
cab_last = fs_basename(srccab);
}
if(cab_first != NULL && cab_last != NULL)
{
printf("Extracting %s from multivolume (%s - %s) ... ", file->filename, cab_first, cab_last);
}
if(cab_first != NULL) fs_path_free(cab_first);
if(cab_last != NULL) fs_path_free(cab_last);
}
int t = cabd->extract(cabd, file, out);
if(t == MSPACK_ERR_OK)
{
printf("SUCCESS\n");
cnt++;
break;
}
else if(t == MSPACK_ERR_OPEN) /* R/O destination */
{
printf("FAILURE (cannot create file!)\n");
}
else if(t == MSPACK_ERR_NONEXT) /* special for multivolume error (no next file) */
{
printf("FAILURE (multivolume archive - next archive part is missing)\n");
}
else if(t == MSPACK_ERR_NOPREV) /* special for multivolume error (no previous file) */
{
printf("FAILURE (multivolume archive - previous archive part is missing)\n");
}
else
{
printf("FAILURE (%d)\n", t);
}
} // infilename
} // for files
cabd->close(cabd, cab);
} // cab
cab_list_clear(&prevlist);
cab_list_clear(&nextlist);
mspack_destroy_cab_decompressor(cabd);
return cnt;
}
/**
* Search in folder for CAB files and in them search for specified file. If file names is found function will stop
* search for next archives.
*
* @param dirname: dirname with cab files
* @param infilename: filename in archive to extract
* @param out: path to extact (with filename)
*
* @return: number of extracet files (should be 0 or 1)
*
**/
int cab_search_unpack(const char *dirname, const char *infilename, const char *out)
{
fs_dir_t *dir = fs_dir_open(dirname);
int cnt = 0;
const char *fn;
scanned_files_list_t sclist;
scanned_files_list_init(&sclist);
if(dir)
{
while((fn = fs_dir_read(dir, FS_FILTER_FILE)) != NULL)
{
if(fs_ext_match(fn, "cab") != 0)
{
char *cabfile = fs_path_get(dirname, fn, NULL);
if(!scanned_files_lookup(&sclist, fn))
{
if(cab_unpack(cabfile, infilename, out, &sclist) > 0)
{
//printf("extracted: %s\n", infilename);
cnt++;
}
fs_path_free(cabfile);
}
}
if(cnt > 0)
{
break;
}
}
fs_dir_close(&dir);
}
scanned_files_clean(&sclist);
return cnt;
}
/**
* Extract driver form VMM32.VXD or diffent W3/W4 file.
*
* @param src: path to W3/W4 file
* @param infilename: driver in archive to extract (without *.VXD extension)
* @param out: path to extact (with filename)
* @param tmpname: path to temporary file in writeable location
*
* @return: PATCH_OK on success otherwise one of PATCH_E_* error code
***/
int wx_unpack(const char *src, const char *infilename, const char *out, const char *tmpname)
{
dos_header_t dos, dos2;
pe_header_t pe, pe2;
pe_w3_t *w3;
FILE *fp, *fp2;
int t;
int status = PATCH_OK;
int exist_temp = 0;
if(fs_file_exists(tmpname))
{
fp = FOPEN_LOG(tmpname, "rb");
exist_temp = 1;
}
else
{
fp = FOPEN_LOG(src, "rb");
}
if(fp)
{
t = pe_read(&dos, &pe, fp);
if(t == PE_W3)
{
w3 = pe_w3_read(&dos, &pe, fp);
if(w3 != NULL)
{
char *path_without_ext = fs_path_get(NULL, infilename, "");
int status_extract = 0;
if(path_without_ext != NULL)
{
status_extract = pe_w3_extract(w3, path_without_ext, out);
fs_path_free(path_without_ext);
}
else
{
status_extract = pe_w3_extract(w3, infilename, out);
}
if(status_extract == PE_OK)
{
status = PATCH_OK;
}
pe_w3_free(w3);
}
else
{
status = PATCH_E_READ;
}
fclose(fp);
}
else if(t == PE_W4)
{
fclose(fp);
if(exist_temp)
{
status = PATCH_E_CONVERT;
}
else
{
if((status = wx_to_w3(src, tmpname)) == PATCH_OK)
{
fp2 = FOPEN_LOG(tmpname, "rb");
if(fp2 != NULL)
{
if(pe_read(&dos2, &pe2, fp2) == PE_W3)
{
w3 = pe_w3_read(&dos2, &pe2, fp2);
if(w3 != NULL)
{
char *path_without_ext = fs_path_get(NULL, infilename, "");
int status_extract = 0;
if(path_without_ext != NULL)
{
status_extract = pe_w3_extract(w3, path_without_ext, out);
fs_path_free(path_without_ext);
}
else
{
status_extract = pe_w3_extract(w3, infilename, out);
}
if(status_extract == PE_OK)
{
status = PATCH_OK;
}
pe_w3_free(w3);
}
else
{
//printf("pe_w3_read FAIL\n");
status = PATCH_E_READ;
}
}
fclose(fp2);
fs_unlink(tmpname);
}
else
{
status = PATCH_E_READ;
}
} // wx_to_w3
}
}
else
{
fclose(fp);
status = PATCH_E_WRONG_TYPE;
}
} // fopen
else
{
status = PATCH_E_READ;
}
return status;
}
/**
* Convert W4/W3 file to W3 file. If file is already in W3 format only copy it.
*
* @param in: input filename
* @param out: output filename
*
* @return: PATCH_OK on success
**/
int wx_to_w3(const char *in, const char *out)
{
FILE *fp;
dos_header_t dos;
pe_header_t pe;
pe_w4_t *w4;
int t;
int status = PATCH_E_CONVERT;
fp = FOPEN_LOG(in, "rb");
if(fp)
{
t = pe_read(&dos, &pe, fp);
if(t == PE_W4)
{
w4 = pe_w4_read(&dos, &pe, fp);
if(w4 != NULL)
{
if(pe_w4_to_w3(w4, out) == PE_OK)
{
status = PATCH_OK;;
}
pe_w4_free(w4);
}
}
else if(t == PE_W3)
{
FILE *fw = fopen(out, "wb");
if(fw)
{
fseek(fp, 0, SEEK_SET);
fs_file_copy(fp, fw, 0);
status = PATCH_OK;
fclose(fw);
}
}
fclose(fp);
}
return status;
}
/**
* Convert W4/W3 file to W4 file. If file is already in W4 format only copy it.
*
* @param in: input filename
* @param out: output filename
*
* @return: PATCH_OK on success
**/
int wx_to_w4(const char *in, const char *out)
{
FILE *fp;
dos_header_t dos;
pe_header_t pe;
pe_w3_t *w3;
int t;
int status = PATCH_E_CONVERT;
fp = FOPEN_LOG(in, "rb");
if(fp)
{
t = pe_read(&dos, &pe, fp);
if(t == PE_W3)
{
w3 = pe_w3_read(&dos, &pe, fp);
if(w3 != NULL)
{
if(pe_w3_to_w4(w3, out) == PE_OK)
{
status = PATCH_OK;;
}
pe_w3_free(w3);
}
}
else if(t == PE_W4)
{
FILE *fw = fopen(out, "wb");
if(fw)
{
fseek(fp, 0, SEEK_SET);
fs_file_copy(fp, fw, 0);
status = PATCH_OK;
fclose(fw);
}
}
fclose(fp);
}
return status;
}
/* context listing - CABS */
struct cab_filelist
{
struct mscab_decompressor *cabd;
struct mscabd_cabinet *cab;
struct mscabd_file *file;
};
/**
* Open cab for file listing
*
**/
cab_filelist_t *cab_filelist_open(const char *file)
{
cab_filelist_t *list = malloc(sizeof(cab_filelist_t));
list->cabd = NULL;
list->cab = NULL;
list->file = NULL;
if((list->cabd = mspack_create_cab_decompressor(NULL)) == NULL)
{
free(list);
return NULL;
}
list->cab = list->cabd->open(list->cabd, file);
if(list->cab == NULL)
{
mspack_destroy_cab_decompressor(list->cabd);
free(list);
return NULL;
}
list->file = list->cab->files;
return list;
}
/**
* Return file name and move pointer to another file
*
**/
const char *cab_filelist_get(cab_filelist_t *list)
{
if(list->file != NULL)
{
const char *s = list->file->filename;
list->file = list->file->next;
return s;
}
return NULL;
}
/**
* Close CAB
*
**/
void cab_filelist_close(cab_filelist_t *list)
{
if(list->cab)
{
list->cabd->close(list->cabd, list->cab);
}
if(list->cabd)
{
mspack_destroy_cab_decompressor(list->cabd);
}
free(list);
}
/* context listting - VXD */
struct vxd_filelist
{
pe_w3_t *w3;
size_t act;
const char *tmp;
};
/**
* Open VXD (W3/W4) for file listting, if W4 convert to W3 using tmp
*
**/
vxd_filelist_t *vxd_filelist_open(const char *file, const char *tmp)
{
dos_header_t dos;
pe_header_t pe;
int type;
FILE *fr;
vxd_filelist_t *list = malloc(sizeof(vxd_filelist_t));
if(list == NULL)
{
return NULL;
}
list->w3 = NULL;
list->act = 0;
list->tmp = NULL;
fr = fopen(file, "rb");
if(!fr)
{
free(list);
return NULL;
}
type = pe_read(&dos, &pe, fr);
if(type == PE_W3)
{
list->w3 = pe_w3_read(&dos, &pe, fr);
fclose(fr);
}
else if(type == PE_W4)
{
fclose(fr);
if(wx_to_w3(file, tmp) == PATCH_OK)
{
fr = fopen(tmp, "rb");
if(fr)
{
type = pe_read(&dos, &pe, fr);
if(type == PE_W3)
{
list->w3 = pe_w3_read(&dos, &pe, fr);
list->tmp = tmp;
}
fclose(fr);
}
}
}
else
{
fclose(fr);
}
if(list->w3 == NULL)