nstool/lib/libnx/source/SacBinary.cpp

85 lines
1.6 KiB
C++
Raw Normal View History

#include <nx/SacBinary.h>
nx::SacBinary::SacBinary()
{
2018-06-24 08:18:54 +00:00
clear();
}
nx::SacBinary::SacBinary(const SacBinary & other)
{
2018-06-24 08:18:54 +00:00
*this = other;
}
void nx::SacBinary::operator=(const SacBinary & other)
{
2018-06-24 08:18:54 +00:00
if (other.getBytes().data())
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
mServices = other.mServices;
}
}
bool nx::SacBinary::operator==(const SacBinary & other) const
{
return (mServices == other.mServices);
}
bool nx::SacBinary::operator!=(const SacBinary & other) const
{
2018-06-24 08:18:54 +00:00
return !(*this == other);
}
void nx::SacBinary::toBytes()
{
size_t totalSize = 0;
2018-06-24 08:18:54 +00:00
for (size_t i = 0; i < mServices.size(); i++)
{
2018-06-24 08:18:54 +00:00
mServices[i].toBytes();
totalSize += mServices[i].getBytes().size();
}
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(totalSize);
for (size_t i = 0, pos = 0; i < mServices.size(); pos += mServices[i].getBytes().size(), i++)
{
2018-06-24 08:18:54 +00:00
memcpy((mRawBinary.data() + pos), mServices[i].getBytes().data(), mServices[i].getBytes().size());
}
}
void nx::SacBinary::fromBytes(const byte_t* data, size_t len)
{
clear();
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(len);
memcpy(mRawBinary.data(), data, mRawBinary.size());
2018-06-24 08:18:54 +00:00
SacEntry sac;
for (size_t pos = 0; pos < len; pos += mServices.atBack().getBytes().size())
{
2018-06-24 08:18:54 +00:00
sac.fromBytes((const byte_t*)(mRawBinary.data() + pos), len - pos);
mServices.addElement(sac);
}
}
const fnd::Vec<byte_t>& nx::SacBinary::getBytes() const
2018-06-24 08:18:54 +00:00
{
return mRawBinary;
}
void nx::SacBinary::clear()
{
2018-06-24 08:18:54 +00:00
mRawBinary.clear();
mServices.clear();
}
const fnd::List<nx::SacEntry>& nx::SacBinary::getServiceList() const
{
return mServices;
}
void nx::SacBinary::addService(const SacEntry& service)
{
mServices.addElement(service);
2018-06-24 08:18:54 +00:00
}