Shaka Packager SDK
Loading...
Searching...
No Matches
absl_flag_hexbytes.cc
1// Copyright 2023 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/utils/absl_flag_hexbytes.h>
8
9#include <cstdint>
10#include <string>
11#include <vector>
12
13#include <absl/strings/ascii.h>
14#include <absl/strings/str_format.h>
15#include <absl/strings/string_view.h>
16#include <absl/types/span.h>
17
18#include <packager/utils/hex_parser.h>
19
20namespace shaka {
21
22// Custom flag parser for HexBytes
23bool AbslParseFlag(absl::string_view text, HexBytes* flag, std::string* error) {
24 std::string hexString(text);
25 absl::RemoveExtraAsciiWhitespace(&hexString);
26
27 if (hexString.empty()) {
28 flag->bytes = std::vector<uint8_t>(0, 0);
29 return true;
30 }
31
32 std::string bytesRaw;
33 if (!shaka::ValidHexStringToBytes(hexString, &bytesRaw)) {
34 *error = "Invalid hex string";
35 return false;
36 }
37
38 absl::string_view hex_str_view(bytesRaw);
39 absl::Span<const uint8_t> span(
40 reinterpret_cast<const uint8_t*>(hex_str_view.data()),
41 hex_str_view.size());
42 flag->bytes = std::vector<uint8_t>(span.begin(), span.end());
43
44 return true;
45}
46
47std::string AbslUnparseFlag(const HexBytes& flag) {
48 std::string result;
49 for (const auto byte : flag.bytes) {
50 absl::StrAppendFormat(&result, "%02x", byte);
51 }
52 return result;
53}
54
55} // namespace shaka
All the methods that are virtual are virtual for mocking.