Subversion Repositories spk

Rev

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

Rev 42 Rev 51
Line 4... Line 4...
4
#include "DirIO.h"
4
#include "DirIO.h"
-
 
5
#include "Logging/Log.h"
5
 
6
 
6
CCatFile::CCatFile ()
7
CCatFile::CCatFile ()
7
{
8
{
8
	m_iDataType = CATFILE_NONE;
9
	m_iDataType = CATFILE_NONE;
9
	m_sData = NULL;
10
	m_sData = NULL;
Line 45... Line 46...
45
{
46
{
46
	if ( error == CATERR_NONE ) return true;
47
	if ( error == CATERR_NONE ) return true;
47
	if ( allowCreate && error == CATERR_CREATED ) return true;
48
	if ( allowCreate && error == CATERR_CREATED ) return true;
48
	return false;
49
	return false;
49
}
50
}
50
int CCatFile::Open ( CyString catfile, CyString addon, int readtype, bool create )
51
int CCatFile::Open ( CyString sCatfile, CyString addon, int readtype, bool create )
51
{
52
{
-
 
53
	Utils::String catfile = sCatfile.ToString();
-
 
54
 
52
	m_bCreate = false;
55
	m_bCreate = false;
53
 
56
 
54
	CyString datfile;
57
	Utils::String datfile;
55
	if ( !catfile.Right(4).Compare(".cat") )
58
	if ( !catfile.right(4).Compare(".cat") ) {
56
	{
-
 
57
		datfile = catfile + ".dat";
59
		datfile = catfile + ".dat";
58
		catfile += ".cat";
60
		catfile += ".cat";
59
	}
61
	}
60
	else
-
 
61
		datfile = catfile.Left ( -4 ) + ".dat";
62
	else datfile = catfile.left(-4) + ".dat";
62
 
63
 
63
	if ( readtype == CATREAD_JUSTCONTENTS )
64
	if ( readtype == CATREAD_JUSTCONTENTS ) create = false;
64
		create = false;
-
 
65
 
65
 
66
	// first check if the dat file exists and opens
66
	// first check if the dat file exists and opens
67
	bool created = false;
67
	bool created = false;
68
	if ( readtype != CATREAD_JUSTCONTENTS )
68
	if ( readtype != CATREAD_JUSTCONTENTS ) {
69
	{
-
 
70
		FILE *id = fopen ( datfile.c_str(), "rb+" );
69
		std::ifstream file(datfile);
71
		if ( !id )
70
		if ( !file.good() ) {
72
		{
-
 
73
			if ( create )
-
 
74
				created = true;
71
			if ( create ) created = true;
75
			else
-
 
76
				return CATERR_NODATFILE;
72
			else		  return CATERR_NODATFILE;
77
		}
73
		}
78
 
74
 
79
		if ( !created && id )
75
		if ( file.is_open() ) file.close();
80
			fclose ( id );
-
 
81
	}
76
	}
82
 
77
 
83
	// now open the cat file to read
78
	// now open the cat file to read
-
 
79
	std::fstream File;
84
	FILE *cid = fopen ( catfile.c_str(), "rb" );
80
	File.open(catfile.c_str(), std::ios::in | std::ios::binary);
85
	if ( !cid )
-
 
86
	{
-
 
87
		if ( create )
81
	if ( !File.is_open() ) {
88
			created = true;
82
		if ( create ) created = true;
89
		else
-
 
90
			return CATERR_NOCATFILE;
83
		else		  return CATERR_NOCATFILE;
91
	}
84
	}
92
 
85
 
93
	if ( created )
86
	if ( created ) {
94
	{
-
 
95
		m_fCatFile.Open ( catfile );
87
		m_fCatFile.Open(catfile);
96
		m_fDatFile.Open ( datfile );
88
		m_fDatFile.Open(datfile);
97
		m_bCreate = true;
89
		m_bCreate = true;
98
		return CATERR_CREATED;
90
		return CATERR_CREATED;
99
	}
91
	}
100
 
92
 
101
	// find the file size
93
	// find the file size
102
	fseek ( cid, 0, SEEK_END );
94
	File.seekg(0, std::ios::end);
103
	size_t lFileSize = ftell ( cid );
95
	size_t lFileSize = File.tellg();
104
	fseek ( cid, 0, SEEK_SET );
96
	File.seekg(0, std::ios::beg);
105
 
97
 
106
	if ( !lFileSize )
98
	if ( !lFileSize ) {
107
	{
-
 
108
		fclose ( cid );
99
		File.close();
109
		return CATERR_FILEEMPTY;
100
		return CATERR_FILEEMPTY;
110
	}
101
	}
111
 
102
 
112
	// size must be multiples of 5
103
	// size must be multiples of 5
113
	size_t size = lFileSize + ((lFileSize % 5) ? 5 - (lFileSize % 5) : 0);
104
	size_t size = lFileSize + ((lFileSize % 5) ? 5 - (lFileSize % 5) : 0);
114
 
105
 
115
	// read cat to buffer
106
	// read cat to buffer
-
 
107
	try {
116
	m_sData = new unsigned char[size + 1];
108
		m_sData = new unsigned char[size + 1];
-
 
109
	}
117
	m_sData[lFileSize] = 0;
110
	catch (std::exception &;e) {
-
 
111
		CLog::logf(CLog::Log_IO, 2, "Memory Exception error, unable to alloc enough memory to store file data, %d (%s)", size + 1, e.what());
-
 
112
		return CATERR_MALLOC;
-
 
113
	}
-
 
114
 
118
	m_lSize = size;
115
	m_lSize = size;
-
 
116
	try {
119
	fread ( m_sData, sizeof(unsigned char), m_lSize, cid );
117
		File.read((char *)m_sData, m_lSize);
120
	if ( ferror(cid)  )
118
	} catch (std::exception &e) {
121
	{
-
 
-
 
119
		CLog::logf(CLog::Log_IO, 3, "CCatFile::Open() unable to read from cat file, %s", e.what());
122
		RemoveData ();
120
		RemoveData ();
123
		fclose ( cid );
121
		File.close();
124
		return CATERR_READCAT;
122
		return CATERR_READCAT;
125
	}
123
	}
126
 
124
 
-
 
125
	m_sData[size] = 0;
127
	m_iDataType = CATFILE_READ;
126
	m_iDataType = CATFILE_READ;
128
 
-
 
129
	fclose ( cid );
127
	File.close();
130
 
128
 
131
	if ( readtype != CATREAD_CAT )
129
	if ( readtype != CATREAD_CAT ) {
132
	{
-
 
133
		if ( !DecryptData () )
-
 
134
			return CATERR_DECRYPT;
130
		if ( !DecryptData () ) return CATERR_DECRYPT;
135
		m_sData[lFileSize] = 0;
131
		m_sData[lFileSize] = 0;
136
 
-
 
137
		ReadFiles ();
132
		ReadFiles ();
138
	}
133
	}
139
 
134
 
140
	m_sAddonDir = addon;
135
	m_sAddonDir = addon;
141
 
136
 
142
	m_fCatFile.Open ( catfile );
137
	m_fCatFile.Open ( catfile );
143
 
138
 
144
	if ( readtype != CATREAD_JUSTCONTENTS )
139
	if ( readtype != CATREAD_JUSTCONTENTS ) {
145
	{
-
 
146
		m_fDatFile.Open ( datfile );
140
		m_fDatFile.Open ( datfile );
147
 
141
 
148
		// check the file size matches
142
		// check the file size matches
149
		long compare = 0;
143
		long compare = 0;
150
		FILE *id = fopen ( datfile.c_str(), "rb" );
144
		std::fstream file(datfile, std::ios::in | std::ios::binary);
151
		if ( id )
145
		if ( file.is_open() ) {
152
		{
-
 
153
			fseek ( id, 0, SEEK_END );
146
			file.seekg(0, std::ios::end);
154
			compare = ftell ( id );
147
			compare = file.tellg();
155
			fclose ( id );
148
			file.close();
156
		}
149
		}
157
		/*
-
 
158
		SInCatFile *c = m_lFiles.Back ()->Data();
150
		SInCatFile *c = m_lFiles.Back ()->Data();
159
		if ( (c->lSize + c->lOffset) != compare )
151
		//if ( (c->lSize + c->lOffset) != compare ) return CATERR_MISMATCH;
160
			return CATERR_MISMATCH;
-
 
161
		*/
-
 
162
	}
152
	}
163
 
-
 
164
 
153
 
165
	if ( readtype >= CATREAD_DAT )
154
	if ( readtype >= CATREAD_DAT ) LoadDatFile ();
166
		LoadDatFile ();
-
 
167
 
155
 
168
	return CATERR_NONE;
156
	return CATERR_NONE;
169
}
157
}
170
 
158
 
171
bool CCatFile::RemoveFile ( CyString file )
159
bool CCatFile::RemoveFile ( CyString file )
172
{
160
{
173
	if ( !m_sAddonDir.Empty() ) {
161
	if ( !m_sAddonDir.Empty() ) {
174
		SInCatFile *f = FindData ( m_sAddonDir + "\\" + file );
162
		SInCatFile *f = FindData ( m_sAddonDir + "\\" + file );
175
		if ( f )
163
		if ( f )
176
			RemoveFile(f);
164
			RemoveFile(f);
177
	}
165
	}
178
	{
166
	{
179
		SInCatFile *f = FindData ( CyString("addon\\") + file );
167
		SInCatFile *f = FindData ( CyString("addon\\") + file );
180
		if ( f )
168
		if ( f )
181
			RemoveFile(f);
169
			RemoveFile(f);
182
	}
170
	}
Line 189... Line 177...
189
 
177
 
190
	return RemoveFile ( f );
178
	return RemoveFile ( f );
191
}
179
}
192
 
180
 
193
void CCatFile::LoadDatFile ()
181
void CCatFile::LoadDatFile ()
194
{
182
{
195
	if ( m_fDatFile.NoFile() )
183
	if ( m_fDatFile.NoFile() )
196
		return;
184
		return;
197
 
185
 
198
	FILE *id = fopen ( m_fDatFile.GetFullFilename().c_str(), "rb" );
186
	std::fstream File(m_fDatFile.GetFullFilename().ToString().c_str(), std::ios::in | std::ios::binary);
199
	if ( id )
187
	if ( File.is_open() ) {
200
	{
-
 
201
		for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() )
188
		for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() )
202
		{
189
		{
203
			SInCatFile *c = node->Data();
190
			SInCatFile *c = node->Data();
204
			if ( c->sData )
-
 
205
				delete c->sData;
191
			if ( c->sData )	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
 
192
 
-
 
193
			try {
-
 
194
				c->sData = new unsigned char[c->lSize + 1];
-
 
195
			}
-
 
196
			catch (std::exception &e) {
-
 
197
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to malloc data storage: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
-
 
198
				continue;
-
 
199
			}
-
 
200
 
-
 
201
			try {
-
 
202
				File.read((char *)c->sData, c->lSize);
-
 
203
			}
-
 
204
			catch(std::exception &e) {
-
 
205
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to read file data: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
-
 
206
				continue;
-
 
207
			}
211
			DecryptDAT(c);
208
			this->DecryptDAT(c);
212
		}
209
		}
213
		fclose ( id );
210
		File.close();
214
	}
211
	}
215
}
212
}
216
 
