views:

299

answers:

4

Ok, I know this has been asked before but after searching I couldn't find a proper answer.

I need to convert a buffer (unsigned char *) to base64, the base64 function I am using takes as paramters:

void Base64Enc(const unsigned char *src, int srclen, unsigned char *dest)

where int srclen is the length of the src string.

My question is, how do I get the length of my buffer. No, it's not null terminated. No, I don't want the sizeof(BYTE). I just need to know what to pass to as srclen so i can convert that buffer to base64.

EDIT:

Here's some code showing what I am doing:

unsigned char *pBytes;
unsigned char *B64Encoded;
int b64size = 0;

if (pBytes = (unsigned char *) GlobalLock(hMem))
{
    DWORD size = (DWORD)GlobalSize(hMem);
    b64size = size / sizeof(unsigned char);
    Base64Enc(pBytes, b64size, B64Encoded);

    // in this case save the buffer to a file just for testing
    if (fp = fopen("ububub.txt", "wb"))
    {   
     printf("data: %s\n", B64Encoded);
        fwrite(B64Encoded, strlen(B64Encoded), 1, fp);
        fclose(fp);
    }
}
+3  A: 

Unfortunately there really isn't a generic answer to this question. In C/C++ it is not possible to simply know how many elements a particular pointer points to. It is the responsibility of the code that maintains the pointer to keep this knowledge.

You'll have to thread this information through from the closest point where you do know the size of the pointer.

JaredPar
The easiest way to keep track of this information is to wrap the pointer in a struct that also contains the length.
Paul Tomblin
@Paul, or just use a vector :)
JaredPar
@JaredPar: Not in C. That's the first thing I'd recommend for C++, though.
David Thornley
@David, doh, came back to the comments and I assumed it was a C++ question
JaredPar
+2  A: 

If it's not NULL terminated or something-terminated, the only way to know the size is to keep track of it when you first got it.

How are you getting the buffer in the first place? Whatever that is probably gives you a method of also obtaining the length of the buffer.

EDIT

Your comment:

I have the size of the memory that the data takes. I tried playing with that but it's not the correct information apparently.

In that case, take your raw size and divide it by the size of each element to get the number of elements:

unsigned numelem = size / sizeof(unsigned char)

Thanks to Todd Gardner for pointing out that sizeof(unsigned char) is defined by the standard to be 1, so the number of elements in your buffer is just the amount of space your buffer uses.

The "divide by size of element" is the more general solution to any type of element.

GMan
I have the size of the memory that the data takes. I tried playing with that but it's not the correct information apparently.
wonderer
No, it's not. What you need is the length of the string, the length of the information your source puts into memory. Look at the API again; there will almost certainly be a way to find the length.
David Thornley
But sizeof(unsigned char) is guaranteed to be 1 (6.5.3.4 of C99)
Todd Gardner
Thanks, I included that. I was just concerned with the general solution.
GMan
See in the question
wonderer
well, this one seems to be working ok.
wonderer
+1  A: 

With a c-style array, you have to already know the size, or have a sentinel terminated (like NULL) array. The run time machinery has access to the amount of memory allocated, but there isn't a safe, portable way to access it.

Todd Gardner
+3  A: 
if (pBytes = (unsigned char *) GlobalLock(hMem))

In this case you have a handle hMem to a global memory block. This handle must have been obtained by a call to GlobalAlloc() or GlobalReAlloc(). When making one of these calls you must have known the memory size. This is the length of bytes you are looking for.

Magnus Skog
wonderer