Kennet

Hi! Folks

I have tried to decode a string I have made using the encoder/decoder from;

Sample Base 64 Encoding and Decoding

Article ID: 191239

Revision: 3.0

http://support.microsoft.com/kb/191239

There¡¯s no problems using the encoder from Base64Coder everything works smoothly. Problems occur when I try to decode the string I've just made. This is what I do and what happens:

1) I have a encoded string in a CString variable:

const CString strInput

2) I create a file where I want to put my decoded information.

HANDLE hFile = ::CreateFile(strOutputFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);

3) I now convert the const CString to a char * like this (it works just fine):

const char* pszText = static_cast<const char*>( strInput );

char *pEncodeData = (char *)pszText;

4) Finally it¡¯s time for decoding the string:

Base64Coder Coder

Coder.Decode(pEncodeData);

CString strDencoded = (LPCTSTR)Coder.DecodedMessage();

What I end up with in strDencoded is pretty far from the string I started with. I just get rubbish. Important to notice is that I¡¯ve tried the string from the sample project, see article ID: 191239. and things work just fine. I get back the original string using above code, no problems. This is the string;

"This is a test \r\n Of the line\r\nBreaks\tDoda.\0"

The faulty string I get is just 1kb, it should be about 20 kb.

If you guys don't have an exact answer then maybe you can give me some hint to what it might be I¡¯m doing wrong.

The above code has been compiled with Visual C++ 6.0. Yes, I know it¡¯s an old fart but I must stick to this compiler while working with this project so no smart comments, please.

Regards

Kennet Carlsen



Re: Visual C++ General Base 64 Encoding and Decoding

dseifert

Your description is a bit confusing (or maybe I misread something). If you have a decoded string (step 1), why do you decode it in step 4 Isn't it already decoded Or do you want to encode Converting something const into non-const is also generally not a very good idea, btw.

An issue I can think of in your example is the conversion of CString to char*. IIRC CString does not automatically convert from unicode, so if your project is unicode aware, pszText points to a string of two byte characters which is not what you want a char* (string of one byte characters) pointing to.




Re: Visual C++ General Base 64 Encoding and Decoding

Kennet

I¡¯m sorry

Yes, I understand if it was confusing, I¡¯m sorry. I meant to write ¡°1) I have a encoded string in a CString variable:¡± and nothing else. I have now changed it as you can se above.

What fascinates me is the fact that I can squeeze that 50-character string through the encode/decode process but when I try my 20kb string I get rubbish. Maybe it has something to do with stuff such as linefeed ¡°\n¡° or something like that. It¡¯s clear that ¡°*** happens¡± when I try a larger encoded file.

I have tried to ¡°manually¡± manipulate the sample string (encoded version) by copy/past a random section of it back to itself. I repeated the process to make sure I got far beyond 50 characters string length. It still works!! The copy/pasted section was repeated in the decoded string that¡¯s all.

I need to come to some understanding of this problem somehow. Maybe I can get my hands on a code snippet somewhere that can shade some light on this matter.

Regards

Kennet Carlsen





Re: Visual C++ General Base 64 Encoding and Decoding

Simple Samples

Don't use casts just to get something to compile because you are likely to have prblems such as this.






Re: Visual C++ General Base 64 Encoding and Decoding

Kennet

Yes, I guess you are right about that entire ¡°static_cast¡±-thing. I wish I could come up with something better. Right now I've run dry on ideas. It still puzzles me that I can encode/decode the sample string without problems

Regards

Kennet Carlsen





Re: Visual C++ General Base 64 Encoding and Decoding

dseifert

