nstool/lib/libhac/source/ServiceAccessControlEntry.cpp

131 lines
2.9 KiB
C++
Raw Normal View History

2018-06-29 04:00:15 +00:00
#include <nx/ServiceAccessControlEntry.h>
2018-06-29 04:00:15 +00:00
nx::ServiceAccessControlEntry::ServiceAccessControlEntry()
{
2018-06-24 08:18:54 +00:00
clear();
}
2018-06-29 04:00:15 +00:00
nx::ServiceAccessControlEntry::ServiceAccessControlEntry(const std::string & name, bool isServer) :
mIsServer(isServer),
mName(name)
{
2018-06-24 08:18:54 +00:00
toBytes();
}
2018-06-29 04:00:15 +00:00
nx::ServiceAccessControlEntry::ServiceAccessControlEntry(const ServiceAccessControlEntry & other)
{
2018-06-24 08:18:54 +00:00
*this = other;
}
2018-06-29 04:00:15 +00:00
void nx::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-06-29 04:00:15 +00:00
bool nx::ServiceAccessControlEntry::operator==(const ServiceAccessControlEntry & other) const
{
2018-06-24 08:18:54 +00:00
return (mIsServer == other.mIsServer) \
&& (mName == other.mName);
}
2018-06-29 04:00:15 +00:00
bool nx::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-06-29 04:00:15 +00:00
void nx::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-06-29 04:00:15 +00:00
void nx::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-06-29 04:00:15 +00:00
const fnd::Vec<byte_t>& nx::ServiceAccessControlEntry::getBytes() const
2018-06-24 08:18:54 +00:00
{
return mRawBinary;
}
2018-06-29 04:00:15 +00:00
void nx::ServiceAccessControlEntry::clear()
{
mIsServer = false;
mName.clear();
}
2018-06-29 04:00:15 +00:00
bool nx::ServiceAccessControlEntry::isServer() const
{
return mIsServer;
}
2018-06-29 04:00:15 +00:00
void nx::ServiceAccessControlEntry::setIsServer(bool isServer)
{
mIsServer = isServer;
}
2018-06-29 04:00:15 +00:00
const std::string & nx::ServiceAccessControlEntry::getName() const
{
return mName;
}
2018-06-29 04:00:15 +00:00
void nx::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
}