Subversion Repositories spk

Rev

Rev 40 | Rev 42 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 40 Rev 41
Line 206... Line 206...
206
			c->sData = new unsigned char[c->lSize + 1];
206
			c->sData = new unsigned char[c->lSize + 1];
207
			size_t readAmount = fread ( c->sData, sizeof(unsigned char), c->lSize, id );
207
			size_t readAmount = fread ( c->sData, sizeof(unsigned char), c->lSize, id );
208
			if ( readAmount != c->lSize )
208
			if ( readAmount != c->lSize )
209
                break;
209
                break;
210
 
210
 
211
			DecryptDAT(c->sData, c->lSize);
211
			DecryptDAT(c);
212
		}
212
		}
213
		fclose ( id );
213
		fclose ( id );
214
	}
214
	}
215
}
215
}
216
 
216
 
Line 339... Line 339...
339
	return ReadFileToData ( c );
339
	return ReadFileToData ( c );
340
}
340
}
341
 
341
 
342
bool CCatFile::ReadFileToData ( SInCatFile *c )
342
bool CCatFile::ReadFileToData ( SInCatFile *c )
343
{
343
{
344
	if ( !c )
-
 
345
		return false;
344
	if ( !c ) return false;
346
	size_t size = 0;
345
	size_t size = 0;
347
	c->sData = ReadData ( c, &size );
346
	if ( !c->sData ) c->sData = ReadData ( c, &size );
348
 
-
 
349
	if ( c->sData )
347
	if ( c->sData )	return true;
350
		return true;
-
 
351
 
-
 
352
	return false;
348
	return false;
353
}
349
}
354
 
350
 
355
unsigned char *CCatFile::ReadData ( SInCatFile *c, size_t *size )
351
unsigned char *CCatFile::ReadData ( SInCatFile *c, size_t *size )
356
{
352
{
Line 363... Line 359...
363
		unsigned char *data = new unsigned char[c->lSize + 1];
359
		unsigned char *data = new unsigned char[c->lSize + 1];
364
		fread ( data, sizeof(unsigned char), c->lSize, id );
360
		fread ( data, sizeof(unsigned char), c->lSize, id );
365
		*size = c->lSize;
361
		*size = c->lSize;
366
		data[c->lSize] = '\0';
362
		data[c->lSize] = '\0';
367
 
363
 
368
		DecryptDAT(data, *size);
364
		DecryptDAT(data, c->lSize);
-
 
365
		c->bDecrypted = true;
369
 
366
 
370
		fclose ( id );
367
		fclose ( id );
371
 
368
 
372
		return data;
369
		return data;
373
	}
370
	}
374
 
371
 
375
	return NULL;
372
	return NULL;
376
}
373
}
377
 
374
 
-
 
375
void CCatFile::DecryptDAT(SInCatFile *pFile) 
-
 
376
{
-
 
377
	if ( pFile->bDecrypted ) return;
-
 
378
	pFile->bDecrypted = true;
-
 
379
	this->DecryptDAT(pFile->sData, pFile->lSize);
-
 
380
}
378
void CCatFile::DecryptDAT(unsigned char *buffer, size_t size)
381
void CCatFile::DecryptDAT(unsigned char *buffer, size_t size)
379
{
382
{
380
	for(unsigned char *pos=buffer, *end=buffer + size; pos < end; pos++){
383
	for(unsigned char *pos=buffer, *end=buffer + size; pos < end; pos++){
381
		*pos^=0x33;
384
		*pos^=0x33;
382
	}
385
	}
Line 393... Line 396...
393
	}
396
	}
394
 
397
 
395
	return NULL;
398
	return NULL;
396
}
399
}
397
 
400
 
