#include "core/CacheCheck.h" #include #include #include #include #include #include // xxHash 0.8.3 (BSD-2-Clause, third_party/xxhash, unmodified), compiled into this file only #define XXH_INLINE_ALL #define XXH_STATIC_LINKING_ONLY #include "../../third_party/xxhash/xxhash.h" namespace fs = std::filesystem; namespace lynx::cachecheck { std::uint64_t hash (const void* data, std::size_t bytes) noexcept { return (std::uint64_t) XXH3_64bits (data, bytes); } struct Hasher::State { XXH3_state_t s; }; Hasher::Hasher() : state (std::make_unique()) { XXH3_64bits_reset (&state->s); } Hasher::~Hasher() = default; void Hasher::add (const void* data, std::size_t bytes) noexcept { if (bytes > 0) XXH3_64bits_update (&state->s, data, bytes); } std::uint64_t Hasher::value() const noexcept { return (std::uint64_t) XXH3_64bits_digest (&state->s); } const char* describe (Verdict v) noexcept { switch (v) { case Verdict::ok: return "ok"; case Verdict::oldFormat: return "an older format without a check"; case Verdict::truncated: return "its size does not match: cut short"; case Verdict::mismatch: return "its content hash does not match: a changed byte"; case Verdict::unreadable: return "not a cache file"; } return "?"; } // --- the log -------------------------------------------------------------------------------------------------------- namespace { std::mutex logLock; std::string logFile, lastLine; std::atomic rebuilt { 0 }; fs::path u8path (const std::string& s) { return fs::path ((const char8_t*) s.c_str()); } template void put (char* at, const T& v) noexcept { std::memcpy (at, &v, sizeof (T)); } template T get (const char* at) noexcept { T v {}; std::memcpy (&v, at, sizeof (T)); return v; } } // namespace void setLogFile (const std::string& path) { const std::lock_guard l (logLock); logFile = path; } namespace { void logLine (const std::string& line); } void reportRebuilt (const std::string& file, Verdict why) { logLine ("cache file " + file + " failed its check (" + describe (why) + ") and was rebuilt"); } void reportDamaged (const std::string& file, Verdict why, const std::string& outcome) { logLine ("cache file " + file + " was damaged (" + describe (why) + "); " + outcome); } namespace { void logLine (const std::string& line) { ++rebuilt; std::fprintf (stderr, "LYNX: %s\n", line.c_str()); const std::lock_guard l (logLock); lastLine = line; if (logFile.empty()) return; std::error_code ec; const auto path = u8path (logFile); if (fs::exists (path, ec) && fs::file_size (path, ec) > 64 * 1024 && ! ec) { // (kept small: the newest half) std::string text; { std::ifstream in (path, std::ios::binary); text.assign (std::istreambuf_iterator (in), {}); } const auto cut = text.find ('\n', text.size() / 2); std::ofstream out (path, std::ios::binary | std::ios::trunc); if (cut != std::string::npos) out << text.substr (cut + 1); } std::ofstream out (path, std::ios::binary | std::ios::app); out << line << "\n"; } } // namespace int rebuiltCount() noexcept { return rebuilt.load(); } std::string lastReport() { const std::lock_guard l (logLock); return lastLine; } // --- *.str ---------------------------------------------------------------------------------------------------------- namespace str { namespace { std::uint64_t hashOf (const char* header, const void* payload, std::size_t payloadBytes, const void* second = nullptr, std::size_t secondBytes = 0) noexcept { char zeroed[headerBytes]; std::memcpy (zeroed, header, headerBytes); put (zeroed + 32, 0); Hasher h; h.add (zeroed, headerBytes); h.add (payload, payloadBytes); if (second != nullptr) h.add (second, secondBytes); return h.value(); } } // namespace void makeHeader (char* header, const Info& info, const float* left, const float* right) noexcept { std::memset (header, 0, headerBytes); std::memcpy (header, magic, 8); put (header + 8, info.sampleRate); put (header + 16, info.frames); put (header + 24, info.channels); const auto channelBytes = (std::size_t) info.frames * sizeof (float); const auto h = hashOf (header, left, channelBytes, info.channels == 2 ? right : nullptr, info.channels == 2 ? channelBytes : 0); put (header + 32, h); } Verdict readHeader (const void* data, std::size_t available, std::uint64_t fileBytes, Info& info) noexcept { const auto* d = static_cast (data); if (available < 8) return Verdict::truncated; if (std::memcmp (d, magicV1, 8) == 0) return Verdict::oldFormat; if (available < headerBytes || std::memcmp (d, magic, 8) != 0) return std::memcmp (d, magic, 8) == 0 ? Verdict::truncated : Verdict::unreadable; info.sampleRate = get (d + 8); info.frames = get (d + 16); info.channels = get (d + 24); // (a changed bit in the header's shape is a mismatch: the hash would say so too, but the size below must be sane first) if (info.frames <= 0 || info.frames > ((std::int64_t) 1 << 40) || ! (info.sampleRate > 0.0 && info.sampleRate < 1.0e7) || (info.channels != 1 && info.channels != 2 && info.channels != stereoSame) || get (d + 28) != 0) return Verdict::mismatch; for (std::size_t i = 40; i < headerBytes; ++i) if (d[i] != 0) return Verdict::mismatch; if (fileBytes != headerBytes + info.payloadBytes()) return Verdict::truncated; return Verdict::ok; } Verdict verify (const void* file, std::size_t bytes, Info& info) noexcept { const auto shape = readHeader (file, bytes, bytes, info); if (shape != Verdict::ok) return shape; char header[headerBytes]; std::memcpy (header, file, headerBytes); const auto stored = get (header + 32); return hashOf (header, static_cast (file) + headerBytes, (std::size_t) info.payloadBytes()) == stored ? Verdict::ok : Verdict::mismatch; } } // namespace str // --- *.pcm ---------------------------------------------------------------------------------------------------------- namespace pcm { void makeHeaderStart (char* header, double sampleRate, std::int64_t frames) noexcept { std::memset (header, 0, headerBytes); std::memcpy (header, magic, 8); put (header + 8, sampleRate); put (header + 16, frames); } Verdict verify (const void* file, std::size_t bytes, double sampleRate, std::int64_t frames, bool content) noexcept { const auto* d = static_cast (file); if (bytes < 8) return Verdict::truncated; if (std::memcmp (d, magicV1, 8) == 0) return Verdict::oldFormat; if (bytes < headerBytes || std::memcmp (d, magic, 8) != 0) return std::memcmp (d, magic, 8) == 0 ? Verdict::truncated : Verdict::unreadable; // (the rate and length are the source file's, from its reader: a header that says otherwise is not this sample's) if (get (d + 8) != sampleRate || get (d + 16) != frames) return Verdict::mismatch; if (frames < 0 || (std::uint64_t) bytes != headerBytes + (std::uint64_t) frames * sizeof (float)) return Verdict::truncated; if (! content) return Verdict::ok; Hasher h; h.add (d, hashOffset); h.add (d + headerBytes, bytes - headerBytes); return h.value() == get (d + hashOffset) ? Verdict::ok : Verdict::mismatch; } } // namespace pcm // --- text ----------------------------------------------------------------------------------------------------------- namespace { constexpr std::string_view sealPrefix = "lynx-cache-check xxh3 "; std::string hex16 (std::uint64_t v) { static const char* digits = "0123456789abcdef"; std::string s (16, '0'); for (int i = 15; i >= 0; --i, v >>= 4) s[(std::size_t) i] = digits[v & 15]; return s; } } // namespace std::string sealText (std::string_view body) { std::string out (body); if (! out.empty() && out.back() != '\n') out += '\n'; const auto h = hash (out.data(), out.size()); out += sealPrefix; out += hex16 (h); out += '\n'; return out; } Verdict unsealText (std::string_view text, std::string_view& body) { body = {}; if (text.empty() || text.back() != '\n') return Verdict::oldFormat; const auto lineStart = text.rfind ('\n', text.size() - 2); const auto start = lineStart == std::string_view::npos ? 0 : lineStart + 1; const auto line = text.substr (start, text.size() - 1 - start); if (line.size() != sealPrefix.size() + 16 || line.substr (0, sealPrefix.size()) != sealPrefix) return Verdict::oldFormat; const auto content = text.substr (0, start); if (hex16 (hash (content.data(), content.size())) != line.substr (sealPrefix.size())) return Verdict::mismatch; body = content; return Verdict::ok; } std::string sidecarText (const void* data, std::size_t bytes) { return std::string (sealPrefix) + hex16 (hash (data, bytes)) + " " + std::to_string ((unsigned long long) bytes) + "\n"; } Verdict sidecarMatches (std::string_view sidecar, const void* data, std::size_t bytes) { if (sidecar.substr (0, sealPrefix.size()) != sealPrefix) return Verdict::oldFormat; const auto expected = sidecarText (data, bytes); if (sidecar == expected) return Verdict::ok; // (the size first: a file cut short says so) const auto space = sidecar.find (' ', sealPrefix.size()); if (space != std::string_view::npos && sidecar.substr (space + 1) != expected.substr (expected.find (' ', sealPrefix.size()) + 1)) return Verdict::truncated; return Verdict::mismatch; } } // namespace lynx::cachecheck