2

We have a Winform client app that is comsuming a web service we write. This client app requests documents that are contained in XML files, generally a PDF written to a base64 encoded binary field in the XML file.

Client successfully downloads, decodes, and opens 99% of the documents correctly.

However, we've started encountering some files that are failing when the client makes this call:

 byte[] buffer = Convert.FromBase64String(xNode["fileIMAGE"].InnerText);

System.FormatException-

Message="Invalid character in a Base-64 string."
Source="mscorlib"

We've written out the base64 blob from the XML file to a text file. I don't see any "\0" characters. I could post the whole blob, but it's quite large.

Any ideas?

sarnold
  • 102,305
  • 22
  • 181
  • 238
paparush
  • 1,340
  • 1
  • 17
  • 25
  • Any chance you could add some code to report all characters that aren't in `isalnum()`, `+`, or `/`? That ought to leave a big pile of newlines or crlf or similar, padding at the end, and your mystery character. – sarnold May 07 '11 at 02:03
  • I'm sitting here writing a test app to parse over the resulting chars to test that very thing. Trying to implement the solution offered here http://stackoverflow.com/questions/3355407/validate-string-is-base64-format-using-regex – paparush May 07 '11 at 02:09
  • There are = (equal signs) in the body of the base64 blob. I don't think that's a valid base64 character (A-Z a-z 0-9 + /) other than padding at the end. – paparush May 07 '11 at 03:13
  • @paparush, hrm, that `=` in the middle sounds troublesome. Can you try your encoding routines on _small_ inputs? Generate random byte strings between ten bytes and ten kilobytes, maybe you'll generate one by happy accident that shows the problem. – sarnold May 07 '11 at 04:00
  • 1
    Is it possible that the blob is multiple Base64 objects, each ending in the = padding? – Ray Henry May 07 '11 at 14:47
  • That's a good question, Ray. I'll start digging into that possibility on Monday. – paparush May 07 '11 at 17:28

1 Answers1

1

Issue Resolved

To stream the file from the server, we use a callback function to read/write chunks of the file. We were base64encoding each chunk. WRONG.

Resolution- Write all the chunks to a global memorystream object. At the end of the callbacks, then do the base64 encoding.

In the callback function:

 if (brData.ChunkNo == 1)
    {

        // Set the Content-type of the file
        if (brData.MimeType.Length < 1)
        {
            mimeType = "application/unknown";
        }
        else
        {
            mimeType = brData.MimeType;
        }

        msbase64Out = new MemoryStream();
    }


    if (brData.bytesJustRead > 0)
    {
        fileMS.WriteTo(msbase64Out);

    }


   if (brData.bytesRemaining < 1)
    {
        byte[] imgBytes = msbase64Out.ToArray();

        string img64 = Convert.ToBase64String(imgBytes);

        viewdocWriter.WriteString(img64);
    }

msbase64Out is a global memory stream that gets written to each time the callback is called. viewdocWriter is a global XML writer that is responsible for writing out the XML stream that gets sent to the client app.

paparush
  • 1,340
  • 1
  • 17
  • 25