3

Sorry, I have almost 20 years without touching C/C++.

I Would like translate this code to Java 8, I found this Reference Manual. I was reading that in this link.

You can read...

Binary Templates are easy to write and look similar to C/C++ structs except they may contain if, for, or while statements as well as functions or complex expressions.

Powerful enough to parse almost any binary file format.

Can be set to run automatically when files are opened.

Templates may be shared and a list of templates for download is available in our Template Repository.

I begins and in the line 2063 I found (sorry, I could only translate, sad 4 lines :(! ).

APFS apfs(0);

I was reading C - function inside struct but looks, so many different!

Reviewing I jump to the line 1994, it looks like a structure with a function call, instead of having attributes!!!.

typedef struct APFS(uint64 apfs_offset) {
  DefineApfsContainer(apfs_offset);  
} ;

Inmediatly you can see in the line 1998

void DefineApfsContainer(uint64 apfs_offset) {
  Apfs_Offset = apfs_offset;
  FSeek(Apfs_Offset);
  if (!CheckApfsAndSetBlockSize()) {
    Printf("\nError, starting point not an APFS container superblock. Set the 'Apfs_Offset' variable to correct value!");
    Exit(1);
  }
  
  obj csb;//container super block in block zero
  
  
  SeekBlock(csb.body.xp_desc_base);
  CheckpointDesc cp(csb.body.xp_desc_blocks);
  
  //SeekBlock(csb.body.xp_data_base);
  //obj checkpoint_data_nx[csb.body.xp_data_blocks] <optimize=false>;
  
  // Checkpoint processing
  local uint i = 0;
  local uint64 max_xid = 0;
  local uint64 max_xid_block_pos = 0;
  SeekBlock(csb.body.xp_desc_base);
  local uint64 base_pos = FTell();
  local uint64 pos = 0;
  
  for (i=0; i< csb.body.xp_desc_blocks; ++i) { // assuming cont. blocks
    if (cp.checkpoint_desc_nx[i].hdr.type == obj_type_container_superblock) {
      if (cp.checkpoint_desc_nx[i].hdr.xid >= max_xid) {
        // validate it 
        pos = base_pos + (i * Block_Size);
        if (fletcher64(pos, Block_Size) == 0) {
          max_xid = cp.checkpoint_desc_nx[i].hdr.xid;
          max_xid_block_pos = pos;
        }
      }
    }
  }
  if (max_xid > csb.hdr.xid)
    Printf("\nFound newer xid=%Lu @ offset 0x%Lu. Using this Container superblock.", max_xid, pos);
  
  FSeek(pos);
  obj valid_csb;
  BookmarkVolumes(valid_csb);
  if (valid_csb.body.efi_jumpstart) {
    SeekBlock(valid_csb.body.efi_jumpstart);
    obj efi_info;
  }
  /*if (valid_csb.body.keylocker_block_count) {
    SeekBlock(valid_csb.body.keylocker_paddr);
    obj keybag; // This is encrypted!
  }*/
}

I need to understand what does that (I don't mean the code, but the way of coding), in order to translate. How should I understand this code (a function as a structure or a structure from a function)?

The nugget of subject. How should I translate it to Java?

joseluisbz
  • 1,491
  • 1
  • 36
  • 58

1 Answers1

1

This code doesn’t quite translate to Java because it’s code-generating code, ie. code that some tool uses to generate the actual C-like code (in a nutshell), or code for a bespoke virtual machine that then implements the data extraction and data packing. The idiomatic way to do this in Java without writing a stand-alone tool would be by attributes and similar mechanisms, introspection, and runtime code generation, repurposing Java syntax to express the same idea. You could attempt to do manual translation - that would grow the code a whole lot. The binary template concept is very powerful - leads to very concise code that would otherwise be tedious to implement.

I’d say that a manual translation will be more work than writing the translator, because you’ll be debugging all the manual mistakes till the cows come home. The syntax is limited enough so that a parser and translator written in Java will be about the same number of lines as a “straightforward” Java implementation of the binary template.

You may look at the tool this template is written for and see if the tool offers a way of translating the template. If the tool is open source then you already have a parser :)

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313