Subversion Repositories spk

Rev

Go to most recent revision | Details | 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
 
211
			DecryptDAT(c->sData, c->lSize);
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
{
344
	if ( !c )
345
		return false;
346
	size_t size = 0;
347
	c->sData = ReadData ( c, &size );
348
 
349
	if ( c->sData )
350
		return true;
351
 
352
	return false;
353
}
354
 
355
unsigned char *CCatFile::ReadData ( SInCatFile *c, size_t *size )
356
{
357
	*size = c->lSize;
358
 
359
	FILE *id = fopen ( m_fDatFile.GetFullFilename().c_str(), "rb" );
360
	if ( id )
361
	{
362
		fseek ( id, (long)c->lOffset, SEEK_SET );
363
		unsigned char *data = new unsigned char[c->lSize + 1];
364
		fread ( data, sizeof(unsigned char), c->lSize, id );
365
		*size = c->lSize;
366
		data[c->lSize] = '\0';
367
 
368
		DecryptDAT(data, *size);
369
 
370
		fclose ( id );
371
 
372
		return data;
373
	}
374
 
375
	return NULL;
376
}
377
 
378
void CCatFile::DecryptDAT(unsigned char *buffer, size_t size)
379
{
380
	for(unsigned char *pos=buffer, *end=buffer + size; pos < end; pos++){
381
		*pos^=0x33;
382
	}
383
}
384
unsigned char *CCatFile::ReadData ( CyString filename, size_t *size )
385
{
386
	*size = 0;
387
 
388
	if ( !m_fDatFile.NoFile() )
389
	{
390
		SInCatFile *c = FindData ( filename );
391
		if ( c )
392
			return ReadData ( c, size );
393
	}
394
 
395
	return NULL;
396
}
397
 
398
unsigned char *CCatFile::UnpackFile ( SInCatFile *c, size_t *size, bool forcepck )
399
{
400
	*size = 0;
401
	if ( !c )
402
		return NULL;
403
 
404
	if ( IsDataPCK ( (const unsigned char *)c->sData, c->lSize ) || forcepck )
405
		return UnPCKData ( c->sData, c->lSize, size );
406
 
407
	*size = c->lSize;
408
	return c->sData;
409
}
410
 
411
bool CCatFile::RemoveFile ( SInCatFile *f )
412
{
413
	if ( (m_bCreate) || (m_lFiles.empty()) )
414
		return false;
415
 
416
	if ( !f )
417
		return false;
418
 
419
	int err = m_fDatFile.TruncateFile ( f->lOffset, f->lSize );
420
	if ( err == FILEERR_TOSMALL )
421
	{
422
		if ( (int)m_fDatFile.GetFilesize() > this->GetEndOffset() )
423
			m_fDatFile.TruncateFile( this->GetEndOffset(), m_fDatFile.GetFilesize() - this->GetEndOffset() );
424
	}
425
	else if ( err != FILEERR_NONE )
426
		return false;
427
 
16 cycrow 428
	// adjust the file positions
429
	bool bAdjust = false;
430
	int iOffset;
431
	for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() ) {
432
		int iNextOffset = node->Data()->lOffset;
433
		if ( bAdjust ) {
434
			node->Data()->lOffset = iOffset;
435
		}
436
		else if ( node->Data() == f ) {
437
			bAdjust = true;
438
		}
439
		else if ( !bAdjust ) continue;
440
		iOffset = iNextOffset;
441
	}
442
 
1 cycrow 443
	// now just write the new cat file
444
	m_lFiles.remove ( f );
445
	WriteCatFile ();
446
 
447
	return true;
448
}
449
 
450
bool CCatFile::WriteCatFile ()
451
{
452
	if ( (m_bCreate) && (m_lFiles.empty()) )
453
		return false;
454
 
455
	CyString cat = m_fCatFile.GetFilename() + "\n";
456
 
457
	for ( SInCatFile *f = m_lFiles.First(); f; f = m_lFiles.Next() )
458
	{
459
		if ( f->sFile.Empty() )
460
			continue;
461
		cat += f->sFile.findreplace("/", "\\") + " " + (long)f->lSize + "\n";
462
	}
463
 
464
	if ( !cat.Length() )
465
		return false;
466
 
467
	size_t len = cat.Length();
16 cycrow 468
//	if ( len % 5)
469
//		len += (5 - (len % 5));
1 cycrow 470
 
471
	unsigned char *data = new unsigned char[len + 1];
472
	memcpy ( data, cat.c_str(), cat.Length() );
473
 
474
	for ( size_t i = len; i > cat.Length(); i-- )
475
		data[i] = '\0';
476
 
477
	DecryptData ( data, len );
478
 
479
	m_fCatFile.WriteData ( (const char *)data, len );
480
 
481
	return true;
482
}
483
 
