Search for string in memory block

Sep 21, 2010 at 2:14am
I did a pretty thorough search for this but didnt come up with anything but strstr and memchr functions. I am trying to search for a 4 byte string in a memory area created with GlobalAlloc and ReadFile.

I created a function with two parameter for a pointer to memory and size. In my loop I'm getting conversion errors of char * to char[4] and such. I also type casted the FileSize as int to get rid of erros with conversions to int since that is what I want to do (I think).

1
2
3
4
5
6
7
8
9
10
int FindPOL2(char *lpBuffer, LPDWORD FileSize) {
	char POL2[4] = {'P','O','L','2'};
	char buffer[4];
	for (int i=0; i<=(int)FileSize-4;i++){
		if (*(lpBuffer+i) == POL2){
			return i;
		}
	}
	return -1;
}


My real question is: since buffer[4] is 4 bytes, why doesn't *(lpBuffer+i) return just 4 bytes? Or am I going to have to do lstrcmp here?
Albeit longer, this in assembly is so much easier.
Last edited on Sep 21, 2010 at 2:15am
Sep 21, 2010 at 2:30am
What you are writing here is not C++, but Microsoft-specific C code.

Why not use std::ifstream, std::istream_iterator and std::search?
Sep 21, 2010 at 2:30am
Is this what you are looking for?
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
#include <cstring>
#include <algorithm>

const int SIZE = 1024;

int main()
{
	char* data = new char[SIZE];

	data[24] = 'P';
	data[25] = 'O';
	data[26] = 'L';
	data[27] = '2';

	const char* POL2 = "POL2";

	char* found = std::search(data, data + SIZE, POL2, POL2 + strlen(POL2));

	if(found < data + SIZE)
	{
		std::cout << "found: " << found - data << '\n';
	}

	delete[] data;
}
Last edited on Sep 21, 2010 at 2:32am
Sep 21, 2010 at 3:11am
Or as a nice neat function:
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
/**
 * Find string 'str' in data buffer 'buf' of length 'len'.
 *
 * @return size_t offset or len if not found.
 */
inline
size_t offset(const char* buf, size_t len, const char* str)
{
	return std::search(buf, buf + len, str, str + strlen(str)) - buf;
}

int main()
{
	char* data = new char[1024];

	data[24] = 'P';
	data[25] = 'O';
	data[26] = 'L';
	data[27] = '2';

	size_t o = offset(data, 1024, "POL2");
	if(o < 1024)
	{
		std::cout << "found: " << o << '\n';
	}
}
found: 24
Last edited on Sep 21, 2010 at 3:12am
Sep 21, 2010 at 1:24pm
If copying the source isn't a performance problem then

 
std::string( buf, len ).find( "POL2" ) != string::npos


works.
Sep 21, 2010 at 6:17pm
PanGalactic: Thanks for the info about what I was writing. Most of my "c++" knowledge has been from looking at others examples. I though that was more or less a standard way to do things with pointers.

jsmith: eventually the buffer is going to be at least a megabyte in size with multiple POL2 sections. And I didn't want to use string library because from what I can tell it will stop at the first Null byte it finds (or is that specifically C strings?)

Galik: Thanks for that code snipplet, I found the search function in the Cstring.h file. I'll test this out later today.
Last edited on Sep 21, 2010 at 6:19pm
Sep 21, 2010 at 6:24pm
Only cstrings are null terminated. std::string can contain nulls as normal values. Also the std::search() function is in the <algorithms> header. I included <cstring> to get strlen();
Topic archived. No new replies allowed.