Subversion Repositories spk

Rev

Rev 40 | Rev 42 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include "CatFile.h"
2
#include <time.h>
3
#include "File.h"
4
#include "DirIO.h"
5
 
6
CCatFile::CCatFile ()
7
{
8
	m_iDataType = CATFILE_NONE;
9
	m_sData = NULL;
10
	m_lSize = 0;
11
	m_bCreate = false;
12
}
13
 
14
CCatFile::~CCatFile ()
15
{
16
	for ( SInCatFile *c = m_lFiles.First(); c; c = m_lFiles.Next() )
17
	{
18
		if ( c->sData )
19
			delete c->sData;
20
		delete c;
21
	}
22
	m_lFiles.clear();
23
}
24
 
25
bool CCatFile::IsAddonDir(CyString dir)
26
{
27
	CyString d = dir;
28
	d = d.FindReplace("\\", "/");
29
	d = d.GetToken("/", 1, 1);
30
 
31
	if ( d.Compare("types") )
32
		return true;
33
	if ( d.Compare("t") )
34
		return true;
35
	if ( d.Compare("maps") )
36
		return true;
37
	if ( d.Compare("director") )
38
		return true;
39
	if ( d.Compare("cutscenes") )
40
		return true;
41
	return false;
42
}
43
 
44
bool CCatFile::Opened(int error, bool allowCreate) 
45
{
46
	if ( error == CATERR_NONE ) return true;
47
	if ( allowCreate && error == CATERR_CREATED ) return true;
48
	return false;
49
}
50
int CCatFile::Open ( CyString catfile, CyString addon, int readtype, bool create )
51
{
52
	m_bCreate = false;
53
 
54
	CyString datfile;
55
	if ( !catfile.Right(4).Compare(".cat") )
56
	{
57
		datfile = catfile + ".dat";
58
		catfile += ".cat";
59
	}
60
	else
61
		datfile = catfile.Left ( -4 ) + ".dat";
62
 
63
	if ( readtype == CATREAD_JUSTCONTENTS )
64
		create = false;
65
 
66
	// first check if the dat file exists and opens
67
	bool created = false;
68
	if ( readtype != CATREAD_JUSTCONTENTS )
69
	{
70
		FILE *id = fopen ( datfile.c_str(), "rb+" );
71
		if ( !id )
72
		{
73
			if ( create )
74
				created = true;
75
			else
76
				return CATERR_NODATFILE;
77
		}
78
 
79
		if ( !created && id )
80
			fclose ( id );
81
	}
82
 
83
	// now open the cat file to read
84
	FILE *cid = fopen ( catfile.c_str(), "rb" );
85
	if ( !cid )
86
	{
87
		if ( create )
88
			created = true;
89
		else
90
			return CATERR_NOCATFILE;
91
	}
92
 
93
	if ( created )
94
	{
95
		m_fCatFile.Open ( catfile );
96
		m_fDatFile.Open ( datfile );
97
		m_bCreate = true;
98
		return CATERR_CREATED;
99
	}
100
 
101
	// find the file size
102
	fseek ( cid, 0, SEEK_END );
103
	size_t lFileSize = ftell ( cid );
104
	fseek ( cid, 0, SEEK_SET );
105
 
106
	if ( !lFileSize )
107
	{
108
		fclose ( cid );
109
		return CATERR_FILEEMPTY;
110
	}
111
 
112
	// size must be multiples of 5
113
	size_t size = lFileSize + ((lFileSize % 5) ? 5 - (lFileSize % 5) : 0);
114
 
115
	// read cat to buffer
116
	m_sData = new unsigned char[size + 1];
117
	m_sData[lFileSize] = 0;
118
	m_lSize = size;
119
	fread ( m_sData, sizeof(unsigned char), m_lSize, cid );
120
	if ( ferror(cid)  )
121
	{
122
		RemoveData ();
123
		fclose ( cid );
124
		return CATERR_READCAT;
125
	}
126
 
127
	m_iDataType = CATFILE_READ;
128
 
129
	fclose ( cid );
130
 
131
	if ( readtype != CATREAD_CAT )
132
	{
133
		if ( !DecryptData () )
134
			return CATERR_DECRYPT;
135
		m_sData[lFileSize] = 0;
136
 
137
		ReadFiles ();
138
	}
139
 
140
	m_sAddonDir = addon;
141
 
142
	m_fCatFile.Open ( catfile );
143
 
144
	if ( readtype != CATREAD_JUSTCONTENTS )
145
	{
146
		m_fDatFile.Open ( datfile );
147
 
148
		// check the file size matches
149
		long compare = 0;
150
		FILE *id = fopen ( datfile.c_str(), "rb" );
151
		if ( id )
152
		{
153
			fseek ( id, 0, SEEK_END );
154
			compare = ftell ( id );
155
			fclose ( id );
156
		}
157
		/*
158
		SInCatFile *c = m_lFiles.Back ()->Data();
159
		if ( (c->lSize + c->lOffset) != compare )
160
			return CATERR_MISMATCH;
161
		*/
162
	}
163
 
164
 
165
	if ( readtype >= CATREAD_DAT )
166
		LoadDatFile ();
167
 
168
	return CATERR_NONE;
169
}
170
 
