| | @@ -0,0 +1,211 @@ |
| 1 | +/* miniz.c v1.15 - public domain deflate/inflate, zlib-subset, ZIP reading/wri
|
| 2 | +ting/appending, PNG writing
|
| 3 | + Rich Geldreich <[email protected]>, last updated Oct. 13, 2013
|
| 4 | + Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://w
|
| 5 | +
|
| 6 | + Most API's defined in miniz.c are optional. For example, to disable the archive re
|
| 7 | + (r->m_zhdr0 * 256 + r-, or to get rid of all stdio usage define MINIZ_NO_STDIO (see the list below for more macros).
|
| 8 | +
|
| 9 | + * Change History
|
| 10 | + 10/1ller must mz_free() the returned heap block (which will typically be larger than *pLen
|
| 11 | +eeded.
|
| 12 | +void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level,
|
| 13 | +mz_bool flip);
|
| 14 | +void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);
|
| 15 | +
|
| 16 | +// Output stream interface.
|
| 17 | +nterface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time.
|
| 18 | +typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, in
|
| 19 | +) compresses a block to an output stream. The above helpers use this function intern return MZ_DATA_ERROR;
|
| 20 | +
|
| 21 | + if (pState->m_has_flushed && (
|
| 22 | +(pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
|
| 23 | + pState->m_has_flushed |= (flush == MZ_FINISH);
|
| 24 | +
|
| 25 | + if ((flush == MZ_FINISH) && (first_call))
|
| 26 | + {
|
| 27 | + // MZ_FINISH on the first cal
|
| 28 | +l implies that the input and output buffers are large enough to hold the entire compressed/decompressed file.
|
| 29 | + decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
|
| 30 | + in_bytes = pStream->avail_
|
| 31 | +(pState->m_has_flushed && (flusER) && (r->m_check_anfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStrea
|
| 32 | +comp_flags);
|
| 33 | + pState->m_last_status = status;
|
| 34 | + pStream->next_in += (mz_uint
|
| 35 | +)in_bytes; pStream->avail_in -= (mz_uint)in_bytes; pStream->total_in += (mz_uint)in_byt
|
| 36 | +nfl_get_adler32(&pState->m_decomp);
|
| 37 | + pStream->next_out += (mz_uint)out_bytes;
|
| 38 | +pStream->avail_out -= (m
|
| 39 | +total_out += (mz_uint)out_bytes;
|
| 40 | +
|
| 41 | + if (status < 0)
|
| 42 | + return MZ_
|
| 43 | +/* miniz.c v1.15 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
|
| 44 | + See "unlicense" statement at the end d void *pImage, int w,
|
| 45 | + int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip);
|
| 46 | +
|
| 47 | +void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chan
|
| 48 | +utput stream interface. The compressor uses this interface to write compressed data.
|
| 49 | +d data when flush==MZ_FINI- public domain deflate/inflate, zlib-subset, ZIP reading/writing/appen/(const void* pBuf, int len, void *pUser);
|
| 50 | +
|
| 51 | +// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function inter
|
| 52 | + f (pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
|
| 53 | + pState->m_has_flushed |= (flush == MZ_FINISH);
|
| 54 | +
|
| 55 | + if ((flush == MZ_FINISH) && (first_c
|
| 56 | +on the first call implies that the input and output buffers are large enough to hold the e
|
| 57 | +ed file.
|
| 58 | + decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
|
| 59 | + in_bytes = pStream->avail_in; out_bytes = pStream->avail_out;
|
| 60 | + status =
|
| 61 | + tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags)
|
| 62 | +;
|
| 63 | + pState->m_last_status = status;
|
| 64 | + pStream->next_in += (mz_uint)in_b
|
| 65 | +ytes; pStream->avail_in -= (mz_uint)in_bytes; pStream->total_in += (mz_uint)in_bytes;
|
| 66 | + pStream->adler = tinfl_get_adler32(&pState->m_decomp);
|
| 67 | +
|
| 68 | +er32(&pState->m_decomp);
|
| 69 | + pStream->next_out += (mz_uint)out_bytes; pStream->avail_out -= (mz_uint)out_bytes; pStream->total_out += (mz_uint
|
| 70 | +/* miniz.c v1.15 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
|
| 71 | + See "unlicense" statement at the end of this file.
|
| 72 | + i))
|
| 73 | + return MZ_BUF_ERROR; // Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH.
|
| 74 | +
|
| 75 | +else if (flush == MZ_FINISH)
|
| 76 | + {
|
| 77 | + // The output buffer MUST be large to hol
|
| 78 | +d data when flush==MZ_FINISH.
|
| 79 | + if (status == TINFL_STATUS_DONE)
|
| 80 | + return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
|
| 81 | +
|
| 82 | +T, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong.
|
| 83 | + else if
|
| 84 | +ng is wrong.
|
| 85 | + else if (!pStream->avail_out)
|
| 86 | + return MZ_BUF_ERROR;
|
| 87 | + }
|
| 88 | + else if ((status == TINFL_STATUS_DONE)
|
| 89 | + tinfl_decompressor riting/appending, PNG writing
|
| 90 | + See "unlicense" statement at the end of this file.
|
| 91 | + Rich G/te->m_dict_avail)) ? MZ_STREA
|
| 92 | +flateEnd(mz_streamp pStream)
|
| 93 | +{
|
| 94 | +
|
| 95 | +Z_STREAM_ERROR;
|
| 96 | + if (pStream->state)
|
| 97 | + {
|
| 98 | + pStream->zfree(pStream->opaque, pStream->st
|
| 99 | +
|
| 100 | +int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource
|
| 101 | +onst unsigned char *pSource, mz_ulong source_len)
|
| 102 | +{
|
| 103 | + mz_stream stream;
|
| 104 | + int status;
|
| 105 | + mem
|
| 106 | +am));
|
| 107 | +
|
| 108 | + // In case mz_ulong is 64-bits (argh I hate longs).
|
| 109 | + if ((source_len | *pD
|
| 110 | +urn MZ_PARAM_ERROR;
|
| 111 | +
|
| 112 | + stream.next_in = pSource;
|
| 113 | + stream.avail_in = (mz_uint32)source_len;
|
| 114 | +
|
| 115 | +stream.next_out
|
| 116 | +t = (mz_uint32)*pD
|
| 117 | +
|
| 118 | +tus != MZ_OK)
|
| 119 | + return s
|
| 120 | +
|
| 121 | +e(&stream, MZ_FINISH);
|
| 122 | + if (status != MZ_STREAM_END)
|
| 123 | + {
|
| 124 | + mz_inflateEnd(&st
|
| 125 | +ream);
|
| 126 | + return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERRO
|
| 127 | +
|
| 128 | +
|
| 129 | + char *mz_error(int err)
|
| 130 | +{
|
| 131 | + static const struct { int m_err; const char *m_
|
| 132 | +pDesc; } s_error_descs[] =
|
| 133 | + {
|
| 134 | + { MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "fi
|
| 135 | +le error" }, { MZ_STRE
|
| 136 | +
|
| 137 | + RD(++q)) && (TDEFL_Ra error" }, { MZ_MEM_ERROR, "out of
|
| 138 | +
|
| 139 | +* miniz.c v1.15 - public doefl else if (!pStream->avail_out)
|
| 140 | + return MZ_BUF_ERROR;
|
| 141 | + }
|
| 142 | + else if ((s
|
| 143 | +
|
| 144 | +ream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)
|
| 145 | +;
|
| 146 | + inreak;
|
| 147 | + }
|
| 148 | +
|
| 149 | + return ((status == TINFL_STATUS_DONE) && (!pState->m_dic
|
| 150 | + MZ_OK;
|
| 151 | +}
|
| 152 | +
|
| 153 | +int mz_inflate
|
| 154 | +End(mz_streamp pStream)
|
| 155 | +{
|
| 156 | +
|
| 157 | +_STREAM_ERROR;
|
| 158 | + if (pStream->state)
|
| 159 | + {
|
| 160 | + pStream->zfree(pStream->opaque, pStream->sta
|
| 161 | +ULL;
|
| 162 | + }
|
| 163 | + return MZ_OK;
|
| 164 | +}
|
| 165 | +
|
| 166 | +int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_le
|
| 167 | +
|
| 168 | + Limitations:
|
| 169 | +pDest_len, const unsigned char *pSource, mz_ulong source_len)
|
| 170 | +{
|
| 171 | + mz_stream stre
|
| 172 | +;
|
| 173 | +
|
| 174 | + // In case mz_ulong is 64-bits (argh I hate longs).
|
| 175 | + if ((source_len | *pD
|
| 176 | +urn MZ_PARAM_ERROR;
|
| 177 | +
|
| 178 | + stream.next_in = pSource;
|
| 179 | + stream.avail_in =
|
| 180 | +
|
| 181 | +
|
| 182 | +_uint32)*pDest_len;
|
| 183 | +
|
| 184 | +
|
| 185 | +
|
| 186 | +)
|
| 187 | + return status;
|
| 188 | +
|
| 189 | + stat
|
| 190 | +
|
| 191 | + return status;
|
| 192 | +
|
| 193 | + status = mz_inflate(&stream, MZ_FINISH);
|
| 194 | + if (status != MZ_STREA
|
| 195 | +d(&stream);
|
| 196 | + return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_E
|
| 197 | + return mz_inflateEnd(&stream);
|
| 198 | +}
|
| 199 | +
|
| 200 | +const char *mz_error(int err)
|
| 201 | +{
|
| 202 | + static const st
|
| 203 | +r *m_pDesc; } s_error_descs[] =
|
| 204 | + {
|
| 205 | + { MZ_OK, "" }, { MZ_STREAM_END, "stream
|
| 206 | + end" }, { MZ_NEED_DICT, "need
|
| 207 | +
|
| 208 | +file error" }, { MZ_STREAM_ERROR, "stream error" },
|
| 209 | + { MZ_DATA_ERROR, "data error" }, {
|
| 210 | +
|
| 211 | +* miniz.c v1.15 - public domain deflate/inflate, zlib-subset, ZIP reading/w.15 - pubn_bytes = pStream->avail/* miniz.c v1. |