-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaswcx.c
536 lines (462 loc) · 13.5 KB
/
caswcx.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
/*
===========================================================
$Name: caswcx.c
$Desc: Open MSX CAS files
$Author: NataliaPC <natalia.pujol@gmail.com>
$Revision: 1.3
$Comments: https://github.com/nataliapc/casMSXwcx
===========================================================
*/
#include "wcxapi.h"
#include "win2nix_binds.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef __MINGW32__
#include <unistd.h>
#endif
#include "types.h"
#include "defs.h"
#include "resource.h"
/*===========================================================
Constants
*/
#define CAS_PACKED 0x0001
#define FIXED_DATE ((1984 - 1980) << 25 | 1 << 21 | 1 << 16 | 0 << 11 | 0 << 5 | 0/2)
#define CAS_HDR_LEN 8
const char CAS_HEADER[] = "\x1F\xA6\xDE\xBA\xCC\x13\x7D\x74";
#define HDR_LEN 10
const char HEAD_BIN[] = "\xD0\xD0\xD0\xD0\xD0\xD0\xD0\xD0\xD0\xD0";
const char HEAD_BAS[] = "\xD3\xD3\xD3\xD3\xD3\xD3\xD3\xD3\xD3\xD3";
const char HEAD_ASC[] = "\xEA\xEA\xEA\xEA\xEA\xEA\xEA\xEA\xEA\xEA";
const char NAME_HBIN[] = "HEADER_BIN";
const char NAME_HASC[] = "HEADER_ASC";
const char NAME_HBAS[] = "HEADER_BAS";
const char NAME_DATA[] = "DATA";
const char CMT_HEAD_BIN[] = "Standard Binary Header";
const char CMT_HEAD_BAS[] = "Standard Tokenized Basic Header";
const char CMT_HEAD_ASC[] = "Standard ASCII Basic Header";
const char CMT_DATA[] = "Standard Data Block";
const char szCfgKey[] = "casMSXwcx";
/*===========================================================
Structs
*/
typedef struct FileList_s {
char szFileName[MAX_PATH-9]; // File name translated
dword dwSize; // File size in bytes
byte *cData; // File data
dword crc32; // Data CRC32
char *szComment; // Text Comment
struct FileList_s *lpNext; // Next entry
} FileList_t;
typedef struct CAShandle_s {
WCHAR szCASname[MAX_PATH * 16]; // CAS file name
dword dwCASflags; // Flags
byte *cRawCAS;
long lSize;
FileList_t *lpFileListHead;
FileList_t *lpFileListCur;
FileList_t *lpFileListThis;
} CAShandle_t;
/*===========================================================
Global variables
*/
char szCfgFile[MAX_PATH]; //Path to .INI config file
char mInvalidReplacer; //Char replacement if invalid one in "Found:" name
BOOL mbEnableWarnings; //Enable warnings
BOOL mTrimBinaryData; //Remove final zeros from DATA end.
/*===========================================================
Internal functions
*/
void GetCfgPath(void)
{
#ifdef WINDOWS
char path[MAX_PATH];
if (GetModuleFileName(GetModuleHandle( NULL ), path, sizeof(path))) {
char *p = strrchr(path, '\\');
if (p)
*++p = '\0';
else
path[0] = '\0';
} else
path[0] = '\0';
strcpy(szCfgFile, path);
strcat(szCfgFile, "plugins\\wcx\\casMSX\\casMSX.ini");
#endif
}
dword crc32b(unsigned char *data, unsigned int len) {
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (len--) {
byte = data[i]; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
static void GetFoundString(FileList_t *pfl, char pInvalidReplacer, byte *pnameOut)
{
int pos=10, lastValidPos=10;
pnameOut[0] = '\0';
for (pos=10; pos<16; pos++) {
if (isalnum(pfl->cData[pos])) {
pnameOut[pos-10] = pfl->cData[pos];
lastValidPos = pos;
} else {
pnameOut[pos-10] = pInvalidReplacer;
}
}
pnameOut[lastValidPos-9] = '\0';
}
static void FreeCAShandler(CAShandle_t *lpH)
{
FileList_t *lpNext;
free(lpH->cRawCAS);
while (lpH->lpFileListHead) {
lpNext = lpH->lpFileListHead->lpNext;
free(lpH->lpFileListHead);
lpH->lpFileListHead = lpNext;
}
free(lpH);
}
static int MakeCASlist(CAShandle_t *lpH)
{
FILE *pFile;
int rc = 0, idx = 1;
long pos = 0, pos2, pos3;
byte foundName[7];
FileList_t *fl;
char *pName, *pCmt;
// Read CAS file
pFile = _wfopen (lpH->szCASname, L"rb");
if (pFile==NULL) return E_EOPEN;
// Get file size
fseek (pFile , 0 , SEEK_END);
lpH->lSize = ftell (pFile);
rewind (pFile);
// Read CAS file
lpH->cRawCAS = (byte*)malloc(lpH->lSize);
lpH->cRawCAS[0] = 0;
if (lpH->cRawCAS==NULL) return E_NO_MEMORY;
if (fread (lpH->cRawCAS, 1, lpH->lSize, pFile) != lpH->lSize) rc = E_EREAD;
if (fclose(pFile)) rc = E_ECLOSE;
if (memcmp(CAS_HEADER, (char*)lpH->cRawCAS, CAS_HDR_LEN) && lpH->lSize) rc = E_UNKNOWN_FORMAT;
if (!rc) {
// Search for CAS blocks
while (!rc && pos < lpH->lSize) {
pos += CAS_HDR_LEN;
if (pos >= lpH->lSize) { rc = E_BAD_DATA; break; }
// Create new block
fl = (FileList_t*)malloc(sizeof(FileList_t));
if (fl==NULL) { rc = E_NO_MEMORY; break; }
memset(fl, 0, sizeof(FileList_t));
// Search block end
pos2 = pos;
while (pos2 < lpH->lSize && memcmp(CAS_HEADER, &lpH->cRawCAS[pos2], CAS_HDR_LEN)) {
pos2++;
}
// Trim last zeros
pos3 = pos2;
if (mTrimBinaryData) {
while (pos3 > pos && lpH->cRawCAS[pos3-1]=='\0') pos3--;
}
// Found & Fill struct
fl->dwSize = pos3-pos;
fl->cData = lpH->cRawCAS + pos;
fl->crc32 = crc32b(fl->cData, fl->dwSize);
pName = pCmt = NULL;
if (fl->dwSize==16) {
GetFoundString(fl, mInvalidReplacer, foundName);
if (!memcmp(HEAD_BIN, fl->cData, HDR_LEN)) {
pName = (char*)NAME_HBIN;
pCmt = (char*)CMT_HEAD_BIN;
}
if (!memcmp(HEAD_ASC, fl->cData, HDR_LEN)) {
pName = (char*)NAME_HASC;
pCmt = (char*)CMT_HEAD_ASC;
}
if (!memcmp(HEAD_BAS, fl->cData, HDR_LEN)) {
pName = (char*)NAME_HBAS;
pCmt = (char*)CMT_HEAD_BAS;
}
sprintf(fl->szFileName, "%02u_%s[%s].%08lx", idx, pName, foundName, fl->crc32);
fl->szComment = pCmt;
}
if (pName==NULL) {
sprintf(fl->szFileName, "%02u_%s.%08lx", idx, NAME_DATA, fl->crc32);
fl->szComment = (char*)CMT_DATA;
}
// Set pointers
if (lpH->lpFileListHead==NULL) lpH->lpFileListHead = fl;
if (lpH->lpFileListCur) lpH->lpFileListCur->lpNext = fl;
lpH->lpFileListCur = fl;
fl->lpNext = NULL;
// Pos to next block
idx++;
pos = pos2;
}
}
if (rc) FreeCAShandler(lpH);
return rc;
}
/*
=====================================================================
Function name : STDCALL OpenArchive
Description : Main entry point to Windows Commander
Return type : WCX_API HANDLE
Argument : tOpenArchiveDataW *ArchiveData
*/
HANDLE CAS_OpenArchive(tOpenArchiveDataW *ArchiveData)
{
CAShandle_t *lpH = (CAShandle_t *)malloc(sizeof(CAShandle_t));
char szBuf[256] __attribute__((unused));
// Check for out of memory
if(lpH==NULL) {
ArchiveData->OpenResult = E_NO_MEMORY;
return 0;
}
memset(lpH, 0, sizeof(CAShandle_t));
#ifdef WINDOWS
GetCfgPath();
GetPrivateProfileString(szCfgKey, "EnableWarning", "1", szBuf, sizeof(szBuf), szCfgFile);
mbEnableWarnings = atoi(szBuf);
GetPrivateProfileString(szCfgKey, "InvalidCharReplacer", "_", szBuf, sizeof(szBuf), szCfgFile);
mInvalidReplacer = szBuf[0];
GetPrivateProfileString(szCfgKey, "TrimBinaryData", "0", szBuf, sizeof(szBuf), szCfgFile);
mTrimBinaryData = atoi(szBuf);
#else
mbEnableWarnings = 1;
mInvalidReplacer = '_';
#endif
// Copy archive name to handler
wcslcpy(lpH->szCASname, ArchiveData->ArcName, sizeof(lpH->szCASname));
// Default responding
ArchiveData->OpenResult = E_BAD_ARCHIVE;
// Read and create CAS list
if ((ArchiveData->OpenResult = MakeCASlist(lpH))) {
free(lpH);
return 0;
}
// Reset entry list
lpH->lpFileListCur = lpH->lpFileListHead;
return (HANDLE)lpH;
}
/*
=====================================================================
Function name : STDCALL ReadHeader
Description : Totalcmd find out what files are in the archive.
Return type : int (error code or 0)
Argument : WCX_API HANDLE hArcData
Argument : tHeaderDataExW *HeaderData
*/
int CAS_ReadHeader(HANDLE hArcData, tHeaderDataExW *HeaderData)
{
CAShandle_t *lpH = (CAShandle_t *)hArcData;
FileList_t *fl;
if (lpH->lpFileListCur) {
fl = lpH->lpFileListCur;
awfilenamecopy(HeaderData->FileName, fl->szFileName);
HeaderData->FileTime = FIXED_DATE;
HeaderData->FileAttr = 0x20;
HeaderData->FileCRC = fl->crc32;
HeaderData->PackSize = fl->dwSize + strlen(CAS_HEADER);
HeaderData->UnpSize = fl->dwSize;
HeaderData->CmtBuf = fl->szComment;
HeaderData->CmtSize = strlen(fl->szComment);
HeaderData->CmtBufSize = 80;
lpH->lpFileListThis = lpH->lpFileListCur;
lpH->lpFileListCur = lpH->lpFileListCur->lpNext;
return 0;
}
return E_END_ARCHIVE;
}
/*
=====================================================================
Function name : STDCALL ProcessFile
Description : Should unpack the specified file or test the
integrity of the archive.
Return type : int (error code or 0)
Argument : WCX_API HANDLE hArcData
Argument : int Operation
Argument : char *DestPath
Argument : char *DestName
*/
int CAS_ProcessFile(HANDLE hArcData, int Operation, WCHAR *DestPath, WCHAR *DestName)
{
FileList_t *fl = ((CAShandle_t *)hArcData)->lpFileListThis;
FILE *pFile;
switch (Operation) {
case PK_SKIP:
case PK_TEST:
return 0;
case PK_EXTRACT:
if (DestName != NULL) {
// Write Raw Block file
if ((pFile=_wfopen (DestName, L"wb"))==NULL) return E_EOPEN;
if (fwrite(fl->cData, 1, fl->dwSize, pFile) != fl->dwSize) return E_EWRITE;
if (fclose(pFile)) return E_ECLOSE;
} else {
return E_EOPEN;
}
return 0;
}
return E_NOT_SUPPORTED;
}
/*
=====================================================================
Function name : STDCALL CloseArchive
Description : Perform all necessary operations when archive
is about to be closed.
Return type : int (error code or 0)
Argument : WCX_API HANDLE hArcData
*/
int CAS_CloseArchive(HANDLE hArcData)
{
CAShandle_t *lpH = (CAShandle_t *)hArcData;
//Free all inner Handler data
FreeCAShandler(lpH);
return 0;
}
/*
=====================================================================
Function name : PackFiles
Description : Specifies what should happen when a user creates,
or adds files to the archive.
Return type : int (error code or 0)
Argument : WCHAR *PackedFile
Argument : WCHAR *SubPath
Argument : WCHAR *SrcPath
Argument : WCHAR *AddList
Argument : int Flags
*/
int CAS_PackFiles (WCHAR *PackedFile, WCHAR *SubPath, WCHAR *SrcPath, WCHAR *AddList, int Flags)
{
word rc = 0;
char *packedfile = (char*)malloc(MAX_PATH*16);
WCHAR *fullpath = (WCHAR*)malloc(MAX_PATH*16);
byte *buff = (byte*)malloc(2048);
long bsize;
FILE *fin=NULL, *fout=NULL;
if (packedfile==NULL || fullpath==NULL || buff==NULL) {
rc = E_NO_MEMORY;
} else {
// Open output file
if ((fout = _wfopen(PackedFile, L"a+b"))==NULL) rc = E_EOPEN;
while (*AddList && !rc) {
// Open input file
wcslcpy(fullpath, SrcPath, MAX_PATH*16);
wcslcat(fullpath, AddList, MAX_PATH*16);
if ((fin = _wfopen(fullpath, L"rb"))==NULL) {
rc = E_EOPEN;
break;
}
// Write CAS Block Header
if (fwrite(CAS_HEADER, 1, CAS_HDR_LEN, fout)!=CAS_HDR_LEN) {
rc = E_EWRITE;
break;
}
// Dump input file to output file
while ((bsize = fread(buff, 1, 2048, fin))) {
if (fwrite(buff, 1, bsize, fout)!=bsize) {
rc = E_EWRITE;
break;
}
}
fclose(fin);
// Delete origin file if it's a move order
if (!rc && (Flags & PK_PACK_MOVE_FILES)) {
DeleteFileT(fullpath);
}
// Go to next file to be added
while(*AddList) AddList++;
AddList++;
}
if (fout) fclose(fout);
}
free(packedfile);
free(fullpath);
free(buff);
return rc;
}
/*
=====================================================================
Function name : DeleteFiles
Description : Delete file(s) from CAS file
Return type : int (error code or 0)
Argument : WCHAR *PackedFile
Argument : WCHAR *DeleteList
*/
int CAS_DeleteFiles (WCHAR *PackedFile, WCHAR *DeleteList)
{
CAShandle_t *lpH = (CAShandle_t *)malloc(sizeof(CAShandle_t));
FileList_t *fl;
char buff[MAX_PATH];
word rc = 0;
BOOL anyFileDeleted = FALSE;
FILE *fout;
if (!lpH) return E_NO_MEMORY;
memset(lpH, 0, sizeof(CAShandle_t));
wcslcpy(lpH->szCASname, PackedFile, sizeof(lpH->szCASname));
if ((rc = MakeCASlist(lpH))) {
free(lpH);
}
if (!rc) {
while (*DeleteList) {
wafilenamecopy(buff, DeleteList);
// Search file to delete
fl = lpH->lpFileListHead;
do {
if (!strcmp(buff, fl->szFileName)) {
// Reset found node
lpH->lSize -= fl->dwSize;
fl->szFileName[0] = '\0';
fl->cData = NULL;
fl->dwSize = 0;
anyFileDeleted = TRUE;
}
fl = fl->lpNext;
} while (fl!=NULL);
// Go to next file to be delete
while(*DeleteList) DeleteList++;
DeleteList++;
}
if (anyFileDeleted) {
// Save file without deleted blocks
fout = _wfopen(PackedFile, L"wb");
fl = lpH->lpFileListHead;
do {
if (fl->cData != NULL) {
fwrite(CAS_HEADER, 1, CAS_HDR_LEN, fout);
fwrite(fl->cData, 1, fl->dwSize, fout);
}
fl = fl->lpNext;
} while (fl!=NULL);
fclose(fout);
}
FreeCAShandler(lpH);
}
return rc;
}
/*
=====================================================================
Function name : GetPackerCaps
Description : Tells Totalcmd what features packer plugin supports.
Return type : int
*/
int CAS_GetPackerCaps(void)
{
// Return capabilities
return PK_CAPS_NEW|
PK_CAPS_MODIFY|
PK_CAPS_MULTIPLE| // Archive can contain multiple files
PK_CAPS_DELETE; // Can delete files
}