171
bool CCatFile::RemoveFile ( CyString file )
172
{
173
	if ( !m_sAddonDir.Empty() ) {
174
		SInCatFile *f = FindData ( m_sAddonDir + "\\" + file );
175
		if ( f )
176
			RemoveFile(f);
177
	}
178
	{
179
		SInCatFile *f = FindData ( CyString("addon\\") + file );
180
		if ( f )
181
			RemoveFile(f);
182
	}
183
	SInCatFile *f = FindData ( file );
184
	if ( !f )
185
	{
186
		m_iError = CATERR_NOFILE;
187
		return false;
188
	}
189
 
190
	return RemoveFile ( f );
191
}
192
 
193
void CCatFile::LoadDatFile ()
194
{
195
	if ( m_fDatFile.NoFile() )
196
		return;
197
 
198
	FILE *id = fopen ( m_fDatFile.GetFullFilename().c_str(), "rb" );
199
	if ( id )
200
	{
201
		for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() )
202
		{
203
			SInCatFile *c = node->Data();
204
			if ( c->sData )
205
				delete c->sData;
206
			c->sData = new unsigned char[c->lSize + 1];
207
			size_t readAmount = fread ( c->sData, sizeof(unsigned char), c->lSize, id );
208
			if ( readAmount != c->lSize )
209
                break;
210
 
41 cycrow 211
			DecryptDAT(c);
1 cycrow 212
		}
213
		fclose ( id );
214
	}
215
}
216
 
217
bool CCatFile::ReadFiles ()
218
{
219
	int num = 0;
220
 
221
	size_t offset = 0;
222
 
223
	unsigned char *data = m_sData;
224
	int spacePos = -1;
225
	while ( m_sData[num] != '\0' )
226
	{
227
		if ( m_sData[num] == '\n' )
228
		{
229
			m_sData[num] = 0;
230
			if ( spacePos != -1 )
231
			{
232
				CyString file;
233
				int size = 0;
234
 
235
				m_sData[spacePos] = '\0';
236
				file = (char *)data;
237
				data = (m_sData + (spacePos + 1));
238
				if ( !CyString((char *)data).IsNumber() )
239
					size = 0;
240
				else
241
					size = atoi((const char *)data);
242
 
243
				if ( size )
244
				{
245
					SInCatFile *catfile = new SInCatFile;
246
 
247
					catfile->lSize = size;
248
					catfile->sFile = file;
249
					catfile->lOffset = offset;
250
					catfile->sData = 0;
251
 
252
					offset += catfile->lSize;
253
 
254
					m_lFiles.push_back ( catfile );
255
				}
256
			}
257
			data = (m_sData + (num + 1));
258
			spacePos = -1;
259
		}
260
		else if ( m_sData[num] == ' ' )
261
			spacePos = num;
262
		++num;
263
	}
264
 
265
	/*
266
	CyString s((const char*)m_sData);
267
	CyString *lines = s.SplitToken ( '\n', &num );
268
 
269
 
270
	for ( int i = 1; i < num; i++ )
271
	{
272
		CyString l = lines[i];
273
		if ( l.NumToken ( ' ' ) <= 1 )
274
			continue;
275
 
276
		SInCatFile *catfile = new SInCatFile;
277
 
278
		catfile->lSize = l.GetToken ( l.NumToken ( ' ' ), ' ' ).ToLong();
279
		catfile->sFile = l.GetToken ( 0, l.NumToken ( ' ' ) - 1, ' ' );
280
		catfile->lOffset = offset;
281
		catfile->sData = 0;
282
 
283
		offset += catfile->lSize;
284
 
285
		m_lFiles.push_back ( catfile );
286
	}
287
*/
288
	return true;
289
}
290
 
291
bool CCatFile::DecryptData ( unsigned char *data, size_t size )
292
{
293
	unsigned char cl=0xDB, dh=0xDC, dl=0xDD, ah=0xDE, ch=0xDF, al;
294
	unsigned char *ptr = data, *end = data + size;
295
	for ( ptr = data; ptr < end; ptr += 5)
296
	{
297
		al=ch;
298
		ch+=5;
299
		*(ptr + 4) ^= al;
300
		al=ah;
301
		ah+=5;
302
		*(ptr + 3) ^= al;
303
		al=dl;
304
		dl+=5;
305
		*(ptr + 2) ^= al;
306
		al=dh;
307
		dh+=5;
308
		*(ptr + 1) ^= al;
309
		al=cl;
310
		cl+=5;
311
		*(ptr + 0) ^= al;
312
	}
313
 
314
	return true;
315
}
316
 
317
bool CCatFile::DecryptData ()
318
{
319
	if ( !DecryptData ( m_sData, m_lSize ) )
320
		return false;
321
 
322
	m_iDataType = CATERR_DECRYPT;
323
 
324
	return true;
325
}
326
 
327
void CCatFile::RemoveData ()
328
{
329
	if ( m_sData )
330
		delete m_sData;
331
	m_sData = NULL;
332
	m_iDataType = CATFILE_NONE;
333
	m_lSize = 0;
334
}
335
 
336
bool CCatFile::ReadFileToData ( CyString filename )
337
{
338
	SInCatFile *c = FindData ( filename );
339
	return ReadFileToData ( c );
340
}
341
 
