20 #include <boost/asio/ip/address.hpp>
21 #include <boost/system/error_code.hpp>
22 #include <string_view>
58 if (!is_valid_device_path(device)) {
67 std::string lower_parity = parity;
68 std::transform(lower_parity.begin(), lower_parity.end(), lower_parity.begin(),
69 [](
unsigned char c) { return std::tolower(c); });
71 if (lower_parity !=
"none" && lower_parity !=
"odd" && lower_parity !=
"even") {
96 if (address.empty())
return false;
101 boost::system::error_code ec;
104 std::string addr_str(address);
105 auto ip = boost::asio::ip::make_address_v4(addr_str, ec);
117 if (ip.to_string() != addr_str) {
128 if (address.find(
'[') != std::string::npos || address.find(
']') != std::string::npos) {
132 boost::system::error_code ec;
133 boost::asio::ip::make_address_v6(address, ec);
149 if (hostname.front() ==
'-' || hostname.back() ==
'-') {
157 while ((end = hostname.find(
'.', start)) != std::string_view::npos) {
158 std::string_view label = hostname.substr(start, end - start);
160 if (label.empty() || label.length() > 63) {
164 if (label.front() ==
'-' || label.back() ==
'-') {
169 for (
char c : label) {
170 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'-') {
178 std::string_view label = hostname.substr(start);
179 if (label.empty() || label.length() > 63) {
183 if (label.front() ==
'-' || label.back() ==
'-') {
187 for (
char c : label) {
188 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'-') {
196 bool InputValidator::is_valid_device_path(
const std::string& device) {
202 if (device.empty()) {
208 if (device.length() >= 5 && device.substr(0, 5) ==
"/dev/") {
210 for (
char c : device) {
211 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'/' && c !=
'_' && c !=
'-') {
219 if (device.length() >= 4 && device.substr(0, 3) ==
"COM") {
220 std::string port_num = device.substr(3);
223 if (port_num.empty() ||
224 !std::all_of(port_num.begin(), port_num.end(), [](
unsigned char c) { return std::isdigit(c); })) {
229 int port = std::stoi(port_num);
230 return port >= 1 && port <= 255;
231 }
catch (
const std::exception&) {
237 if (device ==
"NUL" || device ==
"CON" || device ==
"PRN" || device ==
"AUX" || device ==
"LPT1" ||
238 device ==
"LPT2" || device ==
"LPT3") {
constexpr size_t MAX_DEVICE_PATH_LENGTH
constexpr size_t MAX_HOSTNAME_LENGTH
diagnostics::ValidationException ValidationException