Simple:

I have buffer LPBYTE lpBuf

And I need size of it.I tried sizeof(lpBuf); but doesnt work properly.

How to do it,which function to use?

LPBYTE is a pointer to a BYTE. If you have allocated an array of BYTEs and pointed lpBuf at that allocated array then if you need the size of that buffer you have to store it somewhere.

There is no function you can call that will return the size of an array that you are accessing through a pointer.

LPBYTE is a pointer to a BYTE. If you have allocated an array of BYTEs and pointed lpBuf at that allocated array then if you need the size of that buffer you have to store it somewhere.

There is no function you can call that will return the size of an array that you are accessing through a pointer.

Really??? I need to drop it on disc like file?

And sizeof( *lpBuf); wont work too?

Can I get at least the pointer to last element of lpBuf?

Yes really.

Of course sizeof(*lpBuf) wont work. lpBuf has type LPBYTE (pointer to a BYTE) so *lpBuf has type BYTE so sizeof(*lpBuf) is sizeof(BYTE) which is most likely 1.

Of course you can get a pointer to the last element in the buffer, just calculate it from the start pointer and the buffer size.

Edited 6 Years Ago by Banfa: n/a

Of course you can get a pointer to the last element in the buffer, just calculate it from the start pointer and the buffer size.

Haha,we are going in circles.

Actually i dont have buffer size,and my goal is to find a buffer size,thats why I wanted to calculate it if I knew pointer to the last byte.

Here is RC4() encryption function.Does this function change size of buffer???

I need size of lpBuf that is returned.Does it stay the same as dwBufLen?

LPBYTE RC4(LPBYTE lpBuf, LPBYTE lpKey, DWORD dwBufLen, DWORD dwKeyLen)
{
	int a, b = 0, s[256];
	BYTE swap;
	DWORD dwCount;
	for(a = 0; a < 256; a++)
	{
		s[a] = a;
	}
	for(a = 0; a < 256; a++)
	{
		b = (b + s[a] + lpKey[a % dwKeyLen]) % 256;
		swap = s[a];
		s[a] = s[b];
		s[b] = swap;
	}
	for(dwCount = 0; dwCount < dwBufLen; dwCount++)
	{
		a = (a + 1) % 256;
		b = (b + s[a]) % 256;
		swap = s[a];
		s[a] = s[b];
		s[b] = swap;
		lpBuf[dwCount] ^= s[(s[a] + s[b]) % 256];
	}
	return lpBuf;
}

That function does not change the size of the buffer pointed to by lpBuf, actually it can't because lpBuf is passed by value.

The code of the function implies that dwBufLen must be <= the size of the buffer pointed to by dwBufLen.

You should consult the creator of this function or the creater of the code that calls this function to find out for sure if the meaning and use of dwBufLen is the length of the buffer pointed to by lpBuf; although by normal convention it would be.

No I am sure that that function doesn't alter the buffer length and that even if it did that would have no effect on the buffer length in the calling code.

I know that convention is that a parameter like dwBufLen is the length of the buffer but I can not be sure that that convention is followed in the code you have posted.

Edited 6 Years Ago by Banfa: n/a

dwBufLen is exact length of returned lpBuf 100% sure???

isn't this function for compression too?

I do not know exactly what that function does.

That function does not return a buffer, it accepts a buffer (well 2 buffers really, lpBuf and lpKey) and a parameter (again 2 really for the size of each of the buffers) that by convension is the length of the buffer.

Convention is that the parameter passed in dwBufLen is the length of the buffer pointed to by lpBuf. However that is entirely dependent on the calling code, there is nothing to prevent the calling code (which you don't show) passing a pointer to a buffer that is longer than dwBufLen. However whatever the actual length of the buffer that function on works on the first dwBufLen BYTEs of it.

If that function is doing any compressing then is is compressing the data in lpKey into lpBuf. However the function does not change the size of either of the buffers lpKey and lpBuf.

This article has been dead for over six months. Start a new discussion instead.