nstool/lib/libhac/source/ServiceAccessControlEntry.cpp

132 lines
3 KiB
C++
Raw Permalink Normal View History

2018-08-07 09:31:06 +00:00
#include <cstring>
2018-08-07 07:17:51 +00:00
#include <nn/hac/ServiceAccessControlEntry.h>
2018-08-07 07:17:51 +00:00
nn::hac::ServiceAccessControlEntry::ServiceAccessControlEntry()
{
2018-06-24 08:18:54 +00:00
clear();
}
2018-08-07 07:17:51 +00:00
nn::hac::ServiceAccessControlEntry::ServiceAccessControlEntry(const std::string & name, bool isServer) :
mIsServer(isServer),
mName(name)
{
2018-06-24 08:18:54 +00:00
toBytes();
}
2018-08-07 07:17:51 +00:00
nn::hac::ServiceAccessControlEntry::ServiceAccessControlEntry(const ServiceAccessControlEntry & other)
{
2018-06-24 08:18:54 +00:00
*this = other;
}
2018-08-07 07:17:51 +00:00
void nn::hac::ServiceAccessControlEntry::operator=(const ServiceAccessControlEntry & other)
{
2018-06-24 08:18:54 +00:00
if (other.getBytes().size())
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
this->mIsServer = other.mIsServer;
this->mName = other.mName;
}
}
2018-08-07 07:17:51 +00:00
bool nn::hac::ServiceAccessControlEntry::operator==(const ServiceAccessControlEntry & other) const
{
2018-06-24 08:18:54 +00:00
return (mIsServer == other.mIsServer) \
&& (mName == other.mName);
}
2018-08-07 07:17:51 +00:00
bool nn::hac::ServiceAccessControlEntry::operator!=(const ServiceAccessControlEntry & other) const
{
2018-06-24 08:18:54 +00:00
return !(*this == other);
}
2018-06-24 08:18:54 +00:00
2018-08-07 07:17:51 +00:00
void nn::hac::ServiceAccessControlEntry::toBytes()
{
try {
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(mName.size() + 1);
}
catch (const fnd::Exception& e)
{
2018-06-29 04:00:15 +00:00
throw fnd::Exception(kModuleName, "Failed to allocate memory for ServiceAccessControlEntry: " + std::string(e.what()));
}
if (mName.length() == 0)
{
throw fnd::Exception(kModuleName, "Service name is empty");
}
if (mName.length() > kMaxServiceNameLen)
{
throw fnd::Exception(kModuleName, "Service name string too long (max 8 chars)");
}
// copy data into binary blob
2018-06-24 08:18:54 +00:00
mRawBinary[0] = (mIsServer ? SAC_IS_SERVER : 0) | ((mName.length()-1) & SAC_NAME_LEN_MASK); // bug?
memcpy(mRawBinary.data() + 1, mName.c_str(), mName.length());
}
2018-08-07 07:17:51 +00:00
void nn::hac::ServiceAccessControlEntry::fromBytes(const byte_t* data, size_t len)
{
2018-06-24 08:18:54 +00:00
bool isServer = (data[0] & SAC_IS_SERVER) == SAC_IS_SERVER;
size_t nameLen = (data[0] & SAC_NAME_LEN_MASK) + 1; // bug?
if (nameLen+1 > len)
{
throw fnd::Exception(kModuleName, "SAC entry is too small");
}
if (nameLen == 0)
{
throw fnd::Exception(kModuleName, "SAC entry has no service name");
}
else if (nameLen > kMaxServiceNameLen)
{
throw fnd::Exception(kModuleName, "Service name string too long (max 8 chars)");
}
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(nameLen + 1);
memcpy(mRawBinary.data(), data, mRawBinary.size());
mIsServer = isServer;
2018-06-24 08:18:54 +00:00
mName = std::string((const char*)(mRawBinary.data() + 1), nameLen);
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::hac::ServiceAccessControlEntry::getBytes() const
2018-06-24 08:18:54 +00:00
{
return mRawBinary;
}
2018-08-07 07:17:51 +00:00
void nn::hac::ServiceAccessControlEntry::clear()
{
mIsServer = false;
mName.clear();
}
2018-08-07 07:17:51 +00:00
bool nn::hac::ServiceAccessControlEntry::isServer() const
{
return mIsServer;
}
2018-08-07 07:17:51 +00:00
void nn::hac::ServiceAccessControlEntry::setIsServer(bool isServer)
{
mIsServer = isServer;
}
2018-08-07 07:17:51 +00:00
const std::string & nn::hac::ServiceAccessControlEntry::getName() const
{
return mName;
}
2018-08-07 07:17:51 +00:00
void nn::hac::ServiceAccessControlEntry::setName(const std::string & name)
{
if (name.length() > kMaxServiceNameLen)
{
throw fnd::Exception(kModuleName, "Service name string too long (max 8 chars)");
}
mName = name;
2018-06-24 08:18:54 +00:00
}