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