As written, the code is certainly an invitation for a memory leak. That alone would be enough for me to question the quality of the entire library code and motivate me to rewrite it myself.
There are several ways to improve it ... for example use STL smart pointers or containers. But, the simplest would probably be for the calling function to provide the storage space (as an array) and pass a pointer to it into the conversion function (along with a maximum size). The conversion function would then fill the storage provided by the caller.
I added comments to the function fromBase32
can the experienced users here read through my comments and respond if my understanding of what the code does is correct?
int Base32::fromBase32(byte* in, long length, byte*& out) {
int result = 0; // Length of the array of decoded values.
int buffer = 0;
int bitsLeft = 0;
byte* temp = NULL;
temp = (byte*)malloc(length); // Allocating temporary array.
for (int i = 0; i < length; i++) {// for-loop to iterate through the bytes the parameter "in" is pointing to
byte ch = in[i]; // assign a single character of the "input" byte-sequence to ch
// look up one base32 symbols: from 'A' to 'Z' or from 'a' to 'z' or from '2' to '7'
if ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A)) { ch = ((ch & 0x1F) - 1); }
else if (ch >= 0x32 && ch <= 0x37) { ch -= (0x32 - 26); }
else { free(temp); return 0; }
buffer <<= 5;
buffer |= ch;
bitsLeft += 5;
// if more than 8 bits are processed in the conversion
if (bitsLeft >= 8)
{
// store this byte into array "temp"
temp[result] = (unsigned char)((unsigned int)(buffer >> (bitsLeft - 8)) & 0xFF);
result++;
bitsLeft -= 8;
}
}
// allocate temporary memory in the length of "result" bytes
// and use "out" as the name for the pointer-variable pointing to this adress in RAM
out = (byte*)malloc(result);
memcpy(out, temp, result); // copy byte-sequence stored at "temp" to RAM-location where "out" points to
free(temp); // "mark RAM-area formely reserved for "temp" as free to use
return result;
}
which means after leaving the function fromBase32 the variable handed over by the call to the function
as the third parameter is pointing to that area in RAM where the converted byte-sequence is stored?
Now If I would like to use an array of byte as the third parameter
byte myTargetForConvertedASCII_HexKey[16];
and that this variable called "myTargetForConvertedASCII_HexKey" should contain the converted byte-sequence
How do I have to modify the function?
What I find difficult is that I have to "hand-over" an array of bytes as a parameter or do I hand-over a pointer to the beginning of the byte-array?
If yes how does this look like?
byte mySourceArray[32];
byte myTargetArray[32];
void demoFunc (//not sure how the definition must look like) {
void demoFunc("myTargetArray", "mySourceArray") {
is it obvious, i'm asking, if the data is actually copied when using "&" or if it's really no different than a pointer and it's junk compiler symantics?
There are some C++ things that can only be done with pointers and some that can only be done with references. There are also areas where their applicability overlap. In those cases, you can choose between them based on preferred syntax, error-proofness, etc.