diff options
| author | ubiquitarianism <283527159+ubiquitarianism@users.noreply.github.com> | 2026-11-23 22:36:12 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 22:36:12 -0500 |
| commit | fabe844cd88e6fd6469ffd3d869bbe062ebefe1f (patch) | |
| tree | ffd126d5d76910e65b140c235dfdcf629f292cc5 /runtime/src | |
| parent | f9e7e3e1bfa718eb4d8ca84fd1dbe617038d48d7 (diff) | |
| parent | 2d7be48dec529446e0ead3393e2b3acabb29ff64 (diff) | |
Merge pull request #131 from Mumogg/restore-directstorage
runtime: restore the console DirectStorage shim, with the typed-error inflate it needs (RT-DSTORAGE-RESTORE, #47)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/shader_library.h | 74 |
1 files changed, 46 insertions, 28 deletions
diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 2fe53d4ec..9f752317e 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h | |||
| @@ -33,6 +33,10 @@ namespace leonida { | |||
| 33 | // --------------------------------------------------------------------------------------------------------------- | 33 | // --------------------------------------------------------------------------------------------------------------- |
| 34 | // zlib inflate | 34 | // zlib inflate |
| 35 | 35 | ||
| 36 | // Why zlib_inflate failed, for a caller that reports the reason (runtime/shims/dstorage.cpp turns each into its | ||
| 37 | // E_DSTORAGE_ZLIB_* code). `limit`: the output would pass the caller's limit; the stream was not read to its end. | ||
| 38 | enum class ZlibError { none, header, data, checksum, limit }; | ||
| 39 | |||
| 36 | namespace detail { | 40 | namespace detail { |
| 37 | 41 | ||
| 38 | class BitReader { | 42 | class BitReader { |
| @@ -215,7 +219,7 @@ inline int decode(BitReader& in, const Huffman& h) { | |||
| 215 | return -1; | 219 | return -1; |
| 216 | } | 220 | } |
| 217 | 221 | ||
| 218 | inline bool inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const Huffman& lengths, | 222 | inline ZlibError inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const Huffman& lengths, |
| 219 | const Huffman& distances, std::size_t limit) { | 223 | const Huffman& distances, std::size_t limit) { |
| 220 | static constexpr std::int16_t length_base[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, | 224 | static constexpr std::int16_t length_base[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, |
| 221 | 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258}; | 225 | 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258}; |
| @@ -228,20 +232,21 @@ inline bool inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const H | |||
| 228 | 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; | 232 | 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; |
| 229 | for (;;) { | 233 | for (;;) { |
| 230 | int symbol = decode(in, lengths); | 234 | int symbol = decode(in, lengths); |
| 231 | if (symbol < 0 || in.failed()) return false; | 235 | if (symbol < 0 || in.failed()) return ZlibError::data; |
| 232 | if (symbol < 256) { | 236 | if (symbol < 256) { |
| 233 | if (out.size() >= limit) return false; | 237 | if (out.size() >= limit) return ZlibError::limit; |
| 234 | out.push_back(std::uint8_t(symbol)); | 238 | out.push_back(std::uint8_t(symbol)); |
| 235 | continue; | 239 | continue; |
| 236 | } | 240 | } |
| 237 | if (symbol == 256) return true; | 241 | if (symbol == 256) return ZlibError::none; |
| 238 | symbol -= 257; | 242 | symbol -= 257; |
| 239 | if (symbol >= 29) return false; | 243 | if (symbol >= 29) return ZlibError::data; |
| 240 | const std::size_t length = std::size_t(length_base[symbol]) + in.bits(length_extra[symbol]); | 244 | const std::size_t length = std::size_t(length_base[symbol]) + in.bits(length_extra[symbol]); |
| 241 | const int d = decode(in, distances); | 245 | const int d = decode(in, distances); |
| 242 | if (d < 0 || d >= 30) return false; | 246 | if (d < 0 || d >= 30) return ZlibError::data; |
| 243 | const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); | 247 | const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); |
| 244 | if (in.failed() || distance > out.size() || out.size() + length > limit) return false; | 248 | if (in.failed() || distance > out.size()) return ZlibError::data; |
| 249 | if (out.size() + length > limit) return ZlibError::limit; | ||
| 245 | const std::size_t from = out.size() - distance; | 250 | const std::size_t from = out.size() - distance; |
| 246 | for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); | 251 | for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); |
| 247 | } | 252 | } |
| @@ -250,28 +255,39 @@ inline bool inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const H | |||
| 250 | } // namespace detail | 255 | } // namespace detail |
| 251 | 256 | ||
| 252 | // Inflates a zlib stream (RFC 1950: header, deflate data, Adler-32). Returns false for a malformed stream, a | 257 | // Inflates a zlib stream (RFC 1950: header, deflate data, Adler-32). Returns false for a malformed stream, a |
| 253 | // preset dictionary, a checksum mismatch, or output beyond `limit` bytes. | 258 | // preset dictionary, a checksum mismatch, or output beyond `limit` bytes, and then stores the reason in `*why` when |
| 259 | // it is given: `header` for the two header bytes (not deflate, a window over 32 KiB, a bad check value, a preset | ||
| 260 | // dictionary), `data` for the deflate data or a missing trailer, `checksum` for an Adler-32 mismatch, `limit` for | ||
| 261 | // output beyond `limit`. Bytes after the Adler-32 trailer are not read. | ||
| 254 | inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint8_t>& out, | 262 | inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint8_t>& out, |
| 255 | std::size_t limit = std::size_t(1) << 30) { | 263 | std::size_t limit = std::size_t(1) << 30, ZlibError* why = nullptr) { |
| 256 | out.clear(); | 264 | out.clear(); |
| 257 | if (in.size() < 6) return false; | 265 | const auto fail = [why](ZlibError reason) { |
| 266 | if (why) *why = reason; | ||
| 267 | return false; | ||
| 268 | }; | ||
| 269 | if (why) *why = ZlibError::none; | ||
| 270 | if (in.size() < 2) return fail(ZlibError::header); | ||
| 258 | const unsigned cmf = in[0], flg = in[1]; | 271 | const unsigned cmf = in[0], flg = in[1]; |
| 259 | if ((cmf & 0x0f) != 8 || (cmf >> 4) > 7 || ((cmf << 8) | flg) % 31 != 0 || (flg & 0x20)) return false; | 272 | if ((cmf & 0x0f) != 8 || (cmf >> 4) > 7 || ((cmf << 8) | flg) % 31 != 0 || (flg & 0x20)) |
| 273 | return fail(ZlibError::header); | ||
| 274 | if (in.size() < 6) return fail(ZlibError::data); | ||
| 260 | detail::BitReader reader(in.subspan(2)); | 275 | detail::BitReader reader(in.subspan(2)); |
| 261 | for (bool last = false; !last;) { | 276 | for (bool last = false; !last;) { |
| 262 | last = reader.bits(1) != 0; | 277 | last = reader.bits(1) != 0; |
| 263 | const std::uint32_t type = reader.bits(2); | 278 | const std::uint32_t type = reader.bits(2); |
| 264 | if (reader.failed()) return false; | 279 | if (reader.failed()) return fail(ZlibError::data); |
| 265 | if (type == 0) { // stored | 280 | if (type == 0) { // stored |
| 266 | reader.align(); | 281 | reader.align(); |
| 267 | std::uint8_t b[4]; | 282 | std::uint8_t b[4]; |
| 268 | for (auto& x : b) | 283 | for (auto& x : b) |
| 269 | if (!reader.byte(x)) return false; | 284 | if (!reader.byte(x)) return fail(ZlibError::data); |
| 270 | const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8); | 285 | const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8); |
| 271 | if (length != (~complement & 0xffff) || out.size() + length > limit) return false; | 286 | if (length != (~complement & 0xffff)) return fail(ZlibError::data); |
| 287 | if (out.size() + length > limit) return fail(ZlibError::limit); | ||
| 272 | for (unsigned i = 0; i < length; ++i) { | 288 | for (unsigned i = 0; i < length; ++i) { |
| 273 | std::uint8_t x; | 289 | std::uint8_t x; |
| 274 | if (!reader.byte(x)) return false; | 290 | if (!reader.byte(x)) return fail(ZlibError::data); |
| 275 | out.push_back(x); | 291 | out.push_back(x); |
| 276 | } | 292 | } |
| 277 | } else if (type == 1) { // fixed Huffman | 293 | } else if (type == 1) { // fixed Huffman |
| @@ -287,19 +303,20 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 287 | detail::build_huffman(t.second, lengths, 30); | 303 | detail::build_huffman(t.second, lengths, 30); |
| 288 | return t; | 304 | return t; |
| 289 | }(); | 305 | }(); |
| 290 | if (!detail::inflate_codes(reader, out, tables.first, tables.second, limit)) return false; | 306 | if (const ZlibError e = detail::inflate_codes(reader, out, tables.first, tables.second, limit); e != ZlibError::none) |
| 307 | return fail(e); | ||
| 291 | } else if (type == 2) { // dynamic Huffman | 308 | } else if (type == 2) { // dynamic Huffman |
| 292 | const int literal_count = int(reader.bits(5)) + 257, distance_count = int(reader.bits(5)) + 1; | 309 | const int literal_count = int(reader.bits(5)) + 257, distance_count = int(reader.bits(5)) + 1; |
| 293 | const int code_count = int(reader.bits(4)) + 4; | 310 | const int code_count = int(reader.bits(4)) + 4; |
| 294 | if (literal_count > 286 || distance_count > 30) return false; | 311 | if (literal_count > 286 || distance_count > 30) return fail(ZlibError::data); |
| 295 | static constexpr int order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | 312 | static constexpr int order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 296 | std::int16_t lengths[320] = {}; | 313 | std::int16_t lengths[320] = {}; |
| 297 | for (int i = 0; i < code_count; ++i) lengths[order[i]] = std::int16_t(reader.bits(3)); | 314 | for (int i = 0; i < code_count; ++i) lengths[order[i]] = std::int16_t(reader.bits(3)); |
| 298 | detail::Huffman code_lengths; | 315 | detail::Huffman code_lengths; |
| 299 | if (!detail::build_huffman(code_lengths, lengths, 19)) return false; | 316 | if (!detail::build_huffman(code_lengths, lengths, 19)) return fail(ZlibError::data); |
| 300 | for (int i = 0; i < literal_count + distance_count;) { | 317 | for (int i = 0; i < literal_count + distance_count;) { |
| 301 | const int symbol = detail::decode(reader, code_lengths); | 318 | const int symbol = detail::decode(reader, code_lengths); |
| 302 | if (symbol < 0) return false; | 319 | if (symbol < 0) return fail(ZlibError::data); |
| 303 | if (symbol < 16) { | 320 | if (symbol < 16) { |
| 304 | lengths[i++] = std::int16_t(symbol); | 321 | lengths[i++] = std::int16_t(symbol); |
| 305 | continue; | 322 | continue; |
| @@ -307,7 +324,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 307 | std::int16_t value = 0; | 324 | std::int16_t value = 0; |
| 308 | int repeat; | 325 | int repeat; |
| 309 | if (symbol == 16) { | 326 | if (symbol == 16) { |
| 310 | if (i == 0) return false; | 327 | if (i == 0) return fail(ZlibError::data); |
| 311 | value = lengths[i - 1]; | 328 | value = lengths[i - 1]; |
| 312 | repeat = 3 + int(reader.bits(2)); | 329 | repeat = 3 + int(reader.bits(2)); |
| 313 | } else if (symbol == 17) { | 330 | } else if (symbol == 17) { |
| @@ -315,23 +332,24 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 315 | } else { | 332 | } else { |
| 316 | repeat = 11 + int(reader.bits(7)); | 333 | repeat = 11 + int(reader.bits(7)); |
| 317 | } | 334 | } |
| 318 | if (i + repeat > literal_count + distance_count) return false; | 335 | if (i + repeat > literal_count + distance_count) return fail(ZlibError::data); |
| 319 | while (repeat--) lengths[i++] = value; | 336 | while (repeat--) lengths[i++] = value; |
| 320 | } | 337 | } |
| 321 | if (lengths[256] == 0) return false; // no end-of-block code | 338 | if (lengths[256] == 0) return fail(ZlibError::data); // no end-of-block code |
| 322 | detail::Huffman literal, distance; | 339 | detail::Huffman literal, distance; |
| 323 | if (!detail::build_huffman(literal, lengths, literal_count) || | 340 | if (!detail::build_huffman(literal, lengths, literal_count) || |
| 324 | !detail::build_huffman(distance, lengths + literal_count, distance_count)) | 341 | !detail::build_huffman(distance, lengths + literal_count, distance_count)) |
| 325 | return false; | 342 | return fail(ZlibError::data); |
| 326 | if (!detail::inflate_codes(reader, out, literal, distance, limit)) return false; | 343 | if (const ZlibError e = detail::inflate_codes(reader, out, literal, distance, limit); e != ZlibError::none) |
| 344 | return fail(e); | ||
| 327 | } else { | 345 | } else { |
| 328 | return false; | 346 | return fail(ZlibError::data); |
| 329 | } | 347 | } |
| 330 | if (reader.failed()) return false; | 348 | if (reader.failed()) return fail(ZlibError::data); |
| 331 | } | 349 | } |
| 332 | // Adler-32, big-endian, after the deflate data's last whole byte. | 350 | // Adler-32, big-endian, after the deflate data's last whole byte. |
| 333 | const std::size_t at = 2 + reader.position(); | 351 | const std::size_t at = 2 + reader.position(); |
| 334 | if (at + 4 > in.size()) return false; | 352 | if (at + 4 > in.size()) return fail(ZlibError::data); |
| 335 | std::uint32_t a = 1, b = 0; | 353 | std::uint32_t a = 1, b = 0; |
| 336 | for (const std::uint8_t x : out) { | 354 | for (const std::uint8_t x : out) { |
| 337 | a = (a + x) % 65521; | 355 | a = (a + x) % 65521; |
| @@ -339,7 +357,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 339 | } | 357 | } |
| 340 | const std::uint32_t expected = (std::uint32_t(in[at]) << 24) | (std::uint32_t(in[at + 1]) << 16) | | 358 | const std::uint32_t expected = (std::uint32_t(in[at]) << 24) | (std::uint32_t(in[at + 1]) << 16) | |
| 341 | (std::uint32_t(in[at + 2]) << 8) | in[at + 3]; | 359 | (std::uint32_t(in[at + 2]) << 8) | in[at + 3]; |
| 342 | return ((b << 16) | a) == expected; | 360 | return ((b << 16) | a) == expected || fail(ZlibError::checksum); |
| 343 | } | 361 | } |
| 344 | 362 | ||
| 345 | // --------------------------------------------------------------------------------------------------------------- | 363 | // --------------------------------------------------------------------------------------------------------------- |