Subversion Repositories spk

Rev

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

Rev 65 Rev 82
Line 85... Line 85...
85
 
85
 
86
		if ( file.is_open() ) file.close();
86
		if ( file.is_open() ) file.close();
87
	}
87
	}
88
 
88
 
89
	// now open the cat file to read
89
	// now open the cat file to read
90
	std::fstream File;
90
	CFileIO File(catfile);
91
	File.open(catfile.c_str(), std::ios::in | std::ios::binary);
-
 
92
	if ( !File.is_open() ) {
91
	if ( !File.startRead() ) {
93
		if ( create ) created = true;
92
		if ( create ) created = true;
94
		else		  return CATERR_NOCATFILE;
93
		else		  return CATERR_NOCATFILE;
95
	}
94
	}
96
 
95
 
97
	if ( created ) {
96
	if ( created ) {
Line 100... Line 99...
100
		m_bCreate = true;
99
		m_bCreate = true;
101
		return CATERR_CREATED;
100
		return CATERR_CREATED;
102
	}
101
	}
103
 
102
 
104
	// find the file size
103
	// find the file size
105
	File.seekg(0, std::ios::end);
-
 
106
	size_t lFileSize = File.tellg();
-
 
107
	File.seekg(0, std::ios::beg);
-
 
108
 
-
 
109
	if ( !lFileSize ) {
104
	if ( !File.fileSize() ) {
110
		File.close();
105
		File.close();
111
		return CATERR_FILEEMPTY;
106
		return CATERR_FILEEMPTY;
112
	}
107
	}
113
 
108
 
114
	// size must be multiples of 5
109
	// size must be multiples of 5
115
	size_t size = lFileSize + ((lFileSize % 5) ? 5 - (lFileSize % 5) : 0);
110
	size_t size = File.fileSize() + ((File.fileSize() % 5) ? 5 - (File.fileSize() % 5) : 0);
116
 
111
 
117
	// read cat to buffer
112
	// read cat to buffer
118
	try {
113
	try {
119
		if ( m_sData ) delete [] m_sData;
114
		if ( m_sData ) delete [] m_sData;
120
		m_sData = new unsigned char[size + 1];
115
		m_sData = new unsigned char[size + 1];
Line 124... Line 119...
124
		return CATERR_MALLOC;
119
		return CATERR_MALLOC;
125
	}
120
	}
126
 
121
 
127
	m_lSize = size;
122
	m_lSize = size;
128
	try {
123
	try {
129
		File.read((char *)m_sData, m_lSize);
124
		File.read(m_sData, m_lSize);
130
	} catch (std::exception &e) {
125
	} catch (std::exception &e) {
131
		CLog::logf(CLog::Log_IO, 3, "CCatFile::Open() unable to read from cat file, %s", e.what());
126
		CLog::logf(CLog::Log_IO, 3, "CCatFile::Open() unable to read from cat file, %s", e.what());
132
		RemoveData ();
127
		RemoveData ();
133
		File.close();
128
		File.close();
134
		return CATERR_READCAT;
129
		return CATERR_READCAT;
Line 138... Line 133...
138
	m_iDataType = CATFILE_READ;
133
	m_iDataType = CATFILE_READ;
139
	File.close();
134
	File.close();
140
 
135
 
141
	if ( readtype != CATREAD_CAT ) {
136
	if ( readtype != CATREAD_CAT ) {
142
		if ( !DecryptData () ) return CATERR_DECRYPT;
137
		if ( !DecryptData () ) return CATERR_DECRYPT;
143
		m_sData[lFileSize] = 0;
138
		m_sData[File.fileSize()] = 0;
144
		ReadFiles ();
139
		ReadFiles ();
145
	}
140
	}
146
 
141
 
147
	m_sAddonDir = addon;
142
	m_sAddonDir = addon;
148
 
143
 
Line 151... Line 146...
151
	if ( readtype != CATREAD_JUSTCONTENTS ) {
146
	if ( readtype != CATREAD_JUSTCONTENTS ) {
152
		m_fDatFile.Open ( datfile );
147
		m_fDatFile.Open ( datfile );
153
 
148
 
154
		// check the file size matches
149
		// check the file size matches
155
		long compare = 0;
150
		long compare = 0;
156
		std::fstream file(datfile, std::ios::in | std::ios::binary);
-
 
157
		if ( file.is_open() ) {
151
		if ( m_fDatFile.startRead() ) {
158
			file.seekg(0, std::ios::end);
-
 
159
			compare = file.tellg();
152
			compare = m_fDatFile.fileSize();
160
			file.close();
153
			m_fDatFile.close();
161
		}
154
		}
162
		SInCatFile *c = m_lFiles.Back ()->Data();
155
		SInCatFile *c = m_lFiles.Back ()->Data();
163
		//if ( (c->lSize + c->lOffset) != compare ) return CATERR_MISMATCH;
156
		//if ( (c->lSize + c->lOffset) != compare ) return CATERR_MISMATCH;
164
	}
157
	}
165
 
158
 
Line 193... Line 186...
193
void CCatFile::LoadDatFile ()
186
void CCatFile::LoadDatFile ()
194
{
187
{
195
	if ( m_fDatFile.NoFile() )
188
	if ( m_fDatFile.NoFile() )
196
		return;
189
		return;
197
 
190
 
198
	std::fstream File(m_fDatFile.GetFullFilename().ToString().c_str(), std::ios::in | std::ios::binary);
-
 
199
	if ( File.is_open() ) {
191
	if ( m_fDatFile.startRead() ) {
200
		for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() )
192
		for ( CListNode<SInCatFile> *node = m_lFiles.Front(); node; node = node->next() )
201
		{
193
		{
202
			SInCatFile *c = node->Data();
194
			SInCatFile *c = node->Data();
203
			if ( c->sData )	delete c->sData;
195
			if ( c->sData )	delete c->sData;
204
 
196
 
Line 209... Line 201...
209
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to malloc data storage: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
201
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to malloc data storage: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
210
				continue;
202
				continue;
211
			}
203
			}
212
 
204
 
213
			try {
205
			try {
214
				File.read((char *)c->sData, c->lSize);
206
				m_fDatFile.read(c->sData, c->lSize);
215
			}
207
			}
216
			catch(std::exception &e) {
208
			catch(std::exception &e) {
217
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to read file data: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
209
				CLog::logf(CLog::Log_IO, 2, "CCatFile::LoadDatFile() unable to read file data: %s, %d (%s)", c->sFile.c_str(), c->lSize, e.what());
218
				continue;
210
				continue;
219
			}
211
			}
220
			c->bDecrypted = false;
212
			c->bDecrypted = false;
221
			this->DecryptDAT(c);
213
			this->DecryptDAT(c);
222
		}
214
		}
223
		File.close();
215
		m_fDatFile.close();
224
	}
216
	}
225
}
217
}
226
 
218
 
227
bool CCatFile::ReadFiles ()
219
bool CCatFile::ReadFiles ()
228
{
220
{