2021-10-09 00:58:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2016-2021 Maschell
|
|
|
|
*
|
|
|
|
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
#include <WUD/entities/FST/sectionentry/SectionEntries.h>
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <WUD/entities/FST/stringtable/StringTable.h>
|
|
|
|
#include <utils/blocksize/SectionBlockSize.h>
|
2021-10-11 22:02:35 +02:00
|
|
|
|
2021-10-09 00:58:55 +02:00
|
|
|
#include "DirectoryEntry.h"
|
2022-07-26 08:16:27 +02:00
|
|
|
#include "NodeEntry.h"
|
2021-10-09 00:58:55 +02:00
|
|
|
#include "RootEntry.h"
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
2021-10-09 00:58:55 +02:00
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::optional<std::shared_ptr<NodeEntry>>
|
|
|
|
NodeEntry::AutoDeserialize(const std::vector<uint8_t> &data,
|
|
|
|
uint32_t offset,
|
|
|
|
const std::optional<std::shared_ptr<DirectoryEntry>> &pParent,
|
|
|
|
uint32_t eEntryNumber,
|
|
|
|
const std::shared_ptr<SectionEntries> §ionEntries,
|
|
|
|
const std::shared_ptr<StringTable> &stringTable,
|
|
|
|
const SectionBlockSize &blockSize) {
|
|
|
|
if (offset + NodeEntry::LENGTH >= data.size()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::array<uint8_t, NodeEntry::LENGTH> curEntryData{};
|
|
|
|
std::copy_n(data.begin() + (int) offset, NodeEntry::LENGTH, curEntryData.begin());
|
2021-10-09 00:58:55 +02:00
|
|
|
|
|
|
|
NodeEntryParam param{};
|
2022-07-26 08:16:27 +02:00
|
|
|
param.permission = ((uint16_t *) &curEntryData[12])[0];
|
2021-10-09 00:58:55 +02:00
|
|
|
param.sectionNumber = ((uint16_t *) &curEntryData[14])[0];
|
2022-07-26 08:16:27 +02:00
|
|
|
param.entryNumber = eEntryNumber;
|
|
|
|
param.parent = pParent;
|
|
|
|
param.type = curEntryData[0];
|
|
|
|
param.uint24 = ((uint32_t *) &curEntryData[0])[0] & 0x00FFFFFF;
|
2021-10-09 00:58:55 +02:00
|
|
|
|
|
|
|
if ((param.type & ENTRY_TYPE_Directory) == ENTRY_TYPE_Directory && param.uint24 == 0) { // Root
|
2021-10-11 22:02:35 +02:00
|
|
|
auto res = RootEntry::parseData(curEntryData, param, sectionEntries, stringTable);
|
|
|
|
if (!res.has_value()) {
|
2022-07-26 09:24:06 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to parse node");
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return res;
|
2021-10-09 00:58:55 +02:00
|
|
|
} else if ((param.type & ENTRY_TYPE_Directory) == ENTRY_TYPE_Directory) {
|
2021-10-11 22:02:35 +02:00
|
|
|
auto res = DirectoryEntry::parseData(curEntryData, param, sectionEntries, stringTable);
|
|
|
|
if (!res.has_value()) {
|
2022-07-26 09:24:06 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to parse node");
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto resAsNodeEntry = std::dynamic_pointer_cast<NodeEntry>(res.value());
|
|
|
|
if (resAsNodeEntry == nullptr) {
|
2022-07-26 09:24:06 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to cast to NodeEntry");
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return resAsNodeEntry;
|
2021-10-09 00:58:55 +02:00
|
|
|
} else if ((param.type & ENTRY_TYPE_File) == ENTRY_TYPE_File) {
|
2021-10-11 22:02:35 +02:00
|
|
|
auto res = FileEntry::parseData(curEntryData, param, sectionEntries, stringTable, blockSize);
|
|
|
|
|
|
|
|
if (!res.has_value()) {
|
2022-07-26 09:24:06 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to parse node");
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto resAsNodeEntry = std::dynamic_pointer_cast<NodeEntry>(res.value());
|
|
|
|
if (resAsNodeEntry == nullptr) {
|
2022-07-26 09:24:06 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to cast to NodeEntry");
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return resAsNodeEntry;
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 09:24:06 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("FST Unknown Node Type");
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::string NodeEntry::getName() {
|
|
|
|
auto res = nameString->toString();
|
|
|
|
if (res.has_value()) {
|
|
|
|
return res.value();
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
2021-10-11 22:02:35 +02:00
|
|
|
return "[ERROR]";
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::string NodeEntry::getFullPathInternal() {
|
|
|
|
if (parent.has_value()) {
|
|
|
|
return parent.value()->getFullPathInternal().append("/").append(getName());
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
return getName();
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::string NodeEntry::getFullPath() {
|
2021-10-09 00:58:55 +02:00
|
|
|
return getFullPathInternal();
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::string NodeEntry::getPath() {
|
|
|
|
if (parent.has_value()) {
|
|
|
|
return parent.value()->getFullPath().append("/");
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
return "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NodeEntry::isDirectory() const {
|
|
|
|
return (entryType & ENTRY_TYPE_Directory) == ENTRY_TYPE_Directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool NodeEntry::isFile() const {
|
|
|
|
return (entryType & ENTRY_TYPE_File) == ENTRY_TYPE_File;
|
2021-10-11 22:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeEntry::NodeEntry(const uint16_t pPermission,
|
|
|
|
std::shared_ptr<StringEntry> pNameString,
|
|
|
|
std::shared_ptr<SectionEntry> pSectionEntry,
|
|
|
|
std::optional<std::shared_ptr<DirectoryEntry>> pParent,
|
2022-07-26 08:16:27 +02:00
|
|
|
const uint8_t pType, const uint32_t pEntryNumber) : permission(pPermission),
|
|
|
|
nameString(std::move(pNameString)),
|
|
|
|
sectionEntry(std::move(pSectionEntry)),
|
|
|
|
parent(std::move(pParent)),
|
|
|
|
entryType(pType),
|
|
|
|
entryNumber(pEntryNumber) {
|
2021-10-11 22:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NodeEntry::printPathRecursive() {
|
|
|
|
DEBUG_FUNCTION_LINE("%s", getFullPath().c_str());
|
|
|
|
}
|