int inflateMemory(unsigned char *in, unsigned int inLength, unsigned char *&out) { unsigned int outLength = 0; int ret = inflateMemory(in, inLength, out, outLength); if (ret != Z_OK || out == NULL) { if (ret == Z_MEM_ERROR) { std::cerr << "Error: Out of memory while decompressing data!" << std::endl; } else if (ret == Z_VERSION_ERROR) { std::cerr << "Error: Incompatible zlib version!" << std::endl; } else if (ret == Z_DATA_ERROR) { std::cerr << "Error: Incorrect zlib compressed data!" << std::endl; } else { std::cerr << "Error: Unknown error while decompressing data!" << std::endl; } free(out); out = NULL; outLength = 0; } return outLength; }
Map *MapReader::readMap(const std::string &filename) { logger->log("Attempting to read map %s", filename.c_str()); // Load the file through resource manager ResourceManager *resman = ResourceManager::getInstance(); int fileSize; void *buffer = resman->loadFile(filename, fileSize); Map *map = NULL; if (buffer == NULL) { logger->log("Map file not found (%s)", filename.c_str()); return NULL; } unsigned char *inflated; unsigned int inflatedSize; if (filename.find(".gz", filename.length() - 3) != std::string::npos) { // Inflate the gzipped map data inflatedSize = inflateMemory((unsigned char*) buffer, fileSize, inflated); free(buffer); if (inflated == NULL) { logger->log("Could not decompress map file (%s)", filename.c_str()); return NULL; } } else { inflated = (unsigned char*) buffer; inflatedSize = fileSize; } XML::Document doc((char*) inflated, inflatedSize); free(inflated); xmlNodePtr node = doc.rootNode(); // Parse the inflated map data if (node) { if (!xmlStrEqual(node->name, BAD_CAST "map")) logger->log("Error: Not a map file (%s)!", filename.c_str()); else map = readMap(node, filename); } else logger->log("Error while parsing map file (%s)!", filename.c_str()); if (map) map->setProperty("_filename", filename); return map; }
void *ResourceManager::loadFile(const std::string &filename, int &filesize, bool inflate) { // Attempt to open the specified file using PhysicsFS PHYSFS_file *file = PHYSFS_openRead(filename.c_str()); // If the handler is an invalid pointer indicate failure if (file == NULL) { logger->log("Warning: Failed to load %s: %s", filename.c_str(), PHYSFS_getLastError()); return NULL; } // Log the real dir of the file logger->log("Loaded %s/%s", PHYSFS_getRealDir(filename.c_str()), filename.c_str()); // Get the size of the file filesize = PHYSFS_fileLength(file); // Allocate memory and load the file void *buffer = malloc(filesize); PHYSFS_read(file, buffer, 1, filesize); // Close the file and let the user deallocate the memory PHYSFS_close(file); unsigned char *inflated; unsigned int inflatedSize; if (inflate && filename.find(".gz", filename.length() - 3) != std::string::npos) { // Inflate the gzipped map data inflatedSize = inflateMemory((unsigned char*) buffer, filesize, inflated); free(buffer); if (inflated == NULL) { logger->log("Could not decompress file: %s", filename.c_str()); return NULL; } filesize = inflatedSize; return inflated; } else { return buffer; } }
void *loadCompressedFile(const std::string &filename, int &filesize) { std::ifstream file; file.open(filename.c_str(), std::ios::in); if (file.is_open()) { // Get length of file file.seekg (0, std::ios::end); filesize = file.tellg(); file.seekg(0, std::ios::beg); char *buffer = (char *) malloc(filesize); file.read(buffer, filesize); file.close(); unsigned char *inflated; unsigned int inflatedSize; if (inflated && filename.find(".gz", filename.length() - 3) != std::string::npos) { // Inflate the gzipped map data inflatedSize = inflateMemory((unsigned char*) buffer, filesize, inflated); free(buffer); if (inflated == NULL) { std::cerr << "Could not decompress file: " << filename.c_str() << std::endl; return NULL; } filesize = inflatedSize; return inflated; } else { return buffer; } } else { std::cerr << "Error loading file from drive: " << filename.c_str() << std::endl; } return NULL; }
int inflateMemory(unsigned char *in, const unsigned int inLength, unsigned char *&out) { unsigned int outLength = 0; const int ret = inflateMemory(in, inLength, out, outLength); if (ret != Z_OK || out == NULL) { if (ret == Z_MEM_ERROR) logger->log("Error: Out of memory while decompressing map data!"); else if (ret == Z_VERSION_ERROR) logger->log("Error: Incompatible zlib version!"); else if (ret == Z_DATA_ERROR) logger->log("Error: Incorrect zlib compressed data!"); else logger->log("Error: Unknown error while decompressing map data!"); free(out); out = NULL; outLength = 0; } return outLength; }