Skip to content
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

My aim is not to write the exact code you want to end up using. My only intent is to show how to read an array of structs that have their addresses specified by a previously read array of offsets. That is only the part that you are writing fake code for presumably because you are unsure how ImHex can do that operation. My idea is that once that is understood, the other details for your particular pattern should no be that complicated.

\n

Here is the problem. Youwant to perform this operation

\n
SectionData data_sections[i] @ header.sections[ i].data_offset;
\n

for all values of i between 0 and header.num_sections as if it was being done in a loop. As explained, loops cannot be used inside structs and you need to create an array of structs to accomplish the equivalent work done in the loop the ImHex way.

\n

When I say array of structs I don't mean an array of SectionData structs. If you make an array of them you will only be able to place the first one and the rest will simply follow each other.
\nTo achieve the placing of each array element at the corresponding offset found in the array of offsets a totally new structure, call it whatever you want but I'll use DataStruct, must be created. Inside it you place one of the section array elements using the function shown above to get the corresponding offset. As the array is being created, the array_index() function returns the value of the current array index and since the two indices in the loop were the same hare you just use it directly. This way we place one structure at one address. How and where you get the offsets is not important for the explanation. Using parent is cleaner and preferable but when starting to write patterns there is a tendency to assign data to global variables because it makes things easier. Easier means less powerful too. Again this is not important for the current discussion. So this is what i would think is the explanation you seek.

\n
struct DataStruct {\n    SectionData data_sections @ header.sections[ std::core::array_index()].data_offset;\n};\nThis is not optional. You can't place the entire array at sets of addresses directly. You have to do it one at a time. You wrap it up by creating an array of Datastructs. The array obviously has to be the same size as the array of offsets. \n```cpp\n// Inside some struct that you need to place\nDataStruct ds[header.num_sections];\n...
\n

You don't have to necessarily see the DataStruct variable in your pattern. There are a number of ways like [[inline]] that can let you see exactly what you want in the pattern data window.

\n

To be honest, this idea of using offsets to place structs is almost never needed. All input files can be read from the beginning to the end once you learn whats located where. Also it is preferable to end up with 1 pattern that is placed at zero and reads the entire file. This makes modifying the pattern a lot easier and makes the code look cleaner. If you browse through the pattern library provided by imhex you will see a majority of patterns follow those principles as close as possible. At the very least that was my experience when I did that.

","upvoteCount":0,"url":"https://github.com/WerWolv/ImHex/discussions/1896#discussioncomment-10594507"}}}

Recursive Struct Operations #1896

Answered by paxcut
0xfbad asked this question in Get Help
Sep 9, 2024 · 1 comments · 8 replies
Discussion options

You must be logged in to vote

My aim is not to write the exact code you want to end up using. My only intent is to show how to read an array of structs that have their addresses specified by a previously read array of offsets. That is only the part that you are writing fake code for presumably because you are unsure how ImHex can do that operation. My idea is that once that is understood, the other details for your particular pattern should no be that complicated.

Here is the problem. Youwant to perform this operation

SectionData data_sections[i] @ header.sections[ i].data_offset;

for all values of i between 0 and header.num_sections as if it was being done in a loop. As explained, loops cannot be used inside structs …

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@0xfbad
Comment options

@paxcut
Comment options

@0xfbad
Comment options

@paxcut
Comment options

Answer selected by 0xfbad
@0xfbad
Comment options

@paxcut
Comment options

@0xfbad
Comment options

@paxcut
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants