Set the file pointer.
189{
190 unsigned char grf_header[0x2e];
191 std::ifstream grf_ifs(
getGRFPath().c_str(), std::ios::in | std::ios::binary);
192
196 if (!grf_ifs.good())
198
199
200 grf_ifs.seekg(0, grf_ifs.end);
202 grf_ifs.seekg(0, grf_ifs.beg);
203
204 if (!grf_ifs.read((char *) grf_header, 0x2e))
206
207 if (std::strcmp((const char *) grf_header, "Master of Magic") != 0)
209
210 if (grf_ifs.seekg(
GetLongLong(grf_header + 0x1e), std::ios::cur).
fail())
212
213
215
218
219 unsigned char eheader[8];
220
221 if (!grf_ifs.read((char *) eheader, 8))
223
224 unsigned long compressed_size =
GetULong(eheader);
225 unsigned long decompressed_size =
GetULong(eheader + 4);
226
227 if (compressed_size > (
unsigned long) (
getGRFSize() - grf_ifs.tellg()))
229
230 uint8_t *compressed_buf = new uint8_t[compressed_size];
231 uint8_t *decompressed_buf = new uint8_t[decompressed_size];
232
233 if (!grf_ifs.read((char *) compressed_buf, compressed_size)) {
234 delete[] decompressed_buf;
235 delete[] compressed_buf;
237 }
238
239 uncompress(decompressed_buf, &decompressed_size, compressed_buf, compressed_size);
240
241 delete[] compressed_buf;
242
244
245 for (
int entry = 0, ofs = 0; entry <
getTotalFiles(); ++entry) {
246 std::shared_ptr<DataFile> data_file = std::make_shared<DataFile>();
247 char *fname = (char *) (decompressed_buf + ofs);
248 int ofs2 = ofs + (int) strlen(fname) + 1;
249 int type = decompressed_buf[ofs2 + 12];
250
251 ofs = ofs2 + 17;
252
253 if (strlen(fname) > sizeof(data_file->file_name) - 1) {
255 continue;
256 }
257
259 data_file->compressed_size =
GetLong(decompressed_buf + ofs2 + 0);
260 data_file->compressed_aligned_size =
GetLong(decompressed_buf + ofs2 + 4);
261 data_file->original_size =
GetLong(decompressed_buf + ofs2 + 8);
262 data_file->entry_position =
GetLong(decompressed_buf + ofs2 + 13) + 0x2E;
263 data_file->type = type;
264 std::memcpy(&data_file->file_name, fname, sizeof(data_file->file_name));
265 getFileMap().insert(std::pair<std::string, std::shared_ptr<DataFile>>(data_file->file_name, data_file));
266 }
267 }
268
269 delete[] decompressed_buf;
270
272}
@ GRF_FILE_ERROR_NAME_TOO_LONG
Definition: GRF.hpp:62
@ DATAFILE_TYPE_FILE
Definition: GRF.hpp:41
@ GRF_LOAD_INCOMPLETE_HEADER
Definition: GRF.hpp:50
@ GRF_LOAD_ILLEGAL_DATA_FORMAT
Definition: GRF.hpp:55
@ GRF_LOAD_OK
Definition: GRF.hpp:48
@ GRF_LOAD_MAGIC_ERROR
Definition: GRF.hpp:51
@ GRF_LOAD_FORMAT_ERROR
Definition: GRF.hpp:52
@ GRF_LOAD_READ_ERROR
Definition: GRF.hpp:56
@ GRF_LOAD_PATH_ERROR
Definition: GRF.hpp:49
@ GRF_LOAD_INVALID_VERSION
Definition: GRF.hpp:53
@ GRF_LOAD_HEADER_READ_ERROR
Definition: GRF.hpp:54
std::atomic< bool > fail
Definition: LockedLookupTableTest.cpp:49
int64_t GetLongLong(const unsigned char *buf)
Definition: Utility.cpp:234
unsigned int GetULong(unsigned char *p)
Definition: Utility.cpp:169
int32_t GetLong(const unsigned char *buf)
Definition: Utility.cpp:229
const boost::filesystem::path & getGRFPath() const
Definition: GRF.hpp:105
FileErrorMapType & getFileErrorMap()
Definition: GRF.hpp:118
int getTotalFiles() const
Definition: GRF.hpp:114
void setTotalFiles(int total)
Definition: GRF.hpp:115
std::size_t getGRFSize() const
Definition: GRF.hpp:108
int getGRFVersion() const
Definition: GRF.hpp:111
void setGRFVersion(int version)
Definition: GRF.hpp:112