A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Stream inflated EPUB HTMLs down to disk instead of inflating in memory (#4)

* Downgrade miniz for stability

* Stream HTML from ZIP down to disk instead of loading all in mem

authored by

Dave Allie and committed by
GitHub
de453fed c715c18b

+855 -1106
+7 -13
lib/Epub/Epub.cpp
··· 9 9 bool Epub::findContentOpfFile(const ZipFile& zip, std::string& contentOpfFile) { 10 10 // open up the meta data to find where the content.opf file lives 11 11 size_t s; 12 - const auto metaInfo = zip.readTextFileToMemory("META-INF/container.xml", &s); 12 + const auto metaInfo = reinterpret_cast<char*>(zip.readFileToMemory("META-INF/container.xml", &s, true)); 13 13 if (!metaInfo) { 14 14 Serial.println("Could not find META-INF/container.xml"); 15 15 return false; ··· 57 57 58 58 bool Epub::parseContentOpf(ZipFile& zip, std::string& content_opf_file) { 59 59 // read in the content.opf file and parse it 60 - auto contents = zip.readTextFileToMemory(content_opf_file.c_str()); 60 + auto contents = reinterpret_cast<char*>(zip.readFileToMemory(content_opf_file.c_str(), nullptr, true)); 61 61 62 62 // parse the contents 63 63 tinyxml2::XMLDocument doc; ··· 168 168 return false; 169 169 } 170 170 171 - const auto ncxData = zip.readTextFileToMemory(tocNcxItem.c_str()); 171 + const auto ncxData = reinterpret_cast<char*>(zip.readFileToMemory(tocNcxItem.c_str(), nullptr, true)); 172 172 if (!ncxData) { 173 173 Serial.printf("Could not find %s\n", tocNcxItem.c_str()); 174 174 return false; ··· 308 308 return result; 309 309 } 310 310 311 - uint8_t* Epub::getItemContents(const std::string& itemHref, size_t* size) const { 311 + uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, bool trailingNullByte) const { 312 312 const ZipFile zip("/sd" + filepath); 313 313 const std::string path = normalisePath(itemHref); 314 314 315 - const auto content = zip.readFileToMemory(path.c_str(), size); 315 + const auto content = zip.readFileToMemory(path.c_str(), size, trailingNullByte); 316 316 if (!content) { 317 317 Serial.printf("Failed to read item %s\n", path.c_str()); 318 318 return nullptr; ··· 321 321 return content; 322 322 } 323 323 324 - char* Epub::getTextItemContents(const std::string& itemHref, size_t* size) const { 324 + bool Epub::readItemContentsToStream(const std::string& itemHref, Print& out, const size_t chunkSize) const { 325 325 const ZipFile zip("/sd" + filepath); 326 326 const std::string path = normalisePath(itemHref); 327 327 328 - const auto content = zip.readTextFileToMemory(path.c_str(), size); 329 - if (!content) { 330 - Serial.printf("Failed to read item %s\n", path.c_str()); 331 - return nullptr; 332 - } 333 - 334 - return content; 328 + return zip.readFileToStream(path.c_str(), out, chunkSize); 335 329 } 336 330 337 331 int Epub::getSpineItemsCount() const { return spine.size(); }
+3 -2
lib/Epub/Epub.h
··· 56 56 const std::string& getPath() const; 57 57 const std::string& getTitle() const; 58 58 const std::string& getCoverImageItem() const; 59 - uint8_t* getItemContents(const std::string& itemHref, size_t* size = nullptr) const; 60 - char* getTextItemContents(const std::string& itemHref, size_t* size = nullptr) const; 59 + uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr, 60 + bool trailingNullByte = false) const; 61 + bool readItemContentsToStream(const std::string& itemHref, Print& out, size_t chunkSize) const; 61 62 std::string& getSpineItem(int spineIndex); 62 63 int getSpineItemsCount() const; 63 64 EpubTocEntry& getTocItem(int tocTndex);
+5
lib/Epub/Epub/EpubHtmlParserSlim.cpp
··· 199 199 XML_SetCharacterDataHandler(parser, characterData); 200 200 201 201 FILE* file = fopen(filepath, "r"); 202 + if (!file) { 203 + Serial.printf("Couldn't open file %s\n", filepath); 204 + XML_ParserFree(parser); 205 + return false; 206 + } 202 207 203 208 do { 204 209 void* const buf = XML_GetBuffer(parser, 1024);
+11 -18
lib/Epub/Epub/Section.cpp
··· 64 64 void Section::clearCache() const { SD.rmdir(cachePath.c_str()); } 65 65 66 66 bool Section::persistPageDataToSD() { 67 - size_t size = 0; 68 - auto localPath = epub->getSpineItem(spineIndex); 67 + const auto localPath = epub->getSpineItem(spineIndex); 69 68 70 - const auto html = epub->getItemContents(epub->getSpineItem(spineIndex), &size); 71 - if (!html) { 72 - Serial.println("Failed to read item contents"); 73 - return false; 74 - } 75 - 76 - // TODO: Would love to stream this through an XML visitor 69 + // TODO: Should we get rid of this file all together? 70 + // It currently saves us a bit of memory by allowing for all the inflation bits to be released 71 + // before loading the XML parser 77 72 const auto tmpHtmlPath = epub->getCachePath() + "/.tmp_" + std::to_string(spineIndex) + ".html"; 78 - File f = SD.open(tmpHtmlPath.c_str(), FILE_WRITE); 79 - const auto written = f.write(html, size); 73 + File f = SD.open(tmpHtmlPath.c_str(), FILE_WRITE, true); 74 + bool success = epub->readItemContentsToStream(localPath, f, 1024); 80 75 f.close(); 81 - free(html); 82 76 83 - Serial.printf("Wrote %d bytes to %s\n", written, tmpHtmlPath.c_str()); 84 - 85 - if (size != written) { 86 - Serial.println("Failed to inflate section contents to SD"); 87 - SD.remove(tmpHtmlPath.c_str()); 77 + if (!success) { 78 + Serial.println("Failed to stream item contents"); 88 79 return false; 89 80 } 90 81 82 + Serial.printf("Streamed HTML to %s\n", tmpHtmlPath.c_str()); 83 + 91 84 const auto sdTmpHtmlPath = "/sd" + tmpHtmlPath; 92 85 93 86 auto visitor = 94 87 EpubHtmlParserSlim(sdTmpHtmlPath.c_str(), renderer, [this](const Page* page) { this->onPageComplete(page); }); 95 - const bool success = visitor.parseAndBuildPages(); 88 + success = visitor.parseAndBuildPages(); 96 89 97 90 SD.remove(tmpHtmlPath.c_str()); 98 91 if (!success) {
+224 -57
lib/ZipFile/ZipFile.cpp
··· 3 3 #include <HardwareSerial.h> 4 4 #include <miniz.h> 5 5 6 - int libzInflateOneShot(const uint8_t* inputBuff, const size_t compSize, uint8_t* outputBuff, const size_t uncompSize) { 7 - mz_stream pStream = { 8 - .next_in = inputBuff, 9 - .avail_in = compSize, 10 - .total_in = 0, 11 - .next_out = outputBuff, 12 - .avail_out = uncompSize, 13 - .total_out = 0, 14 - }; 15 - 16 - int status = 0; 17 - status = mz_inflateInit2(&pStream, -MZ_DEFAULT_WINDOW_BITS); 18 - 19 - if (status != MZ_OK) { 20 - Serial.printf("inflateInit2 failed: %d\n", status); 21 - return status; 6 + bool inflateOneShot(const uint8_t* inputBuf, const size_t deflatedSize, uint8_t* outputBuf, const size_t inflatedSize) { 7 + // Setup inflator 8 + const auto inflator = static_cast<tinfl_decompressor*>(malloc(sizeof(tinfl_decompressor))); 9 + if (!inflator) { 10 + Serial.println("Failed to allocate memory for inflator"); 11 + return false; 22 12 } 13 + memset(inflator, 0, sizeof(tinfl_decompressor)); 14 + tinfl_init(inflator); 23 15 24 - status = mz_inflate(&pStream, MZ_FINISH); 25 - if (status != MZ_STREAM_END) { 26 - Serial.printf("inflate failed: %d\n", status); 27 - return status; 28 - } 16 + size_t inBytes = deflatedSize; 17 + size_t outBytes = inflatedSize; 18 + const tinfl_status status = tinfl_decompress(inflator, inputBuf, &inBytes, nullptr, outputBuf, &outBytes, 19 + TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); 20 + free(inflator); 29 21 30 - status = mz_inflateEnd(&pStream); 31 - if (status != MZ_OK) { 32 - Serial.printf("inflateEnd failed: %d\n", status); 33 - return status; 22 + if (status != TINFL_STATUS_DONE) { 23 + Serial.printf("tinfl_decompress() failed with status %d\n", status); 24 + return false; 34 25 } 35 26 36 - return status; 37 - } 38 - 39 - char* ZipFile::readTextFileToMemory(const char* filename, size_t* size) const { 40 - const auto data = readFileToMemory(filename, size, true); 41 - return data ? reinterpret_cast<char*>(data) : nullptr; 27 + return true; 42 28 } 43 29 44 - uint8_t* ZipFile::readFileToMemory(const char* filename, size_t* size, bool trailingNullByte) const { 30 + bool ZipFile::loadFileStat(const char* filename, mz_zip_archive_file_stat* fileStat) const { 45 31 mz_zip_archive zipArchive = {}; 46 32 const bool status = mz_zip_reader_init_file(&zipArchive, filePath.c_str(), 0); 47 33 48 34 if (!status) { 49 35 Serial.printf("mz_zip_reader_init_file() failed!\nError %s\n", mz_zip_get_error_string(zipArchive.m_last_error)); 50 - return nullptr; 36 + return false; 51 37 } 52 38 53 39 // find the file ··· 55 41 if (!mz_zip_reader_locate_file_v2(&zipArchive, filename, nullptr, 0, &fileIndex)) { 56 42 Serial.printf("Could not find file %s\n", filename); 57 43 mz_zip_reader_end(&zipArchive); 58 - return nullptr; 44 + return false; 59 45 } 60 46 61 - mz_zip_archive_file_stat fileStat; 62 - if (!mz_zip_reader_file_stat(&zipArchive, fileIndex, &fileStat)) { 47 + if (!mz_zip_reader_file_stat(&zipArchive, fileIndex, fileStat)) { 63 48 Serial.printf("mz_zip_reader_file_stat() failed!\nError %s\n", mz_zip_get_error_string(zipArchive.m_last_error)); 64 49 mz_zip_reader_end(&zipArchive); 65 - return nullptr; 50 + return false; 66 51 } 67 52 mz_zip_reader_end(&zipArchive); 53 + return true; 54 + } 68 55 69 - uint8_t pLocalHeader[30]; 70 - uint64_t fileOffset = fileStat.m_local_header_ofs; 56 + long ZipFile::getDataOffset(const mz_zip_archive_file_stat& fileStat) const { 57 + constexpr auto localHeaderSize = 30; 71 58 72 - // Reopen the file to manual read out delated bytes 73 - FILE* file = fopen(filePath.c_str(), "rb"); 59 + uint8_t pLocalHeader[localHeaderSize]; 60 + const uint64_t fileOffset = fileStat.m_local_header_ofs; 61 + 62 + FILE* file = fopen(filePath.c_str(), "r"); 74 63 fseek(file, fileOffset, SEEK_SET); 64 + const size_t read = fread(pLocalHeader, 1, localHeaderSize, file); 65 + fclose(file); 75 66 76 - const size_t read = fread(pLocalHeader, 1, 30, file); 77 - if (read != 30) { 67 + if (read != localHeaderSize) { 78 68 Serial.println("Something went wrong reading the local header"); 79 - fclose(file); 80 - return nullptr; 69 + return -1; 81 70 } 82 71 83 72 if (pLocalHeader[0] + (pLocalHeader[1] << 8) + (pLocalHeader[2] << 16) + (pLocalHeader[3] << 24) != 84 73 0x04034b50 /* MZ_ZIP_LOCAL_DIR_HEADER_SIG */) { 85 74 Serial.println("Not a valid zip file header"); 86 - fclose(file); 87 - return nullptr; 75 + return -1; 88 76 } 89 77 90 78 const uint16_t filenameLength = pLocalHeader[26] + (pLocalHeader[27] << 8); 91 79 const uint16_t extraOffset = pLocalHeader[28] + (pLocalHeader[29] << 8); 92 - fileOffset += 30 + filenameLength + extraOffset; 80 + return fileOffset + localHeaderSize + filenameLength + extraOffset; 81 + } 82 + 83 + uint8_t* ZipFile::readFileToMemory(const char* filename, size_t* size, const bool trailingNullByte) const { 84 + mz_zip_archive_file_stat fileStat; 85 + if (!loadFileStat(filename, &fileStat)) { 86 + return nullptr; 87 + } 88 + 89 + const long fileOffset = getDataOffset(fileStat); 90 + if (fileOffset < 0) { 91 + return nullptr; 92 + } 93 + 94 + FILE* file = fopen(filePath.c_str(), "rb"); 93 95 fseek(file, fileOffset, SEEK_SET); 94 96 95 97 const auto deflatedDataSize = static_cast<size_t>(fileStat.m_comp_size); ··· 97 99 const auto dataSize = trailingNullByte ? inflatedDataSize + 1 : inflatedDataSize; 98 100 const auto data = static_cast<uint8_t*>(malloc(dataSize)); 99 101 100 - if (!fileStat.m_method) { 102 + if (fileStat.m_method == MZ_NO_COMPRESSION) { 101 103 // no deflation, just read content 102 104 const size_t dataRead = fread(data, 1, inflatedDataSize, file); 103 105 fclose(file); 106 + 104 107 if (dataRead != inflatedDataSize) { 105 108 Serial.println("Failed to read data"); 109 + free(data); 106 110 return nullptr; 107 111 } 108 - } else { 112 + 113 + // Continue out of block with data set 114 + } else if (fileStat.m_method == MZ_DEFLATED) { 109 115 // Read out deflated content from file 110 116 const auto deflatedData = static_cast<uint8_t*>(malloc(deflatedDataSize)); 111 117 if (deflatedData == nullptr) { ··· 116 122 117 123 const size_t dataRead = fread(deflatedData, 1, deflatedDataSize, file); 118 124 fclose(file); 125 + 119 126 if (dataRead != deflatedDataSize) { 120 127 Serial.printf("Failed to read data, expected %d got %d\n", deflatedDataSize, dataRead); 121 128 free(deflatedData); 129 + free(data); 122 130 return nullptr; 123 131 } 124 132 125 - const int result = libzInflateOneShot(deflatedData, deflatedDataSize, data, inflatedDataSize); 133 + bool success = inflateOneShot(deflatedData, deflatedDataSize, data, inflatedDataSize); 126 134 free(deflatedData); 127 - if (result != MZ_OK) { 135 + 136 + if (!success) { 128 137 Serial.println("Failed to inflate file"); 138 + free(data); 129 139 return nullptr; 130 140 } 141 + 142 + // Continue out of block with data set 143 + } else { 144 + Serial.println("Unsupported compression method"); 145 + fclose(file); 146 + return nullptr; 131 147 } 132 148 133 - if (trailingNullByte) { 134 - data[inflatedDataSize] = '\0'; 149 + if (trailingNullByte) data[inflatedDataSize] = '\0'; 150 + if (size) *size = inflatedDataSize; 151 + return data; 152 + } 153 + 154 + bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t chunkSize) const { 155 + mz_zip_archive_file_stat fileStat; 156 + if (!loadFileStat(filename, &fileStat)) { 157 + return false; 158 + } 159 + 160 + const long fileOffset = getDataOffset(fileStat); 161 + if (fileOffset < 0) { 162 + return false; 163 + } 164 + 165 + FILE* file = fopen(filePath.c_str(), "rb"); 166 + fseek(file, fileOffset, SEEK_SET); 167 + 168 + const auto deflatedDataSize = static_cast<size_t>(fileStat.m_comp_size); 169 + const auto inflatedDataSize = static_cast<size_t>(fileStat.m_uncomp_size); 170 + 171 + if (fileStat.m_method == MZ_NO_COMPRESSION) { 172 + // no deflation, just read content 173 + const auto buffer = static_cast<uint8_t*>(malloc(chunkSize)); 174 + if (!buffer) { 175 + Serial.println("Failed to allocate memory for buffer"); 176 + fclose(file); 177 + return false; 178 + } 179 + 180 + size_t remaining = inflatedDataSize; 181 + while (remaining > 0) { 182 + const size_t dataRead = fread(buffer, 1, remaining < chunkSize ? remaining : chunkSize, file); 183 + if (dataRead == 0) { 184 + Serial.println("Could not read more bytes"); 185 + free(buffer); 186 + fclose(file); 187 + return false; 188 + } 189 + 190 + out.write(buffer, dataRead); 191 + remaining -= dataRead; 192 + } 193 + 194 + fclose(file); 195 + free(buffer); 196 + return true; 135 197 } 136 - if (size) { 137 - *size = inflatedDataSize; 198 + 199 + if (fileStat.m_method == MZ_DEFLATED) { 200 + // Setup inflator 201 + const auto inflator = static_cast<tinfl_decompressor*>(malloc(sizeof(tinfl_decompressor))); 202 + if (!inflator) { 203 + Serial.println("Failed to allocate memory for inflator"); 204 + fclose(file); 205 + return false; 206 + } 207 + memset(inflator, 0, sizeof(tinfl_decompressor)); 208 + tinfl_init(inflator); 209 + 210 + // Setup file read buffer 211 + const auto fileReadBuffer = static_cast<uint8_t*>(malloc(chunkSize)); 212 + if (!fileReadBuffer) { 213 + Serial.println("Failed to allocate memory for zip file read buffer"); 214 + free(inflator); 215 + fclose(file); 216 + return false; 217 + } 218 + 219 + const auto outputBuffer = static_cast<uint8_t*>(malloc(TINFL_LZ_DICT_SIZE)); 220 + if (!outputBuffer) { 221 + Serial.println("Failed to allocate memory for dictionary"); 222 + free(inflator); 223 + free(fileReadBuffer); 224 + fclose(file); 225 + return false; 226 + } 227 + memset(outputBuffer, 0, TINFL_LZ_DICT_SIZE); 228 + 229 + size_t fileRemainingBytes = deflatedDataSize; 230 + size_t processedOutputBytes = 0; 231 + size_t fileReadBufferFilledBytes = 0; 232 + size_t fileReadBufferCursor = 0; 233 + size_t outputCursor = 0; // Current offset in the circular dictionary 234 + 235 + while (true) { 236 + // Load more compressed bytes when needed 237 + if (fileReadBufferCursor >= fileReadBufferFilledBytes) { 238 + if (fileRemainingBytes == 0) { 239 + // Should not be hit, but a safe protection 240 + break; // EOF 241 + } 242 + 243 + fileReadBufferFilledBytes = 244 + fread(fileReadBuffer, 1, fileRemainingBytes < chunkSize ? fileRemainingBytes : chunkSize, file); 245 + fileRemainingBytes -= fileReadBufferFilledBytes; 246 + fileReadBufferCursor = 0; 247 + 248 + if (fileReadBufferFilledBytes == 0) { 249 + // Bad read 250 + break; // EOF 251 + } 252 + } 253 + 254 + // Available bytes in fileReadBuffer to process 255 + size_t inBytes = fileReadBufferFilledBytes - fileReadBufferCursor; 256 + // Space remaining in outputBuffer 257 + size_t outBytes = TINFL_LZ_DICT_SIZE - outputCursor; 258 + 259 + const tinfl_status status = tinfl_decompress(inflator, fileReadBuffer + fileReadBufferCursor, &inBytes, 260 + outputBuffer, outputBuffer + outputCursor, &outBytes, 261 + fileRemainingBytes > 0 ? TINFL_FLAG_HAS_MORE_INPUT : 0); 262 + 263 + // Update input position 264 + fileReadBufferCursor += inBytes; 265 + 266 + // Write output chunk 267 + if (outBytes > 0) { 268 + processedOutputBytes += outBytes; 269 + out.write(outputBuffer + outputCursor, outBytes); 270 + // Update output position in buffer (with wraparound) 271 + outputCursor = (outputCursor + outBytes) & (TINFL_LZ_DICT_SIZE - 1); 272 + } 273 + 274 + Serial.printf("Decompressing - %d/%d deflated into %d/%d inflated\n", deflatedDataSize - fileRemainingBytes, 275 + deflatedDataSize, processedOutputBytes, inflatedDataSize); 276 + 277 + if (status < 0) { 278 + Serial.printf("tinfl_decompress() failed with status %d\n", status); 279 + fclose(file); 280 + free(outputBuffer); 281 + free(fileReadBuffer); 282 + free(inflator); 283 + return false; 284 + } 285 + 286 + if (status == TINFL_STATUS_DONE) { 287 + Serial.println("Decompression finished"); 288 + fclose(file); 289 + free(inflator); 290 + free(fileReadBuffer); 291 + free(outputBuffer); 292 + return true; 293 + } 294 + } 295 + 296 + // If we get here, EOF reached without TINFL_STATUS_DONE 297 + Serial.println("Unexpected EOF"); 298 + fclose(file); 299 + free(outputBuffer); 300 + free(fileReadBuffer); 301 + free(inflator); 302 + return false; 138 303 } 139 - return data; 304 + 305 + Serial.println("Unsupported compression method"); 306 + return false; 140 307 }
+8 -1
lib/ZipFile/ZipFile.h
··· 1 1 #pragma once 2 + #include <Print.h> 3 + 4 + #include <functional> 2 5 #include <string> 6 + 7 + #include "miniz.h" 3 8 4 9 class ZipFile { 5 10 std::string filePath; 11 + bool loadFileStat(const char* filename, mz_zip_archive_file_stat* fileStat) const; 12 + long getDataOffset(const mz_zip_archive_file_stat& fileStat) const; 6 13 7 14 public: 8 15 explicit ZipFile(std::string filePath) : filePath(std::move(filePath)) {} 9 16 ~ZipFile() = default; 10 - char* readTextFileToMemory(const char* filename, size_t* size = nullptr) const; 11 17 uint8_t* readFileToMemory(const char* filename, size_t* size = nullptr, bool trailingNullByte = false) const; 18 + bool readFileToStream(const char* filename, Print& out, size_t chunkSize) const; 12 19 };
+213 -385
lib/miniz/miniz.c
··· 159 159 160 160 #ifndef MINIZ_NO_ZLIB_APIS 161 161 162 - #ifndef MINIZ_NO_DEFLATE_APIS 163 - 164 162 int mz_deflateInit(mz_streamp pStream, int level) { 165 163 return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY); 166 164 } ··· 275 273 memset(&stream, 0, sizeof(stream)); 276 274 277 275 /* In case mz_ulong is 64-bits (argh I hate longs). */ 278 - if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; 276 + if ((source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; 279 277 280 278 stream.next_in = pSource; 281 279 stream.avail_in = (mz_uint32)source_len; ··· 300 298 } 301 299 302 300 mz_ulong mz_compressBound(mz_ulong source_len) { return mz_deflateBound(NULL, source_len); } 303 - 304 - #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ 305 - 306 - #ifndef MINIZ_NO_INFLATE_APIS 307 301 308 302 typedef struct { 309 303 tinfl_decompressor m_decomp; ··· 486 480 memset(&stream, 0, sizeof(stream)); 487 481 488 482 /* In case mz_ulong is 64-bits (argh I hate longs). */ 489 - if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; 483 + if ((*pSource_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; 490 484 491 485 stream.next_in = pSource; 492 486 stream.avail_in = (mz_uint32)*pSource_len; ··· 511 505 return mz_uncompress2(pDest, pDest_len, pSource, &source_len); 512 506 } 513 507 514 - #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ 515 - 516 508 const char* mz_error(int err) { 517 509 static struct { 518 510 int m_err; ··· 590 582 * THE SOFTWARE. 591 583 * 592 584 **************************************************************************/ 593 - 594 - #ifndef MINIZ_NO_DEFLATE_APIS 595 585 596 586 #ifdef __cplusplus 597 587 extern "C" { ··· 680 670 static tdefl_sym_freq* tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq* pSyms0, tdefl_sym_freq* pSyms1) { 681 671 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2]; 682 672 tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1; 683 - MZ_CLEAR_ARR(hist); 673 + MZ_CLEAR_OBJ(hist); 684 674 for (i = 0; i < num_syms; i++) { 685 675 mz_uint freq = pSyms0[i].m_key; 686 676 hist[freq & 0xFF]++; ··· 774 764 int static_table) { 775 765 int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; 776 766 mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; 777 - MZ_CLEAR_ARR(num_codes); 767 + MZ_CLEAR_OBJ(num_codes); 778 768 if (static_table) { 779 769 for (i = 0; i < table_len; i++) num_codes[d->m_huff_code_sizes[table_num][i]]++; 780 770 } else { ··· 794 784 795 785 tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit); 796 786 797 - MZ_CLEAR_ARR(d->m_huff_code_sizes[table_num]); 798 - MZ_CLEAR_ARR(d->m_huff_codes[table_num]); 787 + MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]); 788 + MZ_CLEAR_OBJ(d->m_huff_codes[table_num]); 799 789 for (i = 1, j = num_used_syms; i <= code_size_limit; i++) 800 790 for (l = num_codes[i]; l > 0; l--) d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i); 801 791 } ··· 861 851 } \ 862 852 } 863 853 864 - static const mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 865 - 11, 4, 12, 3, 13, 2, 14, 1, 15}; 854 + static mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 855 + 11, 4, 12, 3, 13, 2, 14, 1, 15}; 866 856 867 857 static void tdefl_start_dynamic_block(tdefl_compressor* d) { 868 858 int num_lit_codes, num_dist_codes, num_bit_lengths; ··· 976 966 977 967 if (flags & 1) { 978 968 mz_uint s0, s1, n0, n1, sym, num_extra_bits; 979 - mz_uint match_len = pLZ_codes[0]; 980 - mz_uint match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); 969 + mz_uint match_len = pLZ_codes[0], match_dist = *(const mz_uint16*)(pLZ_codes + 1); 981 970 pLZ_codes += 3; 982 971 983 972 MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); ··· 1018 1007 1019 1008 if (pOutput_buf >= d->m_pOutput_buf_end) return MZ_FALSE; 1020 1009 1021 - memcpy(pOutput_buf, &bit_buffer, sizeof(mz_uint64)); 1010 + *(mz_uint64*)pOutput_buf = bit_buffer; 1022 1011 pOutput_buf += (bits_in >> 3); 1023 1012 bit_buffer >>= (bits_in & ~7); 1024 1013 bits_in &= 7; ··· 1090 1079 return tdefl_compress_lz_codes(d); 1091 1080 } 1092 1081 1093 - static const mz_uint s_tdefl_num_probes[11] = {0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500}; 1094 - 1095 1082 static int tdefl_flush_block(tdefl_compressor* d, int flush) { 1096 1083 mz_uint saved_bit_buf, saved_bits_in; 1097 1084 mz_uint8* pSaved_output_buf; ··· 1114 1101 d->m_pLZ_code_buf -= (d->m_num_flags_left == 8); 1115 1102 1116 1103 if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) { 1117 - const mz_uint8 cmf = 0x78; 1118 - mz_uint8 flg, flevel = 3; 1119 - mz_uint header, i, mz_un = sizeof(s_tdefl_num_probes) / sizeof(mz_uint); 1120 - 1121 - /* Determine compression level by reversing the process in tdefl_create_comp_flags_from_zip_params() */ 1122 - for (i = 0; i < mz_un; i++) 1123 - if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF)) break; 1124 - 1125 - if (i < 2) 1126 - flevel = 0; 1127 - else if (i < 6) 1128 - flevel = 1; 1129 - else if (i == 6) 1130 - flevel = 2; 1131 - 1132 - header = cmf << 8 | (flevel << 6); 1133 - header += 31 - (header % 31); 1134 - flg = header & 0xFF; 1135 - 1136 - TDEFL_PUT_BITS(cmf, 8); 1137 - TDEFL_PUT_BITS(flg, 8); 1104 + TDEFL_PUT_BITS(0x78, 8); 1105 + TDEFL_PUT_BITS(0x01, 8); 1138 1106 } 1139 1107 1140 1108 TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); ··· 1550 1518 mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ 1551 1519 d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK]; 1552 1520 mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size); 1553 - const mz_uint8* pSrc_end = pSrc ? pSrc + num_bytes_to_process : NULL; 1521 + const mz_uint8* pSrc_end = pSrc + num_bytes_to_process; 1554 1522 src_buf_left -= num_bytes_to_process; 1555 1523 d->m_lookahead_size += num_bytes_to_process; 1556 1524 while (pSrc != pSrc_end) { ··· 1720 1688 if (tdefl_flush_block(d, flush) < 0) return d->m_prev_return_status; 1721 1689 d->m_finished = (flush == TDEFL_FINISH); 1722 1690 if (flush == TDEFL_FULL_FLUSH) { 1723 - MZ_CLEAR_ARR(d->m_hash); 1724 - MZ_CLEAR_ARR(d->m_next); 1691 + MZ_CLEAR_OBJ(d->m_hash); 1692 + MZ_CLEAR_OBJ(d->m_next); 1725 1693 d->m_dict_size = 0; 1726 1694 } 1727 1695 } ··· 1741 1709 d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3; 1742 1710 d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0; 1743 1711 d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3; 1744 - if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_ARR(d->m_hash); 1712 + if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_OBJ(d->m_hash); 1745 1713 d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = 1746 1714 d->m_bits_in = 0; 1747 1715 d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = ··· 1763 1731 d->m_pSrc = NULL; 1764 1732 d->m_src_buf_left = 0; 1765 1733 d->m_out_buf_ofs = 0; 1766 - if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_ARR(d->m_dict); 1734 + if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_OBJ(d->m_dict); 1767 1735 memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); 1768 1736 memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); 1769 1737 return TDEFL_STATUS_OKAY; ··· 1836 1804 return out_buf.m_size; 1837 1805 } 1838 1806 1807 + static const mz_uint s_tdefl_num_probes[11] = {0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500}; 1808 + 1839 1809 /* level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine 1840 1810 * if throughput to fall off a cliff on some files). */ 1841 1811 mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy) { ··· 1941 1911 /* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */ 1942 1912 /* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */ 1943 1913 /* structure size and allocation mechanism. */ 1944 - tdefl_compressor* tdefl_compressor_alloc(void) { return (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor)); } 1914 + tdefl_compressor* tdefl_compressor_alloc() { return (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor)); } 1945 1915 1946 1916 void tdefl_compressor_free(tdefl_compressor* pComp) { MZ_FREE(pComp); } 1947 1917 #endif ··· 1953 1923 #ifdef __cplusplus 1954 1924 } 1955 1925 #endif 1956 - 1957 - #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ 1958 - /************************************************************************** 1959 - * 1960 - * Copyright 2013-2014 RAD Game Tools and Valve Software 1961 - * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC 1962 - * All Rights Reserved. 1963 - * 1964 - * Permission is hereby granted, free of charge, to any person obtaining a copy 1965 - * of this software and associated documentation files (the "Software"), to deal 1966 - * in the Software without restriction, including without limitation the rights 1967 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1968 - * copies of the Software, and to permit persons to whom the Software is 1969 - * furnished to do so, subject to the following conditions: 1970 - * 1971 - * The above copyright notice and this permission notice shall be included in 1972 - * all copies or substantial portions of the Software. 1973 - * 1974 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1975 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1976 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1977 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1978 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1979 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 1980 - * THE SOFTWARE. 1981 - * 1982 - **************************************************************************/ 1983 - 1984 - #ifndef MINIZ_NO_INFLATE_APIS 1926 + /************************************************************************** 1927 + * 1928 + * Copyright 2013-2014 RAD Game Tools and Valve Software 1929 + * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC 1930 + * All Rights Reserved. 1931 + * 1932 + * Permission is hereby granted, free of charge, to any person obtaining a copy 1933 + * of this software and associated documentation files (the "Software"), to deal 1934 + * in the Software without restriction, including without limitation the rights 1935 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1936 + * copies of the Software, and to permit persons to whom the Software is 1937 + * furnished to do so, subject to the following conditions: 1938 + * 1939 + * The above copyright notice and this permission notice shall be included in 1940 + * all copies or substantial portions of the Software. 1941 + * 1942 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1943 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1944 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1945 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1946 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1947 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 1948 + * THE SOFTWARE. 1949 + * 1950 + **************************************************************************/ 1985 1951 1986 1952 #ifdef __cplusplus 1987 1953 extern "C" { ··· 2057 2023 /* Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, 2058 2024 * and tries again until it succeeds or until the */ 2059 2025 /* bit buffer contains >=15 bits (deflate's max. Huffman code size). */ 2060 - #define TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree) \ 2061 - do { \ 2062 - temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \ 2063 - if (temp >= 0) { \ 2064 - code_len = temp >> 9; \ 2065 - if ((code_len) && (num_bits >= code_len)) break; \ 2066 - } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \ 2067 - code_len = TINFL_FAST_LOOKUP_BITS; \ 2068 - do { \ 2069 - temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \ 2070 - } while ((temp < 0) && (num_bits >= (code_len + 1))); \ 2071 - if (temp >= 0) break; \ 2072 - } \ 2073 - TINFL_GET_BYTE(state_index, c); \ 2074 - bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ 2075 - num_bits += 8; \ 2026 + #define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \ 2027 + do { \ 2028 + temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \ 2029 + if (temp >= 0) { \ 2030 + code_len = temp >> 9; \ 2031 + if ((code_len) && (num_bits >= code_len)) break; \ 2032 + } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \ 2033 + code_len = TINFL_FAST_LOOKUP_BITS; \ 2034 + do { \ 2035 + temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ 2036 + } while ((temp < 0) && (num_bits >= (code_len + 1))); \ 2037 + if (temp >= 0) break; \ 2038 + } \ 2039 + TINFL_GET_BYTE(state_index, c); \ 2040 + bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ 2041 + num_bits += 8; \ 2076 2042 } while (num_bits < 15); 2077 2043 2078 2044 /* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because ··· 2086 2052 * the case where the user passes in 1+zillion bytes */ 2087 2053 /* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much 2088 2054 * trickier. */ 2089 - #define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree) \ 2055 + #define TINFL_HUFF_DECODE(state_index, sym, pHuff) \ 2090 2056 do { \ 2091 2057 int temp; \ 2092 2058 mz_uint code_len, c; \ 2093 2059 if (num_bits < 15) { \ 2094 2060 if ((pIn_buf_end - pIn_buf_cur) < 2) { \ 2095 - TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree); \ 2061 + TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \ 2096 2062 } else { \ 2097 2063 bit_buf |= \ 2098 2064 (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \ ··· 2100 2066 num_bits += 16; \ 2101 2067 } \ 2102 2068 } \ 2103 - if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \ 2069 + if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \ 2104 2070 code_len = temp >> 9, temp &= 511; \ 2105 2071 else { \ 2106 2072 code_len = TINFL_FAST_LOOKUP_BITS; \ 2107 2073 do { \ 2108 - temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \ 2074 + temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ 2109 2075 } while (temp < 0); \ 2110 2076 } \ 2111 2077 sym = temp; \ ··· 2114 2080 } \ 2115 2081 MZ_MACRO_END 2116 2082 2117 - static void tinfl_clear_tree(tinfl_decompressor* r) { 2118 - if (r->m_type == 0) 2119 - MZ_CLEAR_ARR(r->m_tree_0); 2120 - else if (r->m_type == 1) 2121 - MZ_CLEAR_ARR(r->m_tree_1); 2122 - else 2123 - MZ_CLEAR_ARR(r->m_tree_2); 2124 - } 2125 - 2126 2083 tinfl_status tinfl_decompress(tinfl_decompressor* r, const mz_uint8* pIn_buf_next, size_t* pIn_buf_size, 2127 2084 mz_uint8* pOut_buf_start, mz_uint8* pOut_buf_next, size_t* pOut_buf_size, 2128 2085 const mz_uint32 decomp_flags) { 2129 - static const mz_uint16 s_length_base[31] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 2130 - 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 2131 - static const mz_uint8 s_length_extra[31] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2132 - 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0}; 2133 - static const mz_uint16 s_dist_base[32] = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 2134 - 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2135 - 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0}; 2136 - static const mz_uint8 s_dist_extra[32] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 2137 - 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; 2086 + static const int s_length_base[31] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 2087 + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 2088 + static const int s_length_extra[31] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2089 + 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0}; 2090 + static const int s_dist_base[32] = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 2091 + 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2092 + 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0}; 2093 + static const int s_dist_extra[32] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 2094 + 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; 2138 2095 static const mz_uint8 s_length_dezigzag[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 2139 - static const mz_uint16 s_min_table_sizes[3] = {257, 1, 4}; 2140 - 2141 - mz_int16* pTrees[3]; 2142 - mz_uint8* pCode_sizes[3]; 2096 + static const int s_min_table_sizes[3] = {257, 1, 4}; 2143 2097 2144 2098 tinfl_status status = TINFL_STATUS_FAILED; 2145 2099 mz_uint32 num_bits, dist, counter, num_extra; 2146 2100 tinfl_bit_buf_t bit_buf; 2147 2101 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size; 2148 - mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL; 2102 + mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; 2149 2103 size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) 2150 2104 ? (size_t)-1 2151 2105 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, ··· 2158 2112 return TINFL_STATUS_BAD_PARAM; 2159 2113 } 2160 2114 2161 - pTrees[0] = r->m_tree_0; 2162 - pTrees[1] = r->m_tree_1; 2163 - pTrees[2] = r->m_tree_2; 2164 - pCode_sizes[0] = r->m_code_size_0; 2165 - pCode_sizes[1] = r->m_code_size_1; 2166 - pCode_sizes[2] = r->m_code_size_2; 2167 - 2168 2115 num_bits = r->m_num_bits; 2169 2116 bit_buf = r->m_bit_buf; 2170 2117 dist = r->m_dist; ··· 2181 2128 counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); 2182 2129 if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 2183 2130 counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || 2184 - ((out_buf_size_mask + 1) < (size_t)((size_t)1 << (8U + (r->m_zhdr0 >> 4))))); 2131 + ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4))))); 2185 2132 if (counter) { 2186 2133 TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); 2187 2134 } ··· 2229 2176 TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED); 2230 2177 } else { 2231 2178 if (r->m_type == 1) { 2232 - mz_uint8* p = r->m_code_size_0; 2179 + mz_uint8* p = r->m_tables[0].m_code_size; 2233 2180 mz_uint i; 2234 2181 r->m_table_sizes[0] = 288; 2235 2182 r->m_table_sizes[1] = 32; 2236 - TINFL_MEMSET(r->m_code_size_1, 5, 32); 2183 + TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32); 2237 2184 for (i = 0; i <= 143; ++i) *p++ = 8; 2238 2185 for (; i <= 255; ++i) *p++ = 9; 2239 2186 for (; i <= 279; ++i) *p++ = 7; ··· 2243 2190 TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); 2244 2191 r->m_table_sizes[counter] += s_min_table_sizes[counter]; 2245 2192 } 2246 - MZ_CLEAR_ARR(r->m_code_size_2); 2193 + MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); 2247 2194 for (counter = 0; counter < r->m_table_sizes[2]; counter++) { 2248 2195 mz_uint s; 2249 2196 TINFL_GET_BITS(14, s, 3); 2250 - r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s; 2197 + r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; 2251 2198 } 2252 2199 r->m_table_sizes[2] = 19; 2253 2200 } 2254 2201 for (; (int)r->m_type >= 0; r->m_type--) { 2255 2202 int tree_next, tree_cur; 2256 - mz_int16* pLookUp; 2257 - mz_int16* pTree; 2258 - mz_uint8* pCode_size; 2203 + tinfl_huff_table* pTable; 2259 2204 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; 2260 - pLookUp = r->m_look_up[r->m_type]; 2261 - pTree = pTrees[r->m_type]; 2262 - pCode_size = pCode_sizes[r->m_type]; 2263 - MZ_CLEAR_ARR(total_syms); 2264 - TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0])); 2265 - tinfl_clear_tree(r); 2266 - for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pCode_size[i]]++; 2205 + pTable = &r->m_tables[r->m_type]; 2206 + MZ_CLEAR_OBJ(total_syms); 2207 + MZ_CLEAR_OBJ(pTable->m_look_up); 2208 + MZ_CLEAR_OBJ(pTable->m_tree); 2209 + for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pTable->m_code_size[i]]++; 2267 2210 used_syms = 0, total = 0; 2268 2211 next_code[0] = next_code[1] = 0; 2269 2212 for (i = 1; i <= 15; ++i) { ··· 2274 2217 TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED); 2275 2218 } 2276 2219 for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) { 2277 - mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index]; 2220 + mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; 2278 2221 if (!code_size) continue; 2279 2222 cur_code = next_code[code_size]++; 2280 2223 for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1); 2281 2224 if (code_size <= TINFL_FAST_LOOKUP_BITS) { 2282 2225 mz_int16 k = (mz_int16)((code_size << 9) | sym_index); 2283 2226 while (rev_code < TINFL_FAST_LOOKUP_SIZE) { 2284 - pLookUp[rev_code] = k; 2227 + pTable->m_look_up[rev_code] = k; 2285 2228 rev_code += (1 << code_size); 2286 2229 } 2287 2230 continue; 2288 2231 } 2289 - if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) { 2290 - pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; 2232 + if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) { 2233 + pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; 2291 2234 tree_cur = tree_next; 2292 2235 tree_next -= 2; 2293 2236 } 2294 2237 rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1); 2295 2238 for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) { 2296 2239 tree_cur -= ((rev_code >>= 1) & 1); 2297 - if (!pTree[-tree_cur - 1]) { 2298 - pTree[-tree_cur - 1] = (mz_int16)tree_next; 2240 + if (!pTable->m_tree[-tree_cur - 1]) { 2241 + pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; 2299 2242 tree_cur = tree_next; 2300 2243 tree_next -= 2; 2301 2244 } else 2302 - tree_cur = pTree[-tree_cur - 1]; 2245 + tree_cur = pTable->m_tree[-tree_cur - 1]; 2303 2246 } 2304 2247 tree_cur -= ((rev_code >>= 1) & 1); 2305 - pTree[-tree_cur - 1] = (mz_int16)sym_index; 2248 + pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index; 2306 2249 } 2307 2250 if (r->m_type == 2) { 2308 2251 for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) { 2309 2252 mz_uint s; 2310 - TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 2253 + TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); 2311 2254 if (dist < 16) { 2312 2255 r->m_len_codes[counter++] = (mz_uint8)dist; 2313 2256 continue; ··· 2324 2267 if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) { 2325 2268 TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED); 2326 2269 } 2327 - TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]); 2328 - TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]); 2270 + TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); 2271 + TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]); 2329 2272 } 2330 2273 } 2331 2274 for (;;) { 2332 2275 mz_uint8* pSrc; 2333 2276 for (;;) { 2334 2277 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) { 2335 - TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 2278 + TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]); 2336 2279 if (counter >= 256) break; 2337 2280 while (pOut_buf_cur >= pOut_buf_end) { 2338 2281 TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); ··· 2354 2297 num_bits += 16; 2355 2298 } 2356 2299 #endif 2357 - if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 2300 + if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 2358 2301 code_len = sym2 >> 9; 2359 2302 else { 2360 2303 code_len = TINFL_FAST_LOOKUP_BITS; 2361 2304 do { 2362 - sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)]; 2305 + sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; 2363 2306 } while (sym2 < 0); 2364 2307 } 2365 2308 counter = sym2; ··· 2374 2317 num_bits += 16; 2375 2318 } 2376 2319 #endif 2377 - if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 2320 + if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 2378 2321 code_len = sym2 >> 9; 2379 2322 else { 2380 2323 code_len = TINFL_FAST_LOOKUP_BITS; 2381 2324 do { 2382 - sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)]; 2325 + sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; 2383 2326 } while (sym2 < 0); 2384 2327 } 2385 2328 bit_buf >>= code_len; ··· 2405 2348 counter += extra_bits; 2406 2349 } 2407 2350 2408 - TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 2351 + TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]); 2409 2352 num_extra = s_dist_extra[dist]; 2410 2353 dist = s_dist_base[dist]; 2411 2354 if (num_extra) { ··· 2479 2422 --pIn_buf_cur; 2480 2423 num_bits -= 8; 2481 2424 } 2482 - bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits); 2425 + bit_buf &= (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); 2483 2426 MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end of non-deflate/zlib streams with 2484 2427 following data (such as gzip streams). */ 2485 2428 ··· 2510 2453 } 2511 2454 } 2512 2455 r->m_num_bits = num_bits; 2513 - r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits); 2456 + r->m_bit_buf = bit_buf & (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); 2514 2457 r->m_dist = dist; 2515 2458 r->m_counter = counter; 2516 2459 r->m_num_extra = num_extra; ··· 2599 2542 mz_uint8* pDict = (mz_uint8*)MZ_MALLOC(TINFL_LZ_DICT_SIZE); 2600 2543 size_t in_buf_ofs = 0, dict_ofs = 0; 2601 2544 if (!pDict) return TINFL_STATUS_FAILED; 2602 - memset(pDict, 0, TINFL_LZ_DICT_SIZE); 2603 2545 tinfl_init(&decomp); 2604 2546 for (;;) { 2605 2547 size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs; ··· 2620 2562 } 2621 2563 2622 2564 #ifndef MINIZ_NO_MALLOC 2623 - tinfl_decompressor* tinfl_decompressor_alloc(void) { 2565 + tinfl_decompressor* tinfl_decompressor_alloc() { 2624 2566 tinfl_decompressor* pDecomp = (tinfl_decompressor*)MZ_MALLOC(sizeof(tinfl_decompressor)); 2625 2567 if (pDecomp) tinfl_init(pDecomp); 2626 2568 return pDecomp; ··· 2632 2574 #ifdef __cplusplus 2633 2575 } 2634 2576 #endif 2635 - 2636 - #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ 2637 - /************************************************************************** 2638 - * 2639 - * Copyright 2013-2014 RAD Game Tools and Valve Software 2640 - * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC 2641 - * Copyright 2016 Martin Raiber 2642 - * All Rights Reserved. 2643 - * 2644 - * Permission is hereby granted, free of charge, to any person obtaining a copy 2645 - * of this software and associated documentation files (the "Software"), to deal 2646 - * in the Software without restriction, including without limitation the rights 2647 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 2648 - * copies of the Software, and to permit persons to whom the Software is 2649 - * furnished to do so, subject to the following conditions: 2650 - * 2651 - * The above copyright notice and this permission notice shall be included in 2652 - * all copies or substantial portions of the Software. 2653 - * 2654 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2655 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2656 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2657 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2658 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2659 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2660 - * THE SOFTWARE. 2661 - * 2662 - **************************************************************************/ 2577 + /************************************************************************** 2578 + * 2579 + * Copyright 2013-2014 RAD Game Tools and Valve Software 2580 + * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC 2581 + * Copyright 2016 Martin Raiber 2582 + * All Rights Reserved. 2583 + * 2584 + * Permission is hereby granted, free of charge, to any person obtaining a copy 2585 + * of this software and associated documentation files (the "Software"), to deal 2586 + * in the Software without restriction, including without limitation the rights 2587 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 2588 + * copies of the Software, and to permit persons to whom the Software is 2589 + * furnished to do so, subject to the following conditions: 2590 + * 2591 + * The above copyright notice and this permission notice shall be included in 2592 + * all copies or substantial portions of the Software. 2593 + * 2594 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2595 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2596 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2597 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2598 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2599 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2600 + * THE SOFTWARE. 2601 + * 2602 + **************************************************************************/ 2663 2603 2664 2604 #ifndef MINIZ_NO_ARCHIVE_APIS 2665 2605 ··· 2674 2614 #else 2675 2615 #include <sys/stat.h> 2676 2616 2677 - #if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__) 2678 - 2679 - #ifndef WIN32_LEAN_AND_MEAN 2680 - #define WIN32_LEAN_AND_MEAN 2681 - #endif 2682 - #ifndef __cplusplus 2683 - #define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 0 2684 - #endif 2685 - #ifndef NOMINMAX 2686 - #define NOMINMAX 2687 - #endif 2688 - #include <windows.h> 2689 - 2690 - static WCHAR* mz_utf8z_to_widechar(const char* str) { 2691 - int reqChars = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); 2692 - WCHAR* wStr = (WCHAR*)malloc(reqChars * sizeof(WCHAR)); 2693 - MultiByteToWideChar(CP_UTF8, 0, str, -1, wStr, reqChars); 2694 - return wStr; 2695 - } 2696 - 2617 + #if defined(_MSC_VER) || defined(__MINGW64__) 2697 2618 static FILE* mz_fopen(const char* pFilename, const char* pMode) { 2698 - WCHAR* wFilename = mz_utf8z_to_widechar(pFilename); 2699 - WCHAR* wMode = mz_utf8z_to_widechar(pMode); 2700 2619 FILE* pFile = NULL; 2701 - errno_t err = _wfopen_s(&pFile, wFilename, wMode); 2702 - free(wFilename); 2703 - free(wMode); 2704 - return err ? NULL : pFile; 2620 + fopen_s(&pFile, pFilename, pMode); 2621 + return pFile; 2705 2622 } 2706 - 2707 2623 static FILE* mz_freopen(const char* pPath, const char* pMode, FILE* pStream) { 2708 - WCHAR* wPath = mz_utf8z_to_widechar(pPath); 2709 - WCHAR* wMode = mz_utf8z_to_widechar(pMode); 2710 2624 FILE* pFile = NULL; 2711 - errno_t err = _wfreopen_s(&pFile, wPath, wMode, pStream); 2712 - free(wPath); 2713 - free(wMode); 2714 - return err ? NULL : pFile; 2715 - } 2716 - 2717 - #if defined(__MINGW32__) 2718 - static int mz_stat(const char* path, struct _stat* buffer) { 2719 - WCHAR* wPath = mz_utf8z_to_widechar(path); 2720 - int res = _wstat(wPath, buffer); 2721 - free(wPath); 2722 - return res; 2723 - } 2724 - #else 2725 - static int mz_stat64(const char* path, struct __stat64* buffer) { 2726 - WCHAR* wPath = mz_utf8z_to_widechar(path); 2727 - int res = _wstat64(wPath, buffer); 2728 - free(wPath); 2729 - return res; 2625 + if (freopen_s(&pFile, pPath, pMode, pStream)) return NULL; 2626 + return pFile; 2730 2627 } 2731 - #endif 2732 - 2733 2628 #ifndef MINIZ_NO_TIME 2734 2629 #include <sys/utime.h> 2735 2630 #endif ··· 2739 2634 #define MZ_FWRITE fwrite 2740 2635 #define MZ_FTELL64 _ftelli64 2741 2636 #define MZ_FSEEK64 _fseeki64 2742 - #if defined(__MINGW32__) 2743 - #define MZ_FILE_STAT_STRUCT _stat 2744 - #define MZ_FILE_STAT mz_stat 2745 - #else 2746 2637 #define MZ_FILE_STAT_STRUCT _stat64 2747 - #define MZ_FILE_STAT mz_stat64 2748 - #endif 2638 + #define MZ_FILE_STAT _stat64 2749 2639 #define MZ_FFLUSH fflush 2750 2640 #define MZ_FREOPEN mz_freopen 2751 2641 #define MZ_DELETE_FILE remove 2752 - 2753 - #elif defined(__WATCOMC__) 2642 + #elif defined(__MINGW32__) 2754 2643 #ifndef MINIZ_NO_TIME 2755 2644 #include <sys/utime.h> 2756 2645 #endif ··· 2758 2647 #define MZ_FCLOSE fclose 2759 2648 #define MZ_FREAD fread 2760 2649 #define MZ_FWRITE fwrite 2761 - #define MZ_FTELL64 _ftelli64 2762 - #define MZ_FSEEK64 _fseeki64 2763 - #define MZ_FILE_STAT_STRUCT stat 2764 - #define MZ_FILE_STAT stat 2650 + #define MZ_FTELL64 ftello64 2651 + #define MZ_FSEEK64 fseeko64 2652 + #define MZ_FILE_STAT_STRUCT _stat 2653 + #define MZ_FILE_STAT _stat 2765 2654 #define MZ_FFLUSH fflush 2766 2655 #define MZ_FREOPEN(f, m, s) freopen(f, m, s) 2767 2656 #define MZ_DELETE_FILE remove 2768 - 2769 2657 #elif defined(__TINYC__) 2770 2658 #ifndef MINIZ_NO_TIME 2771 2659 #include <sys/utime.h> ··· 2781 2669 #define MZ_FFLUSH fflush 2782 2670 #define MZ_FREOPEN(f, m, s) freopen(f, m, s) 2783 2671 #define MZ_DELETE_FILE remove 2784 - 2785 2672 #elif defined(__USE_LARGEFILE64) /* gcc, clang */ 2786 2673 #ifndef MINIZ_NO_TIME 2787 2674 #include <utime.h> ··· 2797 2684 #define MZ_FFLUSH fflush 2798 2685 #define MZ_FREOPEN(p, m, s) freopen64(p, m, s) 2799 2686 #define MZ_DELETE_FILE remove 2800 - 2801 - #elif defined(__APPLE__) || defined(__FreeBSD__) || (defined(__linux__) && defined(__x86_64__)) 2687 + #elif defined(__APPLE__) 2802 2688 #ifndef MINIZ_NO_TIME 2803 2689 #include <utime.h> 2804 2690 #endif ··· 2942 2828 mz_zip_array m_sorted_central_dir_offsets; 2943 2829 2944 2830 /* The flags passed in when the archive is initially opened. */ 2945 - mz_uint32 m_init_flags; 2831 + uint32_t m_init_flags; 2946 2832 2947 2833 /* MZ_TRUE if the archive has a zip64 end of central directory headers, etc. */ 2948 2834 mz_bool m_zip64; ··· 3230 3116 } 3231 3117 3232 3118 /* Give up if we've searched the entire file, or we've gone back "too far" (~64kb) */ 3233 - if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= ((mz_uint64)(MZ_UINT16_MAX) + record_size))) 3234 - return MZ_FALSE; 3119 + if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= (MZ_UINT16_MAX + record_size))) return MZ_FALSE; 3235 3120 3236 3121 cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0); 3237 3122 } ··· 3240 3125 return MZ_TRUE; 3241 3126 } 3242 3127 3243 - static mz_bool mz_zip_reader_eocd64_valid(mz_zip_archive* pZip, uint64_t offset, uint8_t* buf) { 3244 - if (pZip->m_pRead(pZip->m_pIO_opaque, offset, buf, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == 3245 - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) { 3246 - if (MZ_READ_LE32(buf + MZ_ZIP64_ECDH_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG) { 3247 - return MZ_TRUE; 3248 - } 3249 - } 3250 - 3251 - return MZ_FALSE; 3252 - } 3253 - 3254 3128 static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive* pZip, mz_uint flags) { 3255 3129 mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0; 3256 - mz_uint64 cdir_ofs = 0, eocd_ofs = 0, archive_ofs = 0; 3130 + mz_uint64 cdir_ofs = 0; 3257 3131 mz_int64 cur_file_ofs = 0; 3258 3132 const mz_uint8* p; 3259 3133 ··· 3279 3153 MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs)) 3280 3154 return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR); 3281 3155 3282 - eocd_ofs = cur_file_ofs; 3283 3156 /* Read and verify the end of central directory record. */ 3284 3157 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != 3285 3158 MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) ··· 3292 3165 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, pZip64_locator, 3293 3166 MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) { 3294 3167 if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG) { 3295 - pZip->m_pState->m_zip64 = MZ_TRUE; 3168 + zip64_end_of_central_dir_ofs = MZ_READ_LE64(pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS); 3169 + if (zip64_end_of_central_dir_ofs > (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) 3170 + return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3171 + 3172 + if (pZip->m_pRead(pZip->m_pIO_opaque, zip64_end_of_central_dir_ofs, pZip64_end_of_central_dir, 3173 + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) { 3174 + if (MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIG_OFS) == 3175 + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG) { 3176 + pZip->m_pState->m_zip64 = MZ_TRUE; 3177 + } 3178 + } 3296 3179 } 3297 3180 } 3298 3181 } 3299 3182 3300 - if (pZip->m_pState->m_zip64) { 3301 - /* Try locating the EOCD64 right before the EOCD64 locator. This works even 3302 - * when the effective start of the zip header is not yet known. */ 3303 - if (cur_file_ofs < MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) 3304 - return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3305 - 3306 - zip64_end_of_central_dir_ofs = 3307 - cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE; 3308 - 3309 - if (!mz_zip_reader_eocd64_valid(pZip, zip64_end_of_central_dir_ofs, pZip64_end_of_central_dir)) { 3310 - /* That failed, try reading where the locator tells us to. */ 3311 - zip64_end_of_central_dir_ofs = MZ_READ_LE64(pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS); 3312 - 3313 - if (zip64_end_of_central_dir_ofs > (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) 3314 - return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3315 - 3316 - if (!mz_zip_reader_eocd64_valid(pZip, zip64_end_of_central_dir_ofs, pZip64_end_of_central_dir)) 3317 - return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3318 - } 3319 - } 3320 - 3321 3183 pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS); 3322 3184 cdir_entries_on_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS); 3323 3185 num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS); ··· 3365 3227 if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1))) 3366 3228 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); 3367 3229 3368 - if (cdir_size < (mz_uint64)pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) 3230 + if (cdir_size < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) 3369 3231 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3370 3232 3371 3233 if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size) 3372 3234 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3373 3235 3374 - if (eocd_ofs < cdir_ofs + cdir_size) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3375 - 3376 - /* The end of central dir follows the central dir, unless the zip file has 3377 - * some trailing data (e.g. it is appended to an executable file). */ 3378 - archive_ofs = eocd_ofs - (cdir_ofs + cdir_size); 3379 - if (pZip->m_pState->m_zip64) { 3380 - if (archive_ofs < MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) 3381 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3382 - 3383 - archive_ofs -= MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE; 3384 - } 3385 - 3386 - /* Update the archive start position, but only if not specified. */ 3387 - if ((pZip->m_zip_type == MZ_ZIP_TYPE_FILE || pZip->m_zip_type == MZ_ZIP_TYPE_CFILE || 3388 - pZip->m_zip_type == MZ_ZIP_TYPE_USER) && 3389 - pZip->m_pState->m_file_archive_start_ofs == 0) { 3390 - pZip->m_pState->m_file_archive_start_ofs = archive_ofs; 3391 - pZip->m_archive_size -= archive_ofs; 3392 - } 3393 - 3394 3236 pZip->m_central_directory_file_ofs = cdir_ofs; 3395 3237 3396 3238 if (pZip->m_total_files) { ··· 3522 3364 } 3523 3365 3524 3366 void mz_zip_zero_struct(mz_zip_archive* pZip) { 3525 - if (pZip) MZ_CLEAR_PTR(pZip); 3367 + if (pZip) MZ_CLEAR_OBJ(*pZip); 3526 3368 } 3527 3369 3528 3370 static mz_bool mz_zip_reader_end_internal(mz_zip_archive* pZip, mz_bool set_last_error) { ··· 3642 3484 if ((!pZip) || (!pFilename) || ((archive_size) && (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE))) 3643 3485 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3644 3486 3645 - pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_READ_ALLOW_WRITING) ? "r+b" : "rb"); 3487 + pFile = MZ_FOPEN(pFilename, "rb"); 3646 3488 if (!pFile) return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); 3647 3489 3648 3490 file_size = archive_size; ··· 3942 3784 const mz_zip_array* pCentral_dir_offsets = &pState->m_central_dir_offsets; 3943 3785 const mz_zip_array* pCentral_dir = &pState->m_central_dir; 3944 3786 mz_uint32* pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0); 3945 - const mz_uint32 size = pZip->m_total_files; 3787 + const uint32_t size = pZip->m_total_files; 3946 3788 const mz_uint filename_len = (mz_uint)strlen(pFilename); 3947 3789 3948 3790 if (pIndex) *pIndex = 0; ··· 3954 3796 3955 3797 while (l <= h) { 3956 3798 mz_int64 m = l + ((h - l) >> 1); 3957 - mz_uint32 file_index = pIndices[(mz_uint32)m]; 3799 + uint32_t file_index = pIndices[(uint32_t)m]; 3958 3800 3959 3801 int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len); 3960 3802 if (!comp) { ··· 4034 3876 return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND); 4035 3877 } 4036 3878 4037 - static mz_bool mz_zip_reader_extract_to_mem_no_alloc1(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, 4038 - size_t buf_size, mz_uint flags, void* pUser_read_buf, 4039 - size_t user_read_buf_size, const mz_zip_archive_file_stat* st) { 3879 + mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, size_t buf_size, 3880 + mz_uint flags, void* pUser_read_buf, size_t user_read_buf_size) { 4040 3881 int status = TINFL_STATUS_DONE; 4041 3882 mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail; 4042 3883 mz_zip_archive_file_stat file_stat; ··· 4049 3890 (!pZip->m_pRead)) 4050 3891 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4051 3892 4052 - if (st) { 4053 - file_stat = *st; 4054 - } else if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) 4055 - return MZ_FALSE; 3893 + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; 4056 3894 4057 3895 /* A directory or zero length file */ 4058 3896 if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) return MZ_TRUE; ··· 4080 3918 if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 4081 3919 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4082 3920 4083 - cur_file_ofs += (mz_uint64)(MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + 4084 - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 3921 + cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 4085 3922 MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 4086 3923 if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) 4087 3924 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); ··· 4172 4009 return status == TINFL_STATUS_DONE; 4173 4010 } 4174 4011 4175 - mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, size_t buf_size, 4176 - mz_uint flags, void* pUser_read_buf, size_t user_read_buf_size) { 4177 - return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, 4178 - user_read_buf_size, NULL); 4179 - } 4180 - 4181 4012 mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive* pZip, const char* pFilename, void* pBuf, 4182 4013 size_t buf_size, mz_uint flags, void* pUser_read_buf, 4183 4014 size_t user_read_buf_size) { 4184 4015 mz_uint32 file_index; 4185 4016 if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) return MZ_FALSE; 4186 - return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, 4187 - user_read_buf_size, NULL); 4017 + return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, 4018 + user_read_buf_size); 4188 4019 } 4189 4020 4190 4021 mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, size_t buf_size, 4191 4022 mz_uint flags) { 4192 - return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, NULL, 0, NULL); 4023 + return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0); 4193 4024 } 4194 4025 4195 4026 mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive* pZip, const char* pFilename, void* pBuf, size_t buf_size, ··· 4198 4029 } 4199 4030 4200 4031 void* mz_zip_reader_extract_to_heap(mz_zip_archive* pZip, mz_uint file_index, size_t* pSize, mz_uint flags) { 4201 - mz_zip_archive_file_stat file_stat; 4202 - mz_uint64 alloc_size; 4032 + mz_uint64 comp_size, uncomp_size, alloc_size; 4033 + const mz_uint8* p = mz_zip_get_cdh(pZip, file_index); 4203 4034 void* pBuf; 4204 4035 4205 4036 if (pSize) *pSize = 0; 4206 4037 4207 - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return NULL; 4038 + if (!p) { 4039 + mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4040 + return NULL; 4041 + } 4208 4042 4209 - alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; 4043 + comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); 4044 + uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); 4045 + 4046 + alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; 4210 4047 if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) { 4211 4048 mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 4212 4049 return NULL; ··· 4217 4054 return NULL; 4218 4055 } 4219 4056 4220 - if (!mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, (size_t)alloc_size, flags, NULL, 0, &file_stat)) { 4057 + if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags)) { 4221 4058 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 4222 4059 return NULL; 4223 4060 } ··· 4276 4113 if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 4277 4114 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4278 4115 4279 - cur_file_ofs += (mz_uint64)(MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + 4280 - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 4116 + cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 4281 4117 MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 4282 4118 if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) 4283 4119 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); ··· 4489 4325 return NULL; 4490 4326 } 4491 4327 4492 - pState->cur_file_ofs += (mz_uint64)(MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + 4493 - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 4328 + pState->cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 4494 4329 MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 4495 4330 if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size) { 4496 4331 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); ··· 4626 4461 size_t to_copy = MZ_MIN((buf_size - copied_to_caller), pState->out_blk_remain); 4627 4462 4628 4463 /* Copy data to caller's buffer */ 4629 - memcpy((mz_uint8*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy); 4464 + memcpy((uint8_t*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy); 4630 4465 4631 4466 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4632 4467 /* Perform CRC */ ··· 4963 4798 4964 4799 mz_bool mz_zip_validate_archive(mz_zip_archive* pZip, mz_uint flags) { 4965 4800 mz_zip_internal_state* pState; 4966 - mz_uint32 i; 4801 + uint32_t i; 4967 4802 4968 4803 if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 4969 4804 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); ··· 4976 4811 4977 4812 if (pZip->m_archive_size > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 4978 4813 } else { 4814 + if (pZip->m_total_files >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 4815 + 4979 4816 if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 4980 4817 } 4981 4818 ··· 5280 5117 mz_uint64 cur_ofs = 0; 5281 5118 char buf[4096]; 5282 5119 5283 - MZ_CLEAR_ARR(buf); 5120 + MZ_CLEAR_OBJ(buf); 5284 5121 5285 5122 do { 5286 5123 size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning); ··· 5345 5182 #else 5346 5183 if (pZip->m_pIO_opaque != pZip) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5347 5184 5348 - if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE && !(flags & MZ_ZIP_FLAG_READ_ALLOW_WRITING)) { 5185 + if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) { 5349 5186 if (!pFilename) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5350 5187 5351 5188 /* Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable. */ ··· 5613 5450 pState->m_zip64 = MZ_TRUE; 5614 5451 /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */ 5615 5452 } 5616 - if (((mz_uint64)buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) { 5453 + if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) { 5617 5454 pState->m_zip64 = MZ_TRUE; 5618 5455 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ 5619 5456 } ··· 5632 5469 time(&cur_time); 5633 5470 mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date); 5634 5471 } 5635 - #else 5636 - (void)last_modified; 5637 5472 #endif /* #ifndef MINIZ_NO_TIME */ 5638 5473 5639 5474 if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { ··· 5698 5533 } 5699 5534 cur_archive_file_ofs += num_alignment_padding_bytes; 5700 5535 5701 - MZ_CLEAR_ARR(local_dir_header); 5536 + MZ_CLEAR_OBJ(local_dir_header); 5702 5537 5703 5538 if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { 5704 5539 method = MZ_DEFLATED; ··· 5847 5682 mz_uint level_and_flags, const char* user_extra_data, 5848 5683 mz_uint user_extra_data_len, const char* user_extra_data_central, 5849 5684 mz_uint user_extra_data_central_len) { 5850 - mz_uint16 gen_flags; 5685 + mz_uint16 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; 5851 5686 mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; 5852 5687 mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; 5853 5688 mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; ··· 5859 5694 mz_zip_internal_state* pState; 5860 5695 mz_uint64 file_ofs = 0, cur_archive_header_file_ofs; 5861 5696 5697 + if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; 5698 + 5862 5699 if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; 5863 5700 level = level_and_flags & 0xF; 5864 - 5865 - gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; 5866 - 5867 - if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; 5868 5701 5869 5702 /* Sanity checks */ 5870 5703 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ··· 5918 5751 if (pFile_time) { 5919 5752 mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date); 5920 5753 } 5921 - #else 5922 - (void)pFile_time; 5923 5754 #endif 5924 5755 5925 5756 if (max_size <= 3) level = 0; ··· 5939 5770 method = MZ_DEFLATED; 5940 5771 } 5941 5772 5942 - MZ_CLEAR_ARR(local_dir_header); 5773 + MZ_CLEAR_OBJ(local_dir_header); 5943 5774 if (pState->m_zip64) { 5944 5775 if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) { 5945 5776 pExtra_data = extra_data; ··· 6228 6059 #endif /* #ifndef MINIZ_NO_STDIO */ 6229 6060 6230 6061 static mz_bool mz_zip_writer_update_zip64_extension_block(mz_zip_array* pNew_ext, mz_zip_archive* pZip, 6231 - const mz_uint8* pExt, mz_uint32 ext_len, 6232 - mz_uint64* pComp_size, mz_uint64* pUncomp_size, 6233 - mz_uint64* pLocal_header_ofs, mz_uint32* pDisk_start) { 6062 + const mz_uint8* pExt, uint32_t ext_len, mz_uint64* pComp_size, 6063 + mz_uint64* pUncomp_size, mz_uint64* pLocal_header_ofs, 6064 + mz_uint32* pDisk_start) { 6234 6065 /* + 64 should be enough for any new zip64 data */ 6235 6066 if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE)) return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6236 6067 ··· 6374 6205 local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 6375 6206 local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS); 6376 6207 local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS); 6377 - src_archive_bytes_remaining = src_file_stat.m_comp_size + local_header_filename_size + local_header_extra_len; 6208 + src_archive_bytes_remaining = local_header_filename_size + local_header_extra_len + src_file_stat.m_comp_size; 6378 6209 6379 6210 /* Try to find a zip64 extended information field */ 6380 6211 if ((local_header_extra_len) && ··· 6525 6356 6526 6357 if (pZip->m_pState->m_zip64) { 6527 6358 /* dest is zip64, so upgrade the data descriptor */ 6528 - const mz_uint8* pSrc_descriptor = (const mz_uint8*)pBuf + (has_id ? sizeof(mz_uint32) : 0); 6529 - const mz_uint32 src_crc32 = MZ_READ_LE32(pSrc_descriptor); 6530 - const mz_uint64 src_comp_size = MZ_READ_LE32(pSrc_descriptor + sizeof(mz_uint32)); 6531 - const mz_uint64 src_uncomp_size = MZ_READ_LE32(pSrc_descriptor + 2 * sizeof(mz_uint32)); 6359 + const mz_uint32* pSrc_descriptor = (const mz_uint32*)((const mz_uint8*)pBuf + (has_id ? sizeof(mz_uint32) : 0)); 6360 + const mz_uint32 src_crc32 = pSrc_descriptor[0]; 6361 + const mz_uint64 src_comp_size = pSrc_descriptor[1]; 6362 + const mz_uint64 src_uncomp_size = pSrc_descriptor[2]; 6532 6363 6533 6364 mz_write_le32((mz_uint8*)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID); 6534 6365 mz_write_le32((mz_uint8*)pBuf + sizeof(mz_uint32) * 1, src_crc32); ··· 6653 6484 pState = pZip->m_pState; 6654 6485 6655 6486 if (pState->m_zip64) { 6656 - if ((mz_uint64)pState->m_central_dir.m_size >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 6487 + if ((pZip->m_total_files > MZ_UINT32_MAX) || (pState->m_central_dir.m_size >= MZ_UINT32_MAX)) 6488 + return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 6657 6489 } else { 6658 6490 if ((pZip->m_total_files > MZ_UINT16_MAX) || 6659 6491 ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)) ··· 6678 6510 /* Write zip64 end of central directory header */ 6679 6511 mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size; 6680 6512 6681 - MZ_CLEAR_ARR(hdr); 6513 + MZ_CLEAR_OBJ(hdr); 6682 6514 MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG); 6683 6515 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS, 6684 6516 MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) - sizeof(mz_uint64)); ··· 6695 6527 pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE; 6696 6528 6697 6529 /* Write zip64 end of central directory locator */ 6698 - MZ_CLEAR_ARR(hdr); 6530 + MZ_CLEAR_OBJ(hdr); 6699 6531 MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG); 6700 6532 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr); 6701 6533 MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1); ··· 6707 6539 } 6708 6540 6709 6541 /* Write end of central directory record */ 6710 - MZ_CLEAR_ARR(hdr); 6542 + MZ_CLEAR_OBJ(hdr); 6711 6543 MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG); 6712 6544 MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); 6713 6545 MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); ··· 6794 6626 created_new_archive = MZ_TRUE; 6795 6627 } else { 6796 6628 /* Append to an existing archive. */ 6797 - if (!mz_zip_reader_init_file_v2( 6798 - &zip_archive, pZip_filename, 6799 - level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY | MZ_ZIP_FLAG_READ_ALLOW_WRITING, 0, 0)) { 6629 + if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, 6630 + level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) { 6800 6631 if (pErr) *pErr = zip_archive.m_last_error; 6801 6632 return MZ_FALSE; 6802 6633 } 6803 6634 6804 - if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename, 6805 - level_and_flags | MZ_ZIP_FLAG_READ_ALLOW_WRITING)) { 6635 + if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename, level_and_flags)) { 6806 6636 if (pErr) *pErr = zip_archive.m_last_error; 6807 6637 6808 6638 mz_zip_reader_end_internal(&zip_archive, MZ_FALSE); ··· 6983 6813 case MZ_ZIP_VALIDATION_FAILED: 6984 6814 return "validation failed"; 6985 6815 case MZ_ZIP_WRITE_CALLBACK_FAILED: 6986 - return "write callback failed"; 6987 - case MZ_ZIP_TOTAL_ERRORS: 6988 - return "total errors"; 6816 + return "write calledback failed"; 6989 6817 default: 6990 6818 break; 6991 6819 }
+383 -630
lib/miniz/miniz.h
··· 1 - #ifndef MINIZ_EXPORT 2 1 #define MINIZ_EXPORT 3 - #endif 4 - /* miniz.c 3.1.0 - public domain deflate/inflate, zlib-subset, ZIP 5 - reading/writing/appending, PNG writing See "unlicense" statement at the end 6 - of this file. Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 7 - 2013 Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: 8 - http://www.ietf.org/rfc/rfc1951.txt 2 + /* miniz.c 2.2.0 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing 3 + See "unlicense" statement at the end of this file. 4 + Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013 5 + Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt 9 6 10 - Most API's defined in miniz.c are optional. For example, to disable the 11 - archive related functions just define MINIZ_NO_ARCHIVE_APIS, or to get rid of 12 - all stdio usage define MINIZ_NO_STDIO (see the list below for more macros). 7 + Most API's defined in miniz.c are optional. For example, to disable the archive related functions just define 8 + MINIZ_NO_ARCHIVE_APIS, or to get rid of all stdio usage define MINIZ_NO_STDIO (see the list below for more macros). 13 9 14 10 * Low-level Deflate/Inflate implementation notes: 15 11 16 - Compression: Use the "tdefl" API's. The compressor supports raw, static, 17 - and dynamic blocks, lazy or greedy parsing, match length filtering, RLE-only, 18 - and Huffman-only streams. It performs and compresses approximately as well as 19 - zlib. 12 + Compression: Use the "tdefl" API's. The compressor supports raw, static, and dynamic blocks, lazy or 13 + greedy parsing, match length filtering, RLE-only, and Huffman-only streams. It performs and compresses 14 + approximately as well as zlib. 20 15 21 - Decompression: Use the "tinfl" API's. The entire decompressor is 22 - implemented as a single function coroutine: see tinfl_decompress(). It 23 - supports decompression into a 32KB (or larger power of 2) wrapping buffer, or 16 + Decompression: Use the "tinfl" API's. The entire decompressor is implemented as a single function 17 + coroutine: see tinfl_decompress(). It supports decompression into a 32KB (or larger power of 2) wrapping buffer, or 24 18 into a memory block large enough to hold the entire file. 25 19 26 - The low-level tdefl/tinfl API's do not make any use of dynamic memory 27 - allocation. 20 + The low-level tdefl/tinfl API's do not make any use of dynamic memory allocation. 28 21 29 22 * zlib-style API notes: 30 23 31 - miniz.c implements a fairly large subset of zlib. There's enough 32 - functionality present for it to be a drop-in zlib replacement in many apps: 24 + miniz.c implements a fairly large subset of zlib. There's enough functionality present for it to be a drop-in 25 + zlib replacement in many apps: 33 26 The z_stream struct, optional memory allocation callbacks 34 27 deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound 35 28 inflateInit/inflateInit2/inflate/inflateReset/inflateEnd 36 29 compress, compress2, compressBound, uncompress 37 - CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly 38 - routines. Supports raw deflate streams or standard zlib streams with adler-32 39 - checking. 30 + CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly routines. 31 + Supports raw deflate streams or standard zlib streams with adler-32 checking. 40 32 41 33 Limitations: 42 - The callback API's are not implemented yet. No support for gzip headers or 43 - zlib static dictionaries. I've tried to closely emulate zlib's various 44 - flavors of stream flushing and return status codes, but there are no 45 - guarantees that miniz.c pulls this off perfectly. 34 + The callback API's are not implemented yet. No support for gzip headers or zlib static dictionaries. 35 + I've tried to closely emulate zlib's various flavors of stream flushing and return status codes, but 36 + there are no guarantees that miniz.c pulls this off perfectly. 46 37 47 - * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, 48 - originally written by Alex Evans. Supports 1-4 bytes/pixel images. 38 + * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, originally written by 39 + Alex Evans. Supports 1-4 bytes/pixel images. 49 40 50 41 * ZIP archive API notes: 51 42 52 - The ZIP archive API's where designed with simplicity and efficiency in 53 - mind, with just enough abstraction to get the job done with minimal fuss. 54 - There are simple API's to retrieve file information, read files from existing 55 - archives, create new archives, append new files to existing archives, or 56 - clone archive data from one archive to another. It supports archives located 57 - in memory or the heap, on disk (using stdio.h), or you can specify custom 58 - file read/write callbacks. 43 + The ZIP archive API's where designed with simplicity and efficiency in mind, with just enough abstraction to 44 + get the job done with minimal fuss. There are simple API's to retrieve file information, read files from 45 + existing archives, create new archives, append new files to existing archives, or clone archive data from 46 + one archive to another. It supports archives located in memory or the heap, on disk (using stdio.h), 47 + or you can specify custom file read/write callbacks. 59 48 60 - - Archive reading: Just call this function to read a single file from a 61 - disk archive: 49 + - Archive reading: Just call this function to read a single file from a disk archive: 62 50 63 - void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const 64 - char *pArchive_name, size_t *pSize, mz_uint zip_flags); 51 + void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, 52 + size_t *pSize, mz_uint zip_flags); 65 53 66 - For more complex cases, use the "mz_zip_reader" functions. Upon opening an 67 - archive, the entire central directory is located and read as-is into memory, 68 - and subsequent file access only occurs when reading individual files. 54 + For more complex cases, use the "mz_zip_reader" functions. Upon opening an archive, the entire central 55 + directory is located and read as-is into memory, and subsequent file access only occurs when reading individual 56 + files. 69 57 70 - - Archives file scanning: The simple way is to use this function to scan a 71 - loaded archive for a specific file: 58 + - Archives file scanning: The simple way is to use this function to scan a loaded archive for a specific file: 72 59 73 - int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, 74 - const char *pComment, mz_uint flags); 60 + int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); 75 61 76 - The locate operation can optionally check file comments too, which (as one 77 - example) can be used to identify multiple versions of the same file in an 78 - archive. This function uses a simple linear search through the central 62 + The locate operation can optionally check file comments too, which (as one example) can be used to identify 63 + multiple versions of the same file in an archive. This function uses a simple linear search through the central 79 64 directory, so it's not very fast. 80 65 81 - Alternately, you can iterate through all the files in an archive (using 82 - mz_zip_reader_get_num_files()) and retrieve detailed info on each file by 83 - calling mz_zip_reader_file_stat(). 66 + Alternately, you can iterate through all the files in an archive (using mz_zip_reader_get_num_files()) and 67 + retrieve detailed info on each file by calling mz_zip_reader_file_stat(). 84 68 85 - - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer 86 - immediately writes compressed file data to disk and builds an exact image of 87 - the central directory in memory. The central directory image is written all 88 - at once at the end of the archive file when the archive is finalized. 69 + - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer immediately writes compressed file data 70 + to disk and builds an exact image of the central directory in memory. The central directory image is written 71 + all at once at the end of the archive file when the archive is finalized. 89 72 90 - The archive writer can optionally align each file's local header and file 91 - data to any power of 2 alignment, which can be useful when the archive will 92 - be read from optical media. Also, the writer supports placing arbitrary data 93 - blobs at the very beginning of ZIP archives. Archives written using either 94 - feature are still readable by any ZIP tool. 73 + The archive writer can optionally align each file's local header and file data to any power of 2 alignment, 74 + which can be useful when the archive will be read from optical media. Also, the writer supports placing 75 + arbitrary data blobs at the very beginning of ZIP archives. Archives written using either feature are still 76 + readable by any ZIP tool. 95 77 96 - - Archive appending: The simple way to add a single file to an archive is 97 - to call this function: 78 + - Archive appending: The simple way to add a single file to an archive is to call this function: 98 79 99 - mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, 100 - const char *pArchive_name, const void *pBuf, size_t buf_size, const void 101 - *pComment, mz_uint16 comment_size, mz_uint level_and_flags); 80 + mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, 81 + const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); 102 82 103 - The archive will be created if it doesn't already exist, otherwise it'll be 104 - appended to. Note the appending is done in-place and is not an atomic 105 - operation, so if something goes wrong during the operation it's possible the 106 - archive could be left without a central directory (although the local file 107 - headers and file data will be fine, so the archive will be recoverable). 83 + The archive will be created if it doesn't already exist, otherwise it'll be appended to. 84 + Note the appending is done in-place and is not an atomic operation, so if something goes wrong 85 + during the operation it's possible the archive could be left without a central directory (although the local 86 + file headers and file data will be fine, so the archive will be recoverable). 108 87 109 88 For more complex archive modification scenarios: 110 - 1. The safest way is to use a mz_zip_reader to read the existing archive, 111 - cloning only those bits you want to preserve into a new archive using using 112 - the mz_zip_writer_add_from_zip_reader() function (which compiles the 113 - compressed file data as-is). When you're done, delete the old archive and 114 - rename the newly written archive, and you're done. This is safe but requires 115 - a bunch of temporary disk space or heap memory. 89 + 1. The safest way is to use a mz_zip_reader to read the existing archive, cloning only those bits you want to 90 + preserve into a new archive using using the mz_zip_writer_add_from_zip_reader() function (which compiles the 91 + compressed file data as-is). When you're done, delete the old archive and rename the newly written archive, and 92 + you're done. This is safe but requires a bunch of temporary disk space or heap memory. 116 93 117 - 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using 118 - mz_zip_writer_init_from_reader(), append new files as needed, then finalize 119 - the archive which will write an updated central directory to the original 120 - archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() 121 - does.) There's a possibility that the archive's central directory could be 122 - lost with this method if anything goes wrong, though. 94 + 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using mz_zip_writer_init_from_reader(), 95 + append new files as needed, then finalize the archive which will write an updated central directory to the 96 + original archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() does.) There's a 97 + possibility that the archive's central directory could be lost with this method if anything goes wrong, though. 123 98 124 99 - ZIP archive support limitations: 125 - No spanning support. Extraction functions can only handle unencrypted, 126 - stored or deflated files. Requires streams capable of seeking. 100 + No spanning support. Extraction functions can only handle unencrypted, stored or deflated files. 101 + Requires streams capable of seeking. 127 102 128 - * This is a header file library, like stb_image.c. To get only a header file, 129 - either cut and paste the below header, or create miniz.h, #define 130 - MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it. 103 + * This is a header file library, like stb_image.c. To get only a header file, either cut and paste the 104 + below header, or create miniz.h, #define MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it. 131 105 132 - * Important: For best perf. be sure to customize the below macros for your 133 - target platform: #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 #define 134 - MINIZ_LITTLE_ENDIAN 1 #define MINIZ_HAS_64BIT_REGISTERS 1 106 + * Important: For best perf. be sure to customize the below macros for your target platform: 107 + #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 108 + #define MINIZ_LITTLE_ENDIAN 1 109 + #define MINIZ_HAS_64BIT_REGISTERS 1 135 110 136 - * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before 137 - including miniz.c to ensure miniz uses the 64-bit variants: fopen64(), 138 - stat64(), etc. Otherwise you won't be able to process large files (i.e. 139 - 32-bit stat() fails for me on files > 0x7FFFFFFF bytes). 111 + * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before including miniz.c to ensure miniz 112 + uses the 64-bit variants: fopen64(), stat64(), etc. Otherwise you won't be able to process large files 113 + (i.e. 32-bit stat() fails for me on files > 0x7FFFFFFF bytes). 140 114 */ 141 115 #pragma once 142 116 143 117 /* Defines to completely disable specific portions of miniz.c: 144 - If all macros here are defined the only functionality remaining will be 145 - CRC-32 and adler-32. */ 118 + If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl. */ 146 119 147 - /* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on 148 - * stdio for file I/O. */ 120 + /* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O. */ 149 121 /*#define MINIZ_NO_STDIO */ 150 122 151 - /* If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able 152 - * to get the current time, or */ 153 - /* get/set file times, and the C run-time funcs that get/set times won't be 154 - * called. */ 155 - /* The current downside is the times written to your archives will be from 1979. 156 - */ 123 + /* If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or */ 124 + /* get/set file times, and the C run-time funcs that get/set times won't be called. */ 125 + /* The current downside is the times written to your archives will be from 1979. */ 157 126 /*#define MINIZ_NO_TIME */ 158 127 159 - /* Define MINIZ_NO_DEFLATE_APIS to disable all compression API's. */ 160 - /*#define MINIZ_NO_DEFLATE_APIS */ 161 - 162 - /* Define MINIZ_NO_INFLATE_APIS to disable all decompression API's. */ 163 - /*#define MINIZ_NO_INFLATE_APIS */ 164 - 165 128 /* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */ 166 129 /*#define MINIZ_NO_ARCHIVE_APIS */ 167 130 168 - /* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP 169 - * archive API's. */ 131 + /* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP archive API's. */ 170 132 /*#define MINIZ_NO_ARCHIVE_WRITING_APIS */ 171 133 172 - /* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression 173 - * API's. */ 134 + /* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's. */ 174 135 /*#define MINIZ_NO_ZLIB_APIS */ 175 136 176 - /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent 177 - * conflicts against stock zlib. */ 137 + /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib. */ 178 138 /*#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ 179 139 180 140 /* Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc. 181 - Note if MINIZ_NO_MALLOC is defined then the user must always provide custom 182 - user alloc/free/realloc callbacks to the zlib and archive API's, and a few 183 - stand-alone helper API's which don't provide custom user functions (such as 184 - tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. 185 - */ 141 + Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc 142 + callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user 143 + functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. */ 186 144 /*#define MINIZ_NO_MALLOC */ 187 145 188 - #ifdef MINIZ_NO_INFLATE_APIS 189 - #define MINIZ_NO_ARCHIVE_APIS 190 - #endif 191 - 192 - #ifdef MINIZ_NO_DEFLATE_APIS 193 - #define MINIZ_NO_ARCHIVE_WRITING_APIS 194 - #endif 195 - 196 146 #if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) 197 - /* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc 198 - * on Linux */ 147 + /* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux */ 199 148 #define MINIZ_NO_TIME 200 149 #endif 201 150 ··· 213 162 #define MINIZ_X86_OR_X64_CPU 0 214 163 #endif 215 164 216 - /* Set MINIZ_LITTLE_ENDIAN only if not set */ 217 - #if !defined(MINIZ_LITTLE_ENDIAN) 218 - #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) 219 - 220 - #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) 165 + #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU 221 166 /* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */ 222 167 #define MINIZ_LITTLE_ENDIAN 1 223 168 #else 224 169 #define MINIZ_LITTLE_ENDIAN 0 225 170 #endif 226 171 227 - #else 228 - 229 - #if MINIZ_X86_OR_X64_CPU 230 - #define MINIZ_LITTLE_ENDIAN 1 231 - #else 232 - #define MINIZ_LITTLE_ENDIAN 0 233 - #endif 234 - 235 - #endif 236 - #endif 237 - 238 - /* Using unaligned loads and stores causes errors when using UBSan */ 239 - #if defined(__has_feature) 240 - #if __has_feature(undefined_behavior_sanitizer) 241 - #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 242 - #endif 243 - #endif 244 - 245 172 /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */ 246 173 #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES) 247 174 #if MINIZ_X86_OR_X64_CPU 248 - /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient 249 - * integer loads and stores from unaligned addresses. */ 250 - #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 175 + /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned 176 + * addresses. */ 177 + #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 251 178 #define MINIZ_UNALIGNED_USE_MEMCPY 252 179 #else 253 180 #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 ··· 256 183 257 184 #if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || \ 258 185 defined(__ia64__) || defined(__x86_64__) 259 - /* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are 260 - * reasonably fast (and don't involve compiler generated calls to helper 261 - * functions). */ 186 + /* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler 187 + * generated calls to helper functions). */ 262 188 #define MINIZ_HAS_64BIT_REGISTERS 1 263 189 #else 264 190 #define MINIZ_HAS_64BIT_REGISTERS 0 ··· 270 196 271 197 /* ------------------- zlib-style API Definitions. */ 272 198 273 - /* For more compatibility with zlib, miniz.c uses unsigned long for some 274 - * parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! */ 199 + /* For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. Beware: mz_ulong can 200 + * be either 32 or 64-bits! */ 275 201 typedef unsigned long mz_ulong; 276 202 277 - /* mz_free() internally uses the MZ_FREE() macro (which by default calls free() 278 - * unless you've modified the MZ_MALLOC macro) to release a block allocated from 279 - * the heap. */ 203 + /* mz_free() internally uses the MZ_FREE() macro (which by default calls free() unless you've modified the MZ_MALLOC 204 + * macro) to release a block allocated from the heap. */ 280 205 MINIZ_EXPORT void mz_free(void* p); 281 206 282 207 #define MZ_ADLER32_INIT (1) 283 - /* mz_adler32() returns the initial adler-32 value to use when called with 284 - * ptr==NULL. */ 208 + /* mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL. */ 285 209 MINIZ_EXPORT mz_ulong mz_adler32(mz_ulong adler, const unsigned char* ptr, size_t buf_len); 286 210 287 211 #define MZ_CRC32_INIT (0) 288 - /* mz_crc32() returns the initial CRC-32 value to use when called with 289 - * ptr==NULL. */ 212 + /* mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL. */ 290 213 MINIZ_EXPORT mz_ulong mz_crc32(mz_ulong crc, const unsigned char* ptr, size_t buf_len); 291 214 292 215 /* Compression strategies. */ ··· 296 219 #define MZ_DEFLATED 8 297 220 298 221 /* Heap allocation callbacks. 299 - Note that mz_alloc_func parameter types purposely differ from zlib's: items/size 300 - is size_t, not unsigned long. */ 222 + Note that mz_alloc_func parameter types purposely differ from zlib's: items/size is size_t, not unsigned long. */ 301 223 typedef void* (*mz_alloc_func)(void* opaque, size_t items, size_t size); 302 224 typedef void (*mz_free_func)(void* opaque, void* address); 303 225 typedef void* (*mz_realloc_func)(void* opaque, void* address, size_t items, size_t size); 304 226 305 - /* Compression levels: 0-9 are the standard zlib-style levels, 10 is best 306 - * possible compression (not zlib compatible, and may be very slow), 307 - * MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. */ 227 + /* Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and 228 + * may be very slow), MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. */ 308 229 enum { 309 230 MZ_NO_COMPRESSION = 0, 310 231 MZ_BEST_SPEED = 1, ··· 314 235 MZ_DEFAULT_COMPRESSION = -1 315 236 }; 316 237 317 - #define MZ_VERSION "11.3.0" 318 - #define MZ_VERNUM 0xB300 319 - #define MZ_VER_MAJOR 11 320 - #define MZ_VER_MINOR 3 238 + #define MZ_VERSION "10.2.0" 239 + #define MZ_VERNUM 0xA100 240 + #define MZ_VER_MAJOR 10 241 + #define MZ_VER_MINOR 2 321 242 #define MZ_VER_REVISION 0 322 243 #define MZ_VER_SUBREVISION 0 323 244 324 245 #ifndef MINIZ_NO_ZLIB_APIS 325 246 326 - /* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The 327 - * other values are for advanced use (refer to the zlib docs). */ 247 + /* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other values are for advanced use (refer 248 + * to the zlib docs). */ 328 249 enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 }; 329 250 330 251 /* Return status codes. MZ_PARAM_ERROR is non-standard. */ ··· 373 294 /* Returns the version string of miniz.c. */ 374 295 MINIZ_EXPORT const char* mz_version(void); 375 296 376 - #ifndef MINIZ_NO_DEFLATE_APIS 377 - 378 297 /* mz_deflateInit() initializes a compressor with default options: */ 379 298 /* Parameters: */ 380 299 /* pStream must point to an initialized mz_stream struct. */ 381 300 /* level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. */ 382 - /* level 1 enables a specially optimized compression function that's been 383 - * optimized purely for performance, not ratio. */ 384 - /* (This special func. is currently only enabled when 385 - * MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) */ 301 + /* level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio. 302 + */ 303 + /* (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are 304 + * defined.) */ 386 305 /* Return values: */ 387 306 /* MZ_OK on success. */ 388 307 /* MZ_STREAM_ERROR if the stream is bogus. */ ··· 393 312 /* mz_deflateInit2() is like mz_deflate(), except with more control: */ 394 313 /* Additional parameters: */ 395 314 /* method must be MZ_DEFLATED */ 396 - /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with 397 - * zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no 398 - * header or footer) */ 315 + /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with zlib header/adler-32 footer) or 316 + * -MZ_DEFAULT_WINDOW_BITS (raw deflate/no header or footer) */ 399 317 /* mem_level must be between [1, 9] (it's checked but ignored by miniz.c) */ 400 318 MINIZ_EXPORT int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, 401 319 int strategy); 402 320 403 - /* Quickly resets a compressor without having to reallocate anything. Same as 404 - * calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). */ 321 + /* Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by 322 + * mz_deflateInit()/mz_deflateInit2(). */ 405 323 MINIZ_EXPORT int mz_deflateReset(mz_streamp pStream); 406 324 407 - /* mz_deflate() compresses the input to output, consuming as much of the input 408 - * and producing as much output as possible. */ 325 + /* mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible. 326 + */ 409 327 /* Parameters: */ 410 - /* pStream is the stream to read from and write to. You must initialize/update 411 - * the next_in, avail_in, next_out, and avail_out members. */ 412 - /* flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or 413 - * MZ_FINISH. */ 328 + /* pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and 329 + * avail_out members. */ 330 + /* flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH. */ 414 331 /* Return values: */ 415 - /* MZ_OK on success (when flushing, or if more input is needed but not 416 - * available, and/or there's more output to be written but the output buffer is 417 - * full). */ 418 - /* MZ_STREAM_END if all input has been consumed and all output bytes have been 419 - * written. Don't call mz_deflate() on the stream anymore. */ 332 + /* MZ_OK on success (when flushing, or if more input is needed but not available, and/or there's more output to be 333 + * written but the output buffer is full). */ 334 + /* MZ_STREAM_END if all input has been consumed and all output bytes have been written. Don't call mz_deflate() on the 335 + * stream anymore. */ 420 336 /* MZ_STREAM_ERROR if the stream is bogus. */ 421 337 /* MZ_PARAM_ERROR if one of the parameters is invalid. */ 422 - /* MZ_BUF_ERROR if no forward progress is possible because the input and/or 423 - * output buffers are empty. (Fill up the input buffer or free up some output 424 - * space and try again.) */ 338 + /* MZ_BUF_ERROR if no forward progress is possible because the input and/or output buffers are empty. (Fill up the 339 + * input buffer or free up some output space and try again.) */ 425 340 MINIZ_EXPORT int mz_deflate(mz_streamp pStream, int flush); 426 341 427 342 /* mz_deflateEnd() deinitializes a compressor: */ ··· 430 345 /* MZ_STREAM_ERROR if the stream is bogus. */ 431 346 MINIZ_EXPORT int mz_deflateEnd(mz_streamp pStream); 432 347 433 - /* mz_deflateBound() returns a (very) conservative upper bound on the amount of 434 - * data that could be generated by deflate(), assuming flush is set to only 435 - * MZ_NO_FLUSH or MZ_FINISH. */ 348 + /* mz_deflateBound() returns a (very) conservative upper bound on the amount of data that could be generated by 349 + * deflate(), assuming flush is set to only MZ_NO_FLUSH or MZ_FINISH. */ 436 350 MINIZ_EXPORT mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len); 437 351 438 352 /* Single-call compression functions mz_compress() and mz_compress2(): */ 439 - /* Returns MZ_OK on success, or one of the error codes from mz_deflate() on 440 - * failure. */ 353 + /* Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure. */ 441 354 MINIZ_EXPORT int mz_compress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 442 355 mz_ulong source_len); 443 356 MINIZ_EXPORT int mz_compress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 444 357 mz_ulong source_len, int level); 445 358 446 - /* mz_compressBound() returns a (very) conservative upper bound on the amount of 447 - * data that could be generated by calling mz_compress(). */ 359 + /* mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling 360 + * mz_compress(). */ 448 361 MINIZ_EXPORT mz_ulong mz_compressBound(mz_ulong source_len); 449 362 450 - #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ 451 - 452 - #ifndef MINIZ_NO_INFLATE_APIS 453 - 454 363 /* Initializes a decompressor. */ 455 364 MINIZ_EXPORT int mz_inflateInit(mz_streamp pStream); 456 365 457 - /* mz_inflateInit2() is like mz_inflateInit() with an additional option that 458 - * controls the window size and whether or not the stream has been wrapped with 459 - * a zlib header/footer: */ 460 - /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or 461 - * -MZ_DEFAULT_WINDOW_BITS (raw deflate). */ 366 + /* mz_inflateInit2() is like mz_inflateInit() with an additional option that controls the window size and whether or not 367 + * the stream has been wrapped with a zlib header/footer: */ 368 + /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate). */ 462 369 MINIZ_EXPORT int mz_inflateInit2(mz_streamp pStream, int window_bits); 463 370 464 - /* Quickly resets a compressor without having to reallocate anything. Same as 465 - * calling mz_inflateEnd() followed by mz_inflateInit()/mz_inflateInit2(). */ 371 + /* Quickly resets a compressor without having to reallocate anything. Same as calling mz_inflateEnd() followed by 372 + * mz_inflateInit()/mz_inflateInit2(). */ 466 373 MINIZ_EXPORT int mz_inflateReset(mz_streamp pStream); 467 374 468 - /* Decompresses the input stream to the output, consuming only as much of the 469 - * input as needed, and writing as much to the output as possible. */ 375 + /* Decompresses the input stream to the output, consuming only as much of the input as needed, and writing as much to 376 + * the output as possible. */ 470 377 /* Parameters: */ 471 - /* pStream is the stream to read from and write to. You must initialize/update 472 - * the next_in, avail_in, next_out, and avail_out members. */ 378 + /* pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and 379 + * avail_out members. */ 473 380 /* flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. */ 474 - /* On the first call, if flush is MZ_FINISH it's assumed the input and output 475 - * buffers are both sized large enough to decompress the entire stream in a 476 - * single call (this is slightly faster). */ 477 - /* MZ_FINISH implies that there are no more source bytes available beside 478 - * what's already in the input buffer, and that the output buffer is large 479 - * enough to hold the rest of the decompressed data. */ 381 + /* On the first call, if flush is MZ_FINISH it's assumed the input and output buffers are both sized large enough to 382 + * decompress the entire stream in a single call (this is slightly faster). */ 383 + /* MZ_FINISH implies that there are no more source bytes available beside what's already in the input buffer, and that 384 + * the output buffer is large enough to hold the rest of the decompressed data. */ 480 385 /* Return values: */ 481 - /* MZ_OK on success. Either more input is needed but not available, and/or 482 - * there's more output to be written but the output buffer is full. */ 483 - /* MZ_STREAM_END if all needed input has been consumed and all output bytes 484 - * have been written. For zlib streams, the adler-32 of the decompressed data 485 - * has also been verified. */ 386 + /* MZ_OK on success. Either more input is needed but not available, and/or there's more output to be written but the 387 + * output buffer is full. */ 388 + /* MZ_STREAM_END if all needed input has been consumed and all output bytes have been written. For zlib streams, the 389 + * adler-32 of the decompressed data has also been verified. */ 486 390 /* MZ_STREAM_ERROR if the stream is bogus. */ 487 391 /* MZ_DATA_ERROR if the deflate stream is invalid. */ 488 392 /* MZ_PARAM_ERROR if one of the parameters is invalid. */ 489 - /* MZ_BUF_ERROR if no forward progress is possible because the input buffer is 490 - * empty but the inflater needs more input to continue, or if the output buffer 491 - * is not large enough. Call mz_inflate() again */ 492 - /* with more input data, or with more room in the output buffer (except when 493 - * using single call decompression, described above). */ 393 + /* MZ_BUF_ERROR if no forward progress is possible because the input buffer is empty but the inflater needs more input 394 + * to continue, or if the output buffer is not large enough. Call mz_inflate() again */ 395 + /* with more input data, or with more room in the output buffer (except when using single call decompression, 396 + * described above). */ 494 397 MINIZ_EXPORT int mz_inflate(mz_streamp pStream, int flush); 495 398 496 399 /* Deinitializes a decompressor. */ 497 400 MINIZ_EXPORT int mz_inflateEnd(mz_streamp pStream); 498 401 499 402 /* Single-call decompression. */ 500 - /* Returns MZ_OK on success, or one of the error codes from mz_inflate() on 501 - * failure. */ 403 + /* Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. */ 502 404 MINIZ_EXPORT int mz_uncompress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 503 405 mz_ulong source_len); 504 406 MINIZ_EXPORT int mz_uncompress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 505 407 mz_ulong* pSource_len); 506 - #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ 507 408 508 - /* Returns a string description of the specified error code, or NULL if the 509 - * error code is invalid. */ 409 + /* Returns a string description of the specified error code, or NULL if the error code is invalid. */ 510 410 MINIZ_EXPORT const char* mz_error(int err); 511 411 512 - /* Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used 513 - * as a drop-in replacement for the subset of zlib that miniz.c supports. */ 514 - /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you 515 - * use zlib in the same project. */ 412 + /* Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used as a drop-in replacement for the subset 413 + * of zlib that miniz.c supports. */ 414 + /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project. */ 516 415 #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES 517 416 typedef unsigned char Byte; 518 417 typedef unsigned int uInt; ··· 553 452 #define Z_FIXED MZ_FIXED 554 453 #define Z_DEFLATED MZ_DEFLATED 555 454 #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS 556 - /* See mz_alloc_func */ 557 - typedef void* (*alloc_func)(void* opaque, size_t items, size_t size); 558 - /* See mz_free_func */ 559 - typedef void (*free_func)(void* opaque, void* address); 560 - 455 + #define alloc_func mz_alloc_func 456 + #define free_func mz_free_func 561 457 #define internal_state mz_internal_state 562 458 #define z_stream mz_stream 563 - 564 - #ifndef MINIZ_NO_DEFLATE_APIS 565 - /* Compatiblity with zlib API. See called functions for documentation */ 566 - static int deflateInit(mz_streamp pStream, int level) { return mz_deflateInit(pStream, level); } 567 - static int deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy) { 568 - return mz_deflateInit2(pStream, level, method, window_bits, mem_level, strategy); 569 - } 570 - static int deflateReset(mz_streamp pStream) { return mz_deflateReset(pStream); } 571 - static int deflate(mz_streamp pStream, int flush) { return mz_deflate(pStream, flush); } 572 - static int deflateEnd(mz_streamp pStream) { return mz_deflateEnd(pStream); } 573 - static mz_ulong deflateBound(mz_streamp pStream, mz_ulong source_len) { return mz_deflateBound(pStream, source_len); } 574 - static int compress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len) { 575 - return mz_compress(pDest, pDest_len, pSource, source_len); 576 - } 577 - static int compress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len, 578 - int level) { 579 - return mz_compress2(pDest, pDest_len, pSource, source_len, level); 580 - } 581 - static mz_ulong compressBound(mz_ulong source_len) { return mz_compressBound(source_len); } 582 - #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ 583 - 584 - #ifndef MINIZ_NO_INFLATE_APIS 585 - /* Compatiblity with zlib API. See called functions for documentation */ 586 - static int inflateInit(mz_streamp pStream) { return mz_inflateInit(pStream); } 587 - 588 - static int inflateInit2(mz_streamp pStream, int window_bits) { return mz_inflateInit2(pStream, window_bits); } 589 - 590 - static int inflateReset(mz_streamp pStream) { return mz_inflateReset(pStream); } 591 - 592 - static int inflate(mz_streamp pStream, int flush) { return mz_inflate(pStream, flush); } 593 - 594 - static int inflateEnd(mz_streamp pStream) { return mz_inflateEnd(pStream); } 595 - 596 - static int uncompress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len) { 597 - return mz_uncompress(pDest, pDest_len, pSource, source_len); 598 - } 599 - 600 - static int uncompress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong* pSource_len) { 601 - return mz_uncompress2(pDest, pDest_len, pSource, pSource_len); 602 - } 603 - #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ 604 - 605 - static mz_ulong crc32(mz_ulong crc, const unsigned char* ptr, size_t buf_len) { return mz_crc32(crc, ptr, buf_len); } 606 - 607 - static mz_ulong adler32(mz_ulong adler, const unsigned char* ptr, size_t buf_len) { 608 - return mz_adler32(adler, ptr, buf_len); 609 - } 610 - 459 + #define deflateInit mz_deflateInit 460 + #define deflateInit2 mz_deflateInit2 461 + #define deflateReset mz_deflateReset 462 + #define deflate mz_deflate 463 + #define deflateEnd mz_deflateEnd 464 + #define deflateBound mz_deflateBound 465 + #define compress mz_compress 466 + #define compress2 mz_compress2 467 + #define compressBound mz_compressBound 468 + #define inflateInit mz_inflateInit 469 + #define inflateInit2 mz_inflateInit2 470 + #define inflateReset mz_inflateReset 471 + #define inflate mz_inflate 472 + #define inflateEnd mz_inflateEnd 473 + #define uncompress mz_uncompress 474 + #define uncompress2 mz_uncompress2 475 + #define crc32 mz_crc32 476 + #define adler32 mz_adler32 611 477 #define MAX_WBITS 15 612 478 #define MAX_MEM_LEVEL 9 613 - 614 - static const char* zError(int err) { return mz_error(err); } 479 + #define zError mz_error 615 480 #define ZLIB_VERSION MZ_VERSION 616 481 #define ZLIB_VERNUM MZ_VERNUM 617 482 #define ZLIB_VER_MAJOR MZ_VER_MAJOR 618 483 #define ZLIB_VER_MINOR MZ_VER_MINOR 619 484 #define ZLIB_VER_REVISION MZ_VER_REVISION 620 485 #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION 621 - 622 486 #define zlibVersion mz_version 623 487 #define zlib_version mz_version() 624 488 #endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ ··· 637 501 638 502 /* ------------------- Types and macros */ 639 503 typedef unsigned char mz_uint8; 640 - typedef int16_t mz_int16; 641 - typedef uint16_t mz_uint16; 642 - typedef uint32_t mz_uint32; 643 - typedef uint32_t mz_uint; 504 + typedef signed short mz_int16; 505 + typedef unsigned short mz_uint16; 506 + typedef unsigned int mz_uint32; 507 + typedef unsigned int mz_uint; 644 508 typedef int64_t mz_int64; 645 509 typedef uint64_t mz_uint64; 646 510 typedef int mz_bool; ··· 648 512 #define MZ_FALSE (0) 649 513 #define MZ_TRUE (1) 650 514 651 - /* Works around MSVC's spammy "warning C4127: conditional expression is 652 - * constant" message. */ 515 + /* Works around MSVC's spammy "warning C4127: conditional expression is constant" message. */ 653 516 #ifdef _MSC_VER 654 517 #define MZ_MACRO_END while (0, 0) 655 518 #else ··· 665 528 666 529 #ifdef MINIZ_NO_TIME 667 530 typedef struct mz_dummy_time_t_tag { 668 - mz_uint32 m_dummy1; 669 - mz_uint32 m_dummy2; 531 + int m_dummy; 670 532 } mz_dummy_time_t; 671 533 #define MZ_TIME_T mz_dummy_time_t 672 534 #else ··· 688 550 #define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) 689 551 #define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) 690 552 #define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) 691 - #define MZ_CLEAR_ARR(obj) memset((obj), 0, sizeof(obj)) 692 - #define MZ_CLEAR_PTR(obj) memset((obj), 0, sizeof(*obj)) 693 553 694 554 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN 695 555 #define MZ_READ_LE16(p) *((const mz_uint16*)(p)) ··· 728 588 #endif 729 589 #pragma once 730 590 731 - #ifndef MINIZ_NO_DEFLATE_APIS 732 - 733 591 #ifdef __cplusplus 734 592 extern "C" { 735 593 #endif 736 594 /* ------------------- Low-level Compression API Definitions */ 737 595 738 - /* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly 739 - * slower, and raw/dynamic blocks will be output more frequently). */ 740 - #ifndef TDEFL_LESS_MEMORY 596 + /* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be 597 + * output more frequently). */ 741 598 #define TDEFL_LESS_MEMORY 0 742 - #endif 743 599 744 - /* tdefl_init() compression flags logically OR'd together (low 12 bits contain 745 - * the max. number of probes per dictionary search): */ 746 - /* TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes 747 - * per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap 748 - * compression), 4095=Huffman+LZ (slowest/best compression). */ 600 + /* tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary 601 + * search): */ 602 + /* TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 603 + * 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression). */ 749 604 enum { TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF }; 750 605 751 - /* TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before 752 - * the deflate data, and the Adler-32 of the source data at the end. Otherwise, 753 - * you'll get raw deflate data. */ 754 - /* TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even 755 - * when not writing zlib headers). */ 756 - /* TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more 757 - * efficient lazy parsing. */ 758 - /* TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's 759 - * initialization time to the minimum, but the output may vary from run to run 760 - * given the same input (depending on the contents of memory). */ 761 - /* TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) 762 - */ 606 + /* TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of 607 + * the source data at the end. Otherwise, you'll get raw deflate data. */ 608 + /* TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers). */ 609 + /* TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing. */ 610 + /* TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's initialization time to the minimum, but the 611 + * output may vary from run to run given the same input (depending on the contents of memory). */ 612 + /* TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) */ 763 613 /* TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. */ 764 614 /* TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. */ 765 615 /* TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. */ 766 - /* The low 12 bits are reserved to control the max # of hash probes per 767 - * dictionary lookup (see TDEFL_MAX_PROBES_MASK). */ 616 + /* The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK). 617 + */ 768 618 enum { 769 619 TDEFL_WRITE_ZLIB_HEADER = 0x01000, 770 620 TDEFL_COMPUTE_ADLER32 = 0x02000, ··· 777 627 }; 778 628 779 629 /* High level compression functions: */ 780 - /* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block 781 - * allocated via malloc(). */ 630 + /* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc(). */ 782 631 /* On entry: */ 783 632 /* pSrc_buf, src_buf_len: Pointer and size of source block to compress. */ 784 - /* flags: The max match finder probes (default is 128) logically OR'd against 785 - * the above flags. Higher probes are slower but improve compression. */ 633 + /* flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower 634 + * but improve compression. */ 786 635 /* On return: */ 787 636 /* Function returns a pointer to the compressed data, or NULL on failure. */ 788 - /* *pOut_len will be set to the compressed data's size, which could be larger 789 - * than src_buf_len on uncompressible data. */ 637 + /* *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. 638 + */ 790 639 /* The caller must free() the returned block when it's no longer needed. */ 791 640 MINIZ_EXPORT void* tdefl_compress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags); 792 641 793 - /* tdefl_compress_mem_to_mem() compresses a block in memory to another block in 794 - * memory. */ 642 + /* tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. */ 795 643 /* Returns 0 on failure. */ 796 644 MINIZ_EXPORT size_t tdefl_compress_mem_to_mem(void* pOut_buf, size_t out_buf_len, const void* pSrc_buf, 797 645 size_t src_buf_len, int flags); 798 646 799 647 /* Compresses an image to a compressed PNG file in memory. */ 800 648 /* On entry: */ 801 - /* pImage, w, h, and num_chans describe the image to compress. num_chans may be 802 - * 1, 2, 3, or 4. */ 803 - /* The image pitch in bytes per scanline will be w*num_chans. The leftmost 804 - * pixel on the top scanline is stored first in memory. */ 805 - /* level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, 806 - * MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL */ 807 - /* If flip is true, the image will be flipped on the Y axis (useful for OpenGL 808 - * apps). */ 649 + /* pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4. */ 650 + /* The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in 651 + * memory. */ 652 + /* level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc. or a decent default is 653 + * MZ_DEFAULT_LEVEL */ 654 + /* If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps). */ 809 655 /* On return: */ 810 656 /* Function returns a pointer to the compressed data, or NULL on failure. */ 811 657 /* *pLen_out will be set to the size of the PNG image file. */ 812 - /* The caller must mz_free() the returned heap block (which will typically be 813 - * larger than *pLen_out) when it's no longer needed. */ 658 + /* The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no 659 + * longer needed. */ 814 660 MINIZ_EXPORT void* tdefl_write_image_to_png_file_in_memory_ex(const void* pImage, int w, int h, int num_chans, 815 661 size_t* pLen_out, mz_uint level, mz_bool flip); 816 662 MINIZ_EXPORT void* tdefl_write_image_to_png_file_in_memory(const void* pImage, int w, int h, int num_chans, 817 663 size_t* pLen_out); 818 664 819 - /* Output stream interface. The compressor uses this interface to write 820 - * compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. */ 665 + /* Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called 666 + * TDEFL_OUT_BUF_SIZE at a time. */ 821 667 typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void* pUser); 822 668 823 - /* tdefl_compress_mem_to_output() compresses a block to an output stream. The 824 - * above helpers use this function internally. */ 669 + /* tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function 670 + * internally. */ 825 671 MINIZ_EXPORT mz_bool tdefl_compress_mem_to_output(const void* pBuf, size_t buf_len, 826 672 tdefl_put_buf_func_ptr pPut_buf_func, void* pPut_buf_user, int flags); 827 673 ··· 836 682 TDEFL_MAX_MATCH_LEN = 258 837 683 }; 838 684 839 - /* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed 840 - * output block (using static/fixed Huffman codes). */ 685 + /* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman 686 + * codes). */ 841 687 #if TDEFL_LESS_MEMORY 842 688 enum { 843 689 TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, ··· 851 697 #else 852 698 enum { 853 699 TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, 854 - TDEFL_OUT_BUF_SIZE = (mz_uint)((TDEFL_LZ_CODE_BUF_SIZE * 13) / 10), 700 + TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, 855 701 TDEFL_MAX_HUFF_SYMBOLS = 288, 856 702 TDEFL_LZ_HASH_BITS = 15, 857 703 TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, ··· 860 706 }; 861 707 #endif 862 708 863 - /* The low-level tdefl functions below may be used directly if the above helper 864 - * functions aren't flexible enough. The low-level functions don't make any heap 865 - * allocations, unlike the above helper functions. */ 709 + /* The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The 710 + * low-level functions don't make any heap allocations, unlike the above helper functions. */ 866 711 typedef enum { 867 712 TDEFL_STATUS_BAD_PARAM = -2, 868 713 TDEFL_STATUS_PUT_BUF_FAILED = -1, ··· 902 747 } tdefl_compressor; 903 748 904 749 /* Initializes the compressor. */ 905 - /* There is no corresponding deinit() function because the tdefl API's do not 906 - * dynamically allocate memory. */ 907 - /* pBut_buf_func: If NULL, output data will be supplied to the specified 908 - * callback. In this case, the user should call the tdefl_compress_buffer() API 909 - * for compression. */ 910 - /* If pBut_buf_func is NULL the user should always call the tdefl_compress() 911 - * API. */ 912 - /* flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, 913 - * etc.) */ 750 + /* There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory. */ 751 + /* pBut_buf_func: If NULL, output data will be supplied to the specified callback. In this case, the user should call 752 + * the tdefl_compress_buffer() API for compression. */ 753 + /* If pBut_buf_func is NULL the user should always call the tdefl_compress() API. */ 754 + /* flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.) */ 914 755 MINIZ_EXPORT tdefl_status tdefl_init(tdefl_compressor* d, tdefl_put_buf_func_ptr pPut_buf_func, void* pPut_buf_user, 915 756 int flags); 916 757 917 - /* Compresses a block of data, consuming as much of the specified input buffer 918 - * as possible, and writing as much compressed data to the specified output 919 - * buffer as possible. */ 758 + /* Compresses a block of data, consuming as much of the specified input buffer as possible, and writing as much 759 + * compressed data to the specified output buffer as possible. */ 920 760 MINIZ_EXPORT tdefl_status tdefl_compress(tdefl_compressor* d, const void* pIn_buf, size_t* pIn_buf_size, void* pOut_buf, 921 761 size_t* pOut_buf_size, tdefl_flush flush); 922 762 923 - /* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a 924 - * non-NULL tdefl_put_buf_func_ptr. */ 763 + /* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr. */ 925 764 /* tdefl_compress_buffer() always consumes the entire input buffer. */ 926 765 MINIZ_EXPORT tdefl_status tdefl_compress_buffer(tdefl_compressor* d, const void* pIn_buf, size_t in_buf_size, 927 766 tdefl_flush flush); ··· 930 769 MINIZ_EXPORT mz_uint32 tdefl_get_adler32(tdefl_compressor* d); 931 770 932 771 /* Create tdefl_compress() flags given zlib-style compression parameters. */ 933 - /* level may range from [0,10] (where 10 is absolute max compression, but may be 934 - * much slower on some files) */ 772 + /* level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files) */ 935 773 /* window_bits may be -15 (raw deflate) or 15 (zlib) */ 936 - /* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, 937 - * MZ_RLE, or MZ_FIXED */ 774 + /* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED */ 938 775 MINIZ_EXPORT mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy); 939 776 940 777 #ifndef MINIZ_NO_MALLOC ··· 948 785 #ifdef __cplusplus 949 786 } 950 787 #endif 951 - 952 - #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ 953 788 #pragma once 954 789 955 790 /* ------------------- Low-level Decompression API Definitions */ 956 791 957 - #ifndef MINIZ_NO_INFLATE_APIS 958 - 959 792 #ifdef __cplusplus 960 793 extern "C" { 961 794 #endif 962 795 /* Decompression flags used by tinfl_decompress(). */ 963 - /* TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and 964 - * ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the 965 - * input is a raw deflate stream. */ 966 - /* TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available 967 - * beyond the end of the supplied input buffer. If clear, the input buffer 968 - * contains all remaining input. */ 969 - /* TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large 970 - * enough to hold the entire decompressed stream. If clear, the output buffer is 971 - * at least the size of the dictionary (typically 32KB). */ 972 - /* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the 973 - * decompressed bytes. */ 796 + /* TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a 797 + * valid zlib stream). Otherwise, the input is a raw deflate stream. */ 798 + /* TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. 799 + * If clear, the input buffer contains all remaining input. */ 800 + /* TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed 801 + * stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB). */ 802 + /* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. */ 974 803 enum { 975 804 TINFL_FLAG_PARSE_ZLIB_HEADER = 1, 976 805 TINFL_FLAG_HAS_MORE_INPUT = 2, ··· 979 808 }; 980 809 981 810 /* High level decompression functions: */ 982 - /* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block 983 - * allocated via malloc(). */ 811 + /* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). */ 984 812 /* On entry: */ 985 - /* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data 986 - * to decompress. */ 813 + /* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. */ 987 814 /* On return: */ 988 815 /* Function returns a pointer to the decompressed data, or NULL on failure. */ 989 - /* *pOut_len will be set to the decompressed data's size, which could be larger 990 - * than src_buf_len on uncompressible data. */ 991 - /* The caller must call mz_free() on the returned block when it's no longer 992 - * needed. */ 816 + /* *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible 817 + * data. */ 818 + /* The caller must call mz_free() on the returned block when it's no longer needed. */ 993 819 MINIZ_EXPORT void* tinfl_decompress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags); 994 820 995 - /* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block 996 - * in memory. */ 997 - /* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes 998 - * written on success. */ 821 + /* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. */ 822 + /* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. */ 999 823 #define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) 1000 824 MINIZ_EXPORT size_t tinfl_decompress_mem_to_mem(void* pOut_buf, size_t out_buf_len, const void* pSrc_buf, 1001 825 size_t src_buf_len, int flags); 1002 826 1003 - /* tinfl_decompress_mem_to_callback() decompresses a block in memory to an 1004 - * internal 32KB buffer, and a user provided callback function will be called to 1005 - * flush the buffer. */ 827 + /* tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided 828 + * callback function will be called to flush the buffer. */ 1006 829 /* Returns 1 on success or 0 on failure. */ 1007 830 typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void* pUser); 1008 831 MINIZ_EXPORT int tinfl_decompress_mem_to_callback(const void* pIn_buf, size_t* pIn_buf_size, ··· 1024 847 1025 848 /* Return status. */ 1026 849 typedef enum { 1027 - /* This flags indicates the inflator needs 1 or more input bytes to make 1028 - forward progress, but the caller is indicating that no more are available. 1029 - The compressed data */ 1030 - /* is probably corrupted. If you call the inflator again with more bytes it'll 1031 - try to continue processing the input but this is a BAD sign (either the 1032 - data is corrupted or you called it incorrectly). */ 1033 - /* If you call it again with no input you'll just get 1034 - TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */ 850 + /* This flags indicates the inflator needs 1 or more input bytes to make forward progress, but the caller is 851 + indicating that no more are available. The compressed data */ 852 + /* is probably corrupted. If you call the inflator again with more bytes it'll try to continue processing the input 853 + but this is a BAD sign (either the data is corrupted or you called it incorrectly). */ 854 + /* If you call it again with no input you'll just get TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */ 1035 855 TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4, 1036 856 1037 - /* This flag indicates that one or more of the input parameters was obviously 1038 - bogus. (You can try calling it again, but if you get this error the calling 1039 - code is wrong.) */ 857 + /* This flag indicates that one or more of the input parameters was obviously bogus. (You can try calling it again, 858 + but if you get this error the calling code is wrong.) */ 1040 859 TINFL_STATUS_BAD_PARAM = -3, 1041 860 1042 - /* This flags indicate the inflator is finished but the adler32 check of the 1043 - uncompressed data didn't match. If you call it again it'll return 1044 - TINFL_STATUS_DONE. */ 861 + /* This flags indicate the inflator is finished but the adler32 check of the uncompressed data didn't match. If you 862 + call it again it'll return TINFL_STATUS_DONE. */ 1045 863 TINFL_STATUS_ADLER32_MISMATCH = -2, 1046 864 1047 - /* This flags indicate the inflator has somehow failed (bad code, corrupted 1048 - input, etc.). If you call it again without resetting via tinfl_init() it 1049 - it'll just keep on returning the same status failure code. */ 865 + /* This flags indicate the inflator has somehow failed (bad code, corrupted input, etc.). If you call it again without 866 + resetting via tinfl_init() it it'll just keep on returning the same status failure code. */ 1050 867 TINFL_STATUS_FAILED = -1, 1051 868 1052 869 /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */ 1053 870 1054 - /* This flag indicates the inflator has returned every byte of uncompressed 1055 - data that it can, has consumed every byte that it needed, has successfully 1056 - reached the end of the deflate stream, and */ 1057 - /* if zlib headers and adler32 checking enabled that it has successfully 1058 - checked the uncompressed data's adler32. If you call it again you'll just 1059 - get TINFL_STATUS_DONE over and over again. */ 871 + /* This flag indicates the inflator has returned every byte of uncompressed data that it can, has consumed every byte 872 + that it needed, has successfully reached the end of the deflate stream, and */ 873 + /* if zlib headers and adler32 checking enabled that it has successfully checked the uncompressed data's adler32. If 874 + you call it again you'll just get TINFL_STATUS_DONE over and over again. */ 1060 875 TINFL_STATUS_DONE = 0, 1061 876 1062 - /* This flag indicates the inflator MUST have more input data (even 1 byte) 1063 - before it can make any more forward progress, or you need to clear the 1064 - TINFL_FLAG_HAS_MORE_INPUT */ 1065 - /* flag on the next call if you don't have any more source data. If the source 1066 - data was somehow corrupted it's also possible (but unlikely) for the 1067 - inflator to keep on demanding input to */ 877 + /* This flag indicates the inflator MUST have more input data (even 1 byte) before it can make any more forward 878 + progress, or you need to clear the TINFL_FLAG_HAS_MORE_INPUT */ 879 + /* flag on the next call if you don't have any more source data. If the source data was somehow corrupted it's also 880 + possible (but unlikely) for the inflator to keep on demanding input to */ 1068 881 /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */ 1069 882 TINFL_STATUS_NEEDS_MORE_INPUT = 1, 1070 883 1071 - /* This flag indicates the inflator definitely has 1 or more bytes of 1072 - uncompressed data available, but it cannot write this data into the output 1073 - buffer. */ 1074 - /* Note if the source compressed data was corrupted it's possible for the 1075 - inflator to return a lot of uncompressed data to the caller. I've been 1076 - assuming you know how much uncompressed data to expect */ 1077 - /* (either exact or worst case) and will stop calling the inflator and fail 1078 - after receiving too much. In pure streaming scenarios where you have no 1079 - idea how many bytes to expect this may not be possible */ 884 + /* This flag indicates the inflator definitely has 1 or more bytes of uncompressed data available, but it cannot write 885 + this data into the output buffer. */ 886 + /* Note if the source compressed data was corrupted it's possible for the inflator to return a lot of uncompressed 887 + data to the caller. I've been assuming you know how much uncompressed data to expect */ 888 + /* (either exact or worst case) and will stop calling the inflator and fail after receiving too much. In pure 889 + streaming scenarios where you have no idea how many bytes to expect this may not be possible */ 1080 890 /* so I may need to add some code to address this. */ 1081 891 TINFL_STATUS_HAS_MORE_OUTPUT = 2 1082 892 } tinfl_status; ··· 1089 899 MZ_MACRO_END 1090 900 #define tinfl_get_adler32(r) (r)->m_check_adler32 1091 901 1092 - /* Main low-level decompressor coroutine function. This is the only function 1093 - * actually needed for decompression. All the other functions are just 1094 - * high-level helpers for improved usability. */ 1095 - /* This is a universal API, i.e. it can be used as a building block to build any 1096 - * desired higher level decompression API. In the limit case, it can be called 1097 - * once per every byte input or output. */ 902 + /* Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the 903 + * other functions are just high-level helpers for improved usability. */ 904 + /* This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. 905 + * In the limit case, it can be called once per every byte input or output. */ 1098 906 MINIZ_EXPORT tinfl_status tinfl_decompress(tinfl_decompressor* r, const mz_uint8* pIn_buf_next, size_t* pIn_buf_size, 1099 907 mz_uint8* pOut_buf_start, mz_uint8* pOut_buf_next, size_t* pOut_buf_size, 1100 908 const mz_uint32 decomp_flags); ··· 1108 916 TINFL_FAST_LOOKUP_BITS = 10, 1109 917 TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS 1110 918 }; 919 + 920 + typedef struct { 921 + mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0]; 922 + mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; 923 + } tinfl_huff_table; 1111 924 1112 925 #if MINIZ_HAS_64BIT_REGISTERS 1113 926 #define TINFL_USE_64BIT_BITBUF 1 ··· 1128 941 m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES]; 1129 942 tinfl_bit_buf_t m_bit_buf; 1130 943 size_t m_dist_from_out_buf_start; 1131 - mz_int16 m_look_up[TINFL_MAX_HUFF_TABLES][TINFL_FAST_LOOKUP_SIZE]; 1132 - mz_int16 m_tree_0[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; 1133 - mz_int16 m_tree_1[TINFL_MAX_HUFF_SYMBOLS_1 * 2]; 1134 - mz_int16 m_tree_2[TINFL_MAX_HUFF_SYMBOLS_2 * 2]; 1135 - mz_uint8 m_code_size_0[TINFL_MAX_HUFF_SYMBOLS_0]; 1136 - mz_uint8 m_code_size_1[TINFL_MAX_HUFF_SYMBOLS_1]; 1137 - mz_uint8 m_code_size_2[TINFL_MAX_HUFF_SYMBOLS_2]; 944 + tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; 1138 945 mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; 1139 946 }; 1140 947 ··· 1142 949 } 1143 950 #endif 1144 951 1145 - #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ 1146 - 1147 952 #pragma once 1148 953 1149 954 /* ------------------- ZIP archive reading/writing */ ··· 1155 960 #endif 1156 961 1157 962 enum { 1158 - /* Note: These enums can be reduced as needed to save memory or stack space - 1159 - they are pretty conservative. */ 963 + /* Note: These enums can be reduced as needed to save memory or stack space - they are pretty conservative. */ 1160 964 MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024, 1161 965 MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512, 1162 966 MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512 ··· 1166 970 /* Central directory file index. */ 1167 971 mz_uint32 m_file_index; 1168 972 1169 - /* Byte offset of this entry in the archive's central directory. Note we 1170 - * currently only support up to UINT_MAX or less bytes in the central dir. */ 973 + /* Byte offset of this entry in the archive's central directory. Note we currently only support up to UINT_MAX or less 974 + * bytes in the central dir. */ 1171 975 mz_uint64 m_central_dir_ofs; 1172 976 1173 977 /* These fields are copied directly from the zip's central dir. */ ··· 1176 980 mz_uint16 m_bit_flag; 1177 981 mz_uint16 m_method; 1178 982 983 + #ifndef MINIZ_NO_TIME 984 + MZ_TIME_T m_time; 985 + #endif 986 + 1179 987 /* CRC-32 of uncompressed data. */ 1180 988 mz_uint32 m_crc32; 1181 989 1182 990 /* File's compressed size. */ 1183 991 mz_uint64 m_comp_size; 1184 992 1185 - /* File's uncompressed size. Note, I've seen some old archives where directory 1186 - * entries had 512 bytes for their uncompressed sizes, but when you try to 1187 - * unpack them you actually get 0 bytes. */ 993 + /* File's uncompressed size. Note, I've seen some old archives where directory entries had 512 bytes for their 994 + * uncompressed sizes, but when you try to unpack them you actually get 0 bytes. */ 1188 995 mz_uint64 m_uncomp_size; 1189 996 1190 997 /* Zip internal and external file attributes. */ ··· 1200 1007 /* MZ_TRUE if the entry appears to be a directory. */ 1201 1008 mz_bool m_is_directory; 1202 1009 1203 - /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip 1204 - * doesn't support) */ 1010 + /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip doesn't support) */ 1205 1011 mz_bool m_is_encrypted; 1206 1012 1207 - /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a 1208 - * compression method we support. */ 1013 + /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a compression method we support. */ 1209 1014 mz_bool m_is_supported; 1210 1015 1211 1016 /* Filename. If string ends in '/' it's a subdirectory entry. */ ··· 1216 1021 /* Guaranteed to be zero terminated, may be truncated to fit. */ 1217 1022 char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE]; 1218 1023 1219 - #ifdef MINIZ_NO_TIME 1220 - MZ_TIME_T m_padding; 1221 - #else 1222 - MZ_TIME_T m_time; 1223 - #endif 1224 1024 } mz_zip_archive_file_stat; 1225 1025 1226 1026 typedef size_t (*mz_file_read_func)(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n); ··· 1242 1042 MZ_ZIP_FLAG_IGNORE_PATH = 0x0200, 1243 1043 MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400, 1244 1044 MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800, 1245 - MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG = 0x1000, /* if enabled, mz_zip_reader_locate_file() will be called on each 1246 - file as its validated to ensure the func finds the file in the 1247 - central dir (intended for testing) */ 1248 - MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY = 0x2000, /* validate the local headers, but don't decompress the entire 1249 - file and check the crc32 */ 1250 - MZ_ZIP_FLAG_WRITE_ZIP64 = 0x4000, /* always use the zip64 file format, instead of the original zip 1251 - file format with automatic switch to zip64. Use as flags 1252 - parameter with mz_zip_writer_init*_v2 */ 1045 + MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG = 1046 + 0x1000, /* if enabled, mz_zip_reader_locate_file() will be called on each file as its validated to ensure the func 1047 + finds the file in the central dir (intended for testing) */ 1048 + MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY = 1049 + 0x2000, /* validate the local headers, but don't decompress the entire file and check the crc32 */ 1050 + MZ_ZIP_FLAG_WRITE_ZIP64 = 0x4000, /* always use the zip64 file format, instead of the original zip file format with 1051 + automatic switch to zip64. Use as flags parameter with mz_zip_writer_init*_v2 */ 1253 1052 MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000, 1254 1053 MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000, 1255 1054 /*After adding a compressed file, seek back 1256 1055 to local file header and set the correct sizes*/ 1257 - MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000, 1258 - MZ_ZIP_FLAG_READ_ALLOW_WRITING = 0x40000 1056 + MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000 1259 1057 } mz_zip_flags; 1260 1058 1261 1059 typedef enum { ··· 1268 1066 MZ_ZIP_TOTAL_TYPES 1269 1067 } mz_zip_type; 1270 1068 1271 - /* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or 1272 - * modify this enum. */ 1069 + /* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or modify this enum. */ 1273 1070 typedef enum { 1274 1071 MZ_ZIP_NO_ERROR = 0, 1275 1072 MZ_ZIP_UNDEFINED_ERROR, ··· 1337 1134 mz_uint flags; 1338 1135 1339 1136 int status; 1340 - 1137 + #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 1138 + mz_uint file_crc32; 1139 + #endif 1341 1140 mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining, out_buf_ofs, cur_file_ofs; 1342 1141 mz_zip_archive_file_stat file_stat; 1343 1142 void* pRead_buf; ··· 1347 1146 1348 1147 tinfl_decompressor inflator; 1349 1148 1350 - #ifdef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 1351 - mz_uint padding; 1352 - #else 1353 - mz_uint file_crc32; 1354 - #endif 1355 - 1356 1149 } mz_zip_reader_extract_iter_state; 1357 1150 1358 1151 /* -------- ZIP reading */ ··· 1366 1159 #ifndef MINIZ_NO_STDIO 1367 1160 /* Read a archive from a disk file. */ 1368 1161 /* file_start_ofs is the file offset where the archive actually begins, or 0. */ 1369 - /* actual_archive_size is the true total size of the archive, which may be 1370 - * smaller than the file's actual size on disk. If zero the entire file is 1371 - * treated as the archive. */ 1162 + /* actual_archive_size is the true total size of the archive, which may be smaller than the file's actual size on disk. 1163 + * If zero the entire file is treated as the archive. */ 1372 1164 MINIZ_EXPORT mz_bool mz_zip_reader_init_file(mz_zip_archive* pZip, const char* pFilename, mz_uint32 flags); 1373 1165 MINIZ_EXPORT mz_bool mz_zip_reader_init_file_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint flags, 1374 1166 mz_uint64 file_start_ofs, mz_uint64 archive_size); 1375 1167 1376 - /* Read an archive from an already opened FILE, beginning at the current file 1377 - * position. */ 1378 - /* The archive is assumed to be archive_size bytes long. If archive_size is 0, 1379 - * then the entire rest of the file is assumed to contain the archive. */ 1168 + /* Read an archive from an already opened FILE, beginning at the current file position. */ 1169 + /* The archive is assumed to be archive_size bytes long. If archive_size is 0, then the entire rest of the file is 1170 + * assumed to contain the archive. */ 1380 1171 /* The FILE will NOT be closed when mz_zip_reader_end() is called. */ 1381 1172 MINIZ_EXPORT mz_bool mz_zip_reader_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint64 archive_size, 1382 1173 mz_uint flags); 1383 1174 #endif 1384 1175 1385 - /* Ends archive reading, freeing all allocations, and closing the input archive 1386 - * file if mz_zip_reader_init_file() was used. */ 1176 + /* Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was 1177 + * used. */ 1387 1178 MINIZ_EXPORT mz_bool mz_zip_reader_end(mz_zip_archive* pZip); 1388 1179 1389 1180 /* -------- ZIP reading or writing */ 1390 1181 1391 1182 /* Clears a mz_zip_archive struct to all zeros. */ 1392 - /* Important: This must be done before passing the struct to any mz_zip 1393 - * functions. */ 1183 + /* Important: This must be done before passing the struct to any mz_zip functions. */ 1394 1184 MINIZ_EXPORT void mz_zip_zero_struct(mz_zip_archive* pZip); 1395 1185 1396 1186 MINIZ_EXPORT mz_zip_mode mz_zip_get_mode(mz_zip_archive* pZip); ··· 1403 1193 MINIZ_EXPORT mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive* pZip); 1404 1194 MINIZ_EXPORT MZ_FILE* mz_zip_get_cfile(mz_zip_archive* pZip); 1405 1195 1406 - /* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf. 1407 - */ 1196 + /* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf. */ 1408 1197 MINIZ_EXPORT size_t mz_zip_read_archive_data(mz_zip_archive* pZip, mz_uint64 file_ofs, void* pBuf, size_t n); 1409 1198 1410 - /* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct. 1411 - * These functions retrieve/manipulate this field. */ 1199 + /* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct. These functions retrieve/manipulate this 1200 + * field. */ 1412 1201 /* Note that the m_last_error functionality is not thread safe. */ 1413 1202 MINIZ_EXPORT mz_zip_error mz_zip_set_last_error(mz_zip_archive* pZip, mz_zip_error err_num); 1414 1203 MINIZ_EXPORT mz_zip_error mz_zip_peek_last_error(mz_zip_archive* pZip); ··· 1422 1211 /* MZ_TRUE if the file is encrypted/strong encrypted. */ 1423 1212 MINIZ_EXPORT mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive* pZip, mz_uint file_index); 1424 1213 1425 - /* MZ_TRUE if the compression method is supported, and the file is not 1426 - * encrypted, and the file is not a compressed patch file. */ 1214 + /* MZ_TRUE if the compression method is supported, and the file is not encrypted, and the file is not a compressed patch 1215 + * file. */ 1427 1216 MINIZ_EXPORT mz_bool mz_zip_reader_is_file_supported(mz_zip_archive* pZip, mz_uint file_index); 1428 1217 1429 1218 /* Retrieves the filename of an archive file entry. */ 1430 - /* Returns the number of bytes written to pFilename, or if filename_buf_size is 1431 - * 0 this function returns the number of bytes needed to fully store the 1432 - * filename. */ 1219 + /* Returns the number of bytes written to pFilename, or if filename_buf_size is 0 this function returns the number of 1220 + * bytes needed to fully store the filename. */ 1433 1221 MINIZ_EXPORT mz_uint mz_zip_reader_get_filename(mz_zip_archive* pZip, mz_uint file_index, char* pFilename, 1434 1222 mz_uint filename_buf_size); 1435 1223 ··· 1445 1233 MINIZ_EXPORT mz_bool mz_zip_reader_file_stat(mz_zip_archive* pZip, mz_uint file_index, mz_zip_archive_file_stat* pStat); 1446 1234 1447 1235 /* MZ_TRUE if the file is in zip64 format. */ 1448 - /* A file is considered zip64 if it contained a zip64 end of central directory 1449 - * marker, or if it contained any zip64 extended file information fields in the 1450 - * central directory. */ 1236 + /* A file is considered zip64 if it contained a zip64 end of central directory marker, or if it contained any zip64 1237 + * extended file information fields in the central directory. */ 1451 1238 MINIZ_EXPORT mz_bool mz_zip_is_zip64(mz_zip_archive* pZip); 1452 1239 1453 1240 /* Returns the total central directory size in bytes. */ ··· 1455 1242 MINIZ_EXPORT size_t mz_zip_get_central_dir_size(mz_zip_archive* pZip); 1456 1243 1457 1244 /* Extracts a archive file to a memory buffer using no memory allocation. */ 1458 - /* There must be at least enough room on the stack to store the inflator's state 1459 - * (~34KB or so). */ 1245 + /* There must be at least enough room on the stack to store the inflator's state (~34KB or so). */ 1460 1246 MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, 1461 1247 size_t buf_size, mz_uint flags, void* pUser_read_buf, 1462 1248 size_t user_read_buf_size); ··· 1471 1257 size_t buf_size, mz_uint flags); 1472 1258 1473 1259 /* Extracts a archive file to a dynamically allocated heap buffer. */ 1474 - /* The memory will be allocated via the mz_zip_archive's alloc/realloc 1475 - * functions. */ 1260 + /* The memory will be allocated via the mz_zip_archive's alloc/realloc functions. */ 1476 1261 /* Returns NULL and sets the last error on failure. */ 1477 1262 MINIZ_EXPORT void* mz_zip_reader_extract_to_heap(mz_zip_archive* pZip, mz_uint file_index, size_t* pSize, 1478 1263 mz_uint flags); 1479 1264 MINIZ_EXPORT void* mz_zip_reader_extract_file_to_heap(mz_zip_archive* pZip, const char* pFilename, size_t* pSize, 1480 1265 mz_uint flags); 1481 1266 1482 - /* Extracts a archive file using a callback function to output the file's data. 1483 - */ 1267 + /* Extracts a archive file using a callback function to output the file's data. */ 1484 1268 MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive* pZip, mz_uint file_index, 1485 1269 mz_file_write_func pCallback, void* pOpaque, mz_uint flags); 1486 1270 MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive* pZip, const char* pFilename, ··· 1497 1281 MINIZ_EXPORT mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState); 1498 1282 1499 1283 #ifndef MINIZ_NO_STDIO 1500 - /* Extracts a archive file to a disk file and sets its last accessed and 1501 - * modified times. */ 1284 + /* Extracts a archive file to a disk file and sets its last accessed and modified times. */ 1502 1285 /* This function only extracts files, not archive directory records. */ 1503 1286 MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_file(mz_zip_archive* pZip, mz_uint file_index, const char* pDst_filename, 1504 1287 mz_uint flags); 1505 1288 MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive* pZip, const char* pArchive_filename, 1506 1289 const char* pDst_filename, mz_uint flags); 1507 1290 1508 - /* Extracts a archive file starting at the current position in the destination 1509 - * FILE stream. */ 1291 + /* Extracts a archive file starting at the current position in the destination FILE stream. */ 1510 1292 MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive* pZip, mz_uint file_index, MZ_FILE* File, 1511 1293 mz_uint flags); 1512 1294 MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive* pZip, const char* pArchive_filename, ··· 1517 1299 /* TODO */ 1518 1300 typedef void *mz_zip_streaming_extract_state_ptr; 1519 1301 mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags); 1520 - mz_uint64 mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); 1521 - mz_uint64 mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); 1522 - mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, mz_uint64 new_ofs); 1302 + uint64_t mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); 1303 + uint64_t mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); 1304 + mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, uint64_t new_ofs); 1523 1305 size_t mz_zip_streaming_extract_read(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, void *pBuf, size_t buf_size); 1524 1306 mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); 1525 1307 #endif 1526 1308 1527 - /* This function compares the archive's local headers, the optional local zip64 1528 - * extended information block, and the optional descriptor following the 1529 - * compressed data vs. the data in the central directory. */ 1530 - /* It also validates that each file can be successfully uncompressed unless the 1531 - * MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY is specified. */ 1309 + /* This function compares the archive's local headers, the optional local zip64 extended information block, and the 1310 + * optional descriptor following the compressed data vs. the data in the central directory. */ 1311 + /* It also validates that each file can be successfully uncompressed unless the MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY is 1312 + * specified. */ 1532 1313 MINIZ_EXPORT mz_bool mz_zip_validate_file(mz_zip_archive* pZip, mz_uint file_index, mz_uint flags); 1533 1314 1534 - /* Validates an entire archive by calling mz_zip_validate_file() on each file. 1535 - */ 1315 + /* Validates an entire archive by calling mz_zip_validate_file() on each file. */ 1536 1316 MINIZ_EXPORT mz_bool mz_zip_validate_archive(mz_zip_archive* pZip, mz_uint flags); 1537 1317 1538 1318 /* Misc utils/helpers, valid for ZIP reading or writing */ 1539 1319 MINIZ_EXPORT mz_bool mz_zip_validate_mem_archive(const void* pMem, size_t size, mz_uint flags, mz_zip_error* pErr); 1540 - #ifndef MINIZ_NO_STDIO 1541 1320 MINIZ_EXPORT mz_bool mz_zip_validate_file_archive(const char* pFilename, mz_uint flags, mz_zip_error* pErr); 1542 - #endif 1543 1321 1544 - /* Universal end function - calls either mz_zip_reader_end() or 1545 - * mz_zip_writer_end(). */ 1322 + /* Universal end function - calls either mz_zip_reader_end() or mz_zip_writer_end(). */ 1546 1323 MINIZ_EXPORT mz_bool mz_zip_end(mz_zip_archive* pZip); 1547 1324 1548 1325 /* -------- ZIP writing */ ··· 1550 1327 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 1551 1328 1552 1329 /* Inits a ZIP archive writer. */ 1553 - /*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init 1554 - * or mz_zip_writer_init_v2*/ 1555 - /*The output is streamable, i.e. file_ofs in mz_file_write_func always increases 1556 - * only by n*/ 1330 + /*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init or mz_zip_writer_init_v2*/ 1331 + /*The output is streamable, i.e. file_ofs in mz_file_write_func always increases only by n*/ 1557 1332 MINIZ_EXPORT mz_bool mz_zip_writer_init(mz_zip_archive* pZip, mz_uint64 existing_size); 1558 1333 MINIZ_EXPORT mz_bool mz_zip_writer_init_v2(mz_zip_archive* pZip, mz_uint64 existing_size, mz_uint flags); 1559 1334 ··· 1563 1338 size_t initial_allocation_size, mz_uint flags); 1564 1339 1565 1340 #ifndef MINIZ_NO_STDIO 1566 - MINIZ_EXPORT 1567 - mz_bool mz_zip_writer_init_file(mz_zip_archive* pZip, const char* pFilename, mz_uint64 size_to_reserve_at_beginning); 1341 + MINIZ_EXPORT mz_bool mz_zip_writer_init_file(mz_zip_archive* pZip, const char* pFilename, 1342 + mz_uint64 size_to_reserve_at_beginning); 1568 1343 MINIZ_EXPORT mz_bool mz_zip_writer_init_file_v2(mz_zip_archive* pZip, const char* pFilename, 1569 1344 mz_uint64 size_to_reserve_at_beginning, mz_uint flags); 1570 1345 MINIZ_EXPORT mz_bool mz_zip_writer_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint flags); 1571 1346 #endif 1572 1347 1573 - /* Converts a ZIP archive reader object into a writer object, to allow efficient 1574 - * in-place file appends to occur on an existing archive. */ 1575 - /* For archives opened using mz_zip_reader_init_file, pFilename must be the 1576 - * archive's filename so it can be reopened for writing. If the file can't be 1577 - * reopened, mz_zip_reader_end() will be called. */ 1578 - /* For archives opened using mz_zip_reader_init_mem, the memory block must be 1579 - * growable using the realloc callback (which defaults to realloc unless you've 1580 - * overridden it). */ 1581 - /* Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's 1582 - * user provided m_pWrite function cannot be NULL. */ 1583 - /* Note: In-place archive modification is not recommended unless you know what 1584 - * you're doing, because if execution stops or something goes wrong before */ 1348 + /* Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an 1349 + * existing archive. */ 1350 + /* For archives opened using mz_zip_reader_init_file, pFilename must be the archive's filename so it can be reopened for 1351 + * writing. If the file can't be reopened, mz_zip_reader_end() will be called. */ 1352 + /* For archives opened using mz_zip_reader_init_mem, the memory block must be growable using the realloc callback (which 1353 + * defaults to realloc unless you've overridden it). */ 1354 + /* Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be 1355 + * NULL. */ 1356 + /* Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops 1357 + * or something goes wrong before */ 1585 1358 /* the archive is finalized the file's central directory will be hosed. */ 1586 1359 MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader(mz_zip_archive* pZip, const char* pFilename); 1587 1360 MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint flags); 1588 1361 1589 - /* Adds the contents of a memory buffer to an archive. These functions record 1590 - * the current local time into the archive. */ 1591 - /* To add a directory entry, call this method with an archive name ending in a 1592 - * forwardslash with an empty buffer. */ 1593 - /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, 1594 - * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or 1595 - * just set to MZ_DEFAULT_COMPRESSION. */ 1362 + /* Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive. 1363 + */ 1364 + /* To add a directory entry, call this method with an archive name ending in a forwardslash with an empty buffer. */ 1365 + /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or 1366 + * more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. */ 1596 1367 MINIZ_EXPORT mz_bool mz_zip_writer_add_mem(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, 1597 1368 size_t buf_size, mz_uint level_and_flags); 1598 1369 1599 - /* Like mz_zip_writer_add_mem(), except you can specify a file comment field, 1600 - * and optionally supply the function with already compressed data. */ 1601 - /* uncomp_size/uncomp_crc32 are only used if the MZ_ZIP_FLAG_COMPRESSED_DATA 1602 - * flag is specified. */ 1370 + /* Like mz_zip_writer_add_mem(), except you can specify a file comment field, and optionally supply the function with 1371 + * already compressed data. */ 1372 + /* uncomp_size/uncomp_crc32 are only used if the MZ_ZIP_FLAG_COMPRESSED_DATA flag is specified. */ 1603 1373 MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, 1604 1374 size_t buf_size, const void* pComment, mz_uint16 comment_size, 1605 1375 mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32); ··· 1611 1381 mz_uint user_extra_data_local_len, const char* user_extra_data_central, 1612 1382 mz_uint user_extra_data_central_len); 1613 1383 1614 - /* Adds the contents of a file to an archive. This function also records the 1615 - * disk file's modified time into the archive. */ 1616 - /* File data is supplied via a read callback function. User 1617 - * mz_zip_writer_add_(c)file to add a file directly.*/ 1384 + /* Adds the contents of a file to an archive. This function also records the disk file's modified time into the archive. 1385 + */ 1386 + /* File data is supplied via a read callback function. User mz_zip_writer_add_(c)file to add a file directly.*/ 1618 1387 MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback( 1619 1388 mz_zip_archive* pZip, const char* pArchive_name, mz_file_read_func read_callback, void* callback_opaque, 1620 1389 mz_uint64 max_size, const MZ_TIME_T* pFile_time, const void* pComment, mz_uint16 comment_size, ··· 1622 1391 const char* user_extra_data_central, mz_uint user_extra_data_central_len); 1623 1392 1624 1393 #ifndef MINIZ_NO_STDIO 1625 - /* Adds the contents of a disk file to an archive. This function also records 1626 - * the disk file's modified time into the archive. */ 1627 - /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, 1628 - * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or 1629 - * just set to MZ_DEFAULT_COMPRESSION. */ 1394 + /* Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the 1395 + * archive. */ 1396 + /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or 1397 + * more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. */ 1630 1398 MINIZ_EXPORT mz_bool mz_zip_writer_add_file(mz_zip_archive* pZip, const char* pArchive_name, const char* pSrc_filename, 1631 1399 const void* pComment, mz_uint16 comment_size, mz_uint level_and_flags); 1632 1400 1633 - /* Like mz_zip_writer_add_file(), except the file data is read from the 1634 - * specified FILE stream. */ 1401 + /* Like mz_zip_writer_add_file(), except the file data is read from the specified FILE stream. */ 1635 1402 MINIZ_EXPORT mz_bool mz_zip_writer_add_cfile(mz_zip_archive* pZip, const char* pArchive_name, MZ_FILE* pSrc_file, 1636 1403 mz_uint64 max_size, const MZ_TIME_T* pFile_time, const void* pComment, 1637 1404 mz_uint16 comment_size, mz_uint level_and_flags, ··· 1640 1407 #endif 1641 1408 1642 1409 /* Adds a file to an archive by fully cloning the data from another archive. */ 1643 - /* This function fully clones the source file's compressed data (no 1644 - * recompression), along with its full filename, extra data (it may add or 1645 - * modify the zip64 local header extra data field), and the optional descriptor 1646 - * following the compressed data. */ 1410 + /* This function fully clones the source file's compressed data (no recompression), along with its full filename, extra 1411 + * data (it may add or modify the zip64 local header extra data field), and the optional descriptor following the 1412 + * compressed data. */ 1647 1413 MINIZ_EXPORT mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive* pZip, mz_zip_archive* pSource_zip, 1648 1414 mz_uint src_file_index); 1649 1415 1650 - /* Finalizes the archive by writing the central directory records followed by 1651 - * the end of central directory record. */ 1652 - /* After an archive is finalized, the only valid call on the mz_zip_archive 1653 - * struct is mz_zip_writer_end(). */ 1654 - /* An archive must be manually finalized by calling this function for it to be 1655 - * valid. */ 1416 + /* Finalizes the archive by writing the central directory records followed by the end of central directory record. */ 1417 + /* After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end(). */ 1418 + /* An archive must be manually finalized by calling this function for it to be valid. */ 1656 1419 MINIZ_EXPORT mz_bool mz_zip_writer_finalize_archive(mz_zip_archive* pZip); 1657 1420 1658 - /* Finalizes a heap archive, returning a pointer to the heap block and its size. 1659 - */ 1660 - /* The heap block will be allocated using the mz_zip_archive's alloc/realloc 1661 - * callbacks. */ 1421 + /* Finalizes a heap archive, returning a poiner to the heap block and its size. */ 1422 + /* The heap block will be allocated using the mz_zip_archive's alloc/realloc callbacks. */ 1662 1423 MINIZ_EXPORT mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive* pZip, void** ppBuf, size_t* pSize); 1663 1424 1664 - /* Ends archive writing, freeing all allocations, and closing the output file if 1665 - * mz_zip_writer_init_file() was used. */ 1666 - /* Note for the archive to be valid, it *must* have been finalized before ending 1667 - * (this function will not do it for you). */ 1425 + /* Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used. */ 1426 + /* Note for the archive to be valid, it *must* have been finalized before ending (this function will not do it for you). 1427 + */ 1668 1428 MINIZ_EXPORT mz_bool mz_zip_writer_end(mz_zip_archive* pZip); 1669 1429 1670 1430 /* -------- Misc. high-level helper functions: */ 1671 1431 1672 - /* mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) 1673 - * appends a memory blob to a ZIP archive. */ 1674 - /* Note this is NOT a fully safe operation. If it crashes or dies in some way 1675 - * your archive can be left in a screwed up state (without a central directory). 1676 - */ 1677 - /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, 1678 - * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or 1679 - * just set to MZ_DEFAULT_COMPRESSION. */ 1680 - /* TODO: Perhaps add an option to leave the existing central dir in place in 1681 - * case the add dies? We could then truncate the file (so the old central dir 1682 - * would be at the end) if something goes wrong. */ 1432 + /* mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive. */ 1433 + /* Note this is NOT a fully safe operation. If it crashes or dies in some way your archive can be left in a screwed up 1434 + * state (without a central directory). */ 1435 + /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or 1436 + * more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. */ 1437 + /* TODO: Perhaps add an option to leave the existing central dir in place in case the add dies? We could then truncate 1438 + * the file (so the old central dir would be at the end) if something goes wrong. */ 1683 1439 MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place(const char* pZip_filename, const char* pArchive_name, 1684 1440 const void* pBuf, size_t buf_size, const void* pComment, 1685 1441 mz_uint16 comment_size, mz_uint level_and_flags); ··· 1688 1444 mz_uint16 comment_size, mz_uint level_and_flags, 1689 1445 mz_zip_error* pErr); 1690 1446 1691 - #ifndef MINIZ_NO_STDIO 1692 1447 /* Reads a single file from an archive into a heap block. */ 1693 - /* If pComment is not NULL, only the file with the specified comment will be 1694 - * extracted. */ 1448 + /* If pComment is not NULL, only the file with the specified comment will be extracted. */ 1695 1449 /* Returns NULL on failure. */ 1696 1450 MINIZ_EXPORT void* mz_zip_extract_archive_file_to_heap(const char* pZip_filename, const char* pArchive_name, 1697 1451 size_t* pSize, mz_uint flags); 1698 1452 MINIZ_EXPORT void* mz_zip_extract_archive_file_to_heap_v2(const char* pZip_filename, const char* pArchive_name, 1699 1453 const char* pComment, size_t* pSize, mz_uint flags, 1700 1454 mz_zip_error* pErr); 1701 - #endif 1702 1455 1703 1456 #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */ 1704 1457
+1
src/screens/EpubReaderScreen.cpp
··· 135 135 } 136 136 } 137 137 138 + // TODO: Failure handling 138 139 void EpubReaderScreen::renderPage() { 139 140 if (!epub) { 140 141 return;