398
unsigned char *CCatFile::UnpackFile ( SInCatFile *c, size_t *size, bool forcepck )
401
unsigned char *CCatFile::UnpackFile ( SInCatFile *c, size_t *size)
399
{
402
{
400
	*size = 0;
403
	*size = 0;
401
	if ( !c )
404
	if ( !c )
402
		return NULL;
405
		return NULL;
403
 
406
 
404
	if ( IsDataPCK ( (const unsigned char *)c->sData, c->lSize ) || forcepck )
-
 
405
		return UnPCKData ( c->sData, c->lSize, size );
407
	this->DecryptDAT(c);
406
 
408
 
-
 
409
	int iFiletype = this->_checkFiletype(c->sData, 3);
-
 
410
 
-
 
411
	// plain file
-
 
412
	if ( iFiletype == FILETYPE_PLAIN ) {
407
	*size = c->lSize;
413
		*size = c->lSize;
408
	return c->sData;
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);
409
}
431
}
410
 
432
 
411
bool CCatFile::RemoveFile ( SInCatFile *f )
433
bool CCatFile::RemoveFile ( SInCatFile *f )
412
{
434
{
413
	if ( (m_bCreate) || (m_lFiles.empty()) )
435
	if ( (m_bCreate) || (m_lFiles.empty()) )
Line 589... Line 611...
589
		unsigned char *data = new unsigned char[size + 1];
611
		unsigned char *data = new unsigned char[size + 1];
590
		fread ( data, sizeof(unsigned char), size, id );
612
		fread ( data, sizeof(unsigned char), size, id );
591
		data[size] = '\0';
613
		data[size] = '\0';
592
		fclose ( id );
614
		fclose ( id );
593
 
615
 
-
 
616
		int iFileType = this->_checkFiletype(data, 3);
594
		if ( CheckPackedExtension(to) && !IsDataPCK ( data, size ) )
617
		if ( CheckPackedExtension(to) && iFileType == FILETYPE_PLAIN )
595
		{
618
		{
596
			size_t newsize = 0;
619
			size_t newsize = 0;
597
			unsigned char *newdata = PCKData ( data, size, &newsize, bXor );
620
			unsigned char *newdata = PCKData ( data, size, &newsize, bXor );
598
 
621
 
599
			this->DecryptDAT(newdata, newsize);
-
 
600
			f->lSize = newsize;
622
			f->lSize = newsize;
-
 
623
			this->DecryptDAT(newdata, f->lSize);
601
			append = m_fDatFile.AppendDataToPos ( (const char *)newdata, newsize, f->lOffset );
624
			append = m_fDatFile.AppendDataToPos ( (const char *)newdata, newsize, f->lOffset );
602
 
625
 
603
			delete [] newdata;
626
			delete [] newdata;
604
		}
627
		}
605
		else 
628
		else 
606
		{
629
		{
607
			this->DecryptDAT(data, size);
630
			this->DecryptDAT(data, size);
608
			f->lSize = size;
631
			f->lSize = size;
609
			append = m_fDatFile.AppendDataToPos ( (const char *)data, size, f->lOffset );
632
			append = m_fDatFile.AppendDataToPos ( (const char *)data, f->lSize, f->lOffset );
610
		}
633
		}
611
 
634
 
612
		delete [] data;
635
		delete [] data;
613
	}
636
	}
614
 
637
 
Line 788... Line 811...
788
	}
811
	}
789
 
812
 
790
	return ExtractFile ( f, to, preserve );
813
	return ExtractFile ( f, to, preserve );
791
}
814
}
792
 
815
 
-
 
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
 
793
bool CCatFile::ExtractFile ( SInCatFile *f, CyString to, bool preserve )
832
bool CCatFile::ExtractFile ( SInCatFile *f, CyString to, bool preserve )
794
{
833
{
795
	unsigned char *data = 0;
834
	unsigned char *data = 0;
796
	size_t size = 0;
835
	size_t size = 0;
797
 
836
 
798
	// load the data from file
837
	// load the data from file
799
	ReadFileToData ( f );
838
	ReadFileToData ( f );
800
 
839
 
801
	data = UnpackFile ( f, &size, CheckPackedExtension(f->sFile) );
840
	data = this->UnpackFile(f, &size);
802
	if ( !data )
841
	if ( !data )
803
	{
842
	{
804
		m_iError = CATERR_CANTREAD;
843
		m_iError = CATERR_CANTREAD;
805
		return false;
844
		return false;
806
	}
845
	}