Hi everybody,
my main goal is to use the base32-library to convert a base32-encoded sequency of characters into their ASCII-code representation.
I found this library which claims to can do this
The base32-library has a single example that tries to demonstrate the use.
Well for adpating the demo-code to my needs the comments are too poor for me to understand.
these are the lines of code that are of intererest (I add the complete demo-sketch below)
byte* tempDecoded = NULL; // pointer to a "byte"-variable??
// ASCII-code representation of the character-sequency as array of bytes
byte enc6[] = {'f', 'o', 'o', 'b', 'a', 'r'};
// base32-encoded representation of the character-sequence as array of bytes
byte dec6[] = {'M', 'Z', 'X', 'W', '6', 'Y', 'T', 'B', 'O', 'I', '=', '=', '=', '=', '=', '='}; // MZXW6YTBOI======
// definition of the function convert from base32 to ASCII
int Base32::fromBase32(byte* in, long length, byte*& out)
// call of the conversion-function embedded into checking to be equal to the pre-defined
// array of bytes
if (base32.fromBase32(dec0, sizeof(dec0), (byte*&)tempDecoded) == 0 &&
memcmp(tempDecoded, enc0, sizeof(enc0)) == 0) {} else { return false; }
free(tempDecoded);
Now my questions are:
There is a definition of a "bytepointer"
byte* tempDecoded = NULL; // pointer to a "byte"-variable??
the "variable" where the conversion-result is stored into is written as
(byte*&)tempDecoded
My - maybe wrong - understanding of this is:
start to store the sequence of bytes at the adress of variable tempDecoded
How the heck can I assure that nothing wrong gets overwritten through the byte-sequence???
Somehow I have the feeling I do not understand what is happening. And that my description is wrong. But I'm unable to solve it myself.
Somehow this function seems to allocate memory and free the memory in the end.
Without understanding what exactly this code is doing I find it very dangerous to modify it.
I would prefer that the function is used in the same way as a cpystr or memcpy-command is used.
With cpystr / memcpy the compiler expects a byte-array or string as the parameter and is able to interpret it as pointer to the RAM-location where the character-sequence is stored.
This is done by simply using the variable itself with no pointer-asterics or anything
char str1[]="Sample string";
char str2[40];
strcpy (str2,str1);//str1 copies to str2
Now this "fromBase32-function uses the asterics and the "&"-symbol in combination like this
(byte*&)tempDecoded)
and I have no idea what this means
byte-pointer by reference???
referenced bytepointer?????
And how I would have to modify it to make it work with arrays of char containing the character-sequence.
here is the complete democode that comes with the library
#include "Base32.h"
Base32 base32;
boolean tests()
{
String encoded, decoded = "";
byte* tempEncoded = NULL;
byte* tempDecoded = NULL;
// encoding decoding
byte enc0[] = {}; byte dec0[] = {};
byte enc1[] = {'f'}; byte dec1[] = {'M', 'Y', '=', '=', '=', '=', '=', '='}; // MY======
byte enc2[] = {'f', 'o'}; byte dec2[] = {'M', 'Z', 'X', 'Q', '=', '=', '=', '='}; // MZXQ====
byte enc3[] = {'f', 'o', 'o'}; byte dec3[] = {'M', 'Z', 'X', 'W', '6', '=', '=', '='}; // MZXW6===
byte enc4[] = {'f', 'o', 'o', 'b'}; byte dec4[] = {'M', 'Z', 'X', 'W', '6', 'Y', 'Q', '='}; // MZXW6YQ=
byte enc5[] = {'f', 'o', 'o', 'b', 'a'}; byte dec5[] = {'M', 'Z', 'X', 'W', '6', 'Y', 'T', 'B'}; // MZXW6YTB
byte enc6[] = {'f', 'o', 'o', 'b', 'a', 'r'}; byte dec6[] = {'M', 'Z', 'X', 'W', '6', 'Y', 'T', 'B', 'O', 'I', '=', '=', '=', '=', '=', '='}; // MZXW6YTBOI======
byte enc7[] = {0}; byte dec7[] = {'A', 'A'};
byte enc8[] = {0, 0}; byte dec8[] = {'A', 'A', 'A', 'A'};
byte enc9[] = {0, 0, 0}; byte dec9[] = {'A', 'A', 'A', 'A', 'A'};
byte enc10[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0F, 0xD5, 0x9B, 0x8D};
byte dec10[] = {0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x42, 0x37, 0x4B, 0x5A, 0x58, 0x44, 0x49};
if (base32.toBase32(enc0, sizeof(enc0), (byte*&)tempEncoded, true) == 0 &&
memcmp(tempEncoded, dec0, sizeof(dec0)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec0, sizeof(dec0), (byte*&)tempDecoded) == 0 &&
memcmp(tempDecoded, enc0, sizeof(enc0)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc1, sizeof(enc1), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec1, sizeof(dec1)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec1, sizeof(dec1), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc1, sizeof(enc1)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc2, sizeof(enc2), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec2, sizeof(dec2)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec2, sizeof(dec2), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc2, sizeof(enc2)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc3, sizeof(enc3), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec3, sizeof(dec3)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec3, sizeof(dec3), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc3, sizeof(enc3)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc4, sizeof(enc4), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec4, sizeof(dec4)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec4, sizeof(dec4), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc4, sizeof(enc4)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc5, sizeof(enc5), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec5, sizeof(dec5)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec5, sizeof(dec5), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc5, sizeof(enc5)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc6, sizeof(enc6), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec6, sizeof(dec6)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec6, sizeof(dec6), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc6, sizeof(enc6)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc7, sizeof(enc7), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec7, sizeof(dec7)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec7, sizeof(dec7), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc7, sizeof(enc7)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc8, sizeof(enc8), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec8, sizeof(dec8)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec8, sizeof(dec8), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc8, sizeof(enc8)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc9, sizeof(enc9), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec9, sizeof(dec9)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec9, sizeof(dec9), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc9, sizeof(enc9)) == 0) {} else { return false; }
free(tempDecoded);
if (base32.toBase32(enc10, sizeof(enc10), (byte*&)tempEncoded, true) > 0 &&
memcmp(tempEncoded, dec10, sizeof(dec10)) == 0) {} else { return false; }
free(tempEncoded);
if (base32.fromBase32(dec10, sizeof(dec10), (byte*&)tempDecoded) > 0 &&
memcmp(tempDecoded, enc10, sizeof(enc10)) == 0) {} else { return false; }
free(tempDecoded);
return true;
}
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
if (tests() == true)
{ // if tests passed, then on-board LED is blinking
while (1)
{
state = !state;
digitalWrite(13, state);
delay(100);
}
}
else
{ // if tests failed, then on-board LED is on
digitalWrite(13, state);
}
}
void loop()
{
}
thank you very much in advance
best regards Stefan