Pregunta

I try to get the header of a dmg file. I saw this a link that help me a lot . So I make a c++ program to get all this thing .

here it is :

struct Header
{
int img;
char tag[4];
short size;
short   version;
int format;
int flag;
int num_block;
int offset;
int length;
int offset_comment;
int length_comment;
int offset_creator;
int length_creator;
char space[16];
};

void read_header(const char *path)
{
 Header head;

 FILE *file =  fopen(path, "rb");


 fread(&head, 1, sizeof(Header), file);

 printf(" size short = %d\n", sizeof( short ));
 printf(" size int = %d\n", sizeof( int ));
 printf(" size struct = %d\n", sizeof( Header ));

 printf("img %d\n", head.img);
 printf("tag %s\n", head.tag);  
 printf("size %d\n", head.size);
 printf("version %d\n", head.version);
 printf("format %d\n", head.format);
 printf("flag %d\n", head.flag);
 printf("num_block %d\n", head.num_block);
 printf("offset %d\n", head.offset);
 printf("length %d\n", head.length);
 printf("offset_comment %d\n", head.offset_comment);
 printf("length_comment %d\n", head.length_comment);
 printf("offset_creator %d\n", head.offset_creator);
 printf("length_creator %d\n", head.length_creator);

 }

And I get this :

size short = 2
size int = 4
size struct = 64
img 152133
tag 
size 0
version 0
format 0
flag 0
num_block 0
offset 0
length 0
offset_comment 0
length_comment 0
offset_creator 0
length_creator 0

I don't know why all my value are null except the first one . My dmg files is good and i can open it. Is anybody know why i get null value ?

¿Fue útil?

Solución

Firstly, when you encounter errors like this, it's best to actually look at the data in it's raw form to determine if the problem is with your code, or with the data that it is trying to interpret.

In this case, your problem is that the file format is different - when you create a .dmg file using disk utility it does not look even slightly like the format that is specified in the wikipedia page.

in other words - your code is fine, the issue is with the data that it is processing.

It turns out that the 'header' for a .dmg file is actually stored at the end of the file - tools such as dmg2img - http://vu1tur.eu.org/tools/ can be used to parse the data appropriately; plus it contains a better definition of the header than the one you're using

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top