484
bool CCatFile::CheckExtensionPck ( CyString filename )
485
{
486
	CyString ext = filename.GetToken ( ".", filename.NumToken  ( "." ) ).lower();
487
 
488
	if ( ext == "xml" )
489
		return true;
490
	else if ( ext == "txt" )
491
		return true;
492
	else if ( ext == "bob" )
493
		return true;
494
	else if ( ext == "bod" )
495
		return true;
496
 
497
	return false;
498
}
499
 
500
bool CCatFile::CheckPackedExtension ( CyString filename )
501
{
502
	CyString ext = filename.GetToken ( ".", filename.NumToken  ( "." ) ).lower();
503
 
504
	if ( ext == "pck" )
505
		return true;
506
	else if ( ext == "pbb" )
507
		return true;
508
	else if ( ext == "pbd" )
509
		return true;
510
 
511
	return false;
512
}
513
 
514
CyString CCatFile::PckChangeExtension ( CyString f )
515
{
516
	CFileIO fo ( f );
517
	CyString ext = fo.GetFileExtension ().lower();
518
	if ( ext == "txt" )
519
		return fo.ChangeFileExtension ( "pck" );
520
	else if ( ext == "xml" )
521
		return fo.ChangeFileExtension ( "pck" );
522
	else if ( ext == "bob" )
523
		return fo.ChangeFileExtension ( "pbb" );
524
	else if ( ext == "bod" )
525
		return fo.ChangeFileExtension ( "pbd" );
526
 
527
	return f;
528
}
529
 
530
bool CCatFile::AppendFile ( CyString filename, CyString to, bool pck, bool bXor )
531
{
532
	if ( (!m_bCreate) && (!m_fCatFile.Exists ()) )
533
		return false;
534
 
535
	if ( !m_sAddonDir.Empty() && CCatFile::IsAddonDir(to) ) {
536
		to = m_sAddonDir + "\\" + to;
537
	}
538
 
539
	if ( filename.IsIn ( "::" ) )
540
		return WriteFromCat ( filename.GetToken ( "::", 1, 1 ), filename.GetToken ( "::", 2, 2 ) );
541
 
542
	// first open the file to check it exists
543
	FILE *id = fopen ( filename.c_str(), "rb" );
544
	if ( !id )
545
		return false;
546
	fclose ( id );
547
 
548
	// then check if the file already exists
549
	if ( !m_lFiles.empty() )
550
	{
551
		SInCatFile *f = FindData ( to );
552
		if ( f )
553
		{
554
			if ( !RemoveFile ( f ) )
555
				return false;
556
		}
557
	}
558
 
559
	bool append = false;
560
 
561
	SInCatFile *f = new SInCatFile;
562
	f->sData = 0;
563
	f->lOffset = this->GetEndOffset();
564
 
565
	bool dofile = true;
566
	if ( pck && CheckExtensionPck(filename) )
567
	{
568
		to = PckChangeExtension ( to );
569
 
570
		if ( !m_lFiles.empty() )
571
		{
572
			SInCatFile *checkf = FindData ( to );
573
			if ( checkf )
574
			{
575
				if ( !RemoveFile ( checkf ) )
576
					return false;
577
			}
578
		}
579
	}
580
 
581
	if ( !m_lFiles.size() )
582
		m_fDatFile.WipeFile();
583
 
584
//	if ( CheckPackedExtension(to) && !CheckPackedExtension(filename) )
585
	{
586
		FILE *id = fopen ( filename.c_str(), "rb" );
587
		if ( !id )
588
			return false;
589
		fseek ( id, 0, SEEK_END );
590
		size_t size = ftell ( id );
591
		fseek ( id, 0, SEEK_SET );
592
		unsigned char *data = new unsigned char[size + 1];
593
		fread ( data, sizeof(unsigned char), size, id );
594
		data[size] = '\0';
595
		fclose ( id );
596
 
597
		if ( CheckPackedExtension(to) && !IsDataPCK ( data, size ) )
598
		{
599
			size_t newsize = 0;
600
			unsigned char *newdata = PCKData ( data, size, &newsize, bXor );
601
 
602
			this->DecryptDAT(newdata, newsize);
603
			f->lSize = newsize;
604
			append = m_fDatFile.AppendDataToPos ( (const char *)newdata, newsize, f->lOffset );
605
 
606
			delete [] newdata;
607
		}
608
		else 
609
		{
610
			this->DecryptDAT(data, size);
611
			f->lSize = size;
612
			append = m_fDatFile.AppendDataToPos ( (const char *)data, size, f->lOffset );
613
		}
614
 
615
		delete [] data;
616
	}
617
 
618
	if ( append )
619
	{
620
		m_bCreate = false;
621
		f->sFile = to;
622
		m_lFiles.push_back ( f );
623
		WriteCatFile ();
624
	}
625
	else
626
		delete f;
627
 
628
	return true;
629
}
630
 
631
bool CCatFile::AddData ( CyString catfile, unsigned char *data, size_t size, CyString to, bool pck, bool create )
632
{
633
	int err = Open ( catfile, "", CATREAD_CATDECRYPT, create );
634
	if ( (err != CATERR_NONE) && (err != CATERR_CREATED) )
635
		return false;
636
 
637
	return AppendData ( data, size, to, pck );
638
}
639
 
640
int CCatFile::GetEndOffset()
641
{
642
	if ( m_lFiles.empty() )
643
		return 0;
644
	else
645
		return m_lFiles.Back()->Data()->lOffset + m_lFiles.Back()->Data()->lSize;
646
}
647
 
648
 
649
bool CCatFile::AppendData ( unsigned char *data, size_t size, CyString to, bool pck, bool bXor )
650
{
651
	if ( (!m_bCreate) && (!m_fCatFile.Exists ()) )
652
		return false;
653
 
654
	if ( (size <= 0) || (!data) )
655
		return false;
656
 
657
	// then check if the file already exists
658
	if ( !m_lFiles.empty() )
659
	{
660
		SInCatFile *f = FindData ( to );
661
		if ( f )
662
		{
663
			if ( !RemoveFile ( f ) )
664
				return false;
665
		}
666
	}
667
 
668
	bool append = false;
669
 
670
	if ( !m_lFiles.size() )
671
		m_fDatFile.WipeFile();
672
 
673
	SInCatFile *f = new SInCatFile;
674
	f->sData = 0;
675
	if ( m_lFiles.empty() )
676
		f->lOffset = 0;
677
	else
678
		f->lOffset = m_fDatFile.GetFilesize();
679
 
680
	// if file extension is packed but not the file, then pack it, "pck" forces it to be packed unless it already is
681
 
682
	f->lSize = size;
683
	if ( (((pck) && (CheckExtensionPck (to))) || ((CheckPackedExtension ( to )))) && (!IsDataPCK ( data, size )) )
684
	{
685
		to = PckChangeExtension ( to );
686
 
687
		if ( !m_lFiles.empty() )
688
		{
689
			SInCatFile *f = FindData ( to );
690
			if ( f )
691
			{
692
				if ( !RemoveFile ( f ) )
693
					return false;
694
			}
695
		}
696
		size_t newsize = 0;
697
		unsigned char *d = PCKData ( data, size, &newsize, bXor );
698
 
699
		f->lSize = newsize;
700
		this->DecryptDAT(d, newsize);
701
		append = m_fDatFile.AppendDataToPos ( (const char *)d, newsize, f->lOffset );
702
 
703
		delete [] d;
704
	}
705
	else {
706
		this->DecryptDAT(data, size);
707
		append = m_fDatFile.AppendDataToPos ( (const char *)data, size, f->lOffset );
708
	}
709
 
710
	if ( append )
711
	{
712
		m_bCreate = false;
713
		f->sFile = to;
714
		m_lFiles.push_back ( f );
715
		WriteCatFile ();
716
	}
717
	else
718
		delete f;
719
 
720
	return true;
721
}
722
 
