Public V0.0.1

This commit is contained in:
2025-05-04 13:32:07 +02:00
commit 996998785c
30 changed files with 4397 additions and 0 deletions

29
source/3dsx.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <ctrff/3dsx.hpp>
#include <ctrff/binutil.hpp>
namespace ctrff {
CTRFF_API void _3dsx::Write(std::fstream& f) const {
// To be written
}
CTRFF_API void _3dsx::Read(std::fstream& f) {
BinUtil r(f);
r.ReadEx(Magic);
r.ReadEx(HeaderSize);
r.ReadEx(RelocHeaderSize);
r.ReadEx(FormatVersion);
r.ReadEx(Flags);
r.ReadEx(CodeSegSize);
r.ReadEx(RodataSegSize);
r.ReadEx(DataSegSize);
r.ReadEx(BssSize);
r.ReadEx(SMDHOff);
r.ReadEx(SMDHSize);
r.ReadEx(FsOff);
if (HasMeta()) {
f.seekg(SMDHOff, std::ios::beg);
Meta.Read(f);
}
}
} // namespace ctrff

189
source/bcstm.cpp Normal file
View File

@ -0,0 +1,189 @@
#include <ctrff/bcstm.hpp>
/** Using this a single time so inline it */
inline PD::u32 Swap32(PD::u32 in) {
return (in >> 24) | ((in >> 8) & 0x0000FF00) | ((in << 8) & 0x00FF0000) |
(in << 24);
}
namespace ctrff {
CTRFF_API void BCSTM::LoadFile(const std::string& path) {
CleanUp();
pFile.open(path, std::ios::in | std::ios::binary);
if (!pFile.is_open()) {
throw std::runtime_error("BCSTM Error: File not found!");
}
pReader.Read(pHeader.Magic);
pReader.Read(pHeader.Endianness);
if (pHeader.Endianness == Big) {
pHeader.Magic = Swap32(pHeader.Magic);
}
/** Check for 'CSTM' */
if (pHeader.Magic != 0x4D545343) {
throw std::runtime_error("BCSTM Error: Invalid File!");
}
pReader.Read(pHeader.HeaderSize);
pReader.Read(pHeader.Version);
pReader.Read(pHeader.FileSize);
pReader.Read(pHeader.NumBlocks);
pReader.Read(pHeader.Reserved);
for (PD::u16 i = 0; i < pHeader.NumBlocks; i++) {
SizedReference ref;
ReadSizedReference(ref);
if (ref.Ref.TypeID == Ref_InfoBlock) {
pInfoBlockRef = ref;
} else if (ref.Ref.TypeID == Ref_SeekBlock) {
pSeekBlockRef = ref;
} else if (ref.Ref.TypeID == Ref_DataBlock) {
pDataBlockRef = ref;
}
}
pFile.seekg(pInfoBlockRef.Ref.Offset);
ReadInfoBlock(pInfoBlock);
ReadGotoBeginning();
}
CTRFF_API void BCSTM::ReadReference(Reference& ref) {
pReader.Read(ref.TypeID);
pReader.Read(ref.Padding);
pReader.Read(ref.Offset);
}
CTRFF_API void BCSTM::ReadSizedReference(SizedReference& ref) {
ReadReference(ref.Ref);
pReader.Read(ref.Size);
}
CTRFF_API void BCSTM::ReadInfoBlock(InfoBlock& block) {
pReader.Read(block.Header.Magic);
pReader.Read(block.Header.Size);
ReadReference(block.StreamInfoRef);
ReadReference(block.TrackInfoTabRef);
ReadReference(block.ChannelInfoTabRef);
pReader.Read(block.StreamInfo.Encoding);
if (block.StreamInfo.Encoding != DSP_ADPCM) {
throw std::runtime_error("Only DSP ADPCM is supported yet!");
}
pReader.Read(block.StreamInfo.Loop);
pReader.Read(block.StreamInfo.ChannelCount);
pReader.Read(block.StreamInfo.Padding);
pReader.Read(block.StreamInfo.SampleRate);
pReader.Read(block.StreamInfo.LoopStartFrame);
pReader.Read(block.StreamInfo.LoopEndFrame);
pReader.Read(block.StreamInfo.SampleBlockNum);
pReader.Read(block.StreamInfo.SampleBlockSize);
pReader.Read(block.StreamInfo.SampleBlockSampleNum);
pReader.Read(block.StreamInfo.LastSampleBlockSize);
pReader.Read(block.StreamInfo.LastSampleBlockSampleNum);
pReader.Read(block.StreamInfo.LastSampleBlockPaddedSize);
pReader.Read(block.StreamInfo.SeekDataSize);
pReader.Read(block.StreamInfo.SeekIntervalSampleNum);
ReadReference(block.StreamInfo.SampleDataRef);
if (block.TrackInfoTabRef.Offset != 0xffffffff) {
pFile.seekg(pInfoBlockRef.Ref.Offset + sizeof(BlockHeader) +
block.TrackInfoTabRef.Offset,
std::ios::beg);
ReadReferenceTab(block.TrackInfoTab);
}
if (block.ChannelInfoTabRef.Offset != 0xffffffff) {
pFile.seekg(pInfoBlockRef.Ref.Offset + sizeof(BlockHeader) +
block.ChannelInfoTabRef.Offset,
std::ios::beg);
ReadReferenceTab(block.ChannelInfoTab);
}
for (auto& it : block.ChannelInfoTab.Refs) {
pFile.seekg(pInfoBlockRef.Ref.Offset + sizeof(BlockHeader) +
block.ChannelInfoTabRef.Offset + it.Offset,
std::ios::beg);
Reference r;
ReadReference(r);
block.ChannelInfoRefs.Add(r);
}
for (size_t i = 0; i < block.ChannelInfoRefs.Size(); i++) {
size_t off = pInfoBlockRef.Ref.Offset;
off += sizeof(BlockHeader);
off += block.ChannelInfoTabRef.Offset;
off += block.ChannelInfoTab.Refs[i].Offset;
off += block.ChannelInfoRefs[i].Offset;
pFile.seekg(off, std::ios::beg);
DSP_ADPCM_Info t; /** temp */
pReader.ReadEx(t); /** This Section gets read normally */
pDSP_ADPCM_Info.Add(t);
}
}
CTRFF_API void BCSTM::ReadSeekBlock(SD_Block& block) {
pReader.Read(pSeekBlock.Header.Magic);
pReader.Read(pSeekBlock.Header.Size);
if ((pSeekBlock.Header.Size % 20) != 0) {
throw std::runtime_error("BCSTM: SeekBlock Size is not 0x20 aligned!");
}
pSeekBlock.Data.Reserve(pSeekBlock.Header.Size + 1);
for (PD::u32 i = 0; i < pSeekBlock.Header.Size; i++) {
PD::u8 v;
pReader.Read(v);
pSeekBlock.Data.Add(v);
}
}
CTRFF_API void BCSTM::ReadReferenceTab(ReferenceTable& tab) {
pReader.Read(tab.Count);
tab.Refs.Reserve(tab.Count + 1);
for (PD::u32 i = 0; i < tab.Count; i++) {
Reference r;
pReader.Read(r.TypeID);
pReader.Read(r.Padding);
pReader.Read(r.Offset);
tab.Refs.Add(r);
}
}
CTRFF_API void BCSTM::ReadGotoBeginning(bool use_loop_beg) {
/**
* Go Up by 0x20 to skip header and empty section
* due to 0x20 alignment
*/
size_t off = pDataBlockRef.Ref.Offset + 0x20;
/** Shift to loop start if enabled */
if (use_loop_beg) {
off += GetNumBlocks() * GetNumChannels() * GetLoopStart();
// off += GetNumChannels() * pInfoBlock.StreamInfo.LoopStartFrame;
}
// block_size * channel_count * loop_start
try {
pFile.seekg(off, std::ios::beg);
} catch (const std::exception& e) {
throw std::runtime_error(e.what());
}
if (pFile.tellg() > pHeader.FileSize) {
throw std::runtime_error("BCSTM: Seeked Out of range!");
}
}
CTRFF_API void BCSTM::ReadBlock(PD::u32 block, PD::u8* ref) {
if (pFile.tellg() > pHeader.FileSize || block >= GetNumBlocks()) {
throw std::runtime_error("BCSTM: Decode block Out of range!");
}
pFile.read(
reinterpret_cast<char*>(ref),
(block == (GetNumBlocks() - 1) ? pInfoBlock.StreamInfo.LastSampleBlockSize
: GetBlockSize()));
}
CTRFF_API void BCSTM::CleanUp() {
if (pFile.is_open()) {
try {
pFile.close();
} catch (const std::exception& e) {
throw std::runtime_error(e.what());
}
}
pInfoBlock.ChannelInfoRefs.Clear();
pInfoBlock.ChannelInfoTab.Refs.Clear();
pInfoBlock.ChannelInfoTab.Count = 0;
pInfoBlock.TrackInfoTab.Refs.Clear();
pInfoBlock.TrackInfoTab.Count = 0;
pDSP_ADPCM_Info.Clear();
}
} // namespace ctrff

