rawRead using a struct with variable leght

ade90036 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 5 09:04:28 PDT 2017


Hi everyone,

I'm trying out Dland, always been and have been a big fan. So to 
give it a good run i wanted to create is a java class parser, 
based on the spec released here. ( 
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html.)

The class file can be represented in the following "struct" like:

ClassFile {
     u4             magic;
     u2             minor_version;
     u2             major_version;
     u2             constant_pool_count;
     cp_info        constant_pool[constant_pool_count-1];
     u2             access_flags;
     u2             this_class;
     u2             super_class;
     u2             interfaces_count;
     u2             interfaces[interfaces_count];
     u2             fields_count;
     field_info     fields[fields_count];
     u2             methods_count;
     method_info    methods[methods_count];
     u2             attributes_count;
     attribute_info attributes[attributes_count];
}

where:
u4 == ubyte[4] --> integer
u2 == ubyte[2] --> short
u1 == ubyte[1] --> byte

I have the first 4 fields parsing, however i stumble upon an 
example where you can use a rawRead() with a struct, therefore 
representing the entire structure and then reading from rawRead() 
like

````d
ClassFile[1] classFileStruct;
f.rawRead(claddFileStruct);
````

Unfortunately the struct doesn't know at compile time what the 
size of the constant_pool array, or at-least was not able to 
specify it dynamically.

What is the best approach to go about parsing such structure?

Should i have structs to represent the blocks of fixed fields 
from the structure and then parsing each variable (length) 
structure manually?


Something like this?

````
ClassHeader[1] classHeader;
f.rawRead(classHeader);

CpInfo[] cpInfo = new CpInfo[classHeader.constant_pool_count];
f.rawRead(cpInfo);
`````

Really appreciate any knowledgeable suggestions.

Regards

Ade



More information about the Digitalmars-d-learn mailing list