Is memcmp() Comapres Characters Length As Well As Data in This Length?

1. Is memcmp compares length as well as data to see if they equal? or just compares number length?

2. I got this code, if you could explain it in words, i will appreciate it. I almost understood the code, but i need somebody to clarify it just to ensure i got i understood it right...Please explain where i put ***********.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Assume size is determined already...
typedef unsigned char BYTE; // if not defined

char *memblock;
memblock = new char[size]; 

BYTE IPAddrOld[] = { 1, 2, 3, 4, 5};
BYTE IPAddrNew[] = { 88, 55, 33, 1, 00};


for (size_t sz = 0; sz < size; ++sz) {

//the first parameter confuses me, i don't get it ******
		if (!memcmp(memblock + sz, IpAddrOld, sizeof(IPAddrOld)) {

			 // found it! Replace with new one **** first parameter confuses me here *****
			 memcpy(memblock + sz, IPAddrNew, sizeof(IPAddrNew);

			 break; // done.
		 }
}


Thanks
Last edited on
The first parameter is the address of the first block of memory to compare against the ipaddrold. Since I can't see the rest of the program I have no idea if that makes sense. I don't know what memblock is set to. The only thing I see is an uninitialized block of memory of an unknown size. The first arg is a pointer to the first memblock and the second arg is the pointer to the second memblock. The third param is the number of bytes to compare beginning with the bytes pointed to by the first two params.

It is okay to add to a pointer. It simply increments the original starting address by sz memory locations.
Thats the main code:

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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
        fstream fileStream;
        ifstream::pos_type size;
        char *memblock;
 
        fileStream.open ("application.exe", fstream::ate | fstream::in | fstream::binary);
 
        if (!(fileStream.is_open()))
        {
                cout<<"Error opening file";
                cout<<"*Make sure the file app.exe exists in the folder."<<endl;
                fileStream.close();
                exit(-1);
        }
 
       //read the whole .bin file into memblock
        size = fileStream.tellg();
        memblock = new char[size];
        fileStream.seekg(0, ios::beg);
        fileStream.read(memblock, size);      //read whole content of the file into memblock up to size - i think read() goes into a loop implicitly - I hope this is right.
        fileStream.close();
 
        cout << "the complete file content is in memblock right now ***"<<endl;
 
        string IPAddress = "3235352e3235352e3235352e323535"; //equivelant of 255.255.255.255
        string newIpAddress = "3032322e3133302e3034352e303130"; //equivelant of 022.130.045.010
 
        //pre-condition: use for loop until you find IPAddress in memblock
        //then overwrite IPAddress with newIPAddress. Quit
        //Application. Just before quiting the application make sure file size
        //has not been changed. The newIpAddress bytes has extact
        //same size as IPAddress size.
 
        //post-condition: .exe is edited successfully (carries the newIpAddress)
 
        delete [] memblock;
        system("pause");
        return 0;
}
http://cplusplus.com/reference/clibrary/cstring/memcmp.html

memcmp compares the data pointed to by each of the first two params. Since only one length is specified there is no way it can compare the lengths of the two buffers because it has no idea what each is pointing to. The first two params are simply memory addresses. The only thing it can do is is compare each byte pointed to until x number of bytes have been compared, where x is the third input parameter. The above link probably explains it better than I can.
thx.....

I changed it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Assume size is determined already...
typedef unsigned char BYTE; // if not defined

BYTE IPAddrOld[] = { 1, 2, 3, 4, 5};  // Declaring new char array with already initialized values, size=5*sizeof(BYTE) = 5 bytes
BYTE IPAddrNew[} = { 88, 55, 33, 1, 00}; // Same as earlier, size=5*sizeof(BYTE) = 5 bytes


/ / I assume that size is 5, and memblock = new char[5]
for (size_t sz = 0; sz < size; ++sz) {

      
        if (memcmp(&memblock[sz], IpAddrOld, sizeof(IPAddrOld) == 0) {
             memcpy(&memblock[sz], IPAddrNew, sizeof(IPAddrNew);
             break; // done.
         }
}


Last Question:

BYTE IPAddrOld[] = { 1, 2, 3, 4, 5, 6, 44, 66, 33, 55, 66, ff}; // Declaring new char array with already initialized values, size=5*sizeof(BYTE) = 5 bytes
BYTE IPAddrNew[} = { 88, 55, 33, 1, 00}; // Same as earlier, size=5*sizeof(BYTE) = 5 bytes

What if i want to replace 6 up to 55 with 88 up to 00? Size of IPAddrOld[] is larger...What i though to do is the following:

1. before i go into if(memcmp()), i put every five chars into a variable string.
2. compare it with the string 6,44,66,33,55 - if true, then proceed to if(memcmp()). If not true, repeat the loop.

Do you have any suggestions?

I hope to find a reply...
Last edited on
I knew the answer already...thx
Topic archived. No new replies allowed.