1212
1313namespace pl ::lib::libstd::mem {
1414
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) {
1616 u32 occurrences = 0 ;
1717 const u64 bufferSize = ctx->getDataSize ();
1818
@@ -25,7 +25,7 @@ namespace pl::lib::libstd::mem {
2525 std::vector<u8 > bytes (std::max (sequence.size (), size_t (4 * 1024 )) + sequence.size (), 0x00 );
2626 for (u64 offset = offsetFrom; offset < offsetTo; offset += bytes.size () - sequence.size ()) {
2727 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 );
2929 ctx->handleAbort ();
3030
3131 for (u64 i = 0 ; i < bytes.size () - sequence.size (); i += 1 ) {
@@ -74,9 +74,9 @@ namespace pl::lib::libstd::mem {
7474
7575 /* find_sequence_in_range(occurrence_index, start_offset, end_offset, bytes...) */
7676 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 ();
8080
8181 std::vector<u8 > sequence;
8282 for (u32 i = 3 ; i < params.size (); i++) {
@@ -88,59 +88,62 @@ namespace pl::lib::libstd::mem {
8888 sequence.push_back (u8 (byte));
8989 }
9090
91- return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, sequence).value_or (-1 );
91+ return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, ctx-> getSectionId (), sequence).value_or (-1 );
9292 });
9393
9494 /* find_string_in_range(occurrence_index, start_offset, end_offset, string) */
9595 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 );
100100
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 );
102102 });
103103
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;
109110
110111 if (size < 1 || size > 16 )
111112 err::E0012 .throwError (fmt::format (" Read size {} is out of range." , size), " Try a value between 1 and 16." );
112113
113114 u128 result = 0 ;
114- ctx->readData (address, &result, size, ptrn::Pattern::MainSectionId );
115+ ctx->readData (address, &result, size, section );
115116 result = hlp::changeEndianess (result, size, endian);
116117
117118 return result;
118119 });
119120
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;
125127
126128 if (size < 1 || size > 16 )
127129 err::E0012 .throwError (fmt::format (" Read size {} is out of range." , size), " Try a value between 1 and 16." );
128130
129131
130132 i128 value = 0 ;
131- ctx->readData (address, &value, size, ptrn::Pattern::MainSectionId );
133+ ctx->readData (address, &value, size, section );
132134 value = hlp::changeEndianess (value, size, endian);
133135
134136 return hlp::signExtend (size * 8 , value);
135137 });
136138
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;
141144
142145 std::string result (size, ' \x00 ' );
143- ctx->readData (address, result.data (), size, ptrn::Pattern::MainSectionId );
146+ ctx->readData (address, result.data (), size, section );
144147
145148 return result;
146149 });
0 commit comments