Subversion Repositories spk

Rev

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

Rev 213 Rev 227
Line 123... Line 123...
123
	try {
123
	try {
124
		if ( m_sData ) delete [] m_sData;
124
		if ( m_sData ) delete [] m_sData;
125
		m_sData = new unsigned char[size + 1];
125
		m_sData = new unsigned char[size + 1];
126
	}
126
	}
127
	catch (std::exception &e) {
127
	catch (std::exception &e) {
128
		CLog::logf(CLog::Log_IO, 2, "Memory Exception error, unable to alloc enough memory to store file data, %d (%s)", size + 1, e.what());
128
		CLog::logf(CLog::Log_IO, 2, L"Memory Exception error, unable to alloc enough memory to store file data, %d (%hs)", size + 1, e.what());
129
		return CATERR_MALLOC;
129
		return CATERR_MALLOC;
130
	}
130
	}
131
 
131
 
132
	m_lSize = size;
132
	m_lSize = size;
133
	try {
133
	try {
134
		File.read(m_sData, m_lSize);
134
		File.read(m_sData, m_lSize);
135
	} catch (std::exception &e) {
135
	} catch (std::exception &e) {
136
		CLog::logf(CLog::Log_IO, 3, "CCatFile::Open() unable to read from cat file, %s", e.what());
136
		CLog::logf(CLog::Log_IO, 3, L"CCatFile::Open() unable to read from cat file, %hs", e.what());
137
		RemoveData ();
137
		RemoveData ();
138
		File.close();
138
		File.close();
139
		return CATERR_READCAT;
139
		return CATERR_READCAT;
140
	}
140
	}
141
 
141
 
Line 213... Line 213...
213
 
213
 
214
			try {
214
			try {
215
				c->sData = new unsigned char[c->lSize + 1];
215
				c->sData = new unsigned char[c->lSize + 1];
216
			}
216
			}
217
			catch (std::exception &e) {
217
			catch (std::exception &e) {
218
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to malloc data storage: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
218
				CLog::logf(CLog::Log_IO, 2, L"CCatFile::LoadDatFile() unable to malloc data storage: %s, %d (%hs)", c->sFile.c_str(), c->lSize, e.what());
219
				continue;
219
				continue;
220
			}
220
			}
221
 
221
 
222
			try {
222
			try {
223
				m_fDatFile.read(c->sData, c->lSize);
223
				m_fDatFile.read(c->sData, c->lSize);
224
			}
224
			}
225
			catch(std::exception &e) {
225
			catch(std::exception &e) {
226
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to read file data: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
226
				CLog::logf(CLog::Log_IO, 2, L"CCatFile::LoadDatFile() unable to read file data: %s, %d (%hs)", c->sFile.c_str(), c->lSize, e.what());
227
				continue;
227
				continue;
228
			}
228
			}
229
			c->bDecrypted = false;
229
			c->bDecrypted = false;
230
			this->DecryptDAT(c);
230
			this->DecryptDAT(c);
231
		}
231
		}
Line 519... Line 519...
519
 
519
 
520
		bool ret = m_fCatFile.write(data, cat.length());
520
		bool ret = m_fCatFile.write(data, cat.length());
521
		delete []data;
521
		delete []data;
522
	}
522
	}
523
	catch(std::exception &e) {
523
	catch(std::exception &e) {
524
		CLog::logf(CLog::Log_IO, 2, "CCatFile::WriteCatFile() unable to malloc, %d (%s)", len + 1, e.what());
524
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::WriteCatFile() unable to malloc, %d (%hs)", len + 1, e.what());
525
		return false;
525
		return false;
526
	}
526
	}
527
 
527
 
528
	m_fCatFile.close();
528
	m_fCatFile.close();
529
	m_bCatChanged = false;
529
	m_bCatChanged = false;
Line 580... Line 580...
580
	return f;
580
	return f;
581
}
581
}
582
 
582
 
