How can I recover binary data from the INTDATA file using the provided C structure?

Can anyone help me in solving this i am in analysis paralysis right now cannot wrap my head around the problem Programming Exercise NOTE: ALL INFORMATION REQUIRED TO COMPLETE THE TASK IS INCLUDED IN THIS README FILE. The INTDATA file contains an hex-ASCII dump of binary data, with some header information for each block. This file was produced by extracting the binary data from a data collection unit running an Intel CPU, and saving it in human-readable format. The task is to write a recovery program that operates on the hex-ASCII representation of the binary data and recovers the data correctly. Here is a 'c' structure definition which shows, in the data block, the positions of the items in the block: struct flashdata { uint16 filename_id; // number for file uint32 sequence_in_file; // file sequence number uint16 number_records; // recs in block uint16 data_length; // data length uint16 flags; // unused presently unsigned char compression_type; // unused presently unsigned char spare; // unused presently unsigned char data[2048-14]; // the data itself }; The blocks need to have the binary data extracted from them. Then check the block. If the filename_id is not 0002 then discard the block. If the block has obvious errors (data_length>2048-14) then discard. Otherwise use the data_length, number_records and data fields to extract the data records. Record structure: <length><flags><len2> <data.......>
attachment 0
9 Replies
RED HAT
RED HAT4mo ago
@electro_coco Start by writing a function to read the INTDATA file and convert the hex-ASCII data back into binary. This will give you the raw data blocks that match the flashdata structure, once you have the binary data, you’ll need to parse it into flashdata structures. For each block, check the filename_id. If it’s not 0002, discard the block, next, validate the block by checking data_length. If data_length is greater than 2048-14, discard the block, for valid blocks, extract the data records using the number_records and data_length fields. You’ll need to carefully parse the <length>, <flags>, <len2>, and <data> fields from the data array.
electro_coco
electro_coco4mo ago
thank you i was not sure that whether i should convert data back to binary
electro_coco
electro_coco4mo ago
how can i convert hex ascii to binary
RED HAT
RED HAT4mo ago
Use strtol in C to convert the hex-ASCII strings into binary. You can read each line of the file, convert the hex values, and store them in a buffer that represents your binary data block.
RED HAT
RED HAT4mo ago
Once you have the binary data, you can cast it to a flashdata struct. Make sure you handle the endianness correctly, as Intel CPUs are little-endian.
electro_coco
electro_coco4mo ago
thank you i knew that i have to cast it to flash data struct the binary data of each block but how to handle endianness
RED HAT
RED HAT4mo ago
For instance if you detect that the data is in big-endian format and needs to be converted to little-endian (or vice versa), you’ll have to perform byte swapping to convert a 16-bit or 32-bit value from big-endian to little-endian, using this approach
uint16_t swap_uint16(uint16_t val) {
return (val << 8) | (val >> 8);
}

uint32_t swap_uint32(uint32_t val) {
return ((val << 24) & 0xFF000000) |
((val << 8) & 0x00FF0000) |
((val >> 8) & 0x0000FF00) |
((val >> 24) & 0x000000FF);
}
uint16_t swap_uint16(uint16_t val) {
return (val << 8) | (val >> 8);
}

uint32_t swap_uint32(uint32_t val) {
return ((val << 24) & 0xFF000000) |
((val << 8) & 0x00FF0000) |
((val >> 8) & 0x0000FF00) |
((val >> 24) & 0x000000FF);
}
electro_coco
electro_coco4mo ago
so the complete binary data of file needs to be converting to big endian
Want results from more Discord servers?
Add your server