How to Search for a Piece of Text in Binary File by Using fstream - C/C++?

Feb 24, 2009 at 1:15am
I got a binary file, i opened the file. I need to look for this ASCII "255.255.255.255" which of type const char *, and then replace it with the user newly inserted any data, in this case IP.

I find this solution simple & interesting:

I'll need to find the following sequence of bytes (hex): 32 35 35 2E 32 35 35 2E 32 35 35 2E 32 35 35. Read the file byte-by-byte, and try to match the pattern. If/when you do find that sequence, you'll write out the new IP address starting at the location you found the first '2'.

Rather than trying to overwrite the original file, it's probably easier to create a new file with the altered sequence of bytes. You should be able to do it in one pass, and you won't need to read the entire file into memory. Maybe just a buffer of 15 bytes, a sliding window that you move through your input file, would do the trick.

********************************************************************************
************

QUESTION:

>>You should be able to do it in one pass, and you won't need to read the entire file into memory. Maybe just a buffer of 15 bytes, a sliding window that you move through your input file, would do the trick

Can you re-explain the above part to me please? - A sliding window that you move through your input file, would do the trick, does sliding window here means reading data in fragments or this a special term for developers? - I guess its my lack of English sometimes puts barrier on me...
Feb 24, 2009 at 1:42am
I think it means something like this:

|123456789012345|67890
and move like this I believe:
1|234567890123456|7890 12|345678901234567|890

etc...
Feb 24, 2009 at 3:49am
Hm, ok...I see

Excuse me, i got one more question...

Here is what i did! I do not know how good this piece of code is, it works %90 i need to fix it little bit, but the main disadvantage is i have to use Hex Workshop to get the offset of ip & offset of port then pass it manually to the parameter. I find it inefficient thats why i though to do it through fstream since its more flexible. I want to locate IP then overwrite it, same thing with port. Please look at the code & give suggestions, comments, etc - if you don't mind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//get offsets through parameters and data to be inserted in place offset
editBinary(long offsetOfIP, long offsetOfPortNumber, const char *IPAddress, const char *portNumber)
{
    const DWORD MAX_NUMBER_OF_BYTES_IN_IP = 15;
    const DWORD MAX_NUMBER_OF_BYTES_IN_PORT = 5;

       DWORD numOfBytesWritten = 0;    
  
       HANDLE hFile = CreateFile(L"application.exe", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
       if(hFile == INVALID_HANDLE_VALUE){
        cout<<"CreateFile() error: "<<GetLastError()<<endl;
        cout<<"*Make sure the file app.exe exists in the folder."<<endl;
        cout<<"Please press any key to go back"<<endl;
        cin.get();
        //get back
        }

       //point to IP offset
       DWORD successResult = SetFilePointer(hFile, offsetOfIP, NULL, FILE_CURRENT);
       if(successResult == INVALID_SET_FILE_POINTER)
       {
        cout<<"SetFilePointer() error: "<<GetLastError()<<endl;
        cout<<"Please press any key to go back"<<endl;
        cin.get();
        //get back
    }
  
       //overwrite the old IP - IPAddress contains the new IP
       BOOL numberOfIPBytesWrittenToFile = WriteFile(hFile, IPAddress, MAX_NUMBER_OF_BYTES_IN_IP, &numOfBytesWritten, NULL);

       if(numberOfIPBytesWrittenToFile == 0)
       {
        cout<<"WriteFile() error: "<<GetLastError()<<endl;
        cout<<"Please press any key to go back"<<endl;
        cin.get();
        //get back
        }

        //point to where port is located
    successResult = SetFilePointer(hFile, offsetOfPortNumber, NULL, FILE_BEGIN);
    if(successResult == INVALID_SET_FILE_POINTER)
    {
        cout<<"SetFilePointer() error: "<<GetLastError()<<endl;
        cout<<"Please press any key to go back"<<endl;
        cin.get();
        //get back
    }

        //overwrite the old port number - portNumber contains the new port number
    numOfBytesWritten = 0;  
        BOOL numberOfPortBytesWrittenToFile = WriteFile(hFile, portNumber, MAX_NUMBER_OF_BYTES_IN_PORT, &numOfBytesWritten, NULL);

     if(numberOfPortBytesWrittenToFile == 0)
     {
        cout<<"WriteFile() error: "<<GetLastError()<<endl;
        cout<<"Please press any key to go back"<<endl;
        cin.get();
        //get back
     }

        //close file
        BOOL isFileClosed = CloseHandle(hFile);

    if(isFileClosed == 0)
    {
        cout<<"CloseHandle() error: "<<GetLastError()<<endl;
        cout<<"Please press any key to go back"<<endl;
        cin.get();
        //get back
    }

        cout<<"Editing Succeeded."<<endl;
}
Topic archived. No new replies allowed.