0

Excuse the simplicity of my question, I'm not used to working with Windows types.

I have an LPBYTE buffer and i wish to XOR each byte of it with another byte.

What is the proper way of getting the length of the buffer and iterating through it in C++? I'm trying to do something akin to:

LPBYTE buf = *something*;
char key = 'X';

for(int i=0;i<len(buf);i++)
    buf[i] = buf[i] ^ key;

Many Thanks!

Dave White
  • 321
  • 1
  • 3
  • 13
  • 3
    `LPBYTE` is the same as `unsigned char *`, in case that helps. – Mark Ransom Jan 01 '12 at 22:13
  • An `LPBYTE` _points_ to a buffer. – Lightness Races in Orbit Jan 01 '12 at 22:13
  • 2
    Unless the data pointed to by the `LPBYTE` is null-terminated (and it certainly doesn't scream out "string-like data-type"), then there isn't a way of discovering how long it is. – Oliver Charlesworth Jan 01 '12 at 22:14
  • so if its null terminated I can just get the length with `strlen` ? buf is actually assign to the value of `(LPBYTE) LockResource(LoadResource(NULL, hRsrc));` , any guesses if that's null terminated? – Dave White Jan 01 '12 at 22:18
  • 1
    There have been a bunch of questions about xor 'encryption' in the past few weeks. Pretty typical for end-of-semester projects. The usefulness is zero, it is nothing but a Caesar cypher that's trivially broken by a frequency count. Have you considered meeting with your class mates to hammer this out? Incredibly useful Real World experience there, working together as a team. Very important when you get a real job. Not otherwise modeled well at SO, this is a place for answers, not practices. – Hans Passant Jan 01 '12 at 22:46

2 Answers2

3

A LPBYTE buffer in C(/C++) is just an address somewhere in memory, so you need to keep track of the length of that buffer preferably in an explicit way by having a size value.

E.g.

// use a struct instead to keep things together    
struct 
{
   LPBYTE buffer;
   size_t size;

} yourbuffer;

// init part
BYTE somewriteabledata[200];

yourbuffer.buffer = somewriteabledata;
yourbuffer.size = sizeof(somewriteabledata);

char key = 'X';

for(int i=0;i<yourbuffer.size;i++)
    yourbuffer.buf[i] = yourbuffer.buf[i] ^ key;
AndersK
  • 35,813
  • 6
  • 60
  • 86
2

buf is actually assign to the value of (LPBYTE) LockResource(LoadResource(NULL, hRsrc));, any guesses if that's null terminated?

Depends from the type of resource, but most probably no. Anyhow, since you are working with resources, you can get the resource size using the SizeofResource function.

Still, I'm not sure if you can write on stuff returned by LockResource (it actually returns a pointer to the region containing the resource, that is probably just a region in the memory-mapped executable). Most probably you'll want to copy it elsewhere before XORing it.

HGLOBAL resource=LoadResource(NULL, hRsrc);
if(resource==NULL)
{
    // ... handle the failure ...
}
LPBYTE resPtr=LockResource(resource);
DWORD resSize=SizeofResource(NULL, hRsrc);
if(resPtr==NULL || resSize==0)
{
    // ...
}
std::vector<BYTE> buffer(resPtr, resPtr+resSize);
// Now do whatever you want to do with your buffer
for(size_t i=0; i<buffer.size(); ++i)
    buffer[i]^=key;
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299