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 ) {
@@ -59,24 +59,31 @@ namespace pl::lib::libstd::mem {
5959 {
6060
6161 /* base_address() */
62- runtime.addFunction (nsStdMem, " base_address" , FunctionParameterCount::none (), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
63- wolv::util::unused (params);
62+ runtime.addFunction (nsStdMem, " base_address" , FunctionParameterCount::between (0 , 1 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
63+ auto section = params.size () == 1 ? params[0 ].toUnsigned () : ptrn::Pattern::MainSectionId;
64+ if (section == 0xFFFF'FFFF'FFFF'FFFF )
65+ section = ctx->getUserSectionId ();
66+
67+ if (section != ptrn::Pattern::MainSectionId)
68+ return 0 ;
6469
6570 return u128 (ctx->getDataBaseAddress ());
6671 });
6772
6873 /* size() */
69- runtime.addFunction (nsStdMem, " size" , FunctionParameterCount::none (), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
70- wolv::util::unused (params);
74+ runtime.addFunction (nsStdMem, " size" , FunctionParameterCount::between (0 , 1 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
75+ auto section = params.size () == 1 ? params[0 ].toUnsigned () : ptrn::Pattern::MainSectionId;
76+ if (section == 0xFFFF'FFFF'FFFF'FFFF )
77+ section = ctx->getUserSectionId ();
7178
72- return u128 (ctx->getDataSize ( ));
79+ return u128 (ctx->getSectionSize (section ));
7380 });
7481
7582 /* find_sequence_in_range(occurrence_index, start_offset, end_offset, bytes...) */
7683 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 () );
84+ const u64 occurrenceIndex = params[0 ].toUnsigned ();
85+ const u64 offsetFrom = params[1 ].toUnsigned ();
86+ const u64 offsetTo = params[2 ].toUnsigned ();
8087
8188 std::vector<u8 > sequence;
8289 for (u32 i = 3 ; i < params.size (); i++) {
@@ -88,59 +95,68 @@ namespace pl::lib::libstd::mem {
8895 sequence.push_back (u8 (byte));
8996 }
9097
91- return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, sequence).value_or (-1 );
98+ return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, ctx-> getUserSectionId (), sequence).value_or (-1 );
9299 });
93100
94101 /* find_string_in_range(occurrence_index, start_offset, end_offset, string) */
95102 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 );
103+ const u64 occurrenceIndex = params[0 ].toUnsigned ();
104+ const u64 offsetFrom = params[1 ].toUnsigned ();
105+ const u64 offsetTo = params[2 ].toUnsigned ();
106+ const auto string = params[3 ].toString (false );
100107
101- return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, std::vector<u8 >(string.data (), string.data () + string.size ())).value_or (-1 );
108+ return findSequence (ctx, occurrenceIndex, offsetFrom, offsetTo, ctx-> getUserSectionId (), std::vector<u8 >(string.data (), string.data () + string.size ())).value_or (-1 );
102109 });
103110
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 ();
111+ /* read_unsigned(address, size, endian, section) */
112+ runtime.addFunction (nsStdMem, " read_unsigned" , FunctionParameterCount::between (3 , 4 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
113+ const u64 address = params[0 ].toUnsigned ();
114+ const size_t size = params[1 ].toSigned ();
115+ const types::Endian endian = params[2 ].toUnsigned ();
116+ u64 section = params.size () == 4 ? params[3 ].toUnsigned () : ptrn::Pattern::MainSectionId;
117+ if (section == 0xFFFF'FFFF'FFFF'FFFF )
118+ section = ctx->getUserSectionId ();
109119
110120 if (size < 1 || size > 16 )
111121 err::E0012 .throwError (fmt::format (" Read size {} is out of range." , size), " Try a value between 1 and 16." );
112122
113123 u128 result = 0 ;
114- ctx->readData (address, &result, size, ptrn::Pattern::MainSectionId );
124+ ctx->readData (address, &result, size, section );
115125 result = hlp::changeEndianess (result, size, endian);
116126
117127 return result;
118128 });
119129
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 ();
130+ /* read_signed(address, size, endian, section) */
131+ runtime.addFunction (nsStdMem, " read_signed" , FunctionParameterCount::between (3 , 4 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
132+ const u64 address = params[0 ].toUnsigned ();
133+ const size_t size = params[1 ].toSigned ();
134+ const types::Endian endian = params[2 ].toUnsigned ();
135+ u64 section = params.size () == 4 ? params[3 ].toUnsigned () : ptrn::Pattern::MainSectionId;
136+ if (section == 0xFFFF'FFFF'FFFF'FFFF )
137+ section = ctx->getUserSectionId ();
125138
126139 if (size < 1 || size > 16 )
127140 err::E0012 .throwError (fmt::format (" Read size {} is out of range." , size), " Try a value between 1 and 16." );
128141
129142
130143 i128 value = 0 ;
131- ctx->readData (address, &value, size, ptrn::Pattern::MainSectionId );
144+ ctx->readData (address, &value, size, section );
132145 value = hlp::changeEndianess (value, size, endian);
133146
134147 return hlp::signExtend (size * 8 , value);
135148 });
136149
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 ());
150+ /* read_string(address, size, endian, section) */
151+ runtime.addFunction (nsStdMem, " read_string" , FunctionParameterCount::between (2 , 3 ), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
152+ const u64 address = params[0 ].toUnsigned ();
153+ const size_t size = params[1 ].toSigned ();
154+ u64 section = params.size () == 3 ? params[2 ].toUnsigned () : ptrn::Pattern::MainSectionId;
155+ if (section == 0xFFFF'FFFF'FFFF'FFFF )
156+ section = ctx->getUserSectionId ();
141157
142158 std::string result (size, ' \x00 ' );
143- ctx->readData (address, result.data (), size, ptrn::Pattern::MainSectionId );
159+ ctx->readData (address, result.data (), size, section );
144160
145161 return result;
146162 });
0 commit comments