Answer the question
In order to leave comments, you need to log in
How to understand binary files?
Hello.
It became interesting how you can parse binary files. Everywhere I go into the documentation for them there is some kind of garbage (on the .3ds screen):
I'm just wondering what all these numbers mean (0x4D4D, 0x0002, etc.), and not "open the file in binary mode and read" . Can you please explain intelligibly what all these numbers mean, and not in abstruse words, I know you bydlocoders) 00) And preferably with some simple example on the pluses
Answer the question
In order to leave comments, you need to log in
As the wikipedia page for the same .3ds format says - "The first two bytes of the chunk are its ID". Each format has its own binary representation with its own chunks. A typical example would be Windows executables - PE and for Linux - ELF. For ELF files, there is a good specification that explains what chunks are, what their headers are, what fields the headers have, what the size of the chunk (if it is not variable).
There are also printer spool files - they also have their own headers.
From the general, what can be taken out:
In the binary representation, information is not stored in its naked form, but is most often wrapped in blocks with headers. Headers can have identifiers (like the numbers you asked about), block size, and other ancillary information that depends on the header specification.
Promised links to binary specs:
ELF - Executable and Linkable format
EMFSPL
EMF
PE
#include <stdio.h>
#include <windows.h>
#define RTN_OK 0
#define RTN_FAILURE 1
const size_t expected_count = 1;
char* read_file_name(int argc, char** argv);
int main(int argc, char** argv)
{
// Parsing command line args
char* file_name = read_file_name(argc, argv);
// Opening our executable
FILE* portable_executable = fopen(file_name, "r");
// Reading dos header
IMAGE_DOS_HEADER dos_header_buffer;
fread(&dos_header_buffer, sizeof(IMAGE_DOS_HEADER), expected_count, portable_executable);
// Setting offset for reading PE header
fseek(portable_executable, (long)dos_header_buffer.e_lfanew, SEEK_SET)
// Reading signature field from PE header
DWORD signature = 0;
fread(&signature, sizeof(DWORD), expected_count, portable_executable);
// Reading file header
IMAGE_FILE_HEADER file_header_buffer;
fread(&file_header_buffer, sizeof(IMAGE_FILE_HEADER), expected_count, portable_executable);
// Reading optinal header
IMAGE_OPTIONAL_HEADER optional_header_buffer;
fread(&optional_header_buffer, file_header_buffer.SizeOfOptionalHeader, expected_count, portable_executable);
// Get info about all the sections
for (int i = 0; i < file_header_buffer.NumberOfSections; ++i)
{
// Reading each section header
IMAGE_SECTION_HEADER section_header_buffer;
fread(§ion_header_buffer, sizeof(IMAGE_SECTION_HEADER), expected_count, portable_executable);
printf("Section name: %7s, Raw data size: %10d, Pointer to raw data: %10d\n",
section_header_buffer.Name,
section_header_buffer.SizeOfRawData,
section_header_buffer.PointerToRawData);
}
fclose(portable_executable);
}
char* read_file_name(int argc, char** argv)
{
if (argc != 2)
{
return NULL;
}
return argv[1];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question