723
CyString CCatFile::RenameFileExtension(SInCatFile *f)
724
{
725
	CFileIO fo ( f->sFile );
726
	CyString ext = fo.GetFileExtension ().lower();
727
	if ( ext == "pck" )
728
	{
729
		CyString firstDir = f->sFile.FindReplace("/", "\\").GetToken("\\", 1, 1).lower();
730
 
731
		if ( firstDir == "t" )
732
			return fo.ChangeFileExtension ( "xml" );
733
		else if ( firstDir == "director" )
734
			return fo.ChangeFileExtension ( "xml" );
735
		return fo.ChangeFileExtension ( "txt" );
736
	}
737
	else if ( ext == "pbb" )
738
		return fo.ChangeFileExtension ( "bob" );
739
	else if ( ext == "pbd" )
740
		return fo.ChangeFileExtension ( "bod" );
741
	return f->sFile;
742
}
743
 
744
void CCatFile::FindFiles(CyStringList *files, CyString filemask)
745
{
746
	if ( m_lFiles.empty() )
747
		return;
748
 
749
	for ( SInCatFile *f = m_lFiles.First(); f; f = m_lFiles.Next() )
750
	{
751
		if ( filemask.WildMatch(f->sFile) )
752
			files->PushBack(f->sFile);
753
	}
754
}
755
 
756
bool CCatFile::ExtractAll(CyString dir)
757
{
758
	if ( m_lFiles.empty() )
759
		return false;
760
 
761
	for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() )
762
	{
763
		if ( !ExtractFile(node->Data(), dir, true) )
764
			return false;
765
	}
766
 
767
	return true;
768
}
769
bool CCatFile::ExtractFile ( CyString filename, CyString to, bool preserve )
770
{
771
	if ( m_lFiles.empty() )
772
		return false;
773
 
774
	// check if file exists
775
	if ( !m_sAddonDir.Empty() ) {
776
		SInCatFile *f = FindData ( m_sAddonDir + "\\" + filename );
777
		if ( f )
778
			return ExtractFile ( f, to, preserve );
779
	}
780
	{
781
		SInCatFile *f = FindData ( CyString("addon\\") + filename );
782
		if ( f )
783
			return ExtractFile ( f, to, preserve );
784
	}
785
 
786
	SInCatFile *f = FindData ( filename );
787
	if ( !f )
788
	{
789
		m_iError = CATERR_NOFILE;
790
		return false;
791
	}
792
 
793
	return ExtractFile ( f, to, preserve );
794
}
795
 
796
bool CCatFile::ExtractFile ( SInCatFile *f, CyString to, bool preserve )
797
{
798
	unsigned char *data = 0;
799
	size_t size = 0;
800
 
801
	// load the data from file
802
	ReadFileToData ( f );
803
 
804
	data = UnpackFile ( f, &size, CheckPackedExtension(f->sFile) );
805
	if ( !data )
806
	{
807
		m_iError = CATERR_CANTREAD;
808
		return false;
809
	}
810
 
811
	CFileIO fo(CCatFile::RenameFileExtension(f));
812
	// check for a file name
813
	CyString checkFile = to;
814
	CyString todir, tofile = to;
815
	if ( checkFile.IsAnyIn("\\/") )
816
	{
817
		checkFile = checkFile.FindReplace ( "\\", "/" );
818
		checkFile = checkFile.FindReplace ( "//", "/" );
819
		tofile = checkFile.GetToken("/", checkFile.NumToken('/'));
820
 
821
		if ( !checkFile.IsIn(".") || preserve )
822
		{
823
			tofile = fo.GetFilename();
824
			todir = checkFile;
825
		}
826
		else
827
		{
828
			todir = CFileIO(checkFile).GetDir();
829
			tofile = CFileIO(checkFile).GetFilename();
830
		}
831
	}
832
 
833
	if ( tofile.Empty() )
834
	{
835
		if ( !tofile.Empty() )
836
			tofile += "/";
837
		tofile += fo.GetFilename();
838
		todir = CFileIO(tofile).GetDir();
839
		tofile = CFileIO(tofile).GetFilename();
840
	}
841
 
842
	if ( preserve )
843
	{
844
		if ( !fo.GetDir().Compare(todir.Right(- (int)fo.GetDir().Length())) )
845
		{
846
			if ( !todir.Empty() && todir.Right(1) != "/" )
847
				todir += "/";
848
			todir += fo.GetDir();
849
		}
850
	}
851
 
852
	if ( tofile.Empty() )
853
		tofile = fo.GetFilename();
854
 
855
	// create the directory to extract to
856
	if ( !todir.Empty() && !CDirIO(todir).Create() )
857
		m_iError = CATERR_CANTCREATEDIR;
858
	else
859
	{
860
		CyString file = todir;
861
		if ( !file.Empty() )
862
			file += "/";
863
		file += tofile;
864
		file = file.FindReplace("/", "\\");
865
		file = file.FindReplace("\\\\", "\\");
866
 
867
		FILE *id = fopen ( file.c_str(), "wb" );
868
		if ( !id )
869
			m_iError = CATERR_INVALIDDEST;
870
		else
871
		{
872
			fwrite ( data, sizeof(char), size, id );
873
			fclose ( id );
874
 
875
			ClearError ();
876
 
877
			delete data;
878
			f->sData = 0;
879
 
880
			return true;
881
		}
882
	}
883
 
884
	delete data;
885
	f->sData = 0;
886
 
887
	return false;
888
}
889
 