142
source/bcwav.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <ctrff/bcwav.hpp>
/** Using this a single time so inline it */
inline PD::u32 Swap32(PD::u32 in) {
return (in >> 24) | ((in >> 8) & 0x0000FF00) | ((in << 8) & 0x00FF0000) |
(in << 24);
}
namespace ctrff {
CTRFF_API void BCWAV::LoadFile(const std::string& path) {
CleanUp();
pFile.open(path, std::ios::in | std::ios::binary);
if (!pFile.is_open()) {
throw std::runtime_error("BCWAV Error: File not found!");
}
pReader.Read(pHeader.Magic);
pReader.Read(pHeader.Endianness);
if (pHeader.Endianness == Big) {
pHeader.Magic = Swap32(pHeader.Magic);
}
/** Check for 'CWAV' */
if (pHeader.Magic != 0x56415743) {
throw std::runtime_error("BCWAV Error: Invalid File!");
}
pReader.Read(pHeader.HeaderSize);
pReader.Read(pHeader.Version);
pReader.Read(pHeader.FileSize);
pReader.Read(pHeader.NumBlocks);
pReader.Read(pHeader.Reserved);
for (PD::u16 i = 0; i < pHeader.NumBlocks; i++) {
SizedReference ref;
ReadSizedReference(ref);
if (ref.Ref.TypeID == Ref_InfoBlock) {
pInfoBlockRef = ref;
} else if (ref.Ref.TypeID == Ref_DataBlock) {
pDataBlockRef = ref;
}
}
pFile.seekg(pInfoBlockRef.Ref.Offset);
ReadInfoBlock(pInfoBlock);
}
CTRFF_API void BCWAV::ReadReference(Reference& ref) {
pReader.Read(ref.TypeID);
pReader.Read(ref.Padding);
pReader.Read(ref.Offset);
}
CTRFF_API void BCWAV::ReadSizedReference(SizedReference& ref) {
ReadReference(ref.Ref);
pReader.Read(ref.Size);
}
CTRFF_API void BCWAV::ReadInfoBlock(InfoBlock& block) {
pReader.Read(block.Header.Magic);
pReader.Read(block.Header.Size);
pReader.Read(block.Encoding);
pReader.Read(block.Loop);
pReader.Read(block.Padding);
pReader.Read(block.SampleRate);
pReader.Read(block.LoopStartFrame);
pReader.Read(block.LoopEndFrame);
pReader.Read(block.Reserved);
ReadReferenceTab(block.ChannelInfoTab);
for (auto& it : block.ChannelInfoTab.Refs) {
pFile.seekg(pInfoBlockRef.Ref.Offset + sizeof(BlockHeader) + it.Offset,
std::ios::beg);
Reference r;
ReadReference(r);
block.ChannelInfoRefs.Add(r);
}
for (size_t i = 0; i < block.ChannelInfoRefs.Size(); i++) {
size_t off = pInfoBlockRef.Ref.Offset;
off += sizeof(BlockHeader);
off += block.ChannelInfoTab.Refs[i].Offset;
off += block.ChannelInfoRefs[i].Offset;
pFile.seekg(off, std::ios::beg);
DSP_ADPCM_Info t; /** temp */
pReader.ReadEx(t); /** This Section gets read normally */
pDSP_ADPCM_Info.Add(t);
}
}
CTRFF_API void BCWAV::ReadReferenceTab(ReferenceTable& tab) {
pReader.Read(tab.Count);
tab.Refs.Reserve(tab.Count + 1);
for (PD::u32 i = 0; i < tab.Count; i++) {
Reference r;
pReader.Read(r.TypeID);
pReader.Read(r.Padding);
pReader.Read(r.Offset);
tab.Refs.Add(r);
}
}
CTRFF_API void BCWAV::ReadGotoBeginning(bool use_loop_beg) {
/**
* Go Up by 0x20 to skip header and empty section
* due to 0x20 alignment
*/
size_t off = pDataBlockRef.Ref.Offset + 0x20;
/** Shift to loop start if enabled */
if (use_loop_beg) {
// off += GetNumBlocks() * GetNumChannels() * GetLoopStart();
// off += GetNumChannels() * pInfoBlock.StreamInfo.LoopStartFrame;
}
// block_size * channel_count * loop_start
try {
pFile.seekg(off, std::ios::beg);
} catch (const std::exception& e) {
throw std::runtime_error(e.what());
}
if (pFile.tellg() > pHeader.FileSize) {
throw std::runtime_error("BCWAV: Seeked Out of range!");
}
}
CTRFF_API void BCWAV::ReadBlock(PD::u32 block, PD::u8* ref) {
// if (pFile.tellg() > pHeader.FileSize || block >= GetNumBlocks()) {
// throw std::runtime_error("BCWAV: Decode block Out of range!");
// }
// pFile.read(
// reinterpret_cast<char*>(ref),
// (block == (GetNumBlocks() - 1) ?
// pInfoBlock.StreamInfo.LastSampleBlockSize
// : GetBlockSize()));
}
CTRFF_API void BCWAV::CleanUp() {
if (pFile.is_open()) {
try {
pFile.close();
} catch (const std::exception& e) {
throw std::runtime_error(e.what());
}
}
pInfoBlock.ChannelInfoRefs.Clear();
pInfoBlock.ChannelInfoTab.Refs.Clear();
pInfoBlock.ChannelInfoTab.Count = 0;
pDSP_ADPCM_Info.Clear();
}
} // namespace ctrff