213
 
217
bool CCatFile::ReadFiles ()
214
bool CCatFile::ReadFiles ()
218
{
215
{
Line 260... Line 257...
260
		else if ( m_sData[num] == ' ' )
257
		else if ( m_sData[num] == ' ' )
261
			spacePos = num;
258
			spacePos = num;
262
		++num;
259
		++num;
263
	}
260
	}
264
 
261
 
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;
262
	return true;
289
}
263
}
290
 
264
 
291
bool CCatFile::DecryptData ( unsigned char *data, size_t size )
265
bool CCatFile::DecryptData ( unsigned char *data, size_t size )
292
{
266
{
Line 314... Line 288...
314
	return true;
288
	return true;
315
}
289
}
316
 
290
 
317
bool CCatFile::DecryptData ()
291
bool CCatFile::DecryptData ()
318
{
292
{
319
	if ( !DecryptData ( m_sData, m_lSize ) )
293
	if ( !DecryptData ( m_sData, m_lSize ) ) return false;
320
		return false;
-
 
321
 
294
 
322
	m_iDataType = CATERR_DECRYPT;
295
	m_iDataType = CATERR_DECRYPT;
323
 
296
 
324
	return true;
297
	return true;
325
}
298
}
326
 
299
 
327
void CCatFile::RemoveData ()
300
void CCatFile::RemoveData ()
328
{
301
{
329
	if ( m_sData )
-
 
330
		delete m_sData;
302
	if ( m_sData ) delete m_sData;
331
	m_sData = NULL;
303
	m_sData = NULL;
332
	m_iDataType = CATFILE_NONE;
304
	m_iDataType = CATFILE_NONE;
333
	m_lSize = 0;
305
	m_lSize = 0;
334
}
306
}
335
 