342
bool CCatFile::ReadFileToData ( SInCatFile *c )
343
{
41 cycrow 344
	if ( !c ) return false;
1 cycrow 345
	size_t size = 0;
41 cycrow 346
	if ( !c->sData ) c->sData = ReadData ( c, &size );
347
	if ( c->sData )	return true;
1 cycrow 348
	return false;
349
}
350
 
351
unsigned char *CCatFile::ReadData ( SInCatFile *c, size_t *size )
352
{
353
	*size = c->lSize;
354
 
355
	FILE *id = fopen ( m_fDatFile.GetFullFilename().c_str(), "rb" );
356
	if ( id )
357
	{
358
		fseek ( id, (long)c->lOffset, SEEK_SET );
359
		unsigned char *data = new unsigned char[c->lSize + 1];
360
		fread ( data, sizeof(unsigned char), c->lSize, id );
361
		*size = c->lSize;
362
		data[c->lSize] = '\0';
363
 
41 cycrow 364
		DecryptDAT(data, c->lSize);
365
		c->bDecrypted = true;
1 cycrow 366
 
367
		fclose ( id );
368
 
369
		return data;
370
	}
371
 
372
	return NULL;
373
}
374
 
41 cycrow 375
void CCatFile::DecryptDAT(SInCatFile *pFile) 
376
{
377
	if ( pFile->bDecrypted ) return;
378
	pFile->bDecrypted = true;
379
	this->DecryptDAT(pFile->sData, pFile->lSize);
380
}
1 cycrow 381
void CCatFile::DecryptDAT(unsigned char *buffer, size_t size)
382
{
383
	for(unsigned char *pos=buffer, *end=buffer + size; pos < end; pos++){
384
		*pos^=0x33;
385
	}
386
}
387
unsigned char *CCatFile::ReadData ( CyString filename, size_t *size )
388
{
389
	*size = 0;
390
 
391
	if ( !m_fDatFile.NoFile() )
392
	{
393
		SInCatFile *c = FindData ( filename );
394
		if ( c )
395
			return ReadData ( c, size );
396
	}
397
 
398
	return NULL;
399
}
400
 
41 cycrow 401
unsigned char *CCatFile::UnpackFile ( SInCatFile *c, size_t *size)
1 cycrow 402
{
403
	*size = 0;
404
	if ( !c )
405
		return NULL;
406
 
41 cycrow 407
	this->DecryptDAT(c);
1 cycrow 408
 
41 cycrow 409
	int iFiletype = this->_checkFiletype(c->sData, 3);
410
 
411
	// plain file
412
	if ( iFiletype == FILETYPE_PLAIN ) {
413
		*size = c->lSize;
414
		return c->sData;
415
	}
416
 
417
	//otherwise unpack it
418
 
419
//	if ( IsDataPCK ( (const unsigned char *)c->sData, c->lSize ) || forcepck )
420
	/*int iOffset = 0;
421
	if ( iFiletype == FILETYPE_PCK ) {
422
		iOffset = 1;
423
		for(size_t i=0; i < c->lSize; i++){
424
			c->sData[i] ^= magic;
425
		}
426
	}
427
 
428
 
429
	return UnPCKData ( c->sData + iOffset, c->lSize - iOffset, size );*/
430
	return UnPCKData(c->sData, c->lSize, size, (iFiletype == FILETYPE_PCK) ? true : false);
1 cycrow 431
}
432
 
433
bool CCatFile::RemoveFile ( SInCatFile *f )
434
{
435
	if ( (m_bCreate) || (m_lFiles.empty()) )
436
		return false;
437
 
438
	if ( !f )
439
		return false;
440
 
441
	int err = m_fDatFile.TruncateFile ( f->lOffset, f->lSize );
442
	if ( err == FILEERR_TOSMALL )
443
	{
444
		if ( (int)m_fDatFile.GetFilesize() > this->GetEndOffset() )
445
			m_fDatFile.TruncateFile( this->GetEndOffset(), m_fDatFile.GetFilesize() - this->GetEndOffset() );
446
	}
447
	else if ( err != FILEERR_NONE )
448
		return false;
449
 
16 cycrow 450
	// adjust the file positions
40 cycrow 451
	int iOffset = -1;
16 cycrow 452
	for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() ) {
40 cycrow 453
		if ( node->Data() == f ) {
454
			iOffset = node->Data()->lOffset;
455
		}
456
		else if ( iOffset >= 0 ) {
16 cycrow 457
			node->Data()->lOffset = iOffset;
40 cycrow 458
			iOffset += node->Data()->lSize;
16 cycrow 459
		}
460
	}
461
 
1 cycrow 462
	// now just write the new cat file
463
	m_lFiles.remove ( f );
464
	WriteCatFile ();
465
 
466
	return true;
467
}
468
 
469
bool CCatFile::WriteCatFile ()
470
{
471
	if ( (m_bCreate) && (m_lFiles.empty()) )
472
		return false;
473
 
474
	CyString cat = m_fCatFile.GetFilename() + "\n";
475
 
476
	for ( SInCatFile *f = m_lFiles.First(); f; f = m_lFiles.Next() )
477
	{
478
		if ( f->sFile.Empty() )
479
			continue;
480
		cat += f->sFile.findreplace("/", "\\") + " " + (long)f->lSize + "\n";
481
	}
482
 
483
	if ( !cat.Length() )
484
		return false;
485
 
486
	size_t len = cat.Length();
16 cycrow 487
//	if ( len % 5)
488
//		len += (5 - (len % 5));
1 cycrow 489
 
490
	unsigned char *data = new unsigned char[len + 1];
491
	memcpy ( data, cat.c_str(), cat.Length() );
492
 
493
	for ( size_t i = len; i > cat.Length(); i-- )
494
		data[i] = '\0';
495
 
496
	DecryptData ( data, len );
497
 
498
	m_fCatFile.WriteData ( (const char *)data, len );
499
 
500
	return true;
501
}
502
 
