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/>.
|
|
|
|
****************************************************************************/
|
2022-07-26 08:16:27 +02:00
|
|
|
#include "StringTable.h"
|
2021-10-09 00:58:55 +02:00
|
|
|
#include <coreinit/debug.h>
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <cstring>
|
2021-10-11 22:02:35 +02:00
|
|
|
#include <utils/logger.h>
|
2021-10-09 00:58:55 +02:00
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
|
|
|
|
std::optional<std::shared_ptr<StringTable>> StringTable::make_shared(const std::vector<uint8_t> &data, uint32_t offset, uint32_t stringCount) {
|
|
|
|
if (offset >= data.size()) {
|
|
|
|
DEBUG_FUNCTION_LINE("Invalid offset for reading StringTable");
|
|
|
|
return {};
|
|
|
|
}
|
2022-07-26 08:16:27 +02:00
|
|
|
auto stringTable = std::shared_ptr<StringTable>(new StringTable());
|
2021-10-09 00:58:55 +02:00
|
|
|
uint32_t curOffset = 0;
|
|
|
|
uint32_t i;
|
2021-10-11 22:02:35 +02:00
|
|
|
for (i = 0; curOffset < data.size() && i < stringCount; ++curOffset) {
|
2021-10-09 00:58:55 +02:00
|
|
|
if (data[offset + curOffset] == (uint8_t) 0) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < stringCount) {
|
2021-10-11 22:02:35 +02:00
|
|
|
DEBUG_FUNCTION_LINE("stringtable is broken");
|
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t curLength = 0;
|
|
|
|
for (i = 0; i < stringCount; ++i) {
|
2022-07-26 08:16:27 +02:00
|
|
|
curOffset = offset + curLength;
|
2021-10-11 22:02:35 +02:00
|
|
|
stringTable->stringMap[curLength] = std::make_shared<StringEntry>(stringTable, curLength);
|
2022-07-26 08:16:27 +02:00
|
|
|
stringTable->strings[curLength] = (char *) &data[curOffset];
|
2021-10-09 00:58:55 +02:00
|
|
|
|
|
|
|
curLength += strlen((char *) &data[curOffset]) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringTable;
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::optional<std::string> StringTable::getByAddress(uint32_t address) {
|
|
|
|
if (strings.count(address) > 0) {
|
|
|
|
return strings[address];
|
|
|
|
}
|
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::optional<std::shared_ptr<StringEntry>> StringTable::getStringEntry(uint32_t address) {
|
|
|
|
if (stringMap.count(address) > 0) {
|
|
|
|
return stringMap[address];
|
|
|
|
}
|
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t StringTable::getSize() {
|
|
|
|
uint32_t capacity = 1; // root entry
|
2022-07-26 08:16:27 +02:00
|
|
|
for (auto &cur : strings) {
|
2021-10-09 00:58:55 +02:00
|
|
|
capacity += cur.second.length() + 1;
|
|
|
|
}
|
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::optional<std::shared_ptr<StringEntry>> StringTable::getEntry(std::string &str) {
|
2022-07-26 08:16:27 +02:00
|
|
|
for (auto &cur : strings) {
|
2021-10-09 00:58:55 +02:00
|
|
|
if (cur.second == str) {
|
|
|
|
return stringMap[cur.first];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|