Merge pull request #28 from jakcron/refactor-ISerialisable

[nx|nx-hb|es|nstool] Refactor related to ISerialisable + More
This commit is contained in:
Jack 2018-06-29 15:41:37 +08:00 committed by GitHub
commit a079bec2bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
159 changed files with 4310 additions and 5410 deletions

View file

@ -1,13 +1,12 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/cert.h>
namespace es
{
class CertificateBody
: public fnd::ISerialiseableBinary
: public fnd::ISerialisable
{
public:
CertificateBody();
@ -17,12 +16,12 @@ namespace es
bool operator==(const CertificateBody& other) const;
bool operator!=(const CertificateBody& other) const;
void importBinary(const byte_t* src, size_t size);
void exportBinary();
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* src, size_t size);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
const std::string& getIssuer() const;
@ -50,7 +49,7 @@ namespace es
const std::string kModuleName = "CERTIFICATE_BODY";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
std::string mIssuer;
@ -61,9 +60,5 @@ namespace es
crypto::rsa::sRsa4096Key mRsa4096PublicKey;
crypto::rsa::sRsa2048Key mRsa2048PublicKey;
crypto::ecdsa::sEcdsa240Point mEcdsa240PublicKey;
// helpers
bool isEqual(const CertificateBody& other) const;
void copyFrom(const CertificateBody& other);
};
}

View file

@ -1,30 +1,25 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/ticket.h>
namespace es
{
class SectionHeader_V2 :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
SectionHeader_V2();
SectionHeader_V2(const SectionHeader_V2& other);
SectionHeader_V2(const byte_t* bytes, size_t len);
void operator=(const SectionHeader_V2& other);
bool operator==(const SectionHeader_V2& other) const;
bool operator!=(const SectionHeader_V2& other) const;
void operator=(const SectionHeader_V2& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
virtual void exportBinary();
virtual void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* data, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
virtual void clear();
@ -48,7 +43,7 @@ namespace es
const std::string kModuleName = "SECTION_HEADER_V2";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
uint32_t mSectionOffset;
@ -56,11 +51,6 @@ namespace es
uint32_t mSectionSize;
uint16_t mRecordNum;
ticket::SectionType mSectionType;
// helpers
bool isEqual(const SectionHeader_V2& other) const;
void copyFrom(const SectionHeader_V2& other);
};
}

View file

@ -1,13 +1,12 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/sign.h>
namespace es
{
class SignatureBlock
: public fnd::ISerialiseableBinary
: public fnd::ISerialisable
{
public:
SignatureBlock();
@ -17,12 +16,12 @@ namespace es
bool operator==(const SignatureBlock& other) const;
bool operator!=(const SignatureBlock& other) const;
void importBinary(const byte_t* src, size_t size);
void exportBinary();
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* src, size_t size);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
es::sign::SignType getSignType() const;
@ -31,23 +30,19 @@ namespace es
bool isLittleEndian() const;
void setLittleEndian(bool isLE);
const fnd::MemoryBlob& getSignature() const;
void setSignature(const fnd::MemoryBlob& signature);
const fnd::Vec<byte_t>& getSignature() const;
void setSignature(const fnd::Vec<byte_t>& signature);
private:
const std::string kModuleName = "SIGNATURE_BLOCK";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
es::sign::SignType mSignType;
bool mIsLittleEndian;
fnd::MemoryBlob mSignature;
// helpers
bool isEqual(const SignatureBlock& other) const;
void copyFrom(const SignatureBlock& other);
fnd::Vec<byte_t> mSignature;
};
}

View file

@ -1,113 +1,137 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/SignatureBlock.h>
namespace es
{
template <class T>
class SignedData
: public fnd::ISerialiseableBinary
: public fnd::ISerialisable
{
public:
SignedData()
{
clear();
}
SignedData(const SignedData& other)
{
copyFrom(other);
}
SignedData();
SignedData(const SignedData& other);
void operator=(const SignedData& other)
{
copyFrom(other);
}
bool operator==(const SignedData& other) const
{
return isEqual(other);
}
bool operator!=(const SignedData& other) const
{
return !(*this == other);
}
void operator=(const SignedData& other);
bool operator==(const SignedData& other) const;
bool operator!=(const SignedData& other) const;
void importBinary(const byte_t* src, size_t size)
{
mSignature.importBinary(src, size);
mBody.importBinary(src + mSignature.getSize(), size - mSignature.getSize());
// export/import
void toBytes();
void fromBytes(const byte_t* src, size_t size);
const fnd::Vec<byte_t>& getBytes() const;
mBinaryBlob.alloc(mSignature.getSize() + mBody.getSize());
memcpy(mBinaryBlob.getBytes(), src, mBinaryBlob.getSize());
}
// variables
void clear();
void exportBinary()
{
mSignature.exportBinary();
mBody.exportBinary();
const es::SignatureBlock& getSignature() const;
void setSignature(const SignatureBlock& signature);
mBinaryBlob.alloc(mSignature.getSize() + mBody.getSize());
memcpy(mBinaryBlob.getBytes(), mSignature.getBytes(), mSignature.getSize());
memcpy(mBinaryBlob.getBytes() + mSignature.getSize(), mBody.getBytes(), mBody.getSize());
}
const byte_t* getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t getSize() const
{
return mBinaryBlob.getSize();
}
void clear()
{
mBinaryBlob.clear();
mSignature.clear();
mBody.clear();
}
const es::SignatureBlock& getSignature() const
{
return mSignature;
}
void setSignature(const SignatureBlock& signature)
{
mSignature = signature;
}
const T& getBody() const
{
return mBody;
}
void setBody(const T& body)
{
mBody = body;
}
const T& getBody() const;
void setBody(const T& body);
private:
const std::string kModuleName = "SIGNED_DATA";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
SignatureBlock mSignature;
T mBody;
// helpers
bool isEqual(const SignedData& other) const
{
return (mSignature == other.mSignature) \
&& (mBody == other.mBody);
}
void copyFrom(const SignedData& other)
{
mBinaryBlob = other.mBinaryBlob;
mSignature = other.mSignature;
mBody = other.mBody;
}
};
template <class T>
inline SignedData<T>::SignedData()
{
clear();
}
template <class T>
inline SignedData<T>::SignedData(const SignedData& other)
{
*this = other;
}
template <class T>
inline void SignedData<T>::operator=(const SignedData& other)
{
mRawBinary = other.mRawBinary;
mSignature = other.mSignature;
mBody = other.mBody;
}
template <class T>
inline bool SignedData<T>::operator==(const SignedData& other) const
{
return (mSignature == other.mSignature) \
&& (mBody == other.mBody);
}
template <class T>
inline bool SignedData<T>::operator!=(const SignedData& other) const
{
return !(*this == other);
}
template <class T>
inline void SignedData<T>::toBytes()
{
mSignature.toBytes();
mBody.toBytes();
mRawBinary.alloc(mSignature.getBytes().size() + mBody.getBytes().size());
memcpy(mRawBinary.data(), mSignature.getBytes().data(), mSignature.getBytes().size());
memcpy(mRawBinary.data() + mSignature.getBytes().size(), mBody.getBytes().data(), mBody.getBytes().size());
}
template <class T>
inline void SignedData<T>::fromBytes(const byte_t* src, size_t size)
{
mSignature.fromBytes(src, size);
mBody.fromBytes(src + mSignature.getBytes().size(), size - mSignature.getBytes().size());
mRawBinary.alloc(mSignature.getBytes().size() + mBody.getBytes().size());
memcpy(mRawBinary.data(), src, mRawBinary.size());
}
template <class T>
inline const fnd::Vec<byte_t>& SignedData<T>::getBytes() const
{
return mRawBinary;
}
template <class T>
inline void SignedData<T>::clear()
{
mRawBinary.clear();
mSignature.clear();
mBody.clear();
}
template <class T>
inline const es::SignatureBlock& SignedData<T>::getSignature() const
{
return mSignature;
}
template <class T>
inline void SignedData<T>::setSignature(const SignatureBlock& signature)
{
mSignature = signature;
}
template <class T>
inline const T& SignedData<T>::getBody() const
{
return mBody;
}
template <class T>
inline void SignedData<T>::setBody(const T& body)
{
mBody = body;
}
}

View file

@ -1,33 +1,28 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/ticket.h>
namespace es
{
class TicketBody_V2 :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
TicketBody_V2();
TicketBody_V2(const TicketBody_V2& other);
TicketBody_V2(const byte_t* bytes, size_t len);
void operator=(const TicketBody_V2& other);
bool operator==(const TicketBody_V2& other) const;
bool operator!=(const TicketBody_V2& other) const;
void operator=(const TicketBody_V2& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
virtual void exportBinary();
virtual void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
virtual void clear();
void clear();
const std::string& getIssuer() const;
void setIssuer(const std::string& issuer);
@ -87,7 +82,7 @@ namespace es
const std::string kModuleName = "TICKET_BODY_V2";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
std::string mIssuer;
@ -108,10 +103,6 @@ namespace es
uint32_t mSectHeaderOffset;
uint16_t mSectNum;
uint16_t mSectEntrySize;
// helpers
bool isEqual(const TicketBody_V2& other) const;
void copyFrom(const TicketBody_V2& other);
};
}

View file

