-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSafeTrackName.cpp
More file actions
152 lines (132 loc) · 3.89 KB
/
Copy pathSafeTrackName.cpp
File metadata and controls
152 lines (132 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) OpenMOQ contributors.
* This source code is licensed under the Apache 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "SafeTrackName.h"
#include <charconv>
#include <vector>
namespace openmoq::moqx {
namespace {
constexpr char kHexDigits[] = "0123456789abcdef";
bool passesThrough(unsigned char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
}
// from_chars neither skips whitespace nor accepts a sign for unsigned types,
// so requiring it to consume both digits is the whole validation.
std::optional<char> decodeHexPair(std::string_view pair) {
uint32_t value = 0;
auto [ptr, ec] = std::from_chars(pair.data(), pair.data() + pair.size(), value, 16);
if (ec != std::errc{} || ptr != pair.data() + pair.size()) {
return std::nullopt;
}
return static_cast<char>(value);
}
// Unencoded length, including the '-' between tuples. A lower bound on the
// result: any byte outside the pass-through set expands to three.
size_t rawSize(const moxygen::TrackNamespace& ns) {
size_t size = ns.trackNamespace.empty() ? 0 : ns.trackNamespace.size() - 1;
for (const auto& tuple : ns.trackNamespace) {
size += tuple.size();
}
return size;
}
void appendSafe(std::string& out, std::string_view bytes) {
for (char ch : bytes) {
auto c = static_cast<unsigned char>(ch);
if (passesThrough(c)) {
out += ch;
continue;
}
out += '.';
out += kHexDigits[c >> 4];
out += kHexDigits[c & 0x0f];
}
}
} // namespace
std::string safeName(std::string_view bytes) {
std::string out;
out.reserve(bytes.size());
appendSafe(out, bytes);
return out;
}
namespace {
void appendSafe(std::string& out, const moxygen::TrackNamespace& ns) {
bool first = true;
for (const auto& tuple : ns.trackNamespace) {
if (!first) {
out += '-';
}
first = false;
appendSafe(out, tuple);
}
}
} // namespace
std::string safeName(const moxygen::TrackNamespace& ns) {
std::string out;
out.reserve(rawSize(ns));
appendSafe(out, ns);
return out;
}
std::string safeName(const moxygen::FullTrackName& ftn) {
std::string out;
out.reserve(rawSize(ftn.trackNamespace) + 2 + ftn.trackName.size());
appendSafe(out, ftn.trackNamespace);
out += "--";
appendSafe(out, ftn.trackName);
return out;
}
std::optional<std::string> parseSafeBytes(std::string_view text) {
std::string out;
out.reserve(text.size());
for (size_t i = 0; i < text.size(); ++i) {
auto c = static_cast<unsigned char>(text[i]);
if (passesThrough(c)) {
out += text[i];
continue;
}
if (c != '.' || i + 2 >= text.size()) {
return std::nullopt;
}
auto byte = decodeHexPair(text.substr(i + 1, 2));
if (!byte) {
return std::nullopt;
}
out += *byte;
i += 2;
}
return out;
}
std::optional<moxygen::TrackNamespace> parseSafeNamespace(std::string_view text) {
std::vector<std::string> tuples;
if (!text.empty()) {
size_t start = 0;
while (start <= text.size()) {
auto end = text.find('-', start);
auto piece = text.substr(start, end == std::string_view::npos ? end : end - start);
auto decoded = parseSafeBytes(piece);
if (!decoded) {
return std::nullopt;
}
tuples.push_back(std::move(*decoded));
if (end == std::string_view::npos) {
break;
}
start = end + 1;
}
}
return moxygen::TrackNamespace(std::move(tuples));
}
std::optional<moxygen::FullTrackName> parseSafeFullTrackName(std::string_view text) {
auto split = text.rfind("--");
if (split == std::string_view::npos) {
return std::nullopt;
}
auto ns = parseSafeNamespace(text.substr(0, split));
auto track = parseSafeBytes(text.substr(split + 2));
if (!ns || !track) {
return std::nullopt;
}
return moxygen::FullTrackName{std::move(*ns), std::move(*track)};
}
} // namespace openmoq::moqx