edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB552edc4Tahinli2 months ago2048 commits
| 1 | //! DEFLATE (RFC 1951) and zlib (RFC 1950) decompression. |
| 2 | //! |
| 3 | //! Two entry points, both one-shot over a complete buffer: [`inflate`] for a |
| 4 | //! raw DEFLATE stream (what PNG's `IDAT` chain and Matroska header stripping |
| 5 | //! hand over) and [`inflate_zlib`] for the same thing inside a zlib wrapper, |
| 6 | //! header checked and Adler-32 verified. |
| 7 | //! |
| 8 | //! Contracts worth knowing before implementing against this crate: |
| 9 | //! |
| 10 | //! - Every call carries a `limit`, the largest output it may produce. A stream |
| 11 | //! that would exceed it stops with [`Error::LimitExceeded`]; a compressed |
| 12 | //! megabyte of zeros is not allowed to become a gigabyte of memory. Pass |
| 13 | //! `usize::MAX` to mean "no ceiling", and mean it. |
| 14 | //! - Malformed input is an error, never a panic. Truncation is |
| 15 | //! [`Error::Truncated`], anything else the format forbids is |
| 16 | //! [`Error::Corrupt`] naming what was wrong. |
| 17 | //! - Decoding is table-driven through a two-level lookup: no Huffman code is |
| 18 | //! walked bit by bit, and the fixed trees are built once per process. |
| 19 | //! |
| 20 | //! Decoder state lives in one struct threaded through the block loop, so |
| 21 | //! resuming across buffer boundaries stays a change to the entry points rather |
| 22 | //! than a rewrite; today the public API is one-shot because both callers are. |
| 23 | //! |
| 24 | //! No async, no unsafe, no external dependencies. |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | use fmt; |
| 34 | |
| 35 | use Bits; |
| 36 | |
| 37 | /// Everything a compressed stream can get wrong. |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | /// Which wrapper, if any, surrounds the DEFLATE data. |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | /// Decompress a raw DEFLATE stream, producing at most `limit` bytes. |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | /// Decompress a zlib stream, producing at most `limit` bytes. |
| 122 | /// |
| 123 | /// The two header bytes are checked and the Adler-32 trailer is verified |
| 124 | /// against the output. |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | /// Decompress onto the end of an existing buffer. |
| 132 | /// |
| 133 | /// `limit` bounds the total length of `out`, not the bytes added. On failure |
| 134 | /// `out` keeps whatever was decompressed before the error, which is what a |
| 135 | /// caller reporting partial progress wants. |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | /// Validate the RFC 1950 header and hand back the DEFLATE data behind it. |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | /// RFC 1950 §9. Chunked so the sums cannot overflow between reductions. |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 |