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 <iostream>
10#include <vector>
11
12#include <absl/flags/flag.h>
13#include <absl/flags/parse.h>
14#include <absl/flags/usage.h>
15#include <absl/strings/ascii.h>
16#include <absl/strings/escaping.h>
17#include <absl/strings/str_format.h>
18#include <absl/strings/str_join.h>
19#include <absl/strings/str_split.h>
20
21#include <packager/utils/hex_parser.h>
22
23namespace shaka {
24
25// Custom flag parser for HexBytes
26bool AbslParseFlag(absl::string_view text, HexBytes* flag, std::string* error) {
27 std::string hexString(text);
28 absl::RemoveExtraAsciiWhitespace(&hexString);
29
30 if (hexString.empty()) {
31 flag->bytes = std::vector<uint8_t>(0, 0);
32 return true;
33 }
34
35 std::string bytesRaw;
36 if (!shaka::ValidHexStringToBytes(hexString, &bytesRaw)) {
37 *error = "Invalid hex string";
38 return false;
39 }
40
41 absl::string_view hex_str_view(bytesRaw);
42 absl::Span<const uint8_t> span(
43 reinterpret_cast<const uint8_t*>(hex_str_view.data()),
44 hex_str_view.size());
45 flag->bytes = std::vector<uint8_t>(span.begin(), span.end());
46
47 return true;
48}
49
50std::string AbslUnparseFlag(const HexBytes& flag) {
51 std::string result;
52 for (const auto byte : flag.bytes) {
53 absl::StrAppendFormat(&result, "%02x", byte);
54 }
55 return result;
56}
57
58} // namespace shaka
All the methods that are virtual are virtual for mocking.