503
bool CCatFile::CheckExtensionPck ( CyString filename )
504
{
505
	CyString ext = filename.GetToken ( ".", filename.NumToken  ( "." ) ).lower();
506
 
507
	if ( ext == "xml" )
508
		return true;
509
	else if ( ext == "txt" )
510
		return true;
511
	else if ( ext == "bob" )
512
		return true;
513
	else if ( ext == "bod" )
514
		return true;
515
 
516
	return false;
517
}
518
 
519
bool CCatFile::CheckPackedExtension ( CyString filename )
520
{
521
	CyString ext = filename.GetToken ( ".", filename.NumToken  ( "." ) ).lower();
522
 
523
	if ( ext == "pck" )
524
		return true;
525
	else if ( ext == "pbb" )
526
		return true;
527
	else if ( ext == "pbd" )
528
		return true;
529
 
530
	return false;
531
}
532
 
533
CyString CCatFile::PckChangeExtension ( CyString f )
534
{
535
	CFileIO fo ( f );
536
	CyString ext = fo.GetFileExtension ().lower();
537
	if ( ext == "txt" )
538
		return fo.ChangeFileExtension ( "pck" );
539
	else if ( ext == "xml" )
540
		return fo.ChangeFileExtension ( "pck" );
541
	else if ( ext == "bob" )
542
		return fo.ChangeFileExtension ( "pbb" );
543
	else if ( ext == "bod" )
544
		return fo.ChangeFileExtension ( "pbd" );
545
 
546
	return f;
547
}
548
 
549
bool CCatFile::AppendFile ( CyString filename, CyString to, bool pck, bool bXor )
550
{
551
	if ( (!m_bCreate) && (!m_fCatFile.Exists ()) )
552
		return false;
553
 
554
	if ( !m_sAddonDir.Empty() && CCatFile::IsAddonDir(to) ) {
555
		to = m_sAddonDir + "\\" + to;
556
	}
557
 
558
	if ( filename.IsIn ( "::" ) )
559
		return WriteFromCat ( filename.GetToken ( "::", 1, 1 ), filename.GetToken ( "::", 2, 2 ) );
560
 
561
	// first open the file to check it exists
562
	FILE *id = fopen ( filename.c_str(), "rb" );
563
	if ( !id )
564
		return false;
565
	fclose ( id );
566
 
567
	// then check if the file already exists
568
	if ( !m_lFiles.empty() )
569
	{
570
		SInCatFile *f = FindData ( to );
571
		if ( f )
572
		{
573
			if ( !RemoveFile ( f ) )
574
				return false;
575
		}
576
	}
577
 
578
	bool append = false;
579
 
580
	SInCatFile *f = new SInCatFile;
581
	f->sData = 0;
582
	f->lOffset = this->GetEndOffset();
583
 
584
	bool dofile = true;
585
	if ( pck && CheckExtensionPck(filename) )
586
	{
587
		to = PckChangeExtension ( to );
588
 
589
		if ( !m_lFiles.empty() )
590
		{
591
			SInCatFile *checkf = FindData ( to );
592
			if ( checkf )
593
			{
594
				if ( !RemoveFile ( checkf ) )
595
					return false;
596
			}
597
		}
598
	}
599
 
600
	if ( !m_lFiles.size() )
601
		m_fDatFile.WipeFile();
602
 
603
//	if ( CheckPackedExtension(to) && !CheckPackedExtension(filename) )
604
	{
605
		FILE *id = fopen ( filename.c_str(), "rb" );
606
		if ( !id )
607
			return false;
608
		fseek ( id, 0, SEEK_END );
609
		size_t size = ftell ( id );
610
		fseek ( id, 0, SEEK_SET );
611
		unsigned char *data = new unsigned char[size + 1];
612
		fread ( data, sizeof(unsigned char), size, id );
613
		data[size] = '\0';
614
		fclose ( id );
615
 
41 cycrow 616
		int iFileType = this->_checkFiletype(data, 3);
617
		if ( CheckPackedExtension(to) && iFileType == FILETYPE_PLAIN )
1 cycrow 618
		{
619
			size_t newsize = 0;
620
			unsigned char *newdata = PCKData ( data, size, &newsize, bXor );
621
 
622
			f->lSize = newsize;
41 cycrow 623
			this->DecryptDAT(newdata, f->lSize);
1 cycrow 624
			append = m_fDatFile.AppendDataToPos ( (const char *)newdata, newsize, f->lOffset );
625
 
626
			delete [] newdata;
627
		}
628
		else 
629
		{
630
			this->DecryptDAT(data, size);
631
			f->lSize = size;
41 cycrow 632
			append = m_fDatFile.AppendDataToPos ( (const char *)data, f->lSize, f->lOffset );
1 cycrow 633
		}
634
 
635
		delete [] data;
636
	}
637
 
638
	if ( append )
639
	{
640
		m_bCreate = false;
641
		f->sFile = to;
642
		m_lFiles.push_back ( f );
643
		WriteCatFile ();
644
	}
645
	else
646
		delete f;
647
 
648
	return true;
649
}
650
 
