12
12
13
13
namespace pl ::lib::libstd::mem {
14
14
15
- static std::optional<i128 > findSequence (::pl::core::Evaluator *ctx, u64 occurrenceIndex, u64 offsetFrom, u64 offsetTo, const std::vector<u8 > &sequence) {
15
+ static std::optional<i128 > findSequence (::pl::core::Evaluator *ctx, u64 occurrenceIndex, u64 offsetFrom, u64 offsetTo, u64 section, const std::vector<u8 > &sequence) {
16
16
u32 occurrences = 0 ;
17
17
const u64 bufferSize = ctx->getDataSize ();
18
18
@@ -25,7 +25,7 @@ namespace pl::lib::libstd::mem {
25
25
std::vector<u8 > bytes (std::max (sequence.size (), size_t (4 * 1024 )) + sequence.size (), 0x00 );
26
26
for (u64 offset = offsetFrom; offset < offsetTo; offset += bytes.size () - sequence.size ()) {
27
27
const auto bytesToRead = std::min<std::size_t >(bytes.size (), offsetTo - offset);
28
- ctx->readData (offset, bytes.data (), bytesToRead, ptrn::Pattern::MainSectionId );
28
+ ctx->readData (offset, bytes.data (), bytesToRead, section );
29
29
ctx->handleAbort ();
30
30
31
31
for (u64 i = 0 ; i < bytes.size () - sequence.size (); i += 1 ) {
@@ -74,9 +74,9 @@ namespace pl::lib::libstd::mem {
74
74
75
75
/* find_sequence_in_range(occurrence_index, start_offset, end_offset, bytes...) */
76
76
runtime.addFunction (nsStdMem, " find_sequence_in_range" , FunctionParameterCount::moreThan (3 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
77
- auto occurrenceIndex = u64 ( params[0 ].toUnsigned () );
78
- auto offsetFrom = u64 ( params[1 ].toUnsigned () );
79
- auto offsetTo = u64 ( params[2 ].toUnsigned () );
77
+ const u64 occurrenceIndex = params[0 ].toUnsigned ();
78
+ const u64 offsetFrom = params[1 ].toUnsigned ();
79
+ const u64 offsetTo = params[2 ].toUnsigned ();
80
80
81
81
std::vector<u8 > sequence;
82
82
for (u32 i = 3 ; i < params.size (); i++) {
@@ -88,59 +88,62 @@ namespace pl::lib::libstd::mem {
88
88
sequence.push_back (u8 (byte));
89
89
}
90
90
91
- return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, sequence).value_or (-1 );
91
+ return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, ctx-> getSectionId (), sequence).value_or (-1 );
92
92
});
93
93
94
94
/* find_string_in_range(occurrence_index, start_offset, end_offset, string) */
95
95
runtime.addFunction (nsStdMem, " find_string_in_range" , FunctionParameterCount::exactly (4 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
96
- auto occurrenceIndex = u64 ( params[0 ].toUnsigned () );
97
- auto offsetFrom = u64 ( params[1 ].toUnsigned () );
98
- auto offsetTo = u64 ( params[2 ].toUnsigned () );
99
- auto string = params[3 ].toString (false );
96
+ const u64 occurrenceIndex = params[0 ].toUnsigned ();
97
+ const u64 offsetFrom = params[1 ].toUnsigned ();
98
+ const u64 offsetTo = params[2 ].toUnsigned ();
99
+ const auto string = params[3 ].toString (false );
100
100
101
- return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, std::vector<u8 >(string.data (), string.data () + string.size ())).value_or (-1 );
101
+ return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, ctx-> getSectionId (), std::vector<u8 >(string.data (), string.data () + string.size ())).value_or (-1 );
102
102
});
103
103
104
- /* read_unsigned(address, size, endian) */
105
- runtime.addFunction (nsStdMem, " read_unsigned" , FunctionParameterCount::exactly (3 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
106
- auto address = u64 (params[0 ].toUnsigned ());
107
- auto size = size_t (params[1 ].toSigned ());
108
- types::Endian endian = params[2 ].toUnsigned ();
104
+ /* read_unsigned(address, size, endian, section) */
105
+ runtime.addFunction (nsStdMem, " read_unsigned" , FunctionParameterCount::between (3 , 4 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
106
+ const u64 address = params[0 ].toUnsigned ();
107
+ const size_t size = params[1 ].toSigned ();
108
+ const types::Endian endian = params[2 ].toUnsigned ();
109
+ const u64 section = params.size () == 4 ? params[3 ].toUnsigned () : ptrn::Pattern::MainSectionId;
109
110
110
111
if (size < 1 || size > 16 )
111
112
err::E0012 .throwError (fmt::format (" Read size {} is out of range." , size), " Try a value between 1 and 16." );
112
113
113
114
u128 result = 0 ;
114
- ctx->readData (address, &result, size, ptrn::Pattern::MainSectionId );
115
+ ctx->readData (address, &result, size, section );
115
116
result = hlp::changeEndianess (result, size, endian);
116
117
117
118
return result;
118
119
});
119
120
120
- /* read_signed(address, size, endian) */
121
- runtime.addFunction (nsStdMem, " read_signed" , FunctionParameterCount::exactly (3 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
122
- auto address = u64 (params[0 ].toUnsigned ());
123
- auto size = size_t (params[1 ].toSigned ());
124
- types::Endian endian = params[2 ].toUnsigned ();
121
+ /* read_signed(address, size, endian, section) */
122
+ runtime.addFunction (nsStdMem, " read_signed" , FunctionParameterCount::between (3 , 4 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
123
+ const u64 address = params[0 ].toUnsigned ();
124
+ const size_t size = params[1 ].toSigned ();
125
+ const types::Endian endian = params[2 ].toUnsigned ();
126
+ const u64 section = params.size () == 4 ? params[3 ].toUnsigned () : ptrn::Pattern::MainSectionId;
125
127
126
128
if (size < 1 || size > 16 )
127
129
err::E0012 .throwError (fmt::format (" Read size {} is out of range." , size), " Try a value between 1 and 16." );
128
130
129
131
130
132
i128 value = 0 ;
131
- ctx->readData (address, &value, size, ptrn::Pattern::MainSectionId );
133
+ ctx->readData (address, &value, size, section );
132
134
value = hlp::changeEndianess (value, size, endian);
133
135
134
136
return hlp::signExtend (size * 8 , value);
135
137
});
136
138
137
- /* read_string(address, size, endian) */
138
- runtime.addFunction (nsStdMem, " read_string" , FunctionParameterCount::exactly (2 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
139
- auto address = u64 (params[0 ].toUnsigned ());
140
- auto size = size_t (params[1 ].toUnsigned ());
139
+ /* read_string(address, size, endian, section) */
140
+ runtime.addFunction (nsStdMem, " read_string" , FunctionParameterCount::between (2 , 3 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
141
+ const u64 address = params[0 ].toUnsigned ();
142
+ const size_t size = params[1 ].toSigned ();
143
+ const u64 section = params.size () == 3 ? params[2 ].toUnsigned () : ptrn::Pattern::MainSectionId;
141
144
142
145
std::string result (size, ' \x00 ' );
143
- ctx->readData (address, result.data (), size, ptrn::Pattern::MainSectionId );
146
+ ctx->readData (address, result.data (), size, section );
144
147
145
148
return result;
146
149
});
0 commit comments