|
27 | 27 | #define READ_LE32(arr, i) \ |
28 | 28 | (((uint32_t)arr[i + 0] << 0) | ((uint32_t)arr[i + 1] << 8) | ((uint32_t)arr[i + 2] << 16) | ((uint32_t)arr[i + 3] << 24)) |
29 | 29 |
|
| 30 | +static constexpr uint8_t SAR_MSG_VSCRIPT_RUNTIME_CHECKSUM = 0x14; |
| 31 | + |
30 | 32 | // clang-format off |
31 | 33 | static const uint32_t crcTable[256] = { |
32 | 34 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, |
@@ -560,6 +562,40 @@ static void addVpkInternalChecksum(const VpkInternalData &vpk) { |
560 | 562 | engine->demorecorder->RecordData(data.data(), data.size()); |
561 | 563 | } |
562 | 564 |
|
| 565 | +static size_t BoundedCStringLen(const char *str, size_t maxLen) { |
| 566 | + if (!str) return 0; |
| 567 | + size_t len = 0; |
| 568 | + while (len < maxLen && str[len]) ++len; |
| 569 | + return len; |
| 570 | +} |
| 571 | + |
| 572 | +void RecordRuntimeVscriptChecksum(const char *scriptName, const char *scriptData) { |
| 573 | + if (!scriptName || !*scriptName || !scriptData || !*scriptData) return; |
| 574 | + |
| 575 | + // Avoid an unbounded C-string search in case file data is not null-terminated. |
| 576 | + constexpr size_t MAX_SCRIPT_SIZE = 8 * 1024 * 1024; |
| 577 | + size_t scriptLen = BoundedCStringLen(scriptData, MAX_SCRIPT_SIZE); |
| 578 | + if (scriptLen == 0) return; |
| 579 | + |
| 580 | + uint32_t sum = 0; |
| 581 | + if (scriptLen < MAX_SCRIPT_SIZE) { |
| 582 | + sum = crc32(scriptData, scriptLen); |
| 583 | + } |
| 584 | + |
| 585 | + if (engine->demorecorder->isRecordingDemo && engine->demorecorder->GetTick() >= 0) { |
| 586 | + size_t nameLen = strlen(scriptName); |
| 587 | + size_t bufLen = nameLen + 6; |
| 588 | + auto *buf = new uint8_t[bufLen]; |
| 589 | + buf[0] = SAR_MSG_VSCRIPT_RUNTIME_CHECKSUM; |
| 590 | + *reinterpret_cast<uint32_t *>(buf + 1) = sum; |
| 591 | + strcpy(reinterpret_cast<char *>(buf + 5), scriptName); |
| 592 | + engine->demorecorder->RecordData(buf, bufLen); |
| 593 | + delete[] buf; |
| 594 | + } else { |
| 595 | + engine->demorecorder->queuedVScriptChecksums.emplace_back(scriptName, sum); |
| 596 | + } |
| 597 | +} |
| 598 | + |
563 | 599 | void AddDemoVpkChecksums() { |
564 | 600 | if (g_vpkThread.joinable()) g_vpkThread.join(); |
565 | 601 |
|
|
0 commit comments