upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 20:23:21 -0600
committerMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 20:23:21 -0600
commit2d7be48dec529446e0ead3393e2b3acabb29ff64 (patch)
treee33ffbd80a396f79ea4842fbd3e45d7f145df73e /runtime/src
parent9ec34cdff41b19d7671bc260e5f40dc69790d905 (diff)
runtime: restore the console DirectStorage shim (DStorageGetFactory) on main
The merge that brought the mirrored pull requests into main (6dece9231) left the DirectStorage shim out, noting that it "needs the typed-error zlib inflate". The shim's commits (f87d05fdd, ac1843da2, 00551ba00, by mariuslozo-boop and Kelebek) are ancestors of main, but its files are not in the tree, so the retail executable's XCRDAPI.dll!DStorageGetFactory import is unimplemented (issue #47, stop 5 of docs/retail_boot_chain.md). - shims/dstorage.cpp, shims/dstorage_xs_abi.h, test/dstorage_test.cpp and docs/directstorage.md are back as those commits left them. - leonida::zlib_inflate reports why it failed through an optional ZlibError* (header, data, checksum, limit), which the shim turns into its E_DSTORAGE_ZLIB_* codes. This is the same interface the shim was written against, added to main's current table-driven inflater; callers that pass no pointer behave as before. - registry.cpp classes xcrdapi.dll!DStorageGetFactory as an HRESULT import, the retail spelling #47 found missing. - CMake and scripts/build_runtime.ps1 build the shim into gta6rt, and CMake builds and registers dstorage_test. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HxBQf4pXH8PWpC5oZsM4bt
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/shader_library.h74
1 files changed, 46 insertions, 28 deletions
diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h
index 352e54325..4df1ad5d3 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.
38enum class ZlibError { none, header, data, checksum, limit };
39
36namespace detail { 40namespace detail {
37 41
38class BitReader { 42class BitReader {
@@ -215,7 +219,7 @@ inline int decode(BitReader& in, const Huffman& h) {
215 return -1; 219 return -1;
216} 220}
217 221
218inline bool inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, const Huffman& lengths, 222inline 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.
254inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint8_t>& out, 262inline 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// ---------------------------------------------------------------------------------------------------------------