307
 
Line 350... Line 322...
350
 
322
 
351
unsigned char *CCatFile::ReadData ( SInCatFile *c, size_t *size )
323
unsigned char *CCatFile::ReadData ( SInCatFile *c, size_t *size )
352
{
324
{
353
	*size = c->lSize;
325
	*size = c->lSize;
354
 
326
 
355
	FILE *id = fopen ( m_fDatFile.GetFullFilename().c_str(), "rb" );
327
	std::fstream File(m_fDatFile.GetFullFilename().c_str(), std::ios::in | std::ios::binary);
-
 
328
	if ( File.is_open() ) {
-
 
329
		File.seekg(c->lOffset, std::ios::beg);
-
 
330
		unsigned char *data;
356
	if ( id )
331
		try {
-
 
332
			data = new unsigned char[c->lSize + 1];
357
	{
333
		}
358
		fseek ( id, (long)c-&gt;lOffset, SEEK_SET );
334
		catch(std::exception &amp;e) {
-
 
335
			CLog::logf(CLog::Log_IO, 2, "CCatFile::ReadData() unable to malloc data storage: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
-
 
336
			File.close();
-
 
337
			return NULL;
-
 
338
		}
-
 
339
 
-
 
340
		try {
359
		unsigned char *data = new unsigned char[c->lSize + 1];
341
			File.read((char *)data, c->lSize);
-
 
342
		}
360
		fread ( data, sizeof(unsigned char), c-&gt;lSize, id );
343
		catch(std::exception &amp;e) {
-
 
344
			CLog::logf(CLog::Log_IO, 2, "CCatFile::ReadData() unable to read file data: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
-
 
345
			File.close();
-
 
346
			return NULL;
-
 
347
		}
-
 
348
 
-
 
349
		File.close();
361
		*size = c->lSize;
350
		*size = c->lSize;
362
		data[c->lSize] = '\0';
351
		data[c->lSize] = '\0';
363
 
352
 
364
		DecryptDAT(data, c->lSize);
353
		DecryptDAT(data, c->lSize);
365
		c->bDecrypted = true;
354
		c->bDecrypted = true;
366
 
-
 
367
		fclose ( id );
-
 
368
 
355
 
369
		return data;
356
		return data;
370
	}
357
	}
371
 
358
 
372
	return NULL;
359
	return NULL;