adamvr / arduino-base64

A base64 library for the arduino platform, written in C
MIT License
184 stars 102 forks source link

some problem with the library not sure what #12

Open shirish47 opened 9 years ago

shirish47 commented 9 years ago

the below one is a base64 conversion by library: eyJldmVudCI6IlVzZXIgUmF0aW5nIiwicHJvcGVydGllcyI6eyJkaXN0aW5jdF9pZCI6IjEzNzkzIiwidG9rZW4iOiJjZDVjOTVlMzZjZTFkZmJjMzE2Mjk1MDFmODQzYTYyNSIsIkRldmljZSI6IjMyMSIsIk1vdmllSUQiOiIzMjEiLCJSYXRpbmciOiIzMjEifX0A

and this one is online based conversion: eyJldmVudCI6IlVzZXIgUmF0aW5nIiwicHJvcGVydGllcyI6eyJkaXN0aW5jdF9pZCI6IjEzNzkzIiwidG9rZW4iOiJjZDVjOTVlMzZjZTFkZmJjMzE2Mjk1MDFmODQzYTYyNSIsIkRldmljZSI6IjMyMSIsIk1vdmllSUQiOiIzMjEiLCJSYXRpbmciOiIzMjEifX0=

both have one minute difference and that is at the end. I am using this string to upload data to an online service called Mixpanel although both fails but I want to make sure this library is making proper convesion.

shirish47 commented 9 years ago

does the library introduce newline character at the end?

shirish47 commented 9 years ago

Hi Shirish,

I took a look at the base64 library you are using and it would appear a character is being appended at the end of the Base64 encoded payload.

Take a look at this line: https://github.com/adamvr/arduino-base64/blob/master/Base64.cpp#L46.

On that line, the character '\0' is appended to the end of the Base64 payload -- from my perspective this does not make sense as the payload does not require this character.

Do you mind and checking if I am reading this correctly? Is there something I am missing here in regards to the behavior I am reading from this code?

I see you opened an issue against this lib on Github -- feel free to read what I sent and see if this information should be added to your issue.

Happy to assist further here in any way possible -- just let me know how I can be of assistance!

Thanks, Ryan

As Ryan Suggested I commented the line 46 in base64 but I don't know how the length has to be changed. but anyways when I print I get a correct converted string but with the input string also printed after that?? why is that??

I think that is because the last '\0' is removed and that encoded string lies above the input string so when println prints it does not stop until a \0 in the input string.

e-lin commented 9 years ago

Hi, I met the same issue as @shirish47 described. Any hint by now?

shirish47 commented 9 years ago

I have not yet solved the problem but you can get into the code and put go to line no 64 where they add a \0 character... if you cange that character you will not be able to print ...because print requires \0 char .. if you remove that and put something else ..it prints a complete decoded string but gets into memory of ther variable...

e-lin commented 9 years ago

Hi @shirish47

In your ino file, try to get your input length by strlen(input).

shirish47 commented 9 years ago

hey buddy pls how could I resolve this issue?

@e-lin did you solved it?

e-lin commented 9 years ago

@shirish47 I've fixed it. I would rather say this is not the issue of this library but the string length of your input parameter is maybe not correct.

palto42 commented 6 years ago

I also run into some issue with this library because of the last assingment output[decLen] = '\0'; in the encode and decode functions. If you move the whole print function after the encode, then the input string will not print any more because the first char was overwritten with /0.

//  Serial.print(input); Serial.print(" = "); // moved to after encode function

  // note input is consumed in this step: it will be empty afterwards
  base64_encode(encoded, input, inputLen); 

  Serial.print(input); Serial.print(" = "); // moved here from before encode function
  Serial.println(encoded);

Then the output result looks like this:

Base64 example
 = SGVsbG8gd29ybGQA
Zm9vYmFy = foobar

As you can see, the first string doesn't print any more.

After several tests I found that the calculation of the encode and decode length doesn’t consider the trailing \0 for the string termination.

In order to get correct results, two things in the example code need to be changed: 1) Correct the input length, this must not count the trailing \0

 int inputLen = sizeof(input) - 1; // don't count the trailing \0 of srings

or use the runtime caclulation strlen() which doesn't count the trailing \0

 int inputLen = strlen(input) ; 

2) Increase the length of the output char array by +1 for the terminating \0

char encoded[encodedLen+1]; // add +1 for terminating /0
palto42 commented 6 years ago

I just sent a pull request https://github.com/adamvr/arduino-base64/pull/23 for a corrected example code.