@ -7,17 +7,30 @@ es::CertificateBody::CertificateBody()
es::CertificateBody::CertificateBody(const CertificateBody& other)
{
copyFrom(other);
*this = other;
}
void es::CertificateBody::operator=(const CertificateBody& other)
{
copyFrom(other);
mRawBinary = other.mRawBinary;
mIssuer = other.mIssuer;
mSubject = other.mSubject;
mCertId = other.mCertId;
mPublicKeyType = other.mPublicKeyType;
mRsa4096PublicKey = other.mRsa4096PublicKey;
mRsa2048PublicKey = other.mRsa2048PublicKey;
mEcdsa240PublicKey = other.mEcdsa240PublicKey;
}
bool es::CertificateBody::operator==(const CertificateBody& other) const
{
return isEqual(other);
return (mIssuer == other.mIssuer) \
&& (mSubject == other.mSubject) \
&& (mCertId == other.mCertId) \
&& (mPublicKeyType == other.mPublicKeyType) \
&& (mRsa4096PublicKey == other.mRsa4096PublicKey) \
&& (mRsa2048PublicKey == other.mRsa2048PublicKey) \
&& (mEcdsa240PublicKey == other.mEcdsa240PublicKey);
}
bool es::CertificateBody::operator!=(const CertificateBody& other) const
@ -25,7 +38,55 @@ bool es::CertificateBody::operator!=(const CertificateBody& other) const
return !(*this == other);
}
void es::CertificateBody::importBinary(const byte_t* src, size_t size)
void es::CertificateBody::toBytes()
{
// get public key size
size_t pubkeySize = 0;
switch (mPublicKeyType)
{
case (cert::RSA4096):
pubkeySize = sizeof(sRsa4096PublicKeyBlock);
break;
case (cert::RSA2048):
pubkeySize = sizeof(sRsa2048PublicKeyBlock);
break;
case (cert::ECDSA240):
pubkeySize = sizeof(sEcdsa240PublicKeyBlock);
break;
default:
throw fnd::Exception(kModuleName, "Unknown public key type");
}
mRawBinary.alloc(sizeof(sCertificateHeader) + pubkeySize);
sCertificateHeader* hdr = (sCertificateHeader*)mRawBinary.data();
// copy header vars
strncpy(hdr->issuer, mIssuer.c_str(), cert::kIssuerSize);
hdr->key_type = mPublicKeyType;
strncpy(hdr->subject, mSubject.c_str(), cert::kSubjectSize);
hdr->cert_id = mCertId;
// copy public key
if (mPublicKeyType == cert::RSA4096)
{
sRsa4096PublicKeyBlock* pubkey = (sRsa4096PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa4096PublicKey.modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa4096PublicKey.public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::RSA2048)
{
sRsa2048PublicKeyBlock* pubkey = (sRsa2048PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa2048PublicKey.modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa2048PublicKey.public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::ECDSA240)
{
sEcdsa240PublicKeyBlock* pubkey = (sEcdsa240PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
pubkey->public_key = mEcdsa240PublicKey;
}
}
void es::CertificateBody::fromBytes(const byte_t* src, size_t size)
{
clear();
@ -61,11 +122,11 @@ void es::CertificateBody::importBinary(const byte_t* src, size_t size)
}
// save raw binary
mBinaryBlob.alloc((sizeof(sCertificateHeader) + pubkeySize));
memcpy(mBinaryBlob.getBytes(), src, mBinaryBlob.getSize());
mRawBinary.alloc((sizeof(sCertificateHeader) + pubkeySize));
memcpy(mRawBinary.data(), src, mRawBinary.size());
// save hdr variables
hdr = (const sCertificateHeader*)mBinaryBlob.getBytes();
hdr = (const sCertificateHeader*)mRawBinary.data();
if (hdr->issuer[0] != 0)
mIssuer = std::string(hdr->issuer, cert::kIssuerSize);
@ -77,80 +138,28 @@ void es::CertificateBody::importBinary(const byte_t* src, size_t size)
// save public key
if (mPublicKeyType == cert::RSA4096)
{
const sRsa4096PublicKeyBlock* pubkey = (const sRsa4096PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
const sRsa4096PublicKeyBlock* pubkey = (const sRsa4096PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(mRsa4096PublicKey.modulus, pubkey->modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(mRsa4096PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::RSA2048)
{
const sRsa2048PublicKeyBlock* pubkey = (const sRsa2048PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
const sRsa2048PublicKeyBlock* pubkey = (const sRsa2048PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(mRsa2048PublicKey.modulus, pubkey->modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(mRsa2048PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::ECDSA240)
{
const sEcdsa240PublicKeyBlock* pubkey = (const sEcdsa240PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
const sEcdsa240PublicKeyBlock* pubkey = (const sEcdsa240PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
mEcdsa240PublicKey = pubkey->public_key;
}
}
void es::CertificateBody::exportBinary()
const fnd::Vec<byte_t>& es::CertificateBody::getBytes() const
{
// get public key size
size_t pubkeySize = 0;
switch (mPublicKeyType)
{
case (cert::RSA4096):
pubkeySize = sizeof(sRsa4096PublicKeyBlock);
break;
case (cert::RSA2048):
pubkeySize = sizeof(sRsa2048PublicKeyBlock);
break;
case (cert::ECDSA240):
pubkeySize = sizeof(sEcdsa240PublicKeyBlock);
break;
default:
throw fnd::Exception(kModuleName, "Unknown public key type");
}
mBinaryBlob.alloc(sizeof(sCertificateHeader) + pubkeySize);
sCertificateHeader* hdr = (sCertificateHeader*)mBinaryBlob.getBytes();
// copy header vars
strncpy(hdr->issuer, mIssuer.c_str(), cert::kIssuerSize);
hdr->key_type = mPublicKeyType;
strncpy(hdr->subject, mSubject.c_str(), cert::kSubjectSize);
hdr->cert_id = mCertId;
// copy public key
if (mPublicKeyType == cert::RSA4096)
{
sRsa4096PublicKeyBlock* pubkey = (sRsa4096PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa4096PublicKey.modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa4096PublicKey.public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::RSA2048)
{
sRsa2048PublicKeyBlock* pubkey = (sRsa2048PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa2048PublicKey.modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa2048PublicKey.public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::ECDSA240)
{
sEcdsa240PublicKeyBlock* pubkey = (sEcdsa240PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
pubkey->public_key = mEcdsa240PublicKey;
}
return mRawBinary;
}
const byte_t* es::CertificateBody::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t es::CertificateBody::getSize() const
{
return mBinaryBlob.getSize();
}
void es::CertificateBody::clear()
{
@ -242,28 +251,4 @@ const crypto::ecdsa::sEcdsa240Point& es::CertificateBody::getEcdsa240PublicKey()
void es::CertificateBody::setEcdsa240PublicKey(const crypto::ecdsa::sEcdsa240Point& key)
{
mEcdsa240PublicKey = key;
}
bool es::CertificateBody::isEqual(const CertificateBody& other) const
{
return (mIssuer == other.mIssuer) \
&& (mSubject == other.mSubject) \
&& (mCertId == other.mCertId) \
&& (mPublicKeyType == other.mPublicKeyType) \
&& (mRsa4096PublicKey == other.mRsa4096PublicKey) \
&& (mRsa2048PublicKey == other.mRsa2048PublicKey) \
&& (mEcdsa240PublicKey == other.mEcdsa240PublicKey);
}
void es::CertificateBody::copyFrom(const CertificateBody& other)
{
mBinaryBlob = other.mBinaryBlob;
mIssuer = other.mIssuer;
mSubject = other.mSubject;
mCertId = other.mCertId;
mPublicKeyType = other.mPublicKeyType;
mRsa4096PublicKey = other.mRsa4096PublicKey;
mRsa2048PublicKey = other.mRsa2048PublicKey;
mEcdsa240PublicKey = other.mEcdsa240PublicKey;
}
}

View file

@ -7,89 +7,18 @@ es::SectionHeader_V2::SectionHeader_V2()
es::SectionHeader_V2::SectionHeader_V2(const SectionHeader_V2 & other)
{
copyFrom(other);
}
es::SectionHeader_V2::SectionHeader_V2(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
bool es::SectionHeader_V2::operator==(const SectionHeader_V2 & other) const
{
return isEqual(other);
}
bool es::SectionHeader_V2::operator!=(const SectionHeader_V2 & other) const
{
return !isEqual(other);
*this = other;
}
void es::SectionHeader_V2::operator=(const SectionHeader_V2 & other)
{
copyFrom(other);
}
const byte_t * es::SectionHeader_V2::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t es::SectionHeader_V2::getSize() const
{
return mBinaryBlob.getSize();
}
void es::SectionHeader_V2::exportBinary()
{
mBinaryBlob.alloc(sizeof(sSectionHeader_v2));
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mBinaryBlob.getBytes();
hdr->section_offset = (mSectionOffset);
hdr->record_size = (mRecordSize);
hdr->section_size = (mSectionSize);
hdr->record_num = (mRecordNum);
hdr->section_type = (mSectionType);
}
void es::SectionHeader_V2::importBinary(const byte_t * bytes, size_t len)
{
if (len < sizeof(sSectionHeader_v2))
if (other.getBytes().size())
{
throw fnd::Exception(kModuleName, "Binary too small");
}
clear();
mBinaryBlob.alloc(sizeof(sSectionHeader_v2));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mBinaryBlob.getBytes();
mSectionOffset = hdr->section_offset.get();
mRecordSize = hdr->record_size.get();
mSectionSize = hdr->section_size.get();
mRecordNum = hdr->record_num.get();
mSectionType = (ticket::SectionType)hdr->section_type.get();
}
bool es::SectionHeader_V2::isEqual(const SectionHeader_V2 & other) const
{
return (mSectionOffset == other.mSectionOffset) \
&& (mRecordSize == other.mRecordSize) \
&& (mSectionSize == other.mSectionSize) \
&& (mRecordNum == other.mRecordNum) \
&& (mSectionType == other.mSectionType);
}
void es::SectionHeader_V2::copyFrom(const SectionHeader_V2 & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
mBinaryBlob.clear();
mRawBinary.clear();
mSectionOffset = other.mSectionOffset;
mRecordSize = other.mRecordSize;
mSectionSize = other.mSectionSize;
@ -98,9 +27,60 @@ void es::SectionHeader_V2::copyFrom(const SectionHeader_V2 & other)
}
}
bool es::SectionHeader_V2::operator==(const SectionHeader_V2 & other) const
{
return (mSectionOffset == other.mSectionOffset) \
&& (mRecordSize == other.mRecordSize) \
&& (mSectionSize == other.mSectionSize) \
&& (mRecordNum == other.mRecordNum) \
&& (mSectionType == other.mSectionType);
}
bool es::SectionHeader_V2::operator!=(const SectionHeader_V2 & other) const
{
return !(*this ==other);
}
void es::SectionHeader_V2::toBytes()
{
mRawBinary.alloc(sizeof(sSectionHeader_v2));
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mRawBinary.data();
hdr->section_offset = (mSectionOffset);
hdr->record_size = (mRecordSize);
hdr->section_size = (mSectionSize);
hdr->record_num = (mRecordNum);
hdr->section_type = (mSectionType);
}
void es::SectionHeader_V2::fromBytes(const byte_t * bytes, size_t len)
{
if (len < sizeof(sSectionHeader_v2))
{
throw fnd::Exception(kModuleName, "Binary too small");
}
clear();
mRawBinary.alloc(sizeof(sSectionHeader_v2));
memcpy(mRawBinary.data(), bytes, mRawBinary.size());
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mRawBinary.data();
mSectionOffset = hdr->section_offset.get();
mRecordSize = hdr->record_size.get();
mSectionSize = hdr->section_size.get();
mRecordNum = hdr->record_num.get();
mSectionType = (ticket::SectionType)hdr->section_type.get();
}
const fnd::Vec<byte_t>& es::SectionHeader_V2::getBytes() const
{
return mRawBinary;
}
void es::SectionHeader_V2::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mSectionOffset = 0;
mRecordSize = 0;
mSectionSize = 0;

View file

@ -7,17 +7,22 @@ es::SignatureBlock::SignatureBlock()
es::SignatureBlock::SignatureBlock(const SignatureBlock& other)
{
copyFrom(other);
*this = other;
}
void es::SignatureBlock::operator=(const SignatureBlock& other)
{
copyFrom(other);
mRawBinary = other.mRawBinary;
mSignType = other.mSignType;
mIsLittleEndian = other.mIsLittleEndian;
mSignature = other.mSignature;
}
bool es::SignatureBlock::operator==(const SignatureBlock& other) const
{
return isEqual(other);
return (mSignType == other.mSignType) \
&& (mIsLittleEndian == other.mIsLittleEndian) \
&& (mSignature == other.mSignature);
}
bool es::SignatureBlock::operator!=(const SignatureBlock& other) const
@ -25,7 +30,45 @@ bool es::SignatureBlock::operator!=(const SignatureBlock& other) const
return !(*this == other);
}
void es::SignatureBlock::importBinary(const byte_t* src, size_t size)
void es::SignatureBlock::toBytes()
{
size_t totalSize = 0;
size_t sigSize = 0;
switch (mSignType)
{
case (sign::SIGN_RSA4096_SHA1):
case (sign::SIGN_RSA4096_SHA256):
totalSize = sizeof(sRsa4096SignBlock);
sigSize = crypto::rsa::kRsa4096Size;
break;
case (sign::SIGN_RSA2048_SHA1):
case (sign::SIGN_RSA2048_SHA256):
totalSize = sizeof(sRsa2048SignBlock);
sigSize = crypto::rsa::kRsa2048Size;
break;
case (sign::SIGN_ECDSA240_SHA1):
case (sign::SIGN_ECDSA240_SHA256):
totalSize = sizeof(sEcdsa240SignBlock);
sigSize = sign::kEcdsaSigSize;
break;
default:
throw fnd::Exception(kModuleName, "Unknown signature type");
}
if (mSignature.size() != sigSize)
throw fnd::Exception(kModuleName, "Signature size is incorrect");
// commit to binary
mRawBinary.alloc(totalSize);
if (mIsLittleEndian)
*(le_uint32_t*)(mRawBinary.data()) = mSignType;
else
*(be_uint32_t*)(mRawBinary.data()) = mSignType;
memcpy(mRawBinary.data() + 4, mSignature.data(), sigSize);
}
void es::SignatureBlock::fromBytes(const byte_t* src, size_t size)
{
clear();
@ -87,65 +130,22 @@ void es::SignatureBlock::importBinary(const byte_t* src, size_t size)
throw fnd::Exception(kModuleName, "Certificate too small");
}
mBinaryBlob.alloc(totalSize);
memcpy(mBinaryBlob.getBytes(), src, totalSize);
mRawBinary.alloc(totalSize);
memcpy(mRawBinary.data(), src, totalSize);
mSignType = (sign::SignType)signType;
mSignature.alloc(sigSize);
memcpy(mSignature.getBytes(), mBinaryBlob.getBytes() + 4, sigSize);
memcpy(mSignature.data(), mRawBinary.data() + 4, sigSize);
}
void es::SignatureBlock::exportBinary()
const fnd::Vec<byte_t>& es::SignatureBlock::getBytes() const
{
size_t totalSize = 0;
size_t sigSize = 0;
switch (mSignType)
{
case (sign::SIGN_RSA4096_SHA1):
case (sign::SIGN_RSA4096_SHA256):
totalSize = sizeof(sRsa4096SignBlock);
sigSize = crypto::rsa::kRsa4096Size;
break;
case (sign::SIGN_RSA2048_SHA1):
case (sign::SIGN_RSA2048_SHA256):
totalSize = sizeof(sRsa2048SignBlock);
sigSize = crypto::rsa::kRsa2048Size;
break;
case (sign::SIGN_ECDSA240_SHA1):
case (sign::SIGN_ECDSA240_SHA256):
totalSize = sizeof(sEcdsa240SignBlock);
sigSize = sign::kEcdsaSigSize;
break;
default:
throw fnd::Exception(kModuleName, "Unknown signature type");
}
if (mSignature.getSize() != sigSize)
throw fnd::Exception(kModuleName, "Signature size is incorrect");
// commit to binary
mBinaryBlob.alloc(totalSize);
if (mIsLittleEndian)
*(le_uint32_t*)(mBinaryBlob.getBytes()) = mSignType;
else
*(be_uint32_t*)(mBinaryBlob.getBytes()) = mSignType;
memcpy(mBinaryBlob.getBytes() + 4, mSignature.getBytes(), sigSize);
}
const byte_t* es::SignatureBlock::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t es::SignatureBlock::getSize() const
{
return mBinaryBlob.getSize();
return mRawBinary;
}
void es::SignatureBlock::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mSignType = sign::SIGN_RSA4096_SHA1;
mIsLittleEndian = false;
mSignature.clear();
@ -171,27 +171,12 @@ void es::SignatureBlock::setLittleEndian(bool isLE)
mIsLittleEndian = isLE;
}
const fnd::MemoryBlob& es::SignatureBlock::getSignature() const
const fnd::Vec<byte_t>& es::SignatureBlock::getSignature() const
{
return mSignature;
}
void es::SignatureBlock::setSignature(const fnd::MemoryBlob& signature)
void es::SignatureBlock::setSignature(const fnd::Vec<byte_t>& signature)
{
mSignature = signature;
}
bool es::SignatureBlock::isEqual(const SignatureBlock& other) const
{
return (mSignType == other.mSignType) \
&& (mIsLittleEndian == other.mIsLittleEndian) \
&& (mSignature == other.mSignature);
}
void es::SignatureBlock::copyFrom(const SignatureBlock& other)
{
mBinaryBlob = other.mBinaryBlob;
mSignType = other.mSignType;
mIsLittleEndian = other.mIsLittleEndian;
mSignature = other.mSignature;
}

View file

@ -9,43 +9,68 @@ es::TicketBody_V2::TicketBody_V2()
es::TicketBody_V2::TicketBody_V2(const TicketBody_V2 & other)
{
copyFrom(other);
}
es::TicketBody_V2::TicketBody_V2(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
bool es::TicketBody_V2::operator==(const TicketBody_V2 & other) const
{
return isEqual(other);
}
bool es::TicketBody_V2::operator!=(const TicketBody_V2 & other) const
{
return !isEqual(other);
*this = other;
}
void es::TicketBody_V2::operator=(const TicketBody_V2 & other)
{
copyFrom(other);
if (other.getBytes().size())
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
mIssuer = other.mIssuer;
memcpy(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize);
mEncType = other.mEncType;
mTicketVersion = other.mTicketVersion;
mLicenseType = other.mLicenseType;
mPreInstall = other.mPreInstall;
mSharedTitle = other.mSharedTitle;
mAllowAllContent = other.mAllowAllContent;
memcpy(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize);
mTicketId = other.mTicketId;
mDeviceId = other.mDeviceId;
memcpy(mRightsId, other.mRightsId, ticket::kRightsIdSize);
mAccountId = other.mAccountId;
mSectTotalSize = other.mSectTotalSize;
mSectHeaderOffset = other.mSectHeaderOffset;
mSectNum = other.mSectNum;
mSectEntrySize = other.mSectEntrySize;
}
}
const byte_t * es::TicketBody_V2::getBytes() const
bool es::TicketBody_V2::operator==(const TicketBody_V2 & other) const
{
return mBinaryBlob.getBytes();
return (mIssuer == other.mIssuer) \
&& (memcmp(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize) == 0) \
&& (mEncType == other.mEncType) \
&& (mTicketVersion == other.mTicketVersion) \
&& (mLicenseType == other.mLicenseType) \
&& (mPreInstall == other.mPreInstall) \
&& (mSharedTitle == other.mSharedTitle) \
&& (mAllowAllContent == other.mAllowAllContent) \
&& (memcmp(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize) == 0) \
&& (mTicketId == other.mTicketId) \
&& (mDeviceId == other.mDeviceId) \
&& (memcmp(mRightsId, other.mRightsId, ticket::kRightsIdSize) == 0) \
&& (mAccountId == other.mAccountId) \
&& (mSectTotalSize == other.mSectTotalSize) \
&& (mSectHeaderOffset == other.mSectHeaderOffset) \
&& (mSectNum == other.mSectNum) \
&& (mSectEntrySize == other.mSectEntrySize);
}
size_t es::TicketBody_V2::getSize() const
bool es::TicketBody_V2::operator!=(const TicketBody_V2 & other) const
{
return mBinaryBlob.getSize();
return !(*this == other);
}
void es::TicketBody_V2::exportBinary()
void es::TicketBody_V2::toBytes()
{
mBinaryBlob.alloc(sizeof(sTicketBody_v2));
sTicketBody_v2* body = (sTicketBody_v2*)mBinaryBlob.getBytes();
mRawBinary.alloc(sizeof(sTicketBody_v2));
sTicketBody_v2* body = (sTicketBody_v2*)mRawBinary.data();
body->format_version = (ticket::kFormatVersion);
@ -69,7 +94,7 @@ void es::TicketBody_V2::exportBinary()
body->sect_entry_size = (mSectEntrySize);
}
void es::TicketBody_V2::importBinary(const byte_t * bytes, size_t len)
void es::TicketBody_V2::fromBytes(const byte_t * bytes, size_t len)
{
if (len < sizeof(sTicketBody_v2))
{
@ -78,9 +103,9 @@ void es::TicketBody_V2::importBinary(const byte_t * bytes, size_t len)
clear();
mBinaryBlob.alloc(sizeof(sTicketBody_v2));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
sTicketBody_v2* body = (sTicketBody_v2*)mBinaryBlob.getBytes();
mRawBinary.alloc(sizeof(sTicketBody_v2));
memcpy(mRawBinary.data(), bytes, mRawBinary.size());
sTicketBody_v2* body = (sTicketBody_v2*)mRawBinary.data();
if (body->format_version != ticket::kFormatVersion)
{
@ -106,9 +131,14 @@ void es::TicketBody_V2::importBinary(const byte_t * bytes, size_t len)
mSectEntrySize = body->sect_entry_size.get();
}
const fnd::Vec<byte_t>& es::TicketBody_V2::getBytes() const
{
return mRawBinary;
}
void es::TicketBody_V2::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mIssuer.clear();
memset(mEncTitleKey, 0, ticket::kEncTitleKeySize);
mEncType = ticket::AES128_CBC;
@ -151,7 +181,7 @@ const byte_t * es::TicketBody_V2::getEncTitleKey() const
void es::TicketBody_V2::setEncTitleKey(const byte_t * data, size_t len)
{
memset(mEncTitleKey, 0, ticket::kEncTitleKeySize);
memcpy(mEncTitleKey, data, MIN(len, ticket::kEncTitleKeySize));
memcpy(mEncTitleKey, data, _MIN(len, ticket::kEncTitleKeySize));
}
es::ticket::TitleKeyEncType es::TicketBody_V2::getTitleKeyEncType() const
@ -232,7 +262,7 @@ const byte_t * es::TicketBody_V2::getReservedRegion() const
void es::TicketBody_V2::setReservedRegion(const byte_t * data, size_t len)
{
memset(mReservedRegion, 0, ticket::kReservedRegionSize);
memcpy(mReservedRegion, data, MIN(len, ticket::kReservedRegionSize));
memcpy(mReservedRegion, data, _MIN(len, ticket::kReservedRegionSize));
}
uint64_t es::TicketBody_V2::getTicketId() const
@ -313,54 +343,4 @@ uint16_t es::TicketBody_V2::getSectionEntrySize() const
void es::TicketBody_V2::setSectionEntrySize(uint16_t size)
{
mSectEntrySize = size;
}
bool es::TicketBody_V2::isEqual(const TicketBody_V2 & other) const
{
return (mIssuer == other.mIssuer) \
&& (memcmp(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize) == 0) \
&& (mEncType == other.mEncType) \
&& (mTicketVersion == other.mTicketVersion) \
&& (mLicenseType == other.mLicenseType) \
&& (mPreInstall == other.mPreInstall) \
&& (mSharedTitle == other.mSharedTitle) \
&& (mAllowAllContent == other.mAllowAllContent) \
&& (memcmp(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize) == 0) \
&& (mTicketId == other.mTicketId) \
&& (mDeviceId == other.mDeviceId) \
&& (memcmp(mRightsId, other.mRightsId, ticket::kRightsIdSize) == 0) \
&& (mAccountId == other.mAccountId) \
&& (mSectTotalSize == other.mSectTotalSize) \
&& (mSectHeaderOffset == other.mSectHeaderOffset) \
&& (mSectNum == other.mSectNum) \
&& (mSectEntrySize == other.mSectEntrySize);
}
void es::TicketBody_V2::copyFrom(const TicketBody_V2 & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
clear();
mIssuer = other.mIssuer;
memcpy(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize);
mEncType = other.mEncType;
mTicketVersion = other.mTicketVersion;
mLicenseType = other.mLicenseType;
mPreInstall = other.mPreInstall;
mSharedTitle = other.mSharedTitle;
mAllowAllContent = other.mAllowAllContent;
memcpy(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize);
mTicketId = other.mTicketId;
mDeviceId = other.mDeviceId;
memcpy(mRightsId, other.mRightsId, ticket::kRightsIdSize);
mAccountId = other.mAccountId;
mSectTotalSize = other.mSectTotalSize;
mSectHeaderOffset = other.mSectHeaderOffset;
mSectNum = other.mSectNum;
mSectEntrySize = other.mSectEntrySize;
}
}

View file

@ -119,6 +119,9 @@
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<None Include="makefile" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\fnd\BitMath.h" />
<ClInclude Include="include\fnd\elf.h" />
@ -126,27 +129,23 @@
<ClInclude Include="include\fnd\Exception.h" />
<ClInclude Include="include\fnd\IFile.h" />
<ClInclude Include="include\fnd\io.h" />
<ClInclude Include="include\fnd\ISerialiseableBinary.h" />
<ClInclude Include="include\fnd\ISerialisable.h" />
<ClInclude Include="include\fnd\List.h" />
<ClInclude Include="include\fnd\MemoryBlob.h" />
<ClInclude Include="include\fnd\ResourceFileReader.h" />
<ClInclude Include="include\fnd\SimpleFile.h" />
<ClInclude Include="include\fnd\SimpleTextOutput.h" />
<ClInclude Include="include\fnd\StringConv.h" />
<ClInclude Include="include\fnd\types.h" />
<ClInclude Include="include\fnd\Vec.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="source\Exception.cpp" />
<ClCompile Include="source\io.cpp" />
<ClCompile Include="source\MemoryBlob.cpp" />
<ClCompile Include="source\ResourceFileReader.cpp" />
<ClCompile Include="source\SimpleFile.cpp" />
<ClCompile Include="source\SimpleTextOutput.cpp" />
<ClCompile Include="source\StringConv.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="makefile" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View file

@ -15,22 +15,40 @@
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="makefile" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\fnd\BitMath.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\elf.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\Endian.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\Exception.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\IFile.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\io.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\ISerialiseableBinary.h">
<ClInclude Include="include\fnd\ISerialisable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\List.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\MemoryBlob.h">
<ClInclude Include="include\fnd\ResourceFileReader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\SimpleFile.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\SimpleTextOutput.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\StringConv.h">
@ -39,22 +57,7 @@
<ClInclude Include="include\fnd\types.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\BitMath.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\Endian.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\SimpleTextOutput.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\IFile.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\ResourceFileReader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\fnd\SimpleFile.h">
<ClInclude Include="include\fnd\Vec.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
@ -65,23 +68,17 @@
<ClCompile Include="source\io.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\MemoryBlob.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\StringConv.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\SimpleTextOutput.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\ResourceFileReader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\SimpleFile.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="makefile" />
<ClCompile Include="source\SimpleTextOutput.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\StringConv.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View file

@ -0,0 +1,21 @@
#pragma once
#include <fnd/types.h>
#include <fnd/Vec.h>
namespace fnd
{
class ISerialisable
{
public:
// serialise
virtual void toBytes() = 0;
// deserialise
virtual void fromBytes(const byte_t* data, size_t len) = 0;
// get byte vector
virtual const fnd::Vec<byte_t>& getBytes() const = 0;
// clear data
virtual void clear() = 0;
};
}

View file

@ -1,17 +0,0 @@
#pragma once
#include <fnd/types.h>
namespace fnd
{
class ISerialiseableBinary
{
public:
virtual const byte_t* getBytes() const = 0;
virtual size_t getSize() const = 0;
virtual void exportBinary() = 0;
virtual void importBinary(const byte_t* bytes, size_t len) = 0;
virtual void clear() = 0;
};
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <fnd/types.h>
#include <vector>
//#include <fnd/Vec.h>
namespace fnd
{
@ -8,130 +9,159 @@ namespace fnd
class List
{
public:
List() :
mElements()
{
}
// constructors
List();
List(const List<T>& other);
List(const T* elements, size_t num) :
mElements(num)
{
initList(elements, num);
}
// copy operator
void operator=(const List<T>& other);
// assignment operator
const List& operator=(const List& other)
{
mElements.clear();
for (size_t i = 0; i < other.getSize(); i++)
{
mElements.push_back(other[i]);
}
// equivalence operators
bool operator==(const List<T>& other) const;
bool operator!=(const List<T>& other) const;
return *this;
}
// back relative insertion
void addElement(const T& element);
// element access
const T& operator[](size_t index) const;
T& operator[](size_t index);
const T& atBack() const;
T& atBack();
// comparision operator
bool operator==(const List& other) const
{
if (other.getSize() != this->getSize())
{
return false;
}
// element num
size_t size() const;
for (size_t i = 0; i < this->getSize(); i++)
{
if (getElement(i) != other[i])
{
return false;
}
}
// clear List
void clear();
return true;
}
bool operator!=(const List& other) const
{
return !operator==(other);
}
// access operators
const T& getElement(size_t index) const
{
return mElements[index];
}
T& getElement(size_t index)
{
if (index == mElements.size()) mElements.push_back(T());
return mElements[index];
}
const T& operator[](size_t index) const { return getElement(index); }
T& operator[](size_t index) { return getElement(index); }
const T& atBack() const { return getElement(getSize() - 1); }
T& atBack() { return getElement(getSize() - 1); }
// functions
void addElement(const T& element) { mElements.push_back(element); }
size_t getIndexOf(const T& key) const
{
for (size_t i = 0; i < getSize(); i++)
{
if (getElement(i) == key) return i;
}
throw Exception("LIST", "Element does not exist");
}
bool hasElement(const T& key) const
{
try
{
getIndexOf(key);
} catch (const Exception&)
{
return false;
}
return true;
}
// special
template <class X>
size_t getIndexOf(const X& key) const
{
for (size_t i = 0; i < getSize(); i++)
{
if (getElement(i) == key) return i;
}
throw Exception("LIST", "Element does not exist");
}
template <class X>
bool hasElement(const X& key) const
{
try
{
getIndexOf(key);
} catch (const Exception&)
{
return false;
}
return true;
}
size_t getSize() const { return mElements.size(); }
void clear() { mElements.clear(); }
// element access by key
template <class K>
bool hasElement(const K& key) const;
template <class K>
const T& getElement(const K& key) const;
template <class K>
T& getElement(const K& key);
private:
std::vector<T> mElements;
std::vector<T> m_Vec;
};
void initList(T* elements, size_t num)
template<class T>
inline List<T>::List() :
m_Vec()
{
}
template<class T>
inline List<T>::List(const List<T>& other) :
List()
{
*this = other;
}
template<class T>
inline void List<T>::operator=(const List<T>& other)
{
m_Vec = other.m_Vec;
}
template<class T>
inline bool List<T>::operator==(const List<T>& other) const
{
return m_Vec == other.m_Vec;
}
template<class T>
inline bool List<T>::operator!=(const List<T>& other) const
{
return !(*this == other);
}
template<class T>
inline void List<T>::addElement(const T & element)
{
m_Vec.push_back(element);
}
template<class T>
inline const T & List<T>::operator[](size_t index) const
{
return m_Vec[index];
}
template<class T>
inline T & List<T>::operator[](size_t index)
{
return m_Vec[index];
}
template<class T>
inline const T & List<T>::atBack() const
{
return m_Vec.back();
}
template<class T>
inline T & List<T>::atBack()
{
return m_Vec.back();
}
template<class T>
inline size_t List<T>::size() const
{
return m_Vec.size();
}
template<class T>
inline void List<T>::clear()
{
m_Vec.clear();
}
template<class T>
template<class K>
inline bool List<T>::hasElement(const K & key) const
{
for (size_t i = 0; i < m_Vec.size(); i++)
{
mElements.clear();
for (size_t i = 0; i < num; i++)
if (m_Vec[i] == key)
{
mElements.push_back(elements[i]);
return true;
}
}
};
}
return false;
}
template<class T>
template<class K>
inline const T & List<T>::getElement(const K & key) const
{
for (size_t i = 0; i < m_Vec.size(); i++)
{
if (m_Vec[i] == key)
{
return m_Vec[i];
}
}
throw fnd::Exception("getElement(): element does not exist");
}
template<class T>
template<class K>
inline T & List<T>::getElement(const K & key)
{
for (size_t i = 0; i < m_Vec.size(); i++)
{
if (m_Vec[i] == key)
{
return m_Vec[i];
}
}
throw fnd::Exception("getElement(): element does not exist");
}
}

View file

@ -1,42 +0,0 @@
#pragma once
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fnd/types.h>
namespace fnd
{
class MemoryBlob
{
public:
MemoryBlob();
MemoryBlob(const byte_t* bytes, size_t len);
bool operator==(const MemoryBlob& other) const;
bool operator!=(const MemoryBlob& other) const;
void operator=(const MemoryBlob& other);
void alloc(size_t size);
void extend(size_t new_size);
void clear();
inline byte_t& operator[](size_t index) { return mData[index]; }
inline const byte_t& operator[](size_t index) const { return mData[index]; }
inline byte_t* getBytes() { return mData.data(); }
inline const byte_t* getBytes() const { return mData.data(); }
inline size_t getSize() const { return mVisableSize; }
private:
const std::string kModuleName = "MEMORY_BLOB";
static const size_t kAllocBlockSize = 0x1000;
std::vector<byte_t> mData;
size_t mSize;
size_t mVisableSize;
void allocateMemory(size_t size);
void clearMemory();
};
}

View file

@ -0,0 +1,198 @@
#pragma once
#include <fnd/types.h>
namespace fnd
{
template <class T>
class Vec
{
public:
// constructors
Vec();
Vec(const Vec<T>& other);
Vec(const T* array, size_t num);
~Vec();
// copy operator
void operator=(const Vec<T>& other);
// equivalence operators
bool operator==(const Vec<T>& other) const;
bool operator!=(const Vec<T>& other) const;
// element access
const T& operator[](size_t index) const;
T& operator[](size_t index);
// raw access
const T* data() const;
T* data();
// element num
size_t size() const;
// allocate vector
void alloc(size_t new_size);
// resize vector
void resize(size_t new_size);
// clear vector
void clear();
private:
T* m_Vec;
size_t m_Size;
void copyFrom(const T * array, size_t num);
};
template<class T>
inline Vec<T>::Vec() :
m_Vec(nullptr),
m_Size(0)
{}
template<class T>
inline Vec<T>::Vec(const Vec<T>& other) :
Vec()
{
copyFrom(other.data(), other.size());
}
template<class T>
inline Vec<T>::Vec(const T * array, size_t num) :
Vec()
{
copyFrom(array, num);
}
template<class T>
inline Vec<T>::~Vec()
{
clear();
}
template<class T>
inline void Vec<T>::operator=(const Vec<T>& other)
{
copyFrom(other.data(), other.size());
}
template<class T>
inline bool Vec<T>::operator==(const Vec<T>& other) const
{
bool isEqual = true;
if (m_Size == other.m_Size)
{
for (size_t i = 0; i < m_Size && isEqual; i++)
{
if (m_Vec[i] != other.m_Vec[i])
{
isEqual = false;
}
}
}
else
{
isEqual = false;
}
return isEqual;
}
template<class T>
inline bool Vec<T>::operator!=(const Vec<T>& other) const
{
return !(*this == other);
}
template<class T>
inline const T & Vec<T>::operator[](size_t index) const
{
return m_Vec[index];
}
template<class T>
inline T & Vec<T>::operator[](size_t index)
{
return m_Vec[index];
}
template<class T>
inline const T * Vec<T>::data() const
{
return m_Vec;
}
template<class T>
inline T * Vec<T>::data()
{
return m_Vec;
}
template<class T>
inline size_t Vec<T>::size() const
{
return m_Size;
}
template<class T>
inline void Vec<T>::alloc(size_t new_size)
{
clear();
m_Vec = new T[new_size];
if (m_Vec == nullptr)
{
fnd::Exception("Vec", "Failed to allocate memory for vector");
}
m_Size = new_size;
}
template<class T>
inline void Vec<T>::resize(size_t new_size)
{
if (m_Vec != nullptr)
{
T* new_vec = new T[new_size];
if (new_vec == nullptr)
{
fnd::Exception("Vec", "Failed to allocate memory for vector");
}
for (size_t i = 0; i < _MIN(m_Size, new_size); i++)
{
new_vec[i] = m_Vec[i];
}
delete[] m_Vec;
m_Vec = new_vec;
m_Size = new_size;
}
else
{
alloc(new_size);
}
}
template<class T>
inline void Vec<T>::clear()
{
if (m_Vec != nullptr)
{
delete[] m_Vec;
}
m_Vec = nullptr;
m_Size = 0;
}
template<class T>
inline void Vec<T>::copyFrom(const T * array, size_t num)
{
clear();
alloc(num);
for (size_t i = 0; i < m_Size; i++)
{
m_Vec[i] = array[i];
}
}
}

View file

@ -1,6 +1,5 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
namespace fnd
{
@ -13,10 +12,6 @@ namespace fnd
#endif
size_t getFileSize(const std::string& path);
void readFile(const std::string& path, MemoryBlob& blob);
void readFile(const std::string& path, size_t offset, size_t len, MemoryBlob& blob);
void writeFile(const std::string& path, const MemoryBlob& blob);
void writeFile(const std::string& path, const byte_t* data, size_t len);
void makeDirectory(const std::string& path);
void getEnvironVar(std::string& var, const std::string& key);
void appendToPath(std::string& base, const std::string& add);

View file

@ -6,8 +6,8 @@
typedef uint8_t byte_t;
#define MIN(x,y) ((x) <= (y)? (x) : (y))
#define MAX(x,y) ((x) >= (y)? (x) : (y))
#define _MIN(x,y) ((x) <= (y)? (x) : (y))
#define _MAX(x,y) ((x) >= (y)? (x) : (y))
static inline uint64_t align(uint64_t size, uint64_t align)
{

View file

@ -1,89 +0,0 @@
#include <fnd/MemoryBlob.h>
using namespace fnd;
MemoryBlob::MemoryBlob() :
mData(),
mSize(0),
mVisableSize(0)
{
}
fnd::MemoryBlob::MemoryBlob(const byte_t * bytes, size_t len) :
mData(),
mSize(0),
mVisableSize(0)
{
alloc(len);
memcpy(getBytes(), bytes, getSize());
}
bool fnd::MemoryBlob::operator==(const MemoryBlob & other) const
{
bool isEqual = true;
if (this->getSize() == other.getSize())
{
isEqual = memcmp(this->getBytes(), other.getBytes(), this->getSize()) == 0;
}
else
{
isEqual = false;
}
return isEqual;
}
bool fnd::MemoryBlob::operator!=(const MemoryBlob & other) const
{
return !operator==(other);
}
void fnd::MemoryBlob::operator=(const MemoryBlob & other)
{
alloc(other.getSize());
memcpy(getBytes(), other.getBytes(), getSize());
}
void MemoryBlob::alloc(size_t size)
{
if (size > mSize)
{
allocateMemory(size);
}
else
{
mVisableSize = size;
clearMemory();
}
}
void MemoryBlob::extend(size_t new_size)
{
try {
mData.resize(new_size);
}
catch (...) {
throw fnd::Exception(kModuleName, "extend() failed to allocate memory");
}
}
void fnd::MemoryBlob::clear()
{
mVisableSize = 0;
}
void MemoryBlob::allocateMemory(size_t size)
{
mSize = (size_t)align(size, kAllocBlockSize);
mVisableSize = size;
extend(mSize);
clearMemory();
}
void MemoryBlob::clearMemory()
{
memset(mData.data(), 0, mSize);
}

View file

@ -6,7 +6,7 @@ void fnd::SimpleTextOutput::hxdStyleDump(const byte_t* data, size_t len, size_t
// iterate over blocks
for (size_t i = 0; i < (len / row_len); i++)
{
printf("%08" PRIx64 " | ", i*row_len);
printf("%08" PRIx64 " | ", (uint64_t)(i * row_len));
// for block i print each byte
for (size_t j = 0; j < row_len; j++)
{
@ -26,7 +26,7 @@ void fnd::SimpleTextOutput::hxdStyleDump(const byte_t* data, size_t len, size_t
if ((len % row_len) > 0)
{
size_t i = (len / row_len);
printf("%08" PRIx64 " | ", i * row_len);
printf("%08" PRIx64 " | ", (uint64_t)(i * row_len));
// for block i print each byte
for (size_t j = 0; j < row_len; j++)
{

View file

@ -11,9 +11,6 @@
using namespace fnd;
static const std::string kModuleName = "IO";
static const size_t kBlockSize = 0x100000;
size_t io::getFileSize(const std::string& path)
{
std::ifstream f;
@ -25,112 +22,6 @@ size_t io::getFileSize(const std::string& path)
return static_cast<size_t>(f.tellg() - begin_pos);
}
void io::readFile(const std::string& path, MemoryBlob & blob)
{
fnd::SimpleFile file;
try
{
file.open(path, file.Read);
}
catch (const fnd::Exception&)
{
throw fnd::Exception(kModuleName, "Failed to open \"" + path + "\": does not exist");
}
size_t filesz, filepos;
filesz = file.size();
try {
blob.alloc(filesz);
}
catch (const fnd::Exception& e)
{
throw fnd::Exception(kModuleName, "Failed to allocate memory for file: " + std::string(e.what()));
}
for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize)
{
file.read(blob.getBytes() + filepos, kBlockSize);
}
if (filesz)
{
file.read(blob.getBytes() + filepos, filesz);
}
}
void fnd::io::readFile(const std::string& path, size_t offset, size_t len, MemoryBlob& blob)
{
fnd::SimpleFile file;
try
{
file.open(path, file.Read);
} catch (const fnd::Exception&)
{
throw fnd::Exception(kModuleName, "Failed to open \"" + path + "\": does not exist");
}
size_t filesz, filepos;
filesz = file.size();
file.seek(offset);
if (filesz < len || filesz < offset || filesz < (offset + len))
{
throw Exception(kModuleName, "Failed to open \"" + path + "\": file to small");
}
try
{
blob.alloc(filesz);
} catch (const fnd::Exception& e)
{
throw fnd::Exception(kModuleName, "Failed to allocate memory for file: " + std::string(e.what()));
}
for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize)
{
file.read(blob.getBytes() + filepos, kBlockSize);
}
if (filesz)
{
file.read(blob.getBytes() + filepos, filesz);
}
}
void io::writeFile(const std::string& path, const MemoryBlob & blob)
{
writeFile(path, blob.getBytes(), blob.getSize());
}
void io::writeFile(const std::string & path, const byte_t * data, size_t len)
{
fnd::SimpleFile file;
try
{
file.open(path, file.Create);
} catch (const fnd::Exception&)
{
throw fnd::Exception(kModuleName, "Failed to open \"" + path + "\": does not exist");
}
size_t filesz, filepos;
filesz = len;
for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize)
{
file.write(data + filepos, kBlockSize);
}
if (filesz)
{
file.write(data + filepos, filesz);
}
}
void io::makeDirectory(const std::string& path)
{
#ifdef _WIN32

View file

@ -1,13 +1,12 @@
#pragma once
#include <nx/aset.h>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
namespace nx
{
class AssetHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct sSection
@ -29,25 +28,21 @@ namespace nx
bool operator!=(const sSection& other) const
{
return !(*this == other);
return !operator==(other);
}
};
AssetHeader();
AssetHeader(const AssetHeader& other);
AssetHeader(const byte_t* bytes, size_t len);
void operator=(const AssetHeader& other);
bool operator==(const AssetHeader& other) const;
bool operator!=(const AssetHeader& other) const;
void operator=(const AssetHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -64,16 +59,12 @@ namespace nx
const std::string kModuleName = "NRO_ASSET_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
sSection mIconInfo;
sSection mNacpInfo;
sSection mRomfsInfo;
// helpers
bool isEqual(const AssetHeader& other) const;
void copyFrom(const AssetHeader& other);
};
}

View file

@ -6,7 +6,7 @@ namespace nx
{
namespace aset
{
static const uint32_t kAssetSig = _MAKE_STRUCT_SIGNATURE("ASET");
static const uint32_t kAssetStructMagic = _MAKE_STRUCT_MAGIC_U32("ASET");
static const uint32_t kDefaultAssetFormatVersion = 0;
}
@ -20,7 +20,7 @@ namespace nx
struct sAssetHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
le_uint32_t format_version;
sAssetSection icon;
sAssetSection nacp;

View file

@ -6,6 +6,6 @@ namespace nx
{
namespace nro
{
static const uint64_t kNroHomebrewSig = _MAKE_STRUCT_SIGNATURE_U64("HOMEBREW");
static const uint64_t kNroHomebrewStructMagic = _MAKE_STRUCT_MAGIC_U64("HOMEBREW");
}
}

View file

@ -7,17 +7,22 @@ nx::AssetHeader::AssetHeader()
nx::AssetHeader::AssetHeader(const AssetHeader& other)
{
copyFrom(other);
*this = other;
}
nx::AssetHeader::AssetHeader(const byte_t* bytes, size_t len)
void nx::AssetHeader::operator=(const AssetHeader& other)
{
importBinary(bytes, len);
mRawBinary = other.mRawBinary;
mIconInfo = other.mIconInfo;
mNacpInfo = other.mNacpInfo;
mRomfsInfo = other.mRomfsInfo;
}
bool nx::AssetHeader::operator==(const AssetHeader& other) const
{
return isEqual(other);
return (mIconInfo == other.mIconInfo) \
&& (mNacpInfo == other.mNacpInfo) \
&& (mRomfsInfo == other.mRomfsInfo);
}
bool nx::AssetHeader::operator!=(const AssetHeader& other) const
@ -25,28 +30,13 @@ bool nx::AssetHeader::operator!=(const AssetHeader& other) const
return !(*this == other);
}
void nx::AssetHeader::operator=(const AssetHeader& other)
void nx::AssetHeader::toBytes()
{
copyFrom(other);
}
const byte_t* nx::AssetHeader::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t nx::AssetHeader::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::AssetHeader::exportBinary()
{
mBinaryBlob.alloc(sizeof(sAssetHeader));
nx::sAssetHeader* hdr = (nx::sAssetHeader*)mBinaryBlob.getBytes();
mRawBinary.alloc(sizeof(sAssetHeader));
nx::sAssetHeader* hdr = (nx::sAssetHeader*)mRawBinary.data();
// set header identifers
hdr->signature = aset::kAssetSig;
hdr->st_magic = aset::kAssetStructMagic;
hdr->format_version = aset::kDefaultAssetFormatVersion;
// set icon section
@ -62,7 +52,7 @@ void nx::AssetHeader::exportBinary()
hdr->romfs.size = mRomfsInfo.size;
}
void nx::AssetHeader::importBinary(const byte_t* bytes, size_t len)
void nx::AssetHeader::fromBytes(const byte_t* bytes, size_t len)
{
// check input data size
if (len < sizeof(sAssetHeader))
@ -74,14 +64,14 @@ void nx::AssetHeader::importBinary(const byte_t* bytes, size_t len)
clear();
// allocate internal local binary copy
mBinaryBlob.alloc(sizeof(sAssetHeader));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
mRawBinary.alloc(sizeof(sAssetHeader));
memcpy(mRawBinary.data(), bytes, mRawBinary.size());
// get sAssetHeader ptr
const nx::sAssetHeader* hdr = (const nx::sAssetHeader*)mBinaryBlob.getBytes();
const nx::sAssetHeader* hdr = (const nx::sAssetHeader*)mRawBinary.data();
// check NRO signature
if (hdr->signature.get() != aset::kAssetSig)
if (hdr->st_magic.get() != aset::kAssetStructMagic)
{
throw fnd::Exception(kModuleName, "ASET header corrupt (unrecognised header signature)");
}
@ -100,9 +90,14 @@ void nx::AssetHeader::importBinary(const byte_t* bytes, size_t len)
mRomfsInfo.size = hdr->romfs.size.get();
}
const fnd::Vec<byte_t>& nx::AssetHeader::getBytes() const
{
return mRawBinary;
}
void nx::AssetHeader::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
memset(&mIconInfo, 0, sizeof(mIconInfo));
memset(&mNacpInfo, 0, sizeof(mNacpInfo));
memset(&mRomfsInfo, 0, sizeof(mRomfsInfo));
@ -136,18 +131,4 @@ const nx::AssetHeader::sSection& nx::AssetHeader::getRomfsInfo() const
void nx::AssetHeader::setRomfsInfo(const sSection& info)
{
mRomfsInfo = info;
}
bool nx::AssetHeader::isEqual(const AssetHeader& other) const
{
return (mIconInfo == other.mIconInfo) \
&& (mNacpInfo == other.mNacpInfo) \
&& (mRomfsInfo == other.mRomfsInfo);
}
void nx::AssetHeader::copyFrom(const AssetHeader& other)
{
mIconInfo = other.mIconInfo;
mNacpInfo = other.mNacpInfo;
mRomfsInfo = other.mRomfsInfo;
}

View file

@ -0,0 +1,53 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/ISerialisable.h>
#include <nx/aci.h>
#include <nx/FileSystemAccessControlBinary.h>
#include <nx/ServiceAccessControlBinary.h>
#include <nx/KernelCapabilityBinary.h>
namespace nx
{
class AccessControlInfoBinary : public fnd::ISerialisable
{
public:
AccessControlInfoBinary();
AccessControlInfoBinary(const AccessControlInfoBinary& other);
void operator=(const AccessControlInfoBinary& other);
bool operator==(const AccessControlInfoBinary& other) const;
bool operator!=(const AccessControlInfoBinary& other) const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* data, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
uint64_t getProgramId() const;
void setProgramId(uint64_t program_id);
const nx::FileSystemAccessControlBinary& getFileSystemAccessControl() const;
void setFileSystemAccessControl(const FileSystemAccessControlBinary& fac);
const nx::ServiceAccessControlBinary& getServiceAccessControl() const;
void setServiceAccessControl(const ServiceAccessControlBinary& sac);
const nx::KernelCapabilityBinary& getKernelCapabilities() const;
void setKernelCapabilities(const KernelCapabilityBinary& kc);
private:
const std::string kModuleName = "ACCESS_CONTROL_INFO_BINARY";
// raw data
fnd::Vec<byte_t> mRawBinary;
// variables
uint64_t mProgramId;
nx::FileSystemAccessControlBinary mFileSystemAccessControl;
nx::ServiceAccessControlBinary mServiceAccessControl;
nx::KernelCapabilityBinary mKernelCapabilities;
};
}

View file

@ -0,0 +1,88 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/List.h>
#include <fnd/ISerialisable.h>
#include <nx/aci.h>
#include <nx/FileSystemAccessControlBinary.h>
#include <nx/ServiceAccessControlBinary.h>
#include <nx/KernelCapabilityBinary.h>
namespace nx
{
class AccessControlInfoDescBinary : public fnd::ISerialisable
{
public:
struct sProgramIdRestrict
{
uint64_t min;
uint64_t max;
void operator=(const sProgramIdRestrict& other)
{
min = other.min;
max = other.max;
}
bool operator==(const sProgramIdRestrict& other) const
{
return (min == other.min) \
&& (max == other.max);
}
bool operator!=(const sProgramIdRestrict& other) const
{
return !(*this == other);
}
};
AccessControlInfoDescBinary();
AccessControlInfoDescBinary(const AccessControlInfoDescBinary& other);
void operator=(const AccessControlInfoDescBinary& other);
bool operator==(const AccessControlInfoDescBinary& other) const;
bool operator!=(const AccessControlInfoDescBinary& other) const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* data, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
void generateSignature(const crypto::rsa::sRsa2048Key& key);
void validateSignature(const crypto::rsa::sRsa2048Key& key) const;
// variables
void clear();
const crypto::rsa::sRsa2048Key& getNcaHeaderSignature2Key() const;
void setNcaHeaderSignature2Key(const crypto::rsa::sRsa2048Key& key);
const fnd::List<aci::Flag>& getFlagList() const;
void setFlagList(const fnd::List<aci::Flag>& flags);
const sProgramIdRestrict& getProgramIdRestrict() const;
void setProgramIdRestrict(const sProgramIdRestrict& pid_restrict);
const nx::FileSystemAccessControlBinary& getFileSystemAccessControl() const;
void setFileSystemAccessControl(const FileSystemAccessControlBinary& fac);
const nx::ServiceAccessControlBinary& getServiceAccessControl() const;
void setServiceAccessControl(const ServiceAccessControlBinary& sac);
const nx::KernelCapabilityBinary& getKernelCapabilities() const;
void setKernelCapabilities(const KernelCapabilityBinary& kc);
private:
const std::string kModuleName = "ACCESS_CONTROL_INFO_DESC_BINARY";
// raw data
fnd::Vec<byte_t> mRawBinary;
// variables
crypto::rsa::sRsa2048Key mNcaHeaderSignature2Key;
fnd::List<aci::Flag> mFlags;
sProgramIdRestrict mProgramIdRestrict;
nx::FileSystemAccessControlBinary mFileSystemAccessControl;
nx::ServiceAccessControlBinary mServiceAccessControl;
nx::KernelCapabilityBinary mKernelCapabilities;
};
}

View file

@ -1,59 +0,0 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
#include <nx/AciHeader.h>
#include <nx/FacBinary.h>
#include <nx/SacBinary.h>
#include <nx/KcBinary.h>
namespace nx
{
class AciBinary :
public AciHeader
{
public:
AciBinary();
AciBinary(const AciBinary& other);
AciBinary(const byte_t* bytes, size_t len);
bool operator==(const AciBinary& other) const;
bool operator!=(const AciBinary& other) const;
void operator=(const AciBinary& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
virtual void exportBinary();
virtual void importBinary(const byte_t* bytes, size_t len);
// variables
virtual void clear();
const FacBinary& getFac() const;
void setFac(const FacBinary& fac);
const SacBinary& getSac() const;
void setSac(const SacBinary& sac);
const KcBinary& getKc() const;
void setKc(const KcBinary& kc);
private:
const std::string kModuleName = "ACI_BINARY";
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
FacBinary mFac;
SacBinary mSac;
KcBinary mKc;
bool isEqual(const AciBinary& other) const;
void copyFrom(const AciBinary& other);
};
}

View file

@ -1,116 +0,0 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/aci.h>
namespace nx
{
class AciHeader :
public fnd::ISerialiseableBinary
{
public:
enum AciType
{
TYPE_ACI0, // for Access Control Info
TYPE_ACID // for Access Control Info Desc
};
struct sSection
{
size_t offset;
size_t size;
void operator=(const sSection& other)
{
offset = other.offset;
size = other.size;
}
bool operator==(const sSection& other) const
{
return (offset == other.offset) \
&& (size == other.size);
}
bool operator!=(const sSection& other) const
{
return !operator==(other);
}
};
AciHeader();
AciHeader(const AciHeader& other);
AciHeader(const byte_t* bytes, size_t len);
bool operator==(const AciHeader& other) const;
bool operator!=(const AciHeader& other) const;
void operator=(const AciHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
virtual void exportBinary();
virtual void importBinary(const byte_t* bytes, size_t len);
// variables
virtual void clear();
size_t getAciSize() const;
// ACI0 only
uint64_t getProgramId() const;
void setProgramId(uint64_t program_id);
// ACID only
size_t getAcidSize() const;
//void setAcidSize(size_t size);
uint64_t getProgramIdMin() const;
void setProgramIdMin(uint64_t program_id);
uint64_t getProgramIdMax() const;
void setProgramIdMax(uint64_t program_id);
// ACID & ACI0
void setHeaderOffset(size_t offset);
AciType getAciType() const;
void setAciType(AciType type);
bool isProduction() const;
void setIsProduction(bool isProduction);
bool isUnqualifiedApproval() const;
void setIsUnqualifiedApproval(bool isUnqualifiedApproval);
const sSection& getFacPos() const;
void setFacSize(size_t size);
const sSection& getSacPos() const;
void setSacSize(size_t size);
const sSection& getKcPos() const;
void setKcSize(size_t size);
private:
const std::string kModuleName = "ACI_HEADER";
// raw data
fnd::MemoryBlob mBinaryBlob;
// ACI variables
uint64_t mProgramId;
// ACID variables
size_t mAcidSize;
uint64_t mProgramIdMin;
uint64_t mProgramIdMax;
// ACI(D) variables
size_t mHeaderOffset;
AciType mType;
bool mIsProduction;
bool mIsUnqualifiedApproval;
sSection mFac, mSac, mKc;
void calculateSectionOffsets();
bool isEqual(const AciHeader& other) const;
void copyFrom(const AciHeader& other);
};
}

View file

@ -1,50 +0,0 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <nx/AciBinary.h>
#include <crypto/rsa.h>
namespace nx
{
class AcidBinary :
public AciBinary
{
public:
AcidBinary();
AcidBinary(const AcidBinary& other);
AcidBinary(const byte_t* bytes, size_t len);
bool operator==(const AcidBinary& other) const;
bool operator!=(const AcidBinary& other) const;
void operator=(const AcidBinary& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
virtual void exportBinary();
void signBinary(const crypto::rsa::sRsa2048Key& key);
virtual void importBinary(const byte_t* bytes, size_t len);
void verifyBinary(const crypto::rsa::sRsa2048Key& key) const;
// variables
virtual void clear();
const crypto::rsa::sRsa2048Key& getNcaHeader2RsaKey() const;
void setNcaHeader2RsaKey(const crypto::rsa::sRsa2048Key& key);
private:
const std::string kModuleName = "ACID_BINARY";
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
crypto::rsa::sRsa2048Key mEmbeddedPublicKey;
bool isEqual(const AcidBinary& other) const;
void copyFrom(const AcidBinary& other);
};
}

View file

@ -1,15 +1,14 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/nacp.h>
namespace nx
{
class ApplicationControlPropertyBinary :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct sTitle
@ -86,19 +85,15 @@ namespace nx
ApplicationControlPropertyBinary();
ApplicationControlPropertyBinary(const ApplicationControlPropertyBinary& other);
ApplicationControlPropertyBinary(const byte_t* bytes, size_t len);
void operator=(const ApplicationControlPropertyBinary& other);
bool operator==(const ApplicationControlPropertyBinary& other) const;
bool operator!=(const ApplicationControlPropertyBinary& other) const;
void operator=(const ApplicationControlPropertyBinary& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -222,7 +217,7 @@ namespace nx
const std::string kModuleName = "APPLICATION_CONTROL_PROPERTY";
// raw data
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
fnd::List<sTitle> mTitle;
@ -263,8 +258,5 @@ namespace nx
nacp::PlayLogQueryCapability mPlayLogQueryCapability;
nacp::RepairFlag mRepairFlag;
byte_t mProgramIndex;
bool isEqual(const ApplicationControlPropertyBinary& other) const;
void copyFrom(const ApplicationControlPropertyBinary& other);
};
}

View file

@ -1,6 +1,6 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <nx/cnmt.h>
@ -8,7 +8,7 @@
namespace nx
{
class ContentMetaBinary :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct ContentInfo
@ -18,13 +18,12 @@ namespace nx
size_t size;
cnmt::ContentType type;
ContentInfo& operator=(const ContentInfo& other)
void operator=(const ContentInfo& other)
{
hash = other.hash;
memcpy(nca_id, other.nca_id, cnmt::kContentIdLen);
size = other.size;
type = other.type;
return *this;
}
bool operator==(const ContentInfo& other) const
@ -48,13 +47,12 @@ namespace nx
cnmt::ContentMetaType type;
byte_t attributes;
ContentMetaInfo& operator=(const ContentMetaInfo& other)
void operator=(const ContentMetaInfo& other)
{
id = other.id;
version = other.version;
type = other.type;
attributes = other.attributes;
return *this;
}
bool operator==(const ContentMetaInfo& other) const
@ -76,11 +74,10 @@ namespace nx
uint64_t patch_id;
uint32_t required_system_version;
ApplicationMetaExtendedHeader& operator=(const ApplicationMetaExtendedHeader& other)
void operator=(const ApplicationMetaExtendedHeader& other)
{
patch_id = other.patch_id;
required_system_version = other.required_system_version;
return *this;
}
bool operator==(const ApplicationMetaExtendedHeader& other) const
@ -100,11 +97,10 @@ namespace nx
uint64_t application_id;
uint32_t required_system_version;
PatchMetaExtendedHeader& operator=(const PatchMetaExtendedHeader& other)
void operator=(const PatchMetaExtendedHeader& other)
{
application_id = other.application_id;
required_system_version = other.required_system_version;
return *this;
}
bool operator==(const PatchMetaExtendedHeader& other) const
@ -124,11 +120,10 @@ namespace nx
uint64_t application_id;
uint32_t required_system_version;
AddOnContentMetaExtendedHeader& operator=(const AddOnContentMetaExtendedHeader& other)
void operator=(const AddOnContentMetaExtendedHeader& other)
{
application_id = other.application_id;
required_system_version = other.required_system_version;
return *this;
}
bool operator==(const AddOnContentMetaExtendedHeader& other) const
@ -147,10 +142,9 @@ namespace nx
{
uint64_t application_id;
DeltaMetaExtendedHeader& operator=(const DeltaMetaExtendedHeader& other)
void operator=(const DeltaMetaExtendedHeader& other)
{
application_id = other.application_id;
return *this;
}
bool operator==(const DeltaMetaExtendedHeader& other) const
@ -166,15 +160,15 @@ namespace nx
ContentMetaBinary();
ContentMetaBinary(const ContentMetaBinary& other);
ContentMetaBinary(const byte_t* bytes, size_t len);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
void operator=(const ContentMetaBinary& other);
bool operator==(const ContentMetaBinary& other) const;
bool operator!=(const ContentMetaBinary& other) const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -212,8 +206,8 @@ namespace nx
const fnd::List<nx::ContentMetaBinary::ContentMetaInfo>& getContentMetaInfo() const;
void setContentMetaInfo(const fnd::List<nx::ContentMetaBinary::ContentMetaInfo>& info);
const fnd::MemoryBlob& getExtendedData() const;
void setExtendedData(const fnd::MemoryBlob& data);
const fnd::Vec<byte_t>& getExtendedData() const;
void setExtendedData(const fnd::Vec<byte_t>& data);
const nx::sDigest& getDigest() const;
void setDigest(const nx::sDigest& digest);
@ -223,7 +217,7 @@ namespace nx
const std::string kModuleName = "CONTENT_META_BINARY";
// binary blob
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
uint64_t mTitleId;
@ -231,7 +225,7 @@ namespace nx
cnmt::ContentMetaType mType;
byte_t mAttributes;
uint32_t mRequiredDownloadSystemVersion;
fnd::MemoryBlob mExtendedHeader;
fnd::Vec<byte_t> mExtendedHeader;
ApplicationMetaExtendedHeader mApplicationMetaExtendedHeader;
PatchMetaExtendedHeader mPatchMetaExtendedHeader;
@ -240,7 +234,7 @@ namespace nx
fnd::List<nx::ContentMetaBinary::ContentInfo> mContentInfo;
fnd::List<nx::ContentMetaBinary::ContentMetaInfo> mContentMetaInfo;
fnd::MemoryBlob mExtendedData;
fnd::Vec<byte_t> mExtendedData;
nx::sDigest mDigest;
inline size_t getExtendedHeaderOffset() const { return sizeof(sContentMetaHeader); }
@ -253,8 +247,5 @@ namespace nx
bool validateExtendedHeaderSize(cnmt::ContentMetaType type, size_t exhdrSize) const;
size_t getExtendedDataSize(cnmt::ContentMetaType type, const byte_t* data) const;
void validateBinary(const byte_t* bytes, size_t len) const;
bool isEqual(const ContentMetaBinary& other) const;
void copyFrom(const ContentMetaBinary& other);
};
}

View file

@ -1,54 +0,0 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
#include <nx/FacHeader.h>
namespace nx
{
class FacBinary :
public FacHeader
{
public:
FacBinary();
FacBinary(const FacBinary& other);
FacBinary(const byte_t* bytes, size_t len);
bool operator==(const FacBinary& other) const;
bool operator!=(const FacBinary& other) const;
void operator=(const FacBinary& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
// variables
void clear();
const fnd::List<uint32_t>& getContentOwnerIdList() const;
void setContentOwnerIdList(const fnd::List<uint32_t>& list);
const fnd::List<uint32_t>& getSaveDataOwnerIdList() const;
void setSaveDataOwnerIdList(const fnd::List<uint32_t>& list);
private:
const std::string kModuleName = "FAC_BINARY";
static const uint32_t kFacFormatVersion = 1;
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
fnd::List<uint32_t> mContentOwnerIdList;
fnd::List<uint32_t> mSaveDataOwnerIdList;
bool isEqual(const FacBinary& other) const;
void copyFrom(const FacBinary& other);
};
}

View file

@ -1,107 +0,0 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
class FacHeader :
public fnd::ISerialiseableBinary
{
public:
enum FsAccessFlag
{
FSA_APPLICATION_INFO,
FSA_BOOT_MODE_CONTROL,
FSA_CALIBRATION,
FSA_SYSTEM_SAVE_DATA,
FSA_GAME_CARD,
FSA_SAVE_DATA_BACKUP,
FSA_SAVE_DATA_MANAGEMENT,
FSA_BIS_ALL_RAW,
FSA_GAME_CARD_RAW,
FSA_GAME_CARD_PRIVATE,
FSA_SET_TIME,
FSA_CONTENT_MANAGER,
FSA_IMAGE_MANAGER,
FSA_CREATE_SAVE_DATA,
FSA_SYSTEM_SAVE_DATA_MANAGEMENT,
FSA_BIS_FILE_SYSTEM,
FSA_SYSTEM_UPDATE,
FSA_SAVE_DATA_META,
FSA_DEVICE_SAVE_CONTROL,
FSA_SETTINGS_CONTROL,
FSA_DEBUG = 62,
FSA_FULL_PERMISSION = 63,
};
FacHeader();
FacHeader(const FacHeader& other);
FacHeader(const byte_t* bytes, size_t len);
bool operator==(const FacHeader& other) const;
bool operator!=(const FacHeader& other) const;
void operator=(const FacHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
// variables
void clear();
size_t getFacSize() const;
uint32_t getFormatVersion() const;
void setFormatVersion(uint32_t version);
const fnd::List<FsAccessFlag>& getFsaRightsList() const;
void setFsaRightsList(const fnd::List<FsAccessFlag>& list);
size_t getContentOwnerIdOffset() const;
size_t getContentOwnerIdSize() const;
void setContentOwnerIdSize(size_t size);
size_t getSaveDataOwnerIdOffset() const;
size_t getSaveDataOwnerIdSize() const;
void setSaveDataOwnerIdSize(size_t size);
private:
const std::string kModuleName = "FAC_HEADER";
static const uint32_t kFacFormatVersion = 1;
#pragma pack (push, 1)
struct sFacHeader
{
le_uint32_t version; // default 1
le_uint64_t fac_flags;
struct sFacSection
{
le_uint32_t start;
le_uint32_t end;
} content_owner_ids, save_data_owner_ids; // the data for these follow later in binary. start/end relative to base of FacData instance
};
#pragma pack (pop)
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
uint32_t mVersion;
fnd::List<FsAccessFlag> mFsaRights;
struct sSection
{
size_t offset;
size_t size;
} mContentOwnerIdPos, mSaveDataOwnerIdPos;
void calculateOffsets();
bool isEqual(const FacHeader& other) const;
void copyFrom(const FacHeader& other);
};
}

View file

@ -0,0 +1,52 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <nx/fac.h>
namespace nx
{
class FileSystemAccessControlBinary : public fnd::ISerialisable
{
public:
FileSystemAccessControlBinary();
FileSystemAccessControlBinary(const FileSystemAccessControlBinary& other);
void operator=(const FileSystemAccessControlBinary& other);
bool operator==(const FileSystemAccessControlBinary& other) const;
bool operator!=(const FileSystemAccessControlBinary& other) const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* data, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
uint32_t getFormatVersion() const;
void setFormatVersion(uint32_t version);
const fnd::List<fac::FsAccessFlag>& getFsaRightsList() const;
void setFsaRightsList(const fnd::List<fac::FsAccessFlag>& list);
const fnd::List<uint32_t>& getContentOwnerIdList() const;
void setContentOwnerIdList(const fnd::List<uint32_t>& list);
const fnd::List<uint32_t>& getSaveDataOwnerIdList() const;
void setSaveDataOwnerIdList(const fnd::List<uint32_t>& list);
private:
const std::string kModuleName = "FILE_SYSTEM_ACCESS_CONTROL_BINARY";
// raw data
fnd::Vec<byte_t> mRawBinary;
// variables
uint32_t mVersion;
fnd::List<fac::FsAccessFlag> mFsaRights;
fnd::List<uint32_t> mContentOwnerIdList;
fnd::List<uint32_t> mSaveDataOwnerIdList;
};
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -9,23 +9,27 @@ namespace nx
{
public:
HandleTableSizeEntry();
HandleTableSizeEntry(const KernelCapability& kernel_cap);
HandleTableSizeEntry(const KernelCapabilityEntry& kernel_cap);
HandleTableSizeEntry(uint16_t size);
void operator=(const HandleTableSizeEntry& other);
bool operator==(const HandleTableSizeEntry& other) const;
bool operator!=(const HandleTableSizeEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
uint16_t getHandleTableSize() const;
void setHandleTableSize(uint16_t size);
private:
const std::string kModuleName = "HANDLE_TABLE_SIZE_ENTRY";
static const KernelCapability::KernelCapId kCapId = KernelCapability::KC_HANDLE_TABLE_SIZE;
static const kc::KernelCapId kCapId = kc::KC_HANDLE_TABLE_SIZE;
static const uint16_t kValBits = 10;
static const uint16_t kMaxHandleTableSize = BIT(kValBits) - 1;
KernelCapability mCap;
KernelCapabilityEntry mCap;
uint16_t mHandleTableSize;
inline void updateCapField()

View file

@ -10,13 +10,13 @@ namespace nx
public:
HandleTableSizeHandler();
void operator=(const HandleTableSizeHandler& other);
bool operator==(const HandleTableSizeHandler& other) const;
bool operator!=(const HandleTableSizeHandler& other) const;
void operator=(const HandleTableSizeHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -30,9 +30,6 @@ namespace nx
bool mIsSet;
HandleTableSizeEntry mEntry;
void copyFrom(const HandleTableSizeHandler& other);
bool isEqual(const HandleTableSizeHandler& other) const;
};
}

View file

@ -1,13 +1,13 @@
#pragma once
#include <nx/hierarchicalintegrity.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
#include <crypto/sha.h>
namespace nx
{
class HierarchicalIntegrityHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct sLayer
@ -36,19 +36,15 @@ namespace nx
HierarchicalIntegrityHeader();
HierarchicalIntegrityHeader(const HierarchicalIntegrityHeader& other);
HierarchicalIntegrityHeader(const byte_t* bytes, size_t len);
void operator=(const HierarchicalIntegrityHeader& other);
bool operator==(const HierarchicalIntegrityHeader& other) const;
bool operator!=(const HierarchicalIntegrityHeader& other) const;
void operator=(const HierarchicalIntegrityHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -62,14 +58,11 @@ namespace nx
const std::string kModuleName = "HIERARCHICAL_INTEGRITY_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
fnd::List<sLayer> mLayerInfo;
fnd::List<crypto::sha::sSha256Hash> mMasterHashList;
bool isEqual(const HierarchicalIntegrityHeader& other) const;
void copyFrom(const HierarchicalIntegrityHeader& other);
};
}

View file

@ -1,13 +1,12 @@
#pragma once
#include <nx/hierarchicalsha256.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
class HierarchicalSha256Header :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct sLayer
@ -34,19 +33,15 @@ namespace nx
HierarchicalSha256Header();
HierarchicalSha256Header(const HierarchicalSha256Header& other);
HierarchicalSha256Header(const byte_t* bytes, size_t len);
void operator=(const HierarchicalSha256Header& other);
bool operator==(const HierarchicalSha256Header& other) const;
bool operator!=(const HierarchicalSha256Header& other) const;
void operator=(const HierarchicalSha256Header& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -63,15 +58,12 @@ namespace nx
const std::string kModuleName = "HIERARCHICAL_SHA256_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
crypto::sha::sSha256Hash mMasterHash;
size_t mHashBlockSize;
fnd::List<sLayer> mLayerInfo;
bool isEqual(const HierarchicalSha256Header& other) const;
void copyFrom(const HierarchicalSha256Header& other);
};
}

View file

@ -1,15 +1,15 @@
#pragma once
#include <fnd/types.h>
#include <fnd/List.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
class IKernelCapabilityHandler
{
public:
virtual void importKernelCapabilityList(const fnd::List<KernelCapability>& caps) = 0;
virtual void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const = 0;
virtual void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps) = 0;
virtual void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const = 0;
virtual void clear() = 0;
virtual bool isSet() const = 0;
};

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -13,12 +13,16 @@ namespace nx
static const uint32_t kInteruptNum = 2;
InteruptEntry();
InteruptEntry(const KernelCapability& kernel_cap);
InteruptEntry(const KernelCapabilityEntry& kernel_cap);
InteruptEntry(uint32_t interupt0, uint32_t interupt1);
void operator=(const InteruptEntry& other);
bool operator==(const InteruptEntry& other) const;
bool operator!=(const InteruptEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
uint32_t operator[](size_t index) const;
@ -28,9 +32,9 @@ namespace nx
private:
const std::string kModuleName = "INTERUPT_ENTRY";
static const KernelCapability::KernelCapId kCapId = KernelCapability::KC_ENABLE_INTERUPTS;
static const kc::KernelCapId kCapId = kc::KC_ENABLE_INTERUPTS;
KernelCapability mCap;
KernelCapabilityEntry mCap;
uint32_t mInterupt[kInteruptNum];
inline void updateCapField()

View file

@ -10,13 +10,13 @@ namespace nx
public:
InteruptHandler();
void operator=(const InteruptHandler& other);
bool operator==(const InteruptHandler& other) const;
bool operator!=(const InteruptHandler& other) const;
void operator=(const InteruptHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -29,9 +29,6 @@ namespace nx
bool mIsSet;
fnd::List<uint16_t> mInterupts;
void copyFrom(const InteruptHandler& other);
bool isEqual(const InteruptHandler& other) const;
};
}

View file

@ -1,62 +0,0 @@
#pragma once
#include <fnd/types.h>
namespace nx
{
class KernelCapability
{
public:
enum KernelCapId
{
KC_INVALID = 0,
KC_THREAD_INFO = 3,
KC_ENABLE_SYSTEM_CALLS = 4,
KC_MEMORY_MAP = 6,
KC_IO_MEMORY_MAP = 7,
KC_ENABLE_INTERUPTS = 11,
KC_MISC_PARAMS = 13,
KC_KERNEL_VERSION = 14,
KC_HANDLE_TABLE_SIZE = 15,
KC_MISC_FLAGS = 16
};
KernelCapability();
KernelCapability(KernelCapId type);
KernelCapability(KernelCapId type, uint32_t field);
const KernelCapability& operator=(const KernelCapability& other);
bool operator==(const KernelCapability& other) const;
bool operator!=(const KernelCapability& other) const;
uint32_t getCap() const;
void setCap(uint32_t cap);
KernelCapId getType() const;
void setType(KernelCapId type);
uint32_t getField() const;
void setField(uint32_t field);
private:
KernelCapId mType;
uint32_t mField;
inline uint32_t getFieldShift() const { return mType + 1; }
inline uint32_t getFieldMask() const { return BIT(31 - mType) - 1; }
inline uint32_t getCapMask() const { return BIT(mType) - 1; }
inline KernelCapId getCapId(uint32_t cap) const
{
KernelCapId id = KC_INVALID;
for (byte_t tmp = 0; tmp < 31; tmp++)
{
if (((cap >> tmp) & 1) == 0)
{
id = (KernelCapId)tmp;
break;
}
}
return id;
}
};
}

View file

@ -1,9 +1,8 @@
#pragma once
#include <string>
#include <vector>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/ThreadInfoHandler.h>
#include <nx/SystemCallHandler.h>
#include <nx/MemoryMappingHandler.h>
@ -15,25 +14,21 @@
namespace nx
{
class KcBinary :
public fnd::ISerialiseableBinary
class KernelCapabilityBinary :
public fnd::ISerialisable
{
public:
KcBinary();
KcBinary(const KcBinary& other);
KcBinary(const byte_t* bytes, size_t len);
KernelCapabilityBinary();
KernelCapabilityBinary(const KernelCapabilityBinary& other);
bool operator==(const KcBinary& other) const;
bool operator!=(const KcBinary& other) const;
void operator=(const KcBinary& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
void operator=(const KernelCapabilityBinary& other);
bool operator==(const KernelCapabilityBinary& other) const;
bool operator!=(const KernelCapabilityBinary& other) const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
virtual const fnd::Vec<byte_t>& getBytes() const;
// variables (consider further abstraction?)
void clear();
@ -65,7 +60,7 @@ namespace nx
const std::string kModuleName = "KC_BINARY";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
ThreadInfoHandler mThreadInfo;
@ -76,11 +71,6 @@ namespace nx
KernelVersionHandler mKernelVersion;
HandleTableSizeHandler mHandleTableSize;
MiscFlagsHandler mMiscFlags;
void clearVariables();
bool isEqual(const KcBinary& other) const;
void copyFrom(const KcBinary& other);
};
}

View file

@ -0,0 +1,50 @@
#pragma once
#include <fnd/types.h>
#include <nx/kc.h>
namespace nx
{
class KernelCapabilityEntry
{
public:
KernelCapabilityEntry();
KernelCapabilityEntry(kc::KernelCapId type);
KernelCapabilityEntry(kc::KernelCapId type, uint32_t field);
void operator=(const KernelCapabilityEntry& other);
bool operator==(const KernelCapabilityEntry& other) const;
bool operator!=(const KernelCapabilityEntry& other) const;
uint32_t getCap() const;
void setCap(uint32_t cap);
kc::KernelCapId getType() const;
void setType(kc::KernelCapId type);
uint32_t getField() const;
void setField(uint32_t field);
private:
kc::KernelCapId mType;
uint32_t mField;
inline uint32_t getFieldShift() const { return mType + 1; }
inline uint32_t getFieldMask() const { return BIT(31 - mType) - 1; }
inline uint32_t getCapMask() const { return BIT(mType) - 1; }
inline kc::KernelCapId getCapId(uint32_t cap) const
{
kc::KernelCapId id = kc::KC_INVALID;
for (byte_t tmp = 0; tmp < 31; tmp++)
{
if (((cap >> tmp) & 1) == 0)
{
id = (kc::KernelCapId)tmp;
break;
}
}
return id;
}
};
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -9,12 +9,16 @@ namespace nx
{
public:
KernelVersionEntry();
KernelVersionEntry(const KernelCapability& kernel_cap);
KernelVersionEntry(const KernelCapabilityEntry& kernel_cap);
KernelVersionEntry(uint16_t major, uint8_t minor);
void operator=(const KernelVersionEntry& other);
bool operator==(const KernelVersionEntry& other) const;
bool operator!=(const KernelVersionEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
uint16_t getVerMajor() const;
@ -23,13 +27,13 @@ namespace nx
void setVerMinor(uint8_t minor);
private:
const std::string kModuleName = "KERNEL_VERSION_ENTRY";
static const KernelCapability::KernelCapId kCapId = KernelCapability::KC_KERNEL_VERSION;
static const kc::KernelCapId kCapId = kc::KC_KERNEL_VERSION;
static const uint32_t kVerMajorBits = 13;
static const uint32_t kVerMajorMax = BIT(kVerMajorBits) - 1;
static const uint32_t kVerMinorBits = 4;
static const uint32_t kVerMinorMax = BIT(kVerMinorBits) - 1;
KernelCapability mCap;
KernelCapabilityEntry mCap;
uint16_t mVerMajor;
uint8_t mVerMinor;

View file

@ -10,13 +10,13 @@ namespace nx
public:
KernelVersionHandler();
void operator=(const KernelVersionHandler& other);
bool operator==(const KernelVersionHandler& other) const;
bool operator!=(const KernelVersionHandler& other) const;
void operator=(const KernelVersionHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -32,9 +32,6 @@ namespace nx
bool mIsSet;
KernelVersionEntry mEntry;
void copyFrom(const KernelVersionHandler& other);
bool isEqual(const KernelVersionHandler& other) const;
};
}

View file

@ -52,13 +52,13 @@ namespace nx
MemoryMappingHandler();
void operator=(const MemoryMappingHandler& other);
bool operator==(const MemoryMappingHandler& other) const;
bool operator!=(const MemoryMappingHandler& other) const;
void operator=(const MemoryMappingHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -73,9 +73,6 @@ namespace nx
bool mIsSet;
fnd::List<sMemoryMapping> mMemRange;
fnd::List<sMemoryMapping> mMemPage;
void copyFrom(const MemoryMappingHandler& other);
bool isEqual(const MemoryMappingHandler& other) const;
};
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -9,13 +9,17 @@ namespace nx
{
public:
MemoryPageEntry();
MemoryPageEntry(const KernelCapability& kernel_cap);
MemoryPageEntry(const KernelCapabilityEntry& kernel_cap);
MemoryPageEntry(uint32_t page);
MemoryPageEntry(uint32_t page, bool flag);
void operator=(const MemoryPageEntry& other);
bool operator==(const MemoryPageEntry& other) const;
bool operator!=(const MemoryPageEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
uint32_t getPage() const;
@ -30,7 +34,7 @@ namespace nx
static const uint32_t kPageBits = 24;
static const uint32_t kMaxPage = BIT(kPageBits) - 1;
KernelCapability mCap;
KernelCapabilityEntry mCap;
uint32_t mPage;
bool mFlag;
bool mUseFlag;
@ -41,7 +45,7 @@ namespace nx
field |= (uint32_t)(mPage & kMaxPage) << 0;
field |= (uint32_t)(mFlag) << kPageBits;
mCap.setField(field);
mCap.setType(mUseFlag ? KernelCapability::KC_MEMORY_MAP : KernelCapability::KC_IO_MEMORY_MAP);
mCap.setType(mUseFlag ? kc::KC_MEMORY_MAP : kc::KC_IO_MEMORY_MAP);
}
inline void processCapField()
@ -49,7 +53,7 @@ namespace nx
uint32_t field = mCap.getField();
mPage = (field >> 0) & kMaxPage;
mFlag = (field >> kPageBits);
mUseFlag = mCap.getType() == KernelCapability::KC_MEMORY_MAP;
mUseFlag = mCap.getType() == kc::KC_MEMORY_MAP;
}
};

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -9,23 +9,27 @@ namespace nx
{
public:
MiscFlagsEntry();
MiscFlagsEntry(const KernelCapability& kernel_cap);
MiscFlagsEntry(const KernelCapabilityEntry& kernel_cap);
MiscFlagsEntry(uint32_t flags);
void operator=(const MiscFlagsEntry& other);
bool operator==(const MiscFlagsEntry& other) const;
bool operator!=(const MiscFlagsEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
uint32_t getFlags() const;
void setFlags(uint32_t flags);
private:
const std::string kModuleName = "MISC_FLAG_ENTRY";
static const KernelCapability::KernelCapId kCapId = KernelCapability::KC_MISC_FLAGS;
static const kc::KernelCapId kCapId = kc::KC_MISC_FLAGS;
static const uint32_t kValueBits = 15;
static const uint32_t kMaxVal = BIT(kValueBits)-1;
KernelCapability mCap;
KernelCapabilityEntry mCap;
uint32_t mFlags;
inline void updateCapField()

View file

@ -30,13 +30,13 @@ namespace nx
MiscFlagsHandler();
void operator=(const MiscFlagsHandler& other);
bool operator==(const MiscFlagsHandler& other) const;
bool operator!=(const MiscFlagsHandler& other) const;
void operator=(const MiscFlagsHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -50,9 +50,6 @@ namespace nx
bool mIsSet;
fnd::List<Flags> mFlags;
void copyFrom(const MiscFlagsHandler& other);
bool isEqual(const MiscFlagsHandler& other) const;
};
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -9,23 +9,27 @@ namespace nx
{
public:
MiscParamsEntry();
MiscParamsEntry(const KernelCapability& kernel_cap);
MiscParamsEntry(const KernelCapabilityEntry& kernel_cap);
MiscParamsEntry(byte_t program_type);
void operator=(const MiscParamsEntry& other);
bool operator==(const MiscParamsEntry& other) const;
bool operator!=(const MiscParamsEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
byte_t getProgramType() const;
void setProgramType(byte_t type);
private:
const std::string kModuleName = "MISC_PARAMS_ENTRY";
static const KernelCapability::KernelCapId kCapId = KernelCapability::KC_MISC_PARAMS;
static const kc::KernelCapId kCapId = kc::KC_MISC_PARAMS;
static const byte_t kValBits = 3;
static const byte_t kMaxProgramType = BIT(kValBits)-1;
KernelCapability mCap;
KernelCapabilityEntry mCap;
byte_t mProgramType;
inline void updateCapField()

View file

@ -10,13 +10,13 @@ namespace nx
public:
MiscParamsHandler();
void operator=(const MiscParamsHandler& other);
bool operator==(const MiscParamsHandler& other) const;
bool operator!=(const MiscParamsHandler& other) const;
void operator=(const MiscParamsHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -30,9 +30,6 @@ namespace nx
bool mIsSet;
MiscParamsEntry mEntry;
void copyFrom(const MiscParamsHandler& other);
bool isEqual(const MiscParamsHandler& other) const;
};
}

View file

@ -1,13 +1,12 @@
#pragma once
#include <nx/nca.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
class NcaHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
enum FormatVersion
@ -49,19 +48,18 @@ namespace nx
NcaHeader();
NcaHeader(const NcaHeader& other);
NcaHeader(const byte_t* bytes, size_t len);
void operator=(const NcaHeader& other);
bool operator==(const NcaHeader& other) const;
bool operator!=(const NcaHeader& other) const;
void operator=(const NcaHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -95,7 +93,7 @@ namespace nx
const std::string kModuleName = "NCA_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
FormatVersion mFormatVersion;
@ -113,8 +111,6 @@ namespace nx
uint64_t blockNumToSize(uint32_t block_num) const;
uint32_t sizeToBlockNum(uint64_t real_size) const;
bool isEqual(const NcaHeader& other) const;
void copyFrom(const NcaHeader& other);
};
}

View file

@ -1,54 +1,79 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
#include <nx/NpdmHeader.h>
#include <nx/AciBinary.h>
#include <nx/AcidBinary.h>
#include <fnd/ISerialisable.h>
#include <nx/npdm.h>
#include <nx/AccessControlInfoBinary.h>
#include <nx/AccessControlInfoDescBinary.h>
namespace nx
{
class NpdmBinary :
public NpdmHeader
public fnd::ISerialisable
{
public:
NpdmBinary();
NpdmBinary(const NpdmBinary& other);
NpdmBinary(const byte_t* bytes, size_t len);
void operator=(const NpdmBinary& other);
bool operator==(const NpdmBinary& other) const;
bool operator!=(const NpdmBinary& other) const;
void operator=(const NpdmBinary& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
const AciBinary& getAci() const;
void setAci(const AciBinary& aci);
npdm::InstructionType getInstructionType() const;
void setInstructionType(npdm::InstructionType type);
const AcidBinary& getAcid() const;
void setAcid(const AcidBinary& acid);
npdm::ProcAddrSpaceType getProcAddressSpaceType() const;
void setProcAddressSpaceType(npdm::ProcAddrSpaceType type);
byte_t getMainThreadPriority() const;
void setMainThreadPriority(byte_t priority);
byte_t getMainThreadCpuId() const;
void setMainThreadCpuId(byte_t cpu_id);
uint32_t getVersion() const;
void setVersion(uint32_t version);
uint32_t getMainThreadStackSize() const;
void setMainThreadStackSize(uint32_t size);
const std::string& getName() const;
void setName(const std::string& name);
const std::string& getProductCode() const;
void setProductCode(const std::string& product_code);
const AccessControlInfoBinary& getAci() const;
void setAci(const AccessControlInfoBinary& aci);
const AccessControlInfoDescBinary& getAcid() const;
void setAcid(const AccessControlInfoDescBinary& acid);
private:
const std::string kModuleName = "NPDM_BINARY";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
AciBinary mAci;
AcidBinary mAcid;
bool isEqual(const NpdmBinary& other) const;
void copyFrom(const NpdmBinary& other);
npdm::InstructionType mInstructionType;
npdm::ProcAddrSpaceType mProcAddressSpaceType;
byte_t mMainThreadPriority;
byte_t mMainThreadCpuId;
uint32_t mVersion;
uint32_t mMainThreadStackSize;
std::string mName;
std::string mProductCode;
AccessControlInfoBinary mAci;
AccessControlInfoDescBinary mAcid;
};
}

View file

@ -1,109 +0,0 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/npdm.h>
namespace nx
{
class NpdmHeader :
public fnd::ISerialiseableBinary
{
public:
struct sSection
{
size_t offset;
size_t size;
void operator=(const sSection& other)
{
offset = other.offset;
size = other.size;
}
bool operator==(const sSection& other) const
{
return (offset == other.offset) \
&& (size == other.size);
}
bool operator!=(const sSection& other) const
{
return !operator==(other);
}
};
NpdmHeader();
NpdmHeader(const NpdmHeader& other);
NpdmHeader(const byte_t* bytes, size_t len);
bool operator==(const NpdmHeader& other) const;
bool operator!=(const NpdmHeader& other) const;
void operator=(const NpdmHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
// variables
void clear();
size_t getNpdmSize() const;
npdm::InstructionType getInstructionType() const;
void setInstructionType(npdm::InstructionType type);
npdm::ProcAddrSpaceType getProcAddressSpaceType() const;
void setProcAddressSpaceType(npdm::ProcAddrSpaceType type);
byte_t getMainThreadPriority() const;
void setMainThreadPriority(byte_t priority);
byte_t getMainThreadCpuId() const;
void setMainThreadCpuId(byte_t cpu_id);
uint32_t getVersion() const;
void setVersion(uint32_t version);
uint32_t getMainThreadStackSize() const;
void setMainThreadStackSize(uint32_t size);
const std::string& getName() const;
void setName(const std::string& name);
const std::string& getProductCode() const;
void setProductCode(const std::string& product_code);
const sSection& getAciPos() const;
void setAciSize(size_t size);
const sSection& getAcidPos() const;
void setAcidSize(size_t size);
private:
const std::string kModuleName = "NPDM_HEADER";
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
npdm::InstructionType mInstructionType;
npdm::ProcAddrSpaceType mProcAddressSpaceType;
byte_t mMainThreadPriority;
byte_t mMainThreadCpuId;
uint32_t mVersion;
uint32_t mMainThreadStackSize;
std::string mName;
std::string mProductCode;
sSection mAciPos;
sSection mAcidPos;
void calculateOffsets();
bool isEqual(const NpdmHeader& other) const;
void copyFrom(const NpdmHeader& other);
};
}

View file

@ -1,13 +1,12 @@
#pragma once
#include <nx/nro.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
class NroHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct sRoCrt
@ -75,19 +74,15 @@ namespace nx
NroHeader();
NroHeader(const NroHeader& other);
NroHeader(const byte_t* bytes, size_t len);
void operator=(const NroHeader& other);
bool operator==(const NroHeader& other) const;
bool operator!=(const NroHeader& other) const;
void operator=(const NroHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -125,7 +120,7 @@ namespace nx
const std::string kModuleName = "NRO_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
sRoCrt mRoCrt;
@ -138,10 +133,6 @@ namespace nx
sSection mRoEmbeddedInfo;
sSection mRoDynStrInfo;
sSection mRoDynSymInfo;
// helpers
bool isEqual(const NroHeader& other) const;
void copyFrom(const NroHeader& other);
};
}

View file

@ -1,13 +1,12 @@
#pragma once
#include <nx/nso.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
class NsoHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct sModuleId
@ -87,19 +86,15 @@ namespace nx
NsoHeader();
NsoHeader(const NsoHeader& other);
NsoHeader(const byte_t* bytes, size_t len);
void operator=(const NsoHeader& other);
bool operator==(const NsoHeader& other) const;
bool operator!=(const NsoHeader& other) const;
void operator=(const NsoHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -134,7 +129,7 @@ namespace nx
const std::string kModuleName = "NSO_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
sModuleId mModuleId;
@ -146,10 +141,6 @@ namespace nx
sLayout mRoEmbeddedInfo;
sLayout mRoDynStrInfo;
sLayout mRoDynSymInfo;
// helpers
bool isEqual(const NsoHeader& other) const;
void copyFrom(const NsoHeader& other);
};
}

View file

@ -1,16 +1,15 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/pfs.h>
namespace nx
{
class PfsHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
enum FsType
@ -64,24 +63,22 @@ namespace nx
PfsHeader();
PfsHeader(const PfsHeader& other);
PfsHeader(const byte_t* bytes, size_t len);
void operator=(const PfsHeader& other);
bool operator==(const PfsHeader& other) const;
bool operator!=(const PfsHeader& other) const;
void operator=(const PfsHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
FsType getFsType() const;
void setFsType(FsType type);
const fnd::List<sFile>& getFileList() const;
@ -92,7 +89,7 @@ namespace nx
const std::string kModuleName = "PFS_HEADER";
// binary blob
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
FsType mFsType;
@ -100,8 +97,6 @@ namespace nx
size_t getFileEntrySize(FsType fs_type);
void calculateOffsets(size_t data_offset);
bool isEqual(const PfsHeader& other) const;
void copyFrom(const PfsHeader& other);
};
}

View file

@ -1,48 +0,0 @@
#pragma once
#include <string>
#include <vector>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/SacEntry.h>
namespace nx
{
class SacBinary :
public fnd::ISerialiseableBinary
{
public:
SacBinary();
SacBinary(const SacBinary& other);
SacBinary(const byte_t* bytes, size_t len);
bool operator==(const SacBinary& other) const;
bool operator!=(const SacBinary& other) const;
void operator=(const SacBinary& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
// variables
void clear();
const fnd::List<SacEntry>& getServiceList() const;
void addService(const SacEntry& service);
private:
const std::string kModuleName = "SAC_BINARY";
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
fnd::List<SacEntry> mServices;
bool isEqual(const SacBinary& other) const;
void copyFrom(const SacBinary& other);
};
}

View file

@ -1,55 +0,0 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
class SacEntry :
public fnd::ISerialiseableBinary
{
public:
SacEntry();
SacEntry(const std::string& name, bool isServer);
SacEntry(const SacEntry& other);
bool operator==(const SacEntry& other) const;
bool operator!=(const SacEntry& other) const;
void operator=(const SacEntry& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
// variables
void clear();
bool isServer() const;
void setIsServer(bool isServer);
const std::string& getName() const;
void setName(const std::string& name);
private:
const std::string kModuleName = "SAC_ENTRY";
static const size_t kMaxServiceNameLen = 8;
enum SacEntryFlag
{
SAC_IS_SERVER = BIT(7),
SAC_NAME_LEN_MASK = BIT(7) - 1
};
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
bool mIsServer;
std::string mName;
bool isEqual(const SacEntry& other) const;
void copyFrom(const SacEntry& other);
};
}

View file

@ -0,0 +1,40 @@
#pragma once
#include <string>
#include <vector>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <nx/ServiceAccessControlEntry.h>
namespace nx
{
class ServiceAccessControlBinary :
public fnd::ISerialisable
{
public:
ServiceAccessControlBinary();
ServiceAccessControlBinary(const ServiceAccessControlBinary& other);
void operator=(const ServiceAccessControlBinary& other);
bool operator==(const ServiceAccessControlBinary& other) const;
bool operator!=(const ServiceAccessControlBinary& other) const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
const fnd::List<ServiceAccessControlEntry>& getServiceList() const;
void addService(const ServiceAccessControlEntry& service);
private:
const std::string kModuleName = "SERVICE_ACCESS_CONTROL_BINARY";
// raw binary
fnd::Vec<byte_t> mRawBinary;
// variables
fnd::List<ServiceAccessControlEntry> mServices;
};
}

View file

@ -0,0 +1,51 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/ISerialisable.h>
namespace nx
{
class ServiceAccessControlEntry :
public fnd::ISerialisable
{
public:
ServiceAccessControlEntry();
ServiceAccessControlEntry(const std::string& name, bool isServer);
ServiceAccessControlEntry(const ServiceAccessControlEntry& other);
void operator=(const ServiceAccessControlEntry& other);
bool operator==(const ServiceAccessControlEntry& other) const;
bool operator!=(const ServiceAccessControlEntry& other) const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
bool isServer() const;
void setIsServer(bool isServer);
const std::string& getName() const;
void setName(const std::string& name);
private:
const std::string kModuleName = "SERVICE_ACCESS_CONTROL_ENTRY";
static const size_t kMaxServiceNameLen = 8;
enum ServiceAccessControlEntryFlag
{
SAC_IS_SERVER = _BIT(7),
SAC_NAME_LEN_MASK = _BIT(7) - 1
};
// raw binary
fnd::Vec<byte_t> mRawBinary;
// variables
bool mIsServer;
std::string mName;
bool isEqual(const ServiceAccessControlEntry& other) const;
void copyFrom(const ServiceAccessControlEntry& other);
};
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -9,12 +9,16 @@ namespace nx
{
public:
SystemCallEntry();
SystemCallEntry(const KernelCapability& kernel_cap);
SystemCallEntry(const KernelCapabilityEntry& kernel_cap);
SystemCallEntry(uint32_t upper_bits, uint32_t lower_bits);
void operator=(const SystemCallEntry& other);
bool operator==(const SystemCallEntry& other) const;
bool operator!=(const SystemCallEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
uint32_t getSystemCallUpperBits() const;
@ -24,13 +28,13 @@ namespace nx
private:
const std::string kModuleName = "SYSTEM_CALL_ENTRY";
static const KernelCapability::KernelCapId kCapId = KernelCapability::KC_ENABLE_SYSTEM_CALLS;
static const kc::KernelCapId kCapId = kc::KC_ENABLE_SYSTEM_CALLS;
static const uint32_t kSysCallUpperBits = 3;
static const uint32_t kSysCallLowerBits = 24;
static const uint32_t kSysCallUpperMax = BIT(kSysCallUpperBits) - 1;
static const uint32_t kSysCallLowerMax = BIT(kSysCallLowerBits) - 1;
KernelCapability mCap;
KernelCapabilityEntry mCap;
uint32_t mSystemCallUpper;
uint32_t mSystemCallLower;

View file

@ -11,13 +11,13 @@ namespace nx
SystemCallHandler();
void operator=(const SystemCallHandler& other);
bool operator==(const SystemCallHandler& other) const;
bool operator!=(const SystemCallHandler& other) const;
void operator=(const SystemCallHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -31,9 +31,6 @@ namespace nx
bool mIsSet;
fnd::List<uint8_t> mSystemCalls;
void copyFrom(const SystemCallHandler& other);
bool isEqual(const SystemCallHandler& other) const;
};
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <nx/KernelCapability.h>
#include <nx/KernelCapabilityEntry.h>
namespace nx
{
@ -9,12 +9,16 @@ namespace nx
{
public:
ThreadInfoEntry();
ThreadInfoEntry(const KernelCapability& kernel_cap);
ThreadInfoEntry(const KernelCapabilityEntry& kernel_cap);
ThreadInfoEntry(uint8_t min_priority, uint8_t max_priority, uint8_t min_cpu_id, uint8_t max_cpu_id);
void operator=(const ThreadInfoEntry& other);
bool operator==(const ThreadInfoEntry& other) const;
bool operator!=(const ThreadInfoEntry& other) const;
// kernel capability
const KernelCapability& getKernelCapability() const;
void setKernelCapability(const KernelCapability& kernel_cap);
const KernelCapabilityEntry& getKernelCapability() const;
void setKernelCapability(const KernelCapabilityEntry& kernel_cap);
// variables
uint8_t getMinPriority() const;
@ -28,13 +32,13 @@ namespace nx
private:
const std::string kModuleName = "THREAD_INFO_ENTRY";
static const KernelCapability::KernelCapId kCapId = KernelCapability::KC_THREAD_INFO;
static const kc::KernelCapId kCapId = kc::KC_THREAD_INFO;
static const uint8_t kValBits = 6;
static const uint8_t kMaxVal = BIT(kValBits)-1;
static const uint8_t kDefaultPriority = 6;
static const uint8_t kDefaultCpuId = 8;
KernelCapability mCap;
KernelCapabilityEntry mCap;
uint8_t mMinPriority;
uint8_t mMaxPriority;
uint8_t mMinCpuId;

View file

@ -10,13 +10,13 @@ namespace nx
public:
ThreadInfoHandler();
void operator=(const ThreadInfoHandler& other);
bool operator==(const ThreadInfoHandler& other) const;
bool operator!=(const ThreadInfoHandler& other) const;
void operator=(const ThreadInfoHandler& other);
// kernel capabilty list in/out
void importKernelCapabilityList(const fnd::List<KernelCapability>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const;
void importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps);
void exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const;
void clear();
bool isSet() const;
@ -36,9 +36,6 @@ namespace nx
bool mIsSet;
ThreadInfoEntry mEntry;
void copyFrom(const ThreadInfoHandler& other);
bool isEqual(const ThreadInfoHandler& other) const;
};
}

View file

@ -1,30 +1,25 @@
#pragma once
#include <nx/xci.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialisable.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
class XciHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
XciHeader();
XciHeader(const XciHeader& other);
XciHeader(const byte_t* bytes, size_t len);
void operator=(const XciHeader& other);
bool operator==(const XciHeader& other) const;
bool operator!=(const XciHeader& other) const;
void operator=(const XciHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -92,7 +87,7 @@ namespace nx
const std::string kModuleName = "XCI_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
uint32_t mRomAreaStartPage;
@ -125,10 +120,6 @@ namespace nx
uint32_t mUppVersion;
byte_t mUppHash[8];
uint64_t mUppId;
// helpers
bool isEqual(const XciHeader& other) const;
void copyFrom(const XciHeader& other);
};
}

View file

@ -1,46 +1,53 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
#include <crypto/rsa.h>
#include <nx/macro.h>
namespace nx
{
namespace aci
{
static const uint32_t kAciStructSig = _MAKE_STRUCT_SIGNATURE("ACI0");
static const uint32_t kAciDescStructSig = _MAKE_STRUCT_SIGNATURE("ACID");
static const size_t kAciAlignSize = 0x10;
static const uint32_t kAciStructMagic = _MAKE_STRUCT_MAGIC_U32("ACI0");
static const uint32_t kAciDescStructMagic = _MAKE_STRUCT_MAGIC_U32("ACID");
static const size_t kSectionAlignSize = 0x10;
enum Flags
enum Flag
{
FLAG_PRODUCTION,
FLAG_UNQUALIFIED_APPROVAL
};
}
#pragma pack(push,1)
struct sAciSection
{
le_uint32_t offset;
le_uint32_t size;
};
struct sAciHeader
{
le_uint32_t signature;
le_uint32_t size; // includes prefacing signature, set only in ACID made by SDK (it enables easy resigning)
byte_t reserved_0[4];
le_uint32_t flags; // set in ACID only
union uProgramIdInfo
{
struct sRestrictProgramId
{
le_uint64_t min;
le_uint64_t max;
} program_id_restrict;
le_uint64_t program_id;
} program_id_info;
struct sAciSection
{
le_uint32_t offset; // aligned by 0x10 from the last one
le_uint32_t size;
} fac, sac, kc;
le_uint32_t st_magic;
byte_t reserved_00[0xC];
le_uint64_t program_id;
byte_t reserved_01[0x8];
sAciSection fac;
sAciSection sac;
sAciSection kc;
};
struct sAciDescHeader
{
byte_t signature[crypto::rsa::kRsa2048Size];
byte_t nca_rsa_signature2_modulus[crypto::rsa::kRsa2048Size];
le_uint32_t st_magic;
le_uint32_t signed_size;
byte_t reserved_00[0x4];
le_uint32_t flags;
le_uint64_t program_id_min;
le_uint64_t program_id_max;
sAciSection fac;
sAciSection sac;
sAciSection kc;
};
#pragma pack(pop)
}

View file

@ -1,9 +1,6 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{

View file

@ -0,0 +1,50 @@
#pragma once
#include <fnd/types.h>
namespace nx
{
namespace fac
{
static const uint32_t kFacFormatVersion = 1;
static const size_t kSectionAlignSize = 4;
enum FsAccessFlag
{
FSA_APPLICATION_INFO,
FSA_BOOT_MODE_CONTROL,
FSA_CALIBRATION,
FSA_SYSTEM_SAVE_DATA,
FSA_GAME_CARD,
FSA_SAVE_DATA_BACKUP,
FSA_SAVE_DATA_MANAGEMENT,
FSA_BIS_ALL_RAW,
FSA_GAME_CARD_RAW,
FSA_GAME_CARD_PRIVATE,
FSA_SET_TIME,
FSA_CONTENT_MANAGER,
FSA_IMAGE_MANAGER,
FSA_CREATE_SAVE_DATA,
FSA_SYSTEM_SAVE_DATA_MANAGEMENT,
FSA_BIS_FILE_SYSTEM,
FSA_SYSTEM_UPDATE,
FSA_SAVE_DATA_META,
FSA_DEVICE_SAVE_CONTROL,
FSA_SETTINGS_CONTROL,
FSA_DEBUG = 62,
FSA_FULL_PERMISSION = 63,
};
}
#pragma pack(push,1)
struct sFacHeader
{
le_uint32_t version; // default 1
le_uint64_t fac_flags;
struct sFacSection
{
le_uint32_t start;
le_uint32_t end;
} content_owner_ids, save_data_owner_ids; // the data for these follow later in binary. start/end relative to base of FacData instance
};
#pragma pack(pop)
}

View file

@ -1,16 +1,12 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/macro.h>
namespace nx
{
// Also known to the public as IVFC
namespace hierarchicalintegrity
{
static const uint32_t kStructSig = _MAKE_STRUCT_SIGNATURE("IVFC");
static const uint32_t kStructMagic = _MAKE_STRUCT_MAGIC_U32("IVFC");
static const uint32_t kRomfsTypeId = 0x20000;
static const size_t kDefaultLayerNum = 6;
static const size_t kHeaderAlignLen = 0x20;
@ -19,7 +15,7 @@ namespace nx
#pragma pack(push,1)
struct sHierarchicalIntegrityHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
le_uint32_t type_id;
le_uint32_t master_hash_size;
le_uint32_t layer_num;

View file

@ -1,8 +1,6 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{

24
lib/libnx/include/nx/kc.h Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include <fnd/types.h>
#include <crypto/rsa.h>
#include <nx/macro.h>
namespace nx
{
namespace kc
{
enum KernelCapId
{
KC_INVALID = 0,
KC_THREAD_INFO = 3,
KC_ENABLE_SYSTEM_CALLS = 4,
KC_MEMORY_MAP = 6,
KC_IO_MEMORY_MAP = 7,
KC_ENABLE_INTERUPTS = 11,
KC_MISC_PARAMS = 13,
KC_KERNEL_VERSION = 14,
KC_HANDLE_TABLE_SIZE = 15,
KC_MISC_FLAGS = 16
};
}
}

View file

@ -1,5 +1,5 @@
#pragma once
#include <cstdint>
#define _MAKE_STRUCT_SIGNATURE(x) ((uint32_t)(x[3]) << 24 | (uint32_t)(x[2]) << 16 | (uint32_t)(x[1]) << 8 | (uint32_t)(x[0]))
#define _MAKE_STRUCT_SIGNATURE_U64(x) ((uint64_t)(x[7]) << 56 | (uint64_t)(x[6]) << 48 | (uint64_t)(x[5]) << 40 | (uint64_t)(x[4]) << 32 | (uint64_t)(x[3]) << 24 | (uint64_t)(x[2]) << 16 | (uint64_t)(x[1]) << 8 | (uint64_t)(x[0]))
#define _MAKE_STRUCT_MAGIC_U32(x) ((uint32_t)(x[3]) << 24 | (uint32_t)(x[2]) << 16 | (uint32_t)(x[1]) << 8 | (uint32_t)(x[0]))
#define _MAKE_STRUCT_MAGIC_U64(x) ((uint64_t)(x[7]) << 56 | (uint64_t)(x[6]) << 48 | (uint64_t)(x[5]) << 40 | (uint64_t)(x[4]) << 32 | (uint64_t)(x[3]) << 24 | (uint64_t)(x[2]) << 16 | (uint64_t)(x[1]) << 8 | (uint64_t)(x[0]))

View file

@ -1,12 +1,22 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
namespace nacp
{
static const size_t kNameLength = 0x200;
static const size_t kPublisherLength = 0x100;
static const size_t kMaxLanguageCount = 16;
static const size_t kIsbnLength = 37;
static const size_t kRatingAgeCount = 32;
static const size_t kDisplayVersionLength = 16;
static const size_t kApplicationErrorCodeCategoryLength = 8;
static const size_t kLocalCommunicationIdCount = 8;
static const size_t kBcatPassphraseLength = 65;
static const size_t kPlayLogQueryableApplicationIdCount = 16;
static const int8_t kUnusedAgeRating = -1;
enum AocRegistrationType
{
AOC_AllOnLaunch,
@ -144,18 +154,6 @@ namespace nx
VCAP_Manual,
VCAP_Enable
};
static const size_t kNameLength = 0x200;
static const size_t kPublisherLength = 0x100;
static const size_t kMaxLanguageCount = 16;
static const size_t kIsbnLength = 37;
static const size_t kRatingAgeCount = 32;
static const size_t kDisplayVersionLength = 16;
static const size_t kApplicationErrorCodeCategoryLength = 8;
static const size_t kLocalCommunicationIdCount = 8;
static const size_t kBcatPassphraseLength = 65;
static const size_t kPlayLogQueryableApplicationIdCount = 16;
static const int8_t kUnusedAgeRating = -1;
}

View file

@ -1,18 +1,16 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <crypto/rsa.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/macro.h>
namespace nx
{
namespace nca
{
static const uint32_t kNca2Sig = _MAKE_STRUCT_SIGNATURE("NCA2");
static const uint32_t kNca3Sig = _MAKE_STRUCT_SIGNATURE("NCA3");
static const uint32_t kNca2StructMagic = _MAKE_STRUCT_MAGIC_U32("NCA2");
static const uint32_t kNca3StructMagic = _MAKE_STRUCT_MAGIC_U32("NCA3");
static const size_t kSectorSize = 0x200;
static const size_t kPartitionNum = 4;
static const size_t kHeaderSectorNum = 6;
@ -89,7 +87,7 @@ namespace nx
#pragma pack(push,1)
struct sNcaHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
byte_t distribution_type;
byte_t content_type;
byte_t key_generation;

View file

@ -1,20 +1,16 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/macro.h>
namespace nx
{
namespace npdm
{
static const uint32_t kNpdmStructSig = _MAKE_STRUCT_SIGNATURE("META");
static const uint32_t kNpdmStructMagic = _MAKE_STRUCT_MAGIC_U32("META");
static const size_t kNameMaxLen = 0x10;
static const size_t kProductCodeMaxLen = 0x10;
static const uint32_t kMaxPriority = BIT(6) -1 ;
static const size_t kNpdmAlignSize = 0x10;
static const uint32_t kMaxPriority = BIT(6) - 1;
static const size_t kSectionAlignSize = 0x10;
static const uint32_t kDefaultMainThreadStackSize = 4096;
enum InstructionType
@ -34,7 +30,7 @@ namespace nx
struct sNpdmHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
byte_t reserved_0[8];
byte_t flags;
byte_t reserved_1;

View file

@ -1,15 +1,12 @@
#pragma once
#include <fnd/types.h>
#include <crypto/sha.h>
#include <nx/macro.h>
namespace nx
{
namespace nro
{
static const uint32_t kNroSig = _MAKE_STRUCT_SIGNATURE("NRO0");
static const uint32_t kNroStructMagic = _MAKE_STRUCT_MAGIC_U32("NRO0");
static const uint32_t kDefaultFormatVersion = 0;
static const size_t kRoCrtSize = 8;
static const size_t kModuleIdSize = 32;
@ -26,7 +23,7 @@ namespace nx
{
byte_t ro_crt[nro::kRoCrtSize];
byte_t reserved_0[8];
le_uint32_t signature;
le_uint32_t st_magic;
le_uint32_t format_version;
le_uint32_t size;
le_uint32_t flags;

View file

@ -7,7 +7,7 @@ namespace nx
{
namespace nrr
{
static const uint32_t kNrrSig = _MAKE_STRUCT_SIGNATURE("NRR0");
static const uint32_t kNrrStructMagic = _MAKE_STRUCT_MAGIC_U32("NRR0");
}
#pragma pack(push,1)
@ -21,7 +21,7 @@ namespace nx
struct sNrrHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
byte_t reserved_0[28];
sNrrCertificate certificate;
byte_t nrr_body_signature[crypto::rsa::kRsa2048Size];

View file

@ -7,7 +7,9 @@ namespace nx
{
namespace nso
{
static const uint32_t kNsoSig = _MAKE_STRUCT_SIGNATURE("NSO0");
static const uint32_t kNsoStructMagic = _MAKE_STRUCT_MAGIC_U32("NSO0");
static const uint32_t kDefaultFormatVersion = 0;
static const size_t kModuleIdSize = 32;
enum HeaderFlags
{
@ -18,9 +20,6 @@ namespace nx
FLAG_RO_HASH,
FLAG_DATA_HASH
};
static const uint32_t kDefaultFormatVersion = 0;
static const size_t kModuleIdSize = 32;
}
#pragma pack(push,1)
@ -39,7 +38,7 @@ namespace nx
struct sNsoHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
le_uint32_t format_version;
byte_t reserved_1[4];
le_uint32_t flags;

View file

@ -1,22 +1,20 @@
#include <string>
#include <fnd/types.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/macro.h>
namespace nx
{
namespace pfs
{
static const uint32_t kPfsSig = _MAKE_STRUCT_SIGNATURE("PFS0");
static const uint32_t kHashedPfsSig = _MAKE_STRUCT_SIGNATURE("HFS0");
static const uint32_t kPfsStructMagic = _MAKE_STRUCT_MAGIC_U32("PFS0");
static const uint32_t kHashedPfsStructMagic = _MAKE_STRUCT_MAGIC_U32("HFS0");
static const size_t kHeaderAlign = 64;
}
#pragma pack(push,1)
struct sPfsHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
le_uint32_t file_num;
le_uint32_t name_table_size;
byte_t padding[4];

View file

@ -1,14 +1,13 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
namespace romfs
{
static const uint64_t kRomfsHeaderAlign = 0x200;
static const uint32_t kInvalidAddr = 0xffffffff;
enum HeaderSectionIndex
{
DIR_HASHMAP_TABLE,
@ -17,9 +16,6 @@ namespace nx
FILE_NODE_TABLE,
SECTION_NUM
};
static const uint64_t kRomfsHeaderAlign = 0x200;
static const uint32_t kInvalidAddr = 0xffffffff;
}
#pragma pack(push,1)

View file

@ -1,28 +1,28 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/List.h>
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <crypto/rsa.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/macro.h>
namespace nx
{
namespace xci
{
static const uint32_t kXciSig = _MAKE_STRUCT_SIGNATURE("HEAD");
static const uint32_t kXciStructMagic = _MAKE_STRUCT_MAGIC_U32("HEAD");
static const uint32_t kHeaderEncOffset = 0x90;
static const uint32_t kHeaderEncSize = 0x70;
static const uint32_t kPageSize = 0x200;
static const uint32_t kUppHashLen = 8;
/*
static const uint32_t kCardKeyAreaPageCount = 8;
static const uint32_t kCardHeaderPageCount = 1;
static const uint32_t kReservedAreaPageCount = 55;
static const uint32_t kCertAreaStartPageAddress = kCardHeaderPageCount + kReservedAreaPageCount + kCardKeyAreaPageCount;
static const uint32_t kCertAreaPageCount = 64;
static const uint32_t kNormalAreaStartPageAddress = kReservedAreaPageCount + kCertAreaPageCount + kCardHeaderPageCount + kCardKeyAreaPageCount;
*/
const std::string kUpdatePartitionStr = "update";
const std::string kLogoPartitionStr = "logo";
@ -68,7 +68,7 @@ namespace nx
#pragma pack(push,1)
struct sXciHeader
{
le_uint32_t signature;
le_uint32_t st_magic;
le_uint32_t rom_area_start_page;
le_uint32_t backup_area_start_page;
byte_t key_flag;

View file

@ -19,29 +19,32 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<None Include="makefile" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\nx\AccessControlInfoBinary.h" />
<ClInclude Include="include\nx\AccessControlInfoDescBinary.h" />
<ClInclude Include="include\nx\aci.h" />
<ClInclude Include="include\nx\AciBinary.h" />
<ClInclude Include="include\nx\AcidBinary.h" />
<ClInclude Include="include\nx\AciHeader.h" />
<ClInclude Include="include\nx\AesKeygen.h" />
<ClInclude Include="include\nx\ApplicationControlPropertyBinary.h" />
<ClInclude Include="include\nx\ApplicationControlPropertyUtils.h" />
<ClInclude Include="include\nx\cnmt.h" />
<ClInclude Include="include\nx\ContentMetaBinary.h" />
<ClInclude Include="include\nx\elf.h" />
<ClInclude Include="include\nx\FacBinary.h" />
<ClInclude Include="include\nx\FacHeader.h" />
<ClInclude Include="include\nx\fac.h" />
<ClInclude Include="include\nx\FileSystemAccessControlBinary.h" />
<ClInclude Include="include\nx\HandleTableSizeEntry.h" />
<ClInclude Include="include\nx\HandleTableSizeHandler.h" />
<ClInclude Include="include\nx\hierarchicalintegrity.h" />
<ClInclude Include="include\nx\HierarchicalIntegrityHeader.h" />
<ClInclude Include="include\nx\hierarchicalsha256.h" />
<ClInclude Include="include\nx\HierarchicalSha256Header.h" />
<ClInclude Include="include\nx\IKernelCapabilityHandler.h" />
<ClInclude Include="include\nx\InteruptEntry.h" />
<ClInclude Include="include\nx\InteruptHandler.h" />
<ClInclude Include="include\nx\hierarchicalintegrity.h" />
<ClInclude Include="include\nx\KcBinary.h" />
<ClInclude Include="include\nx\KernelCapability.h" />
<ClInclude Include="include\nx\kc.h" />
<ClInclude Include="include\nx\KernelCapabilityBinary.h" />
<ClInclude Include="include\nx\KernelCapabilityEntry.h" />
<ClInclude Include="include\nx\KernelVersionEntry.h" />
<ClInclude Include="include\nx\KernelVersionHandler.h" />
<ClInclude Include="include\nx\macro.h" />
@ -53,22 +56,20 @@
<ClInclude Include="include\nx\MiscParamsHandler.h" />
<ClInclude Include="include\nx\nacp.h" />
<ClInclude Include="include\nx\nca.h" />
<ClInclude Include="include\nx\NcaHeader.h" />
<ClInclude Include="include\nx\NcaUtils.h" />
<ClInclude Include="include\nx\npdm.h" />
<ClInclude Include="include\nx\NpdmBinary.h" />
<ClInclude Include="include\nx\NpdmHeader.h" />
<ClInclude Include="include\nx\NcaHeader.h" />
<ClInclude Include="include\nx\nro.h" />
<ClInclude Include="include\nx\NroHeader.h" />
<ClInclude Include="include\nx\nrr.h" />
<ClInclude Include="include\nx\nso.h" />
<ClInclude Include="include\nx\NsoHeader.h" />
<ClInclude Include="include\nx\NXCrypto.h" />
<ClInclude Include="include\nx\pfs.h" />
<ClInclude Include="include\nx\PfsHeader.h" />
<ClInclude Include="include\nx\romfs.h" />
<ClInclude Include="include\nx\SacBinary.h" />
<ClInclude Include="include\nx\SacEntry.h" />
<ClInclude Include="include\nx\ServiceAccessControlBinary.h" />
<ClInclude Include="include\nx\ServiceAccessControlEntry.h" />
<ClInclude Include="include\nx\SystemCallEntry.h" />
<ClInclude Include="include\nx\SystemCallHandler.h" />
<ClInclude Include="include\nx\ThreadInfoEntry.h" />
@ -78,26 +79,21 @@
<ClInclude Include="include\nx\XciUtils.h" />
</ItemGroup>
<ItemGroup>
<None Include="makefile" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="source\AciBinary.cpp" />
<ClCompile Include="source\AcidBinary.cpp" />
<ClCompile Include="source\AciHeader.cpp" />
<ClCompile Include="source\AccessControlInfoBinary.cpp" />
<ClCompile Include="source\AccessControlInfoDescBinary.cpp" />
<ClCompile Include="source\AesKeygen.cpp" />
<ClCompile Include="source\ApplicationControlPropertyBinary.cpp" />
<ClCompile Include="source\ApplicationControlPropertyUtils.cpp" />
<ClCompile Include="source\ContentMetaBinary.cpp" />
<ClCompile Include="source\FacBinary.cpp" />
<ClCompile Include="source\FacHeader.cpp" />
<ClCompile Include="source\FileSystemAccessControlBinary.cpp" />
<ClCompile Include="source\HandleTableSizeEntry.cpp" />
<ClCompile Include="source\HandleTableSizeHandler.cpp" />
<ClCompile Include="source\HierarchicalIntegrityHeader.cpp" />
<ClCompile Include="source\HierarchicalSha256Header.cpp" />
<ClCompile Include="source\InteruptEntry.cpp" />
<ClCompile Include="source\InteruptHandler.cpp" />
<ClCompile Include="source\KcBinary.cpp" />
<ClCompile Include="source\KernelCapability.cpp" />
<ClCompile Include="source\KernelCapabilityBinary.cpp" />
<ClCompile Include="source\KernelCapabilityEntry.cpp" />
<ClCompile Include="source\KernelVersionEntry.cpp" />
<ClCompile Include="source\KernelVersionHandler.cpp" />
<ClCompile Include="source\MemoryMappingHandler.cpp" />
@ -109,12 +105,11 @@
<ClCompile Include="source\NcaHeader.cpp" />
<ClCompile Include="source\NcaUtils.cpp" />
<ClCompile Include="source\NpdmBinary.cpp" />
<ClCompile Include="source\NpdmHeader.cpp" />
<ClCompile Include="source\NroHeader.cpp" />
<ClCompile Include="source\NsoHeader.cpp" />
<ClCompile Include="source\PfsHeader.cpp" />
<ClCompile Include="source\SacBinary.cpp" />
<ClCompile Include="source\SacEntry.cpp" />
<ClCompile Include="source\ServiceAccessControlBinary.cpp" />
<ClCompile Include="source\ServiceAccessControlEntry.cpp" />
<ClCompile Include="source\SystemCallEntry.cpp" />
<ClCompile Include="source\SystemCallHandler.cpp" />
<ClCompile Include="source\ThreadInfoEntry.cpp" />

View file

@ -15,19 +15,40 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\nx\AciBinary.h">
<None Include="makefile" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\nx\AccessControlInfoBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\AcidBinary.h">
<ClInclude Include="include\nx\AccessControlInfoDescBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\AciHeader.h">
<ClInclude Include="include\nx\aci.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\FacBinary.h">
<ClInclude Include="include\nx\AesKeygen.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\FacHeader.h">
<ClInclude Include="include\nx\ApplicationControlPropertyBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\ApplicationControlPropertyUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\cnmt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\ContentMetaBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\elf.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\fac.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\FileSystemAccessControlBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\HandleTableSizeEntry.h">
@ -36,6 +57,18 @@
<ClInclude Include="include\nx\HandleTableSizeHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\hierarchicalintegrity.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\HierarchicalIntegrityHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\hierarchicalsha256.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\HierarchicalSha256Header.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\IKernelCapabilityHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -45,10 +78,13 @@
<ClInclude Include="include\nx\InteruptHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\KcBinary.h">
<ClInclude Include="include\nx\kc.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\KernelCapability.h">
<ClInclude Include="include\nx\KernelCapabilityBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\KernelCapabilityEntry.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\KernelVersionEntry.h">
@ -57,6 +93,9 @@
<ClInclude Include="include\nx\KernelVersionHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\macro.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\MemoryMappingHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -75,25 +114,52 @@
<ClInclude Include="include\nx\MiscParamsHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NpdmBinary.h">
<ClInclude Include="include\nx\nacp.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NpdmHeader.h">
<ClInclude Include="include\nx\nca.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NcaHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NXCrypto.h">
<ClInclude Include="include\nx\NcaUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\npdm.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NpdmBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nro.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NroHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nrr.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nso.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NsoHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\pfs.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\PfsHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\SacBinary.h">
<ClInclude Include="include\nx\romfs.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\SacEntry.h">
<ClInclude Include="include\nx\ServiceAccessControlBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\ServiceAccessControlEntry.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\SystemCallEntry.h">
@ -108,108 +174,36 @@
<ClInclude Include="include\nx\ThreadInfoHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nca.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\pfs.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\xci.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\aci.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\AesKeygen.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NcaUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\npdm.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\romfs.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\XciHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\XciUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\hierarchicalsha256.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\ContentMetaBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\cnmt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\hierarchicalintegrity.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\HierarchicalSha256Header.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\HierarchicalIntegrityHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nso.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NsoHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\macro.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nro.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nrr.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\NroHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\ApplicationControlPropertyBinary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\ApplicationControlPropertyUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\nacp.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\nx\elf.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="makefile" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="source\AciBinary.cpp">
<ClCompile Include="source\AccessControlInfoBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\AcidBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\AciHeader.cpp">
<ClCompile Include="source\AccessControlInfoDescBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\AesKeygen.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\ApplicationControlPropertyBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\ApplicationControlPropertyUtils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\ContentMetaBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\FacBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\FacHeader.cpp">
<ClCompile Include="source\FileSystemAccessControlBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\HandleTableSizeEntry.cpp">
@ -230,10 +224,10 @@
<ClCompile Include="source\InteruptHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\KcBinary.cpp">
<ClCompile Include="source\KernelCapabilityBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\KernelCapability.cpp">
<ClCompile Include="source\KernelCapabilityEntry.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\KernelVersionEntry.cpp">
@ -269,9 +263,6 @@
<ClCompile Include="source\NpdmBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\NpdmHeader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\NroHeader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@ -281,10 +272,10 @@
<ClCompile Include="source\PfsHeader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\SacBinary.cpp">
<ClCompile Include="source\ServiceAccessControlBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\SacEntry.cpp">
<ClCompile Include="source\ServiceAccessControlEntry.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\SystemCallEntry.cpp">
@ -305,11 +296,5 @@
<ClCompile Include="source\XciUtils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\ApplicationControlPropertyBinary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\ApplicationControlPropertyUtils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View file

@ -0,0 +1,172 @@
#include <nx/AccessControlInfoBinary.h>
nx::AccessControlInfoBinary::AccessControlInfoBinary()
{
clear();
}
nx::AccessControlInfoBinary::AccessControlInfoBinary(const AccessControlInfoBinary & other)
{
*this = other;
}
void nx::AccessControlInfoBinary::operator=(const AccessControlInfoBinary & other)
{
mRawBinary = other.mRawBinary;
mProgramId = other.mProgramId;
mFileSystemAccessControl = other.mFileSystemAccessControl;
mServiceAccessControl = other.mServiceAccessControl;
mKernelCapabilities = other.mKernelCapabilities;
}
bool nx::AccessControlInfoBinary::operator==(const AccessControlInfoBinary & other) const
{
return (mProgramId == other.mProgramId) \
&& (mFileSystemAccessControl == other.mFileSystemAccessControl) \
&& (mServiceAccessControl == other.mServiceAccessControl) \
&& (mKernelCapabilities == other.mKernelCapabilities);
}
bool nx::AccessControlInfoBinary::operator!=(const AccessControlInfoBinary & other) const
{
return !(*this == other);
}
void nx::AccessControlInfoBinary::toBytes()
{
if (mFileSystemAccessControl.getBytes().size() == 0)
mFileSystemAccessControl.toBytes();
if (mServiceAccessControl.getBytes().size() == 0)
mServiceAccessControl.toBytes();
if (mKernelCapabilities.getBytes().size() == 0)
mKernelCapabilities.toBytes();
// determine section layout
struct sLayout {
uint32_t offset, size;
} fac, sac, kc;
fac.offset = align(sizeof(sAciHeader), aci::kSectionAlignSize);
fac.size = (uint32_t)mFileSystemAccessControl.getBytes().size();
sac.offset = align(fac.offset + fac.size, aci::kSectionAlignSize);
sac.size = (uint32_t)mServiceAccessControl.getBytes().size();
kc.offset = align(sac.offset + sac.size, aci::kSectionAlignSize);
kc.size = (uint32_t)mKernelCapabilities.getBytes().size();
// get total size
size_t total_size = _MAX(_MAX(fac.offset + fac.size, sac.offset + sac.size), kc.offset + kc.size);
mRawBinary.alloc(total_size);
sAciHeader* hdr = (sAciHeader*)mRawBinary.data();
// set type
hdr->st_magic = aci::kAciStructMagic;
// set program id
hdr->program_id = mProgramId;
// set offset/size
hdr->fac.offset = fac.offset;
hdr->fac.size = fac.size;
hdr->sac.offset = sac.offset;
hdr->sac.size = sac.size;
hdr->kc.offset = kc.offset;
hdr->kc.size = kc.size;
}
void nx::AccessControlInfoBinary::fromBytes(const byte_t* data, size_t len)
{
// check size
if (len < sizeof(sAciHeader))
{
throw fnd::Exception(kModuleName, "AccessControlInfo binary is too small");
}
// clear variables
clear();
// save a copy of the header
sAciHeader hdr;
memcpy((void*)&hdr, data, sizeof(sAciHeader));
// check magic
if (hdr.st_magic.get() != aci::kAciStructMagic)
{
throw fnd::Exception(kModuleName, "AccessControlInfo header corrupt");
}
// get total size
size_t total_size = _MAX(_MAX(hdr.fac.offset.get() + hdr.fac.size.get(), hdr.sac.offset.get() + hdr.sac.size.get()), hdr.kc.offset.get() + hdr.kc.size.get());
// validate binary size
if (len < total_size)
{
throw fnd::Exception(kModuleName, "AccessControlInfo binary is too small");
}
// allocate memory for header
mRawBinary.alloc(total_size);
memcpy(mRawBinary.data(), data, mRawBinary.size());
// save variables
mProgramId = hdr.program_id.get();
mFileSystemAccessControl.fromBytes(mRawBinary.data() + hdr.fac.offset.get(), hdr.fac.size.get());
mServiceAccessControl.fromBytes(mRawBinary.data() + hdr.sac.offset.get(), hdr.sac.size.get());
mKernelCapabilities.fromBytes(mRawBinary.data() + hdr.kc.offset.get(), hdr.kc.size.get());
}
const fnd::Vec<byte_t>& nx::AccessControlInfoBinary::getBytes() const
{
return mRawBinary;
}
void nx::AccessControlInfoBinary::clear()
{
mRawBinary.clear();
mProgramId = 0;
mFileSystemAccessControl.clear();
mServiceAccessControl.clear();
mKernelCapabilities.clear();
}
uint64_t nx::AccessControlInfoBinary::getProgramId() const
{
return mProgramId;
}
void nx::AccessControlInfoBinary::setProgramId(uint64_t program_id)
{
mProgramId = program_id;
}
const nx::FileSystemAccessControlBinary& nx::AccessControlInfoBinary::getFileSystemAccessControl() const
{
return mFileSystemAccessControl;
}
void nx::AccessControlInfoBinary::setFileSystemAccessControl(const nx::FileSystemAccessControlBinary& fac)
{
mFileSystemAccessControl = fac;
}
const nx::ServiceAccessControlBinary& nx::AccessControlInfoBinary::getServiceAccessControl() const
{
return mServiceAccessControl;
}
void nx::AccessControlInfoBinary::setServiceAccessControl(const nx::ServiceAccessControlBinary& sac)
{
mServiceAccessControl = sac;
}
const nx::KernelCapabilityBinary& nx::AccessControlInfoBinary::getKernelCapabilities() const
{
return mKernelCapabilities;
}
void nx::AccessControlInfoBinary::setKernelCapabilities(const nx::KernelCapabilityBinary& kc)
{
mKernelCapabilities = kc;
}

View file

@ -0,0 +1,250 @@
#include <nx/AccessControlInfoDescBinary.h>
nx::AccessControlInfoDescBinary::AccessControlInfoDescBinary()
{
clear();
}
nx::AccessControlInfoDescBinary::AccessControlInfoDescBinary(const AccessControlInfoDescBinary & other)
{
*this = other;
}
void nx::AccessControlInfoDescBinary::operator=(const AccessControlInfoDescBinary & other)
{
mRawBinary = other.mRawBinary;
mNcaHeaderSignature2Key = other.mNcaHeaderSignature2Key;
mFlags = other.mFlags;
mProgramIdRestrict = other.mProgramIdRestrict;
mFileSystemAccessControl = other.mFileSystemAccessControl;
mServiceAccessControl = other.mServiceAccessControl;
mKernelCapabilities = other.mKernelCapabilities;
}
bool nx::AccessControlInfoDescBinary::operator==(const AccessControlInfoDescBinary & other) const
{
return (mNcaHeaderSignature2Key == other.mNcaHeaderSignature2Key) \
&& (mFlags == other.mFlags) \
&& (mProgramIdRestrict == other.mProgramIdRestrict) \
&& (mFileSystemAccessControl == other.mFileSystemAccessControl) \
&& (mServiceAccessControl == other.mServiceAccessControl) \
&& (mKernelCapabilities == other.mKernelCapabilities);
}
bool nx::AccessControlInfoDescBinary::operator!=(const AccessControlInfoDescBinary & other) const
{
return !(*this == other);
}
void nx::AccessControlInfoDescBinary::toBytes()
{
if (mFileSystemAccessControl.getBytes().size() == 0)
mFileSystemAccessControl.toBytes();
if (mServiceAccessControl.getBytes().size() == 0)
mServiceAccessControl.toBytes();
if (mKernelCapabilities.getBytes().size() == 0)
mKernelCapabilities.toBytes();
// determine section layout
struct sLayout {
uint32_t offset, size;
} fac, sac, kc;
fac.offset = align(sizeof(sAciDescHeader), aci::kSectionAlignSize);
fac.size = (uint32_t)mFileSystemAccessControl.getBytes().size();
sac.offset = align(fac.offset + fac.size, aci::kSectionAlignSize);
sac.size = (uint32_t)mServiceAccessControl.getBytes().size();
kc.offset = align(sac.offset + sac.size, aci::kSectionAlignSize);
kc.size = (uint32_t)mKernelCapabilities.getBytes().size();
// get total size
size_t total_size = _MAX(_MAX(fac.offset + fac.size, sac.offset + sac.size), kc.offset + kc.size);
mRawBinary.alloc(total_size);
sAciDescHeader* hdr = (sAciDescHeader*)mRawBinary.data();
// set rsa modulus
memcpy(hdr->nca_rsa_signature2_modulus, mNcaHeaderSignature2Key.modulus, crypto::rsa::kRsa2048Size);
// set type
hdr->st_magic = aci::kAciDescStructMagic;
// set "acid size"
hdr->signed_size = total_size - crypto::rsa::kRsa2048Size;
// set flags
uint32_t flags = 0;
for (size_t i = 0; i < mFlags.size(); i++)
flags |= _BIT(mFlags[i]);
hdr->flags = flags;
// set program id restrict settings
hdr->program_id_min = mProgramIdRestrict.min;
hdr->program_id_max = mProgramIdRestrict.max;
// set offset/size
hdr->fac.offset = fac.offset;
hdr->fac.size = fac.size;
hdr->sac.offset = sac.offset;
hdr->sac.size = sac.size;
hdr->kc.offset = kc.offset;
hdr->kc.size = kc.size;
}
void nx::AccessControlInfoDescBinary::fromBytes(const byte_t* data, size_t len)
{
// check size
if (len < sizeof(sAciDescHeader))
{
throw fnd::Exception(kModuleName, "AccessControlInfoDesc binary is too small");
}
// clear variables
clear();
// save a copy of the header
sAciDescHeader hdr;
memcpy((void*)&hdr, data, sizeof(sAciDescHeader));
// check magic
if (hdr.st_magic.get() != aci::kAciDescStructMagic)
{
throw fnd::Exception(kModuleName, "AccessControlInfoDesc header corrupt");
}
// get total size
size_t total_size = _MAX(_MAX(hdr.fac.offset.get() + hdr.fac.size.get(), hdr.sac.offset.get() + hdr.sac.size.get()), hdr.kc.offset.get() + hdr.kc.size.get());
// validate binary size
if (len < total_size)
{
throw fnd::Exception(kModuleName, "AccessControlInfoDesc binary is too small");
}
// allocate memory for header
mRawBinary.alloc(total_size);
memcpy(mRawBinary.data(), data, mRawBinary.size());
// save variables
memcpy(mNcaHeaderSignature2Key.modulus, hdr.nca_rsa_signature2_modulus, crypto::rsa::kRsa2048Size);
for (size_t i = 0; i < 32; i++)
{
if (_HAS_BIT(hdr.flags.get(), i))
mFlags.addElement((aci::Flag)i);
}
mProgramIdRestrict.min = hdr.program_id_min.get();
mProgramIdRestrict.max = hdr.program_id_max.get();
mFileSystemAccessControl.fromBytes(mRawBinary.data() + hdr.fac.offset.get(), hdr.fac.size.get());
mServiceAccessControl.fromBytes(mRawBinary.data() + hdr.sac.offset.get(), hdr.sac.size.get());
mKernelCapabilities.fromBytes(mRawBinary.data() + hdr.kc.offset.get(), hdr.kc.size.get());
}
const fnd::Vec<byte_t>& nx::AccessControlInfoDescBinary::getBytes() const
{
return mRawBinary;
}
void nx::AccessControlInfoDescBinary::generateSignature(const crypto::rsa::sRsa2048Key& key)
{
if (mRawBinary.size() == 0)
toBytes();
byte_t hash[crypto::sha::kSha256HashLen];
crypto::sha::Sha256(mRawBinary.data() + crypto::rsa::kRsa2048Size, mRawBinary.size() - crypto::rsa::kRsa2048Size, hash);
if (crypto::rsa::pkcs::rsaSign(key, crypto::sha::HASH_SHA256, hash, mRawBinary.data()) != 0)
{
throw fnd::Exception(kModuleName, "Failed to sign Access Control Info Desc");
}
}
void nx::AccessControlInfoDescBinary::validateSignature(const crypto::rsa::sRsa2048Key& key) const
{
if (mRawBinary.size() == 0)
throw fnd::Exception(kModuleName, "No Access Control Info Desc binary exists to verify");
byte_t hash[crypto::sha::kSha256HashLen];
crypto::sha::Sha256(mRawBinary.data() + crypto::rsa::kRsa2048Size, mRawBinary.size() - crypto::rsa::kRsa2048Size, hash);
if (crypto::rsa::pss::rsaVerify(key, crypto::sha::HASH_SHA256, hash, mRawBinary.data()) != 0)
{
throw fnd::Exception(kModuleName, "Failed to verify Access Control Info Desc");
}
}
void nx::AccessControlInfoDescBinary::clear()
{
mRawBinary.clear();
memset((void*)&mNcaHeaderSignature2Key, 0, sizeof(mNcaHeaderSignature2Key));
mFlags.clear();
mProgramIdRestrict.min = 0;
mProgramIdRestrict.max = 0;
mFileSystemAccessControl.clear();
mServiceAccessControl.clear();
mKernelCapabilities.clear();
}
const crypto::rsa::sRsa2048Key& nx::AccessControlInfoDescBinary::getNcaHeaderSignature2Key() const
{
return mNcaHeaderSignature2Key;
}
void nx::AccessControlInfoDescBinary::setNcaHeaderSignature2Key(const crypto::rsa::sRsa2048Key& key)
{
mNcaHeaderSignature2Key = key;
}
const fnd::List<nx::aci::Flag>& nx::AccessControlInfoDescBinary::getFlagList() const
{
return mFlags;
}
void nx::AccessControlInfoDescBinary::setFlagList(const fnd::List<nx::aci::Flag>& flags)
{
mFlags = flags;
}
const nx::AccessControlInfoDescBinary::sProgramIdRestrict& nx::AccessControlInfoDescBinary::getProgramIdRestrict() const
{
return mProgramIdRestrict;
}
void nx::AccessControlInfoDescBinary::setProgramIdRestrict(const sProgramIdRestrict& pid_restrict)
{
mProgramIdRestrict = pid_restrict;
}
const nx::FileSystemAccessControlBinary& nx::AccessControlInfoDescBinary::getFileSystemAccessControl() const
{
return mFileSystemAccessControl;
}
void nx::AccessControlInfoDescBinary::setFileSystemAccessControl(const nx::FileSystemAccessControlBinary& fac)
{
mFileSystemAccessControl = fac;
}
const nx::ServiceAccessControlBinary& nx::AccessControlInfoDescBinary::getServiceAccessControl() const
{
return mServiceAccessControl;
}
void nx::AccessControlInfoDescBinary::setServiceAccessControl(const nx::ServiceAccessControlBinary& sac)
{
mServiceAccessControl = sac;
}
const nx::KernelCapabilityBinary& nx::AccessControlInfoDescBinary::getKernelCapabilities() const
{
return mKernelCapabilities;
}
void nx::AccessControlInfoDescBinary::setKernelCapabilities(const nx::KernelCapabilityBinary& kc)
{
mKernelCapabilities = kc;
}

View file

@ -1,158 +0,0 @@
#include <nx/AciBinary.h>
nx::AciBinary::AciBinary()
{
clear();
}
nx::AciBinary::AciBinary(const AciBinary & other)
{
copyFrom(other);
}
nx::AciBinary::AciBinary(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
bool nx::AciBinary::operator==(const AciBinary & other) const
{
return isEqual(other);
}
bool nx::AciBinary::operator!=(const AciBinary & other) const
{
return !isEqual(other);
}
void nx::AciBinary::operator=(const AciBinary & other)
{
copyFrom(other);
}
const byte_t * nx::AciBinary::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t nx::AciBinary::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::AciBinary::exportBinary()
{
// export components
mFac.exportBinary();
mSac.exportBinary();
mKc.exportBinary();
// set sizes
setFacSize(mFac.getSize());
setSacSize(mSac.getSize());
setKcSize(mKc.getSize());
// export header
AciHeader::exportBinary();
// allocate binary
mBinaryBlob.alloc(getAciSize());
// copy header
memcpy(mBinaryBlob.getBytes(), AciHeader::getBytes(), AciHeader::getSize());
// copy components
memcpy(mBinaryBlob.getBytes() + getFacPos().offset, mFac.getBytes(), mFac.getSize());
memcpy(mBinaryBlob.getBytes() + getSacPos().offset, mSac.getBytes(), mSac.getSize());
memcpy(mBinaryBlob.getBytes() + getKcPos().offset, mKc.getBytes(), mKc.getSize());
}
void nx::AciBinary::importBinary(const byte_t * bytes, size_t len)
{
AciHeader::importBinary(bytes, len);
if (getAciSize() > len)
{
throw fnd::Exception(kModuleName, "ACI binary too small");
}
mBinaryBlob.alloc(getAciSize());
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
if (getFacPos().size > 0)
{
mFac.importBinary(mBinaryBlob.getBytes() + getFacPos().offset, getFacPos().size);
}
if (getSacPos().size > 0)
{
mSac.importBinary(mBinaryBlob.getBytes() + getSacPos().offset, getSacPos().size);
}
if (getKcPos().size > 0)
{
mKc.importBinary(mBinaryBlob.getBytes() + getKcPos().offset, getKcPos().size);
}
}
void nx::AciBinary::clear()
{
AciHeader::clear();
mFac.clear();
mSac.clear();
mKc.clear();
}
const nx::FacBinary & nx::AciBinary::getFac() const
{
return mFac;
}
void nx::AciBinary::setFac(const FacBinary & fac)
{
mFac = fac;
}
const nx::SacBinary & nx::AciBinary::getSac() const
{
return mSac;
}
void nx::AciBinary::setSac(const SacBinary & sac)
{
mSac = sac;
}
const nx::KcBinary & nx::AciBinary::getKc() const
{
return mKc;
}
void nx::AciBinary::setKc(const KcBinary & kc)
{
mKc = kc;
}
bool nx::AciBinary::isEqual(const AciBinary & other) const
{
return (AciHeader::operator==(other)) \
&& (mFac == other.mFac) \
&& (mSac == other.mSac) \
&& (mKc == other.mKc);
}
void nx::AciBinary::copyFrom(const AciBinary & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
AciHeader::operator=(other);
mFac = other.mFac;
mSac = other.mSac;
mKc = other.mKc;
}
}

View file

@ -1,324 +0,0 @@
#include <nx/AciHeader.h>
using namespace nx;
void AciHeader::calculateSectionOffsets()
{
mFac.offset = align(mHeaderOffset, aci::kAciAlignSize) + align(sizeof(sAciHeader), aci::kAciAlignSize);
mSac.offset = mFac.offset + align(mFac.size, aci::kAciAlignSize);
mKc.offset = mSac.offset + align(mSac.size, aci::kAciAlignSize);
}
bool AciHeader::isEqual(const AciHeader & other) const
{
return (mHeaderOffset == other.mHeaderOffset) \
&& (mType == other.mType) \
&& (mIsProduction == other.mIsProduction) \
&& (mIsUnqualifiedApproval == other.mIsUnqualifiedApproval) \
&& (mAcidSize == other.mAcidSize) \
&& (mProgramIdMin == other.mProgramIdMin) \
&& (mProgramIdMax == other.mProgramIdMax) \
&& (mProgramId == other.mProgramId) \
&& (mFac == other.mFac) \
&& (mSac == other.mSac) \
&& (mKc == other.mKc);
}
void AciHeader::copyFrom(const AciHeader & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
mHeaderOffset = other.mHeaderOffset;
mType = other.mType;
mIsProduction = other.mIsProduction;
mIsUnqualifiedApproval = other.mIsUnqualifiedApproval;
mAcidSize = other.mAcidSize;
mProgramIdMin = other.mProgramIdMin;
mProgramIdMax = other.mProgramIdMax;
mProgramId = other.mProgramId;
mFac = other.mFac;
mSac = other.mSac;
mKc = other.mKc;
}
}
AciHeader::AciHeader()
{
clear();
}
AciHeader::AciHeader(const AciHeader & other)
{
importBinary(other.getBytes(), other.getSize());
}
AciHeader::AciHeader(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
bool AciHeader::operator==(const AciHeader & other) const
{
return isEqual(other);
}
bool AciHeader::operator!=(const AciHeader & other) const
{
return !isEqual(other);
}
void AciHeader::operator=(const AciHeader & other)
{
this->importBinary(other.getBytes(), other.getSize());
}
const byte_t * AciHeader::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t AciHeader::getSize() const
{
return mBinaryBlob.getSize();
}
void AciHeader::exportBinary()
{
mBinaryBlob.alloc(sizeof(sAciHeader));
sAciHeader* hdr = (sAciHeader*)mBinaryBlob.getBytes();
// set type
switch (mType)
{
case (TYPE_ACI0):
hdr->signature = aci::kAciStructSig;
break;
case (TYPE_ACID):
hdr->signature = aci::kAciDescStructSig;
break;
default:
throw fnd::Exception(kModuleName, "Unexpected ACI type");
}
// set offset/size
calculateSectionOffsets();
hdr->fac.offset = (uint32_t)mFac.offset;
hdr->fac.size = (uint32_t)mFac.size;
hdr->sac.offset = (uint32_t)mSac.offset;
hdr->sac.size = (uint32_t)mSac.size;
hdr->kc.offset = (uint32_t)mKc.offset;
hdr->kc.size = (uint32_t)mKc.size;
uint32_t flags = 0;
if (mIsProduction)
flags |= _BIT(aci::FLAG_PRODUCTION);
if (mIsUnqualifiedApproval)
flags |= _BIT(aci::FLAG_UNQUALIFIED_APPROVAL);
hdr->flags = flags;
if (mType == TYPE_ACI0)
{
// set program
hdr->program_id_info.program_id = mProgramId;
}
else if (mType == TYPE_ACID)
{
mAcidSize = getAciSize();
hdr->size = (uint32_t)mAcidSize;
hdr->program_id_info.program_id_restrict.min = mProgramIdMin;
hdr->program_id_info.program_id_restrict.max = mProgramIdMax;
}
}
void AciHeader::importBinary(const byte_t * bytes, size_t len)
{
if (len < sizeof(sAciHeader))
{
throw fnd::Exception(kModuleName, "ACI header too small");
}
clear();
mBinaryBlob.alloc(sizeof(sAciHeader));
memcpy(mBinaryBlob.getBytes(), bytes, sizeof(sAciHeader));
sAciHeader* hdr = (sAciHeader*)mBinaryBlob.getBytes();
switch (hdr->signature.get())
{
case (aci::kAciStructSig):
mType = TYPE_ACI0;
break;
case (aci::kAciDescStructSig):
mType = TYPE_ACID;
break;
default:
throw fnd::Exception(kModuleName, "ACI header corrupt");
}
if (mType == TYPE_ACI0)
{
mProgramId = hdr->program_id_info.program_id.get();
mIsProduction = false;
mIsUnqualifiedApproval = false;
mAcidSize = 0;
mProgramIdMin = 0;
mProgramIdMax = 0;
}
else if (mType == TYPE_ACID)
{
mProgramId = 0;
mIsProduction = _HAS_BIT(hdr->flags.get(), aci::FLAG_PRODUCTION);
mIsUnqualifiedApproval = _HAS_BIT(hdr->flags.get(), aci::FLAG_UNQUALIFIED_APPROVAL);
mAcidSize = hdr->size.get();
mProgramIdMin = hdr->program_id_info.program_id_restrict.min.get();
mProgramIdMax = hdr->program_id_info.program_id_restrict.max.get();
}
// the header offset is the MIN(sac.offset, fac.offset, kc.offset) - sizeof(sHeader)
mHeaderOffset = MAX(MIN(hdr->sac.offset.get(), MIN(hdr->fac.offset.get(), hdr->kc.offset.get())), align(sizeof(sAciHeader), aci::kAciAlignSize)) - align(sizeof(sAciHeader), aci::kAciAlignSize);
mFac.offset = hdr->fac.offset.get() - mHeaderOffset;
mFac.size = hdr->fac.size.get();
mSac.offset = hdr->sac.offset.get() - mHeaderOffset;
mSac.size = hdr->sac.size.get();
mKc.offset = hdr->kc.offset.get() - mHeaderOffset;
mKc.size = hdr->kc.size.get();
}
void nx::AciHeader::clear()
{
mBinaryBlob.clear();
mHeaderOffset = 0;
mType = TYPE_ACI0;
mProgramId = 0;
mProgramIdMin = 0;
mProgramIdMax = 0;
mAcidSize = 0;
mIsProduction = false;
mIsUnqualifiedApproval = false;
mFac.offset = 0;
mFac.size = 0;
mSac.offset = 0;
mSac.size = 0;
mKc.offset = 0;
mKc.size = 0;
}
size_t nx::AciHeader::getAciSize() const
{
return MAX(MAX(MAX(mSac.offset + mSac.size, mKc.offset + mKc.size), mFac.offset + mFac.size), sizeof(sAciHeader));
}
size_t nx::AciHeader::getAcidSize() const
{
return mAcidSize;
}
/*
void nx::AciHeader::setAcidSize(size_t size)
{
mAcidSize = size;
}
*/
uint64_t nx::AciHeader::getProgramIdMin() const
{
return mProgramIdMin;
}
void nx::AciHeader::setProgramIdMin(uint64_t program_id)
{
mProgramIdMin = program_id;
}
uint64_t nx::AciHeader::getProgramIdMax() const
{
return mProgramIdMax;
}
void nx::AciHeader::setProgramIdMax(uint64_t program_id)
{
mProgramIdMax = program_id;
}
void nx::AciHeader::setHeaderOffset(size_t offset)
{
mHeaderOffset = offset;
}
AciHeader::AciType AciHeader::getAciType() const
{
return mType;
}
void AciHeader::setAciType(AciType type)
{
mType = type;
}
bool nx::AciHeader::isProduction() const
{
return mIsProduction;
}
void nx::AciHeader::setIsProduction(bool isProduction)
{
mIsProduction = isProduction;
}
bool nx::AciHeader::isUnqualifiedApproval() const
{
return mIsUnqualifiedApproval;
}
void nx::AciHeader::setIsUnqualifiedApproval(bool isUnqualifiedApproval)
{
mIsUnqualifiedApproval = isUnqualifiedApproval;
}
uint64_t AciHeader::getProgramId() const
{
return mProgramId;
}
void AciHeader::setProgramId(uint64_t program_id)
{
mProgramId = program_id;
}
const AciHeader::sSection & AciHeader::getFacPos() const
{
return mFac;
}
void AciHeader::setFacSize(size_t size)
{
mFac.size = size;
}
const AciHeader::sSection & AciHeader::getSacPos() const
{
return mSac;
}
void AciHeader::setSacSize(size_t size)
{
mSac.size = size;
}
const AciHeader::sSection & AciHeader::getKcPos() const
{
return mKc;
}
void AciHeader::setKcSize(size_t size)
{
mKc.size = size;
}

View file

@ -1,142 +0,0 @@
#include <nx/AcidBinary.h>
nx::AcidBinary::AcidBinary()
{
clear();
}
nx::AcidBinary::AcidBinary(const AcidBinary & other)
{
copyFrom(other);
}
nx::AcidBinary::AcidBinary(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
void nx::AcidBinary::clear()
{
AciBinary::clear();
mEmbeddedPublicKey = crypto::rsa::sRsa2048Key();
}
const crypto::rsa::sRsa2048Key & nx::AcidBinary::getNcaHeader2RsaKey() const
{
return mEmbeddedPublicKey;
}
void nx::AcidBinary::setNcaHeader2RsaKey(const crypto::rsa::sRsa2048Key & key)
{
mEmbeddedPublicKey = key;
}
bool nx::AcidBinary::isEqual(const AcidBinary & other) const
{
return AciBinary::operator==(other) \
&& (mEmbeddedPublicKey == other.mEmbeddedPublicKey);
}
void nx::AcidBinary::copyFrom(const AcidBinary & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
AciBinary::operator=(other);
mEmbeddedPublicKey = other.mEmbeddedPublicKey;
}
}
bool nx::AcidBinary::operator==(const AcidBinary & other) const
{
return isEqual(other);
}
bool nx::AcidBinary::operator!=(const AcidBinary & other) const
{
return !isEqual(other);
}
void nx::AcidBinary::operator=(const AcidBinary & other)
{
copyFrom(other);
}
const byte_t * nx::AcidBinary::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t nx::AcidBinary::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::AcidBinary::exportBinary()
{
AciBinary::setHeaderOffset(crypto::rsa::kRsa2048Size); // not include signature
AciBinary::exportBinary();
mBinaryBlob.alloc(AciBinary::getSize() + crypto::rsa::kRsa2048Size * 2);
memcpy(mBinaryBlob.getBytes() + crypto::rsa::kRsa2048Size, mEmbeddedPublicKey.modulus, crypto::rsa::kRsa2048Size);
memcpy(mBinaryBlob.getBytes() + crypto::rsa::kRsa2048Size * 2, AciBinary::getBytes(), AciBinary::getSize());
}
void nx::AcidBinary::signBinary(const crypto::rsa::sRsa2048Key & key)
{
if (mBinaryBlob.getSize() == 0)
{
exportBinary();
}
byte_t hash[crypto::sha::kSha256HashLen];
crypto::sha::Sha256(mBinaryBlob.getBytes() + crypto::rsa::kRsa2048Size, mBinaryBlob.getSize() - crypto::rsa::kRsa2048Size, hash);
if (crypto::rsa::pkcs::rsaSign(key, crypto::sha::HASH_SHA256, hash, mBinaryBlob.getBytes()) != 0)
{
throw fnd::Exception(kModuleName, "Failed to sign ACID");
}
}
void nx::AcidBinary::importBinary(const byte_t * bytes, size_t len)
{
if (len <= crypto::rsa::kRsa2048Size*2)
{
throw fnd::Exception(kModuleName, "ACID binary too small");
}
// import aci binary past sig + pubkey
AciBinary::importBinary(bytes + crypto::rsa::kRsa2048Size * 2, len - crypto::rsa::kRsa2048Size * 2);
// save internal copy
size_t acid_size = AciBinary::getSize() + crypto::rsa::kRsa2048Size * 2;
if (acid_size > len)
{
throw fnd::Exception(kModuleName, "ACID binary too small");
}
mBinaryBlob.alloc(acid_size);
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
memcpy(mEmbeddedPublicKey.modulus, bytes + crypto::rsa::kRsa2048Size, crypto::rsa::kRsa2048Size);
}
void nx::AcidBinary::verifyBinary(const crypto::rsa::sRsa2048Key & key) const
{
if (mBinaryBlob.getSize() == 0)
{
throw fnd::Exception(kModuleName, "No ACID binary exists to verify");
}
byte_t hash[crypto::sha::kSha256HashLen];
crypto::sha::Sha256(mBinaryBlob.getBytes() + crypto::rsa::kRsa2048Size, mBinaryBlob.getSize() - crypto::rsa::kRsa2048Size, hash);
if (crypto::rsa::pss::rsaVerify(key, crypto::sha::HASH_SHA256, hash, mBinaryBlob.getBytes()) != 0)
{
throw fnd::Exception(kModuleName, "Failed to verify ACID");
}
}

View file

@ -7,17 +7,92 @@ nx::ApplicationControlPropertyBinary::ApplicationControlPropertyBinary()
nx::ApplicationControlPropertyBinary::ApplicationControlPropertyBinary(const ApplicationControlPropertyBinary& other)
{
copyFrom(other);
*this = other;
}
nx::ApplicationControlPropertyBinary::ApplicationControlPropertyBinary(const byte_t* bytes, size_t len)
void nx::ApplicationControlPropertyBinary::operator=(const ApplicationControlPropertyBinary& other)
{
importBinary(bytes, len);
clear();
mTitle = other.mTitle;
mIsbn = other.mIsbn;
mStartupUserAccount = other.mStartupUserAccount;
mTouchScreenUsageMode = other.mTouchScreenUsageMode;
mAocRegistrationType = other.mAocRegistrationType;
mAttributeFlag = other.mAttributeFlag;
mParentalControlFlag = other.mParentalControlFlag;
mScreenshotMode = other.mScreenshotMode;
mVideoCaptureMode = other.mVideoCaptureMode;
mDataLossConfirmation = other.mDataLossConfirmation;
mPlayLogPolicy = other.mPlayLogPolicy;
mPresenceGroupId = other.mPresenceGroupId;
mRatingAge = other.mRatingAge;
mDisplayVersion = other.mDisplayVersion;
mAocBaseId = other.mAocBaseId;
mSaveDatawOwnerId = other.mSaveDatawOwnerId;
mUserAccountSaveDataSize = other.mUserAccountSaveDataSize;
mDeviceSaveDataSize = other.mDeviceSaveDataSize;
mBcatDeliveryCacheStorageSize = other.mBcatDeliveryCacheStorageSize;
mApplicationErrorCodeCategory = other.mApplicationErrorCodeCategory;
mLocalCommunicationId = other.mLocalCommunicationId;
mLogoType = other.mLogoType;
mLogoHandling = other.mLogoHandling;
mRuntimeAocInstallMode = other.mRuntimeAocInstallMode;
mCrashReportMode = other.mCrashReportMode;
mHdcp = other.mHdcp;
mSeedForPsuedoDeviceId = other.mSeedForPsuedoDeviceId;
mBcatPassphase = other.mBcatPassphase;
mUserAccountSaveDataMax = other.mUserAccountSaveDataMax;
mDeviceSaveDataMax = other.mDeviceSaveDataMax;
mTemporaryStorageSize = other.mTemporaryStorageSize;
mCacheStorageSize = other.mCacheStorageSize;
mCacheStorageDataAndJournalSizeMax = other.mCacheStorageDataAndJournalSizeMax;
mCacheStorageIndex = other.mCacheStorageIndex;
mPlayLogQueryableApplicationId = other.mPlayLogQueryableApplicationId;
mPlayLogQueryCapability = other.mPlayLogQueryCapability;
mRepairFlag = other.mRepairFlag;
mProgramIndex = other.mProgramIndex;
}
bool nx::ApplicationControlPropertyBinary::operator==(const ApplicationControlPropertyBinary& other) const
{
return isEqual(other);
return (mTitle == other.mTitle) \
&& (mIsbn == other.mIsbn) \
&& (mStartupUserAccount == other.mStartupUserAccount) \
&& (mTouchScreenUsageMode == other.mTouchScreenUsageMode) \
&& (mAocRegistrationType == other.mAocRegistrationType) \
&& (mAttributeFlag == other.mAttributeFlag) \
&& (mParentalControlFlag == other.mParentalControlFlag) \
&& (mScreenshotMode == other.mScreenshotMode) \
&& (mVideoCaptureMode == other.mVideoCaptureMode) \
&& (mDataLossConfirmation == other.mDataLossConfirmation) \
&& (mPlayLogPolicy == other.mPlayLogPolicy) \
&& (mPresenceGroupId == other.mPresenceGroupId) \
&& (mRatingAge == other.mRatingAge) \
&& (mDisplayVersion == other.mDisplayVersion) \
&& (mAocBaseId == other.mAocBaseId) \
&& (mSaveDatawOwnerId == other.mSaveDatawOwnerId) \
&& (mUserAccountSaveDataSize == other.mUserAccountSaveDataSize) \
&& (mDeviceSaveDataSize == other.mDeviceSaveDataSize) \
&& (mBcatDeliveryCacheStorageSize == other.mBcatDeliveryCacheStorageSize) \
&& (mApplicationErrorCodeCategory == other.mApplicationErrorCodeCategory) \
&& (mLocalCommunicationId == other.mLocalCommunicationId) \
&& (mLogoType == other.mLogoType) \
&& (mLogoHandling == other.mLogoHandling) \
&& (mRuntimeAocInstallMode == other.mRuntimeAocInstallMode) \
&& (mCrashReportMode == other.mCrashReportMode) \
&& (mHdcp == other.mHdcp) \
&& (mSeedForPsuedoDeviceId == other.mSeedForPsuedoDeviceId) \
&& (mBcatPassphase == other.mBcatPassphase) \
&& (mUserAccountSaveDataMax == other.mUserAccountSaveDataMax) \
&& (mDeviceSaveDataMax == other.mDeviceSaveDataMax) \
&& (mTemporaryStorageSize == other.mTemporaryStorageSize) \
&& (mCacheStorageSize == other.mCacheStorageSize) \
&& (mCacheStorageDataAndJournalSizeMax == other.mCacheStorageDataAndJournalSizeMax) \
&& (mCacheStorageIndex == other.mCacheStorageIndex) \
&& (mPlayLogQueryableApplicationId == other.mPlayLogQueryableApplicationId) \
&& (mPlayLogQueryCapability == other.mPlayLogQueryCapability) \
&& (mRepairFlag == other.mRepairFlag) \
&& (mProgramIndex == other.mProgramIndex);
}
bool nx::ApplicationControlPropertyBinary::operator!=(const ApplicationControlPropertyBinary& other) const
@ -25,30 +100,15 @@ bool nx::ApplicationControlPropertyBinary::operator!=(const ApplicationControlPr
return !(*this == other);
}
void nx::ApplicationControlPropertyBinary::operator=(const ApplicationControlPropertyBinary& other)
void nx::ApplicationControlPropertyBinary::toBytes()
{
copyFrom(other);
}
mRawBinary.alloc(sizeof(nx::sApplicationControlProperty));
const byte_t* nx::ApplicationControlPropertyBinary::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t nx::ApplicationControlPropertyBinary::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::ApplicationControlPropertyBinary::exportBinary()
{
mBinaryBlob.alloc(sizeof(nx::sApplicationControlProperty));
sApplicationControlProperty* data = (sApplicationControlProperty*)mBinaryBlob.getBytes();
sApplicationControlProperty* data = (sApplicationControlProperty*)mRawBinary.data();
// strings
uint32_t supported_langs = 0;
for (size_t i = 0; i < mTitle.getSize(); i++)
for (size_t i = 0; i < mTitle.size(); i++)
{
supported_langs = _BIT(mTitle[i].language);
strncpy(data->title[mTitle[i].language].name, mTitle[i].name.c_str(), nacp::kNameLength);
@ -82,18 +142,18 @@ void nx::ApplicationControlPropertyBinary::exportBinary()
// misc params
data->presence_group_id = mPresenceGroupId;
memset(data->rating_age, nacp::kUnusedAgeRating, nacp::kRatingAgeCount); // clear ratings
for (size_t i = 0; i < mRatingAge.getSize(); i++)
for (size_t i = 0; i < mRatingAge.size(); i++)
{
data->rating_age[mRatingAge[i].organisation] = mRatingAge[i].age;
}
data->add_on_content_base_id = mAocBaseId;
data->save_data_owner_id = mSaveDatawOwnerId;
for (size_t i = 0; i < mLocalCommunicationId.getSize() && i < nacp::kLocalCommunicationIdCount; i++)
for (size_t i = 0; i < mLocalCommunicationId.size() && i < nacp::kLocalCommunicationIdCount; i++)
{
data->local_communication_id[i] = mLocalCommunicationId[i];
}
data->seed_for_pseudo_device_id = mSeedForPsuedoDeviceId;
for (size_t i = 0; i < mPlayLogQueryableApplicationId.getSize() && i < nacp::kPlayLogQueryableApplicationIdCount; i++)
for (size_t i = 0; i < mPlayLogQueryableApplicationId.size() && i < nacp::kPlayLogQueryableApplicationIdCount; i++)
{
data->play_log_queryable_application_id[i] = mPlayLogQueryableApplicationId[i];
}
@ -116,7 +176,7 @@ void nx::ApplicationControlPropertyBinary::exportBinary()
data->cache_storage_data_and_journal_size_max = mCacheStorageDataAndJournalSizeMax;
}
void nx::ApplicationControlPropertyBinary::importBinary(const byte_t* bytes, size_t len)
void nx::ApplicationControlPropertyBinary::fromBytes(const byte_t* bytes, size_t len)
{
if (len < sizeof(nx::sApplicationControlProperty))
{
@ -125,10 +185,10 @@ void nx::ApplicationControlPropertyBinary::importBinary(const byte_t* bytes, siz
clear();
mBinaryBlob.alloc(sizeof(nx::sApplicationControlProperty));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
mRawBinary.alloc(sizeof(nx::sApplicationControlProperty));
memcpy(mRawBinary.data(), bytes, mRawBinary.size());
const sApplicationControlProperty* data = (const sApplicationControlProperty*)mBinaryBlob.getBytes();
const sApplicationControlProperty* data = (const sApplicationControlProperty*)mRawBinary.data();
// strings
for (size_t i = 0; i < nacp::kMaxLanguageCount; i++)
@ -204,9 +264,14 @@ void nx::ApplicationControlPropertyBinary::importBinary(const byte_t* bytes, siz
mCacheStorageDataAndJournalSizeMax = (int64_t)data->cache_storage_data_and_journal_size_max.get();
}
const fnd::Vec<byte_t>& nx::ApplicationControlPropertyBinary::getBytes() const
{
return mRawBinary;
}
void nx::ApplicationControlPropertyBinary::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mTitle.clear();
mIsbn.clear();
mStartupUserAccount = nacp::USER_None;
@ -625,89 +690,4 @@ byte_t nx::ApplicationControlPropertyBinary::getProgramIndex() const
void nx::ApplicationControlPropertyBinary::setProgramIndex(byte_t var)
{
mProgramIndex = var;
}
bool nx::ApplicationControlPropertyBinary::isEqual(const ApplicationControlPropertyBinary& other) const
{
return (mTitle == other.mTitle) \
&& (mIsbn == other.mIsbn) \
&& (mStartupUserAccount == other.mStartupUserAccount) \
&& (mTouchScreenUsageMode == other.mTouchScreenUsageMode) \
&& (mAocRegistrationType == other.mAocRegistrationType) \
&& (mAttributeFlag == other.mAttributeFlag) \
&& (mParentalControlFlag == other.mParentalControlFlag) \
&& (mScreenshotMode == other.mScreenshotMode) \
&& (mVideoCaptureMode == other.mVideoCaptureMode) \
&& (mDataLossConfirmation == other.mDataLossConfirmation) \
&& (mPlayLogPolicy == other.mPlayLogPolicy) \
&& (mPresenceGroupId == other.mPresenceGroupId) \
&& (mRatingAge == other.mRatingAge) \
&& (mDisplayVersion == other.mDisplayVersion) \
&& (mAocBaseId == other.mAocBaseId) \
&& (mSaveDatawOwnerId == other.mSaveDatawOwnerId) \
&& (mUserAccountSaveDataSize == other.mUserAccountSaveDataSize) \
&& (mDeviceSaveDataSize == other.mDeviceSaveDataSize) \
&& (mBcatDeliveryCacheStorageSize == other.mBcatDeliveryCacheStorageSize) \
&& (mApplicationErrorCodeCategory == other.mApplicationErrorCodeCategory) \
&& (mLocalCommunicationId == other.mLocalCommunicationId) \
&& (mLogoType == other.mLogoType) \
&& (mLogoHandling == other.mLogoHandling) \
&& (mRuntimeAocInstallMode == other.mRuntimeAocInstallMode) \
&& (mCrashReportMode == other.mCrashReportMode) \
&& (mHdcp == other.mHdcp) \
&& (mSeedForPsuedoDeviceId == other.mSeedForPsuedoDeviceId) \
&& (mBcatPassphase == other.mBcatPassphase) \
&& (mUserAccountSaveDataMax == other.mUserAccountSaveDataMax) \
&& (mDeviceSaveDataMax == other.mDeviceSaveDataMax) \
&& (mTemporaryStorageSize == other.mTemporaryStorageSize) \
&& (mCacheStorageSize == other.mCacheStorageSize) \
&& (mCacheStorageDataAndJournalSizeMax == other.mCacheStorageDataAndJournalSizeMax) \
&& (mCacheStorageIndex == other.mCacheStorageIndex) \
&& (mPlayLogQueryableApplicationId == other.mPlayLogQueryableApplicationId) \
&& (mPlayLogQueryCapability == other.mPlayLogQueryCapability) \
&& (mRepairFlag == other.mRepairFlag) \
&& (mProgramIndex == other.mProgramIndex);
}
void nx::ApplicationControlPropertyBinary::copyFrom(const ApplicationControlPropertyBinary& other)
{
clear();
mTitle = other.mTitle;
mIsbn = other.mIsbn;
mStartupUserAccount = other.mStartupUserAccount;
mTouchScreenUsageMode = other.mTouchScreenUsageMode;
mAocRegistrationType = other.mAocRegistrationType;
mAttributeFlag = other.mAttributeFlag;
mParentalControlFlag = other.mParentalControlFlag;
mScreenshotMode = other.mScreenshotMode;
mVideoCaptureMode = other.mVideoCaptureMode;
mDataLossConfirmation = other.mDataLossConfirmation;
mPlayLogPolicy = other.mPlayLogPolicy;
mPresenceGroupId = other.mPresenceGroupId;
mRatingAge = other.mRatingAge;
mDisplayVersion = other.mDisplayVersion;
mAocBaseId = other.mAocBaseId;
mSaveDatawOwnerId = other.mSaveDatawOwnerId;
mUserAccountSaveDataSize = other.mUserAccountSaveDataSize;
mDeviceSaveDataSize = other.mDeviceSaveDataSize;
mBcatDeliveryCacheStorageSize = other.mBcatDeliveryCacheStorageSize;
mApplicationErrorCodeCategory = other.mApplicationErrorCodeCategory;
mLocalCommunicationId = other.mLocalCommunicationId;
mLogoType = other.mLogoType;
mLogoHandling = other.mLogoHandling;
mRuntimeAocInstallMode = other.mRuntimeAocInstallMode;
mCrashReportMode = other.mCrashReportMode;
mHdcp = other.mHdcp;
mSeedForPsuedoDeviceId = other.mSeedForPsuedoDeviceId;
mBcatPassphase = other.mBcatPassphase;
mUserAccountSaveDataMax = other.mUserAccountSaveDataMax;
mDeviceSaveDataMax = other.mDeviceSaveDataMax;
mTemporaryStorageSize = other.mTemporaryStorageSize;
mCacheStorageSize = other.mCacheStorageSize;
mCacheStorageDataAndJournalSizeMax = other.mCacheStorageDataAndJournalSizeMax;
mCacheStorageIndex = other.mCacheStorageIndex;
mPlayLogQueryableApplicationId = other.mPlayLogQueryableApplicationId;
mPlayLogQueryCapability = other.mPlayLogQueryCapability;
mRepairFlag = other.mRepairFlag;
mProgramIndex = other.mProgramIndex;
}
}

View file

@ -7,39 +7,73 @@ nx::ContentMetaBinary::ContentMetaBinary()
nx::ContentMetaBinary::ContentMetaBinary(const ContentMetaBinary & other)
{
copyFrom(other);
*this = other;
}
nx::ContentMetaBinary::ContentMetaBinary(const byte_t * bytes, size_t len)
void nx::ContentMetaBinary::operator=(const ContentMetaBinary& other)
{
importBinary(bytes, len);
if (other.getBytes().size() > 0)
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
mTitleId = other.mTitleId;
mTitleVersion = other.mTitleVersion;
mType = other.mType;
mAttributes = other.mAttributes;
mRequiredDownloadSystemVersion = other.mRequiredDownloadSystemVersion;
mExtendedHeader = other.mExtendedHeader;
mApplicationMetaExtendedHeader = other.mApplicationMetaExtendedHeader;
mPatchMetaExtendedHeader = other.mPatchMetaExtendedHeader;
mAddOnContentMetaExtendedHeader = other.mAddOnContentMetaExtendedHeader;
mDeltaMetaExtendedHeader = other.mDeltaMetaExtendedHeader;
mContentInfo = other.mContentInfo;
mContentMetaInfo = other.mContentMetaInfo;
mExtendedData = other.mExtendedData;
memcpy(mDigest.data, other.mDigest.data, cnmt::kDigestLen);
}
}
const byte_t * nx::ContentMetaBinary::getBytes() const
bool nx::ContentMetaBinary::operator==(const ContentMetaBinary& other) const
{
return mBinaryBlob.getBytes();
return (mTitleId == other.mTitleId) \
&& (mTitleVersion == other.mTitleVersion) \
&& (mType == other.mType) \
&& (mAttributes == other.mAttributes) \
&& (mRequiredDownloadSystemVersion == other.mRequiredDownloadSystemVersion) \
&& (mExtendedHeader == other.mExtendedHeader) \
&& (mApplicationMetaExtendedHeader == other.mApplicationMetaExtendedHeader) \
&& (mPatchMetaExtendedHeader == other.mPatchMetaExtendedHeader) \
&& (mAddOnContentMetaExtendedHeader == other.mAddOnContentMetaExtendedHeader) \
&& (mDeltaMetaExtendedHeader == other.mDeltaMetaExtendedHeader) \
&& (mContentInfo == other.mContentInfo) \
&& (mContentMetaInfo == other.mContentMetaInfo) \
&& (mExtendedData == other.mExtendedData) \
&& (memcmp(mDigest.data, other.mDigest.data, cnmt::kDigestLen) == 0);
}
size_t nx::ContentMetaBinary::getSize() const
bool nx::ContentMetaBinary::operator!=(const ContentMetaBinary& other) const
{
return mBinaryBlob.getSize();
return !(*this == other);
}
void nx::ContentMetaBinary::exportBinary()
void nx::ContentMetaBinary::toBytes()
{
throw fnd::Exception(kModuleName, "exportBinary() not implemented");
}
void nx::ContentMetaBinary::importBinary(const byte_t * bytes, size_t len)
void nx::ContentMetaBinary::fromBytes(const byte_t* data, size_t len)
{
// clear member variables
clear();
// validate layout
validateBinary(bytes, len);
validateBinary(data, len);
// get pointer to header structure
const sContentMetaHeader* hdr = (const sContentMetaHeader*)bytes;
const sContentMetaHeader* hdr = (const sContentMetaHeader*)data;
mTitleId = hdr->id.get();
mTitleVersion = hdr->version.get();
@ -52,55 +86,59 @@ void nx::ContentMetaBinary::importBinary(const byte_t * bytes, size_t len)
if (hdr->exhdr_size.get() > 0)
{
mExtendedHeader.alloc(hdr->exhdr_size.get());
memcpy(mExtendedHeader.getBytes(), bytes + getExtendedHeaderOffset(), hdr->exhdr_size.get());
memcpy(mExtendedHeader.data(), data + getExtendedHeaderOffset(), hdr->exhdr_size.get());
switch (mType)
{
case (cnmt::METATYPE_APPLICATION):
mApplicationMetaExtendedHeader.patch_id = ((sApplicationMetaExtendedHeader*)mExtendedHeader.getBytes())->patch_id.get();
mApplicationMetaExtendedHeader.required_system_version = ((sApplicationMetaExtendedHeader*)mExtendedHeader.getBytes())->required_system_version.get();
mApplicationMetaExtendedHeader.patch_id = ((sApplicationMetaExtendedHeader*)mExtendedHeader.data())->patch_id.get();
mApplicationMetaExtendedHeader.required_system_version = ((sApplicationMetaExtendedHeader*)mExtendedHeader.data())->required_system_version.get();
break;
case (cnmt::METATYPE_PATCH):
mPatchMetaExtendedHeader.application_id = ((sPatchMetaExtendedHeader*)mExtendedHeader.getBytes())->application_id.get();
mPatchMetaExtendedHeader.required_system_version = ((sPatchMetaExtendedHeader*)mExtendedHeader.getBytes())->required_system_version.get();
mPatchMetaExtendedHeader.application_id = ((sPatchMetaExtendedHeader*)mExtendedHeader.data())->application_id.get();
mPatchMetaExtendedHeader.required_system_version = ((sPatchMetaExtendedHeader*)mExtendedHeader.data())->required_system_version.get();
break;
case (cnmt::METATYPE_ADD_ON_CONTENT):
mAddOnContentMetaExtendedHeader.application_id = ((sAddOnContentMetaExtendedHeader*)mExtendedHeader.getBytes())->application_id.get();
mAddOnContentMetaExtendedHeader.required_system_version = ((sAddOnContentMetaExtendedHeader*)mExtendedHeader.getBytes())->required_system_version.get();
mAddOnContentMetaExtendedHeader.application_id = ((sAddOnContentMetaExtendedHeader*)mExtendedHeader.data())->application_id.get();
mAddOnContentMetaExtendedHeader.required_system_version = ((sAddOnContentMetaExtendedHeader*)mExtendedHeader.data())->required_system_version.get();
break;
case (cnmt::METATYPE_DELTA):
mDeltaMetaExtendedHeader.application_id = ((sDeltaMetaExtendedHeader*)mExtendedHeader.getBytes())->application_id.get();
mDeltaMetaExtendedHeader.application_id = ((sDeltaMetaExtendedHeader*)mExtendedHeader.data())->application_id.get();
break;
default:
break;
}
exdata_size = getExtendedDataSize(mType, mExtendedHeader.getBytes());
exdata_size = getExtendedDataSize(mType, mExtendedHeader.data());
}
// save content info
if (hdr->content_count.get() > 0)
{
const sContentInfo* info = (const sContentInfo*)(bytes + getContentInfoOffset(hdr->exhdr_size.get()));
const sContentInfo* info = (const sContentInfo*)(data + getContentInfoOffset(hdr->exhdr_size.get()));
ContentInfo cinfo;
for (size_t i = 0; i < hdr->content_count.get(); i++)
{
mContentInfo[i].hash = info[i].content_hash;
memcpy(mContentInfo[i].nca_id, info[i].content_id, cnmt::kContentIdLen);
mContentInfo[i].size = (uint64_t)(info[i].size_lower.get()) | (uint64_t)(info[i].size_higher.get()) << 32;
mContentInfo[i].type = (cnmt::ContentType)info[i].content_type;
cinfo.hash = info[i].content_hash;
memcpy(cinfo.nca_id, info[i].content_id, cnmt::kContentIdLen);
cinfo.size = (uint64_t)(info[i].size_lower.get()) | (uint64_t)(info[i].size_higher.get()) << 32;
cinfo.type = (cnmt::ContentType)info[i].content_type;
mContentInfo.addElement(cinfo);
}
}
// save content meta info
if (hdr->content_meta_count.get() > 0)
{
const sContentMetaInfo* info = (const sContentMetaInfo*)(bytes + getContentMetaInfoOffset(hdr->exhdr_size.get(), hdr->content_count.get()));
const sContentMetaInfo* info = (const sContentMetaInfo*)(data + getContentMetaInfoOffset(hdr->exhdr_size.get(), hdr->content_count.get()));
ContentMetaInfo cmeta;
for (size_t i = 0; i < hdr->content_meta_count.get(); i++)
{
mContentMetaInfo[i].id = info[i].id.get();
mContentMetaInfo[i].version = info[i].version.get();
mContentMetaInfo[i].type = (cnmt::ContentMetaType)info[i].type;
mContentMetaInfo[i].attributes = info[i].attributes;
{
cmeta.id = info[i].id.get();
cmeta.version = info[i].version.get();
cmeta.type = (cnmt::ContentMetaType)info[i].type;
cmeta.attributes = info[i].attributes;
mContentMetaInfo.addElement(cmeta);
}
}
@ -108,17 +146,21 @@ void nx::ContentMetaBinary::importBinary(const byte_t * bytes, size_t len)
if (exdata_size > 0)
{
mExtendedData.alloc(exdata_size);
memcpy(mExtendedData.getBytes(), bytes + getExtendedDataOffset(hdr->exhdr_size.get(), hdr->content_count.get(), hdr->content_meta_count.get()), exdata_size);
memcpy(mExtendedData.data(), data + getExtendedDataOffset(hdr->exhdr_size.get(), hdr->content_count.get(), hdr->content_meta_count.get()), exdata_size);
}
// save digest
memcpy(mDigest.data, bytes + getDigestOffset(hdr->exhdr_size.get(), hdr->content_count.get(), hdr->content_meta_count.get(), exdata_size), cnmt::kDigestLen);
memcpy(mDigest.data, data + getDigestOffset(hdr->exhdr_size.get(), hdr->content_count.get(), hdr->content_meta_count.get(), exdata_size), cnmt::kDigestLen);
}
const fnd::Vec<byte_t>& nx::ContentMetaBinary::getBytes() const
{
return mRawBinary;
}
void nx::ContentMetaBinary::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mTitleId = 0;
mTitleVersion = 0;
mType = cnmt::METATYPE_SYSTEM_PROGRAM;
@ -245,12 +287,12 @@ void nx::ContentMetaBinary::setContentMetaInfo(const fnd::List<nx::ContentMetaBi
mContentMetaInfo = info;
}
const fnd::MemoryBlob & nx::ContentMetaBinary::getExtendedData() const
const fnd::Vec<byte_t> & nx::ContentMetaBinary::getExtendedData() const
{
return mExtendedData;
}
void nx::ContentMetaBinary::setExtendedData(const fnd::MemoryBlob & data)
void nx::ContentMetaBinary::setExtendedData(const fnd::Vec<byte_t> & data)
{
mExtendedData = data;
}
@ -307,7 +349,7 @@ size_t nx::ContentMetaBinary::getExtendedDataSize(cnmt::ContentMetaType type, co
return exdata_len;
}
void nx::ContentMetaBinary::validateBinary(const byte_t * bytes, size_t len) const
void nx::ContentMetaBinary::validateBinary(const byte_t * data, size_t len) const
{
// check if it is large enough to read the header
if (len < sizeof(sContentMetaHeader))
@ -316,7 +358,7 @@ void nx::ContentMetaBinary::validateBinary(const byte_t * bytes, size_t len) con
}
// get pointer to header structure
const sContentMetaHeader* hdr = (const sContentMetaHeader*)bytes;
const sContentMetaHeader* hdr = (const sContentMetaHeader*)data;
// validate extended header size
if (validateExtendedHeaderSize((cnmt::ContentMetaType)hdr->type, hdr->exhdr_size.get()) == false)
@ -331,52 +373,8 @@ void nx::ContentMetaBinary::validateBinary(const byte_t * bytes, size_t len) con
}
// check binary size again with extended data size
if (len < getTotalSize(hdr->exhdr_size.get(), hdr->content_count.get(), hdr->content_meta_count.get(), getExtendedDataSize((cnmt::ContentMetaType)hdr->type, bytes + getExtendedHeaderOffset())))
if (len < getTotalSize(hdr->exhdr_size.get(), hdr->content_count.get(), hdr->content_meta_count.get(), getExtendedDataSize((cnmt::ContentMetaType)hdr->type, data + getExtendedHeaderOffset())))
{
throw fnd::Exception(kModuleName, "Binary too small");
}
}
bool nx::ContentMetaBinary::isEqual(const ContentMetaBinary & other) const
{
return (mTitleId == other.mTitleId) \
&& (mTitleVersion == other.mTitleVersion) \
&& (mType == other.mType) \
&& (mAttributes == other.mAttributes) \
&& (mRequiredDownloadSystemVersion == other.mRequiredDownloadSystemVersion) \
&& (mExtendedHeader == other.mExtendedHeader) \
&& (mApplicationMetaExtendedHeader == other.mApplicationMetaExtendedHeader) \
&& (mPatchMetaExtendedHeader == other.mPatchMetaExtendedHeader) \
&& (mAddOnContentMetaExtendedHeader == other.mAddOnContentMetaExtendedHeader) \
&& (mDeltaMetaExtendedHeader == other.mDeltaMetaExtendedHeader) \
&& (mContentInfo == other.mContentInfo) \
&& (mContentMetaInfo == other.mContentMetaInfo) \
&& (mExtendedData == other.mExtendedData) \
&& (memcmp(mDigest.data, other.mDigest.data, cnmt::kDigestLen) == 0);
}
void nx::ContentMetaBinary::copyFrom(const ContentMetaBinary & other)
{
if (other.getSize() > 0)
{
importBinary(other.getBytes(), other.getSize());
}
else
{
clear();
mTitleId = other.mTitleId;
mTitleVersion = other.mTitleVersion;
mType = other.mType;
mAttributes = other.mAttributes;
mRequiredDownloadSystemVersion = other.mRequiredDownloadSystemVersion;
mExtendedHeader = other.mExtendedHeader;
mApplicationMetaExtendedHeader = other.mApplicationMetaExtendedHeader;
mPatchMetaExtendedHeader = other.mPatchMetaExtendedHeader;
mAddOnContentMetaExtendedHeader = other.mAddOnContentMetaExtendedHeader;
mDeltaMetaExtendedHeader = other.mDeltaMetaExtendedHeader;
mContentInfo = other.mContentInfo;
mContentMetaInfo = other.mContentMetaInfo;
mExtendedData = other.mExtendedData;
memcpy(mDigest.data, other.mDigest.data, cnmt::kDigestLen);
}
}
}

View file

@ -1,138 +0,0 @@
#include <nx/FacBinary.h>
nx::FacBinary::FacBinary()
{}
nx::FacBinary::FacBinary(const FacBinary & other)
{
copyFrom(other);
}
nx::FacBinary::FacBinary(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
bool nx::FacBinary::operator==(const FacBinary & other) const
{
return isEqual(other);
}
bool nx::FacBinary::operator!=(const FacBinary & other) const
{
return !isEqual(other);
}
void nx::FacBinary::operator=(const FacBinary & other)
{
copyFrom(other);
}
const byte_t * nx::FacBinary::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t nx::FacBinary::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::FacBinary::exportBinary()
{
FacHeader::setContentOwnerIdSize(mContentOwnerIdList.getSize() * sizeof(uint32_t));
FacHeader::setSaveDataOwnerIdSize(mSaveDataOwnerIdList.getSize() * sizeof(uint32_t));
FacHeader::exportBinary();
mBinaryBlob.alloc(getFacSize());
memcpy(mBinaryBlob.getBytes(), FacHeader::getBytes(), FacHeader::getSize());
uint32_t* rawContentOwnerIds = (uint32_t*)(mBinaryBlob.getBytes() + FacHeader::getContentOwnerIdOffset());
for (size_t i = 0; i < mContentOwnerIdList.getSize(); i++)
{
rawContentOwnerIds[i] = le_word(mContentOwnerIdList[i]);
}
uint32_t* rawSaveDataOwnerIds = (uint32_t*)(mBinaryBlob.getBytes() + FacHeader::getSaveDataOwnerIdOffset());
for (size_t i = 0; i < mSaveDataOwnerIdList.getSize(); i++)
{
rawSaveDataOwnerIds[i] = le_word(mSaveDataOwnerIdList[i]);
}
}
void nx::FacBinary::importBinary(const byte_t * bytes, size_t len)
{
clear();
FacHeader::importBinary(bytes, len);
if (FacHeader::getFacSize() > len)
{
throw fnd::Exception(kModuleName, "FAC binary too small");
}
mBinaryBlob.alloc(FacHeader::getFacSize());
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
uint32_t* rawContentOwnerIds = (uint32_t*)(mBinaryBlob.getBytes() + FacHeader::getContentOwnerIdOffset());
size_t rawContentOwnerIdNum = FacHeader::getContentOwnerIdSize() / sizeof(uint32_t);
for (size_t i = 0; i < rawContentOwnerIdNum; i++)
{
mContentOwnerIdList.addElement(le_word(rawContentOwnerIds[i]));
}
uint32_t* rawSaveDataOwnerIds = (uint32_t*)(mBinaryBlob.getBytes() + FacHeader::getSaveDataOwnerIdOffset());
size_t rawSaveDataOwnerIdNum = FacHeader::getSaveDataOwnerIdSize() / sizeof(uint32_t);
for (size_t i = 0; i < rawSaveDataOwnerIdNum; i++)
{
mSaveDataOwnerIdList.addElement(le_word(rawSaveDataOwnerIds[i]));
}
}
bool nx::FacBinary::isEqual(const FacBinary & other) const
{
return (FacHeader::operator==(other)) \
&& (mContentOwnerIdList == other.mContentOwnerIdList) \
&& (mSaveDataOwnerIdList == other.mSaveDataOwnerIdList);
}
void nx::FacBinary::copyFrom(const FacBinary & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
FacHeader::operator=(other);
mContentOwnerIdList = other.mContentOwnerIdList;
mSaveDataOwnerIdList = other.mSaveDataOwnerIdList;
}
}
void nx::FacBinary::clear()
{
FacHeader::clear();
mContentOwnerIdList.clear();
mSaveDataOwnerIdList.clear();
}
const fnd::List<uint32_t>& nx::FacBinary::getContentOwnerIdList() const
{
return mContentOwnerIdList;
}
void nx::FacBinary::setContentOwnerIdList(const fnd::List<uint32_t>& list)
{
mContentOwnerIdList = list;
}
const fnd::List<uint32_t>& nx::FacBinary::getSaveDataOwnerIdList() const
{
return mSaveDataOwnerIdList;
}
void nx::FacBinary::setSaveDataOwnerIdList(const fnd::List<uint32_t>& list)
{
mSaveDataOwnerIdList = list;
}

View file

@ -1,204 +0,0 @@
#include <nx/FacHeader.h>
nx::FacHeader::FacHeader() :
mFsaRights()
{
clear();
}
nx::FacHeader::FacHeader(const FacHeader & other) :
mFsaRights()
{
copyFrom(other);
}
nx::FacHeader::FacHeader(const byte_t * bytes, size_t len) :
mFsaRights()
{
importBinary(bytes, len);
}
bool nx::FacHeader::operator==(const FacHeader & other) const
{
return isEqual(other);
}
bool nx::FacHeader::operator!=(const FacHeader & other) const
{
return !isEqual(other);
}
void nx::FacHeader::operator=(const FacHeader & other)
{
copyFrom(other);
}
const byte_t * nx::FacHeader::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t nx::FacHeader::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::FacHeader::exportBinary()
{
mBinaryBlob.alloc(sizeof(sFacHeader));
sFacHeader* hdr = (sFacHeader*)mBinaryBlob.getBytes();
if (mVersion != kFacFormatVersion)
{
fnd::Exception(kModuleName, "Unsupported format version");
}
hdr->version = (mVersion);
uint64_t flag = 0;
for (size_t i = 0; i < mFsaRights.getSize(); i++)
{
flag |= BIT((uint64_t)mFsaRights[i]);
}
hdr->fac_flags = (flag);
calculateOffsets();
hdr->content_owner_ids.start = (uint32_t)(mContentOwnerIdPos.offset);
hdr->content_owner_ids.end = (uint32_t)(mContentOwnerIdPos.offset + mContentOwnerIdPos.size);
hdr->save_data_owner_ids.start = (uint32_t)(mSaveDataOwnerIdPos.offset);
hdr->save_data_owner_ids.end = (uint32_t)(mSaveDataOwnerIdPos.offset + mSaveDataOwnerIdPos.size);
}
void nx::FacHeader::importBinary(const byte_t * bytes, size_t len)
{
if (len < sizeof(sFacHeader))
{
throw fnd::Exception(kModuleName, "FAC header too small");
}
mBinaryBlob.alloc(sizeof(sFacHeader));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
sFacHeader* hdr = (sFacHeader*)mBinaryBlob.getBytes();
if (hdr->version.get() != kFacFormatVersion)
{
throw fnd::Exception(kModuleName, "Unsupported FAC format version");
}
mVersion = hdr->version.get();
clear();
for (uint64_t i = 0; i < 64; i++)
{
if ((hdr->fac_flags.get() >> i) & 1)
{
mFsaRights.addElement((FsAccessFlag)i);
}
}
mContentOwnerIdPos.offset = hdr->content_owner_ids.start.get();
mContentOwnerIdPos.size = hdr->content_owner_ids.end.get() > hdr->content_owner_ids.start.get() ? hdr->content_owner_ids.end.get() - hdr->content_owner_ids.start.get() : 0;
mSaveDataOwnerIdPos.offset = hdr->save_data_owner_ids.start.get();
mSaveDataOwnerIdPos.size = hdr->save_data_owner_ids.end.get() > hdr->save_data_owner_ids.start.get() ? hdr->save_data_owner_ids.end.get() - hdr->save_data_owner_ids.start.get() : 0;
}
void nx::FacHeader::clear()
{
mFsaRights.clear();
mContentOwnerIdPos.offset = 0;
mContentOwnerIdPos.size = 0;
mSaveDataOwnerIdPos.offset = 0;
mSaveDataOwnerIdPos.size = 0;
}
size_t nx::FacHeader::getFacSize() const
{
size_t savedata = getSaveDataOwnerIdOffset() + getSaveDataOwnerIdSize();
size_t content = getContentOwnerIdOffset() + getContentOwnerIdSize();
return MAX(MAX(savedata, content), sizeof(sFacHeader));
}
uint32_t nx::FacHeader::getFormatVersion() const
{
return mVersion;
}
void nx::FacHeader::setFormatVersion(uint32_t version)
{
mVersion = version;
}
const fnd::List<nx::FacHeader::FsAccessFlag>& nx::FacHeader::getFsaRightsList() const
{
return mFsaRights;
}
void nx::FacHeader::setFsaRightsList(const fnd::List<FsAccessFlag>& list)
{
mFsaRights.clear();
for (size_t i = 0; i < list.getSize(); i++)
{
mFsaRights.hasElement(list[i]) ? mFsaRights.addElement(list[i]) : throw fnd::Exception(kModuleName, "FSA right already exists");
}
}
size_t nx::FacHeader::getContentOwnerIdOffset() const
{
return mContentOwnerIdPos.offset;
}
size_t nx::FacHeader::getContentOwnerIdSize() const
{
return mContentOwnerIdPos.size;
}
void nx::FacHeader::setContentOwnerIdSize(size_t size)
{
mContentOwnerIdPos.size = size;
}
size_t nx::FacHeader::getSaveDataOwnerIdOffset() const
{
return mSaveDataOwnerIdPos.offset;
}
size_t nx::FacHeader::getSaveDataOwnerIdSize() const
{
return mSaveDataOwnerIdPos.size;
}
void nx::FacHeader::setSaveDataOwnerIdSize(size_t size)
{
mSaveDataOwnerIdPos.size = size;
}
void nx::FacHeader::calculateOffsets()
{
mContentOwnerIdPos.offset = align(sizeof(sFacHeader), 4);
mSaveDataOwnerIdPos.offset = mContentOwnerIdPos.offset + align(mContentOwnerIdPos.size, 4);
}
bool nx::FacHeader::isEqual(const FacHeader & other) const
{
return (mFsaRights == other.mFsaRights) \
&& (mContentOwnerIdPos.offset == other.mContentOwnerIdPos.offset) \
&& (mContentOwnerIdPos.size == other.mContentOwnerIdPos.size) \
&& (mSaveDataOwnerIdPos.offset == other.mSaveDataOwnerIdPos.offset) \
&& (mSaveDataOwnerIdPos.size == other.mSaveDataOwnerIdPos.size);
}
void nx::FacHeader::copyFrom(const FacHeader & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
mBinaryBlob.clear();
mFsaRights = other.mFsaRights;
mContentOwnerIdPos.offset = other.mContentOwnerIdPos.offset;
mContentOwnerIdPos.size = other.mContentOwnerIdPos.size;
mSaveDataOwnerIdPos.offset = other.mSaveDataOwnerIdPos.offset;
mSaveDataOwnerIdPos.size = other.mSaveDataOwnerIdPos.size;
}
}

View file

@ -0,0 +1,205 @@
#include <nx/FileSystemAccessControlBinary.h>
#include <fnd/SimpleTextOutput.h>
nx::FileSystemAccessControlBinary::FileSystemAccessControlBinary()
{
clear();
}
nx::FileSystemAccessControlBinary::FileSystemAccessControlBinary(const FileSystemAccessControlBinary & other)
{
*this = other;
}
void nx::FileSystemAccessControlBinary::operator=(const FileSystemAccessControlBinary & other)
{
mRawBinary = other.mRawBinary;
mVersion = other.mVersion;
mFsaRights = other.mFsaRights;
mContentOwnerIdList = other.mContentOwnerIdList;
mSaveDataOwnerIdList = other.mSaveDataOwnerIdList;
}
bool nx::FileSystemAccessControlBinary::operator==(const FileSystemAccessControlBinary & other) const
{
return (mVersion == other.mVersion) \
&& (mFsaRights == other.mFsaRights) \
&& (mContentOwnerIdList == other.mContentOwnerIdList) \
&& (mSaveDataOwnerIdList == other.mSaveDataOwnerIdList);
}
bool nx::FileSystemAccessControlBinary::operator!=(const FileSystemAccessControlBinary & other) const
{
return !(*this == other);
}
void nx::FileSystemAccessControlBinary::toBytes()
{
// determine section layout
struct sLayout {
uint32_t offset, size;
} content, savedata;
content.offset = align(sizeof(sFacHeader), fac::kSectionAlignSize);
content.size = (uint32_t)(mContentOwnerIdList.size() * sizeof(uint32_t));
savedata.offset = content.offset + align(content.size, fac::kSectionAlignSize);
savedata.size = (uint32_t)(mSaveDataOwnerIdList.size() * sizeof(uint32_t));
// get total size
size_t total_size = _MAX(_MAX(content.offset + content.size, savedata.offset + savedata.size), align(sizeof(sFacHeader), fac::kSectionAlignSize));
mRawBinary.alloc(total_size);
sFacHeader* hdr = (sFacHeader*)mRawBinary.data();
// set type
hdr->version = mVersion;
// flags
uint64_t flag = 0;
for (size_t i = 0; i < mFsaRights.size(); i++)
{
flag |= _BIT((uint64_t)mFsaRights[i]);
}
hdr->fac_flags = flag;
// set offset/size
hdr->content_owner_ids.start = content.offset;
if (content.size > 0)
hdr->content_owner_ids.end = content.offset + content.size;
hdr->save_data_owner_ids.start = savedata.offset;
if (savedata.size > 0)
hdr->save_data_owner_ids.end = savedata.offset + savedata.size;
// set ids
le_uint32_t* content_owner_ids = (le_uint32_t*)(mRawBinary.data() + content.offset);
for (size_t i = 0; i < mContentOwnerIdList.size(); i++)
{
content_owner_ids[i] = mContentOwnerIdList[i];
}
le_uint32_t* save_data_owner_ids = (le_uint32_t*)(mRawBinary.data() + savedata.offset);
for (size_t i = 0; i < mSaveDataOwnerIdList.size(); i++)
{
save_data_owner_ids[i] = mSaveDataOwnerIdList[i];
}
}
void nx::FileSystemAccessControlBinary::fromBytes(const byte_t* data, size_t len)
{
// check size
if (len < sizeof(sFacHeader))
{
throw fnd::Exception(kModuleName, "FileSystemAccessControlInfo binary is too small");
}
// clear variables
clear();
// save a copy of the header
sFacHeader hdr;
memcpy((void*)&hdr, data, sizeof(sFacHeader));
// check format version
if (hdr.version.get() != fac::kFacFormatVersion)
{
throw fnd::Exception(kModuleName, "FileSystemAccessControlInfo format version unsupported");
}
// get total size
size_t total_size = _MAX(_MAX(hdr.content_owner_ids.end.get(), hdr.save_data_owner_ids.end.get()), align(sizeof(sFacHeader), fac::kSectionAlignSize));
// validate binary size
if (len < total_size)
{
throw fnd::Exception(kModuleName, "FileSystemAccessControlInfo binary is too small");
}
// allocate memory
mRawBinary.alloc(total_size);
memcpy(mRawBinary.data(), data, mRawBinary.size());
// save variables
mVersion = hdr.version.get();
for (size_t i = 0; i < 64; i++)
{
if (_HAS_BIT(hdr.fac_flags.get(), i))
{
mFsaRights.addElement((fac::FsAccessFlag)i);
}
}
// save ids
if (hdr.content_owner_ids.end.get() > hdr.content_owner_ids.start.get())
{
le_uint32_t* content_owner_ids = (le_uint32_t*)(mRawBinary.data() + hdr.content_owner_ids.start.get());
size_t content_owner_id_num = (hdr.content_owner_ids.end.get() - hdr.content_owner_ids.start.get()) / sizeof(uint32_t);
for (size_t i = 0; i < content_owner_id_num; i++)
{
mContentOwnerIdList.addElement(content_owner_ids[i].get());
}
}
if (hdr.save_data_owner_ids.end.get() > hdr.save_data_owner_ids.start.get())
{
le_uint32_t* save_data_owner_ids = (le_uint32_t*)(mRawBinary.data() + hdr.save_data_owner_ids.start.get());
size_t save_data_owner_id_num = (hdr.save_data_owner_ids.end.get() - hdr.save_data_owner_ids.start.get()) / sizeof(uint32_t);
for (size_t i = 0; i < save_data_owner_id_num; i++)
{
mSaveDataOwnerIdList.addElement(save_data_owner_ids[i].get());
}
}
}
const fnd::Vec<byte_t>& nx::FileSystemAccessControlBinary::getBytes() const
{
return mRawBinary;
}
void nx::FileSystemAccessControlBinary::clear()
{
mRawBinary.clear();
mVersion = 0;
mFsaRights.clear();
mContentOwnerIdList.clear();
mSaveDataOwnerIdList.clear();
}
uint32_t nx::FileSystemAccessControlBinary::getFormatVersion() const
{
return mVersion;
}
void nx::FileSystemAccessControlBinary::setFormatVersion(uint32_t format_version)
{
mVersion = format_version;
}
const fnd::List<nx::fac::FsAccessFlag>& nx::FileSystemAccessControlBinary::getFsaRightsList() const
{
return mFsaRights;
}
void nx::FileSystemAccessControlBinary::setFsaRightsList(const fnd::List<fac::FsAccessFlag>& list)
{
mFsaRights = list;
}
const fnd::List<uint32_t>& nx::FileSystemAccessControlBinary::getContentOwnerIdList() const
{
return mContentOwnerIdList;
}
void nx::FileSystemAccessControlBinary::setContentOwnerIdList(const fnd::List<uint32_t>& list)
{
mContentOwnerIdList = list;
}
const fnd::List<uint32_t>& nx::FileSystemAccessControlBinary::getSaveDataOwnerIdList() const
{
return mSaveDataOwnerIdList;
}
void nx::FileSystemAccessControlBinary::setSaveDataOwnerIdList(const fnd::List<uint32_t>& list)
{
mSaveDataOwnerIdList = list;
}

View file

@ -1,13 +1,11 @@
#include <nx/HandleTableSizeEntry.h>
nx::HandleTableSizeEntry::HandleTableSizeEntry() :
mCap(kCapId),
mHandleTableSize(0)
{}
nx::HandleTableSizeEntry::HandleTableSizeEntry(const KernelCapability & kernel_cap) :
nx::HandleTableSizeEntry::HandleTableSizeEntry(const KernelCapabilityEntry & kernel_cap) :
mCap(kCapId),
mHandleTableSize(0)
{
@ -21,16 +19,33 @@ nx::HandleTableSizeEntry::HandleTableSizeEntry(uint16_t size) :
setHandleTableSize(size);
}
const nx::KernelCapability & nx::HandleTableSizeEntry::getKernelCapability() const
void nx::HandleTableSizeEntry::operator=(const HandleTableSizeEntry& other)
{
mHandleTableSize = other.mHandleTableSize;
updateCapField();
}
bool nx::HandleTableSizeEntry::operator==(const HandleTableSizeEntry& other) const
{
return (mHandleTableSize == other.mHandleTableSize);
}
bool nx::HandleTableSizeEntry::operator!=(const HandleTableSizeEntry& other) const
{
return !(*this == other);
}
const nx::KernelCapabilityEntry & nx::HandleTableSizeEntry::getKernelCapability() const
{
return mCap;
}
void nx::HandleTableSizeEntry::setKernelCapability(const KernelCapability & kernel_cap)
void nx::HandleTableSizeEntry::setKernelCapability(const KernelCapabilityEntry & kernel_cap)
{
if (kernel_cap.getType() != kCapId)
{
throw fnd::Exception(kModuleName, "KernelCapability is not type 'HandleTableSize'");
throw fnd::Exception(kModuleName, "KernelCapabilityEntry is not type 'HandleTableSize'");
}
mCap = kernel_cap;

View file

@ -1,42 +1,42 @@
#include <nx/HandleTableSizeHandler.h>
nx::HandleTableSizeHandler::HandleTableSizeHandler() :
mIsSet(false),
mEntry(0)
{}
void nx::HandleTableSizeHandler::operator=(const HandleTableSizeHandler & other)
{
mIsSet = other.mIsSet;
mEntry.setKernelCapability(other.mEntry.getKernelCapability());
}
bool nx::HandleTableSizeHandler::operator==(const HandleTableSizeHandler & other) const
{
return isEqual(other);
return (mIsSet == other.mIsSet) \
&& (mEntry.getKernelCapability() == other.mEntry.getKernelCapability());
}
bool nx::HandleTableSizeHandler::operator!=(const HandleTableSizeHandler & other) const
{
return !isEqual(other);
return !(*this == other);
}
void nx::HandleTableSizeHandler::operator=(const HandleTableSizeHandler & other)
void nx::HandleTableSizeHandler::importKernelCapabilityList(const fnd::List<KernelCapabilityEntry>& caps)
{
copyFrom(other);
}
void nx::HandleTableSizeHandler::importKernelCapabilityList(const fnd::List<KernelCapability>& caps)
{
if (caps.getSize() > kMaxKernelCapNum)
if (caps.size() > kMaxKernelCapNum)
{
throw fnd::Exception(kModuleName, "Too many kernel capabilities");
}
if (caps.getSize() == 0)
if (caps.size() == 0)
return;
mEntry.setKernelCapability(caps[0]);
mIsSet = true;
}
void nx::HandleTableSizeHandler::exportKernelCapabilityList(fnd::List<KernelCapability>& caps) const
void nx::HandleTableSizeHandler::exportKernelCapabilityList(fnd::List<KernelCapabilityEntry>& caps) const
{
if (isSet() == false)
return;
@ -64,16 +64,4 @@ void nx::HandleTableSizeHandler::setHandleTableSize(uint16_t size)
{
mEntry.setHandleTableSize(size);
mIsSet = true;
}
void nx::HandleTableSizeHandler::copyFrom(const HandleTableSizeHandler & other)
{
mIsSet = other.mIsSet;
mEntry.setKernelCapability(other.mEntry.getKernelCapability());
}
bool nx::HandleTableSizeHandler::isEqual(const HandleTableSizeHandler & other) const
{
return (mIsSet == other.mIsSet) \
&& (mEntry.getKernelCapability() == other.mEntry.getKernelCapability());
}
}

View file

@ -8,45 +8,40 @@ nx::HierarchicalIntegrityHeader::HierarchicalIntegrityHeader()
nx::HierarchicalIntegrityHeader::HierarchicalIntegrityHeader(const HierarchicalIntegrityHeader & other)
{
copyFrom(other);
}
nx::HierarchicalIntegrityHeader::HierarchicalIntegrityHeader(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
bool nx::HierarchicalIntegrityHeader::operator==(const HierarchicalIntegrityHeader & other) const
{
return isEqual(other);
}
bool nx::HierarchicalIntegrityHeader::operator!=(const HierarchicalIntegrityHeader & other) const
{
return !isEqual(other);
*this = other;
}
void nx::HierarchicalIntegrityHeader::operator=(const HierarchicalIntegrityHeader & other)
{
copyFrom(other);
if (other.getBytes().size() != 0)
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
mLayerInfo = other.mLayerInfo;
mMasterHashList = other.mMasterHashList;
}
}
const byte_t * nx::HierarchicalIntegrityHeader::getBytes() const
bool nx::HierarchicalIntegrityHeader::operator==(const HierarchicalIntegrityHeader & other) const
{
return mBinaryBlob.getBytes();
return (mLayerInfo == other.mLayerInfo) \
&& (mMasterHashList == other.mMasterHashList);
}
size_t nx::HierarchicalIntegrityHeader::getSize() const
bool nx::HierarchicalIntegrityHeader::operator!=(const HierarchicalIntegrityHeader & other) const
{
return mBinaryBlob.getSize();
return !(*this == other);
}
void nx::HierarchicalIntegrityHeader::exportBinary()
void nx::HierarchicalIntegrityHeader::toBytes()
{
throw fnd::Exception(kModuleName, "exportBinary() not implemented");
}
void nx::HierarchicalIntegrityHeader::importBinary(const byte_t * bytes, size_t len)
void nx::HierarchicalIntegrityHeader::fromBytes(const byte_t* data, size_t len)
{
std::stringstream error_str;
@ -56,10 +51,10 @@ void nx::HierarchicalIntegrityHeader::importBinary(const byte_t * bytes, size_t
throw fnd::Exception(kModuleName, "Header too small");
}
const nx::sHierarchicalIntegrityHeader* hdr = (const nx::sHierarchicalIntegrityHeader*)bytes;
const nx::sHierarchicalIntegrityHeader* hdr = (const nx::sHierarchicalIntegrityHeader*)data;
// Validate Header Sig "IVFC"
if (hdr->signature.get() != hierarchicalintegrity::kStructSig)
if (hdr->st_magic.get() != hierarchicalintegrity::kStructMagic)
{
throw fnd::Exception(kModuleName, "Invalid struct magic");
}
@ -92,24 +87,29 @@ void nx::HierarchicalIntegrityHeader::importBinary(const byte_t * bytes, size_t
}
// copy to internal storage
mBinaryBlob.alloc(total_size);
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
mRawBinary.alloc(total_size);
memcpy(mRawBinary.data(), data, mRawBinary.size());
// save layer info
const nx::sHierarchicalIntegrityLayerInfo* layer_info = (const nx::sHierarchicalIntegrityLayerInfo*)(mBinaryBlob.getBytes() + sizeof(nx::sHierarchicalIntegrityHeader));
const nx::sHierarchicalIntegrityLayerInfo* layer_info = (const nx::sHierarchicalIntegrityLayerInfo*)(mRawBinary.data() + sizeof(nx::sHierarchicalIntegrityHeader));
for (size_t i = 0; i < hierarchicalintegrity::kDefaultLayerNum; i++)
{
mLayerInfo.addElement({layer_info[i].offset.get(), layer_info[i].size.get(), layer_info[i].block_size.get()});
}
// save hash list
const crypto::sha::sSha256Hash* hash_list = (const crypto::sha::sSha256Hash*)(mBinaryBlob.getBytes() + master_hash_offset);
const crypto::sha::sSha256Hash* hash_list = (const crypto::sha::sSha256Hash*)(mRawBinary.data() + master_hash_offset);
for (size_t i = 0; i < hdr->master_hash_size.get()/sizeof(crypto::sha::sSha256Hash); i++)
{
mMasterHashList.addElement(hash_list[i]);
}
}
const fnd::Vec<byte_t>& nx::HierarchicalIntegrityHeader::getBytes() const
{
return mRawBinary;
}
void nx::HierarchicalIntegrityHeader::clear()
{
mLayerInfo.clear();
@ -134,23 +134,4 @@ const fnd::List<crypto::sha::sSha256Hash>& nx::HierarchicalIntegrityHeader::getM
void nx::HierarchicalIntegrityHeader::setMasterHashList(const fnd::List<crypto::sha::sSha256Hash>& master_hash_list)
{
mMasterHashList = master_hash_list;
}
bool nx::HierarchicalIntegrityHeader::isEqual(const HierarchicalIntegrityHeader & other) const
{
return (mLayerInfo == other.mLayerInfo) \
&& (mMasterHashList == other.mMasterHashList);
}
void nx::HierarchicalIntegrityHeader::copyFrom(const HierarchicalIntegrityHeader & other)
{
if (other.getSize() != 0)
{
importBinary(other.getBytes(), other.getSize());
}
else
{
mLayerInfo = other.mLayerInfo;
mMasterHashList = other.mMasterHashList;
}
}
}

View file

@ -1,7 +1,6 @@
#include <sstream>
#include <nx/HierarchicalSha256Header.h>
nx::HierarchicalSha256Header::HierarchicalSha256Header()
{
clear();
@ -9,45 +8,41 @@ nx::HierarchicalSha256Header::HierarchicalSha256Header()
nx::HierarchicalSha256Header::HierarchicalSha256Header(const HierarchicalSha256Header & other)
{
copyFrom(other);
}
nx::HierarchicalSha256Header::HierarchicalSha256Header(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
}
bool nx::HierarchicalSha256Header::operator==(const HierarchicalSha256Header & other) const
{
return isEqual(other);
}
bool nx::HierarchicalSha256Header::operator!=(const HierarchicalSha256Header & other) const
{
return !isEqual(other);
*this = other;
}
void nx::HierarchicalSha256Header::operator=(const HierarchicalSha256Header & other)
{
copyFrom(other);
if (other.getBytes().size() != 0)
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
mMasterHash = other.mMasterHash;
mHashBlockSize = other.mHashBlockSize;
mLayerInfo = other.mLayerInfo;
}
}
const byte_t * nx::HierarchicalSha256Header::getBytes() const
bool nx::HierarchicalSha256Header::operator==(const HierarchicalSha256Header & other) const
{
return mBinaryBlob.getBytes();
return (mMasterHash == other.mMasterHash) \
&& (mHashBlockSize == other.mHashBlockSize) \
&& (mLayerInfo == other.mLayerInfo);
}
size_t nx::HierarchicalSha256Header::getSize() const
bool nx::HierarchicalSha256Header::operator!=(const HierarchicalSha256Header & other) const
{
return mBinaryBlob.getSize();
return !(*this == other);
}
void nx::HierarchicalSha256Header::exportBinary()
void nx::HierarchicalSha256Header::toBytes()
{
throw fnd::Exception(kModuleName, "exportBinary() not implemented");
}
void nx::HierarchicalSha256Header::importBinary(const byte_t * bytes, size_t len)
void nx::HierarchicalSha256Header::fromBytes(const byte_t* data, size_t len)
{
std::stringstream error_str;
@ -56,7 +51,7 @@ void nx::HierarchicalSha256Header::importBinary(const byte_t * bytes, size_t len
throw fnd::Exception(kModuleName, "Header too small");
}
const nx::sHierarchicalSha256Header* hdr = (const nx::sHierarchicalSha256Header*)bytes;
const nx::sHierarchicalSha256Header* hdr = (const nx::sHierarchicalSha256Header*)data;
if (hdr->layer_num.get() != nx::hierarchicalsha256::kDefaultLayerNum)
{
@ -74,6 +69,11 @@ void nx::HierarchicalSha256Header::importBinary(const byte_t * bytes, size_t len
}
}
const fnd::Vec<byte_t>& nx::HierarchicalSha256Header::getBytes() const
{
return mRawBinary;
}
void nx::HierarchicalSha256Header::clear()
{
memset(mMasterHash.bytes, 0, sizeof(crypto::sha::sSha256Hash));
@ -109,25 +109,4 @@ const fnd::List<nx::HierarchicalSha256Header::sLayer>& nx::HierarchicalSha256Hea
void nx::HierarchicalSha256Header::setLayerInfo(const fnd::List<sLayer>& layer_info)
{
mLayerInfo = layer_info;
}
bool nx::HierarchicalSha256Header::isEqual(const HierarchicalSha256Header & other) const
{
return (mMasterHash == other.mMasterHash) \
&& (mHashBlockSize == other.mHashBlockSize) \
&& (mLayerInfo == other.mLayerInfo);
}
void nx::HierarchicalSha256Header::copyFrom(const HierarchicalSha256Header & other)
{
if (other.getSize() != 0)
{
importBinary(other.getBytes(), other.getSize());
}
else
{
mMasterHash = other.mMasterHash;
mHashBlockSize = other.mHashBlockSize;
mLayerInfo = other.mLayerInfo;
}
}
}

Some files were not shown because too many files have changed in this diff Show more