summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/buildtool/compatibility/TARGETS7
-rw-r--r--src/buildtool/compatibility/native_support.hpp50
2 files changed, 55 insertions, 2 deletions
diff --git a/src/buildtool/compatibility/TARGETS b/src/buildtool/compatibility/TARGETS
index fbae7b48..5ef01708 100644
--- a/src/buildtool/compatibility/TARGETS
+++ b/src/buildtool/compatibility/TARGETS
@@ -1,8 +1,11 @@
{ "compatibility":
{ "type": ["@", "rules", "CC", "library"]
, "name": ["compatibility"]
- , "hdrs": ["compatibility.hpp"]
- , "deps": [["src/buildtool/crypto", "hash_function"]]
+ , "hdrs": ["compatibility.hpp", "native_support.hpp"]
+ , "deps":
+ [ ["src/buildtool/crypto", "hash_function"]
+ , ["@", "gsl-lite", "", "gsl-lite"]
+ ]
, "stage": ["src", "buildtool", "compatibility"]
}
}
diff --git a/src/buildtool/compatibility/native_support.hpp b/src/buildtool/compatibility/native_support.hpp
new file mode 100644
index 00000000..7ec28d40
--- /dev/null
+++ b/src/buildtool/compatibility/native_support.hpp
@@ -0,0 +1,50 @@
+#ifndef INCLUDED_SRC_BUILDTOOL_COMPATIBILITY_NATIVE_SUPPORT_HPP
+#define INCLUDED_SRC_BUILDTOOL_COMPATIBILITY_NATIVE_SUPPORT_HPP
+
+#include <string>
+
+#include <gsl-lite/gsl-lite.hpp>
+
+#include "src/buildtool/compatibility/compatibility.hpp"
+
+/// \brief Helper functions to support the native remote-execution protocol.
+class NativeSupport {
+ static constexpr std::size_t kTagLength = 2;
+ static constexpr std::size_t kTaggedLength = 42;
+ static constexpr auto kBlobTag = "62";
+ static constexpr auto kTreeTag = "74";
+
+ public:
+ [[nodiscard]] static auto IsPrefixed(std::string const& hash) noexcept
+ -> bool {
+ if (Compatibility::IsCompatible()) {
+ return false;
+ }
+ return hash.length() == kTaggedLength;
+ }
+
+ /// \brief Returns a prefixed hash in case of native remote-execution
+ /// protocol (0x62 in case of a blob, 0x74 in case of a tree).
+ [[nodiscard]] static auto Prefix(std::string const& hash,
+ bool is_tree) noexcept -> std::string {
+ if (Compatibility::IsCompatible()) {
+ return hash;
+ }
+ gsl_ExpectsAudit(not IsPrefixed(hash));
+ return (is_tree ? kTreeTag : kBlobTag) + hash;
+ }
+
+ [[nodiscard]] static auto Unprefix(std::string const& hash) noexcept
+ -> std::string {
+ if (Compatibility::IsCompatible()) {
+ return hash;
+ }
+ gsl_ExpectsAudit(IsPrefixed(hash));
+ return hash.substr(kTagLength);
+ }
+
+ [[nodiscard]] static auto IsTree(std::string const& hash) noexcept -> bool {
+ return IsPrefixed(hash) and hash.starts_with(kTreeTag);
+ }
+};
+#endif // INCLUDED_SRC_BUILDTOOL_COMPATIBILITY_NATIVE_SUPPORT_HPP