/****************************************************************************
* 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 .
****************************************************************************/
#include "NodeEntries.h"
#include
std::optional> NodeEntries::DeserializeImpl(const std::vector &pData,
uint32_t pOffset,
const std::optional> &pParent,
uint32_t pEntryNumber,
const std::shared_ptr &pSectionEntries,
const std::shared_ptr &pStringTable,
const SectionBlockSize &pBlockSize) {
auto nodeEntry = NodeEntry::AutoDeserialize(pData, pOffset, pParent, pEntryNumber, pSectionEntries, pStringTable, pBlockSize);
if (!nodeEntry.has_value()) {
DEBUG_FUNCTION_LINE("Failed to AutoDeserialize NodeEntry");
return {};
}
auto asDirEntry = std::dynamic_pointer_cast(nodeEntry.value());
if (asDirEntry != nullptr) {
uint32_t curEntryNumber = asDirEntry->entryNumber + 1;
while (curEntryNumber < asDirEntry->lastEntryNumber) {
auto entry = NodeEntries::DeserializeImpl(pData, pOffset + (curEntryNumber - asDirEntry->entryNumber) * NodeEntry::LENGTH,
asDirEntry, curEntryNumber, pSectionEntries, pStringTable, pBlockSize);
if (!entry.has_value()) {
DEBUG_FUNCTION_LINE("Failed to Deserialize child of NodeEntry");
return {};
}
asDirEntry->addChild(entry.value());
auto childAsDir = std::dynamic_pointer_cast(entry.value());
if (childAsDir != nullptr) {
curEntryNumber = childAsDir->lastEntryNumber;
} else {
curEntryNumber++;
}
}
}
return nodeEntry;
}
std::optional>
NodeEntries::make_unique(const std::vector &data, uint32_t offset, const std::shared_ptr &pSectionEntries, const std::shared_ptr &pStringTable,
const SectionBlockSize &blockSize) {
auto rootEntry = NodeEntries::DeserializeImpl(data, offset, std::nullopt, 0, pSectionEntries, pStringTable, blockSize);
if (!rootEntry.has_value()) {
DEBUG_FUNCTION_LINE("DeserializeImpl for root entry has failed");
return {};
}
auto rootEntryCasted = std::dynamic_pointer_cast(rootEntry.value());
if (rootEntryCasted != nullptr) {
return std::unique_ptr(new NodeEntries(rootEntryCasted));
}
DEBUG_FUNCTION_LINE("Failed to parse Root");
return {};
}
std::shared_ptr NodeEntries::getRootEntry() const {
return rootEntry;
}
NodeEntries::NodeEntries(const std::shared_ptr &pEntry) {
rootEntry = pEntry;
}