2017-08-05 13:09:50 +00:00
|
|
|
#include <nx/SacEntry.h>
|
2017-07-06 03:30:27 +00:00
|
|
|
|
2017-07-06 11:17:21 +00:00
|
|
|
using namespace nx;
|
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
SacEntry::SacEntry() :
|
|
|
|
mIsServer(false),
|
|
|
|
mName("")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
SacEntry::SacEntry(const std::string & name, bool isServer) :
|
|
|
|
mIsServer(isServer),
|
|
|
|
mName(name)
|
|
|
|
{
|
|
|
|
exportBinary();
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
SacEntry::SacEntry(const SacEntry & other)
|
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
copyFrom(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SacEntry::operator==(const SacEntry & other) const
|
|
|
|
{
|
|
|
|
return isEqual(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SacEntry::operator!=(const SacEntry & other) const
|
|
|
|
{
|
|
|
|
return !isEqual(other);
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
void SacEntry::operator=(const SacEntry & other)
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
copyFrom(other);
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
const byte_t * SacEntry::getBytes() const
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
|
|
|
return mBinaryBlob.getBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SacEntry::getSize() const
|
|
|
|
{
|
|
|
|
return mBinaryBlob.getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SacEntry::exportBinary()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
mBinaryBlob.alloc(mName.size() + 1);
|
|
|
|
}
|
|
|
|
catch (const fnd::Exception& e)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Failed to allocate memory for SacEntry: " + std::string(e.what()));
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
if (mName.length() == 0)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Service name is empty");
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
if (mName.length() > kMaxServiceNameLen)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Service name string too long (max 8 chars)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy data into binary blob
|
2017-07-15 08:28:01 +00:00
|
|
|
mBinaryBlob[0] = (mIsServer ? SAC_IS_SERVER : 0) | ((mName.length()-1) & SAC_NAME_LEN_MASK); // bug?
|
2017-07-06 03:30:27 +00:00
|
|
|
memcpy(mBinaryBlob.getBytes() + 1, mName.c_str(), mName.length());
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
void SacEntry::importBinary(const byte_t * bytes, size_t len)
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
|
|
|
bool isServer = (bytes[0] & SAC_IS_SERVER) == SAC_IS_SERVER;
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t nameLen = (bytes[0] & SAC_NAME_LEN_MASK) + 1; // bug?
|
2017-07-06 10:55:58 +00:00
|
|
|
|
|
|
|
if (nameLen+1 > len)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "SAC entry is too small");
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
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)");
|
|
|
|
}
|
|
|
|
|
|
|
|
mBinaryBlob.alloc(nameLen + 1);
|
|
|
|
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
|
|
|
|
|
|
|
|
mIsServer = isServer;
|
|
|
|
mName = std::string((const char*)(mBinaryBlob.getBytes() + 1), nameLen);
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::SacEntry::clear()
|
|
|
|
{
|
|
|
|
mIsServer = false;
|
|
|
|
mName.clear();
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
bool SacEntry::isServer() const
|
|
|
|
{
|
|
|
|
return mIsServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SacEntry::setIsServer(bool isServer)
|
|
|
|
{
|
|
|
|
mIsServer = isServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & SacEntry::getName() const
|
|
|
|
{
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SacEntry::setName(const std::string & name)
|
|
|
|
{
|
|
|
|
if (name.length() > kMaxServiceNameLen)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Service name string too long (max 8 chars)");
|
|
|
|
}
|
|
|
|
|
|
|
|
mName = name;
|
|
|
|
}
|
2017-07-06 10:55:58 +00:00
|
|
|
|
|
|
|
bool SacEntry::isEqual(const SacEntry & other) const
|
|
|
|
{
|
|
|
|
return (mIsServer == other.mIsServer) \
|
|
|
|
&& (mName == other.mName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SacEntry::copyFrom(const SacEntry & other)
|
|
|
|
{
|
|
|
|
if (other.getSize())
|
|
|
|
{
|
|
|
|
importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->mIsServer = other.mIsServer;
|
|
|
|
this->mName = other.mName;
|
|
|
|
}
|
|
|
|
}
|