diff options
| author | ASTROWwwW <astrxwledev@gmail.com> | 2026-11-23 20:00:36 +0100 |
|---|---|---|
| committer | ASTROWwwW <astrxwledev@gmail.com> | 2026-11-23 20:00:36 +0100 |
| commit | ea3d014c3c63ec41debdf75e832397144e4ad018 (patch) | |
| tree | 857e50cea2468c851eae9884959b3388e41375f4 /runtime/src | |
| parent | 7b2054b38a3db5f93308381cdbf678993c56113e (diff) | |
| parent | 00551ba0099699f35893ecd58a9ec34a2b54e95c (diff) | |
Merge branch 'pr7180' into ladder-frame
# Conflicts:
# docs/README.md
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/shader_library.h | 77 |
1 files changed, 48 insertions, 29 deletions
diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 210b54ec6..8e0de4451 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h | |||
| @@ -31,6 +31,10 @@ namespace leonida { | |||
| 31 | // --------------------------------------------------------------------------------------------------------------- | 31 | // --------------------------------------------------------------------------------------------------------------- |
| 32 | // zlib inflate | 32 | // zlib inflate |
| 33 | 33 | ||
| 34 | // Why zlib_inflate failed, for a caller that reports the reason (runtime/shims/dstorage.cpp turns each into its | ||
| 35 | // E_DSTORAGE_ZLIB_* code). `limit`: the output would pass the caller's limit; the stream was not read to its end. | ||
| 36 | enum class ZlibError { none, header, data, checksum, limit }; | ||
| 37 | |||
| 34 | namespace detail { | 38 | namespace detail { |
| 35 | 39 | ||
| 36 | class BitReader { | 40 | class BitReader { |
| @@ -112,8 +116,8 @@ inline int decode(BitReader& in, const Huffman& h) { | |||
| 112 | return -1; | 116 | return -1; |
| 113 | } | 117 | } |
| 114 | 118 | ||
| 115 | inline bool inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const Huffman& lengths, | 119 | inline ZlibError inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const Huffman& lengths, |
| 116 | const Huffman& distances, std::size_t limit) { | 120 | const Huffman& distances, std::size_t limit) { |
| 117 | static constexpr std::int16_t length_base[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, | 121 | static constexpr std::int16_t length_base[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, |
| 118 | 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258}; | 122 | 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258}; |
| 119 | static constexpr std::int16_t length_extra[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, | 123 | static constexpr std::int16_t length_extra[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, |
| @@ -125,20 +129,21 @@ inline bool inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const H | |||
| 125 | 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; | 129 | 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; |
| 126 | for (;;) { | 130 | for (;;) { |
| 127 | int symbol = decode(in, lengths); | 131 | int symbol = decode(in, lengths); |
| 128 | if (symbol < 0 || in.failed()) return false; | 132 | if (symbol < 0 || in.failed()) return ZlibError::data; |
| 129 | if (symbol < 256) { | 133 | if (symbol < 256) { |
| 130 | if (out.size() >= limit) return false; | 134 | if (out.size() >= limit) return ZlibError::limit; |
| 131 | out.push_back(std::uint8_t(symbol)); | 135 | out.push_back(std::uint8_t(symbol)); |
| 132 | continue; | 136 | continue; |
| 133 | } | 137 | } |
| 134 | if (symbol == 256) return true; | 138 | if (symbol == 256) return ZlibError::none; |
| 135 | symbol -= 257; | 139 | symbol -= 257; |
| 136 | if (symbol >= 29) return false; | 140 | if (symbol >= 29) return ZlibError::data; |
| 137 | const std::size_t length = std::size_t(length_base[symbol]) + in.bits(length_extra[symbol]); | 141 | const std::size_t length = std::size_t(length_base[symbol]) + in.bits(length_extra[symbol]); |
| 138 | const int d = decode(in, distances); | 142 | const int d = decode(in, distances); |
| 139 | if (d < 0 || d >= 30) return false; | 143 | if (d < 0 || d >= 30) return ZlibError::data; |
| 140 | const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); | 144 | const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); |
| 141 | if (in.failed() || distance > out.size() || out.size() + length > limit) return false; | 145 | if (in.failed() || distance > out.size()) return ZlibError::data; |
| 146 | if (out.size() + length > limit) return ZlibError::limit; | ||
| 142 | const std::size_t from = out.size() - distance; | 147 | const std::size_t from = out.size() - distance; |
| 143 | for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); | 148 | for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); |
| 144 | } | 149 | } |
| @@ -147,28 +152,39 @@ inline bool inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const H | |||
| 147 | } // namespace detail | 152 | } // namespace detail |
| 148 | 153 | ||
| 149 | // Inflates a zlib stream (RFC 1950: header, deflate data, Adler-32). Returns false for a malformed stream, a | 154 | // Inflates a zlib stream (RFC 1950: header, deflate data, Adler-32). Returns false for a malformed stream, a |
| 150 | // preset dictionary, a checksum mismatch, or output beyond `limit` bytes. | 155 | // preset dictionary, a checksum mismatch, or output beyond `limit` bytes, and then stores the reason in `*why` when |
| 156 | // it is given: `header` for the two header bytes (not deflate, a window over 32 KiB, a bad check value, a preset | ||
| 157 | // dictionary), `data` for the deflate data or a missing trailer, `checksum` for an Adler-32 mismatch, `limit` for | ||
| 158 | // output beyond `limit`. Bytes after the Adler-32 trailer are not read. | ||
| 151 | inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint8_t>& out, | 159 | inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint8_t>& out, |
| 152 | std::size_t limit = std::size_t(1) << 30) { | 160 | std::size_t limit = std::size_t(1) << 30, ZlibError* why = nullptr) { |
| 153 | out.clear(); | 161 | out.clear(); |
| 154 | if (in.size() < 6) return false; | 162 | const auto fail = [why](ZlibError reason) { |
| 163 | if (why) *why = reason; | ||
| 164 | return false; | ||
| 165 | }; | ||
| 166 | if (why) *why = ZlibError::none; | ||
| 167 | if (in.size() < 2) return fail(ZlibError::header); | ||
| 155 | const unsigned cmf = in[0], flg = in[1]; | 168 | const unsigned cmf = in[0], flg = in[1]; |
| 156 | if ((cmf & 0x0f) != 8 || (cmf >> 4) > 7 || ((cmf << 8) | flg) % 31 != 0 || (flg & 0x20)) return false; | 169 | if ((cmf & 0x0f) != 8 || (cmf >> 4) > 7 || ((cmf << 8) | flg) % 31 != 0 || (flg & 0x20)) |
| 170 | return fail(ZlibError::header); | ||
| 171 | if (in.size() < 6) return fail(ZlibError::data); | ||
| 157 | detail::BitReader reader(in.subspan(2)); | 172 | detail::BitReader reader(in.subspan(2)); |
| 158 | for (bool last = false; !last;) { | 173 | for (bool last = false; !last;) { |
| 159 | last = reader.bits(1) != 0; | 174 | last = reader.bits(1) != 0; |
| 160 | const std::uint32_t type = reader.bits(2); | 175 | const std::uint32_t type = reader.bits(2); |
| 161 | if (reader.failed()) return false; | 176 | if (reader.failed()) return fail(ZlibError::data); |
| 162 | if (type == 0) { // stored | 177 | if (type == 0) { // stored |
| 163 | reader.align(); | 178 | reader.align(); |
| 164 | std::uint8_t b[4]; | 179 | std::uint8_t b[4]; |
| 165 | for (auto& x : b) | 180 | for (auto& x : b) |
| 166 | if (!reader.byte(x)) return false; | 181 | if (!reader.byte(x)) return fail(ZlibError::data); |
| 167 | const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8); | 182 | const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8); |
| 168 | if (length != (~complement & 0xffff) || out.size() + length > limit) return false; | 183 | if (length != (~complement & 0xffff)) return fail(ZlibError::data); |
| 184 | if (out.size() + length > limit) return fail(ZlibError::limit); | ||
| 169 | for (unsigned i = 0; i < length; ++i) { | 185 | for (unsigned i = 0; i < length; ++i) { |
| 170 | std::uint8_t x; | 186 | std::uint8_t x; |
| 171 | if (!reader.byte(x)) return false; | 187 | if (!reader.byte(x)) return fail(ZlibError::data); |
| 172 | out.push_back(x); | 188 | out.push_back(x); |
| 173 | } | 189 | } |
| 174 | } else if (type == 1) { // fixed Huffman | 190 | } else if (type == 1) { // fixed Huffman |
| @@ -184,19 +200,21 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 184 | detail::build_huffman(t.second, lengths, 30); | 200 | detail::build_huffman(t.second, lengths, 30); |
| 185 | return t; | 201 | return t; |
| 186 | }(); | 202 | }(); |
| 187 | if (!detail::inflate_codes(reader, out, tables.first, tables.second, limit)) return false; | 203 | if (const ZlibError e = detail::inflate_codes(reader, out, tables.first, tables.second, limit); |
| 204 | e != ZlibError::none) | ||
| 205 | return fail(e); | ||
| 188 | } else if (type == 2) { // dynamic Huffman | 206 | } else if (type == 2) { // dynamic Huffman |
| 189 | const int literal_count = int(reader.bits(5)) + 257, distance_count = int(reader.bits(5)) + 1; | 207 | const int literal_count = int(reader.bits(5)) + 257, distance_count = int(reader.bits(5)) + 1; |
| 190 | const int code_count = int(reader.bits(4)) + 4; | 208 | const int code_count = int(reader.bits(4)) + 4; |
| 191 | if (literal_count > 286 || distance_count > 30) return false; | 209 | if (literal_count > 286 || distance_count > 30) return fail(ZlibError::data); |
| 192 | static constexpr int order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | 210 | static constexpr int order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 193 | std::int16_t lengths[320] = {}; | 211 | std::int16_t lengths[320] = {}; |
| 194 | for (int i = 0; i < code_count; ++i) lengths[order[i]] = std::int16_t(reader.bits(3)); | 212 | for (int i = 0; i < code_count; ++i) lengths[order[i]] = std::int16_t(reader.bits(3)); |
| 195 | detail::Huffman code_lengths; | 213 | detail::Huffman code_lengths; |
| 196 | if (!detail::build_huffman(code_lengths, lengths, 19)) return false; | 214 | if (!detail::build_huffman(code_lengths, lengths, 19)) return fail(ZlibError::data); |
| 197 | for (int i = 0; i < literal_count + distance_count;) { | 215 | for (int i = 0; i < literal_count + distance_count;) { |
| 198 | const int symbol = detail::decode(reader, code_lengths); | 216 | const int symbol = detail::decode(reader, code_lengths); |
| 199 | if (symbol < 0) return false; | 217 | if (symbol < 0) return fail(ZlibError::data); |
| 200 | if (symbol < 16) { | 218 | if (symbol < 16) { |
| 201 | lengths[i++] = std::int16_t(symbol); | 219 | lengths[i++] = std::int16_t(symbol); |
| 202 | continue; | 220 | continue; |
| @@ -204,7 +222,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 204 | std::int16_t value = 0; | 222 | std::int16_t value = 0; |
| 205 | int repeat; | 223 | int repeat; |
| 206 | if (symbol == 16) { | 224 | if (symbol == 16) { |
| 207 | if (i == 0) return false; | 225 | if (i == 0) return fail(ZlibError::data); |
| 208 | value = lengths[i - 1]; | 226 | value = lengths[i - 1]; |
| 209 | repeat = 3 + int(reader.bits(2)); | 227 | repeat = 3 + int(reader.bits(2)); |
| 210 | } else if (symbol == 17) { | 228 | } else if (symbol == 17) { |
| @@ -212,23 +230,24 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 212 | } else { | 230 | } else { |
| 213 | repeat = 11 + int(reader.bits(7)); | 231 | repeat = 11 + int(reader.bits(7)); |
| 214 | } | 232 | } |
| 215 | if (i + repeat > literal_count + distance_count) return false; | 233 | if (i + repeat > literal_count + distance_count) return fail(ZlibError::data); |
| 216 | while (repeat--) lengths[i++] = value; | 234 | while (repeat--) lengths[i++] = value; |
| 217 | } | 235 | } |
| 218 | if (lengths[256] == 0) return false; // no end-of-block code | 236 | if (lengths[256] == 0) return fail(ZlibError::data); // no end-of-block code |
| 219 | detail::Huffman literal, distance; | 237 | detail::Huffman literal, distance; |
| 220 | if (!detail::build_huffman(literal, lengths, literal_count) || | 238 | if (!detail::build_huffman(literal, lengths, literal_count) || |
| 221 | !detail::build_huffman(distance, lengths + literal_count, distance_count)) | 239 | !detail::build_huffman(distance, lengths + literal_count, distance_count)) |
| 222 | return false; | 240 | return fail(ZlibError::data); |
| 223 | if (!detail::inflate_codes(reader, out, literal, distance, limit)) return false; | 241 | if (const ZlibError e = detail::inflate_codes(reader, out, literal, distance, limit); e != ZlibError::none) |
| 242 | return fail(e); | ||
| 224 | } else { | 243 | } else { |
| 225 | return false; | 244 | return fail(ZlibError::data); |
| 226 | } | 245 | } |
| 227 | if (reader.failed()) return false; | 246 | if (reader.failed()) return fail(ZlibError::data); |
| 228 | } | 247 | } |
| 229 | // Adler-32, big-endian, after the deflate data's last whole byte. | 248 | // Adler-32, big-endian, after the deflate data's last whole byte. |
| 230 | const std::size_t at = 2 + reader.position(); | 249 | const std::size_t at = 2 + reader.position(); |
| 231 | if (at + 4 > in.size()) return false; | 250 | if (at + 4 > in.size()) return fail(ZlibError::data); |
| 232 | std::uint32_t a = 1, b = 0; | 251 | std::uint32_t a = 1, b = 0; |
| 233 | for (const std::uint8_t x : out) { | 252 | for (const std::uint8_t x : out) { |
| 234 | a = (a + x) % 65521; | 253 | a = (a + x) % 65521; |
| @@ -236,7 +255,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 236 | } | 255 | } |
| 237 | const std::uint32_t expected = (std::uint32_t(in[at]) << 24) | (std::uint32_t(in[at + 1]) << 16) | | 256 | const std::uint32_t expected = (std::uint32_t(in[at]) << 24) | (std::uint32_t(in[at + 1]) << 16) | |
| 238 | (std::uint32_t(in[at + 2]) << 8) | in[at + 3]; | 257 | (std::uint32_t(in[at + 2]) << 8) | in[at + 3]; |
| 239 | return ((b << 16) | a) == expected; | 258 | return ((b << 16) | a) == expected || fail(ZlibError::checksum); |
| 240 | } | 259 | } |
| 241 | 260 | ||
| 242 | // --------------------------------------------------------------------------------------------------------------- | 261 | // --------------------------------------------------------------------------------------------------------------- |