ImHex icon indicating copy to clipboard operation
ImHex copied to clipboard

[Bug] hex::core::add_virtual_file does not accept char[]

Open rsp4jack opened this issue 1 year ago • 9 comments

Operating System

Linux

What's the issue you encountered?

import hex.core;
import std.mem;

char array[while(!std::mem::eof())] @ 0x0;

hex::core::add_virtual_file("/", array);
E: [ Stack Trace ]
E: /usr/share/imhex/includes/hex/core.pat:47:59
E: 47 | dd_virtual_file(path, pattern);
E:                                    ^
E: <Source Code>:6:39
E: 6 | hex::core::add_virtual_file("/", array);
E:                                           ^


E: runtime error: Cannot cast value to type 'pattern'

How can the issue be reproduced?

Pass a char[] into it

ImHex Version

1.36.0

ImHex Build Type

  • [ ] Nightly or built from sources

Installation type

AUR imhex-bin

Additional context?

Is char[] not a pattern? A u8[] works.

rsp4jack avatar Dec 27 '24 04:12 rsp4jack

Hey You're right, this is a bit confusing. The thing is, char arrays create a string pattern when they're created so they can show up as strings in the pattern data view. When passing those strings to functions, they automatically decay to actual string values and lose their pattern information such as the location where they were in your data. I believe this is sadly pretty hard to fix without breaking many other patterns in the process. I can however document this a bit better in the documentation.

WerWolv avatar Dec 27 '24 09:12 WerWolv

I believe this is sadly pretty hard to fix without breaking many other patterns in the process.

In that case, is it possible to come up with a patch solution, to pass char arrays as-is?

Or, is it possible to cast a char array to u8[] or something else?

rsp4jack avatar Dec 31 '24 12:12 rsp4jack

The easiest solution is to just not use the raw string arrays directly but to wrap them in a struct first.

import hex.core;
import std.mem;

struct String {
    char array[while(!std::mem::eof())];
};

String string @ 0x0;

hex::core::add_virtual_file("/", string); // This will work just fine

WerWolv avatar Dec 31 '24 13:12 WerWolv

This workaround works, but I am working with char arrays with different extent (including while() and even sections), therefore it becomes ugly and sometimes unusable.

Simply we had better have a better solution rather than it.

rsp4jack avatar Dec 31 '24 16:12 rsp4jack

How about this then?

import hex.core;
import std.mem;


struct Char {
    char ch;
};

Char string[while(!std::mem::eof())]@0;

hex::core::add_virtual_file("/", string); // This will work just fine

paxcut avatar Dec 31 '24 16:12 paxcut

@WerWolv ~Hey I just came up with an idea, I think that the second argument of the function should be auto, then it will accept a variety of data, just like hex::dec functions.~

Edit: hex::dec functions do not work with char[].

rsp4jack avatar Dec 31 '24 16:12 rsp4jack

from the docs:

fn add_virtual_file(str path, auto pattern);

paxcut avatar Dec 31 '24 17:12 paxcut

Yeah, the conversion happens when accessing the variable, not when passing it to the function. It might be possible to post-pone that decaying though, I'll need to see if I can do that without breaking existing patterns though. I'll look into it 👍

WerWolv avatar Dec 31 '24 17:12 WerWolv

A workaround:

import hex.core;
import std.mem;

char array[while(!std::mem::eof())] @ 0x0;

u8 pattern[sizeof(array)] @ addressof(array) in 0 [[hidden]];
hex::core::add_virtual_file("/", pattern);

in 0 means placing it in the main section, without it Imhex will try to place it in heap section when in a function.

rsp4jack avatar Jan 19 '25 01:01 rsp4jack