diff options
-rw-r--r-- | tools/knarc/Makefile | 4 | ||||
-rw-r--r-- | tools/knarc/Narc.cpp | 945 | ||||
-rw-r--r-- | tools/knarc/Narc.h | 86 | ||||
-rw-r--r-- | tools/knarc/Source.cpp | 220 | ||||
-rw-r--r-- | tools/knarc/fnmatch.c | 219 | ||||
-rw-r--r-- | tools/knarc/fnmatch.h | 70 |
6 files changed, 917 insertions, 627 deletions
diff --git a/tools/knarc/Makefile b/tools/knarc/Makefile index 62af834f..ef75789b 100644 --- a/tools/knarc/Makefile +++ b/tools/knarc/Makefile @@ -1,7 +1,7 @@ CXXFLAGS := -std=c++17 -O2 -Wall -Wno-switch -lstdc++fs -SRCS := Source.cpp Narc.cpp -HEADERS := Narc.h +SRCS := Source.cpp Narc.cpp fnmatch.c +HEADERS := Narc.h fnmatch.h .PHONY: all clean diff --git a/tools/knarc/Narc.cpp b/tools/knarc/Narc.cpp index 2faed98e..7e698571 100644 --- a/tools/knarc/Narc.cpp +++ b/tools/knarc/Narc.cpp @@ -6,6 +6,7 @@ #include <fstream> #include <iomanip> #include <ios> +#include <iostream> #include <map> #include <memory> #include <regex> @@ -13,10 +14,10 @@ #include <stack> #include <string> #include <vector> -#include <iostream> -#include <fnmatch.h> -#if __GNUC__ <= 7 +#include "fnmatch.h" + +#if (__GNUC__ <= 7) && !defined _MSC_VER #include <experimental/filesystem> namespace fs = std::experimental::filesystem; #else @@ -31,82 +32,82 @@ extern bool pack_no_fnt; 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)); - } - } + 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(); + ifs.close(); - error = e; + error = e; - return false; + return false; } bool Narc::Cleanup(ofstream& ofs, const NarcError& e) { - ofs.close(); + ofs.close(); - error = e; + error = e; - return false; + return false; } vector<fs::directory_entry> Narc::OrderedDirectoryIterator(const fs::path& path, bool recursive) const { - vector<fs::directory_entry> v; - - for (const auto& de : fs::directory_iterator(path)) - { - v.push_back(de); - } - - sort(v.begin(), v.end(), [](const fs::directory_entry& a, const fs::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 (is_directory(v[i])) - { - vector<fs::directory_entry> temp = OrderedDirectoryIterator(v[i], true); - - v.insert(v.end(), temp.begin(), temp.end()); - } - } - } - - return v; + vector<fs::directory_entry> v; + + for (const auto& de : fs::directory_iterator(path)) + { + v.push_back(de); + } + + sort(v.begin(), v.end(), [](const fs::directory_entry& a, const fs::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 (is_directory(v[i])) + { + vector<fs::directory_entry> temp = OrderedDirectoryIterator(v[i], true); + + v.insert(v.end(), temp.begin(), temp.end()); + } + } + } + + return v; } NarcError Narc::GetError() const { - return error; + return error; } class WildcardVector : public vector<string> { @@ -140,451 +141,451 @@ public: bool Narc::Pack(const fs::path& fileName, const fs::path& directory) { - ofstream ofs(fileName, ios::binary); - - if (!ofs.good()) { return Cleanup(ofs, NarcError::InvalidOutputFile); } - - vector<FileAllocationTableEntry> fatEntries; - uint16_t directoryCounter = 1; - - WildcardVector ignore_patterns(directory / ".knarcignore"); - ignore_patterns.push_back(".*ignore"); - ignore_patterns.push_back(".*keep"); - WildcardVector keep_patterns(directory / ".knarckeep"); - - for (const auto& de : OrderedDirectoryIterator(directory, true)) - { - if (is_directory(de)) - { - ++directoryCounter; - } - else if (keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename())) - { - if (debug) { - cerr << "DEBUG: adding file " << de.path() << endl; - } - 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>(file_size(de)); - } - } - - FileAllocationTable fat - { - .Id = 0x46415442, // BTAF - .ChunkSize = static_cast<uint32_t>(sizeof(FileAllocationTable) + ((uint32_t)fatEntries.size() * sizeof(FileAllocationTableEntry))), - .FileCount = static_cast<uint16_t>(fatEntries.size()), - .Reserved = 0x0 - }; - - map<fs::path, string> subTables; - vector<fs::path> paths; - - directoryCounter = 0; - - for (const auto& de : OrderedDirectoryIterator(directory, true)) - { - if (!subTables.count(de.path().parent_path()) && (keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename()))) - { - subTables.insert({ de.path().parent_path(), "" }); - paths.push_back(de.path().parent_path()); - } - - if (is_directory(de)) - { - ++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 if (keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename())) - { - 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 (!pack_no_fnt) - { - fntEntries.push_back( - { - .Offset = static_cast<uint32_t>((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 = static_cast<uint32_t>(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, // BTNF - .ChunkSize = static_cast<uint32_t>(sizeof(FileNameTable) + (fntEntries.size() * sizeof(FileNameTableEntry))) - }; - - if (!pack_no_fnt) - { - 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, // GMIF - .ChunkSize = static_cast<uint32_t>(sizeof(FileImages) + fatEntries.back().End) - }; - - if ((fi.ChunkSize % 4) != 0) - { - fi.ChunkSize += 4 - (fi.ChunkSize % 4); - } - - Header header - { - .Id = 0x4352414E, // NARC - .ByteOrderMark = 0xFFFE, - .Version = 0x100, - .FileSize = static_cast<uint32_t>(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 (!pack_no_fnt) - { - 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 (is_directory(de)) - { - continue; - } - - if (!(keep_patterns.matches(de.path().filename()) || !ignore_patterns.matches(de.path().filename()))) + ofstream ofs(fileName, ios::binary); + + if (!ofs.good()) { return Cleanup(ofs, NarcError::InvalidOutputFile); } + + vector<FileAllocationTableEntry> fatEntries; + uint16_t directoryCounter = 1; + + WildcardVector ignore_patterns(directory / ".knarcignore"); + ignore_patterns.push_back(".*ignore"); + ignore_patterns.push_back(".*keep"); + WildcardVector keep_patterns(directory / ".knarckeep"); + + for (const auto& de : OrderedDirectoryIterator(directory, true)) + { + if (is_directory(de)) + { + ++directoryCounter; + } + else if (keep_patterns.matches(de.path().filename().string()) || !ignore_patterns.matches(de.path().filename().string())) + { + if (debug) { + cerr << "DEBUG: adding file " << de.path() << endl; + } + 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>(file_size(de)); + } + } + + FileAllocationTable fat + { + .Id = 0x46415442, // BTAF + .ChunkSize = static_cast<uint32_t>(sizeof(FileAllocationTable) + ((uint32_t)fatEntries.size() * sizeof(FileAllocationTableEntry))), + .FileCount = static_cast<uint16_t>(fatEntries.size()), + .Reserved = 0x0 + }; + + map<fs::path, string> subTables; + vector<fs::path> paths; + + directoryCounter = 0; + + for (const auto& de : OrderedDirectoryIterator(directory, true)) + { + if (!subTables.count(de.path().parent_path()) && (keep_patterns.matches(de.path().filename().string()) || !ignore_patterns.matches(de.path().filename().string()))) + { + subTables.insert({ de.path().parent_path(), "" }); + paths.push_back(de.path().parent_path()); + } + + if (is_directory(de)) + { + ++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 if (keep_patterns.matches(de.path().filename().string()) || !ignore_patterns.matches(de.path().filename().string())) + { + 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 (!pack_no_fnt) + { + fntEntries.push_back( + { + .Offset = static_cast<uint32_t>((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 = static_cast<uint32_t>(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, // BTNF + .ChunkSize = static_cast<uint32_t>(sizeof(FileNameTable) + (fntEntries.size() * sizeof(FileNameTableEntry))) + }; + + if (!pack_no_fnt) + { + 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, // GMIF + .ChunkSize = static_cast<uint32_t>(sizeof(FileImages) + fatEntries.back().End) + }; + + if ((fi.ChunkSize % 4) != 0) + { + fi.ChunkSize += 4 - (fi.ChunkSize % 4); + } + + Header header + { + .Id = 0x4352414E, // NARC + .ByteOrderMark = 0xFFFE, + .Version = 0x100, + .FileSize = static_cast<uint32_t>(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 (!pack_no_fnt) + { + for (const auto& path : paths) { - continue; + ofs << subTables[path]; } + } - ifstream ifs(de.path(), ios::binary | ios::ate); + AlignDword(ofs, 0xFF); - if (!ifs.good()) - { - ifs.close(); + ofs.write(reinterpret_cast<char*>(&fi), sizeof(FileImages)); - return Cleanup(ofs, NarcError::InvalidInputFile); - } + for (const auto& de : OrderedDirectoryIterator(directory, true)) + { + if (is_directory(de)) + { + continue; + } - streampos length = ifs.tellg(); - unique_ptr<char[]> buffer = make_unique<char[]>(static_cast<unsigned int>(length)); + if (!(keep_patterns.matches(de.path().filename().string()) || !ignore_patterns.matches(de.path().filename().string()))) + { + continue; + } - ifs.seekg(0); - ifs.read(buffer.get(), length); - ifs.close(); + ifstream ifs(de.path(), ios::binary | ios::ate); + + if (!ifs.good()) + { + ifs.close(); + + return Cleanup(ofs, NarcError::InvalidInputFile); + } - ofs.write(buffer.get(), length); + streampos length = ifs.tellg(); + unique_ptr<char[]> buffer = make_unique<char[]>(static_cast<unsigned int>(length)); - AlignDword(ofs, 0xFF); - } + ifs.seekg(0); + ifs.read(buffer.get(), length); + ifs.close(); - ofs.close(); + ofs.write(buffer.get(), length); - return error == NarcError::None ? true : false; + AlignDword(ofs, 0xFF); + } + + ofs.close(); + + return error == NarcError::None ? true : false; } bool Narc::Unpack(const fs::path& fileName, const fs::path& directory) { - ifstream ifs(fileName, ios::binary); + 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); - if (!ifs.good()) { return Cleanup(ifs, NarcError::InvalidInputFile); } + uint16_t fileId = 0x0000; - Header header; - ifs.read(reinterpret_cast<char*>(&header), sizeof(Header)); + 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)); - 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); } + fileNames.get()[fntEntries[i].FirstFileId + fileId] += c; + } - FileAllocationTable fat; - ifs.read(reinterpret_cast<char*>(&fat), sizeof(FileAllocationTable)); + ++fileId; + } + else if (length == 0x80) + { + // Reserved + } + else if (length <= 0xFF) + { + length -= 0x80; + string directoryName; - if (fat.Id != 0x46415442) { return Cleanup(ifs, NarcError::InvalidFileAllocationTableId); } - if (fat.Reserved != 0x0) { return Cleanup(ifs, NarcError::InvalidFileAllocationTableReserved); } + for (uint8_t j = 0; j < length; ++j) + { + uint8_t c; + ifs.read(reinterpret_cast<char*>(&c), sizeof(uint8_t)); - unique_ptr<FileAllocationTableEntry[]> fatEntries = make_unique<FileAllocationTableEntry[]>(fat.FileCount); + directoryName += c; + } - for (uint16_t i = 0; i < fat.FileCount; ++i) - { - ifs.read(reinterpret_cast<char*>(&fatEntries.get()[i]), sizeof(FileAllocationTableEntry)); - } + uint16_t directoryId; + ifs.read(reinterpret_cast<char*>(&directoryId), sizeof(uint16_t)); - FileNameTable fnt; - vector<FileNameTableEntry> FileNameTableEntries; - ifs.read(reinterpret_cast<char*>(&fnt), sizeof(FileNameTable)); + fileNames.get()[directoryId] = directoryName; + } + else + { + return Cleanup(ifs, NarcError::InvalidFileNameTableEntryId); + } + } + } - if (fnt.Id != 0x464E5442) { return Cleanup(ifs, NarcError::InvalidFileNameTableId); } + if ((ifs.tellg() % 4) != 0) + { + ifs.seekg(4 - (ifs.tellg() % 4), ios::cur); + } - vector<FileNameTableEntry> fntEntries; + FileImages fi; + ifs.read(reinterpret_cast<char*>(&fi), sizeof(FileImages)); - do - { - fntEntries.push_back(FileNameTableEntry()); + if (fi.Id != 0x46494D47) { return Cleanup(ifs, NarcError::InvalidFileImagesId); } - 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)); + fs::create_directory(directory); + fs::current_path(directory); - 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; - } + 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); - ++fileId; - } - else if (length == 0x80) - { - // Reserved - } - else if (length <= 0xFF) - { - length -= 0x80; - string directoryName; + 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); - for (uint8_t j = 0; j < length; ++j) - { - uint8_t c; - ifs.read(reinterpret_cast<char*>(&c), sizeof(uint8_t)); + ostringstream oss; + oss << fileName.stem().string() << "_" << setfill('0') << setw(8) << i << ".bin"; - directoryName += c; - } + ofstream ofs(oss.str(), ios::binary); + + if (!ofs.good()) + { + ofs.close(); - uint16_t directoryId; - ifs.read(reinterpret_cast<char*>(&directoryId), sizeof(uint16_t)); + return Cleanup(ifs, NarcError::InvalidOutputFile); + } + + ofs.write(buffer.get(), fatEntries.get()[i].End - fatEntries.get()[i].Start); + ofs.close(); + } + } + else + { + fs::path absolutePath = fs::absolute(fs::current_path()); + + for (size_t i = 0; i < fntEntries.size(); ++i) + { + fs::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()) + { + fs::create_directory(directories.top()); + fs::current_path(directories.top()); + } + + if (fntEntries[i].Utility >= 0xF000) + { + fs::create_directory(fileNames.get()[0xF000 + i]); + fs::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); + } + } + } + } - fileNames.get()[directoryId] = directoryName; - } - else - { - return Cleanup(ifs, NarcError::InvalidFileNameTableEntryId); - } - } - } + ifs.close(); - 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); } - - fs::create_directory(directory); - fs::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 - { - fs::path absolutePath = fs::absolute(fs::current_path()); - - for (size_t i = 0; i < fntEntries.size(); ++i) - { - fs::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()) - { - fs::create_directory(directories.top()); - fs::current_path(directories.top()); - } - - if (fntEntries[i].Utility >= 0xF000) - { - fs::create_directory(fileNames.get()[0xF000 + i]); - fs::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; + return error == NarcError::None ? true : false; } diff --git a/tools/knarc/Narc.h b/tools/knarc/Narc.h index 4516d2d5..7d9cc117 100644 --- a/tools/knarc/Narc.h +++ b/tools/knarc/Narc.h @@ -5,7 +5,7 @@ #include <string> #include <vector> -#if __GNUC__ <= 7 +#if (__GNUC__ <= 7) && !defined _MSC_VER #include <experimental/filesystem> namespace fs = std::experimental::filesystem; #else @@ -15,79 +15,79 @@ namespace fs = std::filesystem; enum class NarcError { - None, - InvalidInputFile, - InvalidHeaderId, - InvalidByteOrderMark, - InvalidVersion, - InvalidHeaderSize, - InvalidChunkCount, - InvalidFileAllocationTableId, - InvalidFileAllocationTableReserved, - InvalidFileNameTableId, - InvalidFileNameTableEntryId, - InvalidFileImagesId, - InvalidOutputFile + 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; + 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; + uint32_t Id; + uint32_t ChunkSize; + uint16_t FileCount; + uint16_t Reserved; }; struct FileAllocationTableEntry { - uint32_t Start; - uint32_t End; + uint32_t Start; + uint32_t End; }; struct FileNameTable { - uint32_t Id; - uint32_t ChunkSize; + uint32_t Id; + uint32_t ChunkSize; }; struct FileNameTableEntry { - uint32_t Offset; - uint16_t FirstFileId; - uint16_t Utility; + uint32_t Offset; + uint16_t FirstFileId; + uint16_t Utility; }; struct FileImages { - uint32_t Id; - uint32_t ChunkSize; + uint32_t Id; + uint32_t ChunkSize; }; class Narc { - public: - NarcError GetError() const; +public: + NarcError GetError() const; - bool Pack(const fs::path& fileName, const fs::path& directory); - bool Unpack(const fs::path& fileName, const fs::path& directory); + bool Pack(const fs::path& fileName, const fs::path& directory); + bool Unpack(const fs::path& fileName, const fs::path& directory); - private: - NarcError error = NarcError::None; +private: + NarcError error = NarcError::None; - void AlignDword(std::ofstream& ofs, uint8_t paddingChar); + void AlignDword(std::ofstream& ofs, uint8_t paddingChar); - bool Cleanup(std::ifstream& ifs, const NarcError& e); - bool Cleanup(std::ofstream& ofs, const NarcError& e); + bool Cleanup(std::ifstream& ifs, const NarcError& e); + bool Cleanup(std::ofstream& ofs, const NarcError& e); - std::vector<fs::directory_entry> OrderedDirectoryIterator(const fs::path& path, bool recursive) const; + std::vector<fs::directory_entry> OrderedDirectoryIterator(const fs::path& path, bool recursive) const; }; diff --git a/tools/knarc/Source.cpp b/tools/knarc/Source.cpp index 21020891..d9a5cf83 100644 --- a/tools/knarc/Source.cpp +++ b/tools/knarc/Source.cpp @@ -11,23 +11,23 @@ bool pack_no_fnt = true; 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; - } + 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; + } } static inline void usage() { @@ -44,102 +44,102 @@ static inline void usage() { int main(int argc, char* argv[]) { - 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; - } - - if (!directory.empty()) { - cerr << "ERROR: Multiple directories 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; - } + 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; + } + + if (!directory.empty()) { + cerr << "ERROR: Multiple directories 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; + } + + if (!fileName.empty()) { + cerr << "ERROR: Multiple files specified" << 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; + } if (!fileName.empty()) { cerr << "ERROR: Multiple files specified" << 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; - } - - if (!fileName.empty()) { - cerr << "ERROR: Multiple files specified" << endl; - return 1; - } - fileName = argv[++i]; - } else if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--debug")) { - debug = true; - } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - usage(); - return 0; - } - else if (!strcmp(argv[i], "-n")) { - pack_no_fnt = false; - } - else { - usage(); - cerr << "ERROR: Unrecognized argument: " << argv[i] << endl; - return 1; - } - } - - if (fileName.empty()) { - cerr << "ERROR: Missing -u or -p" << endl; - return 1; - } - if (directory.empty()) { - cerr << "ERROR: Missing -d" << endl; - return 1; - } - - 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; + fileName = argv[++i]; + } else if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--debug")) { + debug = true; + } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + usage(); + return 0; + } + else if (!strcmp(argv[i], "-n")) { + pack_no_fnt = false; + } + else { + usage(); + cerr << "ERROR: Unrecognized argument: " << argv[i] << endl; + return 1; + } + } + + if (fileName.empty()) { + cerr << "ERROR: Missing -u or -p" << endl; + return 1; + } + if (directory.empty()) { + cerr << "ERROR: Missing -d" << endl; + return 1; + } + + 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/knarc/fnmatch.c b/tools/knarc/fnmatch.c new file mode 100644 index 00000000..6ba0a871 --- /dev/null +++ b/tools/knarc/fnmatch.c @@ -0,0 +1,219 @@ +/* Copyright (C) 1991-2018 Free Software Foundation, Inc. + +NOTE: This source is derived from an old version taken from the GNU C +Library (glibc). + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, 51 Franklin Street - Fifth Floor, +Boston, MA 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +#if defined (CONFIG_BROKETS) +/* We use <config.h> instead of "config.h" so that a compilation + using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h + (which it would do because it found this file in $srcdir). */ +#include <config.h> +#else +#include "config.h" +#endif +#endif + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +/* This code to undef const added in libiberty. */ +#ifndef __STDC__ +/* This is a separate conditional since some stdc systems + reject `defined (const)'. */ +#ifndef const +#define const +#endif +#endif + +#include "fnmatch.h" + +#include <ctype.h> + + /* Comment out all this code if we are using the GNU C Library, and are not + actually compiling the library itself. This code is part of the GNU C + Library, but also included in many other GNU distributions. Compiling + and linking in this code is a waste when using the GNU C library + (especially if it is a shared library). Rather than having every GNU + program understand `configure --with-gnu-libc' and omit the object files, + it is simpler to just do this in the source for each such file. */ + +#if defined (_LIBC) || !defined (__GNU_LIBRARY__) + + +#if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS) +extern int errno; +#endif + +/* Match STRING against the filename pattern PATTERN, returning zero if + it matches, nonzero if not. */ +int +fnmatch(const char* pattern, const char* string, int flags) +{ + register const char* p = pattern, * n = string; + register unsigned char c; + +#define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c)) + + while ((c = *p++) != '\0') + { + c = FOLD(c); + + switch (c) + { + case '?': + if (*n == '\0') + return FNM_NOMATCH; + else if ((flags & FNM_FILE_NAME) && *n == '/') + return FNM_NOMATCH; + else if ((flags & FNM_PERIOD) && *n == '.' && + (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + return FNM_NOMATCH; + break; + + case '\\': + if (!(flags & FNM_NOESCAPE)) + { + c = *p++; + c = FOLD(c); + } + if (FOLD((unsigned char)*n) != c) + return FNM_NOMATCH; + break; + + case '*': + if ((flags & FNM_PERIOD) && *n == '.' && + (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + return FNM_NOMATCH; + + for (c = *p++; c == '?' || c == '*'; c = *p++, ++n) + if (((flags & FNM_FILE_NAME) && *n == '/') || + (c == '?' && *n == '\0')) + return FNM_NOMATCH; + + if (c == '\0') + return 0; + + { + unsigned char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c; + c1 = FOLD(c1); + for (--p; *n != '\0'; ++n) + if ((c == '[' || FOLD((unsigned char)*n) == c1) && + fnmatch(p, n, flags & ~FNM_PERIOD) == 0) + return 0; + return FNM_NOMATCH; + } + + case '[': + { + /* Nonzero if the sense of the character class is inverted. */ + register int negate; + + if (*n == '\0') + return FNM_NOMATCH; + + if ((flags & FNM_PERIOD) && *n == '.' && + (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + return FNM_NOMATCH; + + negate = (*p == '!' || *p == '^'); + if (negate) + ++p; + + c = *p++; + for (;;) + { + register unsigned char cstart = c, cend = c; + + if (!(flags & FNM_NOESCAPE) && c == '\\') + cstart = cend = *p++; + + cstart = cend = FOLD(cstart); + + if (c == '\0') + /* [ (unterminated) loses. */ + return FNM_NOMATCH; + + c = *p++; + c = FOLD(c); + + if ((flags & FNM_FILE_NAME) && c == '/') + /* [/] can never match. */ + return FNM_NOMATCH; + + if (c == '-' && *p != ']') + { + cend = *p++; + if (!(flags & FNM_NOESCAPE) && cend == '\\') + cend = *p++; + if (cend == '\0') + return FNM_NOMATCH; + cend = FOLD(cend); + + c = *p++; + } + + if (FOLD((unsigned char)*n) >= cstart + && FOLD((unsigned char)*n) <= cend) + goto matched; + + if (c == ']') + break; + } + if (!negate) + return FNM_NOMATCH; + break; + + matched:; + /* Skip the rest of the [...] that already matched. */ + while (c != ']') + { + if (c == '\0') + /* [... (unterminated) loses. */ + return FNM_NOMATCH; + + c = *p++; + if (!(flags & FNM_NOESCAPE) && c == '\\') + /* XXX 1003.2d11 is unclear if this is right. */ + ++p; + } + if (negate) + return FNM_NOMATCH; + } + break; + + default: + if (c != FOLD((unsigned char)*n)) + return FNM_NOMATCH; + } + + ++n; + } + + if (*n == '\0') + return 0; + + if ((flags & FNM_LEADING_DIR) && *n == '/') + /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ + return 0; + + return FNM_NOMATCH; +} + +#endif /* _LIBC or not __GNU_LIBRARY__. */ diff --git a/tools/knarc/fnmatch.h b/tools/knarc/fnmatch.h new file mode 100644 index 00000000..508c59a7 --- /dev/null +++ b/tools/knarc/fnmatch.h @@ -0,0 +1,70 @@ +/* Copyright (C) 1991-2020 Free Software Foundation, Inc. + +NOTE: The canonical source of this file is maintained with the GNU C Library. +Bugs can be reported to bug-glibc@prep.ai.mit.edu. + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, 51 Franklin Street - Fifth Floor, +Boston, MA 02110-1301, USA. */ + +#ifndef _FNMATCH_H + +#define _FNMATCH_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined (__cplusplus) || (defined (__STDC__) && __STDC__) +#undef __P +#define __P(args) args +#else /* Not C++ or ANSI C. */ +#undef __P +#define __P(args) () + /* We can get away without defining `const' here only because in this file + it is used only inside the prototype for `fnmatch', which is elided in + non-ANSI C where `const' is problematical. */ +#endif /* C++ or ANSI C. */ + + + /* We #undef these before defining them because some losing systems + (HP-UX A.08.07 for example) define these in <unistd.h>. */ +#undef FNM_PATHNAME +#undef FNM_NOESCAPE +#undef FNM_PERIOD + + /* Bits set in the FLAGS argument to `fnmatch'. */ +#define FNM_PATHNAME (1 << 0) /* No wildcard can ever match `/'. */ +#define FNM_NOESCAPE (1 << 1) /* Backslashes don't quote special chars. */ +#define FNM_PERIOD (1 << 2) /* Leading `.' is matched only explicitly. */ + +#if !defined (_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 2 || defined (_GNU_SOURCE) +#define FNM_FILE_NAME FNM_PATHNAME /* Preferred GNU name. */ +#define FNM_LEADING_DIR (1 << 3) /* Ignore `/...' after a match. */ +#define FNM_CASEFOLD (1 << 4) /* Compare without regard to case. */ +#endif + +/* Value returned by `fnmatch' if STRING does not match PATTERN. */ +#define FNM_NOMATCH 1 + +/* Match STRING against the filename pattern PATTERN, + returning zero if it matches, FNM_NOMATCH if not. */ + extern int fnmatch __P((const char* __pattern, const char* __string, + int __flags)); + +#ifdef __cplusplus +} +#endif + +#endif /* fnmatch.h */ |