This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Okteta structure definition skip bytes

Tags: None
(comma "," separated)
newmarch
Registered Member
Posts
2
Karma
0
Hi,

I'm trying to write an Okteta structure definition in javascript. I think I understand how it works for basic types, but I've run into a problem. I want to know if its possible to skip over a certain number of bytes, or alternatively skip to an offset in the data that I have calculated based on data that I've read. The data I'm trying to interpret is has structures which identify their size and type, so I want to read one of those in, then skip to the next one and repeat the process, ending up with an array of nodes with interpreted size and type.

Thanks for any help,

Dan
arichardson
Registered Member
Posts
3
Karma
0
OS
Hi,

What you want to do is possible, however it is not as nice as I would like it to be.
There is a
Code: Select all
pointer
data type that allows a structure to be somewhere else in the file. The problem is that it only supports absolute positions in the file.
It can be created using the following syntax:
Code: Select all
pointer(uint32(), struct(...))
. The defined struct will be read from the file offset equal to the value of this uint32.
In the future I want to allow any javascript function to be used as the pointer value, however this is not yet possible.
An example structure would be:

Code: Select all
function init() {
    var pointerTarget = struct({ val1: uint8(), val2: uint8() });
    pointerTarget.name = "my_struct"; // have to give it a name otherwise it will be displayed as <pointer target> in the view
    var obj = struct({
        foo: uint32(),
        bar: uint32(),
        somePointer: pointer(/* type of pointer */ uint16(), /* object that is pointed at */ pointerTarget)
    });
    return obj;
}


If you want to just skip a few bytes I am afraid currently the only solution is to add a dummy array as padding in between. This could look like this:

Code: Select all
function init() {
    var pointerTarget = struct({ val1: uint8(), val2: uint8() });
    pointerTarget.name = "my_struct"; // have to give it a name otherwise it will be displayed as <pointer target> in the view
    // fake a relative pointer by using a dynamically sized array
    var obj = struct({
        foo: uint32(),
        bar: uint32(),
        fakePointer: uint16(),
        padding: array(uint8(), function(rootObj) {
            // do some length calculation here, in this case just add halve of the pointer value to the address
            return rootObj.fakePointer.value / 2;
        }),
        target: pointerTarget
    });
    return obj;
}


I hope this helps you with your structure. I must admit that the documentation for the structures tool is quite lacking, I never really got around to writing it. The currently most up to date documentation can be found here: http://userbase.kde.org/Okteta/Writing_structure_definitions. I will try to rewrite it in order to be more useful, but currently I am very busy with KDE Frameworks 5 development.

Regards
Alex
newmarch
Registered Member
Posts
2
Karma
0
Hi Alex,

Thanks for the detailed reply! The pointer type does sound very close to what I want, but I guess I really need the ability to substitute a java function for the uint32() as the target location as you said. I don't think inserting dummy bytes will work in my case as the sizes of the structures I'm working with can be large. It's a shame, but I can still achieve some good value out of my structure decoder without the pointer related part.

Thanks for all the hard work, and also for the link to the new documentation, I hadn't found that before.

Regards,
Dan


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]