651
bool CCatFile::AddData ( CyString catfile, unsigned char *data, size_t size, CyString to, bool pck, bool create )
652
{
653
	int err = Open ( catfile, "", CATREAD_CATDECRYPT, create );
654
	if ( (err != CATERR_NONE) && (err != CATERR_CREATED) )
655
		return false;
656
 
657
	return AppendData ( data, size, to, pck );
658
}
659
 
660
int CCatFile::GetEndOffset()
661
{
662
	if ( m_lFiles.empty() )
663
		return 0;
664
	else
665
		return m_lFiles.Back()->Data()->lOffset + m_lFiles.Back()->Data()->lSize;
666
}
667
 
668
 
669
bool CCatFile::AppendData ( unsigned char *data, size_t size, CyString to, bool pck, bool bXor )
670
{
671
	if ( (!m_bCreate) && (!m_fCatFile.Exists ()) )
672
		return false;
673
 
674
	if ( (size <= 0) || (!data) )
675
		return false;
676
 
677
	// then check if the file already exists
678
	if ( !m_lFiles.empty() )
679
	{
680
		SInCatFile *f = FindData ( to );
681
		if ( f )
682
		{
683
			if ( !RemoveFile ( f ) )
684
				return false;
685
		}
686
	}
687
 
688
	bool append = false;
689
 
690
	if ( !m_lFiles.size() )
691
		m_fDatFile.WipeFile();
692
 
693
	SInCatFile *f = new SInCatFile;
694
	f->sData = 0;
695
	if ( m_lFiles.empty() )
696
		f->lOffset = 0;
697
	else
698
		f->lOffset = m_fDatFile.GetFilesize();
699
 
700
	// if file extension is packed but not the file, then pack it, "pck" forces it to be packed unless it already is
701
 
702
	f->lSize = size;
703
	if ( (((pck) && (CheckExtensionPck (to))) || ((CheckPackedExtension ( to )))) && (!IsDataPCK ( data, size )) )
704
	{
705
		to = PckChangeExtension ( to );
706
 
707
		if ( !m_lFiles.empty() )
708
		{
709
			SInCatFile *f = FindData ( to );
710
			if ( f )
711
			{
712
				if ( !RemoveFile ( f ) )
713
					return false;
714
			}
715
		}
716
		size_t newsize = 0;
717
		unsigned char *d = PCKData ( data, size, &newsize, bXor );
718
 
719
		f->lSize = newsize;
720
		this->DecryptDAT(d, newsize);
721
		append = m_fDatFile.AppendDataToPos ( (const char *)d, newsize, f->lOffset );
722
 
723
		delete [] d;
724
	}
725
	else {
726
		this->DecryptDAT(data, size);
727
		append = m_fDatFile.AppendDataToPos ( (const char *)data, size, f->lOffset );
728
	}
729
 
730
	if ( append )
731
	{
732
		m_bCreate = false;
733
		f->sFile = to;
734
		m_lFiles.push_back ( f );
735
		WriteCatFile ();
736
	}
737
	else
738
		delete f;
739
 
740
	return true;
741
}
742
 
743
CyString CCatFile::RenameFileExtension(SInCatFile *f)
744
{
745
	CFileIO fo ( f->sFile );
746
	CyString ext = fo.GetFileExtension ().lower();
747
	if ( ext == "pck" )
748
	{
749
		CyString firstDir = f->sFile.FindReplace("/", "\\").GetToken("\\", 1, 1).lower();
750
 
751
		if ( firstDir == "t" )
752
			return fo.ChangeFileExtension ( "xml" );
753
		else if ( firstDir == "director" )
754
			return fo.ChangeFileExtension ( "xml" );
755
		return fo.ChangeFileExtension ( "txt" );
756
	}
757
	else if ( ext == "pbb" )
758
		return fo.ChangeFileExtension ( "bob" );
759
	else if ( ext == "pbd" )
760
		return fo.ChangeFileExtension ( "bod" );
761
	return f->sFile;
762
}
763
 
764
void CCatFile::FindFiles(CyStringList *files, CyString filemask)
765
{
766
	if ( m_lFiles.empty() )
767
		return;
768
 
769
	for ( SInCatFile *f = m_lFiles.First(); f; f = m_lFiles.Next() )
770
	{
771
		if ( filemask.WildMatch(f->sFile) )
772
			files->PushBack(f->sFile);
773
	}
774
}
775
 
776
bool CCatFile::ExtractAll(CyString dir)
777
{
778
	if ( m_lFiles.empty() )
779
		return false;
780
 
781
	for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() )
782
	{
783
		if ( !ExtractFile(node->Data(), dir, true) )
784
			return false;
785
	}
786
 
787
	return true;
