PDA

View Full Version : Base64 - cannot decode properly



#Dragon
11th April 2016, 20:26
Hi, I've got a problem I don't know whats going on, it was working well but now it doesn't.
I want to decode a base64 binary file from QString or QByteArray and then save it, but QByteArray::fromBase64 reads only one (first) line of the file...
Could somebody tell me why cant read all and then decode it ?

I checked using some web decoder and it is encoded properly so It shouldn't cause any problems.

Encoded file: http://wklej.org/id/2269494/txt/

http://4img.eu/images/2016/04/11/Screenshotfrom2016-04-11201728.png



void MainWindow::on_btnDecode_clicked()
{
QString input = ui->inputText->toPlainText();

QString output = QByteArray::fromBase64(input.toLatin1());
ui->outputText->setText(output);
}

The code is very simple as You can see, this is only test program to help solve decoding problem in my app.

Thank You for any help
Cheers,

d_stranz
11th April 2016, 21:20
How are you reading "inputText" to get "input"? From the text you posted, it looks like this is EOL delimited, so there is a good chance that interpretation is stopping at the end of the first line. Your online decoder is probably smarter than that and is stripping EOL characters from the string before interpreting it.

#Dragon
11th April 2016, 21:52
I know that something is wrong with EOL, but I can't remove it, I tried exactly everything

eg

input.remove(QRegExp("[\\n\\t\\r]"));

and

input.simplified();

plus many others...
and still doesn't work

d_stranz
11th April 2016, 22:05
When you execute this line:


QString input = ui->inputText->toPlainText();

What does "input" contain? Is it everything, or just the first line?

#Dragon
11th April 2016, 22:09
It contains everything I just pasted all text from http://wklej.org/id/2269494/txt/ into QTextEdit

The file is stream from server response (IMAP Client)

d_stranz
11th April 2016, 23:02
I guess the problem is that I don't know if what you have pasted into the "wklej.org" web site is *exactly* that you get from your IMAP client, or if if has been mangled for display by the web site (or by QTextEdit). The lines I copy from the web site definitely have "CRLF" on the end of every line, so doing this:


QString input = ui->inputText->toPlainText().simplified();

should get rid of them and give you one giant string with no whitespace.

#Dragon
12th April 2016, 10:49
I know what You mean, but it doesn't work for some reason...

Have a look: http://www56.zippyshare.com/v/znevGrDH/file.html
This is a very simple example, just compile it and paste this: http://wklej.org/id/2269494/txt/
Still reads only one line

Cheers,

d_stranz
12th April 2016, 15:40
Replace your code with this:



void MainWindow::on_btDecode_clicked()
{
QString input = ui->inputText->toPlainText().simplified();

QByteArray latin1 = input.toLatin1();
QByteArray outputBytes = QByteArray::fromBase64( latin1 );
QString output = outputBytes;
ui->outputText->setText(output);
}


In the debugger, if you set a breakpoint after QByteArray:: fromBase64() and look at the contents of "outputBytes" you will see that it contains the entire decoded string. Unfortunately, the byte array contains embedded '\0' characters, which are being interpreted as string terminators. So when you copy "outputBytes" to "output", it stops when it gets to the first '\0', and that's what you see.

I don't know what this is that you are trying to decode, but it looks like a mixture of ASCII and binary, and from some of the text I read I suspect you're trying to decompile a program. That's suspicious behaviour as far as I'm concerned, so that will be the end of my help unless you can come up with a good explanation.

#Dragon
12th April 2016, 16:30
Thank You very much, yeah it contains a binary data, I "splitted" some program installator I had in my Downloads folder by WinRar to get 1MB files, I don't want to decompile anything, on the other hand it could be extremely difficult taking C++ app