// Copyright 2022 Google LLC. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd #include #include #include namespace shaka { void HexStringToBytes(const std::string& hex, std::vector* bytes) { std::string raw = absl::HexStringToBytes(hex); absl::string_view str_view(raw); absl::Span span( reinterpret_cast(str_view.data()), str_view.size()); *bytes = std::vector(span.begin(), span.end()); } bool ValidHexStringToBytes(const std::string& hex, std::vector* bytes) { std::string raw; if (!ValidHexStringToBytes(hex, &raw)) return false; absl::string_view str_view(raw); absl::Span span( reinterpret_cast(str_view.data()), str_view.size()); *bytes = std::vector(span.begin(), span.end()); return true; } bool ValidHexStringToBytes(const std::string& hex, std::string* bytes) { // absl::HexStringToBytes will not validate the string! Any invalid byte // sequence will be converted into NUL characters silently. So we do our own // validation here. for (char c : hex) { c = static_cast(std::tolower(c)); if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { // valid } else { return false; } } *bytes = absl::HexStringToBytes(hex); return true; } } // namespace shaka