Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 1 | #include "utils/dbus_path_utils.hpp" |
| 2 | |
| 3 | namespace utils |
| 4 | { |
| 5 | sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path, |
| 6 | const std::string& appended) |
| 7 | { |
| 8 | if (appended.starts_with('/') || !isValidDbusPath(appended)) |
| 9 | { |
| 10 | throw sdbusplus::exception::SdBusError( |
| 11 | static_cast<int>(std::errc::invalid_argument), |
| 12 | "Invalid appended string"); |
| 13 | } |
| 14 | |
| 15 | size_t pos_start = 0; |
| 16 | size_t pos_end = 0; |
| 17 | while ((pos_end = appended.find('/', pos_start)) != std::string::npos) |
| 18 | { |
| 19 | if (pos_start == pos_end) |
| 20 | { |
| 21 | throw sdbusplus::exception::SdBusError( |
| 22 | static_cast<int>(std::errc::invalid_argument), |
| 23 | "Invalid appended string"); |
| 24 | } |
| 25 | path /= std::string_view(appended.begin() + pos_start, |
| 26 | appended.begin() + pos_end); |
| 27 | pos_start = pos_end + 1; |
| 28 | } |
| 29 | path /= std::string_view(appended.begin() + pos_start, appended.end()); |
| 30 | return path; |
| 31 | } |
| 32 | |
| 33 | std::string reportPathToId(const sdbusplus::message::object_path& path) |
| 34 | { |
| 35 | if (path.str.starts_with(constants::reportDirStr)) |
| 36 | { |
| 37 | auto id = path.str.substr(constants::reportDirStr.length()); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 38 | verifyIdPrefixes(id); |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 39 | return id; |
| 40 | } |
| 41 | throw sdbusplus::exception::SdBusError( |
| 42 | static_cast<int>(std::errc::invalid_argument), "Invalid path prefix"); |
| 43 | } |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 44 | |
| 45 | void verifyIdPrefixes(std::string_view id) |
| 46 | { |
| 47 | size_t pos_start = 0; |
| 48 | size_t pos_end = 0; |
| 49 | size_t prefix_cnt = 0; |
| 50 | while ((pos_end = id.find('/', pos_start)) != std::string::npos) |
| 51 | { |
| 52 | if (pos_start == pos_end) |
| 53 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame^] | 54 | throw errors::InvalidArgument("Id", "Invalid prefixes in id."); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (++prefix_cnt > constants::maxPrefixesInId) |
| 58 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame^] | 59 | throw errors::InvalidArgument("Id", "Too many prefixes."); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (pos_end - pos_start > constants::maxPrefixLength) |
| 63 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame^] | 64 | throw errors::InvalidArgument("Id", "Prefix too long."); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | pos_start = pos_end + 1; |
| 68 | } |
| 69 | |
| 70 | if (id.length() - pos_start > constants::maxIdNameLength) |
| 71 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame^] | 72 | throw errors::InvalidArgument("Id", "Too long."); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 73 | } |
| 74 | } |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 75 | } // namespace utils |