2021-08-29 14:18:45 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2021-09-25 16:24:08 +02:00
|
|
|
#include <hex/helpers/net.hpp>
|
2021-10-07 14:49:49 +02:00
|
|
|
#include <hex/helpers/file.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <hex/pattern_language/token.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/pattern_language/log_console.hpp>
|
|
|
|
#include <hex/pattern_language/evaluator.hpp>
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <hex/pattern_language/pattern_data.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <fmt/args.h>
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
2021-11-04 20:32:34 +01:00
|
|
|
std::string format(pl::Evaluator *ctx, auto params) {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto format = pl::Token::literalToString(params[0], true);
|
|
|
|
std::string message;
|
|
|
|
|
|
|
|
fmt::dynamic_format_arg_store<fmt::format_context> formatArgs;
|
|
|
|
|
|
|
|
for (u32 i = 1; i < params.size(); i++) {
|
|
|
|
auto ¶m = params[i];
|
|
|
|
|
|
|
|
std::visit(overloaded {
|
|
|
|
[&](pl::PatternData* value) {
|
2021-11-04 20:32:34 +01:00
|
|
|
formatArgs.push_back(value->toString(ctx->getProvider()));
|
2021-09-21 21:29:18 +02:00
|
|
|
},
|
|
|
|
[&](auto &&value) {
|
|
|
|
formatArgs.push_back(value);
|
|
|
|
}
|
|
|
|
}, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return fmt::vformat(format, formatArgs);
|
|
|
|
} catch (fmt::format_error &error) {
|
|
|
|
hex::pl::LogConsole::abortEvaluation(hex::format("format error: {}", error.what()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
void registerPatternLanguageFunctions() {
|
2021-09-08 15:18:24 +02:00
|
|
|
using namespace hex::pl;
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStd = { "std" };
|
2021-08-27 09:56:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* assert(condition, message) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto condition = Token::literalToBoolean(params[0]);
|
2021-12-10 11:55:27 +01:00
|
|
|
auto message = Token::literalToString(params[1], false);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (!condition)
|
|
|
|
LogConsole::abortEvaluation(hex::format("assertion failed \"{0}\"", message));
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return std::nullopt;
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
/* assert_warn(condition, message) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "assert_warn", 2, [](auto *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto condition = Token::literalToBoolean(params[0]);
|
2021-12-10 11:55:27 +01:00
|
|
|
auto message = Token::literalToString(params[1], false);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (!condition)
|
|
|
|
ctx->getConsole().log(LogConsole::Level::Warning, hex::format("assertion failed \"{0}\"", message));
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return std::nullopt;
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* print(format, args...) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "print", ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
ctx->getConsole().log(LogConsole::Level::Info, format(ctx, params));
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return std::nullopt;
|
|
|
|
});
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* format(format, args...) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "format", ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
return format(ctx, params);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-12-10 11:55:27 +01:00
|
|
|
/* env(name) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "env", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-12-10 11:55:27 +01:00
|
|
|
auto name = Token::literalToString(params[0], false);
|
|
|
|
|
2021-12-18 23:38:16 +01:00
|
|
|
auto env = ctx->getEnvVariable(name);
|
|
|
|
if (env)
|
|
|
|
return env;
|
|
|
|
else {
|
|
|
|
ctx->getConsole().log(LogConsole::Level::Warning, hex::format("environment variable '{}' does not exist", name));
|
|
|
|
return "";
|
|
|
|
}
|
2021-12-10 11:55:27 +01:00
|
|
|
});
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdMem = { "std", "mem" };
|
2021-09-09 23:18:10 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* align_to(alignment, value) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "align_to", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto alignment = Token::literalToUnsigned(params[0]);
|
|
|
|
auto value = Token::literalToUnsigned(params[1]);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
u128 remainder = value % alignment;
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return remainder != 0 ? value + (alignment - remainder) : value;
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
/* base_address() */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "base_address", ContentRegistry::PatternLanguage::NoParameters, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(ctx->getProvider()->getBaseAddress());
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* size() */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "size", ContentRegistry::PatternLanguage::NoParameters, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(ctx->getProvider()->getActualSize());
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-09-09 23:18:10 +02:00
|
|
|
|
|
|
|
/* find_sequence(occurrence_index, bytes...) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "find_sequence", ContentRegistry::PatternLanguage::MoreParametersThan | 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto occurrenceIndex = Token::literalToUnsigned(params[0]);
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
std::vector<u8> sequence;
|
|
|
|
for (u32 i = 1; i < params.size(); i++) {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto byte = Token::literalToUnsigned(params[i]);
|
|
|
|
|
|
|
|
if (byte > 0xFF)
|
|
|
|
LogConsole::abortEvaluation(hex::format("byte #{} value out of range: {} > 0xFF", i, u64(byte)));
|
|
|
|
|
|
|
|
sequence.push_back(u8(byte & 0xFF));
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> bytes(sequence.size(), 0x00);
|
|
|
|
u32 occurrences = 0;
|
2021-09-21 21:29:18 +02:00
|
|
|
for (u64 offset = 0; offset < ctx->getProvider()->getSize() - sequence.size(); offset++) {
|
|
|
|
ctx->getProvider()->read(offset, bytes.data(), bytes.size());
|
2021-09-09 23:18:10 +02:00
|
|
|
|
|
|
|
if (bytes == sequence) {
|
2021-09-21 21:29:18 +02:00
|
|
|
if (occurrences < occurrenceIndex) {
|
2021-09-09 23:18:10 +02:00
|
|
|
occurrences++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(offset);
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("failed to find sequence");
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* read_unsigned(address, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_unsigned", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (size > 16)
|
|
|
|
LogConsole::abortEvaluation("read size out of range");
|
|
|
|
|
|
|
|
u128 result = 0;
|
|
|
|
ctx->getProvider()->read(address, &result, size);
|
|
|
|
|
|
|
|
return result;
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* read_signed(address, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_signed", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (size > 16)
|
|
|
|
LogConsole::abortEvaluation("read size out of range");
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
i128 value;
|
2021-09-21 21:29:18 +02:00
|
|
|
ctx->getProvider()->read(address, &value, size);
|
|
|
|
return hex::signExtend(size * 8, value);
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
2021-09-21 23:29:30 +02:00
|
|
|
/* read_string(address, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_string", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 23:29:30 +02:00
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
std::string result(size, '\x00');
|
|
|
|
ctx->getProvider()->read(address, result.data(), size);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdString = { "std", "string" };
|
2021-08-27 09:56:20 +02:00
|
|
|
{
|
|
|
|
/* length(string) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "length", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(string.length());
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
/* at(string, index) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "at", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
|
|
|
auto index = Token::literalToSigned(params[1]);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-12-19 12:32:15 +01:00
|
|
|
#if defined(OS_MACOS)
|
|
|
|
const auto signIndex = index >> (sizeof(index) * 8 - 1);
|
|
|
|
const auto absIndex = (index ^ signIndex) - signIndex;
|
|
|
|
#else
|
|
|
|
const auto absIndex = std::abs(index);
|
|
|
|
#endif
|
2021-10-09 23:07:58 +02:00
|
|
|
|
|
|
|
if (absIndex > string.length())
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("character index out of range");
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (index >= 0)
|
|
|
|
return char(string[index]);
|
|
|
|
else
|
|
|
|
return char(string[string.length() - -index]);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* substr(string, pos, count) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "substr", 3, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
|
|
|
auto pos = Token::literalToUnsigned(params[1]);
|
|
|
|
auto size = Token::literalToUnsigned(params[2]);
|
|
|
|
|
2021-09-25 00:04:40 +02:00
|
|
|
if (pos > string.length())
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("character index out of range");
|
2021-03-02 13:55:43 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return string.substr(pos, size);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-09-21 21:29:18 +02:00
|
|
|
|
2021-09-26 02:23:10 +02:00
|
|
|
/* parse_int(string, base) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "parse_int", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-26 02:23:10 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
|
|
|
auto base = Token::literalToUnsigned(params[1]);
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
return i128(std::strtoll(string.c_str(), nullptr, base));
|
2021-09-26 02:23:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* parse_float(string) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "parse_float", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-26 02:23:10 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
|
|
|
|
|
|
|
return double(std::strtod(string.c_str(), nullptr));
|
|
|
|
});
|
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
}
|
2021-09-25 16:24:08 +02:00
|
|
|
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdHttp = { "std", "http" };
|
2021-09-25 16:24:08 +02:00
|
|
|
{
|
|
|
|
/* get(url) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdHttp, "get", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-25 16:24:08 +02:00
|
|
|
const auto url = Token::literalToString(params[0], false);
|
|
|
|
|
|
|
|
hex::Net net;
|
|
|
|
return net.getString(url).get().body;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-09-25 16:24:08 +02:00
|
|
|
}
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdFile = { "std", "file" };
|
2021-10-07 14:49:49 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
static u32 fileCounter = 0;
|
|
|
|
static std::map<u32, File> openFiles;
|
|
|
|
|
|
|
|
/* open(path, mode) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "open", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto path = Token::literalToString(params[0], false);
|
|
|
|
const auto modeEnum = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
File::Mode mode;
|
|
|
|
switch (modeEnum) {
|
|
|
|
case 1: mode = File::Mode::Read; break;
|
|
|
|
case 2: mode = File::Mode::Write; break;
|
|
|
|
case 3: mode = File::Mode::Create; break;
|
|
|
|
default:
|
|
|
|
LogConsole::abortEvaluation("invalid file open mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file = File(path, mode);
|
|
|
|
|
|
|
|
if (!file.isValid())
|
|
|
|
LogConsole::abortEvaluation(hex::format("failed to open file {}", path));
|
|
|
|
|
|
|
|
fileCounter++;
|
|
|
|
openFiles.emplace(std::pair{ fileCounter, std::move(file) });
|
|
|
|
|
|
|
|
return u128(fileCounter);
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* close(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "close", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles.erase(file);
|
|
|
|
|
|
|
|
return { };
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* read(file, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "read", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
const auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
return openFiles[file].readString(size);
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* write(file, data) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "write", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
const auto data = Token::literalToString(params[1], true);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].write(data);
|
|
|
|
|
|
|
|
return { };
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* seek(file, offset) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "seek", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
const auto offset = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].seek(offset);
|
|
|
|
|
|
|
|
return { };
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* size(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "size", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
return u128(openFiles[file].getSize());
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* resize(file, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "resize", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
const auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].setSize(size);
|
|
|
|
|
|
|
|
return { };
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* flush(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "flush", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].flush();
|
|
|
|
|
|
|
|
return { };
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* remove(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "remove", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].remove();
|
|
|
|
|
|
|
|
return { };
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
}
|