views:

177

answers:

1

I'm playing around with the openSSL library and it requires me to brush up on pointers, and I'm having difficulty.

I have a objective-c method:

-(unsigned char *)encryptTake1:(unsigned char *)input inputLength:(int)inLen outputLength:(int*)outLen;

It takes some data, encrypts it and returns a pointer to the data and the length of the data as an output parameter.

I want to change this so that the encrypted data is also handled as an output parameter and the return value is used to indicate success or failure. This is what I have:

-(int)encryptTake2:(unsigned char *)input inputLength:(int)inLen output:(unsigned char *)output outputLength:(int*)outLen;

This doesn't work. What am I doing wrong? I think the problem is that (unsigned char *) is the wrong. If (unsigned char *) is wrong then I presume I will also need to change how output is referenced within the method. How?

+2  A: 

It depends on how you are handling memory allocation.

What does -encryptTake1: return? If it returns a newly allocated buffer that the caller must free then you would use unsigned char** in encryptTake2:

-(int)encryptTake2:(unsigned char *)input inputLength:(int)inLen output:(unsigned char **)outputPtr outputLength:(int*)outLen
{
    *outputPtr = malloc(1024);
    unsigned char* output = *outputPtr;
    strcpy(output, "hello");
    ...
}
Darren
Benedict Cohen