9 constexpr std::size_t MAX_COMPONENT_LENGTH = 255;
10 constexpr std::size_t MAX_ENTRY_PATH_LENGTH = 1024;
12 bool is_control_character(
unsigned char c)
14 return c < 0x20 || c == 0x7F;
17 bool is_windows_reserved_device_name(
const std::string& component)
22 const auto stem = component.substr(0, component.find(
'.'));
25 upper.reserve(stem.size());
26 for (
const auto c : stem)
28 upper.push_back(c >=
'a' && c <=
'z' ?
static_cast<char>(c -
'a' +
'A') : c);
31 if (upper ==
"CON" || upper ==
"PRN" || upper ==
"AUX" || upper ==
"NUL")
35 if ((upper.size() == 4) &&
36 (upper.compare(0, 3,
"COM") == 0 || upper.compare(0, 3,
"LPT") == 0) &&
37 (upper[3] >=
'1' && upper[3] <=
'9'))
49 bool is_safe_path_component(
const std::string& component)
51 if (component.empty())
55 if (component ==
"." || component ==
"..")
59 const auto last = component.back();
60 if (last ==
'.' || last ==
' ')
64 if (is_windows_reserved_device_name(component))
74 if (name.empty() || name.size() > MAX_COMPONENT_LENGTH)
79 for (
const auto c : name)
81 const auto uc =
static_cast<unsigned char>(c);
82 if (is_control_character(uc))
100 return is_safe_path_component(name);
105 if (name.empty() || name.size() > MAX_ENTRY_PATH_LENGTH)
115 while (!trimmed.empty() && (trimmed.back() ==
'/' || trimmed.back() ==
'\\'))
125 if (trimmed.front() ==
'/' || trimmed.front() ==
'\\')
130 for (
const auto c : trimmed)
132 const auto uc =
static_cast<unsigned char>(c);
133 if (is_control_character(uc))
147 std::size_t begin = 0;
148 for (std::size_t i = 0; i <= trimmed.size(); ++i)
150 const bool atSeparator = (i == trimmed.size()) || (trimmed[i] ==
'/') || (trimmed[i] ==
'\\');
156 if (!is_safe_path_component(trimmed.substr(begin, i - begin)))
bool is_safe_filename_component(const std::string &name)
bool is_safe_relative_entry_path(const std::string &name)