fzBuffer Data::inflateZIPWithHint(unsigned char *input, unsigned int inLength, unsigned int outLengthHint) { unsigned int outLength = 0; unsigned char* output; int err = inflateMemoryWithHint(input, inLength, &output, &outLength, outLengthHint ); if (err != Z_OK || output == NULL) { if (err == Z_MEM_ERROR) FZLOGERROR("ZIP: Out of memory while decompressing map data."); else if (err == Z_VERSION_ERROR) FZLOGERROR("ZIP: Incompatible zlib version."); else if (err == Z_DATA_ERROR) FZLOGERROR("ZIP: Incorrect zlib compressed data."); else FZLOGERROR("ZIP: Unknown error while decompressing map data."); delete [] output; output = NULL; outLength = 0; } return fzBuffer((char*)output, outLength); }
fzBuffer IO::loadFile(const char *absolutePath) { FZ_ASSERT(absolutePath, "Absolute path cannot be NULL."); if(absolutePath[0] == '\0') return fzBuffer::empty(); FILE *f = fopen(absolutePath, "rb"); if( f == NULL ) return fzBuffer::empty(); fseek(f, 0, SEEK_END); size_t size = ftell(f); fseek(f, 0, SEEK_SET); char *buffer = new(std::nothrow) char[size+1]; if(buffer == NULL) { fclose(f); FZLOGERROR("IO: Impossible to allocate memory."); return fzBuffer::empty(); } size_t read = fread(buffer, 1, size, f); fclose(f); if( read != size ) { FZLOGERROR("IO: Abnormal reading error. Probably the path is not a file."); delete [] buffer; return fzBuffer::empty(); } // NULL TERMINATED buffer[size] = '\0'; return fzBuffer(buffer, size+1); }
void DataStore::setString(const char* value, const char *key) { fzStoreEntry entry; entry.key = fzStrcpy(key); entry.hash = fzHash(key); entry.type = kFZData_string; entry.data = fzBuffer(fzStrcpy(value), strlen(value)+1); setEntry(entry); }
fzBuffer Data::inflateCCZ(unsigned char *input, unsigned int inLength) { // load file into memory const CCZHeader *header = reinterpret_cast<const CCZHeader*>(input); // verify header if(!(header->sig[0] == 'C' && header->sig[1] == 'C' && header->sig[2] == 'Z' && header->sig[3] == '!')) { FZLOGERROR("IO:CCZ: Invalid CCZ file."); return fzBuffer::empty(); } // verify header version if( fzBitOrder_int16BigToHost(header->version) > 2 ) { FZLOGERROR("IO:CCZ: Unsupported version."); return fzBuffer::empty(); } // verify compression format if( fzBitOrder_int16BigToHost(header->compression_type) != CCZ_COMPRESSION_ZLIB ) { FZLOGERROR("IO:CCZ: Unsupported compression method."); return fzBuffer::empty(); } uLong fileLen = inLength-1; uint32_t expectedLen = fzBitOrder_int32BigToHost( header->len ); uLongf realLen = expectedLen; char *contentData = new char[expectedLen]; uLongf source = (uLongf)input + sizeof(CCZHeader); int ret = uncompress((Bytef*)contentData, &realLen, (Bytef*)source, (uLong)(fileLen - sizeof(CCZHeader)) ); if( ret != Z_OK ) { FZLOGERROR("IO:CCZ: Failed to uncompress data. Error code: %d.", ret); delete [] contentData; return fzBuffer::empty(); } FZ_ASSERT(realLen <= expectedLen, "Corrupted .CCZ. The expected uncompressed data length is wrong. Buffer overflow occurred."); return fzBuffer(contentData, expectedLen); }
fzBuffer Data::B64Decode(const char *input, fzUInt inLength) { FZ_ASSERT(input, "Input pointer cannot be NULL"); FZ_ASSERT(inLength > 0, "Input data length cannot be 0."); //should be enough to store 6-bit buffers in 8-bit buffers fzUInt outputSize = Base64decode_len(input); char *output = new(std::nothrow) char[outputSize]; if( output ) { Base64decode(output, input); return fzBuffer(output, outputSize); }else{ FZLOGERROR("Base64: error allocating memory."); return fzBuffer::empty(); } }