41
source/binutil.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <ctrff/binutil.hpp>
namespace ctrff {
/** Supported reads */
template CTRFF_API void BinUtil::Read<PD::u8>(PD::u8&);
template CTRFF_API void BinUtil::Read<PD::u16>(PD::u16&);
template CTRFF_API void BinUtil::Read<PD::u32>(PD::u32&);
template CTRFF_API void BinUtil::Read<PD::u64>(PD::u64&);
/** Supported writes */
template CTRFF_API void BinUtil::Write<PD::u8>(const PD::u8&);
template CTRFF_API void BinUtil::Write<PD::u16>(const PD::u16&);
template CTRFF_API void BinUtil::Write<PD::u32>(const PD::u32&);
template CTRFF_API void BinUtil::Write<PD::u64>(const PD::u64&);
template <typename T>
void BinUtil::Read(T& v) {
// Check if Value could be Read
static_assert(std::is_integral<T>::value, "Cannot Read type T");
v = 0; // Set value to 0 (most cases a windows problem)
std::vector<PD::u8> buf(sizeof(T), 0); // declare buffer
// Read data into buffer
m_file.read(reinterpret_cast<char*>(buf.data()), sizeof(T));
// Loop or in be reverse loop and chift the values
for (int i = 0; i < sizeof(T); i++) {
v |= static_cast<T>(buf[m_big ? sizeof(T) - 1 - i : i]) << (8 * i);
}
}
template <typename T>
void BinUtil::Write(const T& v) {
// Check if Value could Write
static_assert(std::is_integral<T>::value, "Cannot Write type T");
std::vector<PD::u8> buf(sizeof(T), 0); // declare buffer
// Loop or in be reverse loop and write the values
for (size_t i = 0; i < sizeof(T); i++) {
buf[(m_big ? sizeof(T) - 1 - i : i)] = buf[m_big ? sizeof(T) - 1 - i : i] =
static_cast<PD::u8>((v >> (8 * i)) & 0xFF);
}
// Write buffer into file
m_file.write(reinterpret_cast<const char*>(buf.data()), sizeof(T));
}
} // namespace ctrff

