blob: 5c1fddbd83def4b3dc181f1618cc6ee5bd4589fd [file] [log] [blame]
#include "utils/dbus_path_utils.hpp"
namespace utils
{
sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path,
const std::string& appended)
{
if (appended.starts_with('/') || !isValidDbusPath(appended))
{
throw sdbusplus::exception::SdBusError(
static_cast<int>(std::errc::invalid_argument),
"Invalid appended string");
}
size_t pos_start = 0;
size_t pos_end = 0;
while ((pos_end = appended.find('/', pos_start)) != std::string::npos)
{
if (pos_start == pos_end)
{
throw sdbusplus::exception::SdBusError(
static_cast<int>(std::errc::invalid_argument),
"Invalid appended string");
}
path /= std::string_view(appended.begin() + pos_start,
appended.begin() + pos_end);
pos_start = pos_end + 1;
}
path /= std::string_view(appended.begin() + pos_start, appended.end());
return path;
}
std::string reportPathToId(const sdbusplus::message::object_path& path)
{
if (path.str.starts_with(constants::reportDirStr))
{
auto id = path.str.substr(constants::reportDirStr.length());
verifyIdPrefixes(id);
return id;
}
throw sdbusplus::exception::SdBusError(
static_cast<int>(std::errc::invalid_argument), "Invalid path prefix");
}
void verifyIdPrefixes(std::string_view id)
{
size_t pos_start = 0;
size_t pos_end = 0;
size_t prefix_cnt = 0;
while ((pos_end = id.find('/', pos_start)) != std::string::npos)
{
if (pos_start == pos_end)
{
throw sdbusplus::exception::SdBusError(
static_cast<int>(std::errc::invalid_argument),
"Invalid prefixes in id");
}
if (++prefix_cnt > constants::maxPrefixesInId)
{
throw sdbusplus::exception::SdBusError(
static_cast<int>(std::errc::invalid_argument),
"Too many prefixes");
}
if (pos_end - pos_start > constants::maxPrefixLength)
{
throw sdbusplus::exception::SdBusError(
static_cast<int>(std::errc::invalid_argument),
"Prefix too long");
}
pos_start = pos_end + 1;
}
if (id.length() - pos_start > constants::maxIdNameLength)
{
throw sdbusplus::exception::SdBusError(
static_cast<int>(std::errc::invalid_argument), "Id too long");
}
}
} // namespace utils