Results 1 to 5 of 5

Thread: Base64 decoding problem (76 chars per line)

  1. #1
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Base64 decoding problem (76 chars per line)

    Hello, could somebody explain my why QByteArray::fromBase64() does not work if input data contains whitespaces ?

    For example I've got attachment contains binary data (Jpeg image or something) and file looks that:

    Qt Code:
    1. JMZDPEtHBFh0BplzQ+AareD3FJUm//uUZOgAArYrS5s5GmBgBUlDawZMCjyNMUzgaUE1lSa1h400
    2. DsU0Z+hbxXKeC2sz267Ujb7bjvI9a8OrFA5vjdZTqFsTHVVp+fKfSNQclzKepQKxpEXWDg4j6urV
    3. u/f+4duXDyoaUdJy23+uk/LuCyGWCNYkGJwbSVScdafgyX9Iz9qtWFuR7fwn1GObEh4kYUM0jk4
    4. ..
    5. ..
    6. ..
    To copy to clipboard, switch view to plain text mode 

    When I try to decode it only does the first line...
    How can I decode whole file ?

    I tried QByteArray::fromBase64() and c++ function

    (c++ function found on web)

    Qt Code:
    1. static inline bool is_base64(unsigned char c) {
    2. return (isalnum(c) || (c == '+') || (c == '/'));
    3. }
    4.  
    5. static const std::string base64_chars =
    6. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    7. "abcdefghijklmnopqrstuvwxyz"
    8. "0123456789+/";
    9.  
    10. std::string MainWindow::base64_decode(std::string const& encoded_string)
    11. {
    12. int in_len = encoded_string.size();
    13. int i = 0;
    14. int j = 0;
    15. int in_ = 0;
    16. unsigned char char_array_4[4], char_array_3[3];
    17. std::string ret;
    18.  
    19. while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
    20. char_array_4[i++] = encoded_string[in_]; in_++;
    21. if (i ==4) {
    22. for (i = 0; i <4; i++)
    23. char_array_4[i] = base64_chars.find(char_array_4[i]);
    24.  
    25. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
    26. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
    27. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
    28.  
    29. for (i = 0; (i < 3); i++)
    30. ret += char_array_3[i];
    31. i = 0;
    32. }
    33. }
    34.  
    35. if (i) {
    36. for (j = i; j <4; j++)
    37. char_array_4[j] = 0;
    38.  
    39. for (j = 0; j <4; j++)
    40. char_array_4[j] = base64_chars.find(char_array_4[j]);
    41.  
    42. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
    43. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
    44. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
    45.  
    46. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
    47. }
    48.  
    49. return ret;
    50. }
    To copy to clipboard, switch view to plain text mode 

    I can't remove those whitespaces...

    text.simplified();
    text.replace("\\s", "").replace("\\n", "").replace("\\r", "").replace(" ", ;

    Thank You for any help

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Base64 decoding problem (76 chars per line)

    If you look at the base 64 array in that code snippet you will see that it does not contain any whitespace characters.

    So the base64 decoding ends at the first one it encounters because that is when the valid input ends.

    If your input is somehow split into multiple block, you'll first have to reunite these blocks into one.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Base64 decoding problem (76 chars per line)

    If your input is somehow split into multiple block, you'll first have to reunite these blocks into one.
    How can I do that ?

    The main problem is that I can't get one block... I tried many many times and it fails

    //edit

    Qt Code:
    1. QString str, str2;
    2. QTextStream stream(&str);
    3. stream << ui->plainTextEdit->toPlainText();
    4.  
    5. while(!stream.atEnd()){
    6. str2.append(stream.readLine().simplified());
    7. }
    8.  
    9. I tried everything and cant convert it into one string
    To copy to clipboard, switch view to plain text mode 
    Last edited by #Dragon; 1st August 2016 at 22:20.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Base64 decoding problem (76 chars per line)

    Well, it depends on how the encoding program formatted the blocks, you just have to do the reverse.

    - If it inserted newlines, then remove the newlines.
    - If it inserted newline+carriage return, then remove both.
    - If the blocks have defined length N, then take each N bytes.
    - If the blocks have begin and end delimiters, take the bytes between these delimiters.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Base64 decoding problem (76 chars per line)

    Qt Code:
    1. QString base64String = ui->plainTextEdit->toPlainText();
    2. base64String = base64String.replace(QRegExp("[\r\n\s]+"), "");
    To copy to clipboard, switch view to plain text mode 
    you might even be able to do it without the \r\n because they are considered whitespace ( i.e. Part of \s)

Similar Threads

  1. Encoding/Decoding and SQL
    By alexandernst in forum Qt Programming
    Replies: 0
    Last Post: 1st February 2011, 12:16
  2. Decoding extended ascii
    By waynew in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2010, 13:26
  3. decoding from utf8
    By pcaeiro in forum Qt Programming
    Replies: 6
    Last Post: 25th August 2009, 13:51
  4. Decoding an UTF-8 QByteArray...
    By Nyphel in forum Newbie
    Replies: 2
    Last Post: 25th April 2007, 09:03
  5. problem with storing greek chars to a buffer (os linux)
    By nass in forum General Programming
    Replies: 3
    Last Post: 3rd January 2007, 17:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.