Alright, I have a file opened, read into a buffer, and closed. I need to search the file for this string of bytes:

FDXXXXXX06YYYYYY

The FD is an assembly code to load something, but that's not important here. The XXXXXX and YYYYYY are values that are unknown and YYYYYY will need to be changed according to something else later. Now, I have the file loaded up as a binary, and can even easily do a hex dump of this file to a .txt file, so I know it's in the right format to look for bytes in Hex.

My problem is, I don't know how to search a file for a specific byte, and worse, I don't know how to look for a string with unknown gaps in the middle... probably an (if data in address for FD + 4 is 06) I need to search the entire file for every instance of this. If I can make this any more clear, please tell me. Thanks

~Mallos31


EDIT: I don't know if this site is specifically C++ or what, but this is a C++ program.

Well if you've got that far, then searching is just a loop over the range of bytes you have, and an if statement looking for the pattern in question.

Alright, so what statement do I use to search for a specific byte? I'm sure I can figure it out if I know at least what to use.

If I could get some sort of example code, that would be great.

Umm... okay. I knew that, but how do I say
if(byte == 0xFD && (address of FD) + 4 == 0x06)
??
How do I search for the byte, and how do I get the "address of FD" part to add 4 to?

Do you know about arrays?
Do you know how to write a loop to examine each element of the array?

You loaded the file into a buffer, which can be treated for the purposes of syntax as an array.

//ModelCombiner.cpp
//combines and/or replaces model display lists for Ocarina of Time or Majora's Mask

//*notes*
//FD is texture command if it's addrss +4 is an 06 byte.
//01 is a geometry list if it's address +4 is an 06 byte.
//16 blank bytes added to the end of file1 and file2 is appended to the end. 
//format of texture and geometry commands is "FD xx xx xx 06 yy yy yy" and "01 xx xx xx 06 yy yy yy"
//size of file1 needs to be added to the "yy yy yy" each time this pattern is found.
//*end notes*
#include <iostream.h>
#include <fstream.h>
#include <string>
using namespace std; 
int main()
{
        ifstream FILE1;
        ifstream FILE2;
        unsigned char *buffer;
        unsigned char *buffer2;
        int FILE1_size;
		int FILE2_size;
        string file1;
        string file2;
        char* hexdata;
       
        cout << "Please enter the name of the first file" << endl;
        getline (cin, file1);
        
        FILE1.open(file1.c_str(), ios::binary);
        if (!FILE1.is_open() || !FILE1.good())
        {
                cout << "Error opening your first file" << endl;
                system("PAUSE");
                return -1;
        }
        
        cout << "Please enter the name of your second file" << endl;
        getline (cin, file2);
 
        
 
        FILE2.open(file2.c_str(), ios::binary);
        if (!FILE2.is_open() || !FILE2.good())
        {
                cout << "Error opening your second file" << endl;
                system("PAUSE");
                return -1;
        }

		FILE1.seekg(0, ios::end);
		FILE1_size = FILE1.tellg();
		FILE1.seekg(0, ios::beg);
		
		FILE2.seekg(0, ios::end);
		FILE2_size = FILE2.tellg();
		FILE2.seekg(0, ios::beg);
		        
        buffer = new (unsigned char[FILE1_size]);
		FILE1.read ((char*)buffer, FILE1_size);
		FILE1.close();
		
		buffer2 = new (unsigned char[FILE2_size]);
		FILE2.read ((char*)buffer2, FILE2_size);
		FILE2.close();
		
 
 
        cout << "The name of your first file is " << file1 << " and the size is " << FILE1_size << " bytes" << endl;
        cout << "The name of your second file is " << file2 << " and the size is " << FILE2_size << " bytes" << endl;
        printf("%X\n", buffer[0]);
        printf("%X\n", buffer2[0]);
        delete[] buffer;
        delete[] buffer2;
        system("PAUSE");
        return 0;
}

There's my code and notes if it is needed.

Yes, I know about arrays, but I don't know how to write the loop.

You probably have a

unsigned char buffer[BUFSIZE];

filled up with data. The condition you are looking for is that for a certain index i the datum in the buffer is 0xfd, and the datum at i + 4 is 0x06. This literally translates into

if((buffer[i] == 0xfd) && (buffer[i+4] == 0x06))

Apply this in a loop over the buffer (taking proper care near the end), and you are all set.

Okay, as you can see, I have an

unsigned char buffer

, but can I just add the brackets to the end for it to work?

At the moment I wrote that post I didn't see your code. You do not need to add brackets, that has been taken care of by operator new .

I managed to get it to do a search, but it never finds the pattern. I suppose I'm closer, but I still definitely have work to do.

Some of my tinkering from the other day:

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

void myfind(const char *buffer, int size)
{
   for ( int i = 0; i < size; ++i )
   {
      const void *bytes = buffer + i;
      if ( memcmp(bytes, "FD", 2) == 0 )
      {
         printf("%.*s\n", 16, bytes);
         break;
      }
   }
}

int main ()
{
   ifstream file("file.bin", ios::binary);
   if ( file )
   {
      char buffer[1000];
      file.read(buffer, sizeof buffer);
      myfind(buffer,file.gcount());
   }
   return 0;
}

/* file.bin
Alright, I have a file opened, read into a buffer, and closed. I need to search the file for this string of bytes:

FDXXXXXX06YYYYYY

The FD is an assembly code to load something, but that's not important here. The XXXXXX and YYYYYY are values that are unknown and YYYYYY will need to be changed according to something else later. Now, I have the file loaded up as a binary, and can even easily do a hex dump of this file to a .txt file, so I know it's in the right format to look for bytes in Hex.
*/

/* my output
FDXXXXXX06YYYYYY
*/

It's very rough, but it may give you some ideas.

-sigh- I'm not sure if I'm just an idiot or what, but this just isn't working yet. This is (if you couldn't tell) my first program, and I have a lot of learning to do. Someone asked me to make this tool, and I've been working on it forever. This is what I'm supposed to be doing to try and learn C++ and I've learned a lot. But apparently I haven't learned enough to figure this business out so.... yeah. Thank you all who have tried to help.

So start with something like char buff[] = "this is a text string"; and write the loop which finds 's' followed by 'i' 4 places further on.

You don't need megabytes to test with, and it's simply obvious whether you're successful or not.

Alright, I believe I have that part working, but I can't tell until I know how to add the size of the file to the three bytes after the 06 byte. I got it to add to 1 byte, but if it passes FF it crashes the program. What do I do here to add to 3 bytes instead of 1?

Okay, but I mean the brackets that I do the buffer thing with. My compiler gets angry at me when I try to just put them there. :-\ Anyone else think I should have started with something easier?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.