788
}
789
bool CCatFile::ExtractFile ( CyString filename, CyString to, bool preserve )
790
{
791
	if ( m_lFiles.empty() )
792
		return false;
793
 
794
	// check if file exists
795
	if ( !m_sAddonDir.Empty() ) {
796
		SInCatFile *f = FindData ( m_sAddonDir + "\\" + filename );
797
		if ( f )
798
			return ExtractFile ( f, to, preserve );
799
	}
800
	{
801
		SInCatFile *f = FindData ( CyString("addon\\") + filename );
802
		if ( f )
803
			return ExtractFile ( f, to, preserve );
804
	}
805
 
806
	SInCatFile *f = FindData ( filename );
807
	if ( !f )
808
	{
809
		m_iError = CATERR_NOFILE;
810
		return false;
811
	}
812
 
813
	return ExtractFile ( f, to, preserve );
814
}
815
 
41 cycrow 816
int CCatFile::_checkFiletype(const unsigned char *pBuffer, int iSize)
817
{
818
	if(iSize >= 3) {
819
		unsigned char magic = pBuffer[0] ^ 0xC8;
820
		if( (pBuffer[1] ^ magic) == 0x1F && (pBuffer[2] ^ magic) == 0x8B) {
821
			return FILETYPE_PCK;
822
		}
823
	}
824
 
825
	if ( iSize >= 2 && (pBuffer[0] == 0x1F && pBuffer[1] == 0x8B) ) {
826
		return FILETYPE_DEFLATE;
827
	}	
828
 
829
	return FILETYPE_PLAIN;
830
}
831
 
1 cycrow 832
bool CCatFile::ExtractFile ( SInCatFile *f, CyString to, bool preserve )
833
{
834
	unsigned char *data = 0;
835
	size_t size = 0;
836
 
837
	// load the data from file
838
	ReadFileToData ( f );
839
 
41 cycrow 840
	data = this->UnpackFile(f, &size);
1 cycrow 841
	if ( !data )
842
	{
843
		m_iError = CATERR_CANTREAD;
844
		return false;
845
	}
846
 
847
	CFileIO fo(CCatFile::RenameFileExtension(f));
848
	// check for a file name
849
	CyString checkFile = to;
850
	CyString todir, tofile = to;
851
	if ( checkFile.IsAnyIn("\\/") )
852
	{
853
		checkFile = checkFile.FindReplace ( "\\", "/" );
854
		checkFile = checkFile.FindReplace ( "//", "/" );
855
		tofile = checkFile.GetToken("/", checkFile.NumToken('/'));
856
 
857
		if ( !checkFile.IsIn(".") || preserve )
858
		{
859
			tofile = fo.GetFilename();
860
			todir = checkFile;
861
		}
862
		else
863
		{
864
			todir = CFileIO(checkFile).GetDir();
865
			tofile = CFileIO(checkFile).GetFilename();
866
		}
867
	}
868
 
869
	if ( tofile.Empty() )
870
	{
871
		if ( !tofile.Empty() )
872
			tofile += "/";
873
		tofile += fo.GetFilename();
874
		todir = CFileIO(tofile).GetDir();
875
		tofile = CFileIO(tofile).GetFilename();
876
	}
877
 
878
	if ( preserve )
879
	{
880
		if ( !fo.GetDir().Compare(todir.Right(- (int)fo.GetDir().Length())) )
881
		{
882
			if ( !todir.Empty() && todir.Right(1) != "/" )
883
				todir += "/";
884
			todir += fo.GetDir();
885
		}
886
	}
887
 
888
	if ( tofile.Empty() )
889
		tofile = fo.GetFilename();
890
 
891
	// create the directory to extract to
892
	if ( !todir.Empty() && !CDirIO(todir).Create() )
893
		m_iError = CATERR_CANTCREATEDIR;
894
	else
895
	{
896
		CyString file = todir;
897
		if ( !file.Empty() )
898
			file += "/";
899
		file += tofile;
900
		file = file.FindReplace("/", "\\");
901
		file = file.FindReplace("\\\\", "\\");
902
 
903
		FILE *id = fopen ( file.c_str(), "wb" );
904
		if ( !id )
905
			m_iError = CATERR_INVALIDDEST;
906
		else
907
		{
908
			fwrite ( data, sizeof(char), size, id );
909
			fclose ( id );
910
 
911
			ClearError ();
912
 
913
			delete data;
914
			f->sData = 0;
915
 
916
			return true;
917
		}
918
	}
919
 
920
	delete data;
921
	f->sData = 0;
922
 
923
	return false;
924
}
925
 
926
CyString CCatFile::GetErrorString ()
927
{
928
	switch ( m_iError )
929
	{
930
		case CATERR_NONE:
931
			return NullString;
932
		case CATERR_NODATFILE:
933
			return "Unable to open Dat file";
934
		case CATERR_NOCATFILE:
935
			return "Unable to open Cat file";
936
		case CATERR_FILEEMPTY:
937
			return "Cat file is empty";
938
		case CATERR_READCAT:
939
			return "Unable to read from cat file";
940
		case CATERR_DECRYPT:
941
			return "Unable to decrypt cat file";
942
		case CATERR_MISMATCH:
943
			return "File size mismatch with Dat file";
944
		case CATERR_NOFILE:
945
			return "Unable to find file in archive";
946
		case CATERR_CANTREAD:
947
			return "Unable to read file in archive";
948
		case CATERR_CANTCREATEDIR:
949
			return "Unable to create destiantion directory";
950
		case CATERR_INVALIDDEST:
951
			return "Unable to write to destiantion file";
952
	}
953
 
954
	return "Invalid";
955
}
956
 
