1

I am trying to read through a .dat file looking for the particular HEX signature 00 02 00 using C. when it finds the signature it needs to copy 744 bytes from the start of the signature to a buffer.

I have tried various ways including reading into a buffer and using (strtol/strtok/fscanf) and i am running out of options.

Here is an example of the dat file and here is what the 00 02 00 looks like in ASCII .☻.

AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB 00 02 00 AB AB AB AB AB 00 00 00 00 00 00 00 00 00 00 00

any help or suggestions would be highly appriciated.

2 Answers2

0

It's a bit unclear if the contents of the file is really binary, or the hexadecimal representation of binary data. Both are possible, ".dat" is very general.

Regardless, I would load the entire file into memory, then:

  • If binary: use memmem() to look for "\x00 \x02 \x00". This function is a GNU extension: you'll need to write it if you don't have it.
  • If text: use strstr() to look for "00 02 00" (three bytes).
unwind
  • 391,730
  • 64
  • 469
  • 606
0

OK, let me try again.

Given your example for a 1028 bytes buffer, you are searching for a 3 bytes pattern '00 02 00' from a much longer byte stream. So, how would I deal with it?

Use this to implement a circular buffer: How do you implement a circular buffer in C?

Refill the buffer when data running low, such as 2 bytes left for searching for a 3 bytes pattern. Keep at it until done. Be careful with all boundary cases but shouldn't be too hard.

Community
  • 1
  • 1
Stone
  • 13
  • 3