0

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?

Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67

1 Answers1

2

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
  • 25,520
  • 5
  • 61
  • 71
  • Thanks - that's done it! I was getting confused with my `&` and `*` in the calling function too. In the calling function the var is defined as `unsigned char *` and is prefixed with a `&` in the function call. Within `encryptTake2:` all references to the var are prefixed with `*`. I need to re-read chapter 5 of the C book. – Benedict Cohen Dec 06 '09 at 20:50