957
 
958
unsigned char *CompressPCKData ( unsigned char *buffer, size_t size, size_t *retsize, time_t mtime )
959
{
960
	size_t newsize = (size * 2) + 20;
961
	unsigned char *data = (unsigned char *)malloc ( sizeof(unsigned char) * newsize );
962
 
963
	z_stream zs;
964
	char flags=0;
965
 
966
//	error(0);
967
 
968
	unsigned char *d = data;
969
//	if(m_pszComment && strlen(m_pszComment) > 0) flags|=GZ_F_COMMENT;
970
//	if(m_pszFileName && strlen(m_pszFileName) > 0) flags|=GZ_F_FILENAME;
971
 
972
	int pos = PCKHEADERSIZE;
973
	*d = 0x1F; 		d++;
974
	*d = 0x8B; 		d++;
975
	*d = 8;			d++;
976
	*d = flags; 	d++;
977
	memcpy(d, &mtime, sizeof(mtime));
978
	d += 4;
979
	*d = 0; 		d++;
980
	*d = 11; 		d++;
981
//	if(flags & GZ_F_FILENAME) put((const char *)m_pszFileName);
982
//	if(flags & GZ_F_COMMENT) put((const char *)m_pszComment);
983
 
984
	memset(&zs, 0, sizeof(zs));
985
	zs.next_in=buffer;
986
	zs.avail_in=(long)size;
987
 
988
	int ret;
989
	unsigned long ubound;
990
	ret=deflateInit2(&zs, 9, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
991
	if(ret!=Z_OK)
992
		return false;
993
 
994
	ubound=deflateBound(&zs, (unsigned long)size);
995
	if ( newsize < ubound)
996
	{
997
		newsize += ubound;
998
		data = (unsigned char *)realloc ( data, sizeof(unsigned char) * newsize );
999
	}
1000
 
1001
 
1002
	zs.next_out=d;
1003
	zs.avail_out=(unsigned int)newsize - pos;
1004
 
1005
	while((ret=deflate(&zs, Z_FINISH))==Z_OK)
1006
	{
1007
		newsize += 1024;
1008
		data = (unsigned char *)realloc ( data, sizeof(unsigned char) * newsize );
1009
		zs.next_out=data + zs.total_out;
1010
		zs.avail_out=(unsigned int)newsize - zs.total_out;
1011
	}
1012
	pos += zs.total_out;
1013
 
1014
	deflateEnd(&zs);
1015
 
1016
	unsigned long crc=crc32(0, NULL, 0);
1017
	crc=crc32(crc, buffer, (unsigned int)size);
1018
 
1019
	int s = sizeof(crc) + sizeof(size);
1020
	if ( newsize < (size_t)(s + pos) )
1021
	{
1022
		newsize += (s + pos) - newsize;
1023
		data = (unsigned char *)realloc ( data, sizeof(unsigned char) * newsize );
1024
	}
1025
 
1026
	memcpy(&data[pos], &crc, sizeof(crc));
1027
	pos += sizeof(crc);
1028
	memcpy(&data[pos], &size, sizeof(size));
1029
	pos += sizeof(size);
1030
 
1031
	newsize = pos;
1032
 
1033
	unsigned char *retdata = NULL;
1034
	if ( ret == Z_STREAM_END )
1035
	{
1036
		*retsize = newsize;
1037
		retdata = new unsigned char[newsize];
1038
		memcpy ( retdata, data, newsize );
1039
	}
1040
	free ( data );
1041
 
1042
	return retdata;
1043
}
1044
 
1045
unsigned char *PCKData ( unsigned char *data, size_t oldsize, size_t *newsize, bool bXor )
1046
{
1047
	unsigned char *newdata = CompressPCKData ( data, oldsize, newsize, time(NULL) );
1048
	if ( !bXor )
1049
		return newdata;
1050
 
1051
	if ( newdata )
1052
	{
1053
		char magic = (char)clock(), m;
1054
		m=magic ^ 0xC8;
1055
 
1056
		unsigned char *ptr = newdata, *end = newdata + *newsize;
1057
		// XOR encryption
1058
		if ( bXor )
1059
		{
1060
			for ( ; ptr < end; ptr++ )
1061
				(*ptr)^=magic;
1062
		}
1063
 
1064
		unsigned char *finalData = new unsigned char[*newsize + 1];
1065
		finalData[0] = m;
1066
		memcpy ( finalData + 1, newdata, *newsize );
1067
		delete [] newdata;
1068
		(*newsize)++;
1069
		return finalData;
1070
	}
1071
 
1072
	return NULL;
1073
}
1074
 
1075
bool CCatFile::WriteFromCat ( CCatFile *fcat, CyString file )
1076
{
1077
	// now find the file in the cat file
1078
	SInCatFile *getfile = fcat->FindData ( file );
1079
	if ( !getfile )
1080
		return false;
1081
 
1082
	// read the dat from the cat file
1083
	size_t size = 0;
1084
	unsigned char *data = fcat->ReadData ( getfile, &size );
1085
	if ( !data )
1086
		return false;
1087
 
1088
	// now check if it exists in this file
1089
	RemoveFile ( file );
1090
 
1091
	// now write to the new file
1092
	if ( !m_fDatFile.AppendData ( (const char *)data, size ) )
1093
		return false;
1094
 
1095
	// finally add to the list
1096
	SInCatFile *f = new SInCatFile;
1097
	f->sData = 0;
1098
	if ( m_lFiles.empty() )
1099
		f->lOffset = 0;
1100
	else
1101
		f->lOffset = m_lFiles.Back()->Data()->lOffset + m_lFiles.Back()->Data()->lSize;
1102
	f->sFile = file;
1103
	f->lSize = size;
1104
	m_lFiles.push_back ( f );
1105
	WriteCatFile ();
1106
 
1107
	return true;
1108
}
1109
 
1110
bool CCatFile::WriteFromCat ( CyString catfile, CyString file )
1111
{
1112
	CCatFile fcat;
1113
	if ( fcat.Open ( catfile, "", CATREAD_CATDECRYPT, false ) )
1114
		return false;
1115
 
1116
	return this->WriteFromCat(&fcat, file);
1117
}
1118
 
1119
SInCatFile *CCatFile::FindData ( CyString filename )
1120
{
1121
	if ( m_lFiles.empty() )
1122
		return NULL;
1123
 
1124
	CyString check = filename;
1125
	check = check.FindReplace ( "\\", "/" );
1126
	for ( SInCatFile *c = m_lFiles.First(); c; c = m_lFiles.Next() )
1127
	{
1128
		CyString f = c->sFile;
1129
		f = f.FindReplace ( "\\", "/" );
1130
		if ( f.Compare(check) )
1131
			return c;
1132
	}
1133
	return NULL;
1134
}
1135
 
1136
bool CCatFile::MarkRemoveFile( CyString file )
1137
{
1138
	SInCatFile *f = FindData ( file );
1139
	if ( !f )
1140
	{
1141
		m_iError = CATERR_NOFILE;
1142
		return false;
1143
	}
1144
 
1145
	return MarkRemoveFile ( f );
1146
}
1147
bool CCatFile::MarkRemoveFile ( SInCatFile *f )
1148
{
1149
	f->bDelete = true;
1150
	return true;
1151
}
1152
 
1153
 
1154
void CCatFile::WriteFiles()
1155
{
1156
	int count = 0;
1157
	CListNode<SInCatFile> *node;
1158
	for ( node = m_lFiles.Front(); node; node = node->next() )
1159
	{
1160
		if ( node->Data()->bDelete )
1161
			++count;
1162
	}
1163
 
1164
	if ( !count )
1165
		return;
1166
 
1167
	size_t *offset = new size_t[count * 2];
1168
	count = 0;
1169
 
1170
	node = m_lFiles.Front();
1171
	while ( node )
1172
	{
1173
		CListNode<SInCatFile> *nextNode = node->next();
1174
 
1175
		SInCatFile *f = node->Data();
1176
		if ( f->bDelete )
1177
		{
1178
			offset[count++] = f->lOffset;
1179
			offset[count++] = f->lSize;
1180
 
1181
			m_lFiles.remove(node);
1182
			delete f;
1183
		}
1184
 
1185
		node = nextNode;
1186
	}
1187
 
1188
	if ( m_fDatFile.WritePartFile(offset, count) )
1189
		WriteCatFile();
1190
}
1191
 
1192
CyStringList *CCatFile::GetTShipsEntries()
1193
{
1194
	if ( this->ExtractFile("types/tships.pck", "tships.txt") )
1195
	{
1196
		CFileIO TShips("tships.txt");
1197
		if ( TShips.Exists() )
1198
		{
1199
			CyStringList *lines = TShips.ReadLinesStr();
1200
			if ( lines )
1201
			{
1202
				// remove any commands, blank lines, and start
1203
				// and put the id as the data
1204
				int entries = -1;
1205
				for ( SStringList *str = lines->Head(); str; str = str->next )
1206
				{
1207
					str->str.RemoveFirstSpace();
1208
					str->str.RemoveChar(9);
1209
					str->str.RemoveChar('\r');
1210
					str->str.RemoveChar('\n');
1211
					if ( str->str.Empty() )
1212
					{
1213
						str->remove = true;
1214
						continue;
1215
					}
1216
 
1217
					if ( str->str[0] == '/' )
1218
					{
1219
						str->remove = true;
1220
						continue;
1221
					}
1222
 
1223
					if ( entries == -1 )
1224
					{
1225
						entries = str->str.GetToken(";", 2, 2).ToInt();
1226
						str->remove = true;
1227
						continue;
1228
					}
1229
 
1230
					// hopefully we now have a valid tships line
1231
					int num = -1;
1232
					while ( str->data.Empty() )
1233
						str->data = str->str.GetToken(";", num--, (num + 2));
1234
				}
1235
			}
1236
			// remove the tmp file
1237
			TShips.Remove();
1238
 
1239
			if ( lines )
1240
				lines->RemoveMarked();
1241
			return lines;
1242
		}
1243
	}
1244
 
1245
	return NULL;
1246
}
1247
 
1248
CyString CCatFile::GetTShipsEntry(CyString id)
1249
{
1250
	CyStringList *ships = this->GetTShipsEntries();
1251
	if ( ships )
1252
	{
1253
		SStringList *node = ships->FindData(id.upper());
1254
		if ( node )
1255
			return node->str;
1256
	}
1257
	return NullString;
1258
}
1259