890
CyString CCatFile::GetErrorString ()
891
{
892
	switch ( m_iError )
893
	{
894
		case CATERR_NONE:
895
			return NullString;
896
		case CATERR_NODATFILE:
897
			return "Unable to open Dat file";
898
		case CATERR_NOCATFILE:
899
			return "Unable to open Cat file";
900
		case CATERR_FILEEMPTY:
901
			return "Cat file is empty";
902
		case CATERR_READCAT:
903
			return "Unable to read from cat file";
904
		case CATERR_DECRYPT:
905
			return "Unable to decrypt cat file";
906
		case CATERR_MISMATCH:
907
			return "File size mismatch with Dat file";
908
		case CATERR_NOFILE:
909
			return "Unable to find file in archive";
910
		case CATERR_CANTREAD:
911
			return "Unable to read file in archive";
912
		case CATERR_CANTCREATEDIR:
913
			return "Unable to create destiantion directory";
914
		case CATERR_INVALIDDEST:
915
			return "Unable to write to destiantion file";
916
	}
917
 
918
	return "Invalid";
919
}
920
 
921
 
922
unsigned char *CompressPCKData ( unsigned char *buffer, size_t size, size_t *retsize, time_t mtime )
923
{
924
	size_t newsize = (size * 2) + 20;
925
	unsigned char *data = (unsigned char *)malloc ( sizeof(unsigned char) * newsize );
926
 
927
	z_stream zs;
928
	char flags=0;
929
 
930
//	error(0);
931
 
932
	unsigned char *d = data;
933
//	if(m_pszComment && strlen(m_pszComment) > 0) flags|=GZ_F_COMMENT;
934
//	if(m_pszFileName && strlen(m_pszFileName) > 0) flags|=GZ_F_FILENAME;
935
 
936
	int pos = PCKHEADERSIZE;
937
	*d = 0x1F; 		d++;
938
	*d = 0x8B; 		d++;
939
	*d = 8;			d++;
940
	*d = flags; 	d++;
941
	memcpy(d, &mtime, sizeof(mtime));
942
	d += 4;
943
	*d = 0; 		d++;
944
	*d = 11; 		d++;
945
//	if(flags & GZ_F_FILENAME) put((const char *)m_pszFileName);
946
//	if(flags & GZ_F_COMMENT) put((const char *)m_pszComment);
947
 
948
	memset(&zs, 0, sizeof(zs));
949
	zs.next_in=buffer;
950
	zs.avail_in=(long)size;
951
 
952
	int ret;
953
	unsigned long ubound;
954
	ret=deflateInit2(&zs, 9, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
955
	if(ret!=Z_OK)
956
		return false;
957
 
958
	ubound=deflateBound(&zs, (unsigned long)size);
959
	if ( newsize < ubound)
960
	{
961
		newsize += ubound;
962
		data = (unsigned char *)realloc ( data, sizeof(unsigned char) * newsize );
963
	}
964
 
965
 
966
	zs.next_out=d;
967
	zs.avail_out=(unsigned int)newsize - pos;
968
 
969
	while((ret=deflate(&zs, Z_FINISH))==Z_OK)
970
	{
971
		newsize += 1024;
972
		data = (unsigned char *)realloc ( data, sizeof(unsigned char) * newsize );
973
		zs.next_out=data + zs.total_out;
974
		zs.avail_out=(unsigned int)newsize - zs.total_out;
975
	}
976
	pos += zs.total_out;
977
 
978
	deflateEnd(&zs);
979
 
980
	unsigned long crc=crc32(0, NULL, 0);
981
	crc=crc32(crc, buffer, (unsigned int)size);
982
 
983
	int s = sizeof(crc) + sizeof(size);
984
	if ( newsize < (size_t)(s + pos) )
985
	{
986
		newsize += (s + pos) - newsize;
987
		data = (unsigned char *)realloc ( data, sizeof(unsigned char) * newsize );
988
	}
989
 
990
	memcpy(&data[pos], &crc, sizeof(crc));
991
	pos += sizeof(crc);
992
	memcpy(&data[pos], &size, sizeof(size));
993
	pos += sizeof(size);
994
 
995
	newsize = pos;
996
 
997
	unsigned char *retdata = NULL;
998
	if ( ret == Z_STREAM_END )
999
	{
1000
		*retsize = newsize;
1001
		retdata = new unsigned char[newsize];
1002
		memcpy ( retdata, data, newsize );
1003
	}
1004
	free ( data );
1005
 
1006
	return retdata;
1007
}
1008
 
1009
unsigned char *PCKData ( unsigned char *data, size_t oldsize, size_t *newsize, bool bXor )
1010
{
1011
	unsigned char *newdata = CompressPCKData ( data, oldsize, newsize, time(NULL) );
1012
	if ( !bXor )
1013
		return newdata;
1014
 
1015
	if ( newdata )
1016
	{
1017
		char magic = (char)clock(), m;
1018
		m=magic ^ 0xC8;
1019
 
1020
		unsigned char *ptr = newdata, *end = newdata + *newsize;
1021
		// XOR encryption
1022
		if ( bXor )
1023
		{
1024
			for ( ; ptr < end; ptr++ )
1025
				(*ptr)^=magic;
1026
		}
1027
 
1028
		unsigned char *finalData = new unsigned char[*newsize + 1];
1029
		finalData[0] = m;
1030
		memcpy ( finalData + 1, newdata, *newsize );
1031
		delete [] newdata;
1032
		(*newsize)++;
1033
		return finalData;
1034
	}
1035
 
1036
	return NULL;
1037
}
1038
 
1039
bool CCatFile::WriteFromCat ( CCatFile *fcat, CyString file )
1040
{
1041
	// now find the file in the cat file
1042
	SInCatFile *getfile = fcat->FindData ( file );
1043
	if ( !getfile )
1044
		return false;
1045
 
1046
	// read the dat from the cat file
1047
	size_t size = 0;
1048
	unsigned char *data = fcat->ReadData ( getfile, &size );
1049
	if ( !data )
1050
		return false;
1051
 
1052
	// now check if it exists in this file
1053
	RemoveFile ( file );
1054
 
1055
	// now write to the new file
1056
	if ( !m_fDatFile.AppendData ( (const char *)data, size ) )
1057
		return false;
1058
 
1059
	// finally add to the list
1060
	SInCatFile *f = new SInCatFile;
1061
	f->sData = 0;
1062
	if ( m_lFiles.empty() )
1063
		f->lOffset = 0;
1064
	else
1065
		f->lOffset = m_lFiles.Back()->Data()->lOffset + m_lFiles.Back()->Data()->lSize;
1066
	f->sFile = file;
1067
	f->lSize = size;
1068
	m_lFiles.push_back ( f );
1069
	WriteCatFile ();
1070
 
1071
	return true;
1072
}
1073
 
1074
bool CCatFile::WriteFromCat ( CyString catfile, CyString file )
1075
{
1076
	CCatFile fcat;
1077
	if ( fcat.Open ( catfile, "", CATREAD_CATDECRYPT, false ) )
1078
		return false;
1079
 
1080
	return this->WriteFromCat(&fcat, file);
1081
}
1082
 
1083
SInCatFile *CCatFile::FindData ( CyString filename )
1084
{
1085
	if ( m_lFiles.empty() )
1086
		return NULL;
1087
 
1088
	CyString check = filename;
1089
	check = check.FindReplace ( "\\", "/" );
1090
	for ( SInCatFile *c = m_lFiles.First(); c; c = m_lFiles.Next() )
1091
	{
1092
		CyString f = c->sFile;
1093
		f = f.FindReplace ( "\\", "/" );
1094
		if ( f.Compare(check) )
1095
			return c;
1096
	}
1097
	return NULL;
1098
}
1099
 
1100
bool CCatFile::MarkRemoveFile( CyString file )
1101
{
1102
	SInCatFile *f = FindData ( file );
1103
	if ( !f )
1104
	{
1105
		m_iError = CATERR_NOFILE;
1106
		return false;
1107
	}
1108
 
1109
	return MarkRemoveFile ( f );
1110
}
1111
bool CCatFile::MarkRemoveFile ( SInCatFile *f )
1112
{
1113
	f->bDelete = true;
1114
	return true;
1115
}
1116
 
1117
 
1118
void CCatFile::WriteFiles()
1119
{
1120
	int count = 0;
1121
	CListNode<SInCatFile> *node;
1122
	for ( node = m_lFiles.Front(); node; node = node->next() )
1123
	{
1124
		if ( node->Data()->bDelete )
1125
			++count;
1126
	}
1127
 
1128
	if ( !count )
1129
		return;
1130
 
1131
	size_t *offset = new size_t[count * 2];
1132
	count = 0;
1133
 
1134
	node = m_lFiles.Front();
1135
	while ( node )
1136
	{
1137
		CListNode<SInCatFile> *nextNode = node->next();
1138
 
1139
		SInCatFile *f = node->Data();
1140
		if ( f->bDelete )
1141
		{
1142
			offset[count++] = f->lOffset;
1143
			offset[count++] = f->lSize;
1144
 
1145
			m_lFiles.remove(node);
1146
			delete f;
1147
		}
1148
 
1149
		node = nextNode;
1150
	}
1151
 
1152
	if ( m_fDatFile.WritePartFile(offset, count) )
1153
		WriteCatFile();
1154
}
1155
 
1156
CyStringList *CCatFile::GetTShipsEntries()
1157
{
1158
	if ( this->ExtractFile("types/tships.pck", "tships.txt") )
1159
	{
1160
		CFileIO TShips("tships.txt");
1161
		if ( TShips.Exists() )
1162
		{
1163
			CyStringList *lines = TShips.ReadLinesStr();
1164
			if ( lines )
1165
			{
1166
				// remove any commands, blank lines, and start
1167
				// and put the id as the data
1168
				int entries = -1;
1169
				for ( SStringList *str = lines->Head(); str; str = str->next )
1170
				{
1171
					str->str.RemoveFirstSpace();
1172
					str->str.RemoveChar(9);
1173
					str->str.RemoveChar('\r');
1174
					str->str.RemoveChar('\n');
1175
					if ( str->str.Empty() )
1176
					{
1177
						str->remove = true;
1178
						continue;
1179
					}
1180
 
1181
					if ( str->str[0] == '/' )
1182
					{
1183
						str->remove = true;
1184
						continue;
1185
					}
1186
 
1187
					if ( entries == -1 )
1188
					{
1189
						entries = str->str.GetToken(";", 2, 2).ToInt();
1190
						str->remove = true;
1191
						continue;
1192
					}
1193
 
1194
					// hopefully we now have a valid tships line
1195
					int num = -1;
1196
					while ( str->data.Empty() )
1197
						str->data = str->str.GetToken(";", num--, (num + 2));
1198
				}
1199
			}
1200
			// remove the tmp file
1201
			TShips.Remove();
1202
 
1203
			if ( lines )
1204
				lines->RemoveMarked();
1205
			return lines;
1206
		}
1207
	}
1208
 
1209
	return NULL;
1210
}
1211
 
1212
CyString CCatFile::GetTShipsEntry(CyString id)
1213
{
1214
	CyStringList *ships = this->GetTShipsEntries();
1215
	if ( ships )
1216
	{
1217
		SStringList *node = ships->FindData(id.upper());
1218
		if ( node )
1219
			return node->str;
1220
	}
1221
	return NullString;
1222
}
1223