583
bool CCatFile::appendFile(const Utils::WString &filename, const Utils::WString &sTo, bool pck, bool bXor, Utils::WString *sChangeTo)
583
bool CCatFile::appendFile(const Utils::WString &filename, const Utils::WString &sTo, bool pck, bool bXor, Utils::WString *sChangeTo)
584
{
584
{
585
	CLog::logf(CLog::Log_IO, 1, "CCatFile::AppendFile() Adding file, %s, into cat file, %s::%s [PCK:%s XOR:%s]", filename.c_str(), this->m_fCatFile.filename().c_str(), sTo.c_str(), (pck) ? "Yes" : "No", (bXor) ? "Yes" : "No");
585
	CLog::logf(CLog::Log_IO, 1, L"CCatFile::AppendFile() Adding file, %s, into cat file, %s::%s [PCK:%s XOR:%s]", filename.c_str(), this->m_fCatFile.filename().c_str(), sTo.c_str(), (pck) ? L"Yes" : L"No", (bXor) ? L"Yes" : L"No");
586
 
586
 
587
	if ( filename.isin (L"::" ) ) return writeFromCat(filename.token(L"::", 1), filename.token(L"::", 2));
587
	if ( filename.isin (L"::" ) ) return writeFromCat(filename.token(L"::", 1), filename.token(L"::", 2));
588
	if ( (!m_bCreate) && (!m_fDatFile.exists ()) ) {
588
	if ( (!m_bCreate) && (!m_fDatFile.exists ()) ) {
589
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() Cat File: %s, doesn't exist, quitting...", m_fCatFile.fullFilename().c_str());
589
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() Cat File: %s, doesn't exist, quitting...", m_fCatFile.fullFilename().c_str());
590
		return false;
590
		return false;
591
	}
591
	}
592
	Utils::WString to = sTo;
592
	Utils::WString to = sTo;
593
	if ( !_sAddonDir.empty() && CCatFile::IsAddonDir(to) ) {
593
	if ( !_sAddonDir.empty() && CCatFile::IsAddonDir(to) ) {
594
		CLog::logf(CLog::Log_IO, 3, "CCatFile::AppendFile() changing destination to included addon fir, %s => %s", to, _sAddonDir + L"/" + to);
594
		CLog::logf(CLog::Log_IO, 3, L"CCatFile::AppendFile() changing destination to included addon fir, %s => %s", to, _sAddonDir + L"/" + to);
595
		to = _sAddonDir + L"\\" + to;
595
		to = _sAddonDir + L"\\" + to;
596
	}
596
	}
597
 
597
 
598
	// change the file extension and remove the file again
598
	// change the file extension and remove the file again
599
	if ( pck && checkExtensionPck(filename) ) {
599
	if ( pck && checkExtensionPck(filename) ) {
600
		CLog::logf(CLog::Log_IO, 3, "CCatFile::AppendFile() changing file extension for packed file, %s => %s", to.c_str(), PckChangeExtension(to).c_str());
600
		CLog::logf(CLog::Log_IO, 3, L"CCatFile::AppendFile() changing file extension for packed file, %s => %s", to.c_str(), PckChangeExtension(to).c_str());
601
		to = PckChangeExtension(to);
601
		to = PckChangeExtension(to);
602
	}
602
	}
603
	if ( sChangeTo ) *sChangeTo = to;
603
	if ( sChangeTo ) *sChangeTo = to;
604
 
604
 
605
	if ( !_lFiles->empty() ) {
605
	if ( !_lFiles->empty() ) {
606
		SInCatFile *checkf = findData(to);
606
		SInCatFile *checkf = findData(to);
607
		if ( checkf ) {
607
		if ( checkf ) {
608
			CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() removing existing filechanging file extension for packed file, %s => %s", to.c_str(), PckChangeExtension(to).c_str());
608
			CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() removing existing filechanging file extension for packed file, %s => %s", to.c_str(), PckChangeExtension(to).c_str());
609
			if ( !removeFile(checkf) ) {
609
			if ( !removeFile(checkf) ) {
610
				CLog::logf(CLog::Log_IO, 1, "CCatFile::AppendFile() unable to remove existing file, quitting...");
610
				CLog::logf(CLog::Log_IO, 1, L"CCatFile::AppendFile() unable to remove existing file, quitting...");
611
				return false;
611
				return false;
612
			}
612
			}
613
		}
613
		}
614
	}
614
	}
615
 
615
 
Line 620... Line 620...
620
	f->lOffset = this->GetEndOffset();
620
	f->lOffset = this->GetEndOffset();
621
	
621
	
622
	bool dofile = true;
622
	bool dofile = true;
623
 
623
 
624
	if ( _lFiles->empty() )	{
624
	if ( _lFiles->empty() )	{
625
		CLog::logf(CLog::Log_IO, 3, "CCatFile::AppendFile() no existing files found, wipeing the existing dat file");
625
		CLog::logf(CLog::Log_IO, 3, L"CCatFile::AppendFile() no existing files found, wipeing the existing dat file");
626
		m_fDatFile.WipeFile();
626
		m_fDatFile.WipeFile();
627
	}
627
	}
628
 
628
 
629
	CFileIO File(filename);
629
	CFileIO File(filename);
630
	if ( !File.startRead() ) {
630
	if ( !File.startRead() ) {
631
		CLog::logf(CLog::Log_IO, 3, "CCatFile::AppendFile() unable to open file, %s, for reading", filename.c_str());
631
		CLog::logf(CLog::Log_IO, 3, L"CCatFile::AppendFile() unable to open file, %s, for reading", filename.c_str());
632
		return false;
632
		return false;
633
	}
633
	}
634
	
634
	
635
	CLog::logf(CLog::Log_IO, 3, "CCatFile::AppendFile() reading file data, %s:%d", filename.c_str(), File.fileSize());
635
	CLog::logf(CLog::Log_IO, 3, L"CCatFile::AppendFile() reading file data, %s:%d", filename.c_str(), File.fileSize());
636
	unsigned char *data = File.readAll();
636
	unsigned char *data = File.readAll();
637
	File.close();
637
	File.close();
638
 
638
 
639
	// check if we need to pack the file
639
	// check if we need to pack the file
640
	int iFileType = this->_checkFiletype(data, 3);
640
	int iFileType = this->_checkFiletype(data, 3);
641
	CLog::logf(CLog::Log_IO, 3, "CCatFile::AppendFile() preparing to append file data, Type=%d", iFileType);
641
	CLog::logf(CLog::Log_IO, 3, L"CCatFile::AppendFile() preparing to append file data, Type=%d", iFileType);
642
	if ( iFileType == FILETYPE_PLAIN && CheckPackedExtension(to) ) {
642
	if ( iFileType == FILETYPE_PLAIN && CheckPackedExtension(to) ) {
643
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() plain text file needs to packed");
643
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() plain text file needs to packed");
644
		size_t newsize = 0;
644
		size_t newsize = 0;
645
		unsigned char *newdata = PCKData(data, File.fileSize(), &newsize, bXor);
645
		unsigned char *newdata = PCKData(data, File.fileSize(), &newsize, bXor);
646
		CLog::logf(CLog::Log_IO, 1, "CCatFile::AppendFile() file has been packed, %d => %d", File.fileSize(), newsize);
646
		CLog::logf(CLog::Log_IO, 1, L"CCatFile::AppendFile() file has been packed, %d => %d", File.fileSize(), newsize);
647
 
647
 
648
		f->lSize = newsize;
648
		f->lSize = newsize;
649
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() Decrypting data");
649
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() Decrypting data");
650
		this->DecryptDAT(newdata, f->lSize);
650
		this->DecryptDAT(newdata, f->lSize);
651
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() appending data to Dat file, Offset:%d", f->lOffset);
651
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() appending data to Dat file, Offset:%d", f->lOffset);
652
		append = m_fDatFile.AppendDataToPos ( (const char *)newdata, newsize, f->lOffset );
652
		append = m_fDatFile.AppendDataToPos ( (const char *)newdata, newsize, f->lOffset );
653
 
653
 
654
		delete [] newdata;
654
		delete [] newdata;
655
	}
655
	}
656
	else  {
656
	else  {
657
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() Decrypting data");
657
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() Decrypting data");
658
		this->DecryptDAT(data, File.fileSize());
658
		this->DecryptDAT(data, File.fileSize());
659
		f->lSize = File.fileSize();
659
		f->lSize = File.fileSize();
660
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() appending data to Dat file, Offset:%d", f->lOffset);
660
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() appending data to Dat file, Offset:%d", f->lOffset);
661
		append = m_fDatFile.AppendDataToPos ( (const char *)data, f->lSize, f->lOffset );
661
		append = m_fDatFile.AppendDataToPos ( (const char *)data, f->lSize, f->lOffset );
662
	}
662
	}
663
 
663
 
664
	CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() cleaning up memory");
664
	CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() cleaning up memory");
665
	delete [] data;
665
	delete [] data;
666
 
666
 
667
	m_fDatFile.close();
667
	m_fDatFile.close();
668
 
668
 
669
	if ( append ) {
669
	if ( append ) {
670
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() append complete, adding file into cat list");
670
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() append complete, adding file into cat list");
671
		m_bCreate = false;
671
		m_bCreate = false;
672
		f->sFile = to;
672
		f->sFile = to;
673
		_lFiles->push_back(f);
673
		_lFiles->push_back(f);
674
		CLog::logf(CLog::Log_IO, 3, "CCatFile::AppendFile() writing the new cat file");
674
		CLog::logf(CLog::Log_IO, 3, L"CCatFile::AppendFile() writing the new cat file");
675
		m_bCatChanged = true;
675
		m_bCatChanged = true;
676
	}
676
	}
677
	else {
677
	else {
678
		CLog::logf(CLog::Log_IO, 2, "CCatFile::AppendFile() appending failed, cleaning up file data");
678
		CLog::logf(CLog::Log_IO, 2, L"CCatFile::AppendFile() appending failed, cleaning up file data");
679
		delete f;
679
		delete f;
680
	}
680
	}
681
 
681
 
682
	CLog::logf(CLog::Log_IO, 4, "CCatFile::AppendFile() function complete");
682
	CLog::logf(CLog::Log_IO, 4, L"CCatFile::AppendFile() function complete");
683
 
683
 
684
	return append;
684
	return append;
685
}
685
}
686
 
686
 
687
bool CCatFile::addData(const Utils::WString &catfile, unsigned char *data, size_t size, const Utils::WString &to, bool pck, bool create)
687
bool CCatFile::addData(const Utils::WString &catfile, unsigned char *data, size_t size, const Utils::WString &to, bool pck, bool create)