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.

chore: Remove miniz and modularise inflation logic (#1073)

## Summary

* Remove miniz and move completely to uzlib
* Move uzlib interfacing to InflateReader to better modularise inflation
code

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? Yes, Claude helped with
the extraction and refactor

authored by

Dave Allie and committed by
GitHub
ecb5b1b4 f28623da

+337 -8615
+4 -17
lib/EpdFont/FontDecompressor.cpp
··· 1 1 #include "FontDecompressor.h" 2 2 3 3 #include <Logging.h> 4 - #include <uzlib.h> 5 4 6 5 #include <cstdlib> 7 - #include <cstring> 8 6 9 7 bool FontDecompressor::init() { 10 8 clearCache(); 11 - memset(&decomp, 0, sizeof(decomp)); 12 9 return true; 13 10 } 14 11 ··· 82 79 return false; 83 80 } 84 81 85 - // Decompress using uzlib 86 - const uint8_t* inputBuf = &fontData->bitmap[group.compressedOffset]; 87 - 88 - uzlib_uncompress_init(&decomp, NULL, 0); 89 - decomp.source = inputBuf; 90 - decomp.source_limit = inputBuf + group.compressedSize; 91 - decomp.dest_start = outBuf; 92 - decomp.dest = outBuf; 93 - decomp.dest_limit = outBuf + group.uncompressedSize; 94 - 95 - int res = uzlib_uncompress(&decomp); 96 - 97 - if (res < 0 || decomp.dest != decomp.dest_limit) { 98 - LOG_ERR("FDC", "Decompression failed for group %u (status %d)", groupIndex, res); 82 + inflateReader.init(false); 83 + inflateReader.setSource(&fontData->bitmap[group.compressedOffset], group.compressedSize); 84 + if (!inflateReader.read(outBuf, group.uncompressedSize)) { 85 + LOG_ERR("FDC", "Decompression failed for group %u", groupIndex); 99 86 free(outBuf); 100 87 return false; 101 88 }
+2 -4
lib/EpdFont/FontDecompressor.h
··· 1 1 #pragma once 2 2 3 - #include <uzlib.h> 4 - 5 - #include <cstdint> 3 + #include <InflateReader.h> 6 4 7 5 #include "EpdFontData.h" 8 6 ··· 30 28 bool valid = false; 31 29 }; 32 30 33 - struct uzlib_uncomp decomp = {}; 31 + InflateReader inflateReader; 34 32 CacheEntry cache[CACHE_SLOTS] = {}; 35 33 uint32_t accessCounter = 0; 36 34
+78
lib/InflateReader/InflateReader.cpp
··· 1 + #include "InflateReader.h" 2 + 3 + #include <cstring> 4 + #include <type_traits> 5 + 6 + namespace { 7 + constexpr size_t INFLATE_DICT_SIZE = 32768; 8 + } 9 + 10 + // Guarantee the cast pattern in the header comment is valid. 11 + static_assert(std::is_standard_layout<InflateReader>::value, 12 + "InflateReader must be standard-layout for the uzlib callback cast to work"); 13 + 14 + InflateReader::~InflateReader() { deinit(); } 15 + 16 + bool InflateReader::init(const bool streaming) { 17 + deinit(); // free any previously allocated ring buffer and reset state 18 + 19 + if (streaming) { 20 + ringBuffer = static_cast<uint8_t*>(malloc(INFLATE_DICT_SIZE)); 21 + if (!ringBuffer) return false; 22 + memset(ringBuffer, 0, INFLATE_DICT_SIZE); 23 + } 24 + 25 + uzlib_uncompress_init(&decomp, ringBuffer, ringBuffer ? INFLATE_DICT_SIZE : 0); 26 + return true; 27 + } 28 + 29 + void InflateReader::deinit() { 30 + if (ringBuffer) { 31 + free(ringBuffer); 32 + ringBuffer = nullptr; 33 + } 34 + memset(&decomp, 0, sizeof(decomp)); 35 + } 36 + 37 + void InflateReader::setSource(const uint8_t* src, size_t len) { 38 + decomp.source = src; 39 + decomp.source_limit = src + len; 40 + } 41 + 42 + void InflateReader::setReadCallback(int (*cb)(struct uzlib_uncomp*)) { decomp.source_read_cb = cb; } 43 + 44 + void InflateReader::skipZlibHeader() { 45 + uzlib_get_byte(&decomp); 46 + uzlib_get_byte(&decomp); 47 + } 48 + 49 + bool InflateReader::read(uint8_t* dest, size_t len) { 50 + if (!ringBuffer) { 51 + // One-shot mode: back-references use absolute offset from dest_start. 52 + // Valid only when read() is called once with the full output buffer. 53 + decomp.dest_start = dest; 54 + } 55 + decomp.dest = dest; 56 + decomp.dest_limit = dest + len; 57 + 58 + const int res = uzlib_uncompress(&decomp); 59 + if (res < 0) return false; 60 + return decomp.dest == decomp.dest_limit; 61 + } 62 + 63 + InflateStatus InflateReader::readAtMost(uint8_t* dest, size_t maxLen, size_t* produced) { 64 + if (!ringBuffer) { 65 + // One-shot mode: back-references use absolute offset from dest_start. 66 + // Valid only when readAtMost() is called once with the full output buffer. 67 + decomp.dest_start = dest; 68 + } 69 + decomp.dest = dest; 70 + decomp.dest_limit = dest + maxLen; 71 + 72 + const int res = uzlib_uncompress(&decomp); 73 + *produced = static_cast<size_t>(decomp.dest - dest); 74 + 75 + if (res == TINF_DONE) return InflateStatus::Done; 76 + if (res < 0) return InflateStatus::Error; 77 + return InflateStatus::Ok; 78 + }
+85
lib/InflateReader/InflateReader.h
··· 1 + #pragma once 2 + 3 + #include <uzlib.h> 4 + 5 + #include <cstddef> 6 + 7 + // Return value for readAtMost(). 8 + enum class InflateStatus { 9 + Ok, // Output buffer full; more compressed data remains. 10 + Done, // Stream ended cleanly (TINF_DONE). produced may be < maxLen. 11 + Error, // Decompression failed. 12 + }; 13 + 14 + // Streaming deflate decompressor wrapping uzlib. 15 + // 16 + // Two modes: 17 + // init(false) — one-shot: input is a contiguous buffer, call read() once. 18 + // init(true) — streaming: allocates a 32KB ring buffer for back-references 19 + // across multiple read() / readAtMost() calls. 20 + // 21 + // Streaming callback pattern: 22 + // The uzlib read callback receives a `struct uzlib_uncomp*` with no separate 23 + // context pointer. To attach context, make InflateReader the *first member* of 24 + // your context struct, then cast inside the callback: 25 + // 26 + // struct MyCtx { 27 + // InflateReader reader; // must be first 28 + // FsFile* file; 29 + // // ... 30 + // }; 31 + // static int myCb(struct uzlib_uncomp* u) { 32 + // MyCtx* ctx = reinterpret_cast<MyCtx*>(u); // valid: reader.decomp is at offset 0 33 + // // ... fill u->source / u->source_limit, return first byte 34 + // } 35 + // MyCtx ctx; 36 + // ctx.reader.init(true); 37 + // ctx.reader.setReadCallback(myCb); 38 + // 39 + class InflateReader { 40 + public: 41 + InflateReader() = default; 42 + ~InflateReader(); 43 + 44 + InflateReader(const InflateReader&) = delete; 45 + InflateReader& operator=(const InflateReader&) = delete; 46 + 47 + // Initialise decompressor. streaming=true allocates a 32KB ring buffer needed 48 + // when read() or readAtMost() will be called multiple times. 49 + // Returns false only in streaming mode if the ring buffer allocation fails. 50 + bool init(bool streaming = false); 51 + 52 + // Release the ring buffer and reset internal state. 53 + void deinit(); 54 + 55 + // Set the entire compressed input as a contiguous memory buffer. 56 + // Used in one-shot mode; not needed when a read callback is set. 57 + void setSource(const uint8_t* src, size_t len); 58 + 59 + // Set a uzlib-compatible read callback for streaming input. 60 + // See class-level comment for the expected callback/context struct pattern. 61 + void setReadCallback(int (*cb)(uzlib_uncomp*)); 62 + 63 + // Consume the 2-byte zlib header (CMF + FLG) from the input stream. 64 + // Call this once before the first read() when input is zlib-wrapped (e.g. PNG IDAT). 65 + void skipZlibHeader(); 66 + 67 + // Decompress exactly len bytes into dest. 68 + // Returns false if the stream ends before producing len bytes, or on error. 69 + bool read(uint8_t* dest, size_t len); 70 + 71 + // Decompress up to maxLen bytes into dest. 72 + // Sets *produced to the number of bytes written. 73 + // Returns Done when the stream ends cleanly, Ok when there is more to read, 74 + // and Error on failure. 75 + InflateStatus readAtMost(uint8_t* dest, size_t maxLen, size_t* produced); 76 + 77 + // Returns a pointer to the underlying TINF_DATA. 78 + // Useful for advanced streaming setups where the callback needs access to the 79 + // uzlib struct directly (e.g. updating source/source_limit). 80 + uzlib_uncomp* raw() { return &decomp; } 81 + 82 + private: 83 + uzlib_uncomp decomp = {}; 84 + uint8_t* ringBuffer = nullptr; 85 + };
+91 -122
lib/PngToBmpConverter/PngToBmpConverter.cpp
··· 1 1 #include "PngToBmpConverter.h" 2 2 3 3 #include <HalStorage.h> 4 + #include <InflateReader.h> 4 5 #include <Logging.h> 5 - #include <miniz.h> 6 6 7 7 #include <cstdio> 8 8 #include <cstring> ··· 20 20 constexpr int TARGET_MAX_HEIGHT = 800; 21 21 // ============================================================================ 22 22 23 + // BMP writing helpers (same as JpegToBmpConverter) 24 + inline void write16(Print& out, const uint16_t value) { 25 + out.write(value & 0xFF); 26 + out.write((value >> 8) & 0xFF); 27 + } 28 + 29 + inline void write32(Print& out, const uint32_t value) { 30 + out.write(value & 0xFF); 31 + out.write((value >> 8) & 0xFF); 32 + out.write((value >> 16) & 0xFF); 33 + out.write((value >> 24) & 0xFF); 34 + } 35 + 36 + inline void write32Signed(Print& out, const int32_t value) { 37 + out.write(value & 0xFF); 38 + out.write((value >> 8) & 0xFF); 39 + out.write((value >> 16) & 0xFF); 40 + out.write((value >> 24) & 0xFF); 41 + } 42 + 43 + // Paeth predictor function per PNG spec 44 + inline uint8_t paethPredictor(uint8_t a, uint8_t b, uint8_t c) { 45 + int p = static_cast<int>(a) + b - c; 46 + int pa = p > a ? p - a : a - p; 47 + int pb = p > b ? p - b : b - p; 48 + int pc = p > c ? p - c : c - p; 49 + if (pa <= pb && pa <= pc) return a; 50 + if (pb <= pc) return b; 51 + return c; 52 + } 53 + 54 + namespace { 23 55 // PNG constants 24 - static constexpr uint8_t PNG_SIGNATURE[8] = {137, 80, 78, 71, 13, 10, 26, 10}; 56 + uint8_t PNG_SIGNATURE[8] = {137, 80, 78, 71, 13, 10, 26, 10}; 25 57 26 58 // PNG color types 27 59 enum PngColorType : uint8_t { ··· 42 74 }; 43 75 44 76 // Read a big-endian 32-bit value from file 45 - static bool readBE32(FsFile& file, uint32_t& value) { 77 + bool readBE32(FsFile& file, uint32_t& value) { 46 78 uint8_t buf[4]; 47 79 if (file.read(buf, 4) != 4) return false; 48 80 value = (static_cast<uint32_t>(buf[0]) << 24) | (static_cast<uint32_t>(buf[1]) << 16) | ··· 50 82 return true; 51 83 } 52 84 53 - // BMP writing helpers (same as JpegToBmpConverter) 54 - inline void write16(Print& out, const uint16_t value) { 55 - out.write(value & 0xFF); 56 - out.write((value >> 8) & 0xFF); 57 - } 58 - 59 - inline void write32(Print& out, const uint32_t value) { 60 - out.write(value & 0xFF); 61 - out.write((value >> 8) & 0xFF); 62 - out.write((value >> 16) & 0xFF); 63 - out.write((value >> 24) & 0xFF); 64 - } 65 - 66 - inline void write32Signed(Print& out, const int32_t value) { 67 - out.write(value & 0xFF); 68 - out.write((value >> 8) & 0xFF); 69 - out.write((value >> 16) & 0xFF); 70 - out.write((value >> 24) & 0xFF); 71 - } 72 - 73 - static void writeBmpHeader8bit(Print& bmpOut, const int width, const int height) { 85 + void writeBmpHeader8bit(Print& bmpOut, const int width, const int height) { 74 86 const int bytesPerRow = (width + 3) / 4 * 4; 75 87 const int imageSize = bytesPerRow * height; 76 88 const uint32_t paletteSize = 256 * 4; ··· 102 114 } 103 115 } 104 116 105 - static void writeBmpHeader1bit(Print& bmpOut, const int width, const int height) { 117 + void writeBmpHeader1bit(Print& bmpOut, const int width, const int height) { 106 118 const int bytesPerRow = (width + 31) / 32 * 4; 107 119 const int imageSize = bytesPerRow * height; 108 120 const uint32_t fileSize = 62 + imageSize; ··· 131 143 } 132 144 } 133 145 134 - static void writeBmpHeader2bit(Print& bmpOut, const int width, const int height) { 146 + void writeBmpHeader2bit(Print& bmpOut, const int width, const int height) { 135 147 const int bytesPerRow = (width * 2 + 31) / 32 * 4; 136 148 const int imageSize = bytesPerRow * height; 137 149 const uint32_t fileSize = 70 + imageSize; ··· 160 172 bmpOut.write(i); 161 173 } 162 174 } 163 - 164 - // Paeth predictor function per PNG spec 165 - static inline uint8_t paethPredictor(uint8_t a, uint8_t b, uint8_t c) { 166 - int p = static_cast<int>(a) + b - c; 167 - int pa = p > a ? p - a : a - p; 168 - int pb = p > b ? p - b : b - p; 169 - int pc = p > c ? p - c : c - p; 170 - if (pa <= pb && pa <= pc) return a; 171 - if (pb <= pc) return b; 172 - return c; 173 - } 175 + } // namespace 174 176 175 177 // Context for streaming PNG decompression 178 + // IMPORTANT: reader must be the first field - the uzlib callback casts uzlib_uncomp* to PngDecodeContext* 176 179 struct PngDecodeContext { 177 - FsFile& file; 180 + InflateReader reader; // Must be first — callback casts uzlib_uncomp* to PngDecodeContext* 181 + FsFile* file; 178 182 179 183 // PNG image properties 180 184 uint32_t width; ··· 188 192 uint8_t* currentRow; // current defiltered scanline 189 193 uint8_t* previousRow; // previous defiltered scanline 190 194 191 - // zlib decompression state 192 - mz_stream zstream; 193 - bool zstreamInitialized; 194 - 195 195 // Chunk reading state 196 196 uint32_t chunkBytesRemaining; // bytes left in current IDAT chunk 197 197 bool idatFinished; // no more IDAT chunks 198 198 199 - // File read buffer for feeding zlib 199 + // File read buffer for feeding uzlib 200 200 uint8_t readBuf[2048]; 201 201 202 202 // Palette for indexed color (type 3) ··· 209 209 static bool findNextIdatChunk(PngDecodeContext& ctx) { 210 210 while (true) { 211 211 uint32_t chunkLen; 212 - if (!readBE32(ctx.file, chunkLen)) return false; 212 + if (!readBE32(*ctx.file, chunkLen)) return false; 213 213 214 214 uint8_t chunkType[4]; 215 - if (ctx.file.read(chunkType, 4) != 4) return false; 215 + if (ctx.file->read(chunkType, 4) != 4) return false; 216 216 217 217 if (memcmp(chunkType, "IDAT", 4) == 0) { 218 218 ctx.chunkBytesRemaining = chunkLen; ··· 221 221 222 222 // Skip this chunk's data + 4-byte CRC 223 223 // Use seek to skip efficiently 224 - if (!ctx.file.seekCur(chunkLen + 4)) return false; 224 + if (!ctx.file->seekCur(chunkLen + 4)) return false; 225 225 226 226 // If we hit IEND, there are no more chunks 227 227 if (memcmp(chunkType, "IEND", 4) == 0) { ··· 230 230 } 231 231 } 232 232 233 - // Feed compressed data to zlib from IDAT chunks 234 - // Returns number of bytes made available in zstream, or -1 on error 235 - static int feedZlibInput(PngDecodeContext& ctx) { 236 - if (ctx.idatFinished) return 0; 233 + // uzlib callback: reads the next batch of IDAT data from the file 234 + static int pngIdatReadCallback(uzlib_uncomp* uncomp) { 235 + auto* ctx = reinterpret_cast<PngDecodeContext*>(uncomp); 237 236 238 - // If current IDAT chunk is exhausted, skip its CRC and find next 239 - while (ctx.chunkBytesRemaining == 0) { 240 - // Skip 4-byte CRC of previous IDAT 241 - if (!ctx.file.seekCur(4)) return -1; 237 + if (ctx->idatFinished) return -1; 242 238 243 - if (!findNextIdatChunk(ctx)) { 244 - ctx.idatFinished = true; 245 - return 0; 239 + // Skip 4-byte CRC and find next IDAT chunk when current chunk is exhausted 240 + while (ctx->chunkBytesRemaining == 0) { 241 + if (!ctx->file->seekCur(4)) { // skip 4-byte CRC of previous IDAT 242 + ctx->idatFinished = true; 243 + return -1; 244 + } 245 + if (!findNextIdatChunk(*ctx)) { 246 + ctx->idatFinished = true; 247 + return -1; 246 248 } 247 249 } 248 250 249 - // Read from current IDAT chunk 250 - size_t toRead = sizeof(ctx.readBuf); 251 - if (toRead > ctx.chunkBytesRemaining) toRead = ctx.chunkBytesRemaining; 251 + // Read from current IDAT chunk into the read buffer 252 + size_t toRead = sizeof(ctx->readBuf); 253 + if (toRead > ctx->chunkBytesRemaining) toRead = ctx->chunkBytesRemaining; 252 254 253 - int bytesRead = ctx.file.read(ctx.readBuf, toRead); 254 - if (bytesRead <= 0) return -1; 255 - 256 - ctx.chunkBytesRemaining -= bytesRead; 257 - ctx.zstream.next_in = ctx.readBuf; 258 - ctx.zstream.avail_in = bytesRead; 259 - 260 - return bytesRead; 261 - } 262 - 263 - // Decompress exactly 'needed' bytes into 'dest' 264 - static bool decompressBytes(PngDecodeContext& ctx, uint8_t* dest, size_t needed) { 265 - ctx.zstream.next_out = dest; 266 - ctx.zstream.avail_out = needed; 267 - 268 - while (ctx.zstream.avail_out > 0) { 269 - if (ctx.zstream.avail_in == 0) { 270 - int fed = feedZlibInput(ctx); 271 - if (fed < 0) return false; 272 - if (fed == 0) { 273 - // Try one more inflate to flush 274 - int ret = mz_inflate(&ctx.zstream, MZ_SYNC_FLUSH); 275 - if (ctx.zstream.avail_out == 0) break; 276 - return false; 277 - } 278 - } 279 - 280 - int ret = mz_inflate(&ctx.zstream, MZ_SYNC_FLUSH); 281 - if (ret != MZ_OK && ret != MZ_STREAM_END && ret != MZ_BUF_ERROR) { 282 - LOG_ERR("PNG", "zlib inflate error: %d", ret); 283 - return false; 284 - } 285 - if (ret == MZ_STREAM_END) break; 255 + int bytesRead = ctx->file->read(ctx->readBuf, toRead); 256 + if (bytesRead <= 0) { 257 + ctx->idatFinished = true; 258 + return -1; 286 259 } 287 260 288 - return ctx.zstream.avail_out == 0; 261 + ctx->chunkBytesRemaining -= bytesRead; 262 + 263 + // Give uzlib the buffer (skip first byte since we return it directly) 264 + uncomp->source = ctx->readBuf + 1; 265 + uncomp->source_limit = ctx->readBuf + bytesRead; 266 + return ctx->readBuf[0]; 289 267 } 290 268 291 269 // Decode one scanline: decompress filter byte + raw bytes, then unfilter 292 270 static bool decodeScanline(PngDecodeContext& ctx) { 293 271 // Decompress filter byte 294 272 uint8_t filterType; 295 - if (!decompressBytes(ctx, &filterType, 1)) return false; 273 + if (!ctx.reader.read(&filterType, 1)) return false; 296 274 297 275 // Decompress raw row data into currentRow 298 - if (!decompressBytes(ctx, ctx.currentRow, ctx.rawRowBytes)) return false; 276 + if (!ctx.reader.read(ctx.currentRow, ctx.rawRowBytes)) return false; 299 277 300 278 // Apply reverse filter 301 279 const int bpp = ctx.bytesPerPixel; ··· 521 499 } 522 500 523 501 // Initialize decode context 524 - PngDecodeContext ctx = {.file = pngFile, 525 - .width = width, 526 - .height = height, 527 - .bitDepth = bitDepth, 528 - .colorType = colorType, 529 - .bytesPerPixel = bytesPerPixel, 530 - .rawRowBytes = rawRowBytes, 531 - .currentRow = nullptr, 532 - .previousRow = nullptr, 533 - .zstream = {}, 534 - .zstreamInitialized = false, 535 - .chunkBytesRemaining = 0, 536 - .idatFinished = false, 537 - .readBuf = {}, 538 - .palette = {}, 539 - .paletteSize = 0}; 502 + PngDecodeContext ctx = {}; 503 + ctx.file = &pngFile; 504 + ctx.width = width; 505 + ctx.height = height; 506 + ctx.bitDepth = bitDepth; 507 + ctx.colorType = colorType; 508 + ctx.bytesPerPixel = bytesPerPixel; 509 + ctx.rawRowBytes = rawRowBytes; 510 + ctx.paletteSize = 0; 540 511 541 512 // Allocate scanline buffers 542 513 ctx.currentRow = static_cast<uint8_t*>(malloc(rawRowBytes)); ··· 585 556 return false; 586 557 } 587 558 588 - // Initialize zlib decompression 589 - memset(&ctx.zstream, 0, sizeof(ctx.zstream)); 590 - if (mz_inflateInit(&ctx.zstream) != MZ_OK) { 591 - LOG_ERR("PNG", "Failed to initialize zlib"); 559 + // Initialize streaming decompressor with 32KB ring buffer for back-reference history 560 + if (!ctx.reader.init(true)) { 561 + LOG_ERR("PNG", "Failed to init inflate reader"); 592 562 free(ctx.currentRow); 593 563 free(ctx.previousRow); 594 564 return false; 595 565 } 596 - ctx.zstreamInitialized = true; 566 + ctx.reader.setReadCallback(pngIdatReadCallback); 567 + // PNG IDAT data is zlib-wrapped: consume the 2-byte zlib header (CMF + FLG) 568 + ctx.reader.skipZlibHeader(); 597 569 598 570 // Calculate output dimensions (same logic as JpegToBmpConverter) 599 571 int outWidth = width; ··· 618 590 if (outWidth < 1) outWidth = 1; 619 591 if (outHeight < 1) outHeight = 1; 620 592 621 - scaleX_fp = (static_cast<uint32_t>(width) << 16) / outWidth; 622 - scaleY_fp = (static_cast<uint32_t>(height) << 16) / outHeight; 593 + scaleX_fp = (width << 16) / outWidth; 594 + scaleY_fp = (height << 16) / outHeight; 623 595 needsScaling = true; 624 596 625 597 LOG_DBG("PNG", "Scaling %ux%u -> %dx%d (target %dx%d)", width, height, outWidth, outHeight, targetWidth, ··· 643 615 auto* rowBuffer = static_cast<uint8_t*>(malloc(bytesPerRow)); 644 616 if (!rowBuffer) { 645 617 LOG_ERR("PNG", "Failed to allocate row buffer"); 646 - mz_inflateEnd(&ctx.zstream); 647 618 free(ctx.currentRow); 648 619 free(ctx.previousRow); 649 620 return false; ··· 687 658 delete fsDitherer; 688 659 delete atkinson1BitDitherer; 689 660 free(rowBuffer); 690 - mz_inflateEnd(&ctx.zstream); 691 661 free(ctx.currentRow); 692 662 free(ctx.previousRow); 693 663 return false; ··· 842 812 delete fsDitherer; 843 813 delete atkinson1BitDitherer; 844 814 free(rowBuffer); 845 - mz_inflateEnd(&ctx.zstream); 846 815 free(ctx.currentRow); 847 816 free(ctx.previousRow); 848 817
+77 -108
lib/ZipFile/ZipFile.cpp
··· 1 1 #include "ZipFile.h" 2 2 3 3 #include <HalStorage.h> 4 + #include <InflateReader.h> 4 5 #include <Logging.h> 5 - #include <miniz.h> 6 6 7 7 #include <algorithm> 8 8 9 - static bool inflateOneShot(const uint8_t* inputBuf, const size_t deflatedSize, uint8_t* outputBuf, 10 - const size_t inflatedSize) { 11 - const auto inflator = static_cast<tinfl_decompressor*>(malloc(sizeof(tinfl_decompressor))); 12 - if (!inflator) { 13 - LOG_ERR("ZIP", "Failed to allocate memory for inflator"); 14 - return false; 15 - } 16 - memset(inflator, 0, sizeof(tinfl_decompressor)); 17 - tinfl_init(inflator); 9 + struct ZipInflateCtx { 10 + InflateReader reader; // Must be first — callback casts uzlib_uncomp* to ZipInflateCtx* 11 + FsFile* file = nullptr; 12 + size_t fileRemaining = 0; 13 + uint8_t* readBuf = nullptr; 14 + size_t readBufSize = 0; 15 + }; 18 16 19 - size_t inBytes = deflatedSize; 20 - size_t outBytes = inflatedSize; 21 - const tinfl_status status = tinfl_decompress(inflator, inputBuf, &inBytes, nullptr, outputBuf, &outBytes, 22 - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); 17 + namespace { 18 + constexpr uint16_t ZIP_METHOD_STORED = 0; 19 + constexpr uint16_t ZIP_METHOD_DEFLATED = 8; 23 20 24 - free(inflator); 21 + int zipReadCallback(uzlib_uncomp* uncomp) { 22 + auto* ctx = reinterpret_cast<ZipInflateCtx*>(uncomp); 23 + if (ctx->fileRemaining == 0) return -1; 25 24 26 - if (status != TINFL_STATUS_DONE) { 27 - LOG_ERR("ZIP", "tinfl_decompress() failed with status %d", status); 28 - return false; 29 - } 25 + const size_t toRead = ctx->fileRemaining < ctx->readBufSize ? ctx->fileRemaining : ctx->readBufSize; 26 + const size_t bytesRead = ctx->file->read(ctx->readBuf, toRead); 27 + ctx->fileRemaining -= bytesRead; 28 + 29 + if (bytesRead == 0) return -1; 30 30 31 - return true; 31 + uncomp->source = ctx->readBuf + 1; 32 + uncomp->source_limit = ctx->readBuf + bytesRead; 33 + return ctx->readBuf[0]; 32 34 } 35 + } // namespace 33 36 34 37 bool ZipFile::loadAllFileStatSlims() { 35 38 const bool wasOpen = isOpen(); ··· 111 114 112 115 // Phase 1: Try scanning from cursor position first 113 116 uint32_t startPos = lastCentralDirPosValid ? lastCentralDirPos : zipDetails.centralDirOffset; 114 - uint32_t wrapPos = zipDetails.centralDirOffset; 115 117 bool wrapped = false; 116 118 bool found = false; 117 119 ··· 201 203 } 202 204 203 205 if (pLocalHeader[0] + (pLocalHeader[1] << 8) + (pLocalHeader[2] << 16) + (pLocalHeader[3] << 24) != 204 - 0x04034b50 /* MZ_ZIP_LOCAL_DIR_HEADER_SIG */) { 206 + 0x04034b50 /* ZIP local file header signature */) { 205 207 LOG_ERR("ZIP", "Not a valid zip file header"); 206 208 return -1; 207 209 } ··· 415 417 return nullptr; 416 418 } 417 419 418 - if (fileStat.method == MZ_NO_COMPRESSION) { 420 + if (fileStat.method == ZIP_METHOD_STORED) { 419 421 // no deflation, just read content 420 422 const size_t dataRead = file.read(data, inflatedDataSize); 421 423 if (!wasOpen) { ··· 429 431 } 430 432 431 433 // Continue out of block with data set 432 - } else if (fileStat.method == MZ_DEFLATED) { 434 + } else if (fileStat.method == ZIP_METHOD_DEFLATED) { 433 435 // Read out deflated content from file 434 436 const auto deflatedData = static_cast<uint8_t*>(malloc(deflatedDataSize)); 435 437 if (deflatedData == nullptr) { ··· 452 454 return nullptr; 453 455 } 454 456 455 - bool success = inflateOneShot(deflatedData, deflatedDataSize, data, inflatedDataSize); 457 + bool success = false; 458 + { 459 + InflateReader r; 460 + r.init(false); 461 + r.setSource(deflatedData, deflatedDataSize); 462 + success = r.read(data, inflatedDataSize); 463 + } 456 464 free(deflatedData); 457 465 458 466 if (!success) { ··· 495 503 const auto deflatedDataSize = fileStat.compressedSize; 496 504 const auto inflatedDataSize = fileStat.uncompressedSize; 497 505 498 - if (fileStat.method == MZ_NO_COMPRESSION) { 506 + if (fileStat.method == ZIP_METHOD_STORED) { 499 507 // no deflation, just read content 500 508 const auto buffer = static_cast<uint8_t*>(malloc(chunkSize)); 501 509 if (!buffer) { ··· 529 537 return true; 530 538 } 531 539 532 - if (fileStat.method == MZ_DEFLATED) { 533 - auto* inflator = static_cast<tinfl_decompressor*>(malloc(sizeof(tinfl_decompressor))); 534 - if (!inflator) { 535 - LOG_ERR("ZIP", "Failed to allocate memory for inflator"); 540 + if (fileStat.method == ZIP_METHOD_DEFLATED) { 541 + auto* fileReadBuffer = static_cast<uint8_t*>(malloc(chunkSize)); 542 + if (!fileReadBuffer) { 543 + LOG_ERR("ZIP", "Failed to allocate memory for zip file read buffer"); 536 544 if (!wasOpen) { 537 545 close(); 538 546 } 539 547 return false; 540 548 } 541 - memset(inflator, 0, sizeof(tinfl_decompressor)); 542 - tinfl_init(inflator); 543 549 544 - // Setup file read buffer 545 - const auto fileReadBuffer = static_cast<uint8_t*>(malloc(chunkSize)); 546 - if (!fileReadBuffer) { 547 - LOG_ERR("ZIP", "Failed to allocate memory for zip file read buffer"); 548 - free(inflator); 550 + auto* outputBuffer = static_cast<uint8_t*>(malloc(chunkSize)); 551 + if (!outputBuffer) { 552 + LOG_ERR("ZIP", "Failed to allocate memory for output buffer"); 553 + free(fileReadBuffer); 549 554 if (!wasOpen) { 550 555 close(); 551 556 } 552 557 return false; 553 558 } 554 559 555 - const auto outputBuffer = static_cast<uint8_t*>(malloc(TINFL_LZ_DICT_SIZE)); 556 - if (!outputBuffer) { 557 - LOG_ERR("ZIP", "Failed to allocate memory for dictionary"); 558 - free(inflator); 560 + ZipInflateCtx ctx; 561 + ctx.file = &file; 562 + ctx.fileRemaining = deflatedDataSize; 563 + ctx.readBuf = fileReadBuffer; 564 + ctx.readBufSize = chunkSize; 565 + 566 + if (!ctx.reader.init(true)) { 567 + LOG_ERR("ZIP", "Failed to init inflate reader"); 568 + free(outputBuffer); 559 569 free(fileReadBuffer); 560 570 if (!wasOpen) { 561 571 close(); 562 572 } 563 573 return false; 564 574 } 565 - memset(outputBuffer, 0, TINFL_LZ_DICT_SIZE); 575 + ctx.reader.setReadCallback(zipReadCallback); 566 576 567 - size_t fileRemainingBytes = deflatedDataSize; 568 - size_t processedOutputBytes = 0; 569 - size_t fileReadBufferFilledBytes = 0; 570 - size_t fileReadBufferCursor = 0; 571 - size_t outputCursor = 0; // Current offset in the circular dictionary 577 + bool success = false; 578 + size_t totalProduced = 0; 572 579 573 580 while (true) { 574 - // Load more compressed bytes when needed 575 - if (fileReadBufferCursor >= fileReadBufferFilledBytes) { 576 - if (fileRemainingBytes == 0) { 577 - // Should not be hit, but a safe protection 578 - break; // EOF 579 - } 581 + size_t produced; 582 + const InflateStatus status = ctx.reader.readAtMost(outputBuffer, chunkSize, &produced); 580 583 581 - fileReadBufferFilledBytes = 582 - file.read(fileReadBuffer, fileRemainingBytes < chunkSize ? fileRemainingBytes : chunkSize); 583 - fileRemainingBytes -= fileReadBufferFilledBytes; 584 - fileReadBufferCursor = 0; 585 - 586 - if (fileReadBufferFilledBytes == 0) { 587 - // Bad read 588 - break; // EOF 589 - } 584 + totalProduced += produced; 585 + if (totalProduced > static_cast<size_t>(inflatedDataSize)) { 586 + LOG_ERR("ZIP", "Decompressed size exceeds expected (%zu > %zu)", totalProduced, 587 + static_cast<size_t>(inflatedDataSize)); 588 + break; 590 589 } 591 590 592 - // Available bytes in fileReadBuffer to process 593 - size_t inBytes = fileReadBufferFilledBytes - fileReadBufferCursor; 594 - // Space remaining in outputBuffer 595 - size_t outBytes = TINFL_LZ_DICT_SIZE - outputCursor; 596 - 597 - const tinfl_status status = tinfl_decompress(inflator, fileReadBuffer + fileReadBufferCursor, &inBytes, 598 - outputBuffer, outputBuffer + outputCursor, &outBytes, 599 - fileRemainingBytes > 0 ? TINFL_FLAG_HAS_MORE_INPUT : 0); 600 - 601 - // Update input position 602 - fileReadBufferCursor += inBytes; 603 - 604 - // Write output chunk 605 - if (outBytes > 0) { 606 - processedOutputBytes += outBytes; 607 - if (out.write(outputBuffer + outputCursor, outBytes) != outBytes) { 591 + if (produced > 0) { 592 + if (out.write(outputBuffer, produced) != produced) { 608 593 LOG_ERR("ZIP", "Failed to write all output bytes to stream"); 609 - if (!wasOpen) { 610 - close(); 611 - } 612 - free(outputBuffer); 613 - free(fileReadBuffer); 614 - free(inflator); 615 - return false; 594 + break; 616 595 } 617 - // Update output position in buffer (with wraparound) 618 - outputCursor = (outputCursor + outBytes) & (TINFL_LZ_DICT_SIZE - 1); 619 596 } 620 597 621 - if (status < 0) { 622 - LOG_ERR("ZIP", "tinfl_decompress() failed with status %d", status); 623 - if (!wasOpen) { 624 - close(); 598 + if (status == InflateStatus::Done) { 599 + if (totalProduced != static_cast<size_t>(inflatedDataSize)) { 600 + LOG_ERR("ZIP", "Decompressed size mismatch (expected %zu, got %zu)", static_cast<size_t>(inflatedDataSize), 601 + totalProduced); 602 + break; 625 603 } 626 - free(outputBuffer); 627 - free(fileReadBuffer); 628 - free(inflator); 629 - return false; 604 + LOG_DBG("ZIP", "Decompressed %d bytes into %d bytes", deflatedDataSize, inflatedDataSize); 605 + success = true; 606 + break; 630 607 } 631 608 632 - if (status == TINFL_STATUS_DONE) { 633 - LOG_ERR("ZIP", "Decompressed %d bytes into %d bytes", deflatedDataSize, inflatedDataSize); 634 - if (!wasOpen) { 635 - close(); 636 - } 637 - free(inflator); 638 - free(fileReadBuffer); 639 - free(outputBuffer); 640 - return true; 609 + if (status == InflateStatus::Error) { 610 + LOG_ERR("ZIP", "Decompression failed"); 611 + break; 641 612 } 613 + // InflateStatus::Ok: output buffer full, continue 642 614 } 643 615 644 - // If we get here, EOF reached without TINFL_STATUS_DONE 645 - LOG_ERR("ZIP", "Unexpected EOF"); 646 616 if (!wasOpen) { 647 617 close(); 648 618 } 649 619 free(outputBuffer); 650 620 free(fileReadBuffer); 651 - free(inflator); 652 - return false; 621 + return success; // ctx.reader destructor frees the ring buffer 653 622 } 654 623 655 624 if (!wasOpen) {
-6900
lib/miniz/miniz.c
··· 1 - #include "miniz.h" 2 - /************************************************************************** 3 - * 4 - * Copyright 2013-2014 RAD Game Tools and Valve Software 5 - * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC 6 - * All Rights Reserved. 7 - * 8 - * Permission is hereby granted, free of charge, to any person obtaining a copy 9 - * of this software and associated documentation files (the "Software"), to deal 10 - * in the Software without restriction, including without limitation the rights 11 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 - * copies of the Software, and to permit persons to whom the Software is 13 - * furnished to do so, subject to the following conditions: 14 - * 15 - * The above copyright notice and this permission notice shall be included in 16 - * all copies or substantial portions of the Software. 17 - * 18 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 - * THE SOFTWARE. 25 - * 26 - **************************************************************************/ 27 - 28 - typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1]; 29 - typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1]; 30 - typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1]; 31 - 32 - #ifdef __cplusplus 33 - extern "C" { 34 - #endif 35 - 36 - /* ------------------- zlib-style API's */ 37 - 38 - mz_ulong mz_adler32(mz_ulong adler, const unsigned char* ptr, size_t buf_len) { 39 - mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16); 40 - size_t block_len = buf_len % 5552; 41 - if (!ptr) return MZ_ADLER32_INIT; 42 - while (buf_len) { 43 - for (i = 0; i + 7 < block_len; i += 8, ptr += 8) { 44 - s1 += ptr[0], s2 += s1; 45 - s1 += ptr[1], s2 += s1; 46 - s1 += ptr[2], s2 += s1; 47 - s1 += ptr[3], s2 += s1; 48 - s1 += ptr[4], s2 += s1; 49 - s1 += ptr[5], s2 += s1; 50 - s1 += ptr[6], s2 += s1; 51 - s1 += ptr[7], s2 += s1; 52 - } 53 - for (; i < block_len; ++i) s1 += *ptr++, s2 += s1; 54 - s1 %= 65521U, s2 %= 65521U; 55 - buf_len -= block_len; 56 - block_len = 5552; 57 - } 58 - return (s2 << 16) + s1; 59 - } 60 - 61 - /* Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache 62 - * usage against speed": http://www.geocities.com/malbrain/ */ 63 - #if 0 64 - mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) 65 - { 66 - static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 67 - 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c }; 68 - mz_uint32 crcu32 = (mz_uint32)crc; 69 - if (!ptr) 70 - return MZ_CRC32_INIT; 71 - crcu32 = ~crcu32; 72 - while (buf_len--) 73 - { 74 - mz_uint8 b = *ptr++; 75 - crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; 76 - crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; 77 - } 78 - return ~crcu32; 79 - } 80 - #elif defined(USE_EXTERNAL_MZCRC) 81 - /* If USE_EXTERNAL_CRC is defined, an external module will export the 82 - * mz_crc32() symbol for us to use, e.g. an SSE-accelerated version. 83 - * Depending on the impl, it may be necessary to ~ the input/output crc values. 84 - */ 85 - mz_ulong mz_crc32(mz_ulong crc, const mz_uint8* ptr, size_t buf_len); 86 - #else 87 - /* Faster, but larger CPU cache footprint. 88 - */ 89 - mz_ulong mz_crc32(mz_ulong crc, const mz_uint8* ptr, size_t buf_len) { 90 - static const mz_uint32 s_crc_table[256] = { 91 - 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 92 - 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 93 - 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 94 - 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 95 - 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 96 - 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 97 - 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 98 - 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 99 - 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 100 - 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 101 - 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 102 - 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 103 - 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 104 - 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 105 - 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 106 - 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 107 - 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 108 - 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 109 - 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 110 - 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 111 - 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 112 - 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 113 - 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 114 - 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 115 - 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 116 - 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 117 - 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 118 - 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 119 - 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D}; 120 - 121 - mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF; 122 - const mz_uint8* pByte_buf = (const mz_uint8*)ptr; 123 - 124 - while (buf_len >= 4) { 125 - crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; 126 - crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF]; 127 - crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF]; 128 - crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF]; 129 - pByte_buf += 4; 130 - buf_len -= 4; 131 - } 132 - 133 - while (buf_len) { 134 - crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; 135 - ++pByte_buf; 136 - --buf_len; 137 - } 138 - 139 - return ~crc32; 140 - } 141 - #endif 142 - 143 - void mz_free(void* p) { MZ_FREE(p); } 144 - 145 - MINIZ_EXPORT void* miniz_def_alloc_func(void* opaque, size_t items, size_t size) { 146 - (void)opaque, (void)items, (void)size; 147 - return MZ_MALLOC(items * size); 148 - } 149 - MINIZ_EXPORT void miniz_def_free_func(void* opaque, void* address) { 150 - (void)opaque, (void)address; 151 - MZ_FREE(address); 152 - } 153 - MINIZ_EXPORT void* miniz_def_realloc_func(void* opaque, void* address, size_t items, size_t size) { 154 - (void)opaque, (void)address, (void)items, (void)size; 155 - return MZ_REALLOC(address, items * size); 156 - } 157 - 158 - const char* mz_version(void) { return MZ_VERSION; } 159 - 160 - #ifndef MINIZ_NO_ZLIB_APIS 161 - 162 - int mz_deflateInit(mz_streamp pStream, int level) { 163 - return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY); 164 - } 165 - 166 - int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy) { 167 - tdefl_compressor* pComp; 168 - mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy); 169 - 170 - if (!pStream) return MZ_STREAM_ERROR; 171 - if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || 172 - ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) 173 - return MZ_PARAM_ERROR; 174 - 175 - pStream->data_type = 0; 176 - pStream->adler = MZ_ADLER32_INIT; 177 - pStream->msg = NULL; 178 - pStream->reserved = 0; 179 - pStream->total_in = 0; 180 - pStream->total_out = 0; 181 - if (!pStream->zalloc) pStream->zalloc = miniz_def_alloc_func; 182 - if (!pStream->zfree) pStream->zfree = miniz_def_free_func; 183 - 184 - pComp = (tdefl_compressor*)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor)); 185 - if (!pComp) return MZ_MEM_ERROR; 186 - 187 - pStream->state = (struct mz_internal_state*)pComp; 188 - 189 - if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) { 190 - mz_deflateEnd(pStream); 191 - return MZ_PARAM_ERROR; 192 - } 193 - 194 - return MZ_OK; 195 - } 196 - 197 - int mz_deflateReset(mz_streamp pStream) { 198 - if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) return MZ_STREAM_ERROR; 199 - pStream->total_in = pStream->total_out = 0; 200 - tdefl_init((tdefl_compressor*)pStream->state, NULL, NULL, ((tdefl_compressor*)pStream->state)->m_flags); 201 - return MZ_OK; 202 - } 203 - 204 - int mz_deflate(mz_streamp pStream, int flush) { 205 - size_t in_bytes, out_bytes; 206 - mz_ulong orig_total_in, orig_total_out; 207 - int mz_status = MZ_OK; 208 - 209 - if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) 210 - return MZ_STREAM_ERROR; 211 - if (!pStream->avail_out) return MZ_BUF_ERROR; 212 - 213 - if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH; 214 - 215 - if (((tdefl_compressor*)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE) 216 - return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR; 217 - 218 - orig_total_in = pStream->total_in; 219 - orig_total_out = pStream->total_out; 220 - for (;;) { 221 - tdefl_status defl_status; 222 - in_bytes = pStream->avail_in; 223 - out_bytes = pStream->avail_out; 224 - 225 - defl_status = tdefl_compress((tdefl_compressor*)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, 226 - &out_bytes, (tdefl_flush)flush); 227 - pStream->next_in += (mz_uint)in_bytes; 228 - pStream->avail_in -= (mz_uint)in_bytes; 229 - pStream->total_in += (mz_uint)in_bytes; 230 - pStream->adler = tdefl_get_adler32((tdefl_compressor*)pStream->state); 231 - 232 - pStream->next_out += (mz_uint)out_bytes; 233 - pStream->avail_out -= (mz_uint)out_bytes; 234 - pStream->total_out += (mz_uint)out_bytes; 235 - 236 - if (defl_status < 0) { 237 - mz_status = MZ_STREAM_ERROR; 238 - break; 239 - } else if (defl_status == TDEFL_STATUS_DONE) { 240 - mz_status = MZ_STREAM_END; 241 - break; 242 - } else if (!pStream->avail_out) 243 - break; 244 - else if ((!pStream->avail_in) && (flush != MZ_FINISH)) { 245 - if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out)) break; 246 - return MZ_BUF_ERROR; /* Can't make forward progress without some input. 247 - */ 248 - } 249 - } 250 - return mz_status; 251 - } 252 - 253 - int mz_deflateEnd(mz_streamp pStream) { 254 - if (!pStream) return MZ_STREAM_ERROR; 255 - if (pStream->state) { 256 - pStream->zfree(pStream->opaque, pStream->state); 257 - pStream->state = NULL; 258 - } 259 - return MZ_OK; 260 - } 261 - 262 - mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len) { 263 - (void)pStream; 264 - /* This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given 265 - * the way tdefl's blocking works.) */ 266 - return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5); 267 - } 268 - 269 - int mz_compress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len, 270 - int level) { 271 - int status; 272 - mz_stream stream; 273 - memset(&stream, 0, sizeof(stream)); 274 - 275 - /* In case mz_ulong is 64-bits (argh I hate longs). */ 276 - if ((source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; 277 - 278 - stream.next_in = pSource; 279 - stream.avail_in = (mz_uint32)source_len; 280 - stream.next_out = pDest; 281 - stream.avail_out = (mz_uint32)*pDest_len; 282 - 283 - status = mz_deflateInit(&stream, level); 284 - if (status != MZ_OK) return status; 285 - 286 - status = mz_deflate(&stream, MZ_FINISH); 287 - if (status != MZ_STREAM_END) { 288 - mz_deflateEnd(&stream); 289 - return (status == MZ_OK) ? MZ_BUF_ERROR : status; 290 - } 291 - 292 - *pDest_len = stream.total_out; 293 - return mz_deflateEnd(&stream); 294 - } 295 - 296 - int mz_compress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len) { 297 - return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION); 298 - } 299 - 300 - mz_ulong mz_compressBound(mz_ulong source_len) { return mz_deflateBound(NULL, source_len); } 301 - 302 - typedef struct { 303 - tinfl_decompressor m_decomp; 304 - mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; 305 - int m_window_bits; 306 - mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]; 307 - tinfl_status m_last_status; 308 - } inflate_state; 309 - 310 - int mz_inflateInit2(mz_streamp pStream, int window_bits) { 311 - inflate_state* pDecomp; 312 - if (!pStream) return MZ_STREAM_ERROR; 313 - if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) return MZ_PARAM_ERROR; 314 - 315 - pStream->data_type = 0; 316 - pStream->adler = 0; 317 - pStream->msg = NULL; 318 - pStream->total_in = 0; 319 - pStream->total_out = 0; 320 - pStream->reserved = 0; 321 - if (!pStream->zalloc) pStream->zalloc = miniz_def_alloc_func; 322 - if (!pStream->zfree) pStream->zfree = miniz_def_free_func; 323 - 324 - pDecomp = (inflate_state*)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state)); 325 - if (!pDecomp) return MZ_MEM_ERROR; 326 - 327 - pStream->state = (struct mz_internal_state*)pDecomp; 328 - 329 - tinfl_init(&pDecomp->m_decomp); 330 - pDecomp->m_dict_ofs = 0; 331 - pDecomp->m_dict_avail = 0; 332 - pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; 333 - pDecomp->m_first_call = 1; 334 - pDecomp->m_has_flushed = 0; 335 - pDecomp->m_window_bits = window_bits; 336 - 337 - return MZ_OK; 338 - } 339 - 340 - int mz_inflateInit(mz_streamp pStream) { return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS); } 341 - 342 - int mz_inflateReset(mz_streamp pStream) { 343 - inflate_state* pDecomp; 344 - if (!pStream) return MZ_STREAM_ERROR; 345 - 346 - pStream->data_type = 0; 347 - pStream->adler = 0; 348 - pStream->msg = NULL; 349 - pStream->total_in = 0; 350 - pStream->total_out = 0; 351 - pStream->reserved = 0; 352 - 353 - pDecomp = (inflate_state*)pStream->state; 354 - 355 - tinfl_init(&pDecomp->m_decomp); 356 - pDecomp->m_dict_ofs = 0; 357 - pDecomp->m_dict_avail = 0; 358 - pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; 359 - pDecomp->m_first_call = 1; 360 - pDecomp->m_has_flushed = 0; 361 - /* pDecomp->m_window_bits = window_bits */; 362 - 363 - return MZ_OK; 364 - } 365 - 366 - int mz_inflate(mz_streamp pStream, int flush) { 367 - inflate_state* pState; 368 - mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32; 369 - size_t in_bytes, out_bytes, orig_avail_in; 370 - tinfl_status status; 371 - 372 - if ((!pStream) || (!pStream->state)) return MZ_STREAM_ERROR; 373 - if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH; 374 - if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) return MZ_STREAM_ERROR; 375 - 376 - pState = (inflate_state*)pStream->state; 377 - if (pState->m_window_bits > 0) decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER; 378 - orig_avail_in = pStream->avail_in; 379 - 380 - first_call = pState->m_first_call; 381 - pState->m_first_call = 0; 382 - if (pState->m_last_status < 0) return MZ_DATA_ERROR; 383 - 384 - if (pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR; 385 - pState->m_has_flushed |= (flush == MZ_FINISH); 386 - 387 - if ((flush == MZ_FINISH) && (first_call)) { 388 - /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire 389 - * compressed/decompressed file. */ 390 - decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; 391 - in_bytes = pStream->avail_in; 392 - out_bytes = pStream->avail_out; 393 - status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, 394 - &out_bytes, decomp_flags); 395 - pState->m_last_status = status; 396 - pStream->next_in += (mz_uint)in_bytes; 397 - pStream->avail_in -= (mz_uint)in_bytes; 398 - pStream->total_in += (mz_uint)in_bytes; 399 - pStream->adler = tinfl_get_adler32(&pState->m_decomp); 400 - pStream->next_out += (mz_uint)out_bytes; 401 - pStream->avail_out -= (mz_uint)out_bytes; 402 - pStream->total_out += (mz_uint)out_bytes; 403 - 404 - if (status < 0) 405 - return MZ_DATA_ERROR; 406 - else if (status != TINFL_STATUS_DONE) { 407 - pState->m_last_status = TINFL_STATUS_FAILED; 408 - return MZ_BUF_ERROR; 409 - } 410 - return MZ_STREAM_END; 411 - } 412 - /* flush != MZ_FINISH then we must assume there's more input. */ 413 - if (flush != MZ_FINISH) decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT; 414 - 415 - if (pState->m_dict_avail) { 416 - n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); 417 - memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); 418 - pStream->next_out += n; 419 - pStream->avail_out -= n; 420 - pStream->total_out += n; 421 - pState->m_dict_avail -= n; 422 - pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); 423 - return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 424 - } 425 - 426 - for (;;) { 427 - in_bytes = pStream->avail_in; 428 - out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs; 429 - 430 - status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, 431 - pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags); 432 - pState->m_last_status = status; 433 - 434 - pStream->next_in += (mz_uint)in_bytes; 435 - pStream->avail_in -= (mz_uint)in_bytes; 436 - pStream->total_in += (mz_uint)in_bytes; 437 - pStream->adler = tinfl_get_adler32(&pState->m_decomp); 438 - 439 - pState->m_dict_avail = (mz_uint)out_bytes; 440 - 441 - n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); 442 - memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); 443 - pStream->next_out += n; 444 - pStream->avail_out -= n; 445 - pStream->total_out += n; 446 - pState->m_dict_avail -= n; 447 - pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); 448 - 449 - if (status < 0) 450 - return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary 451 - - oh well). */ 452 - else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) 453 - return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by 454 - setting flush to MZ_FINISH. */ 455 - else if (flush == MZ_FINISH) { 456 - /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */ 457 - if (status == TINFL_STATUS_DONE) return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; 458 - /* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If 459 - * there's no more room left in the output buffer then something is wrong. */ 460 - else if (!pStream->avail_out) 461 - return MZ_BUF_ERROR; 462 - } else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) 463 - break; 464 - } 465 - 466 - return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 467 - } 468 - 469 - int mz_inflateEnd(mz_streamp pStream) { 470 - if (!pStream) return MZ_STREAM_ERROR; 471 - if (pStream->state) { 472 - pStream->zfree(pStream->opaque, pStream->state); 473 - pStream->state = NULL; 474 - } 475 - return MZ_OK; 476 - } 477 - int mz_uncompress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong* pSource_len) { 478 - mz_stream stream; 479 - int status; 480 - memset(&stream, 0, sizeof(stream)); 481 - 482 - /* In case mz_ulong is 64-bits (argh I hate longs). */ 483 - if ((*pSource_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; 484 - 485 - stream.next_in = pSource; 486 - stream.avail_in = (mz_uint32)*pSource_len; 487 - stream.next_out = pDest; 488 - stream.avail_out = (mz_uint32)*pDest_len; 489 - 490 - status = mz_inflateInit(&stream); 491 - if (status != MZ_OK) return status; 492 - 493 - status = mz_inflate(&stream, MZ_FINISH); 494 - *pSource_len = *pSource_len - stream.avail_in; 495 - if (status != MZ_STREAM_END) { 496 - mz_inflateEnd(&stream); 497 - return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status; 498 - } 499 - *pDest_len = stream.total_out; 500 - 501 - return mz_inflateEnd(&stream); 502 - } 503 - 504 - int mz_uncompress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len) { 505 - return mz_uncompress2(pDest, pDest_len, pSource, &source_len); 506 - } 507 - 508 - const char* mz_error(int err) { 509 - static struct { 510 - int m_err; 511 - const char* m_pDesc; 512 - } s_error_descs[] = {{MZ_OK, ""}, 513 - {MZ_STREAM_END, "stream end"}, 514 - {MZ_NEED_DICT, "need dictionary"}, 515 - {MZ_ERRNO, "file error"}, 516 - {MZ_STREAM_ERROR, "stream error"}, 517 - {MZ_DATA_ERROR, "data error"}, 518 - {MZ_MEM_ERROR, "out of memory"}, 519 - {MZ_BUF_ERROR, "buf error"}, 520 - {MZ_VERSION_ERROR, "version error"}, 521 - {MZ_PARAM_ERROR, "parameter error"}}; 522 - mz_uint i; 523 - for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) 524 - if (s_error_descs[i].m_err == err) return s_error_descs[i].m_pDesc; 525 - return NULL; 526 - } 527 - 528 - #endif /*MINIZ_NO_ZLIB_APIS */ 529 - 530 - #ifdef __cplusplus 531 - } 532 - #endif 533 - 534 - /* 535 - This is free and unencumbered software released into the public domain. 536 - 537 - Anyone is free to copy, modify, publish, use, compile, sell, or 538 - distribute this software, either in source code form or as a compiled 539 - binary, for any purpose, commercial or non-commercial, and by any 540 - means. 541 - 542 - In jurisdictions that recognize copyright laws, the author or authors 543 - of this software dedicate any and all copyright interest in the 544 - software to the public domain. We make this dedication for the benefit 545 - of the public at large and to the detriment of our heirs and 546 - successors. We intend this dedication to be an overt act of 547 - relinquishment in perpetuity of all present and future rights to this 548 - software under copyright law. 549 - 550 - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 551 - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 552 - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 553 - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 554 - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 555 - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 556 - OTHER DEALINGS IN THE SOFTWARE. 557 - 558 - For more information, please refer to <http://unlicense.org/> 559 - */ 560 - /************************************************************************** 561 - * 562 - * Copyright 2013-2014 RAD Game Tools and Valve Software 563 - * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC 564 - * All Rights Reserved. 565 - * 566 - * Permission is hereby granted, free of charge, to any person obtaining a copy 567 - * of this software and associated documentation files (the "Software"), to deal 568 - * in the Software without restriction, including without limitation the rights 569 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 570 - * copies of the Software, and to permit persons to whom the Software is 571 - * furnished to do so, subject to the following conditions: 572 - * 573 - * The above copyright notice and this permission notice shall be included in 574 - * all copies or substantial portions of the Software. 575 - * 576 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 577 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 578 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 579 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 580 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 581 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 582 - * THE SOFTWARE. 583 - * 584 - **************************************************************************/ 585 - 586 - #ifdef __cplusplus 587 - extern "C" { 588 - #endif 589 - 590 - /* ------------------- Low-level Compression (independent from all decompression API's) */ 591 - 592 - /* Purposely making these tables static for faster init and thread safety. */ 593 - static const mz_uint16 s_tdefl_len_sym[256] = { 594 - 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, 268, 269, 269, 269, 269, 270, 270, 595 - 270, 270, 271, 271, 271, 271, 272, 272, 272, 272, 273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 596 - 274, 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276, 276, 276, 276, 276, 277, 277, 597 - 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 598 - 278, 278, 278, 278, 278, 278, 278, 278, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 599 - 279, 279, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 281, 281, 281, 281, 600 - 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 601 - 281, 281, 281, 281, 281, 281, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 602 - 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 283, 283, 283, 283, 283, 283, 603 - 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 604 - 283, 283, 283, 283, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 605 - 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 285}; 606 - 607 - static const mz_uint8 s_tdefl_len_extra[256] = { 608 - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 609 - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 610 - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 611 - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 612 - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 613 - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 614 - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0}; 615 - 616 - static const mz_uint8 s_tdefl_small_dist_sym[512] = { 617 - 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 618 - 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 619 - 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 620 - 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 621 - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 622 - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 623 - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 624 - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 625 - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 626 - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 627 - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 628 - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 629 - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 630 - 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 631 - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 632 - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 633 - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 634 - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17}; 635 - 636 - static const mz_uint8 s_tdefl_small_dist_extra[512] = { 637 - 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 638 - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 639 - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 640 - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 641 - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 642 - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 643 - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 644 - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 645 - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 646 - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 647 - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 648 - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 649 - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 650 - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}; 651 - 652 - static const mz_uint8 s_tdefl_large_dist_sym[128] = { 653 - 0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 654 - 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 655 - 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 656 - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 657 - 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29}; 658 - 659 - static const mz_uint8 s_tdefl_large_dist_extra[128] = { 660 - 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 661 - 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 662 - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 663 - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 664 - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; 665 - 666 - /* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values. */ 667 - typedef struct { 668 - mz_uint16 m_key, m_sym_index; 669 - } tdefl_sym_freq; 670 - static tdefl_sym_freq* tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq* pSyms0, tdefl_sym_freq* pSyms1) { 671 - mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2]; 672 - tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1; 673 - MZ_CLEAR_OBJ(hist); 674 - for (i = 0; i < num_syms; i++) { 675 - mz_uint freq = pSyms0[i].m_key; 676 - hist[freq & 0xFF]++; 677 - hist[256 + ((freq >> 8) & 0xFF)]++; 678 - } 679 - while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) total_passes--; 680 - for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) { 681 - const mz_uint32* pHist = &hist[pass << 8]; 682 - mz_uint offsets[256], cur_ofs = 0; 683 - for (i = 0; i < 256; i++) { 684 - offsets[i] = cur_ofs; 685 - cur_ofs += pHist[i]; 686 - } 687 - for (i = 0; i < num_syms; i++) pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i]; 688 - { 689 - tdefl_sym_freq* t = pCur_syms; 690 - pCur_syms = pNew_syms; 691 - pNew_syms = t; 692 - } 693 - } 694 - return pCur_syms; 695 - } 696 - 697 - /* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, 698 - * jyrki@diku.dk, November 1996. */ 699 - static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq* A, int n) { 700 - int root, leaf, next, avbl, used, dpth; 701 - if (n == 0) 702 - return; 703 - else if (n == 1) { 704 - A[0].m_key = 1; 705 - return; 706 - } 707 - A[0].m_key += A[1].m_key; 708 - root = 0; 709 - leaf = 2; 710 - for (next = 1; next < n - 1; next++) { 711 - if (leaf >= n || A[root].m_key < A[leaf].m_key) { 712 - A[next].m_key = A[root].m_key; 713 - A[root++].m_key = (mz_uint16)next; 714 - } else 715 - A[next].m_key = A[leaf++].m_key; 716 - if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) { 717 - A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key); 718 - A[root++].m_key = (mz_uint16)next; 719 - } else 720 - A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key); 721 - } 722 - A[n - 2].m_key = 0; 723 - for (next = n - 3; next >= 0; next--) A[next].m_key = A[A[next].m_key].m_key + 1; 724 - avbl = 1; 725 - used = dpth = 0; 726 - root = n - 2; 727 - next = n - 1; 728 - while (avbl > 0) { 729 - while (root >= 0 && (int)A[root].m_key == dpth) { 730 - used++; 731 - root--; 732 - } 733 - while (avbl > used) { 734 - A[next--].m_key = (mz_uint16)(dpth); 735 - avbl--; 736 - } 737 - avbl = 2 * used; 738 - dpth++; 739 - used = 0; 740 - } 741 - } 742 - 743 - /* Limits canonical Huffman code table's max code size. */ 744 - enum { TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32 }; 745 - static void tdefl_huffman_enforce_max_code_size(int* pNum_codes, int code_list_len, int max_code_size) { 746 - int i; 747 - mz_uint32 total = 0; 748 - if (code_list_len <= 1) return; 749 - for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++) pNum_codes[max_code_size] += pNum_codes[i]; 750 - for (i = max_code_size; i > 0; i--) total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i)); 751 - while (total != (1UL << max_code_size)) { 752 - pNum_codes[max_code_size]--; 753 - for (i = max_code_size - 1; i > 0; i--) 754 - if (pNum_codes[i]) { 755 - pNum_codes[i]--; 756 - pNum_codes[i + 1] += 2; 757 - break; 758 - } 759 - total--; 760 - } 761 - } 762 - 763 - static void tdefl_optimize_huffman_table(tdefl_compressor* d, int table_num, int table_len, int code_size_limit, 764 - int static_table) { 765 - int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; 766 - mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; 767 - MZ_CLEAR_OBJ(num_codes); 768 - if (static_table) { 769 - for (i = 0; i < table_len; i++) num_codes[d->m_huff_code_sizes[table_num][i]]++; 770 - } else { 771 - tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms; 772 - int num_used_syms = 0; 773 - const mz_uint16* pSym_count = &d->m_huff_count[table_num][0]; 774 - for (i = 0; i < table_len; i++) 775 - if (pSym_count[i]) { 776 - syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i]; 777 - syms0[num_used_syms++].m_sym_index = (mz_uint16)i; 778 - } 779 - 780 - pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1); 781 - tdefl_calculate_minimum_redundancy(pSyms, num_used_syms); 782 - 783 - for (i = 0; i < num_used_syms; i++) num_codes[pSyms[i].m_key]++; 784 - 785 - tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit); 786 - 787 - MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]); 788 - MZ_CLEAR_OBJ(d->m_huff_codes[table_num]); 789 - for (i = 1, j = num_used_syms; i <= code_size_limit; i++) 790 - for (l = num_codes[i]; l > 0; l--) d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i); 791 - } 792 - 793 - next_code[1] = 0; 794 - for (j = 0, i = 2; i <= code_size_limit; i++) next_code[i] = j = ((j + num_codes[i - 1]) << 1); 795 - 796 - for (i = 0; i < table_len; i++) { 797 - mz_uint rev_code = 0, code, code_size; 798 - if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0) continue; 799 - code = next_code[code_size]++; 800 - for (l = code_size; l > 0; l--, code >>= 1) rev_code = (rev_code << 1) | (code & 1); 801 - d->m_huff_codes[table_num][i] = (mz_uint16)rev_code; 802 - } 803 - } 804 - 805 - #define TDEFL_PUT_BITS(b, l) \ 806 - do { \ 807 - mz_uint bits = b; \ 808 - mz_uint len = l; \ 809 - MZ_ASSERT(bits <= ((1U << len) - 1U)); \ 810 - d->m_bit_buffer |= (bits << d->m_bits_in); \ 811 - d->m_bits_in += len; \ 812 - while (d->m_bits_in >= 8) { \ 813 - if (d->m_pOutput_buf < d->m_pOutput_buf_end) *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \ 814 - d->m_bit_buffer >>= 8; \ 815 - d->m_bits_in -= 8; \ 816 - } \ 817 - } \ 818 - MZ_MACRO_END 819 - 820 - #define TDEFL_RLE_PREV_CODE_SIZE() \ 821 - { \ 822 - if (rle_repeat_count) { \ 823 - if (rle_repeat_count < 3) { \ 824 - d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \ 825 - while (rle_repeat_count--) packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \ 826 - } else { \ 827 - d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \ 828 - packed_code_sizes[num_packed_code_sizes++] = 16; \ 829 - packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \ 830 - } \ 831 - rle_repeat_count = 0; \ 832 - } \ 833 - } 834 - 835 - #define TDEFL_RLE_ZERO_CODE_SIZE() \ 836 - { \ 837 - if (rle_z_count) { \ 838 - if (rle_z_count < 3) { \ 839 - d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \ 840 - while (rle_z_count--) packed_code_sizes[num_packed_code_sizes++] = 0; \ 841 - } else if (rle_z_count <= 10) { \ 842 - d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \ 843 - packed_code_sizes[num_packed_code_sizes++] = 17; \ 844 - packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \ 845 - } else { \ 846 - d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \ 847 - packed_code_sizes[num_packed_code_sizes++] = 18; \ 848 - packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \ 849 - } \ 850 - rle_z_count = 0; \ 851 - } \ 852 - } 853 - 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}; 856 - 857 - static void tdefl_start_dynamic_block(tdefl_compressor* d) { 858 - int num_lit_codes, num_dist_codes, num_bit_lengths; 859 - mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index; 860 - mz_uint8 code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], 861 - packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], prev_code_size = 0xFF; 862 - 863 - d->m_huff_count[0][256] = 1; 864 - 865 - tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE); 866 - tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE); 867 - 868 - for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--) 869 - if (d->m_huff_code_sizes[0][num_lit_codes - 1]) break; 870 - for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--) 871 - if (d->m_huff_code_sizes[1][num_dist_codes - 1]) break; 872 - 873 - memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes); 874 - memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes); 875 - total_code_sizes_to_pack = num_lit_codes + num_dist_codes; 876 - num_packed_code_sizes = 0; 877 - rle_z_count = 0; 878 - rle_repeat_count = 0; 879 - 880 - memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2); 881 - for (i = 0; i < total_code_sizes_to_pack; i++) { 882 - mz_uint8 code_size = code_sizes_to_pack[i]; 883 - if (!code_size) { 884 - TDEFL_RLE_PREV_CODE_SIZE(); 885 - if (++rle_z_count == 138) { 886 - TDEFL_RLE_ZERO_CODE_SIZE(); 887 - } 888 - } else { 889 - TDEFL_RLE_ZERO_CODE_SIZE(); 890 - if (code_size != prev_code_size) { 891 - TDEFL_RLE_PREV_CODE_SIZE(); 892 - d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1); 893 - packed_code_sizes[num_packed_code_sizes++] = code_size; 894 - } else if (++rle_repeat_count == 6) { 895 - TDEFL_RLE_PREV_CODE_SIZE(); 896 - } 897 - } 898 - prev_code_size = code_size; 899 - } 900 - if (rle_repeat_count) { 901 - TDEFL_RLE_PREV_CODE_SIZE(); 902 - } else { 903 - TDEFL_RLE_ZERO_CODE_SIZE(); 904 - } 905 - 906 - tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE); 907 - 908 - TDEFL_PUT_BITS(2, 2); 909 - 910 - TDEFL_PUT_BITS(num_lit_codes - 257, 5); 911 - TDEFL_PUT_BITS(num_dist_codes - 1, 5); 912 - 913 - for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--) 914 - if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]]) break; 915 - num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1)); 916 - TDEFL_PUT_BITS(num_bit_lengths - 4, 4); 917 - for (i = 0; (int)i < num_bit_lengths; i++) 918 - TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3); 919 - 920 - for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;) { 921 - mz_uint code = packed_code_sizes[packed_code_sizes_index++]; 922 - MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2); 923 - TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]); 924 - if (code >= 16) TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]); 925 - } 926 - } 927 - 928 - static void tdefl_start_static_block(tdefl_compressor* d) { 929 - mz_uint i; 930 - mz_uint8* p = &d->m_huff_code_sizes[0][0]; 931 - 932 - for (i = 0; i <= 143; ++i) *p++ = 8; 933 - for (; i <= 255; ++i) *p++ = 9; 934 - for (; i <= 279; ++i) *p++ = 7; 935 - for (; i <= 287; ++i) *p++ = 8; 936 - 937 - memset(d->m_huff_code_sizes[1], 5, 32); 938 - 939 - tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE); 940 - tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE); 941 - 942 - TDEFL_PUT_BITS(1, 2); 943 - } 944 - 945 - static const mz_uint mz_bitmasks[17] = {0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 946 - 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF}; 947 - 948 - #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS 949 - static mz_bool tdefl_compress_lz_codes(tdefl_compressor* d) { 950 - mz_uint flags; 951 - mz_uint8* pLZ_codes; 952 - mz_uint8* pOutput_buf = d->m_pOutput_buf; 953 - mz_uint8* pLZ_code_buf_end = d->m_pLZ_code_buf; 954 - mz_uint64 bit_buffer = d->m_bit_buffer; 955 - mz_uint bits_in = d->m_bits_in; 956 - 957 - #define TDEFL_PUT_BITS_FAST(b, l) \ 958 - { \ 959 - bit_buffer |= (((mz_uint64)(b)) << bits_in); \ 960 - bits_in += (l); \ 961 - } 962 - 963 - flags = 1; 964 - for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1) { 965 - if (flags == 1) flags = *pLZ_codes++ | 0x100; 966 - 967 - if (flags & 1) { 968 - mz_uint s0, s1, n0, n1, sym, num_extra_bits; 969 - mz_uint match_len = pLZ_codes[0], match_dist = *(const mz_uint16*)(pLZ_codes + 1); 970 - pLZ_codes += 3; 971 - 972 - MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); 973 - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], 974 - d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); 975 - TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); 976 - 977 - /* This sequence coaxes MSVC into using cmov's vs. jmp's. */ 978 - s0 = s_tdefl_small_dist_sym[match_dist & 511]; 979 - n0 = s_tdefl_small_dist_extra[match_dist & 511]; 980 - s1 = s_tdefl_large_dist_sym[match_dist >> 8]; 981 - n1 = s_tdefl_large_dist_extra[match_dist >> 8]; 982 - sym = (match_dist < 512) ? s0 : s1; 983 - num_extra_bits = (match_dist < 512) ? n0 : n1; 984 - 985 - MZ_ASSERT(d->m_huff_code_sizes[1][sym]); 986 - TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); 987 - TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); 988 - } else { 989 - mz_uint lit = *pLZ_codes++; 990 - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); 991 - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); 992 - 993 - if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) { 994 - flags >>= 1; 995 - lit = *pLZ_codes++; 996 - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); 997 - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); 998 - 999 - if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) { 1000 - flags >>= 1; 1001 - lit = *pLZ_codes++; 1002 - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); 1003 - TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); 1004 - } 1005 - } 1006 - } 1007 - 1008 - if (pOutput_buf >= d->m_pOutput_buf_end) return MZ_FALSE; 1009 - 1010 - *(mz_uint64*)pOutput_buf = bit_buffer; 1011 - pOutput_buf += (bits_in >> 3); 1012 - bit_buffer >>= (bits_in & ~7); 1013 - bits_in &= 7; 1014 - } 1015 - 1016 - #undef TDEFL_PUT_BITS_FAST 1017 - 1018 - d->m_pOutput_buf = pOutput_buf; 1019 - d->m_bits_in = 0; 1020 - d->m_bit_buffer = 0; 1021 - 1022 - while (bits_in) { 1023 - mz_uint32 n = MZ_MIN(bits_in, 16); 1024 - TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n); 1025 - bit_buffer >>= n; 1026 - bits_in -= n; 1027 - } 1028 - 1029 - TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); 1030 - 1031 - return (d->m_pOutput_buf < d->m_pOutput_buf_end); 1032 - } 1033 - #else 1034 - static mz_bool tdefl_compress_lz_codes(tdefl_compressor* d) { 1035 - mz_uint flags; 1036 - mz_uint8* pLZ_codes; 1037 - 1038 - flags = 1; 1039 - for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1) { 1040 - if (flags == 1) flags = *pLZ_codes++ | 0x100; 1041 - if (flags & 1) { 1042 - mz_uint sym, num_extra_bits; 1043 - mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); 1044 - pLZ_codes += 3; 1045 - 1046 - MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); 1047 - TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], 1048 - d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); 1049 - TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); 1050 - 1051 - if (match_dist < 512) { 1052 - sym = s_tdefl_small_dist_sym[match_dist]; 1053 - num_extra_bits = s_tdefl_small_dist_extra[match_dist]; 1054 - } else { 1055 - sym = s_tdefl_large_dist_sym[match_dist >> 8]; 1056 - num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8]; 1057 - } 1058 - MZ_ASSERT(d->m_huff_code_sizes[1][sym]); 1059 - TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); 1060 - TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); 1061 - } else { 1062 - mz_uint lit = *pLZ_codes++; 1063 - MZ_ASSERT(d->m_huff_code_sizes[0][lit]); 1064 - TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); 1065 - } 1066 - } 1067 - 1068 - TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); 1069 - 1070 - return (d->m_pOutput_buf < d->m_pOutput_buf_end); 1071 - } 1072 - #endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS */ 1073 - 1074 - static mz_bool tdefl_compress_block(tdefl_compressor* d, mz_bool static_block) { 1075 - if (static_block) 1076 - tdefl_start_static_block(d); 1077 - else 1078 - tdefl_start_dynamic_block(d); 1079 - return tdefl_compress_lz_codes(d); 1080 - } 1081 - 1082 - static int tdefl_flush_block(tdefl_compressor* d, int flush) { 1083 - mz_uint saved_bit_buf, saved_bits_in; 1084 - mz_uint8* pSaved_output_buf; 1085 - mz_bool comp_block_succeeded = MZ_FALSE; 1086 - int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && 1087 - (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size; 1088 - mz_uint8* pOutput_buf_start = 1089 - ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) 1090 - ? ((mz_uint8*)d->m_pOut_buf + d->m_out_buf_ofs) 1091 - : d->m_output_buf; 1092 - 1093 - d->m_pOutput_buf = pOutput_buf_start; 1094 - d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16; 1095 - 1096 - MZ_ASSERT(!d->m_output_flush_remaining); 1097 - d->m_output_flush_ofs = 0; 1098 - d->m_output_flush_remaining = 0; 1099 - 1100 - *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left); 1101 - d->m_pLZ_code_buf -= (d->m_num_flags_left == 8); 1102 - 1103 - if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) { 1104 - TDEFL_PUT_BITS(0x78, 8); 1105 - TDEFL_PUT_BITS(0x01, 8); 1106 - } 1107 - 1108 - TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); 1109 - 1110 - pSaved_output_buf = d->m_pOutput_buf; 1111 - saved_bit_buf = d->m_bit_buffer; 1112 - saved_bits_in = d->m_bits_in; 1113 - 1114 - if (!use_raw_block) 1115 - comp_block_succeeded = 1116 - tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48)); 1117 - 1118 - /* If the block gets expanded, forget the current contents of the output buffer and send a raw block instead. */ 1119 - if (((use_raw_block) || 1120 - ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) && 1121 - ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size)) { 1122 - mz_uint i; 1123 - d->m_pOutput_buf = pSaved_output_buf; 1124 - d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; 1125 - TDEFL_PUT_BITS(0, 2); 1126 - if (d->m_bits_in) { 1127 - TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 1128 - } 1129 - for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) { 1130 - TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16); 1131 - } 1132 - for (i = 0; i < d->m_total_lz_bytes; ++i) { 1133 - TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8); 1134 - } 1135 - } 1136 - /* Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output 1137 - buffer when using dynamic codes. */ 1138 - else if (!comp_block_succeeded) { 1139 - d->m_pOutput_buf = pSaved_output_buf; 1140 - d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in; 1141 - tdefl_compress_block(d, MZ_TRUE); 1142 - } 1143 - 1144 - if (flush) { 1145 - if (flush == TDEFL_FINISH) { 1146 - if (d->m_bits_in) { 1147 - TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 1148 - } 1149 - if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) { 1150 - mz_uint i, a = d->m_adler32; 1151 - for (i = 0; i < 4; i++) { 1152 - TDEFL_PUT_BITS((a >> 24) & 0xFF, 8); 1153 - a <<= 8; 1154 - } 1155 - } 1156 - } else { 1157 - mz_uint i, z = 0; 1158 - TDEFL_PUT_BITS(0, 3); 1159 - if (d->m_bits_in) { 1160 - TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 1161 - } 1162 - for (i = 2; i; --i, z ^= 0xFFFF) { 1163 - TDEFL_PUT_BITS(z & 0xFFFF, 16); 1164 - } 1165 - } 1166 - } 1167 - 1168 - MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end); 1169 - 1170 - memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); 1171 - memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); 1172 - 1173 - d->m_pLZ_code_buf = d->m_lz_code_buf + 1; 1174 - d->m_pLZ_flags = d->m_lz_code_buf; 1175 - d->m_num_flags_left = 8; 1176 - d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes; 1177 - d->m_total_lz_bytes = 0; 1178 - d->m_block_index++; 1179 - 1180 - if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0) { 1181 - if (d->m_pPut_buf_func) { 1182 - *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8*)d->m_pIn_buf; 1183 - if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user)) 1184 - return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED); 1185 - } else if (pOutput_buf_start == d->m_output_buf) { 1186 - int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs)); 1187 - memcpy((mz_uint8*)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy); 1188 - d->m_out_buf_ofs += bytes_to_copy; 1189 - if ((n -= bytes_to_copy) != 0) { 1190 - d->m_output_flush_ofs = bytes_to_copy; 1191 - d->m_output_flush_remaining = n; 1192 - } 1193 - } else { 1194 - d->m_out_buf_ofs += n; 1195 - } 1196 - } 1197 - 1198 - return d->m_output_flush_remaining; 1199 - } 1200 - 1201 - #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1202 - #ifdef MINIZ_UNALIGNED_USE_MEMCPY 1203 - static mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8* p) { 1204 - mz_uint16 ret; 1205 - memcpy(&ret, p, sizeof(mz_uint16)); 1206 - return ret; 1207 - } 1208 - static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16* p) { 1209 - mz_uint16 ret; 1210 - memcpy(&ret, p, sizeof(mz_uint16)); 1211 - return ret; 1212 - } 1213 - #else 1214 - #define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16*)(p) 1215 - #define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16*)(p) 1216 - #endif 1217 - static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor* d, mz_uint lookahead_pos, mz_uint max_dist, 1218 - mz_uint max_match_len, mz_uint* pMatch_dist, mz_uint* pMatch_len) { 1219 - mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, 1220 - probe_len; 1221 - mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; 1222 - const mz_uint16 *s = (const mz_uint16*)(d->m_dict + pos), *p, *q; 1223 - mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD2(s); 1224 - MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); 1225 - if (max_match_len <= match_len) return; 1226 - for (;;) { 1227 - for (;;) { 1228 - if (--num_probes_left == 0) return; 1229 - #define TDEFL_PROBE \ 1230 - next_probe_pos = d->m_next[probe_pos]; \ 1231 - if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \ 1232 - probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ 1233 - if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) break; 1234 - TDEFL_PROBE; 1235 - TDEFL_PROBE; 1236 - TDEFL_PROBE; 1237 - } 1238 - if (!dist) break; 1239 - q = (const mz_uint16*)(d->m_dict + probe_pos); 1240 - if (TDEFL_READ_UNALIGNED_WORD2(q) != s01) continue; 1241 - p = s; 1242 - probe_len = 32; 1243 - do { 1244 - } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && 1245 - (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && 1246 - (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && 1247 - (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0)); 1248 - if (!probe_len) { 1249 - *pMatch_dist = dist; 1250 - *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN); 1251 - break; 1252 - } else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8*)p == *(const mz_uint8*)q)) > 1253 - match_len) { 1254 - *pMatch_dist = dist; 1255 - if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len) break; 1256 - c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]); 1257 - } 1258 - } 1259 - } 1260 - #else 1261 - static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor* d, mz_uint lookahead_pos, mz_uint max_dist, 1262 - mz_uint max_match_len, mz_uint* pMatch_dist, mz_uint* pMatch_len) { 1263 - mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, 1264 - probe_len; 1265 - mz_uint num_probes_left = d->m_max_probes[match_len >= 32]; 1266 - const mz_uint8 *s = d->m_dict + pos, *p, *q; 1267 - mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1]; 1268 - MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN); 1269 - if (max_match_len <= match_len) return; 1270 - for (;;) { 1271 - for (;;) { 1272 - if (--num_probes_left == 0) return; 1273 - #define TDEFL_PROBE \ 1274 - next_probe_pos = d->m_next[probe_pos]; \ 1275 - if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \ 1276 - probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \ 1277 - if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) break; 1278 - TDEFL_PROBE; 1279 - TDEFL_PROBE; 1280 - TDEFL_PROBE; 1281 - } 1282 - if (!dist) break; 1283 - p = s; 1284 - q = d->m_dict + probe_pos; 1285 - for (probe_len = 0; probe_len < max_match_len; probe_len++) 1286 - if (*p++ != *q++) break; 1287 - if (probe_len > match_len) { 1288 - *pMatch_dist = dist; 1289 - if ((*pMatch_len = match_len = probe_len) == max_match_len) return; 1290 - c0 = d->m_dict[pos + match_len]; 1291 - c1 = d->m_dict[pos + match_len - 1]; 1292 - } 1293 - } 1294 - } 1295 - #endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES */ 1296 - 1297 - #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN 1298 - #ifdef MINIZ_UNALIGNED_USE_MEMCPY 1299 - static mz_uint32 TDEFL_READ_UNALIGNED_WORD32(const mz_uint8* p) { 1300 - mz_uint32 ret; 1301 - memcpy(&ret, p, sizeof(mz_uint32)); 1302 - return ret; 1303 - } 1304 - #else 1305 - #define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32*)(p) 1306 - #endif 1307 - static mz_bool tdefl_compress_fast(tdefl_compressor* d) { 1308 - /* Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications 1309 - * where raw throughput is valued more highly than ratio. */ 1310 - mz_uint lookahead_pos = d->m_lookahead_pos, lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, 1311 - total_lz_bytes = d->m_total_lz_bytes, num_flags_left = d->m_num_flags_left; 1312 - mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags; 1313 - mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; 1314 - 1315 - while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size))) { 1316 - const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096; 1317 - mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK; 1318 - mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size); 1319 - d->m_src_buf_left -= num_bytes_to_process; 1320 - lookahead_size += num_bytes_to_process; 1321 - 1322 - while (num_bytes_to_process) { 1323 - mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process); 1324 - memcpy(d->m_dict + dst_pos, d->m_pSrc, n); 1325 - if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) 1326 - memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos)); 1327 - d->m_pSrc += n; 1328 - dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK; 1329 - num_bytes_to_process -= n; 1330 - } 1331 - 1332 - dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size); 1333 - if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE)) break; 1334 - 1335 - while (lookahead_size >= 4) { 1336 - mz_uint cur_match_dist, cur_match_len = 1; 1337 - mz_uint8* pCur_dict = d->m_dict + cur_pos; 1338 - mz_uint first_trigram = TDEFL_READ_UNALIGNED_WORD32(pCur_dict) & 0xFFFFFF; 1339 - mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK; 1340 - mz_uint probe_pos = d->m_hash[hash]; 1341 - d->m_hash[hash] = (mz_uint16)lookahead_pos; 1342 - 1343 - if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && 1344 - ((TDEFL_READ_UNALIGNED_WORD32(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == 1345 - first_trigram)) { 1346 - const mz_uint16* p = (const mz_uint16*)pCur_dict; 1347 - const mz_uint16* q = (const mz_uint16*)(d->m_dict + probe_pos); 1348 - mz_uint32 probe_len = 32; 1349 - do { 1350 - } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && 1351 - (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && 1352 - (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && 1353 - (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0)); 1354 - cur_match_len = 1355 - ((mz_uint)(p - (const mz_uint16*)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8*)p == *(const mz_uint8*)q); 1356 - if (!probe_len) cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0; 1357 - 1358 - if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || 1359 - ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U))) { 1360 - cur_match_len = 1; 1361 - *pLZ_code_buf++ = (mz_uint8)first_trigram; 1362 - *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); 1363 - d->m_huff_count[0][(mz_uint8)first_trigram]++; 1364 - } else { 1365 - mz_uint32 s0, s1; 1366 - cur_match_len = MZ_MIN(cur_match_len, lookahead_size); 1367 - 1368 - MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && 1369 - (cur_match_dist <= TDEFL_LZ_DICT_SIZE)); 1370 - 1371 - cur_match_dist--; 1372 - 1373 - pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN); 1374 - #ifdef MINIZ_UNALIGNED_USE_MEMCPY 1375 - memcpy(&pLZ_code_buf[1], &cur_match_dist, sizeof(cur_match_dist)); 1376 - #else 1377 - *(mz_uint16*)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist; 1378 - #endif 1379 - pLZ_code_buf += 3; 1380 - *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80); 1381 - 1382 - s0 = s_tdefl_small_dist_sym[cur_match_dist & 511]; 1383 - s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8]; 1384 - d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++; 1385 - 1386 - d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++; 1387 - } 1388 - } else { 1389 - *pLZ_code_buf++ = (mz_uint8)first_trigram; 1390 - *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); 1391 - d->m_huff_count[0][(mz_uint8)first_trigram]++; 1392 - } 1393 - 1394 - if (--num_flags_left == 0) { 1395 - num_flags_left = 8; 1396 - pLZ_flags = pLZ_code_buf++; 1397 - } 1398 - 1399 - total_lz_bytes += cur_match_len; 1400 - lookahead_pos += cur_match_len; 1401 - dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE); 1402 - cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK; 1403 - MZ_ASSERT(lookahead_size >= cur_match_len); 1404 - lookahead_size -= cur_match_len; 1405 - 1406 - if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) { 1407 - int n; 1408 - d->m_lookahead_pos = lookahead_pos; 1409 - d->m_lookahead_size = lookahead_size; 1410 - d->m_dict_size = dict_size; 1411 - d->m_total_lz_bytes = total_lz_bytes; 1412 - d->m_pLZ_code_buf = pLZ_code_buf; 1413 - d->m_pLZ_flags = pLZ_flags; 1414 - d->m_num_flags_left = num_flags_left; 1415 - if ((n = tdefl_flush_block(d, 0)) != 0) return (n < 0) ? MZ_FALSE : MZ_TRUE; 1416 - total_lz_bytes = d->m_total_lz_bytes; 1417 - pLZ_code_buf = d->m_pLZ_code_buf; 1418 - pLZ_flags = d->m_pLZ_flags; 1419 - num_flags_left = d->m_num_flags_left; 1420 - } 1421 - } 1422 - 1423 - while (lookahead_size) { 1424 - mz_uint8 lit = d->m_dict[cur_pos]; 1425 - 1426 - total_lz_bytes++; 1427 - *pLZ_code_buf++ = lit; 1428 - *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1); 1429 - if (--num_flags_left == 0) { 1430 - num_flags_left = 8; 1431 - pLZ_flags = pLZ_code_buf++; 1432 - } 1433 - 1434 - d->m_huff_count[0][lit]++; 1435 - 1436 - lookahead_pos++; 1437 - dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE); 1438 - cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; 1439 - lookahead_size--; 1440 - 1441 - if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) { 1442 - int n; 1443 - d->m_lookahead_pos = lookahead_pos; 1444 - d->m_lookahead_size = lookahead_size; 1445 - d->m_dict_size = dict_size; 1446 - d->m_total_lz_bytes = total_lz_bytes; 1447 - d->m_pLZ_code_buf = pLZ_code_buf; 1448 - d->m_pLZ_flags = pLZ_flags; 1449 - d->m_num_flags_left = num_flags_left; 1450 - if ((n = tdefl_flush_block(d, 0)) != 0) return (n < 0) ? MZ_FALSE : MZ_TRUE; 1451 - total_lz_bytes = d->m_total_lz_bytes; 1452 - pLZ_code_buf = d->m_pLZ_code_buf; 1453 - pLZ_flags = d->m_pLZ_flags; 1454 - num_flags_left = d->m_num_flags_left; 1455 - } 1456 - } 1457 - } 1458 - 1459 - d->m_lookahead_pos = lookahead_pos; 1460 - d->m_lookahead_size = lookahead_size; 1461 - d->m_dict_size = dict_size; 1462 - d->m_total_lz_bytes = total_lz_bytes; 1463 - d->m_pLZ_code_buf = pLZ_code_buf; 1464 - d->m_pLZ_flags = pLZ_flags; 1465 - d->m_num_flags_left = num_flags_left; 1466 - return MZ_TRUE; 1467 - } 1468 - #endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */ 1469 - 1470 - static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor* d, mz_uint8 lit) { 1471 - d->m_total_lz_bytes++; 1472 - *d->m_pLZ_code_buf++ = lit; 1473 - *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1); 1474 - if (--d->m_num_flags_left == 0) { 1475 - d->m_num_flags_left = 8; 1476 - d->m_pLZ_flags = d->m_pLZ_code_buf++; 1477 - } 1478 - d->m_huff_count[0][lit]++; 1479 - } 1480 - 1481 - static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor* d, mz_uint match_len, mz_uint match_dist) { 1482 - mz_uint32 s0, s1; 1483 - 1484 - MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE)); 1485 - 1486 - d->m_total_lz_bytes += match_len; 1487 - 1488 - d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN); 1489 - 1490 - match_dist -= 1; 1491 - d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF); 1492 - d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8); 1493 - d->m_pLZ_code_buf += 3; 1494 - 1495 - *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80); 1496 - if (--d->m_num_flags_left == 0) { 1497 - d->m_num_flags_left = 8; 1498 - d->m_pLZ_flags = d->m_pLZ_code_buf++; 1499 - } 1500 - 1501 - s0 = s_tdefl_small_dist_sym[match_dist & 511]; 1502 - s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127]; 1503 - d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; 1504 - d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++; 1505 - } 1506 - 1507 - static mz_bool tdefl_compress_normal(tdefl_compressor* d) { 1508 - const mz_uint8* pSrc = d->m_pSrc; 1509 - size_t src_buf_left = d->m_src_buf_left; 1510 - tdefl_flush flush = d->m_flush; 1511 - 1512 - while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) { 1513 - mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos; 1514 - /* Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN. */ 1515 - if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1)) { 1516 - mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK, 1517 - ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2; 1518 - mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ 1519 - d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK]; 1520 - mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size); 1521 - const mz_uint8* pSrc_end = pSrc + num_bytes_to_process; 1522 - src_buf_left -= num_bytes_to_process; 1523 - d->m_lookahead_size += num_bytes_to_process; 1524 - while (pSrc != pSrc_end) { 1525 - mz_uint8 c = *pSrc++; 1526 - d->m_dict[dst_pos] = c; 1527 - if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; 1528 - hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1); 1529 - d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; 1530 - d->m_hash[hash] = (mz_uint16)(ins_pos); 1531 - dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; 1532 - ins_pos++; 1533 - } 1534 - } else { 1535 - while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) { 1536 - mz_uint8 c = *pSrc++; 1537 - mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK; 1538 - src_buf_left--; 1539 - d->m_dict[dst_pos] = c; 1540 - if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c; 1541 - if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN) { 1542 - mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2; 1543 - mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << (TDEFL_LZ_HASH_SHIFT * 2)) ^ 1544 - (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ c) & 1545 - (TDEFL_LZ_HASH_SIZE - 1); 1546 - d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash]; 1547 - d->m_hash[hash] = (mz_uint16)(ins_pos); 1548 - } 1549 - } 1550 - } 1551 - d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size); 1552 - if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) break; 1553 - 1554 - /* Simple lazy/greedy parsing state machine. */ 1555 - len_to_move = 1; 1556 - cur_match_dist = 0; 1557 - cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1); 1558 - cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK; 1559 - if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS)) { 1560 - if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) { 1561 - mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK]; 1562 - cur_match_len = 0; 1563 - while (cur_match_len < d->m_lookahead_size) { 1564 - if (d->m_dict[cur_pos + cur_match_len] != c) break; 1565 - cur_match_len++; 1566 - } 1567 - if (cur_match_len < TDEFL_MIN_MATCH_LEN) 1568 - cur_match_len = 0; 1569 - else 1570 - cur_match_dist = 1; 1571 - } 1572 - } else { 1573 - tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len); 1574 - } 1575 - if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || 1576 - ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) { 1577 - cur_match_dist = cur_match_len = 0; 1578 - } 1579 - if (d->m_saved_match_len) { 1580 - if (cur_match_len > d->m_saved_match_len) { 1581 - tdefl_record_literal(d, (mz_uint8)d->m_saved_lit); 1582 - if (cur_match_len >= 128) { 1583 - tdefl_record_match(d, cur_match_len, cur_match_dist); 1584 - d->m_saved_match_len = 0; 1585 - len_to_move = cur_match_len; 1586 - } else { 1587 - d->m_saved_lit = d->m_dict[cur_pos]; 1588 - d->m_saved_match_dist = cur_match_dist; 1589 - d->m_saved_match_len = cur_match_len; 1590 - } 1591 - } else { 1592 - tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist); 1593 - len_to_move = d->m_saved_match_len - 1; 1594 - d->m_saved_match_len = 0; 1595 - } 1596 - } else if (!cur_match_dist) 1597 - tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]); 1598 - else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) { 1599 - tdefl_record_match(d, cur_match_len, cur_match_dist); 1600 - len_to_move = cur_match_len; 1601 - } else { 1602 - d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]; 1603 - d->m_saved_match_dist = cur_match_dist; 1604 - d->m_saved_match_len = cur_match_len; 1605 - } 1606 - /* Move the lookahead forward by len_to_move bytes. */ 1607 - d->m_lookahead_pos += len_to_move; 1608 - MZ_ASSERT(d->m_lookahead_size >= len_to_move); 1609 - d->m_lookahead_size -= len_to_move; 1610 - d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE); 1611 - /* Check if it's time to flush the current LZ codes to the internal output buffer. */ 1612 - if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) || 1613 - ((d->m_total_lz_bytes > 31 * 1024) && 1614 - (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || 1615 - (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))) { 1616 - int n; 1617 - d->m_pSrc = pSrc; 1618 - d->m_src_buf_left = src_buf_left; 1619 - if ((n = tdefl_flush_block(d, 0)) != 0) return (n < 0) ? MZ_FALSE : MZ_TRUE; 1620 - } 1621 - } 1622 - 1623 - d->m_pSrc = pSrc; 1624 - d->m_src_buf_left = src_buf_left; 1625 - return MZ_TRUE; 1626 - } 1627 - 1628 - static tdefl_status tdefl_flush_output_buffer(tdefl_compressor* d) { 1629 - if (d->m_pIn_buf_size) { 1630 - *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8*)d->m_pIn_buf; 1631 - } 1632 - 1633 - if (d->m_pOut_buf_size) { 1634 - size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining); 1635 - memcpy((mz_uint8*)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n); 1636 - d->m_output_flush_ofs += (mz_uint)n; 1637 - d->m_output_flush_remaining -= (mz_uint)n; 1638 - d->m_out_buf_ofs += n; 1639 - 1640 - *d->m_pOut_buf_size = d->m_out_buf_ofs; 1641 - } 1642 - 1643 - return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY; 1644 - } 1645 - 1646 - tdefl_status tdefl_compress(tdefl_compressor* d, const void* pIn_buf, size_t* pIn_buf_size, void* pOut_buf, 1647 - size_t* pOut_buf_size, tdefl_flush flush) { 1648 - if (!d) { 1649 - if (pIn_buf_size) *pIn_buf_size = 0; 1650 - if (pOut_buf_size) *pOut_buf_size = 0; 1651 - return TDEFL_STATUS_BAD_PARAM; 1652 - } 1653 - 1654 - d->m_pIn_buf = pIn_buf; 1655 - d->m_pIn_buf_size = pIn_buf_size; 1656 - d->m_pOut_buf = pOut_buf; 1657 - d->m_pOut_buf_size = pOut_buf_size; 1658 - d->m_pSrc = (const mz_uint8*)(pIn_buf); 1659 - d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0; 1660 - d->m_out_buf_ofs = 0; 1661 - d->m_flush = flush; 1662 - 1663 - if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || 1664 - (d->m_prev_return_status != TDEFL_STATUS_OKAY) || (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || 1665 - (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) { 1666 - if (pIn_buf_size) *pIn_buf_size = 0; 1667 - if (pOut_buf_size) *pOut_buf_size = 0; 1668 - return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM); 1669 - } 1670 - d->m_wants_to_finish |= (flush == TDEFL_FINISH); 1671 - 1672 - if ((d->m_output_flush_remaining) || (d->m_finished)) return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); 1673 - 1674 - #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN 1675 - if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) && ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) && 1676 - ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0)) { 1677 - if (!tdefl_compress_fast(d)) return d->m_prev_return_status; 1678 - } else 1679 - #endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */ 1680 - { 1681 - if (!tdefl_compress_normal(d)) return d->m_prev_return_status; 1682 - } 1683 - 1684 - if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf)) 1685 - d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8*)pIn_buf, d->m_pSrc - (const mz_uint8*)pIn_buf); 1686 - 1687 - if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) { 1688 - if (tdefl_flush_block(d, flush) < 0) return d->m_prev_return_status; 1689 - d->m_finished = (flush == TDEFL_FINISH); 1690 - if (flush == TDEFL_FULL_FLUSH) { 1691 - MZ_CLEAR_OBJ(d->m_hash); 1692 - MZ_CLEAR_OBJ(d->m_next); 1693 - d->m_dict_size = 0; 1694 - } 1695 - } 1696 - 1697 - return (d->m_prev_return_status = tdefl_flush_output_buffer(d)); 1698 - } 1699 - 1700 - tdefl_status tdefl_compress_buffer(tdefl_compressor* d, const void* pIn_buf, size_t in_buf_size, tdefl_flush flush) { 1701 - MZ_ASSERT(d->m_pPut_buf_func); 1702 - return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush); 1703 - } 1704 - 1705 - tdefl_status tdefl_init(tdefl_compressor* d, tdefl_put_buf_func_ptr pPut_buf_func, void* pPut_buf_user, int flags) { 1706 - d->m_pPut_buf_func = pPut_buf_func; 1707 - d->m_pPut_buf_user = pPut_buf_user; 1708 - d->m_flags = (mz_uint)(flags); 1709 - d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3; 1710 - d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0; 1711 - d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3; 1712 - if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_OBJ(d->m_hash); 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 = 1714 - d->m_bits_in = 0; 1715 - d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = 1716 - d->m_wants_to_finish = 0; 1717 - d->m_pLZ_code_buf = d->m_lz_code_buf + 1; 1718 - d->m_pLZ_flags = d->m_lz_code_buf; 1719 - *d->m_pLZ_flags = 0; 1720 - d->m_num_flags_left = 8; 1721 - d->m_pOutput_buf = d->m_output_buf; 1722 - d->m_pOutput_buf_end = d->m_output_buf; 1723 - d->m_prev_return_status = TDEFL_STATUS_OKAY; 1724 - d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0; 1725 - d->m_adler32 = 1; 1726 - d->m_pIn_buf = NULL; 1727 - d->m_pOut_buf = NULL; 1728 - d->m_pIn_buf_size = NULL; 1729 - d->m_pOut_buf_size = NULL; 1730 - d->m_flush = TDEFL_NO_FLUSH; 1731 - d->m_pSrc = NULL; 1732 - d->m_src_buf_left = 0; 1733 - d->m_out_buf_ofs = 0; 1734 - if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) MZ_CLEAR_OBJ(d->m_dict); 1735 - memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); 1736 - memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); 1737 - return TDEFL_STATUS_OKAY; 1738 - } 1739 - 1740 - tdefl_status tdefl_get_prev_return_status(tdefl_compressor* d) { return d->m_prev_return_status; } 1741 - 1742 - mz_uint32 tdefl_get_adler32(tdefl_compressor* d) { return d->m_adler32; } 1743 - 1744 - mz_bool tdefl_compress_mem_to_output(const void* pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, 1745 - void* pPut_buf_user, int flags) { 1746 - tdefl_compressor* pComp; 1747 - mz_bool succeeded; 1748 - if (((buf_len) && (!pBuf)) || (!pPut_buf_func)) return MZ_FALSE; 1749 - pComp = (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor)); 1750 - if (!pComp) return MZ_FALSE; 1751 - succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY); 1752 - succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE); 1753 - MZ_FREE(pComp); 1754 - return succeeded; 1755 - } 1756 - 1757 - typedef struct { 1758 - size_t m_size, m_capacity; 1759 - mz_uint8* m_pBuf; 1760 - mz_bool m_expandable; 1761 - } tdefl_output_buffer; 1762 - 1763 - static mz_bool tdefl_output_buffer_putter(const void* pBuf, int len, void* pUser) { 1764 - tdefl_output_buffer* p = (tdefl_output_buffer*)pUser; 1765 - size_t new_size = p->m_size + len; 1766 - if (new_size > p->m_capacity) { 1767 - size_t new_capacity = p->m_capacity; 1768 - mz_uint8* pNew_buf; 1769 - if (!p->m_expandable) return MZ_FALSE; 1770 - do { 1771 - new_capacity = MZ_MAX(128U, new_capacity << 1U); 1772 - } while (new_size > new_capacity); 1773 - pNew_buf = (mz_uint8*)MZ_REALLOC(p->m_pBuf, new_capacity); 1774 - if (!pNew_buf) return MZ_FALSE; 1775 - p->m_pBuf = pNew_buf; 1776 - p->m_capacity = new_capacity; 1777 - } 1778 - memcpy((mz_uint8*)p->m_pBuf + p->m_size, pBuf, len); 1779 - p->m_size = new_size; 1780 - return MZ_TRUE; 1781 - } 1782 - 1783 - void* tdefl_compress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags) { 1784 - tdefl_output_buffer out_buf; 1785 - MZ_CLEAR_OBJ(out_buf); 1786 - if (!pOut_len) 1787 - return MZ_FALSE; 1788 - else 1789 - *pOut_len = 0; 1790 - out_buf.m_expandable = MZ_TRUE; 1791 - if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) return NULL; 1792 - *pOut_len = out_buf.m_size; 1793 - return out_buf.m_pBuf; 1794 - } 1795 - 1796 - size_t tdefl_compress_mem_to_mem(void* pOut_buf, size_t out_buf_len, const void* pSrc_buf, size_t src_buf_len, 1797 - int flags) { 1798 - tdefl_output_buffer out_buf; 1799 - MZ_CLEAR_OBJ(out_buf); 1800 - if (!pOut_buf) return 0; 1801 - out_buf.m_pBuf = (mz_uint8*)pOut_buf; 1802 - out_buf.m_capacity = out_buf_len; 1803 - if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) return 0; 1804 - return out_buf.m_size; 1805 - } 1806 - 1807 - static const mz_uint s_tdefl_num_probes[11] = {0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500}; 1808 - 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 1810 - * if throughput to fall off a cliff on some files). */ 1811 - mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy) { 1812 - mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | 1813 - ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); 1814 - if (window_bits > 0) comp_flags |= TDEFL_WRITE_ZLIB_HEADER; 1815 - 1816 - if (!level) 1817 - comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; 1818 - else if (strategy == MZ_FILTERED) 1819 - comp_flags |= TDEFL_FILTER_MATCHES; 1820 - else if (strategy == MZ_HUFFMAN_ONLY) 1821 - comp_flags &= ~TDEFL_MAX_PROBES_MASK; 1822 - else if (strategy == MZ_FIXED) 1823 - comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS; 1824 - else if (strategy == MZ_RLE) 1825 - comp_flags |= TDEFL_RLE_MATCHES; 1826 - 1827 - return comp_flags; 1828 - } 1829 - 1830 - #ifdef _MSC_VER 1831 - #pragma warning(push) 1832 - #pragma warning(disable : 4204) /* nonstandard extension used : non-constant aggregate initializer (also supported by \ 1833 - GNU C and C99, so no big deal) */ 1834 - #endif 1835 - 1836 - /* Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more 1837 - context at http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/. This is actually a modification of Alex's 1838 - original code so PNG files generated by this function pass pngcheck. */ 1839 - void* tdefl_write_image_to_png_file_in_memory_ex(const void* pImage, int w, int h, int num_chans, size_t* pLen_out, 1840 - mz_uint level, mz_bool flip) { 1841 - /* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. */ 1842 - static const mz_uint s_tdefl_png_num_probes[11] = {0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500}; 1843 - tdefl_compressor* pComp = (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor)); 1844 - tdefl_output_buffer out_buf; 1845 - int i, bpl = w * num_chans, y, z; 1846 - mz_uint32 c; 1847 - *pLen_out = 0; 1848 - if (!pComp) return NULL; 1849 - MZ_CLEAR_OBJ(out_buf); 1850 - out_buf.m_expandable = MZ_TRUE; 1851 - out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h); 1852 - if (NULL == (out_buf.m_pBuf = (mz_uint8*)MZ_MALLOC(out_buf.m_capacity))) { 1853 - MZ_FREE(pComp); 1854 - return NULL; 1855 - } 1856 - /* write dummy header */ 1857 - for (z = 41; z; --z) tdefl_output_buffer_putter(&z, 1, &out_buf); 1858 - /* compress image data */ 1859 - tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, 1860 - s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER); 1861 - for (y = 0; y < h; ++y) { 1862 - tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH); 1863 - tdefl_compress_buffer(pComp, (mz_uint8*)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH); 1864 - } 1865 - if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE) { 1866 - MZ_FREE(pComp); 1867 - MZ_FREE(out_buf.m_pBuf); 1868 - return NULL; 1869 - } 1870 - /* write real header */ 1871 - *pLen_out = out_buf.m_size - 41; 1872 - { 1873 - static const mz_uint8 chans[] = {0x00, 0x00, 0x04, 0x02, 0x06}; 1874 - mz_uint8 pnghdr[41] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 1875 - 0x44, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 1876 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x44, 0x41, 0x54}; 1877 - pnghdr[18] = (mz_uint8)(w >> 8); 1878 - pnghdr[19] = (mz_uint8)w; 1879 - pnghdr[22] = (mz_uint8)(h >> 8); 1880 - pnghdr[23] = (mz_uint8)h; 1881 - pnghdr[25] = chans[num_chans]; 1882 - pnghdr[33] = (mz_uint8)(*pLen_out >> 24); 1883 - pnghdr[34] = (mz_uint8)(*pLen_out >> 16); 1884 - pnghdr[35] = (mz_uint8)(*pLen_out >> 8); 1885 - pnghdr[36] = (mz_uint8)*pLen_out; 1886 - c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17); 1887 - for (i = 0; i < 4; ++i, c <<= 8) ((mz_uint8*)(pnghdr + 29))[i] = (mz_uint8)(c >> 24); 1888 - memcpy(out_buf.m_pBuf, pnghdr, 41); 1889 - } 1890 - /* write footer (IDAT CRC-32, followed by IEND chunk) */ 1891 - if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) { 1892 - *pLen_out = 0; 1893 - MZ_FREE(pComp); 1894 - MZ_FREE(out_buf.m_pBuf); 1895 - return NULL; 1896 - } 1897 - c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4); 1898 - for (i = 0; i < 4; ++i, c <<= 8) (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24); 1899 - /* compute final size of file, grab compressed data buffer and return */ 1900 - *pLen_out += 57; 1901 - MZ_FREE(pComp); 1902 - return out_buf.m_pBuf; 1903 - } 1904 - void* tdefl_write_image_to_png_file_in_memory(const void* pImage, int w, int h, int num_chans, size_t* pLen_out) { 1905 - /* Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we can't depend on MZ_DEFAULT_LEVEL being 1906 - * available in case the zlib API's where #defined out) */ 1907 - return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, pLen_out, 6, MZ_FALSE); 1908 - } 1909 - 1910 - #ifndef MINIZ_NO_MALLOC 1911 - /* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */ 1912 - /* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */ 1913 - /* structure size and allocation mechanism. */ 1914 - tdefl_compressor* tdefl_compressor_alloc() { return (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor)); } 1915 - 1916 - void tdefl_compressor_free(tdefl_compressor* pComp) { MZ_FREE(pComp); } 1917 - #endif 1918 - 1919 - #ifdef _MSC_VER 1920 - #pragma warning(pop) 1921 - #endif 1922 - 1923 - #ifdef __cplusplus 1924 - } 1925 - #endif 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 - **************************************************************************/ 1951 - 1952 - #ifdef __cplusplus 1953 - extern "C" { 1954 - #endif 1955 - 1956 - /* ------------------- Low-level Decompression (completely independent from all compression API's) */ 1957 - 1958 - #define TINFL_MEMCPY(d, s, l) memcpy(d, s, l) 1959 - #define TINFL_MEMSET(p, c, l) memset(p, c, l) 1960 - 1961 - #define TINFL_CR_BEGIN \ 1962 - switch (r->m_state) { \ 1963 - case 0: 1964 - #define TINFL_CR_RETURN(state_index, result) \ 1965 - do { \ 1966 - status = result; \ 1967 - r->m_state = state_index; \ 1968 - goto common_exit; \ 1969 - case state_index:; \ 1970 - } \ 1971 - MZ_MACRO_END 1972 - #define TINFL_CR_RETURN_FOREVER(state_index, result) \ 1973 - do { \ 1974 - for (;;) { \ 1975 - TINFL_CR_RETURN(state_index, result); \ 1976 - } \ 1977 - } \ 1978 - MZ_MACRO_END 1979 - #define TINFL_CR_FINISH } 1980 - 1981 - #define TINFL_GET_BYTE(state_index, c) \ 1982 - do { \ 1983 - while (pIn_buf_cur >= pIn_buf_end) { \ 1984 - TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) \ 1985 - ? TINFL_STATUS_NEEDS_MORE_INPUT \ 1986 - : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \ 1987 - } \ 1988 - c = *pIn_buf_cur++; \ 1989 - } \ 1990 - MZ_MACRO_END 1991 - 1992 - #define TINFL_NEED_BITS(state_index, n) \ 1993 - do { \ 1994 - mz_uint c; \ 1995 - TINFL_GET_BYTE(state_index, c); \ 1996 - bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ 1997 - num_bits += 8; \ 1998 - } while (num_bits < (mz_uint)(n)) 1999 - #define TINFL_SKIP_BITS(state_index, n) \ 2000 - do { \ 2001 - if (num_bits < (mz_uint)(n)) { \ 2002 - TINFL_NEED_BITS(state_index, n); \ 2003 - } \ 2004 - bit_buf >>= (n); \ 2005 - num_bits -= (n); \ 2006 - } \ 2007 - MZ_MACRO_END 2008 - #define TINFL_GET_BITS(state_index, b, n) \ 2009 - do { \ 2010 - if (num_bits < (mz_uint)(n)) { \ 2011 - TINFL_NEED_BITS(state_index, n); \ 2012 - } \ 2013 - b = bit_buf & ((1 << (n)) - 1); \ 2014 - bit_buf >>= (n); \ 2015 - num_bits -= (n); \ 2016 - } \ 2017 - MZ_MACRO_END 2018 - 2019 - /* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. 2020 - */ 2021 - /* It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no 2022 - * more). It works by trying to fully decode a */ 2023 - /* Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, 2024 - * and tries again until it succeeds or until the */ 2025 - /* bit buffer contains >=15 bits (deflate's max. Huffman code size). */ 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; \ 2042 - } while (num_bits < 15); 2043 - 2044 - /* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because 2045 - * the zlib API expects the decompressor to never read */ 2046 - /* beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the 2047 - * input, it REALLY needs another byte in order to fully */ 2048 - /* decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, 2049 - * which aren't followed by a byte aligned adler-32. */ 2050 - /* The slow path is only executed at the very end of the input buffer. */ 2051 - /* v1.16: The original macro handled the case at the very end of the passed-in input buffer, but we also need to handle 2052 - * the case where the user passes in 1+zillion bytes */ 2053 - /* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much 2054 - * trickier. */ 2055 - #define TINFL_HUFF_DECODE(state_index, sym, pHuff) \ 2056 - do { \ 2057 - int temp; \ 2058 - mz_uint code_len, c; \ 2059 - if (num_bits < 15) { \ 2060 - if ((pIn_buf_end - pIn_buf_cur) < 2) { \ 2061 - TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \ 2062 - } else { \ 2063 - bit_buf |= \ 2064 - (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \ 2065 - pIn_buf_cur += 2; \ 2066 - num_bits += 16; \ 2067 - } \ 2068 - } \ 2069 - if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \ 2070 - code_len = temp >> 9, temp &= 511; \ 2071 - else { \ 2072 - code_len = TINFL_FAST_LOOKUP_BITS; \ 2073 - do { \ 2074 - temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ 2075 - } while (temp < 0); \ 2076 - } \ 2077 - sym = temp; \ 2078 - bit_buf >>= code_len; \ 2079 - num_bits -= code_len; \ 2080 - } \ 2081 - MZ_MACRO_END 2082 - 2083 - tinfl_status tinfl_decompress(tinfl_decompressor* r, const mz_uint8* pIn_buf_next, size_t* pIn_buf_size, 2084 - mz_uint8* pOut_buf_start, mz_uint8* pOut_buf_next, size_t* pOut_buf_size, 2085 - const mz_uint32 decomp_flags) { 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}; 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}; 2096 - static const int s_min_table_sizes[3] = {257, 1, 4}; 2097 - 2098 - tinfl_status status = TINFL_STATUS_FAILED; 2099 - mz_uint32 num_bits, dist, counter, num_extra; 2100 - tinfl_bit_buf_t bit_buf; 2101 - const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size; 2102 - mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; 2103 - size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) 2104 - ? (size_t)-1 2105 - : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, 2106 - dist_from_out_buf_start; 2107 - 2108 - /* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output 2109 - * file (in which case it doesn't matter). */ 2110 - if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) { 2111 - *pIn_buf_size = *pOut_buf_size = 0; 2112 - return TINFL_STATUS_BAD_PARAM; 2113 - } 2114 - 2115 - num_bits = r->m_num_bits; 2116 - bit_buf = r->m_bit_buf; 2117 - dist = r->m_dist; 2118 - counter = r->m_counter; 2119 - num_extra = r->m_num_extra; 2120 - dist_from_out_buf_start = r->m_dist_from_out_buf_start; 2121 - TINFL_CR_BEGIN 2122 - 2123 - bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; 2124 - r->m_z_adler32 = r->m_check_adler32 = 1; 2125 - if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) { 2126 - TINFL_GET_BYTE(1, r->m_zhdr0); 2127 - TINFL_GET_BYTE(2, r->m_zhdr1); 2128 - counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); 2129 - if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 2130 - counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || 2131 - ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4))))); 2132 - if (counter) { 2133 - TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); 2134 - } 2135 - } 2136 - 2137 - do { 2138 - TINFL_GET_BITS(3, r->m_final, 3); 2139 - r->m_type = r->m_final >> 1; 2140 - if (r->m_type == 0) { 2141 - TINFL_SKIP_BITS(5, num_bits & 7); 2142 - for (counter = 0; counter < 4; ++counter) { 2143 - if (num_bits) 2144 - TINFL_GET_BITS(6, r->m_raw_header[counter], 8); 2145 - else 2146 - TINFL_GET_BYTE(7, r->m_raw_header[counter]); 2147 - } 2148 - if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != 2149 - (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) { 2150 - TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); 2151 - } 2152 - while ((counter) && (num_bits)) { 2153 - TINFL_GET_BITS(51, dist, 8); 2154 - while (pOut_buf_cur >= pOut_buf_end) { 2155 - TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); 2156 - } 2157 - *pOut_buf_cur++ = (mz_uint8)dist; 2158 - counter--; 2159 - } 2160 - while (counter) { 2161 - size_t n; 2162 - while (pOut_buf_cur >= pOut_buf_end) { 2163 - TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); 2164 - } 2165 - while (pIn_buf_cur >= pIn_buf_end) { 2166 - TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT 2167 - : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); 2168 - } 2169 - n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter); 2170 - TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); 2171 - pIn_buf_cur += n; 2172 - pOut_buf_cur += n; 2173 - counter -= (mz_uint)n; 2174 - } 2175 - } else if (r->m_type == 3) { 2176 - TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED); 2177 - } else { 2178 - if (r->m_type == 1) { 2179 - mz_uint8* p = r->m_tables[0].m_code_size; 2180 - mz_uint i; 2181 - r->m_table_sizes[0] = 288; 2182 - r->m_table_sizes[1] = 32; 2183 - TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32); 2184 - for (i = 0; i <= 143; ++i) *p++ = 8; 2185 - for (; i <= 255; ++i) *p++ = 9; 2186 - for (; i <= 279; ++i) *p++ = 7; 2187 - for (; i <= 287; ++i) *p++ = 8; 2188 - } else { 2189 - for (counter = 0; counter < 3; counter++) { 2190 - TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); 2191 - r->m_table_sizes[counter] += s_min_table_sizes[counter]; 2192 - } 2193 - MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); 2194 - for (counter = 0; counter < r->m_table_sizes[2]; counter++) { 2195 - mz_uint s; 2196 - TINFL_GET_BITS(14, s, 3); 2197 - r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; 2198 - } 2199 - r->m_table_sizes[2] = 19; 2200 - } 2201 - for (; (int)r->m_type >= 0; r->m_type--) { 2202 - int tree_next, tree_cur; 2203 - tinfl_huff_table* pTable; 2204 - mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; 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]]++; 2210 - used_syms = 0, total = 0; 2211 - next_code[0] = next_code[1] = 0; 2212 - for (i = 1; i <= 15; ++i) { 2213 - used_syms += total_syms[i]; 2214 - next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); 2215 - } 2216 - if ((65536 != total) && (used_syms > 1)) { 2217 - TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED); 2218 - } 2219 - for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) { 2220 - mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; 2221 - if (!code_size) continue; 2222 - cur_code = next_code[code_size]++; 2223 - for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1); 2224 - if (code_size <= TINFL_FAST_LOOKUP_BITS) { 2225 - mz_int16 k = (mz_int16)((code_size << 9) | sym_index); 2226 - while (rev_code < TINFL_FAST_LOOKUP_SIZE) { 2227 - pTable->m_look_up[rev_code] = k; 2228 - rev_code += (1 << code_size); 2229 - } 2230 - continue; 2231 - } 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; 2234 - tree_cur = tree_next; 2235 - tree_next -= 2; 2236 - } 2237 - rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1); 2238 - for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) { 2239 - tree_cur -= ((rev_code >>= 1) & 1); 2240 - if (!pTable->m_tree[-tree_cur - 1]) { 2241 - pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; 2242 - tree_cur = tree_next; 2243 - tree_next -= 2; 2244 - } else 2245 - tree_cur = pTable->m_tree[-tree_cur - 1]; 2246 - } 2247 - tree_cur -= ((rev_code >>= 1) & 1); 2248 - pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index; 2249 - } 2250 - if (r->m_type == 2) { 2251 - for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) { 2252 - mz_uint s; 2253 - TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); 2254 - if (dist < 16) { 2255 - r->m_len_codes[counter++] = (mz_uint8)dist; 2256 - continue; 2257 - } 2258 - if ((dist == 16) && (!counter)) { 2259 - TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED); 2260 - } 2261 - num_extra = "\02\03\07"[dist - 16]; 2262 - TINFL_GET_BITS(18, s, num_extra); 2263 - s += "\03\03\013"[dist - 16]; 2264 - TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); 2265 - counter += s; 2266 - } 2267 - if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) { 2268 - TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED); 2269 - } 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]); 2272 - } 2273 - } 2274 - for (;;) { 2275 - mz_uint8* pSrc; 2276 - for (;;) { 2277 - if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) { 2278 - TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]); 2279 - if (counter >= 256) break; 2280 - while (pOut_buf_cur >= pOut_buf_end) { 2281 - TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); 2282 - } 2283 - *pOut_buf_cur++ = (mz_uint8)counter; 2284 - } else { 2285 - int sym2; 2286 - mz_uint code_len; 2287 - #if TINFL_USE_64BIT_BITBUF 2288 - if (num_bits < 30) { 2289 - bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); 2290 - pIn_buf_cur += 4; 2291 - num_bits += 32; 2292 - } 2293 - #else 2294 - if (num_bits < 15) { 2295 - bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); 2296 - pIn_buf_cur += 2; 2297 - num_bits += 16; 2298 - } 2299 - #endif 2300 - if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 2301 - code_len = sym2 >> 9; 2302 - else { 2303 - code_len = TINFL_FAST_LOOKUP_BITS; 2304 - do { 2305 - sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; 2306 - } while (sym2 < 0); 2307 - } 2308 - counter = sym2; 2309 - bit_buf >>= code_len; 2310 - num_bits -= code_len; 2311 - if (counter & 256) break; 2312 - 2313 - #if !TINFL_USE_64BIT_BITBUF 2314 - if (num_bits < 15) { 2315 - bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); 2316 - pIn_buf_cur += 2; 2317 - num_bits += 16; 2318 - } 2319 - #endif 2320 - if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 2321 - code_len = sym2 >> 9; 2322 - else { 2323 - code_len = TINFL_FAST_LOOKUP_BITS; 2324 - do { 2325 - sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; 2326 - } while (sym2 < 0); 2327 - } 2328 - bit_buf >>= code_len; 2329 - num_bits -= code_len; 2330 - 2331 - pOut_buf_cur[0] = (mz_uint8)counter; 2332 - if (sym2 & 256) { 2333 - pOut_buf_cur++; 2334 - counter = sym2; 2335 - break; 2336 - } 2337 - pOut_buf_cur[1] = (mz_uint8)sym2; 2338 - pOut_buf_cur += 2; 2339 - } 2340 - } 2341 - if ((counter &= 511) == 256) break; 2342 - 2343 - num_extra = s_length_extra[counter - 257]; 2344 - counter = s_length_base[counter - 257]; 2345 - if (num_extra) { 2346 - mz_uint extra_bits; 2347 - TINFL_GET_BITS(25, extra_bits, num_extra); 2348 - counter += extra_bits; 2349 - } 2350 - 2351 - TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]); 2352 - num_extra = s_dist_extra[dist]; 2353 - dist = s_dist_base[dist]; 2354 - if (num_extra) { 2355 - mz_uint extra_bits; 2356 - TINFL_GET_BITS(27, extra_bits, num_extra); 2357 - dist += extra_bits; 2358 - } 2359 - 2360 - dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; 2361 - if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && 2362 - (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) { 2363 - TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); 2364 - } 2365 - 2366 - pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask); 2367 - 2368 - if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) { 2369 - while (counter--) { 2370 - while (pOut_buf_cur >= pOut_buf_end) { 2371 - TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); 2372 - } 2373 - *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask]; 2374 - } 2375 - continue; 2376 - } 2377 - #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES 2378 - else if ((counter >= 9) && (counter <= dist)) { 2379 - const mz_uint8* pSrc_end = pSrc + (counter & ~7); 2380 - do { 2381 - #ifdef MINIZ_UNALIGNED_USE_MEMCPY 2382 - memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32) * 2); 2383 - #else 2384 - ((mz_uint32*)pOut_buf_cur)[0] = ((const mz_uint32*)pSrc)[0]; 2385 - ((mz_uint32*)pOut_buf_cur)[1] = ((const mz_uint32*)pSrc)[1]; 2386 - #endif 2387 - pOut_buf_cur += 8; 2388 - } while ((pSrc += 8) < pSrc_end); 2389 - if ((counter &= 7) < 3) { 2390 - if (counter) { 2391 - pOut_buf_cur[0] = pSrc[0]; 2392 - if (counter > 1) pOut_buf_cur[1] = pSrc[1]; 2393 - pOut_buf_cur += counter; 2394 - } 2395 - continue; 2396 - } 2397 - } 2398 - #endif 2399 - while (counter > 2) { 2400 - pOut_buf_cur[0] = pSrc[0]; 2401 - pOut_buf_cur[1] = pSrc[1]; 2402 - pOut_buf_cur[2] = pSrc[2]; 2403 - pOut_buf_cur += 3; 2404 - pSrc += 3; 2405 - counter -= 3; 2406 - } 2407 - if (counter > 0) { 2408 - pOut_buf_cur[0] = pSrc[0]; 2409 - if (counter > 1) pOut_buf_cur[1] = pSrc[1]; 2410 - pOut_buf_cur += counter; 2411 - } 2412 - } 2413 - } 2414 - } while (!(r->m_final & 1)); 2415 - 2416 - /* Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other 2417 - * Deflate streams followed by arbitrary data. */ 2418 - /* I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the 2419 - * Adler32 check shouldn't ever need to worry about reading from the bitbuf now. */ 2420 - TINFL_SKIP_BITS(32, num_bits & 7); 2421 - while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) { 2422 - --pIn_buf_cur; 2423 - num_bits -= 8; 2424 - } 2425 - bit_buf &= (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); 2426 - MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end of non-deflate/zlib streams with 2427 - following data (such as gzip streams). */ 2428 - 2429 - if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) { 2430 - for (counter = 0; counter < 4; ++counter) { 2431 - mz_uint s; 2432 - if (num_bits) 2433 - TINFL_GET_BITS(41, s, 8); 2434 - else 2435 - TINFL_GET_BYTE(42, s); 2436 - r->m_z_adler32 = (r->m_z_adler32 << 8) | s; 2437 - } 2438 - } 2439 - TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE); 2440 - 2441 - TINFL_CR_FINISH 2442 - 2443 - common_exit: 2444 - /* As long as we aren't telling the caller that we NEED more input to make forward progress: */ 2445 - /* Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by 2446 - * arbitrary data. */ 2447 - /* We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, 2448 - * though, or we'll lock the caller up into an inf loop. */ 2449 - if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS)) { 2450 - while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) { 2451 - --pIn_buf_cur; 2452 - num_bits -= 8; 2453 - } 2454 - } 2455 - r->m_num_bits = num_bits; 2456 - r->m_bit_buf = bit_buf & (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); 2457 - r->m_dist = dist; 2458 - r->m_counter = counter; 2459 - r->m_num_extra = num_extra; 2460 - r->m_dist_from_out_buf_start = dist_from_out_buf_start; 2461 - *pIn_buf_size = pIn_buf_cur - pIn_buf_next; 2462 - *pOut_buf_size = pOut_buf_cur - pOut_buf_next; 2463 - if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) { 2464 - const mz_uint8* ptr = pOut_buf_next; 2465 - size_t buf_len = *pOut_buf_size; 2466 - mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; 2467 - size_t block_len = buf_len % 5552; 2468 - while (buf_len) { 2469 - for (i = 0; i + 7 < block_len; i += 8, ptr += 8) { 2470 - s1 += ptr[0], s2 += s1; 2471 - s1 += ptr[1], s2 += s1; 2472 - s1 += ptr[2], s2 += s1; 2473 - s1 += ptr[3], s2 += s1; 2474 - s1 += ptr[4], s2 += s1; 2475 - s1 += ptr[5], s2 += s1; 2476 - s1 += ptr[6], s2 += s1; 2477 - s1 += ptr[7], s2 += s1; 2478 - } 2479 - for (; i < block_len; ++i) s1 += *ptr++, s2 += s1; 2480 - s1 %= 65521U, s2 %= 65521U; 2481 - buf_len -= block_len; 2482 - block_len = 5552; 2483 - } 2484 - r->m_check_adler32 = (s2 << 16) + s1; 2485 - if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && 2486 - (r->m_check_adler32 != r->m_z_adler32)) 2487 - status = TINFL_STATUS_ADLER32_MISMATCH; 2488 - } 2489 - return status; 2490 - } 2491 - 2492 - /* Higher level helper functions. */ 2493 - void* tinfl_decompress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags) { 2494 - tinfl_decompressor decomp; 2495 - void *pBuf = NULL, *pNew_buf; 2496 - size_t src_buf_ofs = 0, out_buf_capacity = 0; 2497 - *pOut_len = 0; 2498 - tinfl_init(&decomp); 2499 - for (;;) { 2500 - size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity; 2501 - tinfl_status status = 2502 - tinfl_decompress(&decomp, (const mz_uint8*)pSrc_buf + src_buf_ofs, &src_buf_size, (mz_uint8*)pBuf, 2503 - pBuf ? (mz_uint8*)pBuf + *pOut_len : NULL, &dst_buf_size, 2504 - (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); 2505 - if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) { 2506 - MZ_FREE(pBuf); 2507 - *pOut_len = 0; 2508 - return NULL; 2509 - } 2510 - src_buf_ofs += src_buf_size; 2511 - *pOut_len += dst_buf_size; 2512 - if (status == TINFL_STATUS_DONE) break; 2513 - new_out_buf_capacity = out_buf_capacity * 2; 2514 - if (new_out_buf_capacity < 128) new_out_buf_capacity = 128; 2515 - pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity); 2516 - if (!pNew_buf) { 2517 - MZ_FREE(pBuf); 2518 - *pOut_len = 0; 2519 - return NULL; 2520 - } 2521 - pBuf = pNew_buf; 2522 - out_buf_capacity = new_out_buf_capacity; 2523 - } 2524 - return pBuf; 2525 - } 2526 - 2527 - size_t tinfl_decompress_mem_to_mem(void* pOut_buf, size_t out_buf_len, const void* pSrc_buf, size_t src_buf_len, 2528 - int flags) { 2529 - tinfl_decompressor decomp; 2530 - tinfl_status status; 2531 - tinfl_init(&decomp); 2532 - status = 2533 - tinfl_decompress(&decomp, (const mz_uint8*)pSrc_buf, &src_buf_len, (mz_uint8*)pOut_buf, (mz_uint8*)pOut_buf, 2534 - &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); 2535 - return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len; 2536 - } 2537 - 2538 - int tinfl_decompress_mem_to_callback(const void* pIn_buf, size_t* pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, 2539 - void* pPut_buf_user, int flags) { 2540 - int result = 0; 2541 - tinfl_decompressor decomp; 2542 - mz_uint8* pDict = (mz_uint8*)MZ_MALLOC(TINFL_LZ_DICT_SIZE); 2543 - size_t in_buf_ofs = 0, dict_ofs = 0; 2544 - if (!pDict) return TINFL_STATUS_FAILED; 2545 - tinfl_init(&decomp); 2546 - for (;;) { 2547 - size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs; 2548 - tinfl_status status = tinfl_decompress( 2549 - &decomp, (const mz_uint8*)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size, 2550 - (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))); 2551 - in_buf_ofs += in_buf_size; 2552 - if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user))) break; 2553 - if (status != TINFL_STATUS_HAS_MORE_OUTPUT) { 2554 - result = (status == TINFL_STATUS_DONE); 2555 - break; 2556 - } 2557 - dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1); 2558 - } 2559 - MZ_FREE(pDict); 2560 - *pIn_buf_size = in_buf_ofs; 2561 - return result; 2562 - } 2563 - 2564 - #ifndef MINIZ_NO_MALLOC 2565 - tinfl_decompressor* tinfl_decompressor_alloc() { 2566 - tinfl_decompressor* pDecomp = (tinfl_decompressor*)MZ_MALLOC(sizeof(tinfl_decompressor)); 2567 - if (pDecomp) tinfl_init(pDecomp); 2568 - return pDecomp; 2569 - } 2570 - 2571 - void tinfl_decompressor_free(tinfl_decompressor* pDecomp) { MZ_FREE(pDecomp); } 2572 - #endif 2573 - 2574 - #ifdef __cplusplus 2575 - } 2576 - #endif 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 - **************************************************************************/ 2603 - 2604 - #ifndef MINIZ_NO_ARCHIVE_APIS 2605 - 2606 - #ifdef __cplusplus 2607 - extern "C" { 2608 - #endif 2609 - 2610 - /* ------------------- .ZIP archive reading */ 2611 - 2612 - #ifdef MINIZ_NO_STDIO 2613 - #define MZ_FILE void* 2614 - #else 2615 - #include <sys/stat.h> 2616 - 2617 - #if defined(_MSC_VER) || defined(__MINGW64__) 2618 - static FILE* mz_fopen(const char* pFilename, const char* pMode) { 2619 - FILE* pFile = NULL; 2620 - fopen_s(&pFile, pFilename, pMode); 2621 - return pFile; 2622 - } 2623 - static FILE* mz_freopen(const char* pPath, const char* pMode, FILE* pStream) { 2624 - FILE* pFile = NULL; 2625 - if (freopen_s(&pFile, pPath, pMode, pStream)) return NULL; 2626 - return pFile; 2627 - } 2628 - #ifndef MINIZ_NO_TIME 2629 - #include <sys/utime.h> 2630 - #endif 2631 - #define MZ_FOPEN mz_fopen 2632 - #define MZ_FCLOSE fclose 2633 - #define MZ_FREAD fread 2634 - #define MZ_FWRITE fwrite 2635 - #define MZ_FTELL64 _ftelli64 2636 - #define MZ_FSEEK64 _fseeki64 2637 - #define MZ_FILE_STAT_STRUCT _stat64 2638 - #define MZ_FILE_STAT _stat64 2639 - #define MZ_FFLUSH fflush 2640 - #define MZ_FREOPEN mz_freopen 2641 - #define MZ_DELETE_FILE remove 2642 - #elif defined(__MINGW32__) 2643 - #ifndef MINIZ_NO_TIME 2644 - #include <sys/utime.h> 2645 - #endif 2646 - #define MZ_FOPEN(f, m) fopen(f, m) 2647 - #define MZ_FCLOSE fclose 2648 - #define MZ_FREAD fread 2649 - #define MZ_FWRITE fwrite 2650 - #define MZ_FTELL64 ftello64 2651 - #define MZ_FSEEK64 fseeko64 2652 - #define MZ_FILE_STAT_STRUCT _stat 2653 - #define MZ_FILE_STAT _stat 2654 - #define MZ_FFLUSH fflush 2655 - #define MZ_FREOPEN(f, m, s) freopen(f, m, s) 2656 - #define MZ_DELETE_FILE remove 2657 - #elif defined(__TINYC__) 2658 - #ifndef MINIZ_NO_TIME 2659 - #include <sys/utime.h> 2660 - #endif 2661 - #define MZ_FOPEN(f, m) fopen(f, m) 2662 - #define MZ_FCLOSE fclose 2663 - #define MZ_FREAD fread 2664 - #define MZ_FWRITE fwrite 2665 - #define MZ_FTELL64 ftell 2666 - #define MZ_FSEEK64 fseek 2667 - #define MZ_FILE_STAT_STRUCT stat 2668 - #define MZ_FILE_STAT stat 2669 - #define MZ_FFLUSH fflush 2670 - #define MZ_FREOPEN(f, m, s) freopen(f, m, s) 2671 - #define MZ_DELETE_FILE remove 2672 - #elif defined(__USE_LARGEFILE64) /* gcc, clang */ 2673 - #ifndef MINIZ_NO_TIME 2674 - #include <utime.h> 2675 - #endif 2676 - #define MZ_FOPEN(f, m) fopen64(f, m) 2677 - #define MZ_FCLOSE fclose 2678 - #define MZ_FREAD fread 2679 - #define MZ_FWRITE fwrite 2680 - #define MZ_FTELL64 ftello64 2681 - #define MZ_FSEEK64 fseeko64 2682 - #define MZ_FILE_STAT_STRUCT stat64 2683 - #define MZ_FILE_STAT stat64 2684 - #define MZ_FFLUSH fflush 2685 - #define MZ_FREOPEN(p, m, s) freopen64(p, m, s) 2686 - #define MZ_DELETE_FILE remove 2687 - #elif defined(__APPLE__) 2688 - #ifndef MINIZ_NO_TIME 2689 - #include <utime.h> 2690 - #endif 2691 - #define MZ_FOPEN(f, m) fopen(f, m) 2692 - #define MZ_FCLOSE fclose 2693 - #define MZ_FREAD fread 2694 - #define MZ_FWRITE fwrite 2695 - #define MZ_FTELL64 ftello 2696 - #define MZ_FSEEK64 fseeko 2697 - #define MZ_FILE_STAT_STRUCT stat 2698 - #define MZ_FILE_STAT stat 2699 - #define MZ_FFLUSH fflush 2700 - #define MZ_FREOPEN(p, m, s) freopen(p, m, s) 2701 - #define MZ_DELETE_FILE remove 2702 - 2703 - #else 2704 - #pragma message("Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.") 2705 - #ifndef MINIZ_NO_TIME 2706 - #include <utime.h> 2707 - #endif 2708 - #define MZ_FOPEN(f, m) fopen(f, m) 2709 - #define MZ_FCLOSE fclose 2710 - #define MZ_FREAD fread 2711 - #define MZ_FWRITE fwrite 2712 - #ifdef __STRICT_ANSI__ 2713 - #define MZ_FTELL64 ftell 2714 - #define MZ_FSEEK64 fseek 2715 - #else 2716 - #define MZ_FTELL64 ftello 2717 - #define MZ_FSEEK64 fseeko 2718 - #endif 2719 - #define MZ_FILE_STAT_STRUCT stat 2720 - #define MZ_FILE_STAT stat 2721 - #define MZ_FFLUSH fflush 2722 - #define MZ_FREOPEN(f, m, s) freopen(f, m, s) 2723 - #define MZ_DELETE_FILE remove 2724 - #endif /* #ifdef _MSC_VER */ 2725 - #endif /* #ifdef MINIZ_NO_STDIO */ 2726 - 2727 - #define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c)) 2728 - 2729 - /* Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c 2730 - * doesn't use structs for any of this stuff. */ 2731 - enum { 2732 - /* ZIP archive identifiers and record sizes */ 2733 - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50, 2734 - MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50, 2735 - MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50, 2736 - MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30, 2737 - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46, 2738 - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22, 2739 - 2740 - /* ZIP64 archive identifier and record sizes */ 2741 - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06064b50, 2742 - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50, 2743 - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE = 56, 2744 - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE = 20, 2745 - MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID = 0x0001, 2746 - MZ_ZIP_DATA_DESCRIPTOR_ID = 0x08074b50, 2747 - MZ_ZIP_DATA_DESCRIPTER_SIZE64 = 24, 2748 - MZ_ZIP_DATA_DESCRIPTER_SIZE32 = 16, 2749 - 2750 - /* Central directory header record offsets */ 2751 - MZ_ZIP_CDH_SIG_OFS = 0, 2752 - MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4, 2753 - MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6, 2754 - MZ_ZIP_CDH_BIT_FLAG_OFS = 8, 2755 - MZ_ZIP_CDH_METHOD_OFS = 10, 2756 - MZ_ZIP_CDH_FILE_TIME_OFS = 12, 2757 - MZ_ZIP_CDH_FILE_DATE_OFS = 14, 2758 - MZ_ZIP_CDH_CRC32_OFS = 16, 2759 - MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20, 2760 - MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24, 2761 - MZ_ZIP_CDH_FILENAME_LEN_OFS = 28, 2762 - MZ_ZIP_CDH_EXTRA_LEN_OFS = 30, 2763 - MZ_ZIP_CDH_COMMENT_LEN_OFS = 32, 2764 - MZ_ZIP_CDH_DISK_START_OFS = 34, 2765 - MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36, 2766 - MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38, 2767 - MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42, 2768 - 2769 - /* Local directory header offsets */ 2770 - MZ_ZIP_LDH_SIG_OFS = 0, 2771 - MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4, 2772 - MZ_ZIP_LDH_BIT_FLAG_OFS = 6, 2773 - MZ_ZIP_LDH_METHOD_OFS = 8, 2774 - MZ_ZIP_LDH_FILE_TIME_OFS = 10, 2775 - MZ_ZIP_LDH_FILE_DATE_OFS = 12, 2776 - MZ_ZIP_LDH_CRC32_OFS = 14, 2777 - MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18, 2778 - MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22, 2779 - MZ_ZIP_LDH_FILENAME_LEN_OFS = 26, 2780 - MZ_ZIP_LDH_EXTRA_LEN_OFS = 28, 2781 - MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR = 1 << 3, 2782 - 2783 - /* End of central directory offsets */ 2784 - MZ_ZIP_ECDH_SIG_OFS = 0, 2785 - MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4, 2786 - MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6, 2787 - MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8, 2788 - MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10, 2789 - MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12, 2790 - MZ_ZIP_ECDH_CDIR_OFS_OFS = 16, 2791 - MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20, 2792 - 2793 - /* ZIP64 End of central directory locator offsets */ 2794 - MZ_ZIP64_ECDL_SIG_OFS = 0, /* 4 bytes */ 2795 - MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS = 4, /* 4 bytes */ 2796 - MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS = 8, /* 8 bytes */ 2797 - MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS = 16, /* 4 bytes */ 2798 - 2799 - /* ZIP64 End of central directory header offsets */ 2800 - MZ_ZIP64_ECDH_SIG_OFS = 0, /* 4 bytes */ 2801 - MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS = 4, /* 8 bytes */ 2802 - MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS = 12, /* 2 bytes */ 2803 - MZ_ZIP64_ECDH_VERSION_NEEDED_OFS = 14, /* 2 bytes */ 2804 - MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS = 16, /* 4 bytes */ 2805 - MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS = 20, /* 4 bytes */ 2806 - MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 24, /* 8 bytes */ 2807 - MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS = 32, /* 8 bytes */ 2808 - MZ_ZIP64_ECDH_CDIR_SIZE_OFS = 40, /* 8 bytes */ 2809 - MZ_ZIP64_ECDH_CDIR_OFS_OFS = 48, /* 8 bytes */ 2810 - MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID = 0, 2811 - MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG = 0x10, 2812 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED = 1, 2813 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG = 32, 2814 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION = 64, 2815 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED = 8192, 2816 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 = 1 << 11 2817 - }; 2818 - 2819 - typedef struct { 2820 - void* m_p; 2821 - size_t m_size, m_capacity; 2822 - mz_uint m_element_size; 2823 - } mz_zip_array; 2824 - 2825 - struct mz_zip_internal_state_tag { 2826 - mz_zip_array m_central_dir; 2827 - mz_zip_array m_central_dir_offsets; 2828 - mz_zip_array m_sorted_central_dir_offsets; 2829 - 2830 - /* The flags passed in when the archive is initially opened. */ 2831 - uint32_t m_init_flags; 2832 - 2833 - /* MZ_TRUE if the archive has a zip64 end of central directory headers, etc. */ 2834 - mz_bool m_zip64; 2835 - 2836 - /* MZ_TRUE if we found zip64 extended info in the central directory (m_zip64 will also be slammed to true too, even if 2837 - * we didn't find a zip64 end of central dir header, etc.) */ 2838 - mz_bool m_zip64_has_extended_info_fields; 2839 - 2840 - /* These fields are used by the file, FILE, memory, and memory/heap read/write helpers. */ 2841 - MZ_FILE* m_pFile; 2842 - mz_uint64 m_file_archive_start_ofs; 2843 - 2844 - void* m_pMem; 2845 - size_t m_mem_size; 2846 - size_t m_mem_capacity; 2847 - }; 2848 - 2849 - #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size 2850 - 2851 - #if defined(DEBUG) || defined(_DEBUG) 2852 - static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array* pArray, mz_uint index) { 2853 - MZ_ASSERT(index < pArray->m_size); 2854 - return index; 2855 - } 2856 - #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) \ 2857 - ((element_type*)((array_ptr)->m_p))[mz_zip_array_range_check(array_ptr, index)] 2858 - #else 2859 - #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type*)((array_ptr)->m_p))[index] 2860 - #endif 2861 - 2862 - static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array* pArray, mz_uint32 element_size) { 2863 - memset(pArray, 0, sizeof(mz_zip_array)); 2864 - pArray->m_element_size = element_size; 2865 - } 2866 - 2867 - static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive* pZip, mz_zip_array* pArray) { 2868 - pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p); 2869 - memset(pArray, 0, sizeof(mz_zip_array)); 2870 - } 2871 - 2872 - static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive* pZip, mz_zip_array* pArray, size_t min_new_capacity, 2873 - mz_uint growing) { 2874 - void* pNew_p; 2875 - size_t new_capacity = min_new_capacity; 2876 - MZ_ASSERT(pArray->m_element_size); 2877 - if (pArray->m_capacity >= min_new_capacity) return MZ_TRUE; 2878 - if (growing) { 2879 - new_capacity = MZ_MAX(1, pArray->m_capacity); 2880 - while (new_capacity < min_new_capacity) new_capacity *= 2; 2881 - } 2882 - if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity))) 2883 - return MZ_FALSE; 2884 - pArray->m_p = pNew_p; 2885 - pArray->m_capacity = new_capacity; 2886 - return MZ_TRUE; 2887 - } 2888 - 2889 - static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive* pZip, mz_zip_array* pArray, size_t new_capacity, 2890 - mz_uint growing) { 2891 - if (new_capacity > pArray->m_capacity) { 2892 - if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing)) return MZ_FALSE; 2893 - } 2894 - return MZ_TRUE; 2895 - } 2896 - 2897 - static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive* pZip, mz_zip_array* pArray, size_t new_size, 2898 - mz_uint growing) { 2899 - if (new_size > pArray->m_capacity) { 2900 - if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) return MZ_FALSE; 2901 - } 2902 - pArray->m_size = new_size; 2903 - return MZ_TRUE; 2904 - } 2905 - 2906 - static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive* pZip, mz_zip_array* pArray, size_t n) { 2907 - return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE); 2908 - } 2909 - 2910 - static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive* pZip, mz_zip_array* pArray, const void* pElements, 2911 - size_t n) { 2912 - size_t orig_size = pArray->m_size; 2913 - if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) return MZ_FALSE; 2914 - if (n > 0) memcpy((mz_uint8*)pArray->m_p + orig_size * pArray->m_element_size, pElements, n * pArray->m_element_size); 2915 - return MZ_TRUE; 2916 - } 2917 - 2918 - #ifndef MINIZ_NO_TIME 2919 - static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date) { 2920 - struct tm tm; 2921 - memset(&tm, 0, sizeof(tm)); 2922 - tm.tm_isdst = -1; 2923 - tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900; 2924 - tm.tm_mon = ((dos_date >> 5) & 15) - 1; 2925 - tm.tm_mday = dos_date & 31; 2926 - tm.tm_hour = (dos_time >> 11) & 31; 2927 - tm.tm_min = (dos_time >> 5) & 63; 2928 - tm.tm_sec = (dos_time << 1) & 62; 2929 - return mktime(&tm); 2930 - } 2931 - 2932 - #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 2933 - static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16* pDOS_time, mz_uint16* pDOS_date) { 2934 - #ifdef _MSC_VER 2935 - struct tm tm_struct; 2936 - struct tm* tm = &tm_struct; 2937 - errno_t err = localtime_s(tm, &time); 2938 - if (err) { 2939 - *pDOS_date = 0; 2940 - *pDOS_time = 0; 2941 - return; 2942 - } 2943 - #else 2944 - struct tm* tm = localtime(&time); 2945 - #endif /* #ifdef _MSC_VER */ 2946 - 2947 - *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1)); 2948 - *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday); 2949 - } 2950 - #endif /* MINIZ_NO_ARCHIVE_WRITING_APIS */ 2951 - 2952 - #ifndef MINIZ_NO_STDIO 2953 - #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 2954 - static mz_bool mz_zip_get_file_modified_time(const char* pFilename, MZ_TIME_T* pTime) { 2955 - struct MZ_FILE_STAT_STRUCT file_stat; 2956 - 2957 - /* On Linux with x86 glibc, this call will fail on large files (I think >= 0x80000000 bytes) unless you compiled with 2958 - * _LARGEFILE64_SOURCE. Argh. */ 2959 - if (MZ_FILE_STAT(pFilename, &file_stat) != 0) return MZ_FALSE; 2960 - 2961 - *pTime = file_stat.st_mtime; 2962 - 2963 - return MZ_TRUE; 2964 - } 2965 - #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS*/ 2966 - 2967 - static mz_bool mz_zip_set_file_times(const char* pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time) { 2968 - struct utimbuf t; 2969 - 2970 - memset(&t, 0, sizeof(t)); 2971 - t.actime = access_time; 2972 - t.modtime = modified_time; 2973 - 2974 - return !utime(pFilename, &t); 2975 - } 2976 - #endif /* #ifndef MINIZ_NO_STDIO */ 2977 - #endif /* #ifndef MINIZ_NO_TIME */ 2978 - 2979 - static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive* pZip, mz_zip_error err_num) { 2980 - if (pZip) pZip->m_last_error = err_num; 2981 - return MZ_FALSE; 2982 - } 2983 - 2984 - static mz_bool mz_zip_reader_init_internal(mz_zip_archive* pZip, mz_uint flags) { 2985 - (void)flags; 2986 - if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 2987 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 2988 - 2989 - if (!pZip->m_pAlloc) pZip->m_pAlloc = miniz_def_alloc_func; 2990 - if (!pZip->m_pFree) pZip->m_pFree = miniz_def_free_func; 2991 - if (!pZip->m_pRealloc) pZip->m_pRealloc = miniz_def_realloc_func; 2992 - 2993 - pZip->m_archive_size = 0; 2994 - pZip->m_central_directory_file_ofs = 0; 2995 - pZip->m_total_files = 0; 2996 - pZip->m_last_error = MZ_ZIP_NO_ERROR; 2997 - 2998 - if (NULL == (pZip->m_pState = 2999 - (mz_zip_internal_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) 3000 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 3001 - 3002 - memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); 3003 - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8)); 3004 - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32)); 3005 - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32)); 3006 - pZip->m_pState->m_init_flags = flags; 3007 - pZip->m_pState->m_zip64 = MZ_FALSE; 3008 - pZip->m_pState->m_zip64_has_extended_info_fields = MZ_FALSE; 3009 - 3010 - pZip->m_zip_mode = MZ_ZIP_MODE_READING; 3011 - 3012 - return MZ_TRUE; 3013 - } 3014 - 3015 - static MZ_FORCEINLINE mz_bool mz_zip_reader_filename_less(const mz_zip_array* pCentral_dir_array, 3016 - const mz_zip_array* pCentral_dir_offsets, mz_uint l_index, 3017 - mz_uint r_index) { 3018 - const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, 3019 - MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), 3020 - *pE; 3021 - const mz_uint8* pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, 3022 - MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index)); 3023 - mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), 3024 - r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS); 3025 - mz_uint8 l = 0, r = 0; 3026 - pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; 3027 - pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; 3028 - pE = pL + MZ_MIN(l_len, r_len); 3029 - while (pL < pE) { 3030 - if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) break; 3031 - pL++; 3032 - pR++; 3033 - } 3034 - return (pL == pE) ? (l_len < r_len) : (l < r); 3035 - } 3036 - 3037 - #define MZ_SWAP_UINT32(a, b) \ 3038 - do { \ 3039 - mz_uint32 t = a; \ 3040 - a = b; \ 3041 - b = t; \ 3042 - } \ 3043 - MZ_MACRO_END 3044 - 3045 - /* Heap sort of lowercased filenames, used to help accelerate plain central directory searches by 3046 - * mz_zip_reader_locate_file(). (Could also use qsort(), but it could allocate memory.) */ 3047 - static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive* pZip) { 3048 - mz_zip_internal_state* pState = pZip->m_pState; 3049 - const mz_zip_array* pCentral_dir_offsets = &pState->m_central_dir_offsets; 3050 - const mz_zip_array* pCentral_dir = &pState->m_central_dir; 3051 - mz_uint32* pIndices; 3052 - mz_uint32 start, end; 3053 - const mz_uint32 size = pZip->m_total_files; 3054 - 3055 - if (size <= 1U) return; 3056 - 3057 - pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0); 3058 - 3059 - start = (size - 2U) >> 1U; 3060 - for (;;) { 3061 - mz_uint64 child, root = start; 3062 - for (;;) { 3063 - if ((child = (root << 1U) + 1U) >= size) break; 3064 - child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, 3065 - pIndices[child], pIndices[child + 1U]))); 3066 - if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) break; 3067 - MZ_SWAP_UINT32(pIndices[root], pIndices[child]); 3068 - root = child; 3069 - } 3070 - if (!start) break; 3071 - start--; 3072 - } 3073 - 3074 - end = size - 1; 3075 - while (end > 0) { 3076 - mz_uint64 child, root = 0; 3077 - MZ_SWAP_UINT32(pIndices[end], pIndices[0]); 3078 - for (;;) { 3079 - if ((child = (root << 1U) + 1U) >= end) break; 3080 - child += (((child + 1U) < end) && 3081 - mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])); 3082 - if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) break; 3083 - MZ_SWAP_UINT32(pIndices[root], pIndices[child]); 3084 - root = child; 3085 - } 3086 - end--; 3087 - } 3088 - } 3089 - 3090 - static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive* pZip, mz_uint32 record_sig, mz_uint32 record_size, 3091 - mz_int64* pOfs) { 3092 - mz_int64 cur_file_ofs; 3093 - mz_uint32 buf_u32[4096 / sizeof(mz_uint32)]; 3094 - mz_uint8* pBuf = (mz_uint8*)buf_u32; 3095 - 3096 - /* Basic sanity checks - reject files which are too small */ 3097 - if (pZip->m_archive_size < record_size) return MZ_FALSE; 3098 - 3099 - /* Find the record by scanning the file from the end towards the beginning. */ 3100 - cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0); 3101 - for (;;) { 3102 - int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs); 3103 - 3104 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n) return MZ_FALSE; 3105 - 3106 - for (i = n - 4; i >= 0; --i) { 3107 - mz_uint s = MZ_READ_LE32(pBuf + i); 3108 - if (s == record_sig) { 3109 - if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size) break; 3110 - } 3111 - } 3112 - 3113 - if (i >= 0) { 3114 - cur_file_ofs += i; 3115 - break; 3116 - } 3117 - 3118 - /* Give up if we've searched the entire file, or we've gone back "too far" (~64kb) */ 3119 - if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= (MZ_UINT16_MAX + record_size))) return MZ_FALSE; 3120 - 3121 - cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0); 3122 - } 3123 - 3124 - *pOfs = cur_file_ofs; 3125 - return MZ_TRUE; 3126 - } 3127 - 3128 - static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive* pZip, mz_uint flags) { 3129 - mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0; 3130 - mz_uint64 cdir_ofs = 0; 3131 - mz_int64 cur_file_ofs = 0; 3132 - const mz_uint8* p; 3133 - 3134 - mz_uint32 buf_u32[4096 / sizeof(mz_uint32)]; 3135 - mz_uint8* pBuf = (mz_uint8*)buf_u32; 3136 - mz_bool sort_central_dir = ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0); 3137 - mz_uint32 zip64_end_of_central_dir_locator_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + sizeof(mz_uint32) - 1) / 3138 - sizeof(mz_uint32)]; 3139 - mz_uint8* pZip64_locator = (mz_uint8*)zip64_end_of_central_dir_locator_u32; 3140 - 3141 - mz_uint32 zip64_end_of_central_dir_header_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / 3142 - sizeof(mz_uint32)]; 3143 - mz_uint8* pZip64_end_of_central_dir = (mz_uint8*)zip64_end_of_central_dir_header_u32; 3144 - 3145 - mz_uint64 zip64_end_of_central_dir_ofs = 0; 3146 - 3147 - /* Basic sanity checks - reject files which are too small, and check the first 4 bytes of the file to make sure a 3148 - * local header is there. */ 3149 - if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) 3150 - return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3151 - 3152 - if (!mz_zip_reader_locate_header_sig(pZip, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG, 3153 - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs)) 3154 - return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR); 3155 - 3156 - /* Read and verify the end of central directory record. */ 3157 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != 3158 - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) 3159 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 3160 - 3161 - if (MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) 3162 - return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3163 - 3164 - if (cur_file_ofs >= (MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) { 3165 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, pZip64_locator, 3166 - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) { 3167 - if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG) { 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 - } 3179 - } 3180 - } 3181 - } 3182 - 3183 - pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS); 3184 - cdir_entries_on_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS); 3185 - num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS); 3186 - cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS); 3187 - cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS); 3188 - cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS); 3189 - 3190 - if (pZip->m_pState->m_zip64) { 3191 - mz_uint32 zip64_total_num_of_disks = MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS); 3192 - mz_uint64 zip64_cdir_total_entries = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS); 3193 - mz_uint64 zip64_cdir_total_entries_on_this_disk = 3194 - MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS); 3195 - mz_uint64 zip64_size_of_end_of_central_dir_record = 3196 - MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS); 3197 - mz_uint64 zip64_size_of_central_directory = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_SIZE_OFS); 3198 - 3199 - if (zip64_size_of_end_of_central_dir_record < (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12)) 3200 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3201 - 3202 - if (zip64_total_num_of_disks != 1U) return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); 3203 - 3204 - /* Check for miniz's practical limits */ 3205 - if (zip64_cdir_total_entries > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 3206 - 3207 - pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries; 3208 - 3209 - if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 3210 - 3211 - cdir_entries_on_this_disk = (mz_uint32)zip64_cdir_total_entries_on_this_disk; 3212 - 3213 - /* Check for miniz's current practical limits (sorry, this should be enough for millions of files) */ 3214 - if (zip64_size_of_central_directory > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); 3215 - 3216 - cdir_size = (mz_uint32)zip64_size_of_central_directory; 3217 - 3218 - num_this_disk = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS); 3219 - 3220 - cdir_disk_index = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS); 3221 - 3222 - cdir_ofs = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_OFS_OFS); 3223 - } 3224 - 3225 - if (pZip->m_total_files != cdir_entries_on_this_disk) return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); 3226 - 3227 - if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1))) 3228 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); 3229 - 3230 - if (cdir_size < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) 3231 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3232 - 3233 - if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size) 3234 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3235 - 3236 - pZip->m_central_directory_file_ofs = cdir_ofs; 3237 - 3238 - if (pZip->m_total_files) { 3239 - mz_uint i, n; 3240 - /* Read the entire central directory into a heap block, and allocate another heap block to hold the unsorted central 3241 - * dir file record offsets, and possibly another to hold the sorted indices. */ 3242 - if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) || 3243 - (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE))) 3244 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 3245 - 3246 - if (sort_central_dir) { 3247 - if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE)) 3248 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 3249 - } 3250 - 3251 - if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size) 3252 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 3253 - 3254 - /* Now create an index into the central directory file records, do some basic sanity checking on each record */ 3255 - p = (const mz_uint8*)pZip->m_pState->m_central_dir.m_p; 3256 - for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i) { 3257 - mz_uint total_header_size, disk_index, bit_flags, filename_size, ext_data_size; 3258 - mz_uint64 comp_size, decomp_size, local_header_ofs; 3259 - 3260 - if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)) 3261 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3262 - 3263 - MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, i) = 3264 - (mz_uint32)(p - (const mz_uint8*)pZip->m_pState->m_central_dir.m_p); 3265 - 3266 - if (sort_central_dir) MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i; 3267 - 3268 - comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); 3269 - decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); 3270 - local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS); 3271 - filename_size = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); 3272 - ext_data_size = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS); 3273 - 3274 - if ((!pZip->m_pState->m_zip64_has_extended_info_fields) && (ext_data_size) && 3275 - (MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) == MZ_UINT32_MAX)) { 3276 - /* Attempt to find zip64 extended information field in the entry's extra data */ 3277 - mz_uint32 extra_size_remaining = ext_data_size; 3278 - 3279 - if (extra_size_remaining) { 3280 - const mz_uint8* pExtra_data; 3281 - void* buf = NULL; 3282 - 3283 - if (MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + ext_data_size > n) { 3284 - buf = MZ_MALLOC(ext_data_size); 3285 - if (buf == NULL) return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 3286 - 3287 - if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size, buf, 3288 - ext_data_size) != ext_data_size) { 3289 - MZ_FREE(buf); 3290 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 3291 - } 3292 - 3293 - pExtra_data = (mz_uint8*)buf; 3294 - } else { 3295 - pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size; 3296 - } 3297 - 3298 - do { 3299 - mz_uint32 field_id; 3300 - mz_uint32 field_data_size; 3301 - 3302 - if (extra_size_remaining < (sizeof(mz_uint16) * 2)) { 3303 - MZ_FREE(buf); 3304 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3305 - } 3306 - 3307 - field_id = MZ_READ_LE16(pExtra_data); 3308 - field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); 3309 - 3310 - if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining) { 3311 - MZ_FREE(buf); 3312 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3313 - } 3314 - 3315 - if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) { 3316 - /* Ok, the archive didn't have any zip64 headers but it uses a zip64 extended information field so mark it 3317 - * as zip64 anyway (this can occur with infozip's zip util when it reads compresses files from stdin). */ 3318 - pZip->m_pState->m_zip64 = MZ_TRUE; 3319 - pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE; 3320 - break; 3321 - } 3322 - 3323 - pExtra_data += sizeof(mz_uint16) * 2 + field_data_size; 3324 - extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size; 3325 - } while (extra_size_remaining); 3326 - 3327 - MZ_FREE(buf); 3328 - } 3329 - } 3330 - 3331 - /* I've seen archives that aren't marked as zip64 that uses zip64 ext data, argh */ 3332 - if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX)) { 3333 - if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size)) 3334 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3335 - } 3336 - 3337 - disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS); 3338 - if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1))) 3339 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK); 3340 - 3341 - if (comp_size != MZ_UINT32_MAX) { 3342 - if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > 3343 - pZip->m_archive_size) 3344 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3345 - } 3346 - 3347 - bit_flags = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); 3348 - if (bit_flags & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED) 3349 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); 3350 - 3351 - if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + 3352 - MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + 3353 - MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n) 3354 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3355 - 3356 - n -= total_header_size; 3357 - p += total_header_size; 3358 - } 3359 - } 3360 - 3361 - if (sort_central_dir) mz_zip_reader_sort_central_dir_offsets_by_filename(pZip); 3362 - 3363 - return MZ_TRUE; 3364 - } 3365 - 3366 - void mz_zip_zero_struct(mz_zip_archive* pZip) { 3367 - if (pZip) MZ_CLEAR_OBJ(*pZip); 3368 - } 3369 - 3370 - static mz_bool mz_zip_reader_end_internal(mz_zip_archive* pZip, mz_bool set_last_error) { 3371 - mz_bool status = MZ_TRUE; 3372 - 3373 - if (!pZip) return MZ_FALSE; 3374 - 3375 - if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) { 3376 - if (set_last_error) pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER; 3377 - 3378 - return MZ_FALSE; 3379 - } 3380 - 3381 - if (pZip->m_pState) { 3382 - mz_zip_internal_state* pState = pZip->m_pState; 3383 - pZip->m_pState = NULL; 3384 - 3385 - mz_zip_array_clear(pZip, &pState->m_central_dir); 3386 - mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); 3387 - mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); 3388 - 3389 - #ifndef MINIZ_NO_STDIO 3390 - if (pState->m_pFile) { 3391 - if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) { 3392 - if (MZ_FCLOSE(pState->m_pFile) == EOF) { 3393 - if (set_last_error) pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED; 3394 - status = MZ_FALSE; 3395 - } 3396 - } 3397 - pState->m_pFile = NULL; 3398 - } 3399 - #endif /* #ifndef MINIZ_NO_STDIO */ 3400 - 3401 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 3402 - } 3403 - pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; 3404 - 3405 - return status; 3406 - } 3407 - 3408 - mz_bool mz_zip_reader_end(mz_zip_archive* pZip) { return mz_zip_reader_end_internal(pZip, MZ_TRUE); } 3409 - mz_bool mz_zip_reader_init(mz_zip_archive* pZip, mz_uint64 size, mz_uint flags) { 3410 - if ((!pZip) || (!pZip->m_pRead)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3411 - 3412 - if (!mz_zip_reader_init_internal(pZip, flags)) return MZ_FALSE; 3413 - 3414 - pZip->m_zip_type = MZ_ZIP_TYPE_USER; 3415 - pZip->m_archive_size = size; 3416 - 3417 - if (!mz_zip_reader_read_central_dir(pZip, flags)) { 3418 - mz_zip_reader_end_internal(pZip, MZ_FALSE); 3419 - return MZ_FALSE; 3420 - } 3421 - 3422 - return MZ_TRUE; 3423 - } 3424 - 3425 - static size_t mz_zip_mem_read_func(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n) { 3426 - mz_zip_archive* pZip = (mz_zip_archive*)pOpaque; 3427 - size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n); 3428 - memcpy(pBuf, (const mz_uint8*)pZip->m_pState->m_pMem + file_ofs, s); 3429 - return s; 3430 - } 3431 - 3432 - mz_bool mz_zip_reader_init_mem(mz_zip_archive* pZip, const void* pMem, size_t size, mz_uint flags) { 3433 - if (!pMem) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3434 - 3435 - if (size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3436 - 3437 - if (!mz_zip_reader_init_internal(pZip, flags)) return MZ_FALSE; 3438 - 3439 - pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY; 3440 - pZip->m_archive_size = size; 3441 - pZip->m_pRead = mz_zip_mem_read_func; 3442 - pZip->m_pIO_opaque = pZip; 3443 - pZip->m_pNeeds_keepalive = NULL; 3444 - 3445 - #ifdef __cplusplus 3446 - pZip->m_pState->m_pMem = const_cast<void*>(pMem); 3447 - #else 3448 - pZip->m_pState->m_pMem = (void*)pMem; 3449 - #endif 3450 - 3451 - pZip->m_pState->m_mem_size = size; 3452 - 3453 - if (!mz_zip_reader_read_central_dir(pZip, flags)) { 3454 - mz_zip_reader_end_internal(pZip, MZ_FALSE); 3455 - return MZ_FALSE; 3456 - } 3457 - 3458 - return MZ_TRUE; 3459 - } 3460 - 3461 - #ifndef MINIZ_NO_STDIO 3462 - static size_t mz_zip_file_read_func(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n) { 3463 - mz_zip_archive* pZip = (mz_zip_archive*)pOpaque; 3464 - mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); 3465 - 3466 - file_ofs += pZip->m_pState->m_file_archive_start_ofs; 3467 - 3468 - if (((mz_int64)file_ofs < 0) || 3469 - (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) 3470 - return 0; 3471 - 3472 - return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile); 3473 - } 3474 - 3475 - mz_bool mz_zip_reader_init_file(mz_zip_archive* pZip, const char* pFilename, mz_uint32 flags) { 3476 - return mz_zip_reader_init_file_v2(pZip, pFilename, flags, 0, 0); 3477 - } 3478 - 3479 - mz_bool mz_zip_reader_init_file_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint flags, mz_uint64 file_start_ofs, 3480 - mz_uint64 archive_size) { 3481 - mz_uint64 file_size; 3482 - MZ_FILE* pFile; 3483 - 3484 - if ((!pZip) || (!pFilename) || ((archive_size) && (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE))) 3485 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3486 - 3487 - pFile = MZ_FOPEN(pFilename, "rb"); 3488 - if (!pFile) return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); 3489 - 3490 - file_size = archive_size; 3491 - if (!file_size) { 3492 - if (MZ_FSEEK64(pFile, 0, SEEK_END)) { 3493 - MZ_FCLOSE(pFile); 3494 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED); 3495 - } 3496 - 3497 - file_size = MZ_FTELL64(pFile); 3498 - } 3499 - 3500 - /* TODO: Better sanity check archive_size and the # of actual remaining bytes */ 3501 - 3502 - if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) { 3503 - MZ_FCLOSE(pFile); 3504 - return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3505 - } 3506 - 3507 - if (!mz_zip_reader_init_internal(pZip, flags)) { 3508 - MZ_FCLOSE(pFile); 3509 - return MZ_FALSE; 3510 - } 3511 - 3512 - pZip->m_zip_type = MZ_ZIP_TYPE_FILE; 3513 - pZip->m_pRead = mz_zip_file_read_func; 3514 - pZip->m_pIO_opaque = pZip; 3515 - pZip->m_pState->m_pFile = pFile; 3516 - pZip->m_archive_size = file_size; 3517 - pZip->m_pState->m_file_archive_start_ofs = file_start_ofs; 3518 - 3519 - if (!mz_zip_reader_read_central_dir(pZip, flags)) { 3520 - mz_zip_reader_end_internal(pZip, MZ_FALSE); 3521 - return MZ_FALSE; 3522 - } 3523 - 3524 - return MZ_TRUE; 3525 - } 3526 - 3527 - mz_bool mz_zip_reader_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint64 archive_size, mz_uint flags) { 3528 - mz_uint64 cur_file_ofs; 3529 - 3530 - if ((!pZip) || (!pFile)) return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); 3531 - 3532 - cur_file_ofs = MZ_FTELL64(pFile); 3533 - 3534 - if (!archive_size) { 3535 - if (MZ_FSEEK64(pFile, 0, SEEK_END)) return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED); 3536 - 3537 - archive_size = MZ_FTELL64(pFile) - cur_file_ofs; 3538 - 3539 - if (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE); 3540 - } 3541 - 3542 - if (!mz_zip_reader_init_internal(pZip, flags)) return MZ_FALSE; 3543 - 3544 - pZip->m_zip_type = MZ_ZIP_TYPE_CFILE; 3545 - pZip->m_pRead = mz_zip_file_read_func; 3546 - 3547 - pZip->m_pIO_opaque = pZip; 3548 - pZip->m_pState->m_pFile = pFile; 3549 - pZip->m_archive_size = archive_size; 3550 - pZip->m_pState->m_file_archive_start_ofs = cur_file_ofs; 3551 - 3552 - if (!mz_zip_reader_read_central_dir(pZip, flags)) { 3553 - mz_zip_reader_end_internal(pZip, MZ_FALSE); 3554 - return MZ_FALSE; 3555 - } 3556 - 3557 - return MZ_TRUE; 3558 - } 3559 - 3560 - #endif /* #ifndef MINIZ_NO_STDIO */ 3561 - 3562 - static MZ_FORCEINLINE const mz_uint8* mz_zip_get_cdh(mz_zip_archive* pZip, mz_uint file_index) { 3563 - if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) return NULL; 3564 - return &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, 3565 - MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index)); 3566 - } 3567 - 3568 - mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive* pZip, mz_uint file_index) { 3569 - mz_uint m_bit_flag; 3570 - const mz_uint8* p = mz_zip_get_cdh(pZip, file_index); 3571 - if (!p) { 3572 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3573 - return MZ_FALSE; 3574 - } 3575 - 3576 - m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); 3577 - return (m_bit_flag & 3578 - (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) != 0; 3579 - } 3580 - 3581 - mz_bool mz_zip_reader_is_file_supported(mz_zip_archive* pZip, mz_uint file_index) { 3582 - mz_uint bit_flag; 3583 - mz_uint method; 3584 - 3585 - const mz_uint8* p = mz_zip_get_cdh(pZip, file_index); 3586 - if (!p) { 3587 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3588 - return MZ_FALSE; 3589 - } 3590 - 3591 - method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS); 3592 - bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); 3593 - 3594 - if ((method != 0) && (method != MZ_DEFLATED)) { 3595 - mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); 3596 - return MZ_FALSE; 3597 - } 3598 - 3599 - if (bit_flag & 3600 - (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) { 3601 - mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); 3602 - return MZ_FALSE; 3603 - } 3604 - 3605 - if (bit_flag & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG) { 3606 - mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); 3607 - return MZ_FALSE; 3608 - } 3609 - 3610 - return MZ_TRUE; 3611 - } 3612 - 3613 - mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive* pZip, mz_uint file_index) { 3614 - mz_uint filename_len, attribute_mapping_id, external_attr; 3615 - const mz_uint8* p = mz_zip_get_cdh(pZip, file_index); 3616 - if (!p) { 3617 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3618 - return MZ_FALSE; 3619 - } 3620 - 3621 - filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); 3622 - if (filename_len) { 3623 - if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/') return MZ_TRUE; 3624 - } 3625 - 3626 - /* Bugfix: This code was also checking if the internal attribute was non-zero, which wasn't correct. */ 3627 - /* Most/all zip writers (hopefully) set DOS file/directory attributes in the low 16-bits, so check for the DOS 3628 - * directory flag and ignore the source OS ID in the created by field. */ 3629 - /* FIXME: Remove this check? Is it necessary - we already check the filename. */ 3630 - attribute_mapping_id = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS) >> 8; 3631 - (void)attribute_mapping_id; 3632 - 3633 - external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); 3634 - if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0) { 3635 - return MZ_TRUE; 3636 - } 3637 - 3638 - return MZ_FALSE; 3639 - } 3640 - 3641 - static mz_bool mz_zip_file_stat_internal(mz_zip_archive* pZip, mz_uint file_index, const mz_uint8* pCentral_dir_header, 3642 - mz_zip_archive_file_stat* pStat, mz_bool* pFound_zip64_extra_data) { 3643 - mz_uint n; 3644 - const mz_uint8* p = pCentral_dir_header; 3645 - 3646 - if (pFound_zip64_extra_data) *pFound_zip64_extra_data = MZ_FALSE; 3647 - 3648 - if ((!p) || (!pStat)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3649 - 3650 - /* Extract fields from the central directory record. */ 3651 - pStat->m_file_index = file_index; 3652 - pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index); 3653 - pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS); 3654 - pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS); 3655 - pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS); 3656 - pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS); 3657 - #ifndef MINIZ_NO_TIME 3658 - pStat->m_time = 3659 - mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS), MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS)); 3660 - #endif 3661 - pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS); 3662 - pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); 3663 - pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); 3664 - pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS); 3665 - pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS); 3666 - pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS); 3667 - 3668 - /* Copy as much of the filename and comment as possible. */ 3669 - n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); 3670 - n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1); 3671 - memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); 3672 - pStat->m_filename[n] = '\0'; 3673 - 3674 - n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS); 3675 - n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1); 3676 - pStat->m_comment_size = n; 3677 - memcpy(pStat->m_comment, 3678 - p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + 3679 - MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS), 3680 - n); 3681 - pStat->m_comment[n] = '\0'; 3682 - 3683 - /* Set some flags for convienance */ 3684 - pStat->m_is_directory = mz_zip_reader_is_file_a_directory(pZip, file_index); 3685 - pStat->m_is_encrypted = mz_zip_reader_is_file_encrypted(pZip, file_index); 3686 - pStat->m_is_supported = mz_zip_reader_is_file_supported(pZip, file_index); 3687 - 3688 - /* See if we need to read any zip64 extended information fields. */ 3689 - /* Confusingly, these zip64 fields can be present even on non-zip64 archives (Debian zip on a huge files from stdin 3690 - * piped to stdout creates them). */ 3691 - if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX) { 3692 - /* Attempt to find zip64 extended information field in the entry's extra data */ 3693 - mz_uint32 extra_size_remaining = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS); 3694 - 3695 - if (extra_size_remaining) { 3696 - const mz_uint8* pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); 3697 - 3698 - do { 3699 - mz_uint32 field_id; 3700 - mz_uint32 field_data_size; 3701 - 3702 - if (extra_size_remaining < (sizeof(mz_uint16) * 2)) 3703 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3704 - 3705 - field_id = MZ_READ_LE16(pExtra_data); 3706 - field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); 3707 - 3708 - if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining) 3709 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3710 - 3711 - if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) { 3712 - const mz_uint8* pField_data = pExtra_data + sizeof(mz_uint16) * 2; 3713 - mz_uint32 field_data_remaining = field_data_size; 3714 - 3715 - if (pFound_zip64_extra_data) *pFound_zip64_extra_data = MZ_TRUE; 3716 - 3717 - if (pStat->m_uncomp_size == MZ_UINT32_MAX) { 3718 - if (field_data_remaining < sizeof(mz_uint64)) 3719 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3720 - 3721 - pStat->m_uncomp_size = MZ_READ_LE64(pField_data); 3722 - pField_data += sizeof(mz_uint64); 3723 - field_data_remaining -= sizeof(mz_uint64); 3724 - } 3725 - 3726 - if (pStat->m_comp_size == MZ_UINT32_MAX) { 3727 - if (field_data_remaining < sizeof(mz_uint64)) 3728 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3729 - 3730 - pStat->m_comp_size = MZ_READ_LE64(pField_data); 3731 - pField_data += sizeof(mz_uint64); 3732 - field_data_remaining -= sizeof(mz_uint64); 3733 - } 3734 - 3735 - if (pStat->m_local_header_ofs == MZ_UINT32_MAX) { 3736 - if (field_data_remaining < sizeof(mz_uint64)) 3737 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3738 - 3739 - pStat->m_local_header_ofs = MZ_READ_LE64(pField_data); 3740 - pField_data += sizeof(mz_uint64); 3741 - field_data_remaining -= sizeof(mz_uint64); 3742 - } 3743 - 3744 - break; 3745 - } 3746 - 3747 - pExtra_data += sizeof(mz_uint16) * 2 + field_data_size; 3748 - extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size; 3749 - } while (extra_size_remaining); 3750 - } 3751 - } 3752 - 3753 - return MZ_TRUE; 3754 - } 3755 - 3756 - static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char* pA, const char* pB, mz_uint len, mz_uint flags) { 3757 - mz_uint i; 3758 - if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE) return 0 == memcmp(pA, pB, len); 3759 - for (i = 0; i < len; ++i) 3760 - if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) return MZ_FALSE; 3761 - return MZ_TRUE; 3762 - } 3763 - 3764 - static MZ_FORCEINLINE int mz_zip_filename_compare(const mz_zip_array* pCentral_dir_array, 3765 - const mz_zip_array* pCentral_dir_offsets, mz_uint l_index, 3766 - const char* pR, mz_uint r_len) { 3767 - const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, 3768 - MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), 3769 - *pE; 3770 - mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS); 3771 - mz_uint8 l = 0, r = 0; 3772 - pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; 3773 - pE = pL + MZ_MIN(l_len, r_len); 3774 - while (pL < pE) { 3775 - if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) break; 3776 - pL++; 3777 - pR++; 3778 - } 3779 - return (pL == pE) ? (int)(l_len - r_len) : (l - r); 3780 - } 3781 - 3782 - static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive* pZip, const char* pFilename, mz_uint32* pIndex) { 3783 - mz_zip_internal_state* pState = pZip->m_pState; 3784 - const mz_zip_array* pCentral_dir_offsets = &pState->m_central_dir_offsets; 3785 - const mz_zip_array* pCentral_dir = &pState->m_central_dir; 3786 - mz_uint32* pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0); 3787 - const uint32_t size = pZip->m_total_files; 3788 - const mz_uint filename_len = (mz_uint)strlen(pFilename); 3789 - 3790 - if (pIndex) *pIndex = 0; 3791 - 3792 - if (size) { 3793 - /* yes I could use uint32_t's, but then we would have to add some special case checks in the loop, argh, and */ 3794 - /* honestly the major expense here on 32-bit CPU's will still be the filename compare */ 3795 - mz_int64 l = 0, h = (mz_int64)size - 1; 3796 - 3797 - while (l <= h) { 3798 - mz_int64 m = l + ((h - l) >> 1); 3799 - uint32_t file_index = pIndices[(uint32_t)m]; 3800 - 3801 - int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len); 3802 - if (!comp) { 3803 - if (pIndex) *pIndex = file_index; 3804 - return MZ_TRUE; 3805 - } else if (comp < 0) 3806 - l = m + 1; 3807 - else 3808 - h = m - 1; 3809 - } 3810 - } 3811 - 3812 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND); 3813 - } 3814 - 3815 - int mz_zip_reader_locate_file(mz_zip_archive* pZip, const char* pName, const char* pComment, mz_uint flags) { 3816 - mz_uint32 index; 3817 - if (!mz_zip_reader_locate_file_v2(pZip, pName, pComment, flags, &index)) 3818 - return -1; 3819 - else 3820 - return (int)index; 3821 - } 3822 - 3823 - mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive* pZip, const char* pName, const char* pComment, mz_uint flags, 3824 - mz_uint32* pIndex) { 3825 - mz_uint file_index; 3826 - size_t name_len, comment_len; 3827 - 3828 - if (pIndex) *pIndex = 0; 3829 - 3830 - if ((!pZip) || (!pZip->m_pState) || (!pName)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3831 - 3832 - /* See if we can use a binary search */ 3833 - if (((pZip->m_pState->m_init_flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) && 3834 - (pZip->m_zip_mode == MZ_ZIP_MODE_READING) && 3835 - ((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && 3836 - (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) { 3837 - return mz_zip_locate_file_binary_search(pZip, pName, pIndex); 3838 - } 3839 - 3840 - /* Locate the entry by scanning the entire central directory */ 3841 - name_len = strlen(pName); 3842 - if (name_len > MZ_UINT16_MAX) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3843 - 3844 - comment_len = pComment ? strlen(pComment) : 0; 3845 - if (comment_len > MZ_UINT16_MAX) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3846 - 3847 - for (file_index = 0; file_index < pZip->m_total_files; file_index++) { 3848 - const mz_uint8* pHeader = 3849 - &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, 3850 - MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index)); 3851 - mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS); 3852 - const char* pFilename = (const char*)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; 3853 - if (filename_len < name_len) continue; 3854 - if (comment_len) { 3855 - mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS), 3856 - file_comment_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS); 3857 - const char* pFile_comment = pFilename + filename_len + file_extra_len; 3858 - if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags))) 3859 - continue; 3860 - } 3861 - if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) { 3862 - int ofs = filename_len - 1; 3863 - do { 3864 - if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':')) break; 3865 - } while (--ofs >= 0); 3866 - ofs++; 3867 - pFilename += ofs; 3868 - filename_len -= ofs; 3869 - } 3870 - if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags))) { 3871 - if (pIndex) *pIndex = file_index; 3872 - return MZ_TRUE; 3873 - } 3874 - } 3875 - 3876 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND); 3877 - } 3878 - 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) { 3881 - int status = TINFL_STATUS_DONE; 3882 - mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail; 3883 - mz_zip_archive_file_stat file_stat; 3884 - void* pRead_buf; 3885 - mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; 3886 - mz_uint8* pLocal_header = (mz_uint8*)local_header_u32; 3887 - tinfl_decompressor inflator; 3888 - 3889 - if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || 3890 - (!pZip->m_pRead)) 3891 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 3892 - 3893 - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; 3894 - 3895 - /* A directory or zero length file */ 3896 - if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) return MZ_TRUE; 3897 - 3898 - /* Encryption and patch files are not supported. */ 3899 - if (file_stat.m_bit_flag & 3900 - (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | 3901 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) 3902 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); 3903 - 3904 - /* This function only supports decompressing stored and deflate. */ 3905 - if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 3906 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); 3907 - 3908 - /* Ensure supplied output buffer is large enough. */ 3909 - needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; 3910 - if (buf_size < needed_size) return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL); 3911 - 3912 - /* Read and parse the local directory entry. */ 3913 - cur_file_ofs = file_stat.m_local_header_ofs; 3914 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != 3915 - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 3916 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 3917 - 3918 - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 3919 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3920 - 3921 - cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 3922 - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 3923 - if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) 3924 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 3925 - 3926 - if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) { 3927 - /* The file is stored or the caller has requested the compressed data. */ 3928 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size) 3929 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 3930 - 3931 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 3932 - if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0) { 3933 - if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8*)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) 3934 - return mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED); 3935 - } 3936 - #endif 3937 - 3938 - return MZ_TRUE; 3939 - } 3940 - 3941 - /* Decompress the file either directly from memory or from a file input buffer. */ 3942 - tinfl_init(&inflator); 3943 - 3944 - if (pZip->m_pState->m_pMem) { 3945 - /* Read directly from the archive in memory. */ 3946 - pRead_buf = (mz_uint8*)pZip->m_pState->m_pMem + cur_file_ofs; 3947 - read_buf_size = read_buf_avail = file_stat.m_comp_size; 3948 - comp_remaining = 0; 3949 - } else if (pUser_read_buf) { 3950 - /* Use a user provided read buffer. */ 3951 - if (!user_read_buf_size) return MZ_FALSE; 3952 - pRead_buf = (mz_uint8*)pUser_read_buf; 3953 - read_buf_size = user_read_buf_size; 3954 - read_buf_avail = 0; 3955 - comp_remaining = file_stat.m_comp_size; 3956 - } else { 3957 - /* Temporarily allocate a read buffer. */ 3958 - read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE); 3959 - if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF)) 3960 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 3961 - 3962 - if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) 3963 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 3964 - 3965 - read_buf_avail = 0; 3966 - comp_remaining = file_stat.m_comp_size; 3967 - } 3968 - 3969 - do { 3970 - /* The size_t cast here should be OK because we've verified that the output buffer is >= file_stat.m_uncomp_size 3971 - * above */ 3972 - size_t in_buf_size, out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs); 3973 - if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) { 3974 - read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); 3975 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) { 3976 - status = TINFL_STATUS_FAILED; 3977 - mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED); 3978 - break; 3979 - } 3980 - cur_file_ofs += read_buf_avail; 3981 - comp_remaining -= read_buf_avail; 3982 - read_buf_ofs = 0; 3983 - } 3984 - in_buf_size = (size_t)read_buf_avail; 3985 - status = tinfl_decompress( 3986 - &inflator, (mz_uint8*)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8*)pBuf, (mz_uint8*)pBuf + out_buf_ofs, 3987 - &out_buf_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0)); 3988 - read_buf_avail -= in_buf_size; 3989 - read_buf_ofs += in_buf_size; 3990 - out_buf_ofs += out_buf_size; 3991 - } while (status == TINFL_STATUS_NEEDS_MORE_INPUT); 3992 - 3993 - if (status == TINFL_STATUS_DONE) { 3994 - /* Make sure the entire file was decompressed, and check its CRC. */ 3995 - if (out_buf_ofs != file_stat.m_uncomp_size) { 3996 - mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE); 3997 - status = TINFL_STATUS_FAILED; 3998 - } 3999 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4000 - else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8*)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) { 4001 - mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED); 4002 - status = TINFL_STATUS_FAILED; 4003 - } 4004 - #endif 4005 - } 4006 - 4007 - if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf)) pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 4008 - 4009 - return status == TINFL_STATUS_DONE; 4010 - } 4011 - 4012 - mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive* pZip, const char* pFilename, void* pBuf, 4013 - size_t buf_size, mz_uint flags, void* pUser_read_buf, 4014 - size_t user_read_buf_size) { 4015 - mz_uint32 file_index; 4016 - if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) return MZ_FALSE; 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); 4019 - } 4020 - 4021 - mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, size_t buf_size, 4022 - mz_uint flags) { 4023 - return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0); 4024 - } 4025 - 4026 - mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive* pZip, const char* pFilename, void* pBuf, size_t buf_size, 4027 - mz_uint flags) { 4028 - return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, buf_size, flags, NULL, 0); 4029 - } 4030 - 4031 - void* mz_zip_reader_extract_to_heap(mz_zip_archive* pZip, mz_uint file_index, size_t* pSize, mz_uint flags) { 4032 - mz_uint64 comp_size, uncomp_size, alloc_size; 4033 - const mz_uint8* p = mz_zip_get_cdh(pZip, file_index); 4034 - void* pBuf; 4035 - 4036 - if (pSize) *pSize = 0; 4037 - 4038 - if (!p) { 4039 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4040 - return NULL; 4041 - } 4042 - 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; 4047 - if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) { 4048 - mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 4049 - return NULL; 4050 - } 4051 - 4052 - if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size))) { 4053 - mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4054 - return NULL; 4055 - } 4056 - 4057 - if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags)) { 4058 - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 4059 - return NULL; 4060 - } 4061 - 4062 - if (pSize) *pSize = (size_t)alloc_size; 4063 - return pBuf; 4064 - } 4065 - 4066 - void* mz_zip_reader_extract_file_to_heap(mz_zip_archive* pZip, const char* pFilename, size_t* pSize, mz_uint flags) { 4067 - mz_uint32 file_index; 4068 - if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) { 4069 - if (pSize) *pSize = 0; 4070 - return MZ_FALSE; 4071 - } 4072 - return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags); 4073 - } 4074 - 4075 - mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive* pZip, mz_uint file_index, mz_file_write_func pCallback, 4076 - void* pOpaque, mz_uint flags) { 4077 - int status = TINFL_STATUS_DONE; 4078 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4079 - mz_uint file_crc32 = MZ_CRC32_INIT; 4080 - #endif 4081 - mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs; 4082 - mz_zip_archive_file_stat file_stat; 4083 - void* pRead_buf = NULL; 4084 - void* pWrite_buf = NULL; 4085 - mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; 4086 - mz_uint8* pLocal_header = (mz_uint8*)local_header_u32; 4087 - 4088 - if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead)) 4089 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4090 - 4091 - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; 4092 - 4093 - /* A directory or zero length file */ 4094 - if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) return MZ_TRUE; 4095 - 4096 - /* Encryption and patch files are not supported. */ 4097 - if (file_stat.m_bit_flag & 4098 - (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | 4099 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) 4100 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); 4101 - 4102 - /* This function only supports decompressing stored and deflate. */ 4103 - if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 4104 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); 4105 - 4106 - /* Read and do some minimal validation of the local directory entry (this doesn't crack the zip64 stuff, which we 4107 - * already have from the central dir) */ 4108 - cur_file_ofs = file_stat.m_local_header_ofs; 4109 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != 4110 - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 4111 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4112 - 4113 - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 4114 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4115 - 4116 - cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 4117 - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 4118 - if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) 4119 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4120 - 4121 - /* Decompress the file either directly from memory or from a file input buffer. */ 4122 - if (pZip->m_pState->m_pMem) { 4123 - pRead_buf = (mz_uint8*)pZip->m_pState->m_pMem + cur_file_ofs; 4124 - read_buf_size = read_buf_avail = file_stat.m_comp_size; 4125 - comp_remaining = 0; 4126 - } else { 4127 - read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE); 4128 - if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) 4129 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4130 - 4131 - read_buf_avail = 0; 4132 - comp_remaining = file_stat.m_comp_size; 4133 - } 4134 - 4135 - if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) { 4136 - /* The file is stored or the caller has requested the compressed data. */ 4137 - if (pZip->m_pState->m_pMem) { 4138 - if (((sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > MZ_UINT32_MAX)) 4139 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 4140 - 4141 - if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size) { 4142 - mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED); 4143 - status = TINFL_STATUS_FAILED; 4144 - } else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { 4145 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4146 - file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8*)pRead_buf, (size_t)file_stat.m_comp_size); 4147 - #endif 4148 - } 4149 - 4150 - cur_file_ofs += file_stat.m_comp_size; 4151 - out_buf_ofs += file_stat.m_comp_size; 4152 - comp_remaining = 0; 4153 - } else { 4154 - while (comp_remaining) { 4155 - read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); 4156 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) { 4157 - mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4158 - status = TINFL_STATUS_FAILED; 4159 - break; 4160 - } 4161 - 4162 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4163 - if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { 4164 - file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8*)pRead_buf, (size_t)read_buf_avail); 4165 - } 4166 - #endif 4167 - 4168 - if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) { 4169 - mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED); 4170 - status = TINFL_STATUS_FAILED; 4171 - break; 4172 - } 4173 - 4174 - cur_file_ofs += read_buf_avail; 4175 - out_buf_ofs += read_buf_avail; 4176 - comp_remaining -= read_buf_avail; 4177 - } 4178 - } 4179 - } else { 4180 - tinfl_decompressor inflator; 4181 - tinfl_init(&inflator); 4182 - 4183 - if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) { 4184 - mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4185 - status = TINFL_STATUS_FAILED; 4186 - } else { 4187 - do { 4188 - mz_uint8* pWrite_buf_cur = (mz_uint8*)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); 4189 - size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); 4190 - if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) { 4191 - read_buf_avail = MZ_MIN(read_buf_size, comp_remaining); 4192 - if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) { 4193 - mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4194 - status = TINFL_STATUS_FAILED; 4195 - break; 4196 - } 4197 - cur_file_ofs += read_buf_avail; 4198 - comp_remaining -= read_buf_avail; 4199 - read_buf_ofs = 0; 4200 - } 4201 - 4202 - in_buf_size = (size_t)read_buf_avail; 4203 - status = 4204 - tinfl_decompress(&inflator, (const mz_uint8*)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8*)pWrite_buf, 4205 - pWrite_buf_cur, &out_buf_size, comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); 4206 - read_buf_avail -= in_buf_size; 4207 - read_buf_ofs += in_buf_size; 4208 - 4209 - if (out_buf_size) { 4210 - if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size) { 4211 - mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED); 4212 - status = TINFL_STATUS_FAILED; 4213 - break; 4214 - } 4215 - 4216 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4217 - file_crc32 = (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size); 4218 - #endif 4219 - if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) { 4220 - mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED); 4221 - status = TINFL_STATUS_FAILED; 4222 - break; 4223 - } 4224 - } 4225 - } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT)); 4226 - } 4227 - } 4228 - 4229 - if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) { 4230 - /* Make sure the entire file was decompressed, and check its CRC. */ 4231 - if (out_buf_ofs != file_stat.m_uncomp_size) { 4232 - mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE); 4233 - status = TINFL_STATUS_FAILED; 4234 - } 4235 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4236 - else if (file_crc32 != file_stat.m_crc32) { 4237 - mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED); 4238 - status = TINFL_STATUS_FAILED; 4239 - } 4240 - #endif 4241 - } 4242 - 4243 - if (!pZip->m_pState->m_pMem) pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 4244 - 4245 - if (pWrite_buf) pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf); 4246 - 4247 - return status == TINFL_STATUS_DONE; 4248 - } 4249 - 4250 - mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive* pZip, const char* pFilename, 4251 - mz_file_write_func pCallback, void* pOpaque, mz_uint flags) { 4252 - mz_uint32 file_index; 4253 - if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) return MZ_FALSE; 4254 - 4255 - return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags); 4256 - } 4257 - 4258 - mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive* pZip, mz_uint file_index, 4259 - mz_uint flags) { 4260 - mz_zip_reader_extract_iter_state* pState; 4261 - mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; 4262 - mz_uint8* pLocal_header = (mz_uint8*)local_header_u32; 4263 - 4264 - /* Argument sanity check */ 4265 - if ((!pZip) || (!pZip->m_pState)) return NULL; 4266 - 4267 - /* Allocate an iterator status structure */ 4268 - pState = (mz_zip_reader_extract_iter_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, 4269 - sizeof(mz_zip_reader_extract_iter_state)); 4270 - if (!pState) { 4271 - mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4272 - return NULL; 4273 - } 4274 - 4275 - /* Fetch file details */ 4276 - if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat)) { 4277 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4278 - return NULL; 4279 - } 4280 - 4281 - /* Encryption and patch files are not supported. */ 4282 - if (pState->file_stat.m_bit_flag & 4283 - (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | 4284 - MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) { 4285 - mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); 4286 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4287 - return NULL; 4288 - } 4289 - 4290 - /* This function only supports decompressing stored and deflate. */ 4291 - if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && 4292 - (pState->file_stat.m_method != MZ_DEFLATED)) { 4293 - mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); 4294 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4295 - return NULL; 4296 - } 4297 - 4298 - /* Init state - save args */ 4299 - pState->pZip = pZip; 4300 - pState->flags = flags; 4301 - 4302 - /* Init state - reset variables to defaults */ 4303 - pState->status = TINFL_STATUS_DONE; 4304 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4305 - pState->file_crc32 = MZ_CRC32_INIT; 4306 - #endif 4307 - pState->read_buf_ofs = 0; 4308 - pState->out_buf_ofs = 0; 4309 - pState->pRead_buf = NULL; 4310 - pState->pWrite_buf = NULL; 4311 - pState->out_blk_remain = 0; 4312 - 4313 - /* Read and parse the local directory entry. */ 4314 - pState->cur_file_ofs = pState->file_stat.m_local_header_ofs; 4315 - if (pZip->m_pRead(pZip->m_pIO_opaque, pState->cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != 4316 - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) { 4317 - mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4318 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4319 - return NULL; 4320 - } 4321 - 4322 - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) { 4323 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4324 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4325 - return NULL; 4326 - } 4327 - 4328 - pState->cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + 4329 - MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 4330 - if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size) { 4331 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4332 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4333 - return NULL; 4334 - } 4335 - 4336 - /* Decompress the file either directly from memory or from a file input buffer. */ 4337 - if (pZip->m_pState->m_pMem) { 4338 - pState->pRead_buf = (mz_uint8*)pZip->m_pState->m_pMem + pState->cur_file_ofs; 4339 - pState->read_buf_size = pState->read_buf_avail = pState->file_stat.m_comp_size; 4340 - pState->comp_remaining = pState->file_stat.m_comp_size; 4341 - } else { 4342 - if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) { 4343 - /* Decompression required, therefore intermediate read buffer required */ 4344 - pState->read_buf_size = MZ_MIN(pState->file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE); 4345 - if (NULL == (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)pState->read_buf_size))) { 4346 - mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4347 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4348 - return NULL; 4349 - } 4350 - } else { 4351 - /* Decompression not required - we will be reading directly into user buffer, no temp buf required */ 4352 - pState->read_buf_size = 0; 4353 - } 4354 - pState->read_buf_avail = 0; 4355 - pState->comp_remaining = pState->file_stat.m_comp_size; 4356 - } 4357 - 4358 - if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) { 4359 - /* Decompression required, init decompressor */ 4360 - tinfl_init(&pState->inflator); 4361 - 4362 - /* Allocate write buffer */ 4363 - if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) { 4364 - mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4365 - if (pState->pRead_buf) pZip->m_pFree(pZip->m_pAlloc_opaque, pState->pRead_buf); 4366 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4367 - return NULL; 4368 - } 4369 - } 4370 - 4371 - return pState; 4372 - } 4373 - 4374 - mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive* pZip, const char* pFilename, 4375 - mz_uint flags) { 4376 - mz_uint32 file_index; 4377 - 4378 - /* Locate file index by name */ 4379 - if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) return NULL; 4380 - 4381 - /* Construct iterator */ 4382 - return mz_zip_reader_extract_iter_new(pZip, file_index, flags); 4383 - } 4384 - 4385 - size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size) { 4386 - size_t copied_to_caller = 0; 4387 - 4388 - /* Argument sanity check */ 4389 - if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf)) return 0; 4390 - 4391 - if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)) { 4392 - /* The file is stored or the caller has requested the compressed data, calc amount to return. */ 4393 - copied_to_caller = (size_t)MZ_MIN(buf_size, pState->comp_remaining); 4394 - 4395 - /* Zip is in memory....or requires reading from a file? */ 4396 - if (pState->pZip->m_pState->m_pMem) { 4397 - /* Copy data to caller's buffer */ 4398 - memcpy(pvBuf, pState->pRead_buf, copied_to_caller); 4399 - pState->pRead_buf = ((mz_uint8*)pState->pRead_buf) + copied_to_caller; 4400 - } else { 4401 - /* Read directly into caller's buffer */ 4402 - if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != 4403 - copied_to_caller) { 4404 - /* Failed to read all that was asked for, flag failure and alert user */ 4405 - mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED); 4406 - pState->status = TINFL_STATUS_FAILED; 4407 - copied_to_caller = 0; 4408 - } 4409 - } 4410 - 4411 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4412 - /* Compute CRC if not returning compressed data only */ 4413 - if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) 4414 - pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, (const mz_uint8*)pvBuf, copied_to_caller); 4415 - #endif 4416 - 4417 - /* Advance offsets, dec counters */ 4418 - pState->cur_file_ofs += copied_to_caller; 4419 - pState->out_buf_ofs += copied_to_caller; 4420 - pState->comp_remaining -= copied_to_caller; 4421 - } else { 4422 - do { 4423 - /* Calc ptr to write buffer - given current output pos and block size */ 4424 - mz_uint8* pWrite_buf_cur = (mz_uint8*)pState->pWrite_buf + (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); 4425 - 4426 - /* Calc max output size - given current output pos and block size */ 4427 - size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1)); 4428 - 4429 - if (!pState->out_blk_remain) { 4430 - /* Read more data from file if none available (and reading from file) */ 4431 - if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem)) { 4432 - /* Calc read size */ 4433 - pState->read_buf_avail = MZ_MIN(pState->read_buf_size, pState->comp_remaining); 4434 - if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pState->pRead_buf, 4435 - (size_t)pState->read_buf_avail) != pState->read_buf_avail) { 4436 - mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED); 4437 - pState->status = TINFL_STATUS_FAILED; 4438 - break; 4439 - } 4440 - 4441 - /* Advance offsets, dec counters */ 4442 - pState->cur_file_ofs += pState->read_buf_avail; 4443 - pState->comp_remaining -= pState->read_buf_avail; 4444 - pState->read_buf_ofs = 0; 4445 - } 4446 - 4447 - /* Perform decompression */ 4448 - in_buf_size = (size_t)pState->read_buf_avail; 4449 - pState->status = tinfl_decompress(&pState->inflator, (const mz_uint8*)pState->pRead_buf + pState->read_buf_ofs, 4450 - &in_buf_size, (mz_uint8*)pState->pWrite_buf, pWrite_buf_cur, &out_buf_size, 4451 - pState->comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); 4452 - pState->read_buf_avail -= in_buf_size; 4453 - pState->read_buf_ofs += in_buf_size; 4454 - 4455 - /* Update current output block size remaining */ 4456 - pState->out_blk_remain = out_buf_size; 4457 - } 4458 - 4459 - if (pState->out_blk_remain) { 4460 - /* Calc amount to return. */ 4461 - size_t to_copy = MZ_MIN((buf_size - copied_to_caller), pState->out_blk_remain); 4462 - 4463 - /* Copy data to caller's buffer */ 4464 - memcpy((uint8_t*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy); 4465 - 4466 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4467 - /* Perform CRC */ 4468 - pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy); 4469 - #endif 4470 - 4471 - /* Decrement data consumed from block */ 4472 - pState->out_blk_remain -= to_copy; 4473 - 4474 - /* Inc output offset, while performing sanity check */ 4475 - if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size) { 4476 - mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED); 4477 - pState->status = TINFL_STATUS_FAILED; 4478 - break; 4479 - } 4480 - 4481 - /* Increment counter of data copied to caller */ 4482 - copied_to_caller += to_copy; 4483 - } 4484 - } while ((copied_to_caller < buf_size) && 4485 - ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT))); 4486 - } 4487 - 4488 - /* Return how many bytes were copied into user buffer */ 4489 - return copied_to_caller; 4490 - } 4491 - 4492 - mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState) { 4493 - int status; 4494 - 4495 - /* Argument sanity check */ 4496 - if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState)) return MZ_FALSE; 4497 - 4498 - /* Was decompression completed and requested? */ 4499 - if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) { 4500 - /* Make sure the entire file was decompressed, and check its CRC. */ 4501 - if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size) { 4502 - mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE); 4503 - pState->status = TINFL_STATUS_FAILED; 4504 - } 4505 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 4506 - else if (pState->file_crc32 != pState->file_stat.m_crc32) { 4507 - mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED); 4508 - pState->status = TINFL_STATUS_FAILED; 4509 - } 4510 - #endif 4511 - } 4512 - 4513 - /* Free buffers */ 4514 - if (!pState->pZip->m_pState->m_pMem) pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pRead_buf); 4515 - if (pState->pWrite_buf) pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pWrite_buf); 4516 - 4517 - /* Save status */ 4518 - status = pState->status; 4519 - 4520 - /* Free context */ 4521 - pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState); 4522 - 4523 - return status == TINFL_STATUS_DONE; 4524 - } 4525 - 4526 - #ifndef MINIZ_NO_STDIO 4527 - static size_t mz_zip_file_write_callback(void* pOpaque, mz_uint64 ofs, const void* pBuf, size_t n) { 4528 - (void)ofs; 4529 - 4530 - return MZ_FWRITE(pBuf, 1, n, (MZ_FILE*)pOpaque); 4531 - } 4532 - 4533 - mz_bool mz_zip_reader_extract_to_file(mz_zip_archive* pZip, mz_uint file_index, const char* pDst_filename, 4534 - mz_uint flags) { 4535 - mz_bool status; 4536 - mz_zip_archive_file_stat file_stat; 4537 - MZ_FILE* pFile; 4538 - 4539 - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; 4540 - 4541 - if ((file_stat.m_is_directory) || (!file_stat.m_is_supported)) 4542 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); 4543 - 4544 - pFile = MZ_FOPEN(pDst_filename, "wb"); 4545 - if (!pFile) return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); 4546 - 4547 - status = mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags); 4548 - 4549 - if (MZ_FCLOSE(pFile) == EOF) { 4550 - if (status) mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED); 4551 - 4552 - status = MZ_FALSE; 4553 - } 4554 - 4555 - #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO) 4556 - if (status) mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time); 4557 - #endif 4558 - 4559 - return status; 4560 - } 4561 - 4562 - mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive* pZip, const char* pArchive_filename, 4563 - const char* pDst_filename, mz_uint flags) { 4564 - mz_uint32 file_index; 4565 - if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index)) return MZ_FALSE; 4566 - 4567 - return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags); 4568 - } 4569 - 4570 - mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive* pZip, mz_uint file_index, MZ_FILE* pFile, mz_uint flags) { 4571 - mz_zip_archive_file_stat file_stat; 4572 - 4573 - if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; 4574 - 4575 - if ((file_stat.m_is_directory) || (!file_stat.m_is_supported)) 4576 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); 4577 - 4578 - return mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags); 4579 - } 4580 - 4581 - mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive* pZip, const char* pArchive_filename, MZ_FILE* pFile, 4582 - mz_uint flags) { 4583 - mz_uint32 file_index; 4584 - if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index)) return MZ_FALSE; 4585 - 4586 - return mz_zip_reader_extract_to_cfile(pZip, file_index, pFile, flags); 4587 - } 4588 - #endif /* #ifndef MINIZ_NO_STDIO */ 4589 - 4590 - static size_t mz_zip_compute_crc32_callback(void* pOpaque, mz_uint64 file_ofs, const void* pBuf, size_t n) { 4591 - mz_uint32* p = (mz_uint32*)pOpaque; 4592 - (void)file_ofs; 4593 - *p = (mz_uint32)mz_crc32(*p, (const mz_uint8*)pBuf, n); 4594 - return n; 4595 - } 4596 - 4597 - mz_bool mz_zip_validate_file(mz_zip_archive* pZip, mz_uint file_index, mz_uint flags) { 4598 - mz_zip_archive_file_stat file_stat; 4599 - mz_zip_internal_state* pState; 4600 - const mz_uint8* pCentral_dir_header; 4601 - mz_bool found_zip64_ext_data_in_cdir = MZ_FALSE; 4602 - mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE; 4603 - mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; 4604 - mz_uint8* pLocal_header = (mz_uint8*)local_header_u32; 4605 - mz_uint64 local_header_ofs = 0; 4606 - mz_uint32 local_header_filename_len, local_header_extra_len, local_header_crc32; 4607 - mz_uint64 local_header_comp_size, local_header_uncomp_size; 4608 - mz_uint32 uncomp_crc32 = MZ_CRC32_INIT; 4609 - mz_bool has_data_descriptor; 4610 - mz_uint32 local_header_bit_flags; 4611 - 4612 - mz_zip_array file_data_array; 4613 - mz_zip_array_init(&file_data_array, 1); 4614 - 4615 - if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 4616 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4617 - 4618 - if (file_index > pZip->m_total_files) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4619 - 4620 - pState = pZip->m_pState; 4621 - 4622 - pCentral_dir_header = mz_zip_get_cdh(pZip, file_index); 4623 - 4624 - if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header, &file_stat, &found_zip64_ext_data_in_cdir)) 4625 - return MZ_FALSE; 4626 - 4627 - /* A directory or zero length file */ 4628 - if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size)) return MZ_TRUE; 4629 - 4630 - /* Encryption and patch files are not supported. */ 4631 - if (file_stat.m_is_encrypted) return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION); 4632 - 4633 - /* This function only supports stored and deflate. */ 4634 - if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 4635 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD); 4636 - 4637 - if (!file_stat.m_is_supported) return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE); 4638 - 4639 - /* Read and parse the local directory entry. */ 4640 - local_header_ofs = file_stat.m_local_header_ofs; 4641 - if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != 4642 - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 4643 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4644 - 4645 - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 4646 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4647 - 4648 - local_header_filename_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS); 4649 - local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 4650 - local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS); 4651 - local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS); 4652 - local_header_crc32 = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_CRC32_OFS); 4653 - local_header_bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS); 4654 - has_data_descriptor = (local_header_bit_flags & 8) != 0; 4655 - 4656 - if (local_header_filename_len != strlen(file_stat.m_filename)) 4657 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4658 - 4659 - if ((local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + 4660 - file_stat.m_comp_size) > pZip->m_archive_size) 4661 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4662 - 4663 - if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), 4664 - MZ_FALSE)) { 4665 - mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4666 - goto handle_failure; 4667 - } 4668 - 4669 - if (local_header_filename_len) { 4670 - if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE, file_data_array.m_p, 4671 - local_header_filename_len) != local_header_filename_len) { 4672 - mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4673 - goto handle_failure; 4674 - } 4675 - 4676 - /* I've seen 1 archive that had the same pathname, but used backslashes in the local dir and forward slashes in the 4677 - * central dir. Do we care about this? For now, this case will fail validation. */ 4678 - if (memcmp(file_stat.m_filename, file_data_array.m_p, local_header_filename_len) != 0) { 4679 - mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); 4680 - goto handle_failure; 4681 - } 4682 - } 4683 - 4684 - if ((local_header_extra_len) && 4685 - ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) { 4686 - mz_uint32 extra_size_remaining = local_header_extra_len; 4687 - const mz_uint8* pExtra_data = (const mz_uint8*)file_data_array.m_p; 4688 - 4689 - if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len, 4690 - file_data_array.m_p, local_header_extra_len) != local_header_extra_len) { 4691 - mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4692 - goto handle_failure; 4693 - } 4694 - 4695 - do { 4696 - mz_uint32 field_id, field_data_size, field_total_size; 4697 - 4698 - if (extra_size_remaining < (sizeof(mz_uint16) * 2)) { 4699 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4700 - goto handle_failure; 4701 - } 4702 - 4703 - field_id = MZ_READ_LE16(pExtra_data); 4704 - field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); 4705 - field_total_size = field_data_size + sizeof(mz_uint16) * 2; 4706 - 4707 - if (field_total_size > extra_size_remaining) { 4708 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4709 - goto handle_failure; 4710 - } 4711 - 4712 - if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) { 4713 - const mz_uint8* pSrc_field_data = pExtra_data + sizeof(mz_uint32); 4714 - 4715 - if (field_data_size < sizeof(mz_uint64) * 2) { 4716 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 4717 - goto handle_failure; 4718 - } 4719 - 4720 - local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data); 4721 - local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); 4722 - 4723 - found_zip64_ext_data_in_ldir = MZ_TRUE; 4724 - break; 4725 - } 4726 - 4727 - pExtra_data += field_total_size; 4728 - extra_size_remaining -= field_total_size; 4729 - } while (extra_size_remaining); 4730 - } 4731 - 4732 - /* TODO: parse local header extra data when local_header_comp_size is 0xFFFFFFFF! (big_descriptor.zip) */ 4733 - /* I've seen zips in the wild with the data descriptor bit set, but proper local header values and bogus data 4734 - * descriptors */ 4735 - if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32)) { 4736 - mz_uint8 descriptor_buf[32]; 4737 - mz_bool has_id; 4738 - const mz_uint8* pSrc; 4739 - mz_uint32 file_crc32; 4740 - mz_uint64 comp_size = 0, uncomp_size = 0; 4741 - 4742 - mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4; 4743 - 4744 - if (pZip->m_pRead(pZip->m_pIO_opaque, 4745 - local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + 4746 - local_header_extra_len + file_stat.m_comp_size, 4747 - descriptor_buf, 4748 - sizeof(mz_uint32) * num_descriptor_uint32s) != (sizeof(mz_uint32) * num_descriptor_uint32s)) { 4749 - mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 4750 - goto handle_failure; 4751 - } 4752 - 4753 - has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID); 4754 - pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf; 4755 - 4756 - file_crc32 = MZ_READ_LE32(pSrc); 4757 - 4758 - if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) { 4759 - comp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32)); 4760 - uncomp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32) + sizeof(mz_uint64)); 4761 - } else { 4762 - comp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32)); 4763 - uncomp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32) + sizeof(mz_uint32)); 4764 - } 4765 - 4766 - if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || 4767 - (uncomp_size != file_stat.m_uncomp_size)) { 4768 - mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); 4769 - goto handle_failure; 4770 - } 4771 - } else { 4772 - if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || 4773 - (local_header_uncomp_size != file_stat.m_uncomp_size)) { 4774 - mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); 4775 - goto handle_failure; 4776 - } 4777 - } 4778 - 4779 - mz_zip_array_clear(pZip, &file_data_array); 4780 - 4781 - if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0) { 4782 - if (!mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0)) 4783 - return MZ_FALSE; 4784 - 4785 - /* 1 more check to be sure, although the extract checks too. */ 4786 - if (uncomp_crc32 != file_stat.m_crc32) { 4787 - mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); 4788 - return MZ_FALSE; 4789 - } 4790 - } 4791 - 4792 - return MZ_TRUE; 4793 - 4794 - handle_failure: 4795 - mz_zip_array_clear(pZip, &file_data_array); 4796 - return MZ_FALSE; 4797 - } 4798 - 4799 - mz_bool mz_zip_validate_archive(mz_zip_archive* pZip, mz_uint flags) { 4800 - mz_zip_internal_state* pState; 4801 - uint32_t i; 4802 - 4803 - if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 4804 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4805 - 4806 - pState = pZip->m_pState; 4807 - 4808 - /* Basic sanity checks */ 4809 - if (!pState->m_zip64) { 4810 - if (pZip->m_total_files > MZ_UINT16_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 4811 - 4812 - if (pZip->m_archive_size > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 4813 - } else { 4814 - if (pZip->m_total_files >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 4815 - 4816 - if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 4817 - } 4818 - 4819 - for (i = 0; i < pZip->m_total_files; i++) { 4820 - if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags) { 4821 - mz_uint32 found_index; 4822 - mz_zip_archive_file_stat stat; 4823 - 4824 - if (!mz_zip_reader_file_stat(pZip, i, &stat)) return MZ_FALSE; 4825 - 4826 - if (!mz_zip_reader_locate_file_v2(pZip, stat.m_filename, NULL, 0, &found_index)) return MZ_FALSE; 4827 - 4828 - /* This check can fail if there are duplicate filenames in the archive (which we don't check for when writing - 4829 - * that's up to the user) */ 4830 - if (found_index != i) return mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED); 4831 - } 4832 - 4833 - if (!mz_zip_validate_file(pZip, i, flags)) return MZ_FALSE; 4834 - } 4835 - 4836 - return MZ_TRUE; 4837 - } 4838 - 4839 - mz_bool mz_zip_validate_mem_archive(const void* pMem, size_t size, mz_uint flags, mz_zip_error* pErr) { 4840 - mz_bool success = MZ_TRUE; 4841 - mz_zip_archive zip; 4842 - mz_zip_error actual_err = MZ_ZIP_NO_ERROR; 4843 - 4844 - if ((!pMem) || (!size)) { 4845 - if (pErr) *pErr = MZ_ZIP_INVALID_PARAMETER; 4846 - return MZ_FALSE; 4847 - } 4848 - 4849 - mz_zip_zero_struct(&zip); 4850 - 4851 - if (!mz_zip_reader_init_mem(&zip, pMem, size, flags)) { 4852 - if (pErr) *pErr = zip.m_last_error; 4853 - return MZ_FALSE; 4854 - } 4855 - 4856 - if (!mz_zip_validate_archive(&zip, flags)) { 4857 - actual_err = zip.m_last_error; 4858 - success = MZ_FALSE; 4859 - } 4860 - 4861 - if (!mz_zip_reader_end_internal(&zip, success)) { 4862 - if (!actual_err) actual_err = zip.m_last_error; 4863 - success = MZ_FALSE; 4864 - } 4865 - 4866 - if (pErr) *pErr = actual_err; 4867 - 4868 - return success; 4869 - } 4870 - 4871 - #ifndef MINIZ_NO_STDIO 4872 - mz_bool mz_zip_validate_file_archive(const char* pFilename, mz_uint flags, mz_zip_error* pErr) { 4873 - mz_bool success = MZ_TRUE; 4874 - mz_zip_archive zip; 4875 - mz_zip_error actual_err = MZ_ZIP_NO_ERROR; 4876 - 4877 - if (!pFilename) { 4878 - if (pErr) *pErr = MZ_ZIP_INVALID_PARAMETER; 4879 - return MZ_FALSE; 4880 - } 4881 - 4882 - mz_zip_zero_struct(&zip); 4883 - 4884 - if (!mz_zip_reader_init_file_v2(&zip, pFilename, flags, 0, 0)) { 4885 - if (pErr) *pErr = zip.m_last_error; 4886 - return MZ_FALSE; 4887 - } 4888 - 4889 - if (!mz_zip_validate_archive(&zip, flags)) { 4890 - actual_err = zip.m_last_error; 4891 - success = MZ_FALSE; 4892 - } 4893 - 4894 - if (!mz_zip_reader_end_internal(&zip, success)) { 4895 - if (!actual_err) actual_err = zip.m_last_error; 4896 - success = MZ_FALSE; 4897 - } 4898 - 4899 - if (pErr) *pErr = actual_err; 4900 - 4901 - return success; 4902 - } 4903 - #endif /* #ifndef MINIZ_NO_STDIO */ 4904 - 4905 - /* ------------------- .ZIP archive writing */ 4906 - 4907 - #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 4908 - 4909 - static MZ_FORCEINLINE void mz_write_le16(mz_uint8* p, mz_uint16 v) { 4910 - p[0] = (mz_uint8)v; 4911 - p[1] = (mz_uint8)(v >> 8); 4912 - } 4913 - static MZ_FORCEINLINE void mz_write_le32(mz_uint8* p, mz_uint32 v) { 4914 - p[0] = (mz_uint8)v; 4915 - p[1] = (mz_uint8)(v >> 8); 4916 - p[2] = (mz_uint8)(v >> 16); 4917 - p[3] = (mz_uint8)(v >> 24); 4918 - } 4919 - static MZ_FORCEINLINE void mz_write_le64(mz_uint8* p, mz_uint64 v) { 4920 - mz_write_le32(p, (mz_uint32)v); 4921 - mz_write_le32(p + sizeof(mz_uint32), (mz_uint32)(v >> 32)); 4922 - } 4923 - 4924 - #define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8*)(p), (mz_uint16)(v)) 4925 - #define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8*)(p), (mz_uint32)(v)) 4926 - #define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8*)(p), (mz_uint64)(v)) 4927 - 4928 - static size_t mz_zip_heap_write_func(void* pOpaque, mz_uint64 file_ofs, const void* pBuf, size_t n) { 4929 - mz_zip_archive* pZip = (mz_zip_archive*)pOpaque; 4930 - mz_zip_internal_state* pState = pZip->m_pState; 4931 - mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size); 4932 - 4933 - if (!n) return 0; 4934 - 4935 - /* An allocation this big is likely to just fail on 32-bit systems, so don't even go there. */ 4936 - if ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF)) { 4937 - mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE); 4938 - return 0; 4939 - } 4940 - 4941 - if (new_size > pState->m_mem_capacity) { 4942 - void* pNew_block; 4943 - size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity); 4944 - 4945 - while (new_capacity < new_size) new_capacity *= 2; 4946 - 4947 - if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) { 4948 - mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 4949 - return 0; 4950 - } 4951 - 4952 - pState->m_pMem = pNew_block; 4953 - pState->m_mem_capacity = new_capacity; 4954 - } 4955 - memcpy((mz_uint8*)pState->m_pMem + file_ofs, pBuf, n); 4956 - pState->m_mem_size = (size_t)new_size; 4957 - return n; 4958 - } 4959 - 4960 - static mz_bool mz_zip_writer_end_internal(mz_zip_archive* pZip, mz_bool set_last_error) { 4961 - mz_zip_internal_state* pState; 4962 - mz_bool status = MZ_TRUE; 4963 - 4964 - if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || 4965 - ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) { 4966 - if (set_last_error) mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 4967 - return MZ_FALSE; 4968 - } 4969 - 4970 - pState = pZip->m_pState; 4971 - pZip->m_pState = NULL; 4972 - mz_zip_array_clear(pZip, &pState->m_central_dir); 4973 - mz_zip_array_clear(pZip, &pState->m_central_dir_offsets); 4974 - mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets); 4975 - 4976 - #ifndef MINIZ_NO_STDIO 4977 - if (pState->m_pFile) { 4978 - if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) { 4979 - if (MZ_FCLOSE(pState->m_pFile) == EOF) { 4980 - if (set_last_error) mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED); 4981 - status = MZ_FALSE; 4982 - } 4983 - } 4984 - 4985 - pState->m_pFile = NULL; 4986 - } 4987 - #endif /* #ifndef MINIZ_NO_STDIO */ 4988 - 4989 - if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) { 4990 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem); 4991 - pState->m_pMem = NULL; 4992 - } 4993 - 4994 - pZip->m_pFree(pZip->m_pAlloc_opaque, pState); 4995 - pZip->m_zip_mode = MZ_ZIP_MODE_INVALID; 4996 - return status; 4997 - } 4998 - 4999 - mz_bool mz_zip_writer_init_v2(mz_zip_archive* pZip, mz_uint64 existing_size, mz_uint flags) { 5000 - mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0; 5001 - 5002 - if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 5003 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5004 - 5005 - if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) { 5006 - if (!pZip->m_pRead) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5007 - } 5008 - 5009 - if (pZip->m_file_offset_alignment) { 5010 - /* Ensure user specified file offset alignment is a power of 2. */ 5011 - if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1)) 5012 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5013 - } 5014 - 5015 - if (!pZip->m_pAlloc) pZip->m_pAlloc = miniz_def_alloc_func; 5016 - if (!pZip->m_pFree) pZip->m_pFree = miniz_def_free_func; 5017 - if (!pZip->m_pRealloc) pZip->m_pRealloc = miniz_def_realloc_func; 5018 - 5019 - pZip->m_archive_size = existing_size; 5020 - pZip->m_central_directory_file_ofs = 0; 5021 - pZip->m_total_files = 0; 5022 - 5023 - if (NULL == (pZip->m_pState = 5024 - (mz_zip_internal_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) 5025 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 5026 - 5027 - memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state)); 5028 - 5029 - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8)); 5030 - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32)); 5031 - MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32)); 5032 - 5033 - pZip->m_pState->m_zip64 = zip64; 5034 - pZip->m_pState->m_zip64_has_extended_info_fields = zip64; 5035 - 5036 - pZip->m_zip_type = MZ_ZIP_TYPE_USER; 5037 - pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; 5038 - 5039 - return MZ_TRUE; 5040 - } 5041 - 5042 - mz_bool mz_zip_writer_init(mz_zip_archive* pZip, mz_uint64 existing_size) { 5043 - return mz_zip_writer_init_v2(pZip, existing_size, 0); 5044 - } 5045 - 5046 - mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive* pZip, size_t size_to_reserve_at_beginning, 5047 - size_t initial_allocation_size, mz_uint flags) { 5048 - pZip->m_pWrite = mz_zip_heap_write_func; 5049 - pZip->m_pNeeds_keepalive = NULL; 5050 - 5051 - if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) pZip->m_pRead = mz_zip_mem_read_func; 5052 - 5053 - pZip->m_pIO_opaque = pZip; 5054 - 5055 - if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags)) return MZ_FALSE; 5056 - 5057 - pZip->m_zip_type = MZ_ZIP_TYPE_HEAP; 5058 - 5059 - if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning))) { 5060 - if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size))) { 5061 - mz_zip_writer_end_internal(pZip, MZ_FALSE); 5062 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 5063 - } 5064 - pZip->m_pState->m_mem_capacity = initial_allocation_size; 5065 - } 5066 - 5067 - return MZ_TRUE; 5068 - } 5069 - 5070 - mz_bool mz_zip_writer_init_heap(mz_zip_archive* pZip, size_t size_to_reserve_at_beginning, 5071 - size_t initial_allocation_size) { 5072 - return mz_zip_writer_init_heap_v2(pZip, size_to_reserve_at_beginning, initial_allocation_size, 0); 5073 - } 5074 - 5075 - #ifndef MINIZ_NO_STDIO 5076 - static size_t mz_zip_file_write_func(void* pOpaque, mz_uint64 file_ofs, const void* pBuf, size_t n) { 5077 - mz_zip_archive* pZip = (mz_zip_archive*)pOpaque; 5078 - mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); 5079 - 5080 - file_ofs += pZip->m_pState->m_file_archive_start_ofs; 5081 - 5082 - if (((mz_int64)file_ofs < 0) || 5083 - (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET)))) { 5084 - mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED); 5085 - return 0; 5086 - } 5087 - 5088 - return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile); 5089 - } 5090 - 5091 - mz_bool mz_zip_writer_init_file(mz_zip_archive* pZip, const char* pFilename, mz_uint64 size_to_reserve_at_beginning) { 5092 - return mz_zip_writer_init_file_v2(pZip, pFilename, size_to_reserve_at_beginning, 0); 5093 - } 5094 - 5095 - mz_bool mz_zip_writer_init_file_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint64 size_to_reserve_at_beginning, 5096 - mz_uint flags) { 5097 - MZ_FILE* pFile; 5098 - 5099 - pZip->m_pWrite = mz_zip_file_write_func; 5100 - pZip->m_pNeeds_keepalive = NULL; 5101 - 5102 - if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) pZip->m_pRead = mz_zip_file_read_func; 5103 - 5104 - pZip->m_pIO_opaque = pZip; 5105 - 5106 - if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags)) return MZ_FALSE; 5107 - 5108 - if (NULL == (pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ? "w+b" : "wb"))) { 5109 - mz_zip_writer_end(pZip); 5110 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); 5111 - } 5112 - 5113 - pZip->m_pState->m_pFile = pFile; 5114 - pZip->m_zip_type = MZ_ZIP_TYPE_FILE; 5115 - 5116 - if (size_to_reserve_at_beginning) { 5117 - mz_uint64 cur_ofs = 0; 5118 - char buf[4096]; 5119 - 5120 - MZ_CLEAR_OBJ(buf); 5121 - 5122 - do { 5123 - size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning); 5124 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n) { 5125 - mz_zip_writer_end(pZip); 5126 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5127 - } 5128 - cur_ofs += n; 5129 - size_to_reserve_at_beginning -= n; 5130 - } while (size_to_reserve_at_beginning); 5131 - } 5132 - 5133 - return MZ_TRUE; 5134 - } 5135 - 5136 - mz_bool mz_zip_writer_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint flags) { 5137 - pZip->m_pWrite = mz_zip_file_write_func; 5138 - pZip->m_pNeeds_keepalive = NULL; 5139 - 5140 - if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) pZip->m_pRead = mz_zip_file_read_func; 5141 - 5142 - pZip->m_pIO_opaque = pZip; 5143 - 5144 - if (!mz_zip_writer_init_v2(pZip, 0, flags)) return MZ_FALSE; 5145 - 5146 - pZip->m_pState->m_pFile = pFile; 5147 - pZip->m_pState->m_file_archive_start_ofs = MZ_FTELL64(pZip->m_pState->m_pFile); 5148 - pZip->m_zip_type = MZ_ZIP_TYPE_CFILE; 5149 - 5150 - return MZ_TRUE; 5151 - } 5152 - #endif /* #ifndef MINIZ_NO_STDIO */ 5153 - 5154 - mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint flags) { 5155 - mz_zip_internal_state* pState; 5156 - 5157 - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 5158 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5159 - 5160 - if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) { 5161 - /* We don't support converting a non-zip64 file to zip64 - this seems like more trouble than it's worth. (What about 5162 - * the existing 32-bit data descriptors that could follow the compressed data?) */ 5163 - if (!pZip->m_pState->m_zip64) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5164 - } 5165 - 5166 - /* No sense in trying to write to an archive that's already at the support max size */ 5167 - if (pZip->m_pState->m_zip64) { 5168 - if (pZip->m_total_files == MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 5169 - } else { 5170 - if (pZip->m_total_files == MZ_UINT16_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 5171 - 5172 - if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX) 5173 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE); 5174 - } 5175 - 5176 - pState = pZip->m_pState; 5177 - 5178 - if (pState->m_pFile) { 5179 - #ifdef MINIZ_NO_STDIO 5180 - (void)pFilename; 5181 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5182 - #else 5183 - if (pZip->m_pIO_opaque != pZip) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5184 - 5185 - if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) { 5186 - if (!pFilename) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5187 - 5188 - /* Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable. */ 5189 - if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile))) { 5190 - /* The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it. */ 5191 - mz_zip_reader_end_internal(pZip, MZ_FALSE); 5192 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); 5193 - } 5194 - } 5195 - 5196 - pZip->m_pWrite = mz_zip_file_write_func; 5197 - pZip->m_pNeeds_keepalive = NULL; 5198 - #endif /* #ifdef MINIZ_NO_STDIO */ 5199 - } else if (pState->m_pMem) { 5200 - /* Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback. */ 5201 - if (pZip->m_pIO_opaque != pZip) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5202 - 5203 - pState->m_mem_capacity = pState->m_mem_size; 5204 - pZip->m_pWrite = mz_zip_heap_write_func; 5205 - pZip->m_pNeeds_keepalive = NULL; 5206 - } 5207 - /* Archive is being read via a user provided read function - make sure the user has specified a write function too. */ 5208 - else if (!pZip->m_pWrite) 5209 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5210 - 5211 - /* Start writing new files at the archive's current central directory location. */ 5212 - /* TODO: We could add a flag that lets the user start writing immediately AFTER the existing central dir - this would 5213 - * be safer. */ 5214 - pZip->m_archive_size = pZip->m_central_directory_file_ofs; 5215 - pZip->m_central_directory_file_ofs = 0; 5216 - 5217 - /* Clear the sorted central dir offsets, they aren't useful or maintained now. */ 5218 - /* Even though we're now in write mode, files can still be extracted and verified, but file locates will be slow. */ 5219 - /* TODO: We could easily maintain the sorted central directory offsets. */ 5220 - mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets); 5221 - 5222 - pZip->m_zip_mode = MZ_ZIP_MODE_WRITING; 5223 - 5224 - return MZ_TRUE; 5225 - } 5226 - 5227 - mz_bool mz_zip_writer_init_from_reader(mz_zip_archive* pZip, const char* pFilename) { 5228 - return mz_zip_writer_init_from_reader_v2(pZip, pFilename, 0); 5229 - } 5230 - 5231 - /* TODO: pArchive_name is a terrible name here! */ 5232 - mz_bool mz_zip_writer_add_mem(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, size_t buf_size, 5233 - mz_uint level_and_flags) { 5234 - return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, level_and_flags, 0, 0); 5235 - } 5236 - 5237 - typedef struct { 5238 - mz_zip_archive* m_pZip; 5239 - mz_uint64 m_cur_archive_file_ofs; 5240 - mz_uint64 m_comp_size; 5241 - } mz_zip_writer_add_state; 5242 - 5243 - static mz_bool mz_zip_writer_add_put_buf_callback(const void* pBuf, int len, void* pUser) { 5244 - mz_zip_writer_add_state* pState = (mz_zip_writer_add_state*)pUser; 5245 - if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len) 5246 - return MZ_FALSE; 5247 - 5248 - pState->m_cur_archive_file_ofs += len; 5249 - pState->m_comp_size += len; 5250 - return MZ_TRUE; 5251 - } 5252 - 5253 - #define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2) 5254 - #define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3) 5255 - static mz_uint32 mz_zip_writer_create_zip64_extra_data(mz_uint8* pBuf, mz_uint64* pUncomp_size, mz_uint64* pComp_size, 5256 - mz_uint64* pLocal_header_ofs) { 5257 - mz_uint8* pDst = pBuf; 5258 - mz_uint32 field_size = 0; 5259 - 5260 - MZ_WRITE_LE16(pDst + 0, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID); 5261 - MZ_WRITE_LE16(pDst + 2, 0); 5262 - pDst += sizeof(mz_uint16) * 2; 5263 - 5264 - if (pUncomp_size) { 5265 - MZ_WRITE_LE64(pDst, *pUncomp_size); 5266 - pDst += sizeof(mz_uint64); 5267 - field_size += sizeof(mz_uint64); 5268 - } 5269 - 5270 - if (pComp_size) { 5271 - MZ_WRITE_LE64(pDst, *pComp_size); 5272 - pDst += sizeof(mz_uint64); 5273 - field_size += sizeof(mz_uint64); 5274 - } 5275 - 5276 - if (pLocal_header_ofs) { 5277 - MZ_WRITE_LE64(pDst, *pLocal_header_ofs); 5278 - pDst += sizeof(mz_uint64); 5279 - field_size += sizeof(mz_uint64); 5280 - } 5281 - 5282 - MZ_WRITE_LE16(pBuf + 2, field_size); 5283 - 5284 - return (mz_uint32)(pDst - pBuf); 5285 - } 5286 - 5287 - static mz_bool mz_zip_writer_create_local_dir_header(mz_zip_archive* pZip, mz_uint8* pDst, mz_uint16 filename_size, 5288 - mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size, 5289 - mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, 5290 - mz_uint16 dos_time, mz_uint16 dos_date) { 5291 - (void)pZip; 5292 - memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE); 5293 - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG); 5294 - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0); 5295 - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags); 5296 - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method); 5297 - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time); 5298 - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date); 5299 - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32); 5300 - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX)); 5301 - MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX)); 5302 - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size); 5303 - MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size); 5304 - return MZ_TRUE; 5305 - } 5306 - 5307 - static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive* pZip, mz_uint8* pDst, mz_uint16 filename_size, 5308 - mz_uint16 extra_size, mz_uint16 comment_size, 5309 - mz_uint64 uncomp_size, mz_uint64 comp_size, 5310 - mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, 5311 - mz_uint16 dos_time, mz_uint16 dos_date, 5312 - mz_uint64 local_header_ofs, mz_uint32 ext_attributes) { 5313 - (void)pZip; 5314 - memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); 5315 - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG); 5316 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0); 5317 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags); 5318 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method); 5319 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time); 5320 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date); 5321 - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32); 5322 - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX)); 5323 - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX)); 5324 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size); 5325 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size); 5326 - MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size); 5327 - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes); 5328 - MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_MIN(local_header_ofs, MZ_UINT32_MAX)); 5329 - return MZ_TRUE; 5330 - } 5331 - 5332 - static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive* pZip, const char* pFilename, mz_uint16 filename_size, 5333 - const void* pExtra, mz_uint16 extra_size, const void* pComment, 5334 - mz_uint16 comment_size, mz_uint64 uncomp_size, mz_uint64 comp_size, 5335 - mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, 5336 - mz_uint16 dos_time, mz_uint16 dos_date, mz_uint64 local_header_ofs, 5337 - mz_uint32 ext_attributes, const char* user_extra_data, 5338 - mz_uint user_extra_data_len) { 5339 - mz_zip_internal_state* pState = pZip->m_pState; 5340 - mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size; 5341 - size_t orig_central_dir_size = pState->m_central_dir.m_size; 5342 - mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; 5343 - 5344 - if (!pZip->m_pState->m_zip64) { 5345 - if (local_header_ofs > 0xFFFFFFFF) return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE); 5346 - } 5347 - 5348 - /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */ 5349 - if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + 5350 - user_extra_data_len + comment_size) >= MZ_UINT32_MAX) 5351 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); 5352 - 5353 - if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, 5354 - (mz_uint16)(extra_size + user_extra_data_len), comment_size, uncomp_size, 5355 - comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, 5356 - local_header_ofs, ext_attributes)) 5357 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 5358 - 5359 - if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) || 5360 - (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) || 5361 - (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) || 5362 - (!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data, user_extra_data_len)) || 5363 - (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) || 5364 - (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &central_dir_ofs, 1))) { 5365 - /* Try to resize the central directory array back into its original state. */ 5366 - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 5367 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 5368 - } 5369 - 5370 - return MZ_TRUE; 5371 - } 5372 - 5373 - static mz_bool mz_zip_writer_validate_archive_name(const char* pArchive_name) { 5374 - /* Basic ZIP archive filename validity checks: Valid filenames cannot start with a forward slash, cannot contain a 5375 - * drive letter, and cannot use DOS-style backward slashes. */ 5376 - if (*pArchive_name == '/') return MZ_FALSE; 5377 - 5378 - /* Making sure the name does not contain drive letters or DOS style backward slashes is the responsibility of the 5379 - * program using miniz*/ 5380 - 5381 - return MZ_TRUE; 5382 - } 5383 - 5384 - static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive* pZip) { 5385 - mz_uint32 n; 5386 - if (!pZip->m_file_offset_alignment) return 0; 5387 - n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1)); 5388 - return (mz_uint)((pZip->m_file_offset_alignment - n) & (pZip->m_file_offset_alignment - 1)); 5389 - } 5390 - 5391 - static mz_bool mz_zip_writer_write_zeros(mz_zip_archive* pZip, mz_uint64 cur_file_ofs, mz_uint32 n) { 5392 - char buf[4096]; 5393 - memset(buf, 0, MZ_MIN(sizeof(buf), n)); 5394 - while (n) { 5395 - mz_uint32 s = MZ_MIN(sizeof(buf), n); 5396 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s) 5397 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5398 - 5399 - cur_file_ofs += s; 5400 - n -= s; 5401 - } 5402 - return MZ_TRUE; 5403 - } 5404 - 5405 - mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, size_t buf_size, 5406 - const void* pComment, mz_uint16 comment_size, mz_uint level_and_flags, 5407 - mz_uint64 uncomp_size, mz_uint32 uncomp_crc32) { 5408 - return mz_zip_writer_add_mem_ex_v2(pZip, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 5409 - uncomp_size, uncomp_crc32, NULL, NULL, 0, NULL, 0); 5410 - } 5411 - 5412 - mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, size_t buf_size, 5413 - const void* pComment, mz_uint16 comment_size, mz_uint level_and_flags, 5414 - mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, MZ_TIME_T* last_modified, 5415 - const char* user_extra_data, mz_uint user_extra_data_len, 5416 - const char* user_extra_data_central, mz_uint user_extra_data_central_len) { 5417 - mz_uint16 method = 0, dos_time = 0, dos_date = 0; 5418 - mz_uint level, ext_attributes = 0, num_alignment_padding_bytes; 5419 - mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0; 5420 - size_t archive_name_size; 5421 - mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; 5422 - tdefl_compressor* pComp = NULL; 5423 - mz_bool store_data_uncompressed; 5424 - mz_zip_internal_state* pState; 5425 - mz_uint8* pExtra_data = NULL; 5426 - mz_uint32 extra_size = 0; 5427 - mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE]; 5428 - mz_uint16 bit_flags = 0; 5429 - 5430 - if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; 5431 - 5432 - if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 5433 - bit_flags |= MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; 5434 - 5435 - if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) bit_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; 5436 - 5437 - level = level_and_flags & 0xF; 5438 - store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)); 5439 - 5440 - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || 5441 - (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 5442 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5443 - 5444 - pState = pZip->m_pState; 5445 - 5446 - if (pState->m_zip64) { 5447 - if (pZip->m_total_files == MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 5448 - } else { 5449 - if (pZip->m_total_files == MZ_UINT16_MAX) { 5450 - pState->m_zip64 = MZ_TRUE; 5451 - /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */ 5452 - } 5453 - if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) { 5454 - pState->m_zip64 = MZ_TRUE; 5455 - /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ 5456 - } 5457 - } 5458 - 5459 - if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size)) 5460 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5461 - 5462 - if (!mz_zip_writer_validate_archive_name(pArchive_name)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); 5463 - 5464 - #ifndef MINIZ_NO_TIME 5465 - if (last_modified != NULL) { 5466 - mz_zip_time_t_to_dos_time(*last_modified, &dos_time, &dos_date); 5467 - } else { 5468 - MZ_TIME_T cur_time; 5469 - time(&cur_time); 5470 - mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date); 5471 - } 5472 - #endif /* #ifndef MINIZ_NO_TIME */ 5473 - 5474 - if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { 5475 - uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8*)pBuf, buf_size); 5476 - uncomp_size = buf_size; 5477 - if (uncomp_size <= 3) { 5478 - level = 0; 5479 - store_data_uncompressed = MZ_TRUE; 5480 - } 5481 - } 5482 - 5483 - archive_name_size = strlen(pArchive_name); 5484 - if (archive_name_size > MZ_UINT16_MAX) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); 5485 - 5486 - num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); 5487 - 5488 - /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */ 5489 - if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + 5490 - MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX) 5491 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); 5492 - 5493 - if (!pState->m_zip64) { 5494 - /* Bail early if the archive would obviously become too large */ 5495 - if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + 5496 - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len + 5497 - pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + user_extra_data_central_len + 5498 - MZ_ZIP_DATA_DESCRIPTER_SIZE32) > 0xFFFFFFFF) { 5499 - pState->m_zip64 = MZ_TRUE; 5500 - /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ 5501 - } 5502 - } 5503 - 5504 - if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) { 5505 - /* Set DOS Subdirectory attribute bit. */ 5506 - ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG; 5507 - 5508 - /* Subdirectories cannot contain data. */ 5509 - if ((buf_size) || (uncomp_size)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5510 - } 5511 - 5512 - /* Try to do any allocations before writing to the archive, so if an allocation fails the file remains unmodified. (A 5513 - * good idea if we're doing an in-place modification.) */ 5514 - if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, 5515 - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + 5516 - (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || 5517 - (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) 5518 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 5519 - 5520 - if ((!store_data_uncompressed) && (buf_size)) { 5521 - if (NULL == (pComp = (tdefl_compressor*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)))) 5522 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 5523 - } 5524 - 5525 - if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) { 5526 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5527 - return MZ_FALSE; 5528 - } 5529 - 5530 - local_dir_header_ofs += num_alignment_padding_bytes; 5531 - if (pZip->m_file_offset_alignment) { 5532 - MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0); 5533 - } 5534 - cur_archive_file_ofs += num_alignment_padding_bytes; 5535 - 5536 - MZ_CLEAR_OBJ(local_dir_header); 5537 - 5538 - if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { 5539 - method = MZ_DEFLATED; 5540 - } 5541 - 5542 - if (pState->m_zip64) { 5543 - if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) { 5544 - pExtra_data = extra_data; 5545 - extra_size = 5546 - mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 5547 - (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, 5548 - (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 5549 - } 5550 - 5551 - if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 5552 - (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, 5553 - bit_flags, dos_time, dos_date)) 5554 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 5555 - 5556 - if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != 5557 - sizeof(local_dir_header)) 5558 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5559 - 5560 - cur_archive_file_ofs += sizeof(local_dir_header); 5561 - 5562 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != 5563 - archive_name_size) { 5564 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5565 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5566 - } 5567 - cur_archive_file_ofs += archive_name_size; 5568 - 5569 - if (pExtra_data != NULL) { 5570 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size) 5571 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5572 - 5573 - cur_archive_file_ofs += extra_size; 5574 - } 5575 - } else { 5576 - if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) 5577 - return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 5578 - if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 5579 - (mz_uint16)user_extra_data_len, 0, 0, 0, method, bit_flags, dos_time, 5580 - dos_date)) 5581 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 5582 - 5583 - if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != 5584 - sizeof(local_dir_header)) 5585 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5586 - 5587 - cur_archive_file_ofs += sizeof(local_dir_header); 5588 - 5589 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != 5590 - archive_name_size) { 5591 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5592 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5593 - } 5594 - cur_archive_file_ofs += archive_name_size; 5595 - } 5596 - 5597 - if (user_extra_data_len > 0) { 5598 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != 5599 - user_extra_data_len) 5600 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5601 - 5602 - cur_archive_file_ofs += user_extra_data_len; 5603 - } 5604 - 5605 - if (store_data_uncompressed) { 5606 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size) { 5607 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5608 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5609 - } 5610 - 5611 - cur_archive_file_ofs += buf_size; 5612 - comp_size = buf_size; 5613 - } else if (buf_size) { 5614 - mz_zip_writer_add_state state; 5615 - 5616 - state.m_pZip = pZip; 5617 - state.m_cur_archive_file_ofs = cur_archive_file_ofs; 5618 - state.m_comp_size = 0; 5619 - 5620 - if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, 5621 - tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) || 5622 - (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != TDEFL_STATUS_DONE)) { 5623 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5624 - return mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED); 5625 - } 5626 - 5627 - comp_size = state.m_comp_size; 5628 - cur_archive_file_ofs = state.m_cur_archive_file_ofs; 5629 - } 5630 - 5631 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5632 - pComp = NULL; 5633 - 5634 - if (uncomp_size) { 5635 - mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64]; 5636 - mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32; 5637 - 5638 - MZ_ASSERT(bit_flags & MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR); 5639 - 5640 - MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID); 5641 - MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32); 5642 - if (pExtra_data == NULL) { 5643 - if (comp_size > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 5644 - 5645 - MZ_WRITE_LE32(local_dir_footer + 8, comp_size); 5646 - MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size); 5647 - } else { 5648 - MZ_WRITE_LE64(local_dir_footer + 8, comp_size); 5649 - MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size); 5650 - local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64; 5651 - } 5652 - 5653 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != 5654 - local_dir_footer_size) 5655 - return MZ_FALSE; 5656 - 5657 - cur_archive_file_ofs += local_dir_footer_size; 5658 - } 5659 - 5660 - if (pExtra_data != NULL) { 5661 - extra_size = 5662 - mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 5663 - (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, 5664 - (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 5665 - } 5666 - 5667 - if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, 5668 - (mz_uint16)extra_size, pComment, comment_size, uncomp_size, comp_size, 5669 - uncomp_crc32, method, bit_flags, dos_time, dos_date, local_dir_header_ofs, 5670 - ext_attributes, user_extra_data_central, user_extra_data_central_len)) 5671 - return MZ_FALSE; 5672 - 5673 - pZip->m_total_files++; 5674 - pZip->m_archive_size = cur_archive_file_ofs; 5675 - 5676 - return MZ_TRUE; 5677 - } 5678 - 5679 - mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive* pZip, const char* pArchive_name, 5680 - mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, 5681 - const MZ_TIME_T* pFile_time, const void* pComment, mz_uint16 comment_size, 5682 - mz_uint level_and_flags, const char* user_extra_data, 5683 - mz_uint user_extra_data_len, const char* user_extra_data_central, 5684 - mz_uint user_extra_data_central_len) { 5685 - mz_uint16 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; 5686 - mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; 5687 - mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; 5688 - mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; 5689 - size_t archive_name_size; 5690 - mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; 5691 - mz_uint8* pExtra_data = NULL; 5692 - mz_uint32 extra_size = 0; 5693 - mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE]; 5694 - mz_zip_internal_state* pState; 5695 - mz_uint64 file_ofs = 0, cur_archive_header_file_ofs; 5696 - 5697 - if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; 5698 - 5699 - if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; 5700 - level = level_and_flags & 0xF; 5701 - 5702 - /* Sanity checks */ 5703 - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || 5704 - ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 5705 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5706 - 5707 - pState = pZip->m_pState; 5708 - 5709 - if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) { 5710 - /* Source file is too large for non-zip64 */ 5711 - /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ 5712 - pState->m_zip64 = MZ_TRUE; 5713 - } 5714 - 5715 - /* We could support this, but why? */ 5716 - if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 5717 - 5718 - if (!mz_zip_writer_validate_archive_name(pArchive_name)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); 5719 - 5720 - if (pState->m_zip64) { 5721 - if (pZip->m_total_files == MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 5722 - } else { 5723 - if (pZip->m_total_files == MZ_UINT16_MAX) { 5724 - pState->m_zip64 = MZ_TRUE; 5725 - /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */ 5726 - } 5727 - } 5728 - 5729 - archive_name_size = strlen(pArchive_name); 5730 - if (archive_name_size > MZ_UINT16_MAX) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME); 5731 - 5732 - num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); 5733 - 5734 - /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */ 5735 - if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + 5736 - MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX) 5737 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); 5738 - 5739 - if (!pState->m_zip64) { 5740 - /* Bail early if the archive would obviously become too large */ 5741 - if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + 5742 - MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len + 5743 - pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024 + MZ_ZIP_DATA_DESCRIPTER_SIZE32 + 5744 - user_extra_data_central_len) > 0xFFFFFFFF) { 5745 - pState->m_zip64 = MZ_TRUE; 5746 - /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ 5747 - } 5748 - } 5749 - 5750 - #ifndef MINIZ_NO_TIME 5751 - if (pFile_time) { 5752 - mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date); 5753 - } 5754 - #endif 5755 - 5756 - if (max_size <= 3) level = 0; 5757 - 5758 - if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) { 5759 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5760 - } 5761 - 5762 - cur_archive_file_ofs += num_alignment_padding_bytes; 5763 - local_dir_header_ofs = cur_archive_file_ofs; 5764 - 5765 - if (pZip->m_file_offset_alignment) { 5766 - MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) == 0); 5767 - } 5768 - 5769 - if (max_size && level) { 5770 - method = MZ_DEFLATED; 5771 - } 5772 - 5773 - MZ_CLEAR_OBJ(local_dir_header); 5774 - if (pState->m_zip64) { 5775 - if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) { 5776 - pExtra_data = extra_data; 5777 - if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) 5778 - extra_size = mz_zip_writer_create_zip64_extra_data( 5779 - extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 5780 - (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, 5781 - (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 5782 - else 5783 - extra_size = mz_zip_writer_create_zip64_extra_data( 5784 - extra_data, NULL, NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 5785 - } 5786 - 5787 - if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 5788 - (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, 5789 - gen_flags, dos_time, dos_date)) 5790 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 5791 - 5792 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != 5793 - sizeof(local_dir_header)) 5794 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5795 - 5796 - cur_archive_file_ofs += sizeof(local_dir_header); 5797 - 5798 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != 5799 - archive_name_size) { 5800 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5801 - } 5802 - 5803 - cur_archive_file_ofs += archive_name_size; 5804 - 5805 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size) 5806 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5807 - 5808 - cur_archive_file_ofs += extra_size; 5809 - } else { 5810 - if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) 5811 - return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 5812 - if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 5813 - (mz_uint16)user_extra_data_len, 0, 0, 0, method, gen_flags, dos_time, 5814 - dos_date)) 5815 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 5816 - 5817 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != 5818 - sizeof(local_dir_header)) 5819 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5820 - 5821 - cur_archive_file_ofs += sizeof(local_dir_header); 5822 - 5823 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != 5824 - archive_name_size) { 5825 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5826 - } 5827 - 5828 - cur_archive_file_ofs += archive_name_size; 5829 - } 5830 - 5831 - if (user_extra_data_len > 0) { 5832 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != 5833 - user_extra_data_len) 5834 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5835 - 5836 - cur_archive_file_ofs += user_extra_data_len; 5837 - } 5838 - 5839 - if (max_size) { 5840 - void* pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE); 5841 - if (!pRead_buf) { 5842 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 5843 - } 5844 - 5845 - if (!level) { 5846 - while (1) { 5847 - size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE); 5848 - if (n == 0) break; 5849 - 5850 - if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) { 5851 - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 5852 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 5853 - } 5854 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n) { 5855 - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 5856 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5857 - } 5858 - file_ofs += n; 5859 - uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8*)pRead_buf, n); 5860 - cur_archive_file_ofs += n; 5861 - } 5862 - uncomp_size = file_ofs; 5863 - comp_size = uncomp_size; 5864 - } else { 5865 - mz_bool result = MZ_FALSE; 5866 - mz_zip_writer_add_state state; 5867 - tdefl_compressor* pComp = (tdefl_compressor*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)); 5868 - if (!pComp) { 5869 - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 5870 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 5871 - } 5872 - 5873 - state.m_pZip = pZip; 5874 - state.m_cur_archive_file_ofs = cur_archive_file_ofs; 5875 - state.m_comp_size = 0; 5876 - 5877 - if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, 5878 - tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) { 5879 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5880 - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 5881 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 5882 - } 5883 - 5884 - for (;;) { 5885 - tdefl_status status; 5886 - tdefl_flush flush = TDEFL_NO_FLUSH; 5887 - 5888 - size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE); 5889 - if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) { 5890 - mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 5891 - break; 5892 - } 5893 - 5894 - file_ofs += n; 5895 - uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8*)pRead_buf, n); 5896 - 5897 - if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque)) flush = TDEFL_FULL_FLUSH; 5898 - 5899 - if (n == 0) flush = TDEFL_FINISH; 5900 - 5901 - status = tdefl_compress_buffer(pComp, pRead_buf, n, flush); 5902 - if (status == TDEFL_STATUS_DONE) { 5903 - result = MZ_TRUE; 5904 - break; 5905 - } else if (status != TDEFL_STATUS_OKAY) { 5906 - mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED); 5907 - break; 5908 - } 5909 - } 5910 - 5911 - pZip->m_pFree(pZip->m_pAlloc_opaque, pComp); 5912 - 5913 - if (!result) { 5914 - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 5915 - return MZ_FALSE; 5916 - } 5917 - 5918 - uncomp_size = file_ofs; 5919 - comp_size = state.m_comp_size; 5920 - cur_archive_file_ofs = state.m_cur_archive_file_ofs; 5921 - } 5922 - 5923 - pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); 5924 - } 5925 - 5926 - if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)) { 5927 - mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64]; 5928 - mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32; 5929 - 5930 - MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID); 5931 - MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32); 5932 - if (pExtra_data == NULL) { 5933 - if (comp_size > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 5934 - 5935 - MZ_WRITE_LE32(local_dir_footer + 8, comp_size); 5936 - MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size); 5937 - } else { 5938 - MZ_WRITE_LE64(local_dir_footer + 8, comp_size); 5939 - MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size); 5940 - local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64; 5941 - } 5942 - 5943 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != 5944 - local_dir_footer_size) 5945 - return MZ_FALSE; 5946 - 5947 - cur_archive_file_ofs += local_dir_footer_size; 5948 - } 5949 - 5950 - if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) { 5951 - if (pExtra_data != NULL) { 5952 - extra_size = 5953 - mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 5954 - (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, 5955 - (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 5956 - } 5957 - 5958 - if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 5959 - (mz_uint16)(extra_size + user_extra_data_len), 5960 - (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : uncomp_size, 5961 - (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : comp_size, uncomp_crc32, 5962 - method, gen_flags, dos_time, dos_date)) 5963 - return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); 5964 - 5965 - cur_archive_header_file_ofs = local_dir_header_ofs; 5966 - 5967 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, local_dir_header, sizeof(local_dir_header)) != 5968 - sizeof(local_dir_header)) 5969 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5970 - 5971 - if (pExtra_data != NULL) { 5972 - cur_archive_header_file_ofs += sizeof(local_dir_header); 5973 - 5974 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != 5975 - archive_name_size) { 5976 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5977 - } 5978 - 5979 - cur_archive_header_file_ofs += archive_name_size; 5980 - 5981 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size) 5982 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 5983 - 5984 - cur_archive_header_file_ofs += extra_size; 5985 - } 5986 - } 5987 - 5988 - if (pExtra_data != NULL) { 5989 - extra_size = 5990 - mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 5991 - (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, 5992 - (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 5993 - } 5994 - 5995 - if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, 5996 - (mz_uint16)extra_size, pComment, comment_size, uncomp_size, comp_size, 5997 - uncomp_crc32, method, gen_flags, dos_time, dos_date, local_dir_header_ofs, 5998 - ext_attributes, user_extra_data_central, user_extra_data_central_len)) 5999 - return MZ_FALSE; 6000 - 6001 - pZip->m_total_files++; 6002 - pZip->m_archive_size = cur_archive_file_ofs; 6003 - 6004 - return MZ_TRUE; 6005 - } 6006 - 6007 - #ifndef MINIZ_NO_STDIO 6008 - 6009 - static size_t mz_file_read_func_stdio(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n) { 6010 - MZ_FILE* pSrc_file = (MZ_FILE*)pOpaque; 6011 - mz_int64 cur_ofs = MZ_FTELL64(pSrc_file); 6012 - 6013 - if (((mz_int64)file_ofs < 0) || 6014 - (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pSrc_file, (mz_int64)file_ofs, SEEK_SET)))) 6015 - return 0; 6016 - 6017 - return MZ_FREAD(pBuf, 1, n, pSrc_file); 6018 - } 6019 - 6020 - mz_bool mz_zip_writer_add_cfile(mz_zip_archive* pZip, const char* pArchive_name, MZ_FILE* pSrc_file, mz_uint64 max_size, 6021 - const MZ_TIME_T* pFile_time, const void* pComment, mz_uint16 comment_size, 6022 - mz_uint level_and_flags, const char* user_extra_data, mz_uint user_extra_data_len, 6023 - const char* user_extra_data_central, mz_uint user_extra_data_central_len) { 6024 - return mz_zip_writer_add_read_buf_callback(pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, max_size, 6025 - pFile_time, pComment, comment_size, level_and_flags, user_extra_data, 6026 - user_extra_data_len, user_extra_data_central, user_extra_data_central_len); 6027 - } 6028 - 6029 - mz_bool mz_zip_writer_add_file(mz_zip_archive* pZip, const char* pArchive_name, const char* pSrc_filename, 6030 - const void* pComment, mz_uint16 comment_size, mz_uint level_and_flags) { 6031 - MZ_FILE* pSrc_file = NULL; 6032 - mz_uint64 uncomp_size = 0; 6033 - MZ_TIME_T file_modified_time; 6034 - MZ_TIME_T* pFile_time = NULL; 6035 - mz_bool status; 6036 - 6037 - memset(&file_modified_time, 0, sizeof(file_modified_time)); 6038 - 6039 - #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO) 6040 - pFile_time = &file_modified_time; 6041 - if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time)) 6042 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_STAT_FAILED); 6043 - #endif 6044 - 6045 - pSrc_file = MZ_FOPEN(pSrc_filename, "rb"); 6046 - if (!pSrc_file) return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED); 6047 - 6048 - MZ_FSEEK64(pSrc_file, 0, SEEK_END); 6049 - uncomp_size = MZ_FTELL64(pSrc_file); 6050 - MZ_FSEEK64(pSrc_file, 0, SEEK_SET); 6051 - 6052 - status = mz_zip_writer_add_cfile(pZip, pArchive_name, pSrc_file, uncomp_size, pFile_time, pComment, comment_size, 6053 - level_and_flags, NULL, 0, NULL, 0); 6054 - 6055 - MZ_FCLOSE(pSrc_file); 6056 - 6057 - return status; 6058 - } 6059 - #endif /* #ifndef MINIZ_NO_STDIO */ 6060 - 6061 - static mz_bool mz_zip_writer_update_zip64_extension_block(mz_zip_array* pNew_ext, mz_zip_archive* pZip, 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) { 6065 - /* + 64 should be enough for any new zip64 data */ 6066 - if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE)) return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6067 - 6068 - mz_zip_array_resize(pZip, pNew_ext, 0, MZ_FALSE); 6069 - 6070 - if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) { 6071 - mz_uint8 new_ext_block[64]; 6072 - mz_uint8* pDst = new_ext_block; 6073 - mz_write_le16(pDst, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID); 6074 - mz_write_le16(pDst + sizeof(mz_uint16), 0); 6075 - pDst += sizeof(mz_uint16) * 2; 6076 - 6077 - if (pUncomp_size) { 6078 - mz_write_le64(pDst, *pUncomp_size); 6079 - pDst += sizeof(mz_uint64); 6080 - } 6081 - 6082 - if (pComp_size) { 6083 - mz_write_le64(pDst, *pComp_size); 6084 - pDst += sizeof(mz_uint64); 6085 - } 6086 - 6087 - if (pLocal_header_ofs) { 6088 - mz_write_le64(pDst, *pLocal_header_ofs); 6089 - pDst += sizeof(mz_uint64); 6090 - } 6091 - 6092 - if (pDisk_start) { 6093 - mz_write_le32(pDst, *pDisk_start); 6094 - pDst += sizeof(mz_uint32); 6095 - } 6096 - 6097 - mz_write_le16(new_ext_block + sizeof(mz_uint16), (mz_uint16)((pDst - new_ext_block) - sizeof(mz_uint16) * 2)); 6098 - 6099 - if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block)) 6100 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6101 - } 6102 - 6103 - if ((pExt) && (ext_len)) { 6104 - mz_uint32 extra_size_remaining = ext_len; 6105 - const mz_uint8* pExtra_data = pExt; 6106 - 6107 - do { 6108 - mz_uint32 field_id, field_data_size, field_total_size; 6109 - 6110 - if (extra_size_remaining < (sizeof(mz_uint16) * 2)) 6111 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 6112 - 6113 - field_id = MZ_READ_LE16(pExtra_data); 6114 - field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); 6115 - field_total_size = field_data_size + sizeof(mz_uint16) * 2; 6116 - 6117 - if (field_total_size > extra_size_remaining) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 6118 - 6119 - if (field_id != MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) { 6120 - if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size)) 6121 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6122 - } 6123 - 6124 - pExtra_data += field_total_size; 6125 - extra_size_remaining -= field_total_size; 6126 - } while (extra_size_remaining); 6127 - } 6128 - 6129 - return MZ_TRUE; 6130 - } 6131 - 6132 - /* TODO: This func is now pretty freakin complex due to zip64, split it up? */ 6133 - mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive* pZip, mz_zip_archive* pSource_zip, mz_uint src_file_index) { 6134 - mz_uint n, bit_flags, num_alignment_padding_bytes, src_central_dir_following_data_size; 6135 - mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs; 6136 - mz_uint64 cur_src_file_ofs, cur_dst_file_ofs; 6137 - mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)]; 6138 - mz_uint8* pLocal_header = (mz_uint8*)local_header_u32; 6139 - mz_uint8 new_central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE]; 6140 - size_t orig_central_dir_size; 6141 - mz_zip_internal_state* pState; 6142 - void* pBuf; 6143 - const mz_uint8* pSrc_central_header; 6144 - mz_zip_archive_file_stat src_file_stat; 6145 - mz_uint32 src_filename_len, src_comment_len, src_ext_len; 6146 - mz_uint32 local_header_filename_size, local_header_extra_len; 6147 - mz_uint64 local_header_comp_size, local_header_uncomp_size; 6148 - mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE; 6149 - 6150 - /* Sanity checks */ 6151 - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead)) 6152 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6153 - 6154 - pState = pZip->m_pState; 6155 - 6156 - /* Don't support copying files from zip64 archives to non-zip64, even though in some cases this is possible */ 6157 - if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64)) 6158 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6159 - 6160 - /* Get pointer to the source central dir header and crack it */ 6161 - if (NULL == (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index))) 6162 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6163 - 6164 - if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG) 6165 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 6166 - 6167 - src_filename_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS); 6168 - src_comment_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS); 6169 - src_ext_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS); 6170 - src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len; 6171 - 6172 - /* TODO: We don't support central dir's >= MZ_UINT32_MAX bytes right now (+32 fudge factor in case we need to add more 6173 - * extra data) */ 6174 - if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + 32) >= 6175 - MZ_UINT32_MAX) 6176 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); 6177 - 6178 - num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip); 6179 - 6180 - if (!pState->m_zip64) { 6181 - if (pZip->m_total_files == MZ_UINT16_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 6182 - } else { 6183 - /* TODO: Our zip64 support still has some 32-bit limits that may not be worth fixing. */ 6184 - if (pZip->m_total_files == MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 6185 - } 6186 - 6187 - if (!mz_zip_file_stat_internal(pSource_zip, src_file_index, pSrc_central_header, &src_file_stat, NULL)) 6188 - return MZ_FALSE; 6189 - 6190 - cur_src_file_ofs = src_file_stat.m_local_header_ofs; 6191 - cur_dst_file_ofs = pZip->m_archive_size; 6192 - 6193 - /* Read the source archive's local dir header */ 6194 - if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != 6195 - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 6196 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 6197 - 6198 - if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 6199 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 6200 - 6201 - cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; 6202 - 6203 - /* Compute the total size we need to copy (filename+extra data+compressed data) */ 6204 - local_header_filename_size = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS); 6205 - local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS); 6206 - local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS); 6207 - local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS); 6208 - src_archive_bytes_remaining = local_header_filename_size + local_header_extra_len + src_file_stat.m_comp_size; 6209 - 6210 - /* Try to find a zip64 extended information field */ 6211 - if ((local_header_extra_len) && 6212 - ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) { 6213 - mz_zip_array file_data_array; 6214 - const mz_uint8* pExtra_data; 6215 - mz_uint32 extra_size_remaining = local_header_extra_len; 6216 - 6217 - mz_zip_array_init(&file_data_array, 1); 6218 - if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE)) { 6219 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6220 - } 6221 - 6222 - if (pSource_zip->m_pRead( 6223 - pSource_zip->m_pIO_opaque, 6224 - src_file_stat.m_local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_size, 6225 - file_data_array.m_p, local_header_extra_len) != local_header_extra_len) { 6226 - mz_zip_array_clear(pZip, &file_data_array); 6227 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 6228 - } 6229 - 6230 - pExtra_data = (const mz_uint8*)file_data_array.m_p; 6231 - 6232 - do { 6233 - mz_uint32 field_id, field_data_size, field_total_size; 6234 - 6235 - if (extra_size_remaining < (sizeof(mz_uint16) * 2)) { 6236 - mz_zip_array_clear(pZip, &file_data_array); 6237 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 6238 - } 6239 - 6240 - field_id = MZ_READ_LE16(pExtra_data); 6241 - field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); 6242 - field_total_size = field_data_size + sizeof(mz_uint16) * 2; 6243 - 6244 - if (field_total_size > extra_size_remaining) { 6245 - mz_zip_array_clear(pZip, &file_data_array); 6246 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 6247 - } 6248 - 6249 - if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) { 6250 - const mz_uint8* pSrc_field_data = pExtra_data + sizeof(mz_uint32); 6251 - 6252 - if (field_data_size < sizeof(mz_uint64) * 2) { 6253 - mz_zip_array_clear(pZip, &file_data_array); 6254 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); 6255 - } 6256 - 6257 - local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data); 6258 - local_header_comp_size = 6259 - MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); /* may be 0 if there's a descriptor */ 6260 - 6261 - found_zip64_ext_data_in_ldir = MZ_TRUE; 6262 - break; 6263 - } 6264 - 6265 - pExtra_data += field_total_size; 6266 - extra_size_remaining -= field_total_size; 6267 - } while (extra_size_remaining); 6268 - 6269 - mz_zip_array_clear(pZip, &file_data_array); 6270 - } 6271 - 6272 - if (!pState->m_zip64) { 6273 - /* Try to detect if the new archive will most likely wind up too big and bail early (+(sizeof(mz_uint32) * 4) is for 6274 - * the optional descriptor which could be present, +64 is a fudge factor). */ 6275 - /* We also check when the archive is finalized so this doesn't need to be perfect. */ 6276 - mz_uint64 approx_new_archive_size = 6277 - cur_dst_file_ofs + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + src_archive_bytes_remaining + 6278 - (sizeof(mz_uint32) * 4) + pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + 6279 - src_central_dir_following_data_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 64; 6280 - 6281 - if (approx_new_archive_size >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 6282 - } 6283 - 6284 - /* Write dest archive padding */ 6285 - if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes)) return MZ_FALSE; 6286 - 6287 - cur_dst_file_ofs += num_alignment_padding_bytes; 6288 - 6289 - local_dir_header_ofs = cur_dst_file_ofs; 6290 - if (pZip->m_file_offset_alignment) { 6291 - MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0); 6292 - } 6293 - 6294 - /* The original zip's local header+ext block doesn't change, even with zip64, so we can just copy it over to the dest 6295 - * zip */ 6296 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != 6297 - MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 6298 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 6299 - 6300 - cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE; 6301 - 6302 - /* Copy over the source archive bytes to the dest archive, also ensure we have enough buf space to handle optional 6303 - * data descriptor */ 6304 - if (NULL == (pBuf = pZip->m_pAlloc( 6305 - pZip->m_pAlloc_opaque, 1, 6306 - (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining))))) 6307 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6308 - 6309 - while (src_archive_bytes_remaining) { 6310 - n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining); 6311 - if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n) { 6312 - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 6313 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 6314 - } 6315 - cur_src_file_ofs += n; 6316 - 6317 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) { 6318 - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 6319 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 6320 - } 6321 - cur_dst_file_ofs += n; 6322 - 6323 - src_archive_bytes_remaining -= n; 6324 - } 6325 - 6326 - /* Now deal with the optional data descriptor */ 6327 - bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS); 6328 - if (bit_flags & 8) { 6329 - /* Copy data descriptor */ 6330 - if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir)) { 6331 - /* src is zip64, dest must be zip64 */ 6332 - 6333 - /* name uint32_t's */ 6334 - /* id 1 (optional in zip64?) */ 6335 - /* crc 1 */ 6336 - /* comp_size 2 */ 6337 - /* uncomp_size 2 */ 6338 - if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (sizeof(mz_uint32) * 6)) != 6339 - (sizeof(mz_uint32) * 6)) { 6340 - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 6341 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 6342 - } 6343 - 6344 - n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5); 6345 - } else { 6346 - /* src is NOT zip64 */ 6347 - mz_bool has_id; 6348 - 6349 - if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != 6350 - sizeof(mz_uint32) * 4) { 6351 - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 6352 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); 6353 - } 6354 - 6355 - has_id = (MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID); 6356 - 6357 - if (pZip->m_pState->m_zip64) { 6358 - /* dest is zip64, so upgrade the data descriptor */ 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]; 6363 - 6364 - mz_write_le32((mz_uint8*)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID); 6365 - mz_write_le32((mz_uint8*)pBuf + sizeof(mz_uint32) * 1, src_crc32); 6366 - mz_write_le64((mz_uint8*)pBuf + sizeof(mz_uint32) * 2, src_comp_size); 6367 - mz_write_le64((mz_uint8*)pBuf + sizeof(mz_uint32) * 4, src_uncomp_size); 6368 - 6369 - n = sizeof(mz_uint32) * 6; 6370 - } else { 6371 - /* dest is NOT zip64, just copy it as-is */ 6372 - n = sizeof(mz_uint32) * (has_id ? 4 : 3); 6373 - } 6374 - } 6375 - 6376 - if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) { 6377 - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 6378 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 6379 - } 6380 - 6381 - cur_src_file_ofs += n; 6382 - cur_dst_file_ofs += n; 6383 - } 6384 - pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); 6385 - 6386 - /* Finally, add the new central dir header */ 6387 - orig_central_dir_size = pState->m_central_dir.m_size; 6388 - 6389 - memcpy(new_central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE); 6390 - 6391 - if (pState->m_zip64) { 6392 - /* This is the painful part: We need to write a new central dir header + ext block with updated zip64 fields, and 6393 - * ensure the old fields (if any) are not included. */ 6394 - const mz_uint8* pSrc_ext = pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len; 6395 - mz_zip_array new_ext_block; 6396 - 6397 - mz_zip_array_init(&new_ext_block, sizeof(mz_uint8)); 6398 - 6399 - MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_UINT32_MAX); 6400 - MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_UINT32_MAX); 6401 - MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_UINT32_MAX); 6402 - 6403 - if (!mz_zip_writer_update_zip64_extension_block(&new_ext_block, pZip, pSrc_ext, src_ext_len, 6404 - &src_file_stat.m_comp_size, &src_file_stat.m_uncomp_size, 6405 - &local_dir_header_ofs, NULL)) { 6406 - mz_zip_array_clear(pZip, &new_ext_block); 6407 - return MZ_FALSE; 6408 - } 6409 - 6410 - MZ_WRITE_LE16(new_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS, new_ext_block.m_size); 6411 - 6412 - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) { 6413 - mz_zip_array_clear(pZip, &new_ext_block); 6414 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6415 - } 6416 - 6417 - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, 6418 - src_filename_len)) { 6419 - mz_zip_array_clear(pZip, &new_ext_block); 6420 - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 6421 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6422 - } 6423 - 6424 - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size)) { 6425 - mz_zip_array_clear(pZip, &new_ext_block); 6426 - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 6427 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6428 - } 6429 - 6430 - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, 6431 - pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len + src_ext_len, 6432 - src_comment_len)) { 6433 - mz_zip_array_clear(pZip, &new_ext_block); 6434 - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 6435 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6436 - } 6437 - 6438 - mz_zip_array_clear(pZip, &new_ext_block); 6439 - } else { 6440 - /* sanity checks */ 6441 - if (cur_dst_file_ofs > MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 6442 - 6443 - if (local_dir_header_ofs >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); 6444 - 6445 - MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_dir_header_ofs); 6446 - 6447 - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) 6448 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6449 - 6450 - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, 6451 - src_central_dir_following_data_size)) { 6452 - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 6453 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6454 - } 6455 - } 6456 - 6457 - /* This shouldn't trigger unless we screwed up during the initial sanity checks */ 6458 - if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) { 6459 - /* TODO: Support central dirs >= 32-bits in size */ 6460 - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 6461 - return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE); 6462 - } 6463 - 6464 - n = (mz_uint32)orig_central_dir_size; 6465 - if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) { 6466 - mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 6467 - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); 6468 - } 6469 - 6470 - pZip->m_total_files++; 6471 - pZip->m_archive_size = cur_dst_file_ofs; 6472 - 6473 - return MZ_TRUE; 6474 - } 6475 - 6476 - mz_bool mz_zip_writer_finalize_archive(mz_zip_archive* pZip) { 6477 - mz_zip_internal_state* pState; 6478 - mz_uint64 central_dir_ofs, central_dir_size; 6479 - mz_uint8 hdr[256]; 6480 - 6481 - if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) 6482 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6483 - 6484 - pState = pZip->m_pState; 6485 - 6486 - if (pState->m_zip64) { 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); 6489 - } else { 6490 - if ((pZip->m_total_files > MZ_UINT16_MAX) || 6491 - ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)) 6492 - return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); 6493 - } 6494 - 6495 - central_dir_ofs = 0; 6496 - central_dir_size = 0; 6497 - if (pZip->m_total_files) { 6498 - /* Write central directory */ 6499 - central_dir_ofs = pZip->m_archive_size; 6500 - central_dir_size = pState->m_central_dir.m_size; 6501 - pZip->m_central_directory_file_ofs = central_dir_ofs; 6502 - if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, pState->m_central_dir.m_p, (size_t)central_dir_size) != 6503 - central_dir_size) 6504 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 6505 - 6506 - pZip->m_archive_size += central_dir_size; 6507 - } 6508 - 6509 - if (pState->m_zip64) { 6510 - /* Write zip64 end of central directory header */ 6511 - mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size; 6512 - 6513 - MZ_CLEAR_OBJ(hdr); 6514 - MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG); 6515 - MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS, 6516 - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) - sizeof(mz_uint64)); 6517 - MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS, 0x031E); /* TODO: always Unix */ 6518 - MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_NEEDED_OFS, 0x002D); 6519 - MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, pZip->m_total_files); 6520 - MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files); 6521 - MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_SIZE_OFS, central_dir_size); 6522 - MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_OFS_OFS, central_dir_ofs); 6523 - if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) != 6524 - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) 6525 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 6526 - 6527 - pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE; 6528 - 6529 - /* Write zip64 end of central directory locator */ 6530 - MZ_CLEAR_OBJ(hdr); 6531 - MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG); 6532 - MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr); 6533 - MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1); 6534 - if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) != 6535 - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) 6536 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 6537 - 6538 - pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE; 6539 - } 6540 - 6541 - /* Write end of central directory record */ 6542 - MZ_CLEAR_OBJ(hdr); 6543 - MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG); 6544 - MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); 6545 - MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); 6546 - MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_size)); 6547 - MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_ofs)); 6548 - 6549 - if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != 6550 - MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) 6551 - return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); 6552 - 6553 - #ifndef MINIZ_NO_STDIO 6554 - if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF)) return mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED); 6555 - #endif /* #ifndef MINIZ_NO_STDIO */ 6556 - 6557 - pZip->m_archive_size += MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE; 6558 - 6559 - pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED; 6560 - return MZ_TRUE; 6561 - } 6562 - 6563 - mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive* pZip, void** ppBuf, size_t* pSize) { 6564 - if ((!ppBuf) || (!pSize)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6565 - 6566 - *ppBuf = NULL; 6567 - *pSize = 0; 6568 - 6569 - if ((!pZip) || (!pZip->m_pState)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6570 - 6571 - if (pZip->m_pWrite != mz_zip_heap_write_func) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6572 - 6573 - if (!mz_zip_writer_finalize_archive(pZip)) return MZ_FALSE; 6574 - 6575 - *ppBuf = pZip->m_pState->m_pMem; 6576 - *pSize = pZip->m_pState->m_mem_size; 6577 - pZip->m_pState->m_pMem = NULL; 6578 - pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0; 6579 - 6580 - return MZ_TRUE; 6581 - } 6582 - 6583 - mz_bool mz_zip_writer_end(mz_zip_archive* pZip) { return mz_zip_writer_end_internal(pZip, MZ_TRUE); } 6584 - 6585 - #ifndef MINIZ_NO_STDIO 6586 - mz_bool mz_zip_add_mem_to_archive_file_in_place(const char* pZip_filename, const char* pArchive_name, const void* pBuf, 6587 - size_t buf_size, const void* pComment, mz_uint16 comment_size, 6588 - mz_uint level_and_flags) { 6589 - return mz_zip_add_mem_to_archive_file_in_place_v2(pZip_filename, pArchive_name, pBuf, buf_size, pComment, 6590 - comment_size, level_and_flags, NULL); 6591 - } 6592 - 6593 - mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(const char* pZip_filename, const char* pArchive_name, 6594 - const void* pBuf, size_t buf_size, const void* pComment, 6595 - mz_uint16 comment_size, mz_uint level_and_flags, 6596 - mz_zip_error* pErr) { 6597 - mz_bool status, created_new_archive = MZ_FALSE; 6598 - mz_zip_archive zip_archive; 6599 - struct MZ_FILE_STAT_STRUCT file_stat; 6600 - mz_zip_error actual_err = MZ_ZIP_NO_ERROR; 6601 - 6602 - mz_zip_zero_struct(&zip_archive); 6603 - if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; 6604 - 6605 - if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || 6606 - ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION)) { 6607 - if (pErr) *pErr = MZ_ZIP_INVALID_PARAMETER; 6608 - return MZ_FALSE; 6609 - } 6610 - 6611 - if (!mz_zip_writer_validate_archive_name(pArchive_name)) { 6612 - if (pErr) *pErr = MZ_ZIP_INVALID_FILENAME; 6613 - return MZ_FALSE; 6614 - } 6615 - 6616 - /* Important: The regular non-64 bit version of stat() can fail here if the file is very large, which could cause the 6617 - * archive to be overwritten. */ 6618 - /* So be sure to compile with _LARGEFILE64_SOURCE 1 */ 6619 - if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0) { 6620 - /* Create a new archive. */ 6621 - if (!mz_zip_writer_init_file_v2(&zip_archive, pZip_filename, 0, level_and_flags)) { 6622 - if (pErr) *pErr = zip_archive.m_last_error; 6623 - return MZ_FALSE; 6624 - } 6625 - 6626 - created_new_archive = MZ_TRUE; 6627 - } else { 6628 - /* Append to an existing archive. */ 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)) { 6631 - if (pErr) *pErr = zip_archive.m_last_error; 6632 - return MZ_FALSE; 6633 - } 6634 - 6635 - if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename, level_and_flags)) { 6636 - if (pErr) *pErr = zip_archive.m_last_error; 6637 - 6638 - mz_zip_reader_end_internal(&zip_archive, MZ_FALSE); 6639 - 6640 - return MZ_FALSE; 6641 - } 6642 - } 6643 - 6644 - status = mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, 6645 - level_and_flags, 0, 0); 6646 - actual_err = zip_archive.m_last_error; 6647 - 6648 - /* Always finalize, even if adding failed for some reason, so we have a valid central directory. (This may not always 6649 - * succeed, but we can try.) */ 6650 - if (!mz_zip_writer_finalize_archive(&zip_archive)) { 6651 - if (!actual_err) actual_err = zip_archive.m_last_error; 6652 - 6653 - status = MZ_FALSE; 6654 - } 6655 - 6656 - if (!mz_zip_writer_end_internal(&zip_archive, status)) { 6657 - if (!actual_err) actual_err = zip_archive.m_last_error; 6658 - 6659 - status = MZ_FALSE; 6660 - } 6661 - 6662 - if ((!status) && (created_new_archive)) { 6663 - /* It's a new archive and something went wrong, so just delete it. */ 6664 - int ignoredStatus = MZ_DELETE_FILE(pZip_filename); 6665 - (void)ignoredStatus; 6666 - } 6667 - 6668 - if (pErr) *pErr = actual_err; 6669 - 6670 - return status; 6671 - } 6672 - 6673 - void* mz_zip_extract_archive_file_to_heap_v2(const char* pZip_filename, const char* pArchive_name, const char* pComment, 6674 - size_t* pSize, mz_uint flags, mz_zip_error* pErr) { 6675 - mz_uint32 file_index; 6676 - mz_zip_archive zip_archive; 6677 - void* p = NULL; 6678 - 6679 - if (pSize) *pSize = 0; 6680 - 6681 - if ((!pZip_filename) || (!pArchive_name)) { 6682 - if (pErr) *pErr = MZ_ZIP_INVALID_PARAMETER; 6683 - 6684 - return NULL; 6685 - } 6686 - 6687 - mz_zip_zero_struct(&zip_archive); 6688 - if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 6689 - 0)) { 6690 - if (pErr) *pErr = zip_archive.m_last_error; 6691 - 6692 - return NULL; 6693 - } 6694 - 6695 - if (mz_zip_reader_locate_file_v2(&zip_archive, pArchive_name, pComment, flags, &file_index)) { 6696 - p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags); 6697 - } 6698 - 6699 - mz_zip_reader_end_internal(&zip_archive, p != NULL); 6700 - 6701 - if (pErr) *pErr = zip_archive.m_last_error; 6702 - 6703 - return p; 6704 - } 6705 - 6706 - void* mz_zip_extract_archive_file_to_heap(const char* pZip_filename, const char* pArchive_name, size_t* pSize, 6707 - mz_uint flags) { 6708 - return mz_zip_extract_archive_file_to_heap_v2(pZip_filename, pArchive_name, NULL, pSize, flags, NULL); 6709 - } 6710 - 6711 - #endif /* #ifndef MINIZ_NO_STDIO */ 6712 - 6713 - #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */ 6714 - 6715 - /* ------------------- Misc utils */ 6716 - 6717 - mz_zip_mode mz_zip_get_mode(mz_zip_archive* pZip) { return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID; } 6718 - 6719 - mz_zip_type mz_zip_get_type(mz_zip_archive* pZip) { return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID; } 6720 - 6721 - mz_zip_error mz_zip_set_last_error(mz_zip_archive* pZip, mz_zip_error err_num) { 6722 - mz_zip_error prev_err; 6723 - 6724 - if (!pZip) return MZ_ZIP_INVALID_PARAMETER; 6725 - 6726 - prev_err = pZip->m_last_error; 6727 - 6728 - pZip->m_last_error = err_num; 6729 - return prev_err; 6730 - } 6731 - 6732 - mz_zip_error mz_zip_peek_last_error(mz_zip_archive* pZip) { 6733 - if (!pZip) return MZ_ZIP_INVALID_PARAMETER; 6734 - 6735 - return pZip->m_last_error; 6736 - } 6737 - 6738 - mz_zip_error mz_zip_clear_last_error(mz_zip_archive* pZip) { return mz_zip_set_last_error(pZip, MZ_ZIP_NO_ERROR); } 6739 - 6740 - mz_zip_error mz_zip_get_last_error(mz_zip_archive* pZip) { 6741 - mz_zip_error prev_err; 6742 - 6743 - if (!pZip) return MZ_ZIP_INVALID_PARAMETER; 6744 - 6745 - prev_err = pZip->m_last_error; 6746 - 6747 - pZip->m_last_error = MZ_ZIP_NO_ERROR; 6748 - return prev_err; 6749 - } 6750 - 6751 - const char* mz_zip_get_error_string(mz_zip_error mz_err) { 6752 - switch (mz_err) { 6753 - case MZ_ZIP_NO_ERROR: 6754 - return "no error"; 6755 - case MZ_ZIP_UNDEFINED_ERROR: 6756 - return "undefined error"; 6757 - case MZ_ZIP_TOO_MANY_FILES: 6758 - return "too many files"; 6759 - case MZ_ZIP_FILE_TOO_LARGE: 6760 - return "file too large"; 6761 - case MZ_ZIP_UNSUPPORTED_METHOD: 6762 - return "unsupported method"; 6763 - case MZ_ZIP_UNSUPPORTED_ENCRYPTION: 6764 - return "unsupported encryption"; 6765 - case MZ_ZIP_UNSUPPORTED_FEATURE: 6766 - return "unsupported feature"; 6767 - case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR: 6768 - return "failed finding central directory"; 6769 - case MZ_ZIP_NOT_AN_ARCHIVE: 6770 - return "not a ZIP archive"; 6771 - case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED: 6772 - return "invalid header or archive is corrupted"; 6773 - case MZ_ZIP_UNSUPPORTED_MULTIDISK: 6774 - return "unsupported multidisk archive"; 6775 - case MZ_ZIP_DECOMPRESSION_FAILED: 6776 - return "decompression failed or archive is corrupted"; 6777 - case MZ_ZIP_COMPRESSION_FAILED: 6778 - return "compression failed"; 6779 - case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE: 6780 - return "unexpected decompressed size"; 6781 - case MZ_ZIP_CRC_CHECK_FAILED: 6782 - return "CRC-32 check failed"; 6783 - case MZ_ZIP_UNSUPPORTED_CDIR_SIZE: 6784 - return "unsupported central directory size"; 6785 - case MZ_ZIP_ALLOC_FAILED: 6786 - return "allocation failed"; 6787 - case MZ_ZIP_FILE_OPEN_FAILED: 6788 - return "file open failed"; 6789 - case MZ_ZIP_FILE_CREATE_FAILED: 6790 - return "file create failed"; 6791 - case MZ_ZIP_FILE_WRITE_FAILED: 6792 - return "file write failed"; 6793 - case MZ_ZIP_FILE_READ_FAILED: 6794 - return "file read failed"; 6795 - case MZ_ZIP_FILE_CLOSE_FAILED: 6796 - return "file close failed"; 6797 - case MZ_ZIP_FILE_SEEK_FAILED: 6798 - return "file seek failed"; 6799 - case MZ_ZIP_FILE_STAT_FAILED: 6800 - return "file stat failed"; 6801 - case MZ_ZIP_INVALID_PARAMETER: 6802 - return "invalid parameter"; 6803 - case MZ_ZIP_INVALID_FILENAME: 6804 - return "invalid filename"; 6805 - case MZ_ZIP_BUF_TOO_SMALL: 6806 - return "buffer too small"; 6807 - case MZ_ZIP_INTERNAL_ERROR: 6808 - return "internal error"; 6809 - case MZ_ZIP_FILE_NOT_FOUND: 6810 - return "file not found"; 6811 - case MZ_ZIP_ARCHIVE_TOO_LARGE: 6812 - return "archive is too large"; 6813 - case MZ_ZIP_VALIDATION_FAILED: 6814 - return "validation failed"; 6815 - case MZ_ZIP_WRITE_CALLBACK_FAILED: 6816 - return "write calledback failed"; 6817 - default: 6818 - break; 6819 - } 6820 - 6821 - return "unknown error"; 6822 - } 6823 - 6824 - /* Note: Just because the archive is not zip64 doesn't necessarily mean it doesn't have Zip64 extended information extra 6825 - * field, argh. */ 6826 - mz_bool mz_zip_is_zip64(mz_zip_archive* pZip) { 6827 - if ((!pZip) || (!pZip->m_pState)) return MZ_FALSE; 6828 - 6829 - return pZip->m_pState->m_zip64; 6830 - } 6831 - 6832 - size_t mz_zip_get_central_dir_size(mz_zip_archive* pZip) { 6833 - if ((!pZip) || (!pZip->m_pState)) return 0; 6834 - 6835 - return pZip->m_pState->m_central_dir.m_size; 6836 - } 6837 - 6838 - mz_uint mz_zip_reader_get_num_files(mz_zip_archive* pZip) { return pZip ? pZip->m_total_files : 0; } 6839 - 6840 - mz_uint64 mz_zip_get_archive_size(mz_zip_archive* pZip) { 6841 - if (!pZip) return 0; 6842 - return pZip->m_archive_size; 6843 - } 6844 - 6845 - mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive* pZip) { 6846 - if ((!pZip) || (!pZip->m_pState)) return 0; 6847 - return pZip->m_pState->m_file_archive_start_ofs; 6848 - } 6849 - 6850 - MZ_FILE* mz_zip_get_cfile(mz_zip_archive* pZip) { 6851 - if ((!pZip) || (!pZip->m_pState)) return 0; 6852 - return pZip->m_pState->m_pFile; 6853 - } 6854 - 6855 - size_t mz_zip_read_archive_data(mz_zip_archive* pZip, mz_uint64 file_ofs, void* pBuf, size_t n) { 6856 - if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead)) 6857 - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6858 - 6859 - return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n); 6860 - } 6861 - 6862 - mz_uint mz_zip_reader_get_filename(mz_zip_archive* pZip, mz_uint file_index, char* pFilename, 6863 - mz_uint filename_buf_size) { 6864 - mz_uint n; 6865 - const mz_uint8* p = mz_zip_get_cdh(pZip, file_index); 6866 - if (!p) { 6867 - if (filename_buf_size) pFilename[0] = '\0'; 6868 - mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); 6869 - return 0; 6870 - } 6871 - n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS); 6872 - if (filename_buf_size) { 6873 - n = MZ_MIN(n, filename_buf_size - 1); 6874 - memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n); 6875 - pFilename[n] = '\0'; 6876 - } 6877 - return n + 1; 6878 - } 6879 - 6880 - mz_bool mz_zip_reader_file_stat(mz_zip_archive* pZip, mz_uint file_index, mz_zip_archive_file_stat* pStat) { 6881 - return mz_zip_file_stat_internal(pZip, file_index, mz_zip_get_cdh(pZip, file_index), pStat, NULL); 6882 - } 6883 - 6884 - mz_bool mz_zip_end(mz_zip_archive* pZip) { 6885 - if (!pZip) return MZ_FALSE; 6886 - 6887 - if (pZip->m_zip_mode == MZ_ZIP_MODE_READING) return mz_zip_reader_end(pZip); 6888 - #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 6889 - else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED)) 6890 - return mz_zip_writer_end(pZip); 6891 - #endif 6892 - 6893 - return MZ_FALSE; 6894 - } 6895 - 6896 - #ifdef __cplusplus 6897 - } 6898 - #endif 6899 - 6900 - #endif /*#ifndef MINIZ_NO_ARCHIVE_APIS*/
-1462
lib/miniz/miniz.h
··· 1 - #define MINIZ_EXPORT 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 6 - 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). 9 - 10 - * Low-level Deflate/Inflate implementation notes: 11 - 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. 15 - 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 18 - into a memory block large enough to hold the entire file. 19 - 20 - The low-level tdefl/tinfl API's do not make any use of dynamic memory allocation. 21 - 22 - * zlib-style API notes: 23 - 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: 26 - The z_stream struct, optional memory allocation callbacks 27 - deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound 28 - inflateInit/inflateInit2/inflate/inflateReset/inflateEnd 29 - compress, compress2, compressBound, uncompress 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. 32 - 33 - Limitations: 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. 37 - 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. 40 - 41 - * ZIP archive API notes: 42 - 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. 48 - 49 - - Archive reading: Just call this function to read a single file from a disk archive: 50 - 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); 53 - 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. 57 - 58 - - Archives file scanning: The simple way is to use this function to scan a loaded archive for a specific file: 59 - 60 - int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); 61 - 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 64 - directory, so it's not very fast. 65 - 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(). 68 - 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. 72 - 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. 77 - 78 - - Archive appending: The simple way to add a single file to an archive is to call this function: 79 - 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); 82 - 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). 87 - 88 - For more complex archive modification scenarios: 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. 93 - 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. 98 - 99 - - ZIP archive support limitations: 100 - No spanning support. Extraction functions can only handle unencrypted, stored or deflated files. 101 - Requires streams capable of seeking. 102 - 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. 105 - 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 110 - 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). 114 - */ 115 - #pragma once 116 - 117 - /* Defines to completely disable specific portions of miniz.c: 118 - If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl. */ 119 - 120 - /* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O. */ 121 - /*#define MINIZ_NO_STDIO */ 122 - 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. */ 126 - /*#define MINIZ_NO_TIME */ 127 - 128 - /* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */ 129 - /*#define MINIZ_NO_ARCHIVE_APIS */ 130 - 131 - /* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP archive API's. */ 132 - /*#define MINIZ_NO_ARCHIVE_WRITING_APIS */ 133 - 134 - /* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's. */ 135 - /*#define MINIZ_NO_ZLIB_APIS */ 136 - 137 - /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib. */ 138 - /*#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ 139 - 140 - /* Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc. 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. */ 144 - /*#define MINIZ_NO_MALLOC */ 145 - 146 - #if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) 147 - /* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux */ 148 - #define MINIZ_NO_TIME 149 - #endif 150 - 151 - #include <stddef.h> 152 - 153 - #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS) 154 - #include <time.h> 155 - #endif 156 - 157 - #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || \ 158 - defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__) 159 - /* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */ 160 - #define MINIZ_X86_OR_X64_CPU 1 161 - #else 162 - #define MINIZ_X86_OR_X64_CPU 0 163 - #endif 164 - 165 - #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU 166 - /* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */ 167 - #define MINIZ_LITTLE_ENDIAN 1 168 - #else 169 - #define MINIZ_LITTLE_ENDIAN 0 170 - #endif 171 - 172 - /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */ 173 - #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES) 174 - #if MINIZ_X86_OR_X64_CPU 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 178 - #define MINIZ_UNALIGNED_USE_MEMCPY 179 - #else 180 - #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 181 - #endif 182 - #endif 183 - 184 - #if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || \ 185 - defined(__ia64__) || defined(__x86_64__) 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). */ 188 - #define MINIZ_HAS_64BIT_REGISTERS 1 189 - #else 190 - #define MINIZ_HAS_64BIT_REGISTERS 0 191 - #endif 192 - 193 - #ifdef __cplusplus 194 - extern "C" { 195 - #endif 196 - 197 - /* ------------------- zlib-style API Definitions. */ 198 - 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! */ 201 - typedef unsigned long mz_ulong; 202 - 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. */ 205 - MINIZ_EXPORT void mz_free(void* p); 206 - 207 - #define MZ_ADLER32_INIT (1) 208 - /* mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL. */ 209 - MINIZ_EXPORT mz_ulong mz_adler32(mz_ulong adler, const unsigned char* ptr, size_t buf_len); 210 - 211 - #define MZ_CRC32_INIT (0) 212 - /* mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL. */ 213 - MINIZ_EXPORT mz_ulong mz_crc32(mz_ulong crc, const unsigned char* ptr, size_t buf_len); 214 - 215 - /* Compression strategies. */ 216 - enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 }; 217 - 218 - /* Method */ 219 - #define MZ_DEFLATED 8 220 - 221 - /* Heap allocation callbacks. 222 - Note that mz_alloc_func parameter types purposely differ from zlib's: items/size is size_t, not unsigned long. */ 223 - typedef void* (*mz_alloc_func)(void* opaque, size_t items, size_t size); 224 - typedef void (*mz_free_func)(void* opaque, void* address); 225 - typedef void* (*mz_realloc_func)(void* opaque, void* address, size_t items, size_t size); 226 - 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. */ 229 - enum { 230 - MZ_NO_COMPRESSION = 0, 231 - MZ_BEST_SPEED = 1, 232 - MZ_BEST_COMPRESSION = 9, 233 - MZ_UBER_COMPRESSION = 10, 234 - MZ_DEFAULT_LEVEL = 6, 235 - MZ_DEFAULT_COMPRESSION = -1 236 - }; 237 - 238 - #define MZ_VERSION "10.2.0" 239 - #define MZ_VERNUM 0xA100 240 - #define MZ_VER_MAJOR 10 241 - #define MZ_VER_MINOR 2 242 - #define MZ_VER_REVISION 0 243 - #define MZ_VER_SUBREVISION 0 244 - 245 - #ifndef MINIZ_NO_ZLIB_APIS 246 - 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). */ 249 - enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 }; 250 - 251 - /* Return status codes. MZ_PARAM_ERROR is non-standard. */ 252 - enum { 253 - MZ_OK = 0, 254 - MZ_STREAM_END = 1, 255 - MZ_NEED_DICT = 2, 256 - MZ_ERRNO = -1, 257 - MZ_STREAM_ERROR = -2, 258 - MZ_DATA_ERROR = -3, 259 - MZ_MEM_ERROR = -4, 260 - MZ_BUF_ERROR = -5, 261 - MZ_VERSION_ERROR = -6, 262 - MZ_PARAM_ERROR = -10000 263 - }; 264 - 265 - /* Window bits */ 266 - #define MZ_DEFAULT_WINDOW_BITS 15 267 - 268 - struct mz_internal_state; 269 - 270 - /* Compression/decompression stream struct. */ 271 - typedef struct mz_stream_s { 272 - const unsigned char* next_in; /* pointer to next byte to read */ 273 - unsigned int avail_in; /* number of bytes available at next_in */ 274 - mz_ulong total_in; /* total number of bytes consumed so far */ 275 - 276 - unsigned char* next_out; /* pointer to next byte to write */ 277 - unsigned int avail_out; /* number of bytes that can be written to next_out */ 278 - mz_ulong total_out; /* total number of bytes produced so far */ 279 - 280 - char* msg; /* error msg (unused) */ 281 - struct mz_internal_state* state; /* internal state, allocated by zalloc/zfree */ 282 - 283 - mz_alloc_func zalloc; /* optional heap allocation function (defaults to malloc) */ 284 - mz_free_func zfree; /* optional heap free function (defaults to free) */ 285 - void* opaque; /* heap alloc function user pointer */ 286 - 287 - int data_type; /* data_type (unused) */ 288 - mz_ulong adler; /* adler32 of the source or uncompressed data */ 289 - mz_ulong reserved; /* not used */ 290 - } mz_stream; 291 - 292 - typedef mz_stream* mz_streamp; 293 - 294 - /* Returns the version string of miniz.c. */ 295 - MINIZ_EXPORT const char* mz_version(void); 296 - 297 - /* mz_deflateInit() initializes a compressor with default options: */ 298 - /* Parameters: */ 299 - /* pStream must point to an initialized mz_stream struct. */ 300 - /* level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. */ 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.) */ 305 - /* Return values: */ 306 - /* MZ_OK on success. */ 307 - /* MZ_STREAM_ERROR if the stream is bogus. */ 308 - /* MZ_PARAM_ERROR if the input parameters are bogus. */ 309 - /* MZ_MEM_ERROR on out of memory. */ 310 - MINIZ_EXPORT int mz_deflateInit(mz_streamp pStream, int level); 311 - 312 - /* mz_deflateInit2() is like mz_deflate(), except with more control: */ 313 - /* Additional parameters: */ 314 - /* method must be MZ_DEFLATED */ 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) */ 317 - /* mem_level must be between [1, 9] (it's checked but ignored by miniz.c) */ 318 - MINIZ_EXPORT int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, 319 - int strategy); 320 - 321 - /* Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by 322 - * mz_deflateInit()/mz_deflateInit2(). */ 323 - MINIZ_EXPORT int mz_deflateReset(mz_streamp pStream); 324 - 325 - /* mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible. 326 - */ 327 - /* Parameters: */ 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. */ 331 - /* Return values: */ 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. */ 336 - /* MZ_STREAM_ERROR if the stream is bogus. */ 337 - /* MZ_PARAM_ERROR if one of the parameters is invalid. */ 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.) */ 340 - MINIZ_EXPORT int mz_deflate(mz_streamp pStream, int flush); 341 - 342 - /* mz_deflateEnd() deinitializes a compressor: */ 343 - /* Return values: */ 344 - /* MZ_OK on success. */ 345 - /* MZ_STREAM_ERROR if the stream is bogus. */ 346 - MINIZ_EXPORT int mz_deflateEnd(mz_streamp pStream); 347 - 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. */ 350 - MINIZ_EXPORT mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len); 351 - 352 - /* Single-call compression functions mz_compress() and mz_compress2(): */ 353 - /* Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure. */ 354 - MINIZ_EXPORT int mz_compress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 355 - mz_ulong source_len); 356 - MINIZ_EXPORT int mz_compress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 357 - mz_ulong source_len, int level); 358 - 359 - /* mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling 360 - * mz_compress(). */ 361 - MINIZ_EXPORT mz_ulong mz_compressBound(mz_ulong source_len); 362 - 363 - /* Initializes a decompressor. */ 364 - MINIZ_EXPORT int mz_inflateInit(mz_streamp pStream); 365 - 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). */ 369 - MINIZ_EXPORT int mz_inflateInit2(mz_streamp pStream, int window_bits); 370 - 371 - /* Quickly resets a compressor without having to reallocate anything. Same as calling mz_inflateEnd() followed by 372 - * mz_inflateInit()/mz_inflateInit2(). */ 373 - MINIZ_EXPORT int mz_inflateReset(mz_streamp pStream); 374 - 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. */ 377 - /* Parameters: */ 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. */ 380 - /* flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. */ 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. */ 385 - /* Return values: */ 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. */ 390 - /* MZ_STREAM_ERROR if the stream is bogus. */ 391 - /* MZ_DATA_ERROR if the deflate stream is invalid. */ 392 - /* MZ_PARAM_ERROR if one of the parameters is invalid. */ 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). */ 397 - MINIZ_EXPORT int mz_inflate(mz_streamp pStream, int flush); 398 - 399 - /* Deinitializes a decompressor. */ 400 - MINIZ_EXPORT int mz_inflateEnd(mz_streamp pStream); 401 - 402 - /* Single-call decompression. */ 403 - /* Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. */ 404 - MINIZ_EXPORT int mz_uncompress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 405 - mz_ulong source_len); 406 - MINIZ_EXPORT int mz_uncompress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, 407 - mz_ulong* pSource_len); 408 - 409 - /* Returns a string description of the specified error code, or NULL if the error code is invalid. */ 410 - MINIZ_EXPORT const char* mz_error(int err); 411 - 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. */ 415 - #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES 416 - typedef unsigned char Byte; 417 - typedef unsigned int uInt; 418 - typedef mz_ulong uLong; 419 - typedef Byte Bytef; 420 - typedef uInt uIntf; 421 - typedef char charf; 422 - typedef int intf; 423 - typedef void* voidpf; 424 - typedef uLong uLongf; 425 - typedef void* voidp; 426 - typedef void* const voidpc; 427 - #define Z_NULL 0 428 - #define Z_NO_FLUSH MZ_NO_FLUSH 429 - #define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH 430 - #define Z_SYNC_FLUSH MZ_SYNC_FLUSH 431 - #define Z_FULL_FLUSH MZ_FULL_FLUSH 432 - #define Z_FINISH MZ_FINISH 433 - #define Z_BLOCK MZ_BLOCK 434 - #define Z_OK MZ_OK 435 - #define Z_STREAM_END MZ_STREAM_END 436 - #define Z_NEED_DICT MZ_NEED_DICT 437 - #define Z_ERRNO MZ_ERRNO 438 - #define Z_STREAM_ERROR MZ_STREAM_ERROR 439 - #define Z_DATA_ERROR MZ_DATA_ERROR 440 - #define Z_MEM_ERROR MZ_MEM_ERROR 441 - #define Z_BUF_ERROR MZ_BUF_ERROR 442 - #define Z_VERSION_ERROR MZ_VERSION_ERROR 443 - #define Z_PARAM_ERROR MZ_PARAM_ERROR 444 - #define Z_NO_COMPRESSION MZ_NO_COMPRESSION 445 - #define Z_BEST_SPEED MZ_BEST_SPEED 446 - #define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION 447 - #define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION 448 - #define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY 449 - #define Z_FILTERED MZ_FILTERED 450 - #define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY 451 - #define Z_RLE MZ_RLE 452 - #define Z_FIXED MZ_FIXED 453 - #define Z_DEFLATED MZ_DEFLATED 454 - #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS 455 - #define alloc_func mz_alloc_func 456 - #define free_func mz_free_func 457 - #define internal_state mz_internal_state 458 - #define z_stream mz_stream 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 477 - #define MAX_WBITS 15 478 - #define MAX_MEM_LEVEL 9 479 - #define zError mz_error 480 - #define ZLIB_VERSION MZ_VERSION 481 - #define ZLIB_VERNUM MZ_VERNUM 482 - #define ZLIB_VER_MAJOR MZ_VER_MAJOR 483 - #define ZLIB_VER_MINOR MZ_VER_MINOR 484 - #define ZLIB_VER_REVISION MZ_VER_REVISION 485 - #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION 486 - #define zlibVersion mz_version 487 - #define zlib_version mz_version() 488 - #endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ 489 - 490 - #endif /* MINIZ_NO_ZLIB_APIS */ 491 - 492 - #ifdef __cplusplus 493 - } 494 - #endif 495 - 496 - #pragma once 497 - #include <assert.h> 498 - #include <stdint.h> 499 - #include <stdlib.h> 500 - #include <string.h> 501 - 502 - /* ------------------- Types and macros */ 503 - typedef unsigned char mz_uint8; 504 - typedef signed short mz_int16; 505 - typedef unsigned short mz_uint16; 506 - typedef unsigned int mz_uint32; 507 - typedef unsigned int mz_uint; 508 - typedef int64_t mz_int64; 509 - typedef uint64_t mz_uint64; 510 - typedef int mz_bool; 511 - 512 - #define MZ_FALSE (0) 513 - #define MZ_TRUE (1) 514 - 515 - /* Works around MSVC's spammy "warning C4127: conditional expression is constant" message. */ 516 - #ifdef _MSC_VER 517 - #define MZ_MACRO_END while (0, 0) 518 - #else 519 - #define MZ_MACRO_END while (0) 520 - #endif 521 - 522 - #ifdef MINIZ_NO_STDIO 523 - #define MZ_FILE void* 524 - #else 525 - #include <stdio.h> 526 - #define MZ_FILE FILE 527 - #endif /* #ifdef MINIZ_NO_STDIO */ 528 - 529 - #ifdef MINIZ_NO_TIME 530 - typedef struct mz_dummy_time_t_tag { 531 - int m_dummy; 532 - } mz_dummy_time_t; 533 - #define MZ_TIME_T mz_dummy_time_t 534 - #else 535 - #define MZ_TIME_T time_t 536 - #endif 537 - 538 - #define MZ_ASSERT(x) assert(x) 539 - 540 - #ifdef MINIZ_NO_MALLOC 541 - #define MZ_MALLOC(x) NULL 542 - #define MZ_FREE(x) (void)x, ((void)0) 543 - #define MZ_REALLOC(p, x) NULL 544 - #else 545 - #define MZ_MALLOC(x) malloc(x) 546 - #define MZ_FREE(x) free(x) 547 - #define MZ_REALLOC(p, x) realloc(p, x) 548 - #endif 549 - 550 - #define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) 551 - #define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) 552 - #define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) 553 - 554 - #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN 555 - #define MZ_READ_LE16(p) *((const mz_uint16*)(p)) 556 - #define MZ_READ_LE32(p) *((const mz_uint32*)(p)) 557 - #else 558 - #define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8*)(p))[0]) | ((mz_uint32)(((const mz_uint8*)(p))[1]) << 8U)) 559 - #define MZ_READ_LE32(p) \ 560 - ((mz_uint32)(((const mz_uint8*)(p))[0]) | ((mz_uint32)(((const mz_uint8*)(p))[1]) << 8U) | \ 561 - ((mz_uint32)(((const mz_uint8*)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8*)(p))[3]) << 24U)) 562 - #endif 563 - 564 - #define MZ_READ_LE64(p) \ 565 - (((mz_uint64)MZ_READ_LE32(p)) | (((mz_uint64)MZ_READ_LE32((const mz_uint8*)(p) + sizeof(mz_uint32))) << 32U)) 566 - 567 - #ifdef _MSC_VER 568 - #define MZ_FORCEINLINE __forceinline 569 - #elif defined(__GNUC__) 570 - #define MZ_FORCEINLINE __inline__ __attribute__((__always_inline__)) 571 - #else 572 - #define MZ_FORCEINLINE inline 573 - #endif 574 - 575 - #ifdef __cplusplus 576 - extern "C" { 577 - #endif 578 - 579 - extern MINIZ_EXPORT void* miniz_def_alloc_func(void* opaque, size_t items, size_t size); 580 - extern MINIZ_EXPORT void miniz_def_free_func(void* opaque, void* address); 581 - extern MINIZ_EXPORT void* miniz_def_realloc_func(void* opaque, void* address, size_t items, size_t size); 582 - 583 - #define MZ_UINT16_MAX (0xFFFFU) 584 - #define MZ_UINT32_MAX (0xFFFFFFFFU) 585 - 586 - #ifdef __cplusplus 587 - } 588 - #endif 589 - #pragma once 590 - 591 - #ifdef __cplusplus 592 - extern "C" { 593 - #endif 594 - /* ------------------- Low-level Compression API Definitions */ 595 - 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). */ 598 - #define TDEFL_LESS_MEMORY 0 599 - 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). */ 604 - enum { TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF }; 605 - 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) */ 613 - /* TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. */ 614 - /* TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. */ 615 - /* TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. */ 616 - /* The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK). 617 - */ 618 - enum { 619 - TDEFL_WRITE_ZLIB_HEADER = 0x01000, 620 - TDEFL_COMPUTE_ADLER32 = 0x02000, 621 - TDEFL_GREEDY_PARSING_FLAG = 0x04000, 622 - TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000, 623 - TDEFL_RLE_MATCHES = 0x10000, 624 - TDEFL_FILTER_MATCHES = 0x20000, 625 - TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000, 626 - TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000 627 - }; 628 - 629 - /* High level compression functions: */ 630 - /* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc(). */ 631 - /* On entry: */ 632 - /* pSrc_buf, src_buf_len: Pointer and size of source block to compress. */ 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. */ 635 - /* On return: */ 636 - /* Function returns a pointer to the compressed data, or NULL on failure. */ 637 - /* *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. 638 - */ 639 - /* The caller must free() the returned block when it's no longer needed. */ 640 - MINIZ_EXPORT void* tdefl_compress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags); 641 - 642 - /* tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. */ 643 - /* Returns 0 on failure. */ 644 - MINIZ_EXPORT size_t tdefl_compress_mem_to_mem(void* pOut_buf, size_t out_buf_len, const void* pSrc_buf, 645 - size_t src_buf_len, int flags); 646 - 647 - /* Compresses an image to a compressed PNG file in memory. */ 648 - /* On entry: */ 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). */ 655 - /* On return: */ 656 - /* Function returns a pointer to the compressed data, or NULL on failure. */ 657 - /* *pLen_out will be set to the size of the PNG image file. */ 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. */ 660 - MINIZ_EXPORT void* tdefl_write_image_to_png_file_in_memory_ex(const void* pImage, int w, int h, int num_chans, 661 - size_t* pLen_out, mz_uint level, mz_bool flip); 662 - MINIZ_EXPORT void* tdefl_write_image_to_png_file_in_memory(const void* pImage, int w, int h, int num_chans, 663 - size_t* pLen_out); 664 - 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. */ 667 - typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void* pUser); 668 - 669 - /* tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function 670 - * internally. */ 671 - MINIZ_EXPORT mz_bool tdefl_compress_mem_to_output(const void* pBuf, size_t buf_len, 672 - tdefl_put_buf_func_ptr pPut_buf_func, void* pPut_buf_user, int flags); 673 - 674 - enum { 675 - TDEFL_MAX_HUFF_TABLES = 3, 676 - TDEFL_MAX_HUFF_SYMBOLS_0 = 288, 677 - TDEFL_MAX_HUFF_SYMBOLS_1 = 32, 678 - TDEFL_MAX_HUFF_SYMBOLS_2 = 19, 679 - TDEFL_LZ_DICT_SIZE = 32768, 680 - TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, 681 - TDEFL_MIN_MATCH_LEN = 3, 682 - TDEFL_MAX_MATCH_LEN = 258 683 - }; 684 - 685 - /* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman 686 - * codes). */ 687 - #if TDEFL_LESS_MEMORY 688 - enum { 689 - TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, 690 - TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, 691 - TDEFL_MAX_HUFF_SYMBOLS = 288, 692 - TDEFL_LZ_HASH_BITS = 12, 693 - TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, 694 - TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, 695 - TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS 696 - }; 697 - #else 698 - enum { 699 - TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, 700 - TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, 701 - TDEFL_MAX_HUFF_SYMBOLS = 288, 702 - TDEFL_LZ_HASH_BITS = 15, 703 - TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, 704 - TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, 705 - TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS 706 - }; 707 - #endif 708 - 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. */ 711 - typedef enum { 712 - TDEFL_STATUS_BAD_PARAM = -2, 713 - TDEFL_STATUS_PUT_BUF_FAILED = -1, 714 - TDEFL_STATUS_OKAY = 0, 715 - TDEFL_STATUS_DONE = 1 716 - } tdefl_status; 717 - 718 - /* Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums */ 719 - typedef enum { TDEFL_NO_FLUSH = 0, TDEFL_SYNC_FLUSH = 2, TDEFL_FULL_FLUSH = 3, TDEFL_FINISH = 4 } tdefl_flush; 720 - 721 - /* tdefl's compression state structure. */ 722 - typedef struct { 723 - tdefl_put_buf_func_ptr m_pPut_buf_func; 724 - void* m_pPut_buf_user; 725 - mz_uint m_flags, m_max_probes[2]; 726 - int m_greedy_parsing; 727 - mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size; 728 - mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end; 729 - mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer; 730 - mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, 731 - m_block_index, m_wants_to_finish; 732 - tdefl_status m_prev_return_status; 733 - const void* m_pIn_buf; 734 - void* m_pOut_buf; 735 - size_t *m_pIn_buf_size, *m_pOut_buf_size; 736 - tdefl_flush m_flush; 737 - const mz_uint8* m_pSrc; 738 - size_t m_src_buf_left, m_out_buf_ofs; 739 - mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1]; 740 - mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; 741 - mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; 742 - mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; 743 - mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE]; 744 - mz_uint16 m_next[TDEFL_LZ_DICT_SIZE]; 745 - mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE]; 746 - mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE]; 747 - } tdefl_compressor; 748 - 749 - /* Initializes the compressor. */ 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.) */ 755 - MINIZ_EXPORT tdefl_status tdefl_init(tdefl_compressor* d, tdefl_put_buf_func_ptr pPut_buf_func, void* pPut_buf_user, 756 - int flags); 757 - 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. */ 760 - MINIZ_EXPORT tdefl_status tdefl_compress(tdefl_compressor* d, const void* pIn_buf, size_t* pIn_buf_size, void* pOut_buf, 761 - size_t* pOut_buf_size, tdefl_flush flush); 762 - 763 - /* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr. */ 764 - /* tdefl_compress_buffer() always consumes the entire input buffer. */ 765 - MINIZ_EXPORT tdefl_status tdefl_compress_buffer(tdefl_compressor* d, const void* pIn_buf, size_t in_buf_size, 766 - tdefl_flush flush); 767 - 768 - MINIZ_EXPORT tdefl_status tdefl_get_prev_return_status(tdefl_compressor* d); 769 - MINIZ_EXPORT mz_uint32 tdefl_get_adler32(tdefl_compressor* d); 770 - 771 - /* Create tdefl_compress() flags given zlib-style compression parameters. */ 772 - /* level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files) */ 773 - /* window_bits may be -15 (raw deflate) or 15 (zlib) */ 774 - /* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED */ 775 - MINIZ_EXPORT mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy); 776 - 777 - #ifndef MINIZ_NO_MALLOC 778 - /* Allocate the tdefl_compressor structure in C so that */ 779 - /* non-C language bindings to tdefl_ API don't need to worry about */ 780 - /* structure size and allocation mechanism. */ 781 - MINIZ_EXPORT tdefl_compressor* tdefl_compressor_alloc(void); 782 - MINIZ_EXPORT void tdefl_compressor_free(tdefl_compressor* pComp); 783 - #endif 784 - 785 - #ifdef __cplusplus 786 - } 787 - #endif 788 - #pragma once 789 - 790 - /* ------------------- Low-level Decompression API Definitions */ 791 - 792 - #ifdef __cplusplus 793 - extern "C" { 794 - #endif 795 - /* Decompression flags used by tinfl_decompress(). */ 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. */ 803 - enum { 804 - TINFL_FLAG_PARSE_ZLIB_HEADER = 1, 805 - TINFL_FLAG_HAS_MORE_INPUT = 2, 806 - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, 807 - TINFL_FLAG_COMPUTE_ADLER32 = 8 808 - }; 809 - 810 - /* High level decompression functions: */ 811 - /* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). */ 812 - /* On entry: */ 813 - /* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. */ 814 - /* On return: */ 815 - /* Function returns a pointer to the decompressed data, or NULL on failure. */ 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. */ 819 - MINIZ_EXPORT void* tinfl_decompress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags); 820 - 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. */ 823 - #define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) 824 - MINIZ_EXPORT size_t tinfl_decompress_mem_to_mem(void* pOut_buf, size_t out_buf_len, const void* pSrc_buf, 825 - size_t src_buf_len, int flags); 826 - 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. */ 829 - /* Returns 1 on success or 0 on failure. */ 830 - typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void* pUser); 831 - MINIZ_EXPORT int tinfl_decompress_mem_to_callback(const void* pIn_buf, size_t* pIn_buf_size, 832 - tinfl_put_buf_func_ptr pPut_buf_func, void* pPut_buf_user, int flags); 833 - 834 - struct tinfl_decompressor_tag; 835 - typedef struct tinfl_decompressor_tag tinfl_decompressor; 836 - 837 - #ifndef MINIZ_NO_MALLOC 838 - /* Allocate the tinfl_decompressor structure in C so that */ 839 - /* non-C language bindings to tinfl_ API don't need to worry about */ 840 - /* structure size and allocation mechanism. */ 841 - MINIZ_EXPORT tinfl_decompressor* tinfl_decompressor_alloc(void); 842 - MINIZ_EXPORT void tinfl_decompressor_free(tinfl_decompressor* pDecomp); 843 - #endif 844 - 845 - /* Max size of LZ dictionary. */ 846 - #define TINFL_LZ_DICT_SIZE 32768 847 - 848 - /* Return status. */ 849 - typedef enum { 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. */ 855 - TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4, 856 - 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.) */ 859 - TINFL_STATUS_BAD_PARAM = -3, 860 - 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. */ 863 - TINFL_STATUS_ADLER32_MISMATCH = -2, 864 - 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. */ 867 - TINFL_STATUS_FAILED = -1, 868 - 869 - /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */ 870 - 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. */ 875 - TINFL_STATUS_DONE = 0, 876 - 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 */ 881 - /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */ 882 - TINFL_STATUS_NEEDS_MORE_INPUT = 1, 883 - 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 */ 890 - /* so I may need to add some code to address this. */ 891 - TINFL_STATUS_HAS_MORE_OUTPUT = 2 892 - } tinfl_status; 893 - 894 - /* Initializes the decompressor to its initial state. */ 895 - #define tinfl_init(r) \ 896 - do { \ 897 - (r)->m_state = 0; \ 898 - } \ 899 - MZ_MACRO_END 900 - #define tinfl_get_adler32(r) (r)->m_check_adler32 901 - 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. */ 906 - MINIZ_EXPORT tinfl_status tinfl_decompress(tinfl_decompressor* r, const mz_uint8* pIn_buf_next, size_t* pIn_buf_size, 907 - mz_uint8* pOut_buf_start, mz_uint8* pOut_buf_next, size_t* pOut_buf_size, 908 - const mz_uint32 decomp_flags); 909 - 910 - /* Internal/private bits follow. */ 911 - enum { 912 - TINFL_MAX_HUFF_TABLES = 3, 913 - TINFL_MAX_HUFF_SYMBOLS_0 = 288, 914 - TINFL_MAX_HUFF_SYMBOLS_1 = 32, 915 - TINFL_MAX_HUFF_SYMBOLS_2 = 19, 916 - TINFL_FAST_LOOKUP_BITS = 10, 917 - TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS 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; 924 - 925 - #if MINIZ_HAS_64BIT_REGISTERS 926 - #define TINFL_USE_64BIT_BITBUF 1 927 - #else 928 - #define TINFL_USE_64BIT_BITBUF 0 929 - #endif 930 - 931 - #if TINFL_USE_64BIT_BITBUF 932 - typedef mz_uint64 tinfl_bit_buf_t; 933 - #define TINFL_BITBUF_SIZE (64) 934 - #else 935 - typedef mz_uint32 tinfl_bit_buf_t; 936 - #define TINFL_BITBUF_SIZE (32) 937 - #endif 938 - 939 - struct tinfl_decompressor_tag { 940 - mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, 941 - m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES]; 942 - tinfl_bit_buf_t m_bit_buf; 943 - size_t m_dist_from_out_buf_start; 944 - tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; 945 - mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; 946 - }; 947 - 948 - #ifdef __cplusplus 949 - } 950 - #endif 951 - 952 - #pragma once 953 - 954 - /* ------------------- ZIP archive reading/writing */ 955 - 956 - #ifndef MINIZ_NO_ARCHIVE_APIS 957 - 958 - #ifdef __cplusplus 959 - extern "C" { 960 - #endif 961 - 962 - enum { 963 - /* Note: These enums can be reduced as needed to save memory or stack space - they are pretty conservative. */ 964 - MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024, 965 - MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512, 966 - MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512 967 - }; 968 - 969 - typedef struct { 970 - /* Central directory file index. */ 971 - mz_uint32 m_file_index; 972 - 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. */ 975 - mz_uint64 m_central_dir_ofs; 976 - 977 - /* These fields are copied directly from the zip's central dir. */ 978 - mz_uint16 m_version_made_by; 979 - mz_uint16 m_version_needed; 980 - mz_uint16 m_bit_flag; 981 - mz_uint16 m_method; 982 - 983 - #ifndef MINIZ_NO_TIME 984 - MZ_TIME_T m_time; 985 - #endif 986 - 987 - /* CRC-32 of uncompressed data. */ 988 - mz_uint32 m_crc32; 989 - 990 - /* File's compressed size. */ 991 - mz_uint64 m_comp_size; 992 - 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. */ 995 - mz_uint64 m_uncomp_size; 996 - 997 - /* Zip internal and external file attributes. */ 998 - mz_uint16 m_internal_attr; 999 - mz_uint32 m_external_attr; 1000 - 1001 - /* Entry's local header file offset in bytes. */ 1002 - mz_uint64 m_local_header_ofs; 1003 - 1004 - /* Size of comment in bytes. */ 1005 - mz_uint32 m_comment_size; 1006 - 1007 - /* MZ_TRUE if the entry appears to be a directory. */ 1008 - mz_bool m_is_directory; 1009 - 1010 - /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip doesn't support) */ 1011 - mz_bool m_is_encrypted; 1012 - 1013 - /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a compression method we support. */ 1014 - mz_bool m_is_supported; 1015 - 1016 - /* Filename. If string ends in '/' it's a subdirectory entry. */ 1017 - /* Guaranteed to be zero terminated, may be truncated to fit. */ 1018 - char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE]; 1019 - 1020 - /* Comment field. */ 1021 - /* Guaranteed to be zero terminated, may be truncated to fit. */ 1022 - char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE]; 1023 - 1024 - } mz_zip_archive_file_stat; 1025 - 1026 - typedef size_t (*mz_file_read_func)(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n); 1027 - typedef size_t (*mz_file_write_func)(void* pOpaque, mz_uint64 file_ofs, const void* pBuf, size_t n); 1028 - typedef mz_bool (*mz_file_needs_keepalive)(void* pOpaque); 1029 - 1030 - struct mz_zip_internal_state_tag; 1031 - typedef struct mz_zip_internal_state_tag mz_zip_internal_state; 1032 - 1033 - typedef enum { 1034 - MZ_ZIP_MODE_INVALID = 0, 1035 - MZ_ZIP_MODE_READING = 1, 1036 - MZ_ZIP_MODE_WRITING = 2, 1037 - MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3 1038 - } mz_zip_mode; 1039 - 1040 - typedef enum { 1041 - MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100, 1042 - MZ_ZIP_FLAG_IGNORE_PATH = 0x0200, 1043 - MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400, 1044 - MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800, 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 */ 1052 - MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000, 1053 - MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000, 1054 - /*After adding a compressed file, seek back 1055 - to local file header and set the correct sizes*/ 1056 - MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000 1057 - } mz_zip_flags; 1058 - 1059 - typedef enum { 1060 - MZ_ZIP_TYPE_INVALID = 0, 1061 - MZ_ZIP_TYPE_USER, 1062 - MZ_ZIP_TYPE_MEMORY, 1063 - MZ_ZIP_TYPE_HEAP, 1064 - MZ_ZIP_TYPE_FILE, 1065 - MZ_ZIP_TYPE_CFILE, 1066 - MZ_ZIP_TOTAL_TYPES 1067 - } mz_zip_type; 1068 - 1069 - /* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or modify this enum. */ 1070 - typedef enum { 1071 - MZ_ZIP_NO_ERROR = 0, 1072 - MZ_ZIP_UNDEFINED_ERROR, 1073 - MZ_ZIP_TOO_MANY_FILES, 1074 - MZ_ZIP_FILE_TOO_LARGE, 1075 - MZ_ZIP_UNSUPPORTED_METHOD, 1076 - MZ_ZIP_UNSUPPORTED_ENCRYPTION, 1077 - MZ_ZIP_UNSUPPORTED_FEATURE, 1078 - MZ_ZIP_FAILED_FINDING_CENTRAL_DIR, 1079 - MZ_ZIP_NOT_AN_ARCHIVE, 1080 - MZ_ZIP_INVALID_HEADER_OR_CORRUPTED, 1081 - MZ_ZIP_UNSUPPORTED_MULTIDISK, 1082 - MZ_ZIP_DECOMPRESSION_FAILED, 1083 - MZ_ZIP_COMPRESSION_FAILED, 1084 - MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE, 1085 - MZ_ZIP_CRC_CHECK_FAILED, 1086 - MZ_ZIP_UNSUPPORTED_CDIR_SIZE, 1087 - MZ_ZIP_ALLOC_FAILED, 1088 - MZ_ZIP_FILE_OPEN_FAILED, 1089 - MZ_ZIP_FILE_CREATE_FAILED, 1090 - MZ_ZIP_FILE_WRITE_FAILED, 1091 - MZ_ZIP_FILE_READ_FAILED, 1092 - MZ_ZIP_FILE_CLOSE_FAILED, 1093 - MZ_ZIP_FILE_SEEK_FAILED, 1094 - MZ_ZIP_FILE_STAT_FAILED, 1095 - MZ_ZIP_INVALID_PARAMETER, 1096 - MZ_ZIP_INVALID_FILENAME, 1097 - MZ_ZIP_BUF_TOO_SMALL, 1098 - MZ_ZIP_INTERNAL_ERROR, 1099 - MZ_ZIP_FILE_NOT_FOUND, 1100 - MZ_ZIP_ARCHIVE_TOO_LARGE, 1101 - MZ_ZIP_VALIDATION_FAILED, 1102 - MZ_ZIP_WRITE_CALLBACK_FAILED, 1103 - MZ_ZIP_TOTAL_ERRORS 1104 - } mz_zip_error; 1105 - 1106 - typedef struct { 1107 - mz_uint64 m_archive_size; 1108 - mz_uint64 m_central_directory_file_ofs; 1109 - 1110 - /* We only support up to UINT32_MAX files in zip64 mode. */ 1111 - mz_uint32 m_total_files; 1112 - mz_zip_mode m_zip_mode; 1113 - mz_zip_type m_zip_type; 1114 - mz_zip_error m_last_error; 1115 - 1116 - mz_uint64 m_file_offset_alignment; 1117 - 1118 - mz_alloc_func m_pAlloc; 1119 - mz_free_func m_pFree; 1120 - mz_realloc_func m_pRealloc; 1121 - void* m_pAlloc_opaque; 1122 - 1123 - mz_file_read_func m_pRead; 1124 - mz_file_write_func m_pWrite; 1125 - mz_file_needs_keepalive m_pNeeds_keepalive; 1126 - void* m_pIO_opaque; 1127 - 1128 - mz_zip_internal_state* m_pState; 1129 - 1130 - } mz_zip_archive; 1131 - 1132 - typedef struct { 1133 - mz_zip_archive* pZip; 1134 - mz_uint flags; 1135 - 1136 - int status; 1137 - #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS 1138 - mz_uint file_crc32; 1139 - #endif 1140 - mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining, out_buf_ofs, cur_file_ofs; 1141 - mz_zip_archive_file_stat file_stat; 1142 - void* pRead_buf; 1143 - void* pWrite_buf; 1144 - 1145 - size_t out_blk_remain; 1146 - 1147 - tinfl_decompressor inflator; 1148 - 1149 - } mz_zip_reader_extract_iter_state; 1150 - 1151 - /* -------- ZIP reading */ 1152 - 1153 - /* Inits a ZIP archive reader. */ 1154 - /* These functions read and validate the archive's central directory. */ 1155 - MINIZ_EXPORT mz_bool mz_zip_reader_init(mz_zip_archive* pZip, mz_uint64 size, mz_uint flags); 1156 - 1157 - MINIZ_EXPORT mz_bool mz_zip_reader_init_mem(mz_zip_archive* pZip, const void* pMem, size_t size, mz_uint flags); 1158 - 1159 - #ifndef MINIZ_NO_STDIO 1160 - /* Read a archive from a disk file. */ 1161 - /* file_start_ofs is the file offset where the archive actually begins, or 0. */ 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. */ 1164 - MINIZ_EXPORT mz_bool mz_zip_reader_init_file(mz_zip_archive* pZip, const char* pFilename, mz_uint32 flags); 1165 - MINIZ_EXPORT mz_bool mz_zip_reader_init_file_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint flags, 1166 - mz_uint64 file_start_ofs, mz_uint64 archive_size); 1167 - 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. */ 1171 - /* The FILE will NOT be closed when mz_zip_reader_end() is called. */ 1172 - MINIZ_EXPORT mz_bool mz_zip_reader_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint64 archive_size, 1173 - mz_uint flags); 1174 - #endif 1175 - 1176 - /* Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was 1177 - * used. */ 1178 - MINIZ_EXPORT mz_bool mz_zip_reader_end(mz_zip_archive* pZip); 1179 - 1180 - /* -------- ZIP reading or writing */ 1181 - 1182 - /* Clears a mz_zip_archive struct to all zeros. */ 1183 - /* Important: This must be done before passing the struct to any mz_zip functions. */ 1184 - MINIZ_EXPORT void mz_zip_zero_struct(mz_zip_archive* pZip); 1185 - 1186 - MINIZ_EXPORT mz_zip_mode mz_zip_get_mode(mz_zip_archive* pZip); 1187 - MINIZ_EXPORT mz_zip_type mz_zip_get_type(mz_zip_archive* pZip); 1188 - 1189 - /* Returns the total number of files in the archive. */ 1190 - MINIZ_EXPORT mz_uint mz_zip_reader_get_num_files(mz_zip_archive* pZip); 1191 - 1192 - MINIZ_EXPORT mz_uint64 mz_zip_get_archive_size(mz_zip_archive* pZip); 1193 - MINIZ_EXPORT mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive* pZip); 1194 - MINIZ_EXPORT MZ_FILE* mz_zip_get_cfile(mz_zip_archive* pZip); 1195 - 1196 - /* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf. */ 1197 - MINIZ_EXPORT size_t mz_zip_read_archive_data(mz_zip_archive* pZip, mz_uint64 file_ofs, void* pBuf, size_t n); 1198 - 1199 - /* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct. These functions retrieve/manipulate this 1200 - * field. */ 1201 - /* Note that the m_last_error functionality is not thread safe. */ 1202 - MINIZ_EXPORT mz_zip_error mz_zip_set_last_error(mz_zip_archive* pZip, mz_zip_error err_num); 1203 - MINIZ_EXPORT mz_zip_error mz_zip_peek_last_error(mz_zip_archive* pZip); 1204 - MINIZ_EXPORT mz_zip_error mz_zip_clear_last_error(mz_zip_archive* pZip); 1205 - MINIZ_EXPORT mz_zip_error mz_zip_get_last_error(mz_zip_archive* pZip); 1206 - MINIZ_EXPORT const char* mz_zip_get_error_string(mz_zip_error mz_err); 1207 - 1208 - /* MZ_TRUE if the archive file entry is a directory entry. */ 1209 - MINIZ_EXPORT mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive* pZip, mz_uint file_index); 1210 - 1211 - /* MZ_TRUE if the file is encrypted/strong encrypted. */ 1212 - MINIZ_EXPORT mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive* pZip, mz_uint file_index); 1213 - 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. */ 1216 - MINIZ_EXPORT mz_bool mz_zip_reader_is_file_supported(mz_zip_archive* pZip, mz_uint file_index); 1217 - 1218 - /* Retrieves the filename of an archive file entry. */ 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. */ 1221 - MINIZ_EXPORT mz_uint mz_zip_reader_get_filename(mz_zip_archive* pZip, mz_uint file_index, char* pFilename, 1222 - mz_uint filename_buf_size); 1223 - 1224 - /* Attempts to locates a file in the archive's central directory. */ 1225 - /* Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH */ 1226 - /* Returns -1 if the file cannot be found. */ 1227 - MINIZ_EXPORT int mz_zip_reader_locate_file(mz_zip_archive* pZip, const char* pName, const char* pComment, 1228 - mz_uint flags); 1229 - MINIZ_EXPORT mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive* pZip, const char* pName, const char* pComment, 1230 - mz_uint flags, mz_uint32* file_index); 1231 - 1232 - /* Returns detailed information about an archive file entry. */ 1233 - MINIZ_EXPORT mz_bool mz_zip_reader_file_stat(mz_zip_archive* pZip, mz_uint file_index, mz_zip_archive_file_stat* pStat); 1234 - 1235 - /* MZ_TRUE if the file is in zip64 format. */ 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. */ 1238 - MINIZ_EXPORT mz_bool mz_zip_is_zip64(mz_zip_archive* pZip); 1239 - 1240 - /* Returns the total central directory size in bytes. */ 1241 - /* The current max supported size is <= MZ_UINT32_MAX. */ 1242 - MINIZ_EXPORT size_t mz_zip_get_central_dir_size(mz_zip_archive* pZip); 1243 - 1244 - /* Extracts a archive file to a memory buffer using no memory allocation. */ 1245 - /* There must be at least enough room on the stack to store the inflator's state (~34KB or so). */ 1246 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, 1247 - size_t buf_size, mz_uint flags, void* pUser_read_buf, 1248 - size_t user_read_buf_size); 1249 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive* pZip, const char* pFilename, void* pBuf, 1250 - size_t buf_size, mz_uint flags, void* pUser_read_buf, 1251 - size_t user_read_buf_size); 1252 - 1253 - /* Extracts a archive file to a memory buffer. */ 1254 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, size_t buf_size, 1255 - mz_uint flags); 1256 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive* pZip, const char* pFilename, void* pBuf, 1257 - size_t buf_size, mz_uint flags); 1258 - 1259 - /* Extracts a archive file to a dynamically allocated heap buffer. */ 1260 - /* The memory will be allocated via the mz_zip_archive's alloc/realloc functions. */ 1261 - /* Returns NULL and sets the last error on failure. */ 1262 - MINIZ_EXPORT void* mz_zip_reader_extract_to_heap(mz_zip_archive* pZip, mz_uint file_index, size_t* pSize, 1263 - mz_uint flags); 1264 - MINIZ_EXPORT void* mz_zip_reader_extract_file_to_heap(mz_zip_archive* pZip, const char* pFilename, size_t* pSize, 1265 - mz_uint flags); 1266 - 1267 - /* Extracts a archive file using a callback function to output the file's data. */ 1268 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive* pZip, mz_uint file_index, 1269 - mz_file_write_func pCallback, void* pOpaque, mz_uint flags); 1270 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive* pZip, const char* pFilename, 1271 - mz_file_write_func pCallback, void* pOpaque, mz_uint flags); 1272 - 1273 - /* Extract a file iteratively */ 1274 - MINIZ_EXPORT mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive* pZip, mz_uint file_index, 1275 - mz_uint flags); 1276 - MINIZ_EXPORT mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive* pZip, 1277 - const char* pFilename, 1278 - mz_uint flags); 1279 - MINIZ_EXPORT size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, 1280 - size_t buf_size); 1281 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState); 1282 - 1283 - #ifndef MINIZ_NO_STDIO 1284 - /* Extracts a archive file to a disk file and sets its last accessed and modified times. */ 1285 - /* This function only extracts files, not archive directory records. */ 1286 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_file(mz_zip_archive* pZip, mz_uint file_index, const char* pDst_filename, 1287 - mz_uint flags); 1288 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive* pZip, const char* pArchive_filename, 1289 - const char* pDst_filename, mz_uint flags); 1290 - 1291 - /* Extracts a archive file starting at the current position in the destination FILE stream. */ 1292 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive* pZip, mz_uint file_index, MZ_FILE* File, 1293 - mz_uint flags); 1294 - MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive* pZip, const char* pArchive_filename, 1295 - MZ_FILE* pFile, mz_uint flags); 1296 - #endif 1297 - 1298 - #if 0 1299 - /* TODO */ 1300 - typedef void *mz_zip_streaming_extract_state_ptr; 1301 - mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags); 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); 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); 1306 - mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); 1307 - #endif 1308 - 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. */ 1313 - MINIZ_EXPORT mz_bool mz_zip_validate_file(mz_zip_archive* pZip, mz_uint file_index, mz_uint flags); 1314 - 1315 - /* Validates an entire archive by calling mz_zip_validate_file() on each file. */ 1316 - MINIZ_EXPORT mz_bool mz_zip_validate_archive(mz_zip_archive* pZip, mz_uint flags); 1317 - 1318 - /* Misc utils/helpers, valid for ZIP reading or writing */ 1319 - MINIZ_EXPORT mz_bool mz_zip_validate_mem_archive(const void* pMem, size_t size, mz_uint flags, mz_zip_error* pErr); 1320 - MINIZ_EXPORT mz_bool mz_zip_validate_file_archive(const char* pFilename, mz_uint flags, mz_zip_error* pErr); 1321 - 1322 - /* Universal end function - calls either mz_zip_reader_end() or mz_zip_writer_end(). */ 1323 - MINIZ_EXPORT mz_bool mz_zip_end(mz_zip_archive* pZip); 1324 - 1325 - /* -------- ZIP writing */ 1326 - 1327 - #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 1328 - 1329 - /* Inits a ZIP archive writer. */ 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*/ 1332 - MINIZ_EXPORT mz_bool mz_zip_writer_init(mz_zip_archive* pZip, mz_uint64 existing_size); 1333 - MINIZ_EXPORT mz_bool mz_zip_writer_init_v2(mz_zip_archive* pZip, mz_uint64 existing_size, mz_uint flags); 1334 - 1335 - MINIZ_EXPORT mz_bool mz_zip_writer_init_heap(mz_zip_archive* pZip, size_t size_to_reserve_at_beginning, 1336 - size_t initial_allocation_size); 1337 - MINIZ_EXPORT mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive* pZip, size_t size_to_reserve_at_beginning, 1338 - size_t initial_allocation_size, mz_uint flags); 1339 - 1340 - #ifndef MINIZ_NO_STDIO 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); 1343 - MINIZ_EXPORT mz_bool mz_zip_writer_init_file_v2(mz_zip_archive* pZip, const char* pFilename, 1344 - mz_uint64 size_to_reserve_at_beginning, mz_uint flags); 1345 - MINIZ_EXPORT mz_bool mz_zip_writer_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint flags); 1346 - #endif 1347 - 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 */ 1358 - /* the archive is finalized the file's central directory will be hosed. */ 1359 - MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader(mz_zip_archive* pZip, const char* pFilename); 1360 - MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint flags); 1361 - 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. */ 1367 - MINIZ_EXPORT mz_bool mz_zip_writer_add_mem(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, 1368 - size_t buf_size, mz_uint level_and_flags); 1369 - 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. */ 1373 - MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, 1374 - size_t buf_size, const void* pComment, mz_uint16 comment_size, 1375 - mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32); 1376 - 1377 - MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive* pZip, const char* pArchive_name, const void* pBuf, 1378 - size_t buf_size, const void* pComment, mz_uint16 comment_size, 1379 - mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, 1380 - MZ_TIME_T* last_modified, const char* user_extra_data_local, 1381 - mz_uint user_extra_data_local_len, const char* user_extra_data_central, 1382 - mz_uint user_extra_data_central_len); 1383 - 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.*/ 1387 - MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback( 1388 - mz_zip_archive* pZip, const char* pArchive_name, mz_file_read_func read_callback, void* callback_opaque, 1389 - mz_uint64 max_size, const MZ_TIME_T* pFile_time, const void* pComment, mz_uint16 comment_size, 1390 - mz_uint level_and_flags, const char* user_extra_data_local, mz_uint user_extra_data_local_len, 1391 - const char* user_extra_data_central, mz_uint user_extra_data_central_len); 1392 - 1393 - #ifndef MINIZ_NO_STDIO 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. */ 1398 - MINIZ_EXPORT mz_bool mz_zip_writer_add_file(mz_zip_archive* pZip, const char* pArchive_name, const char* pSrc_filename, 1399 - const void* pComment, mz_uint16 comment_size, mz_uint level_and_flags); 1400 - 1401 - /* Like mz_zip_writer_add_file(), except the file data is read from the specified FILE stream. */ 1402 - MINIZ_EXPORT mz_bool mz_zip_writer_add_cfile(mz_zip_archive* pZip, const char* pArchive_name, MZ_FILE* pSrc_file, 1403 - mz_uint64 max_size, const MZ_TIME_T* pFile_time, const void* pComment, 1404 - mz_uint16 comment_size, mz_uint level_and_flags, 1405 - const char* user_extra_data_local, mz_uint user_extra_data_local_len, 1406 - const char* user_extra_data_central, mz_uint user_extra_data_central_len); 1407 - #endif 1408 - 1409 - /* Adds a file to an archive by fully cloning the data from another archive. */ 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. */ 1413 - MINIZ_EXPORT mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive* pZip, mz_zip_archive* pSource_zip, 1414 - mz_uint src_file_index); 1415 - 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. */ 1419 - MINIZ_EXPORT mz_bool mz_zip_writer_finalize_archive(mz_zip_archive* pZip); 1420 - 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. */ 1423 - MINIZ_EXPORT mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive* pZip, void** ppBuf, size_t* pSize); 1424 - 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 - */ 1428 - MINIZ_EXPORT mz_bool mz_zip_writer_end(mz_zip_archive* pZip); 1429 - 1430 - /* -------- Misc. high-level helper functions: */ 1431 - 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. */ 1439 - MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place(const char* pZip_filename, const char* pArchive_name, 1440 - const void* pBuf, size_t buf_size, const void* pComment, 1441 - mz_uint16 comment_size, mz_uint level_and_flags); 1442 - MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(const char* pZip_filename, const char* pArchive_name, 1443 - const void* pBuf, size_t buf_size, const void* pComment, 1444 - mz_uint16 comment_size, mz_uint level_and_flags, 1445 - mz_zip_error* pErr); 1446 - 1447 - /* Reads a single file from an archive into a heap block. */ 1448 - /* If pComment is not NULL, only the file with the specified comment will be extracted. */ 1449 - /* Returns NULL on failure. */ 1450 - MINIZ_EXPORT void* mz_zip_extract_archive_file_to_heap(const char* pZip_filename, const char* pArchive_name, 1451 - size_t* pSize, mz_uint flags); 1452 - MINIZ_EXPORT void* mz_zip_extract_archive_file_to_heap_v2(const char* pZip_filename, const char* pArchive_name, 1453 - const char* pComment, size_t* pSize, mz_uint flags, 1454 - mz_zip_error* pErr); 1455 - 1456 - #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */ 1457 - 1458 - #ifdef __cplusplus 1459 - } 1460 - #endif 1461 - 1462 - #endif /* MINIZ_NO_ARCHIVE_APIS */
-2
platformio.ini
··· 22 22 build_flags = 23 23 -DARDUINO_USB_MODE=1 24 24 -DARDUINO_USB_CDC_ON_BOOT=1 25 - -DMINIZ_NO_ZLIB_COMPATIBLE_NAMES=1 26 - -DMINIZ_NO_STDIO=1 27 25 -DEINK_DISPLAY_SINGLE_BUFFER_MODE=1 28 26 -DDISABLE_FS_H_WARNING=1 29 27 # https://libexpat.github.io/doc/api/latest/#XML_GE