159
source/helper.cpp Normal file
View File

@ -0,0 +1,159 @@
#include <ctrff/helper.hpp>
#include <cwchar>
#include <iostream>
void MakePixelRGBA(PD::u8 &r, PD::u8 &g, PD::u8 &b, PD::u8 &a, PD::u16 px) {
b = (px & 0x1f) << 3;
g = ((px >> 0x5) & 0x3f) << 2;
r = ((px >> 0xb) & 0x1f) << 3;
a = 0xff;
}
CTRFF_API PD::u16 MakePixel565(const PD::u8 &r, const PD::u8 &g,
const PD::u8 &b) {
PD::u16 res = 0;
res |= (r & ~0x7) << 8;
res |= (g & ~0x3) << 3;
res |= (b) >> 3;
return res;
}
CTRFF_API PD::u32 TileIndex(const int &x, const int &y, const int &w) {
return (((y >> 3) * (w >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
((x & 4) << 2) | ((y & 4) << 3));
}
// TODO: Fix colors
CTRFF_API void ctrff::RGB565toRGBA(std::vector<PD::u8> &img, PD::u16 *icon,
const int &w, const int &h) {
if (img.size() != (48 * 48 * 4)) {
img.resize(48 * 48 * 4);
img.clear();
}
for (PD::u32 y = 0; y < h; y++) {
for (PD::u32 x = 0; x < w; x++) {
auto idx = TileIndex(x, y, w);
PD::u32 pos = (y * w + x) * 4;
MakePixelRGBA(img[pos + 0], img[pos + 1], img[pos + 2], img[pos + 3],
icon[idx]);
}
}
}
CTRFF_API void ctrff::RGBA2RGB565(PD::u16 *out, const std::vector<PD::u8> &img,
const int &w, const int &h) {
if (img.size() != size_t(w * h * 4)) return;
std::vector<PD::u8> px8 = img;
for (PD::u32 y = 0; y < h; y++) {
for (PD::u32 x = 0; x < w; x++) {
auto idx = TileIndex(x, y, w);
PD::u32 pos = (y * w + x) * 4;
out[idx] = MakePixel565(img[pos], img[pos + 1], img[pos + 2]);
}
}
}
CTRFF_API std::vector<PD::u8> ctrff::DownscaleImage(
const std::vector<PD::u8> &img, int w, int h, int scale) {
std::vector<PD::u8> res(((w / scale) * (h / scale)) * 4);
int samples = scale * scale;
for (int y = 0; y < h; y += scale) {
for (int x = 0; x < w; x += scale) {
PD::u32 r = 0;
PD::u32 g = 0;
PD::u32 b = 0;
PD::u32 a = 0;
for (int oy = 0; oy < scale; oy++) {
for (int ox = 0; ox < scale; ox++) {
int pos = ((y + oy) * w + (x + ox)) * 4;
r += img[pos++];
g += img[pos++];
b += img[pos++];
a += img[pos++];
}
}
int pos = ((y / scale) * (w / scale) + (x / scale)) * 4;
res[pos++] = (PD::u8)(r / samples);
res[pos++] = (PD::u8)(g / samples);
res[pos++] = (PD::u8)(b / samples);
res[pos++] = (PD::u8)(a / samples);
}
}
return res;
}
CTRFF_API void ctrff::String2U16(PD::u16 *res, const std::string &src,
size_t max) {
/// GOT FORCED TO REPLACE std::wstring_convert by some
/// manual work as it got removed in cxx20
/// TODO ///
/// ADD SOME ERROR API IN HERE
if (max == 0) return;
size_t len = 0;
size_t i = 0;
while (i < src.size() && len < max) {
PD::u8 c = src[i];
if (c < 0x80) {
// 1byte
res[len++] = c;
i++;
} else if ((c >> 5) == 0x6) {
// 2byte
if (i + 1 >= src.size())
throw std::invalid_argument("Invalid UTF-8 sequence");
res[len++] = ((c & 0x1F) << 6) | (src[i + 1] & 0x3F);
i += 2;
} else if ((c >> 4) == 0xE) {
// 3byte
if (i + 2 >= src.size())
throw std::invalid_argument("Invalid UTF-8 sequence");
res[len++] =
((c & 0x0F) << 12) | ((src[i + 1] & 0x3F) << 6) | (src[i + 2] & 0x3F);
i += 3;
} else if ((c >> 3) == 0x1E) {
// 4byte
if (i + 3 >= src.size())
throw std::invalid_argument("Invalid UTF-8 sequence");
PD::u32 codepoint = ((c & 0x07) << 18) | ((src[i + 1] & 0x3F) << 12) |
((src[i + 2] & 0x3F) << 6) | (src[i + 3] & 0x3F);
codepoint -= 0x10000;
res[len++] = 0xD800 | ((codepoint >> 10) & 0x3FF);
res[len++] = 0xDC00 | (codepoint & 0x3FF);
i += 4;
} else {
return;
}
}
}
CTRFF_API std::string ctrff::U16toU8(PD::u16 *in, size_t max) {
/// GOT FORCED TO REPLACE std::wstring_convert by some
/// manual work as it got removed in cxx20
if (!in || max == 0) {
return "";
}
std::string result;
result.reserve(max * 3);
for (size_t i = 0; i < max; i++) {
uint16_t c = in[i];
if (c < 0x80) {
result.push_back(static_cast<char>(c));
} else if (c < 0x800) {
result.push_back(static_cast<char>(0xC0 | (c >> 6)));
result.push_back(static_cast<char>(0x80 | (c & 0x3F)));
} else if (c < 0x10000) {
result.push_back(static_cast<char>(0xE0 | (c >> 12)));
result.push_back(static_cast<char>(0x80 | ((c >> 6) & 0x3F)));
result.push_back(static_cast<char>(0x80 | (c & 0x3F)));
} else {
continue;
}
}
return result;
}

112
source/lz11.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <cstring>
#include <ctrff/lz11.hpp>
/// REWRITTEN CODE FROM BANNERTOOL !!!!!!
namespace ctrff {
namespace LZ11 {
PD::u32 GetOccurenceLength(const PD::u8* new_ptr, size_t new_len,
const PD::u8* old_ptr, size_t old_len,
PD::u32& disp) {
disp = 0;
if (new_len == 0) {
return 0;
}
PD::u32 res = 0;
if (old_len > 0) {
for (size_t i = 0; i < old_len - 1; i++) {
auto ref = old_ptr + i;
size_t len = 0;
for (size_t j = 0; j < new_len; j++) {
if (*(ref + j) != (*new_ptr + j)) {
break;
}
len++;
}
if (len > res) {
res = len;
disp = old_len - i;
if (res == new_len) {
break;
}
}
}
}
return res;
}
CTRFF_API std::vector<PD::u8> Compress(const std::vector<PD::u8>& in) {
if (in.size() > 0xFFFFFF) {
std::cout << "ERROR: LZ11 input is too large!" << std::endl;
return std::vector<PD::u8>();
}
std::stringstream s;
// SETUP HEADER //
s << static_cast<PD::u8>(0x11);
s << static_cast<PD::u8>(in.size() & 0xFF);
s << static_cast<PD::u8>((in.size() >> 8) & 0xFF);
s << static_cast<PD::u8>((in.size() >> 16) & 0xFF);
size_t res_len = 4; // 4-byte header
PD::u8 out_buf[0x21]; // 33 bytes
out_buf[0] = 0;
size_t obl = 1; // out_buf_len
size_t buf_blocks = 0;
size_t rb = 0;
while (rb < in.size()) {
if (buf_blocks == 8 || obl >= sizeof(out_buf) - 3) {
s.write(reinterpret_cast<const char*>(out_buf), obl);
res_len += obl;
out_buf[0] = 0;
obl = 1;
buf_blocks = 0;
}
PD::u32 disp = 0;
size_t old_len = std::min(rb, static_cast<size_t>(0x1000));
size_t len = LZ11::GetOccurenceLength(
in.data() + rb, std::min(in.size() - rb, static_cast<size_t>(0x10110)),
in.data() + rb - old_len, old_len, disp);
if (len < 3) {
out_buf[obl++] = in[rb++];
} else {
rb += len;
out_buf[0] |= static_cast<PD::u8>(1 << (7 - buf_blocks));
if (len > 0x110) {
out_buf[obl++] =
0x10 | static_cast<PD::u8>(((len - 0x111) >> 12) & 0x0F);
out_buf[obl++] = static_cast<PD::u8>(((len - 0x111) >> 4) & 0xFF);
out_buf[obl] = static_cast<PD::u8>(((len - 0x111) << 4) & 0xF0);
} else if (len > 0x10) {
out_buf[obl++] = 0x00 | static_cast<PD::u8>(((len - 0x11) >> 4) & 0x0F);
out_buf[obl] = static_cast<PD::u8>(((len - 0x11) << 4) & 0xF0);
} else {
out_buf[obl] |= static_cast<PD::u8>(((len - 1) << 4) & 0xF0);
}
obl++;
out_buf[obl++] = static_cast<PD::u8>(((disp - 1) >> 8) & 0x0F);
out_buf[obl++] = static_cast<PD::u8>((disp - 1) & 0xFF);
}
buf_blocks++;
}
if (buf_blocks > 0) {
s.write(reinterpret_cast<const char*>(out_buf), obl);
res_len += obl;
}
if (res_len % 4 != 0) {
PD::u32 pad_len = 4 - (res_len % 4);
PD::u8 pad[4] = {0};
s.write(reinterpret_cast<const char*>(pad), pad_len);
res_len += pad_len;
}
std::vector<PD::u8> res(res_len);
s.read(reinterpret_cast<char*>(res.data()), res.size());
return res;
}
} // namespace LZ11
} // namespace ctrff

127
source/smdh.cpp Normal file
View File

@ -0,0 +1,127 @@
#include <cstring>
#include <ctrff/smdh.hpp>
// magic
static const std::string smdh_magic = "SMDH";
CTRFF_API void ctrff::SMDH::Write(std::fstream &f) const {
f.write(reinterpret_cast<const char *>(Magic), sizeof(Magic));
f.write(reinterpret_cast<const char *>(&Version), sizeof(Version));
f.write(reinterpret_cast<const char *>(&Reserved), sizeof(Reserved));
for (size_t i = 0; i < 16; i++) {
f.write(reinterpret_cast<const char *>(&Titles[i]), sizeof(Title));
}
f.write(reinterpret_cast<const char *>(&Settings), sizeof(Settings));
f.write(reinterpret_cast<const char *>(&Reserved1), sizeof(Reserved1));
f.write(reinterpret_cast<const char *>(&IconSmall), sizeof(IconSmall));
f.write(reinterpret_cast<const char *>(&IconLarge), sizeof(IconLarge));
}
CTRFF_API void ctrff::SMDH::Read(std::fstream &f) {
f.seekg(0, std::ios::end);
if (f.tellg() != SMDH_Size) {
throw std::runtime_error(
"SMDH: File size does not match the SMDH Header size!");
}
f.seekg(0, std::ios::beg);
f.read(reinterpret_cast<char *>(Magic), sizeof(Magic));
if (std::string(Magic) != smdh_magic) {
throw std::runtime_error("SMDH: Invalid SMDH file!");
}
f.read(reinterpret_cast<char *>(&Version), sizeof(Version));
f.read(reinterpret_cast<char *>(&Reserved), sizeof(Reserved));
for (size_t i = 0; i < 16; i++) {
f.read(reinterpret_cast<char *>(&Titles[i]), sizeof(Title));
}
f.read(reinterpret_cast<char *>(&Settings), sizeof(Settings));
f.read(reinterpret_cast<char *>(&Reserved1), sizeof(Reserved1));
f.read(reinterpret_cast<char *>(&IconSmall), sizeof(IconSmall));
f.read(reinterpret_cast<char *>(&IconLarge), sizeof(IconLarge));
}
CTRFF_API void ctrff::SMDH::SetIcon(const std::vector<PD::u8> &buf) {
RGBA2RGB565(IconLarge, buf, 48, 48);
auto small_icon = DownscaleImage(buf, 48, 48, 2);
RGBA2RGB565(IconSmall, small_icon, 24, 24);
}
CTRFF_API std::vector<PD::u8> ctrff::SMDH::GetIcon() {
std::vector<PD::u8> res(48 * 48 * 4);
ctrff::RGB565toRGBA(res, IconLarge, 48, 48);
return res;
}
CTRFF_API void ctrff::SMDH::SetShortTitle(const std::string &t, Language l) {
if (l == Language_All) {
for (int i = 0; i < 16; i++) {
ctrff::String2U16(Titles[i].ShortTitle, t, 0x40);
}
return;
}
ctrff::String2U16(Titles[l].ShortTitle, t, 0x40);
}
CTRFF_API void ctrff::SMDH::SetLongTitle(const std::string &t, Language l) {
if (l == Language_All) {
for (int i = 0; i < 16; i++) {
ctrff::String2U16(Titles[i].LongTitle, t, 0x80);
}
return;
}
ctrff::String2U16(Titles[l].LongTitle, t, 0x80);
}
CTRFF_API void ctrff::SMDH::SetAuthor(const std::string &t, Language l) {
if (l == Language_All) {
for (int i = 0; i < 16; i++) {
ctrff::String2U16(Titles[i].Author, t, 0x40);
}
return;
}
ctrff::String2U16(Titles[l].Author, t, 0x40);
}
CTRFF_API std::string ctrff::SMDH::GetShortTitle(Language l) {
if (l == Language_All) {
return ctrff::U16toU8(Titles[0].ShortTitle, 0x40);
}
return ctrff::U16toU8(Titles[l].ShortTitle, 0x40);
}
CTRFF_API std::string ctrff::SMDH::GetLongTitle(Language l) {
if (l == Language_All) {
return ctrff::U16toU8(Titles[0].LongTitle, 0x80);
}
return ctrff::U16toU8(Titles[l].LongTitle, 0x80);
}
CTRFF_API std::string ctrff::SMDH::GetAuthor(Language l) {
if (l == Language_All) {
return ctrff::U16toU8(Titles[0].Author, 0x40);
}
return ctrff::U16toU8(Titles[l].Author, 0x40);
}
CTRFF_API ctrff::SMDH ctrff::SMDH::Default() {
SMDH n3w; /** new */
for (int i = 0; i < 4; i++) {
n3w.Magic[i] = smdh_magic[i];
}
// Set Defaults
n3w.Settings.MatchmakerID = 0;
n3w.Settings.MatchmakerBitID = 0;
n3w.Settings.EulaVersion = 0;
n3w.Settings.OptimalBannerFrame = 0;
n3w.Settings.StreetpassID = 0;
for (size_t i = 0; i < 16; i++) {
n3w.Settings.Ratings[i] = 0;
}
n3w.Version = 0;
n3w.Reserved = 0;
n3w.Reserved1 = 0;
n3w.Settings.Flags = Flag_DEFAULT;
n3w.Settings.RegionLock = Region_FREE;
std::fill_n(n3w.IconSmall, 0x240, 0);
std::fill_n(n3w.IconLarge, 0x900, 0);
return n3w;
}