summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkr3nshaw <20672068+kr3nshaw@users.noreply.github.com>2020-06-08 02:03:41 +1000
committerkr3nshaw <20672068+kr3nshaw@users.noreply.github.com>2020-06-08 02:03:41 +1000
commit696862b1a3025b9f054a26889f4aec42d0675f30 (patch)
tree50caa361f1af0d546df6683a3f0218474a7973ea
parent760a22318663896b2ebd76d8d3213dcc9e9bcfe5 (diff)
Replaced narccomp with knarc
-rw-r--r--Makefile16
-rw-r--r--tools/knarc/Makefile7
-rw-r--r--tools/knarc/Narc.cpp536
-rw-r--r--tools/knarc/Narc.h86
-rw-r--r--tools/knarc/Source.cpp109
-rw-r--r--tools/narccomp/Makefile13
-rw-r--r--tools/narccomp/narccomp.cpp261
7 files changed, 745 insertions, 283 deletions
diff --git a/Makefile b/Makefile
index 4cb2b845..39594cc8 100644
--- a/Makefile
+++ b/Makefile
@@ -26,9 +26,9 @@ endif
ifeq ($(OS),Windows_NT)
EXE := .exe
-WINE :=
+WINE :=
else
-EXE :=
+EXE :=
WINE := wine
endif
@@ -170,9 +170,9 @@ MWCCARM = tools/mwccarm/$(MWCCVERSION)/mwccarm.exe
# have to use mwldarm for now.
# TODO: Is there a hack workaround to let us go back to GNU LD? Ideally, the
# only dependency should be MWCCARM.
+KNARC = tools/knarc/knarc$(EXE)
MWLDARM = tools/mwccarm/$(MWCCVERSION)/mwldarm.exe
MWASMARM = tools/mwccarm/$(MWCCVERSION)/mwasmarm.exe
-NARCCOMP = tools/narccomp/narccomp$(EXE)
SCANINC = tools/scaninc/scaninc$(EXE)
AS = $(WINE) $(MWASMARM)
@@ -308,13 +308,11 @@ DUMMY != mkdir -p $(ALL_DIRS)
%.png: ;
%.pal: ;
-%.narc: members = $(wildcard $(@D)/$*/*)
-%.narc: $$(members)
- $(NARCCOMP) -o $@ -p 255 $^
+%.narc:
+ $(KNARC) -d $(basename $@)/ -p $@
-%.arc: members = $(wildcard $(@D)/$*/*)
-%.arc: $$(members)
- $(NARCCOMP) -o $@ -p 255 $^
+%.arc:
+ $(KNARC) -d $(basename $@)/ -p $@
files/poketool/personal/pms.narc: ;
diff --git a/tools/knarc/Makefile b/tools/knarc/Makefile
new file mode 100644
index 00000000..a466912c
--- /dev/null
+++ b/tools/knarc/Makefile
@@ -0,0 +1,7 @@
+all: knarc
+
+knarc: Source.cpp Narc.cpp
+ g++ -std=c++17 -o knarc Source.cpp Narc.cpp
+
+clean:
+ rm knarc
diff --git a/tools/knarc/Narc.cpp b/tools/knarc/Narc.cpp
new file mode 100644
index 00000000..5e2713dd
--- /dev/null
+++ b/tools/knarc/Narc.cpp
@@ -0,0 +1,536 @@
+#include "Narc.h"
+
+#include <algorithm>
+#include <cstddef>
+#include <cstdint>
+#include <filesystem>
+#include <fstream>
+#include <iomanip>
+#include <ios>
+#include <map>
+#include <memory>
+#include <regex>
+#include <sstream>
+#include <stack>
+#include <string>
+#include <vector>
+
+using namespace std;
+
+void Narc::AlignDword(ofstream& ofs, uint8_t paddingChar)
+{
+ if ((ofs.tellp() % 4) != 0)
+ {
+ for (int i = 4 - (ofs.tellp() % 4); i-- > 0; )
+ {
+ ofs.write(reinterpret_cast<char*>(&paddingChar), sizeof(uint8_t));
+ }
+ }
+}
+
+bool Narc::Cleanup(ifstream& ifs, const NarcError& e)
+{
+ ifs.close();
+
+ error = e;
+
+ return false;
+}
+
+bool Narc::Cleanup(ofstream& ofs, const NarcError& e)
+{
+ ofs.close();
+
+ error = e;
+
+ return false;
+}
+
+vector<filesystem::directory_entry> Narc::OrderedDirectoryIterator(const filesystem::path& path, bool recursive) const
+{
+ vector<filesystem::directory_entry> v;
+
+ for (const auto& de : filesystem::directory_iterator(path))
+ {
+ v.push_back(de);
+ }
+
+ sort(v.begin(), v.end(), [](const filesystem::directory_entry& a, const filesystem::directory_entry& b)
+ {
+ // I fucking hate C++
+ string aStr = a.path().filename().string();
+ string bStr = b.path().filename().string();
+
+ for (size_t i = 0; i < aStr.size(); ++i)
+ {
+ aStr[i] = tolower(aStr[i]);
+ }
+
+ for (size_t i = 0; i < bStr.size(); ++i)
+ {
+ bStr[i] = tolower(bStr[i]);
+ }
+
+ return aStr < bStr;
+ });
+
+ if (recursive)
+ {
+ size_t vSize = v.size();
+
+ for (size_t i = 0; i < vSize; ++i)
+ {
+ if (v[i].is_directory())
+ {
+ vector<filesystem::directory_entry> temp = OrderedDirectoryIterator(v[i], true);
+
+ v.insert(v.end(), temp.begin(), temp.end());
+ }
+ }
+ }
+
+ return v;
+}
+
+NarcError Narc::GetError() const
+{
+ return error;
+}
+
+bool Narc::Pack(const filesystem::path& fileName, const filesystem::path& directory)
+{
+ ofstream ofs(fileName, ios::binary);
+
+ if (!ofs.good()) { return Cleanup(ofs, NarcError::InvalidOutputFile); }
+
+ vector<FileAllocationTableEntry> fatEntries;
+ uint16_t directoryCounter = 1;
+
+ for (const auto& de : OrderedDirectoryIterator(directory, true))
+ {
+ if (de.is_directory())
+ {
+ ++directoryCounter;
+ }
+ else
+ {
+ fatEntries.push_back(FileAllocationTableEntry
+ {
+ .Start = 0x0,
+ .End = 0x0
+ });
+
+ if (fatEntries.size() > 1)
+ {
+ fatEntries.back().Start = fatEntries.rbegin()[1].End;
+
+ if ((fatEntries.rbegin()[1].End % 4) != 0)
+ {
+ fatEntries.back().Start += 4 - (fatEntries.rbegin()[1].End % 4);
+ }
+ }
+
+ fatEntries.back().End = fatEntries.back().Start + static_cast<uint32_t>(de.file_size());
+ }
+ }
+
+ FileAllocationTable fat
+ {
+ .Id = 0x46415442,
+ .ChunkSize = sizeof(FileAllocationTable) + ((uint32_t)fatEntries.size() * sizeof(FileAllocationTableEntry)),
+ .FileCount = static_cast<uint16_t>(fatEntries.size()),
+ .Reserved = 0x0
+ };
+
+ map<filesystem::path, string> subTables;
+ vector<filesystem::path> paths;
+
+ directoryCounter = 0;
+
+ for (const auto& de : OrderedDirectoryIterator(directory, true))
+ {
+ if (!subTables.count(de.path().parent_path()))
+ {
+ subTables.insert({ de.path().parent_path(), "" });
+ paths.push_back(de.path().parent_path());
+ }
+
+ if (de.is_directory())
+ {
+ ++directoryCounter;
+
+ subTables[de.path().parent_path()] += static_cast<uint8_t>(0x80 + de.path().filename().string().size());
+ subTables[de.path().parent_path()] += de.path().filename().string();
+ subTables[de.path().parent_path()] += (0xF000 + directoryCounter) & 0xFF;
+ subTables[de.path().parent_path()] += (0xF000 + directoryCounter) >> 8;
+ }
+ else
+ {
+ subTables[de.path().parent_path()] += static_cast<uint8_t>(de.path().filename().string().size());
+ subTables[de.path().parent_path()] += de.path().filename().string();
+ }
+ }
+
+ for (auto& subTable : subTables)
+ {
+ subTable.second += '\0';
+ }
+
+ vector<FileNameTableEntry> fntEntries;
+
+ if (!regex_match(filesystem::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin")))
+ {
+ fntEntries.push_back(
+ {
+ .Offset = (directoryCounter + 1) * sizeof(FileNameTableEntry),
+ .FirstFileId = 0x0,
+ .Utility = static_cast<uint16_t>(directoryCounter + 1)
+ });
+
+ for (uint16_t i = 0; i < directoryCounter; ++i)
+ {
+ fntEntries.push_back(
+ {
+ .Offset = fntEntries.back().Offset + subTables[paths[i]].size(),
+ .FirstFileId = fntEntries.back().FirstFileId,
+ .Utility = 0x0
+ });
+
+ for (size_t j = 0; j < (subTables[paths[i]].size() - 1); ++j)
+ {
+ if (static_cast<uint8_t>(subTables[paths[i]][j]) <= 0x7F)
+ {
+ j += static_cast<uint8_t>(subTables[paths[i]][j]);
+ ++fntEntries.back().FirstFileId;
+ }
+ else if (static_cast<uint8_t>(subTables[paths[i]][j]) <= 0xFF)
+ {
+ j += static_cast<uint8_t>(subTables[paths[i]][j]) - 0x80 + 0x2;
+ }
+ }
+
+ fntEntries.back().Utility = 0xF000 + (find(paths.begin(), paths.end(), paths[i + 1].parent_path()) - paths.begin());
+ }
+ }
+ else
+ {
+ fntEntries.push_back(
+ {
+ .Offset = 0x4,
+ .FirstFileId = 0x0,
+ .Utility = 0x1
+ });
+ }
+
+ FileNameTable fnt
+ {
+ .Id = 0x464E5442,
+ .ChunkSize = sizeof(FileNameTable) + (fntEntries.size() * sizeof(FileNameTableEntry))
+ };
+
+ if (!regex_match(filesystem::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin")))
+ {
+ for (const auto& subTable : subTables)
+ {
+ fnt.ChunkSize += subTable.second.size();
+ }
+ }
+
+ if ((fnt.ChunkSize % 4) != 0)
+ {
+ fnt.ChunkSize += 4 - (fnt.ChunkSize % 4);
+ }
+
+ FileImages fi
+ {
+ .Id = 0x46494D47,
+ .ChunkSize = sizeof(FileImages) + fatEntries.back().End
+ };
+
+ if ((fi.ChunkSize % 4) != 0)
+ {
+ fi.ChunkSize += 4 - (fi.ChunkSize % 4);
+ }
+
+ Header header
+ {
+ .Id = 0x4352414E,
+ .ByteOrderMark = 0xFFFE,
+ .Version = 0x100,
+ .FileSize = sizeof(Header) + fat.ChunkSize + fnt.ChunkSize + fi.ChunkSize,
+ .ChunkSize = sizeof(Header),
+ .ChunkCount = 0x3
+ };
+
+ ofs.write(reinterpret_cast<char*>(&header), sizeof(Header));
+ ofs.write(reinterpret_cast<char*>(&fat), sizeof(FileAllocationTable));
+
+ for (auto& entry : fatEntries)
+ {
+ ofs.write(reinterpret_cast<char*>(&entry), sizeof(FileAllocationTableEntry));
+ }
+
+ ofs.write(reinterpret_cast<char*>(&fnt), sizeof(FileNameTable));
+
+ for (auto& entry : fntEntries)
+ {
+ ofs.write(reinterpret_cast<char*>(&entry), sizeof(FileNameTableEntry));
+ }
+
+ if (!regex_match(filesystem::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin")))
+ {
+ for (const auto& path : paths)
+ {
+ ofs << subTables[path];
+ }
+ }
+
+ AlignDword(ofs, 0xFF);
+
+ ofs.write(reinterpret_cast<char*>(&fi), sizeof(FileImages));
+
+ for (const auto& de : OrderedDirectoryIterator(directory, true))
+ {
+ if (de.is_directory())
+ {
+ continue;
+ }
+
+ ifstream ifs(de.path(), ios::binary | ios::ate);
+
+ if (!ifs.good())
+ {
+ ifs.close();
+
+ return Cleanup(ofs, NarcError::InvalidInputFile);
+ }
+
+ streampos length = ifs.tellg();
+ unique_ptr<char[]> buffer = make_unique<char[]>(static_cast<unsigned int>(length));
+
+ ifs.seekg(0);
+ ifs.read(buffer.get(), length);
+ ifs.close();
+
+ ofs.write(buffer.get(), length);
+
+ AlignDword(ofs, 0xFF);
+ }
+
+ ofs.close();
+
+ return error == NarcError::None ? true : false;
+}
+
+bool Narc::Unpack(const filesystem::path& fileName, const filesystem::path& directory)
+{
+ ifstream ifs(fileName, ios::binary);
+
+ if (!ifs.good()) { return Cleanup(ifs, NarcError::InvalidInputFile); }
+
+ Header header;
+ ifs.read(reinterpret_cast<char*>(&header), sizeof(Header));
+
+ if (header.Id != 0x4352414E) { return Cleanup(ifs, NarcError::InvalidHeaderId); }
+ if (header.ByteOrderMark != 0xFFFE) { return Cleanup(ifs, NarcError::InvalidByteOrderMark); }
+ if ((header.Version != 0x0100) && (header.Version != 0x0000)) { return Cleanup(ifs, NarcError::InvalidVersion); }
+ if (header.ChunkSize != 0x10) { return Cleanup(ifs, NarcError::InvalidHeaderSize); }
+ if (header.ChunkCount != 0x3) { return Cleanup(ifs, NarcError::InvalidChunkCount); }
+
+ FileAllocationTable fat;
+ ifs.read(reinterpret_cast<char*>(&fat), sizeof(FileAllocationTable));
+
+ if (fat.Id != 0x46415442) { return Cleanup(ifs, NarcError::InvalidFileAllocationTableId); }
+ if (fat.Reserved != 0x0) { return Cleanup(ifs, NarcError::InvalidFileAllocationTableReserved); }
+
+ unique_ptr<FileAllocationTableEntry[]> fatEntries = make_unique<FileAllocationTableEntry[]>(fat.FileCount);
+
+ for (uint16_t i = 0; i < fat.FileCount; ++i)
+ {
+ ifs.read(reinterpret_cast<char*>(&fatEntries.get()[i]), sizeof(FileAllocationTableEntry));
+ }
+
+ FileNameTable fnt;
+ vector<FileNameTableEntry> FileNameTableEntries;
+ ifs.read(reinterpret_cast<char*>(&fnt), sizeof(FileNameTable));
+
+ if (fnt.Id != 0x464E5442) { return Cleanup(ifs, NarcError::InvalidFileNameTableId); }
+
+ vector<FileNameTableEntry> fntEntries;
+
+ do
+ {
+ fntEntries.push_back(FileNameTableEntry());
+
+ ifs.read(reinterpret_cast<char*>(&fntEntries.back().Offset), sizeof(uint32_t));
+ ifs.read(reinterpret_cast<char*>(&fntEntries.back().FirstFileId), sizeof(uint16_t));
+ ifs.read(reinterpret_cast<char*>(&fntEntries.back().Utility), sizeof(uint16_t));
+ } while (static_cast<uint32_t>(ifs.tellg()) < (header.ChunkSize + fat.ChunkSize + sizeof(FileNameTable) + fntEntries[0].Offset));
+
+ unique_ptr<string[]> fileNames = make_unique<string[]>(0xFFFF);
+
+ for (size_t i = 0; i < fntEntries.size(); ++i)
+ {
+ ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + sizeof(FileNameTable) + fntEntries[i].Offset);
+
+ uint16_t fileId = 0x0000;
+
+ for (uint8_t length = 0x80; length != 0x00; ifs.read(reinterpret_cast<char*>(&length), sizeof(uint8_t)))
+ {
+ if (length <= 0x7F)
+ {
+ for (uint8_t j = 0; j < length; ++j)
+ {
+ uint8_t c;
+ ifs.read(reinterpret_cast<char*>(&c), sizeof(uint8_t));
+
+ fileNames.get()[fntEntries[i].FirstFileId + fileId] += c;
+ }
+
+ ++fileId;
+ }
+ else if (length == 0x80)
+ {
+ // Reserved
+ }
+ else if (length <= 0xFF)
+ {
+ length -= 0x80;
+ string directoryName;
+
+ for (uint8_t j = 0; j < length; ++j)
+ {
+ uint8_t c;
+ ifs.read(reinterpret_cast<char*>(&c), sizeof(uint8_t));
+
+ directoryName += c;
+ }
+
+ uint16_t directoryId;
+ ifs.read(reinterpret_cast<char*>(&directoryId), sizeof(uint16_t));
+
+ fileNames.get()[directoryId] = directoryName;
+ }
+ else
+ {
+ return Cleanup(ifs, NarcError::InvalidFileNameTableEntryId);
+ }
+ }
+ }
+
+ if ((ifs.tellg() % 4) != 0)
+ {
+ ifs.seekg(4 - (ifs.tellg() % 4), ios::cur);
+ }
+
+ FileImages fi;
+ ifs.read(reinterpret_cast<char*>(&fi), sizeof(FileImages));
+
+ if (fi.Id != 0x46494D47) { return Cleanup(ifs, NarcError::InvalidFileImagesId); }
+
+ filesystem::create_directory(directory);
+ filesystem::current_path(directory);
+
+ if (fnt.ChunkSize == 0x10)
+ {
+ for (uint16_t i = 0; i < fat.FileCount; ++i)
+ {
+ ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + fnt.ChunkSize + 8 + fatEntries.get()[i].Start);
+
+ unique_ptr<char[]> buffer = make_unique<char[]>(fatEntries.get()[i].End - fatEntries.get()[i].Start);
+ ifs.read(buffer.get(), fatEntries.get()[i].End - fatEntries.get()[i].Start);
+
+ ostringstream oss;
+ oss << fileName.stem().string() << "_" << setfill('0') << setw(8) << i << ".bin";
+
+ ofstream ofs(oss.str(), ios::binary);
+
+ if (!ofs.good())
+ {
+ ofs.close();
+
+ return Cleanup(ifs, NarcError::InvalidOutputFile);
+ }
+
+ ofs.write(buffer.get(), fatEntries.get()[i].End - fatEntries.get()[i].Start);
+ ofs.close();
+ }
+ }
+ else
+ {
+ filesystem::path absolutePath = filesystem::absolute(filesystem::current_path());
+
+ for (size_t i = 0; i < fntEntries.size(); ++i)
+ {
+ filesystem::current_path(absolutePath);
+ stack<string> directories;
+
+ for (uint16_t j = fntEntries[i].Utility; j > 0xF000; j = fntEntries[j - 0xF000].Utility)
+ {
+ directories.push(fileNames.get()[j]);
+ }
+
+ for (; !directories.empty(); directories.pop())
+ {
+ filesystem::create_directory(directories.top());
+ filesystem::current_path(directories.top());
+ }
+
+ if (fntEntries[i].Utility >= 0xF000)
+ {
+ filesystem::create_directory(fileNames.get()[0xF000 + i]);
+ filesystem::current_path(fileNames.get()[0xF000 + i]);
+ }
+
+ ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + sizeof(FileNameTable) + fntEntries[i].Offset);
+
+ uint16_t fileId = 0x0000;
+
+ for (uint8_t length = 0x80; length != 0x00; ifs.read(reinterpret_cast<char*>(&length), sizeof(uint8_t)))
+ {
+ if (length <= 0x7F)
+ {
+ streampos savedPosition = ifs.tellg();
+
+ ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + fnt.ChunkSize + 8 + fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start);
+
+ unique_ptr<char[]> buffer = make_unique<char[]>(fatEntries.get()[fntEntries[i].FirstFileId + fileId].End - fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start);
+ ifs.read(buffer.get(), fatEntries.get()[fntEntries[i].FirstFileId + fileId].End - fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start);
+
+ ofstream ofs(fileNames.get()[fntEntries[i].FirstFileId + fileId], ios::binary);
+
+ if (!ofs.good())
+ {
+ ofs.close();
+
+ return Cleanup(ifs, NarcError::InvalidOutputFile);
+ }
+
+ ofs.write(buffer.get(), fatEntries.get()[fntEntries[i].FirstFileId + fileId].End - fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start);
+ ofs.close();
+
+ ifs.seekg(savedPosition);
+ ifs.seekg(length, ios::cur);
+
+ ++fileId;
+ }
+ else if (length == 0x80)
+ {
+ // Reserved
+ }
+ else if (length <= 0xFF)
+ {
+ ifs.seekg(static_cast<uint64_t>(length) - 0x80 + 0x2, ios::cur);
+ }
+ else
+ {
+ return Cleanup(ifs, NarcError::InvalidFileNameTableEntryId);
+ }
+ }
+ }
+ }
+
+ ifs.close();
+
+ return error == NarcError::None ? true : false;
+}
diff --git a/tools/knarc/Narc.h b/tools/knarc/Narc.h
new file mode 100644
index 00000000..d9de52bb
--- /dev/null
+++ b/tools/knarc/Narc.h
@@ -0,0 +1,86 @@
+#pragma once
+
+#include <cstdint>
+#include <filesystem>
+#include <fstream>
+#include <string>
+#include <vector>
+
+enum class NarcError
+{
+ None,
+ InvalidInputFile,
+ InvalidHeaderId,
+ InvalidByteOrderMark,
+ InvalidVersion,
+ InvalidHeaderSize,
+ InvalidChunkCount,
+ InvalidFileAllocationTableId,
+ InvalidFileAllocationTableReserved,
+ InvalidFileNameTableId,
+ InvalidFileNameTableEntryId,
+ InvalidFileImagesId,
+ InvalidOutputFile
+};
+
+struct Header
+{
+ uint32_t Id;
+ uint16_t ByteOrderMark;
+ uint16_t Version;
+ uint32_t FileSize;
+ uint16_t ChunkSize;
+ uint16_t ChunkCount;
+};
+
+struct FileAllocationTable
+{
+ uint32_t Id;
+ uint32_t ChunkSize;
+ uint16_t FileCount;
+ uint16_t Reserved;
+};
+
+struct FileAllocationTableEntry
+{
+ uint32_t Start;
+ uint32_t End;
+};
+
+struct FileNameTable
+{
+ uint32_t Id;
+ uint32_t ChunkSize;
+};
+
+struct FileNameTableEntry
+{
+ uint32_t Offset;
+ uint16_t FirstFileId;
+ uint16_t Utility;
+};
+
+struct FileImages
+{
+ uint32_t Id;
+ uint32_t ChunkSize;
+};
+
+class Narc
+{
+ public:
+ NarcError GetError() const;
+
+ bool Pack(const std::filesystem::path& fileName, const std::filesystem::path& directory);
+ bool Unpack(const std::filesystem::path& fileName, const std::filesystem::path& directory);
+
+ private:
+ NarcError error = NarcError::None;
+
+ void AlignDword(std::ofstream& ofs, uint8_t paddingChar);
+
+ bool Cleanup(std::ifstream& ifs, const NarcError& e);
+ bool Cleanup(std::ofstream& ofs, const NarcError& e);
+
+ std::vector<std::filesystem::directory_entry> OrderedDirectoryIterator(const std::filesystem::path& path, bool recursive) const;
+};
diff --git a/tools/knarc/Source.cpp b/tools/knarc/Source.cpp
new file mode 100644
index 00000000..3678997c
--- /dev/null
+++ b/tools/knarc/Source.cpp
@@ -0,0 +1,109 @@
+#include <cstring>
+#include <filesystem>
+#include <iostream>
+#include <string>
+
+#include "Narc.h"
+
+using namespace std;
+
+void PrintError(NarcError error)
+{
+ switch (error)
+ {
+ case NarcError::None: cout << "ERROR: No error???" << endl; break;
+ case NarcError::InvalidInputFile: cout << "ERROR: Invalid input file" << endl; break;
+ case NarcError::InvalidHeaderId: cout << "ERROR: Invalid header ID" << endl; break;
+ case NarcError::InvalidByteOrderMark: cout << "ERROR: Invalid byte order mark" << endl; break;
+ case NarcError::InvalidVersion: cout << "ERROR: Invalid NARC version" << endl; break;
+ case NarcError::InvalidHeaderSize: cout << "ERROR: Invalid header size" << endl; break;
+ case NarcError::InvalidChunkCount: cout << "ERROR: Invalid chunk count" << endl; break;
+ case NarcError::InvalidFileAllocationTableId: cout << "ERROR: Invalid file allocation table ID" << endl; break;
+ case NarcError::InvalidFileAllocationTableReserved: cout << "ERROR: Invalid file allocation table reserved section" << endl; break;
+ case NarcError::InvalidFileNameTableId: cout << "ERROR: Invalid file name table ID" << endl; break;
+ case NarcError::InvalidFileNameTableEntryId: cout << "ERROR: Invalid file name table entry ID" << endl; break;
+ case NarcError::InvalidFileImagesId: cout << "ERROR: Invalid file images ID" << endl; break;
+ case NarcError::InvalidOutputFile: cout << "ERROR: Invalid output file" << endl; break;
+ default: cout << "ERROR: Unknown error???" << endl; break;
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ if (argc != 5)
+ {
+ cout << "OVERVIEW: Knarc" << endl << endl;
+ cout << "USAGE: knarc [options] <inputs>" << endl << endl;
+ cout << "OPTIONS:" << endl;
+ cout << "\t-d\tDirectory to pack from/unpack to" << endl;
+ cout << "\t-p\tPack" << endl;
+ cout << "\t-u\tUnpack" << endl;
+
+ return 1;
+ }
+
+ string directory = "";
+ string fileName = "";
+ bool pack = false;
+
+ for (int i = 1; i < argc; ++i)
+ {
+ if (!strcmp(argv[i], "-d"))
+ {
+ if (i == (argc - 1))
+ {
+ cerr << "ERROR: No directory specified" << endl;
+
+ return 1;
+ }
+
+ directory = argv[++i];
+ }
+ else if (!strcmp(argv[i], "-p"))
+ {
+ if (i == (argc - 1))
+ {
+ cerr << "ERROR: No NARC specified to pack to" << endl;
+
+ return 1;
+ }
+
+ fileName = argv[++i];
+ pack = true;
+ }
+ else if (!strcmp(argv[i], "-u"))
+ {
+ if (i == (argc - 1))
+ {
+ cerr << "ERROR: No NARC specified to unpack from" << endl;
+
+ return 1;
+ }
+
+ fileName = argv[++i];
+ }
+ }
+
+ Narc narc;
+
+ if (pack)
+ {
+ if (!narc.Pack(fileName, directory))
+ {
+ PrintError(narc.GetError());
+
+ return 1;
+ }
+ }
+ else
+ {
+ if (!narc.Unpack(fileName, directory))
+ {
+ PrintError(narc.GetError());
+
+ return 1;
+ }
+ }
+
+ return 0;
+}
diff --git a/tools/narccomp/Makefile b/tools/narccomp/Makefile
deleted file mode 100644
index b484e7a1..00000000
--- a/tools/narccomp/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-CXX = g++
-CXXFLAGS := -O3 -std=c++11
-
-.PHONY: all clean
-
-all: narccomp
- @:
-
-clean:
- $(RM) narccomp narccomp.exe
-
-narccomp: narccomp.cpp
- $(CXX) $(CXXFLAGS) -o $@ $^
diff --git a/tools/narccomp/narccomp.cpp b/tools/narccomp/narccomp.cpp
deleted file mode 100644
index 2cbbeb4c..00000000
--- a/tools/narccomp/narccomp.cpp
+++ /dev/null
@@ -1,261 +0,0 @@
-#include <iostream>
-#include <fstream>
-#include <vector>
-#include <string>
-#include <cstring>
-#include <sstream>
-#include <map>
-#include <set>
-
-using namespace std;
-
-struct Options
-{
- fstream outfile;
- map<string, string> infiles;
- unsigned char padval;
- bool keep_names;
- string host_root;
- string nitro_root;
- Options(int const & argc, char ** const & argv) :
- // Default values
- padval(255),
- keep_names(false)
- {
- char last_opt = 0;
- string outfname; // Use this to avoid clobbering an existing file in the event of an error
- for (string arg : vector<char *>(argv + 1, argv + argc))
- {
- if (last_opt != 0) {
- switch (last_opt)
- {
- case 'p':
- {
- // Padding
- unsigned long x;
- stringstream is(arg);
- is >> x;
- if (x >= 256) {
- stringstream ss;
- ss << "Invalid value for -p: " << x << endl;
- throw invalid_argument(ss.str());
- }
- padval = x;
- break;
- }
- case 'o':
- // Outfile
- if (!outfname.empty()) {
- throw invalid_argument("Can't output more than one file\n");
- }
- outfname = arg;
- break;
- case 'r':
- // Not fully implemented
- if (!host_root.empty()) {
- throw invalid_argument("Duplicate settings for option -r\n");
- }
- host_root = arg;
- break;
- case 'R':
- // Not fully implemented
- if (!nitro_root.empty()) {
- throw invalid_argument("Duplicate settings for option -R\n");
- }
- nitro_root = arg;
- break;
- default:
- stringstream ss;
- ss << "Unrecognized option: -" << (char)last_opt << endl;
- throw invalid_argument(ss.str());
- }
- last_opt = 0;
- }
- else if (arg[0] == '-') {
- if (arg[1] == 0 || arg[2] != 0) {
- stringstream ss;
- ss << "Unrecognized option: " << arg << endl;
- throw invalid_argument(ss.str());
- }
- switch (arg[1]) {
- case 'h':
- cout << "Usage: " << argv[0] << " [-p PADVAL] [-h] -o OUTFILE FILE [FILE ...]" << endl;
- cout << endl;
- cout << " FILE [FILE ...] List of files to be packed into the archive." << endl;
- cout << " -o OUTFILE The destination archive, in NARC format" << endl;
- cout << " -p PADVAL Pad archive members to word with this character. Default: 255" << endl;
- cout << " -h Print this message and exit" << endl;
- exit(0);
- case 'k':
- throw invalid_argument("'-k': not implemented\n");
-// if (keep_names) {
-// throw invalid_argument("'-k' specified more than once\n");
-// }
-// keep_names = true;
-// break;
- default:
- last_opt = arg[1];
- }
- }
- else
- {
- fstream file;
- stringstream uss;
-
- if (!infiles[arg].empty()) {
- stringstream ss;
- ss << "Duplicate file: " << arg << endl;
- throw invalid_argument(ss.str());
- }
- file.open(arg, ios_base::in | ios_base::binary);
- uss << file.rdbuf();
- file.close();
- infiles[arg] = uss.str();
- }
- }
- if (outfname.empty()) {
- throw invalid_argument("Must specify an output file (-o OUTFILE)\n");
- }
- if (infiles.empty()) {
- throw invalid_argument("No input files\n");
- }
- outfile.open(outfname, ios_base::binary | ios_base::out);
- }
-};
-
-struct FatbEntry
-{
- uint32_t start;
- uint32_t end;
-};
-
-struct FatbHeader
-{
- char magic[4];
- uint32_t chunk_size;
- uint16_t file_count;
- char padding[2];
- FatbHeader(uint16_t numfiles) :
- chunk_size(sizeof(FatbHeader) + sizeof(FatbEntry) * numfiles),
- file_count(numfiles)
- { strncpy(magic, "BTAF", 4); memset(padding, 0, 2); }
-};
-
-struct FntbDirHeader
-{
- uint32_t offset;
- uint16_t first_file;
- uint16_t id_or_count;
-};
-
-struct FntbHeader
-{
- char magic[4];
- uint32_t chunk_size;
- FntbHeader() :
- chunk_size(sizeof(FntbHeader) + sizeof(FntbDirHeader))
- { strncpy(magic, "BTNF", 4); }
- string set_names(Options*& opt, FntbDirHeader*& dirHeader) {
- throw runtime_error("not implemented\n");
-// typedef struct NitroFS {
-// bool is_dir;
-// vector<struct NitroFS> children;
-// } nitrofs_t;
-// map<string, nitrofs_t> fs;
-// fs[opt->nitro_root].is_dir = true;
-// string host_root;
-// for (auto pair : opt->infiles) {
-// string fname = pair.first;
-// fname = fname.replace(0, opt->host_root.size(), opt->nitro_root);
-// int pos;
-// while ((pos = fname.find("//")) != fname.npos) {
-// fname = fname.replace(pos, 2, 1, '/');
-// }
-// }
-// return nullptr;
- }
-};
-
-#define FNT_END 0
-#define FNT_LENGTH_MASK 0x7F
-#define FNT_IS_DIR_MASK 0x80
-
-struct FimgHeader
-{
- char magic[4];
- uint32_t chunk_size;
- FimgHeader(size_t image_size) :
- chunk_size(sizeof(FimgHeader) + image_size)
- { strncpy(magic, "GMIF", 4); }
-};
-
-struct NarcHeader
-{
- char magic[4]; // NARC
- uint16_t bom;
- uint16_t version;
- uint32_t size;
- uint16_t chunk_size;
- uint16_t num_chunks;
- NarcHeader(uint16_t numfiles, size_t image_size) :
- bom(0xFFFE),
- version(0x0100),
- size(sizeof(NarcHeader) + sizeof(FatbHeader) + sizeof(FatbEntry) * numfiles + sizeof(FntbHeader) + sizeof(FntbDirHeader) + sizeof(FimgHeader) + image_size),
- chunk_size(sizeof(NarcHeader)),
- num_chunks(3)
- { strncpy(magic, "NARC", 4); }
-};
-
-int main(int argc, char ** argv)
-{
- try
- {
- auto opt = new Options(argc, argv);
- size_t nfiles = opt->infiles.size();
- auto fatb = new FatbEntry[nfiles];
- FatbEntry *entry = fatb;
- for (auto pair : opt->infiles)
- {
- entry->start = (entry == fatb ? 0 : (entry[-1].end + 3) & ~3);
- entry->end = entry->start + pair.second.size();
- entry++;
- }
- size_t image_size = (fatb[nfiles - 1].end + 3) & ~3;
- FntbHeader fntbHeader;
- auto fntbDir = new FntbDirHeader;
- *fntbDir = (FntbDirHeader) {4, 0, 1}; // dummy
- stringstream fnt;
- if (opt->keep_names)
- {
- fntbHeader.set_names(opt, fntbDir);
- }
- NarcHeader narcHeader(nfiles, (fatb[nfiles - 1].end + 3) & ~3);
- FatbHeader fatbHeader(nfiles);
- FimgHeader fimgHeader((fatb[nfiles - 1].end + 3) & ~3);
- opt->outfile.write((char *) &narcHeader, sizeof(NarcHeader));
- opt->outfile.write((char *) &fatbHeader, sizeof(FatbHeader));
- opt->outfile.write((char *) fatb, sizeof(FatbEntry) * nfiles);
- opt->outfile.write((char *) &fntbHeader, sizeof(FntbHeader));
- opt->outfile.write((char *) fntbDir, sizeof(FntbDirHeader));
- opt->outfile.write((char *) &fimgHeader, sizeof(FimgHeader));
- char padding[4];
- memset(padding, opt->padval, 4);
- for (auto pair : opt->infiles)
- {
- stringstream uss(pair.second);
- opt->outfile.write(pair.second.c_str(), pair.second.size());
- opt->outfile.write(padding, (4 - pair.second.size()) & 3);
- }
- opt->outfile.close();
- return 0;
- } catch (invalid_argument e) {
- cerr << "Invalid exception: " << e.what() << endl;
- return 1;
- } catch (runtime_error e) {
- cerr << "Runtime error: " << e.what() << endl;
- return 2;
- } catch (exception e) {
- cerr << "unhandled exception caught" << endl;
- return 3;
- }
-}