CString::GetStringBuffer returns a pointer to the string. But seriously, check unicode (although if the sample string works, this probably isn't the issue. But better safe then sorry).

Overall, I think there is not enough information to see what is going on. For example, where/how do you get the encoded string - it probably is not hardcoded as the example string you use, correct If you maybe could show some code snippet where you set strInput, this may shed more light on it.

Also, did you check that strInput is correct You could verify this by feeding it into the base64 sample app.

Also, have you tried using the debugger to step through the code to see where it stops decoding

P.S.: What relevance has your step 2 above, it doesn't seem to contribute anything to your scenario




Re: Visual C++ General Base 64 Encoding and Decoding

Simple Samples

What are the parameters and return values of the relevant encode/decode sample functions Since the code must be downloaded to get that information, it would help if you provided the information here instead of requiring us to download the smple code. Probably you are expecting the CString class to require more than it really does. For example, instead of:

Code Block

const char* pszText = static_cast( strInput );

char *pEncodeData = (char *)pszText;

Coder.Decode(pEncodeData);

You can probably do:

Code Block

Coder.Decode(strInput);






Re: Visual C++ General Base 64 Encoding and Decoding

Simple Samples

dseifert wrote:
CString::GetStringBuffer returns a pointer to the string.

CString:Surpriseperator LPCTSTR might also work and if so then it would be easier.

dseifert wrote:
But seriously, check unicode (although if the sample string works, this probably isn't the issue. But better safe then sorry).

I also thought that Unicode might be a problem, but note that this is VC 6.






Re: Visual C++ General Base 64 Encoding and Decoding

dseifert

LPCTSTR should indeed work as Decode is declared as Decode(LPCTSTR szMessage). I downloaded the sample app and it looks fine to me (no obvious size limitations, etc), hence my request for information on how he fills the strInput variable.




Re: Visual C++ General Base 64 Encoding and Decoding

Simple Samples

dseifert wrote:
LPCTSTR should indeed work as Decode is declared as Decode(LPCTSTR szMessage). I downloaded the sample app and it looks fine to me (no obvious size limitations, etc), hence my request for information on how he fills the strInput variable.

I am not concerned about the strInput variable as much as I am concerned about the DecodedMessage return value. Knowing the parameter of Decode helps but I don't know the DecodedMessage return value.






Re: Visual C++ General Base 64 Encoding and Decoding

Kennet

Hi!

Thanks all of you for looking into this problem of mine. In all forums like this it¡¯s of course desirable to add as much of the code as possible. In this case it would clearly exceed what I would call a ¡°code snippet¡±. That¡¯s why I¡¯m referring to the Article ID: 191239.

Before we go any further I would like to tell you that I¡¯ve spent several hours doing all kinds of more the less relevant tests. Some of them have been totally out of the window, for sure, I can see that now. I have, I hope, done everything to exclude obvious stupidity.

Now to business!

Point 1)

const CString strInput

I have an encoded string and the encoding part works great. I know that because the function that does the job has been doing it in other applications without any problems for quite some time.

Point 2)

HANDLE hFile¡­¡­..

Creating a file that gets the output from strDencoded further down in the code, not visible here.

Point 3)

const char* pszText = static_cast<const char*>( strInput );

char *pEncodeData = (char *)pszText;

This is my clumsy attempt to convert ¡°const CString&¡± to ¡°char *¡± I must honestly say that I have my suspicions that it¡¯s here I get into trouble, but this is only what I guess. I don¡¯t know for sure. It seems to work but would you guys do something like this Not if you can help it I guess.

Before you sink your teeth into above code snippet keep in mind that I¡¯m restricted to the VC++ 6.0 compiler and therefore have a limited range of options. We will hopefully be compiling the project with C++.NET in a near future I¡¯ve been told. But for now we are stuck with above compiler.

Point 4)

Base64Coder Coder

Coder.Decode(pEncodeData);

CString strDencoded = (LPCTSTR)Coder.DecodedMessage();

The decoder can of course not give me high quality string if the stuff I put into it is rubbish.

What can I say, the string that I get from the encoder using the test string has the same structure as my 20kb after a match with the encoder.

I¡¯m close to ¡°throw in my towel¡±. Maybe I must try to find another approach to this Base64 problem somehow.

Regards

Kennet





Re: Visual C++ General Base 64 Encoding and Decoding

Simple Samples

You did not provide any new information. You have not tried to answer our questons.






Re: Visual C++ General Base 64 Encoding and Decoding

Kennet

I¡¯m sorry but our company policy just don¡¯t give me the right to publish file information onto the internet. Nothing I can do about it I¡¯m afraid. I had hoped that my problems were related to no.3 above and that it maybe was obvious for someone out there.





Re: Visual C++ General Base 64 Encoding and Decoding

Simple Samples

The main thing I asked for is a little bit of information from the Microsoft code. I think it is very reasonable for you to do a little work instead of making me do it. If your company thinks they own the Microsoft code then they are wrong. The time it would have taken to provide the information would be about equal to the time it took to say you cannot do it and it is not true that you cannot.






Re: Visual C++ General Base 64 Encoding and Decoding

Simple Samples

It is very common to write simple samples that recreate a problem. You can do that here if you want to.