Base64 encode/decode bug using xerces-2_9_0 while processing binary files. 
---------------------------------------------------------------------------

                 Key: XERCESJ-1222
                 URL: http://issues.apache.org/jira/browse/XERCESJ-1222
             Project: Xerces2-J
          Issue Type: Bug
    Affects Versions: 2.9.0
         Environment: Microsoft Windows XP [Version 5.1.2600]
            Reporter: Venkat Ganesh


Hi, 

The program Base64EncodeDecode.java shown below, is using 
org.apache.xerces.impl.dv.util.Base64 class for base64 encode/decode. It works 
well with ASCII files. However, it fails with binary files. 

Thanks. 

Testing.
======
Make sure xerces jar files are in the classpath. 

Testing with ASCII files. 
==================
Encode a text file as shown below: 

java Base64EncodeDecode encode foo.xml foo_encoded.xml

Decode the encoded file as shown below: 
java Base64EncodeDecode decode foo_encoded.xml foo_decoded.xml

Verified foo.xml and foo_decoded.xml are identical, as expected. 

Testing with binary files. 
==================
Encode a binary file as shown below: 
java Base64EncodeDecode encode procexp.exe procexp_encoded.exe

Decode the encoded file as shown below: 
java Base64EncodeDecode decode procexp_encoded.exe procexp_decoded.exe

Verify you cannot double click and open the decoded exe file. You will see the 
error message that the decoded exe file is not a valid Win32 application even 
though the original executable (procexp.exe) is a valid executable. The number 
of bytes and size of the original and decoded executables are same; however, 
the third byte in procexp.exe is ox90 while it is 0x3f in the 
procexp_decoded.exe, when viewed using a hex editor. There are many more 
differences. 

I am able to reproduce this issue with xerces-2_9_0 as well as xerces-2_7_1 jar 
files. Is this a known bug? 

How to encode/decode binary files using xerces jar files? 

Thanks and regards, 

V.Ganesh

[EMAIL PROTECTED] 

Base64EncodeDecode.java 
======================

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.xerces.impl.dv.util.Base64;

public class Base64EncodeDecode {
    public static void main(String[] args) throws IOException {
        if ( args.length != 3 )
        {
            System.out.println("Usage java Base64EncodeDecode encode|decode 
<input-file-name> <output-file-name>");
            System.out.println("You can use this program to encode binary files 
also.");
            return;
        }
        StringBuffer sb=new StringBuffer(10000);
        FileInputStream inFile = null;
        FileOutputStream outFile = null;
        try {
            inFile = new FileInputStream(args[1]);
            outFile = new FileOutputStream(args[2]);
            int byteRead=0;
            int ctr=0;
            int numberOfChars=0;
            while ((byteRead = inFile.read()) != -1) {
               sb.append((char ) byteRead);
               numberOfChars++;
            }
            if ( args[0].equalsIgnoreCase("encode") == true )
            {
                String tmpString=sb.toString();
                String outString=Base64.encode(tmpString.getBytes());
                if ( outString == null )
                {
                  System.out.println("Failed- Not able to encode. ");
                } else {
                  outFile.write(outString.getBytes());
                }
            } else {
                String tmpString=sb.toString();
                byte outByteArray[] =Base64.decode(tmpString);
                if ( outByteArray == null )
                {
                  System.out.println("Failed- Not able to decode. ");
                } else {
                  outFile.write(outByteArray);
                }
            }
        } finally {
            if (inFile != null) {
                inFile.close();
            }
            if (outFile != null) {
                outFile.close();
            }
        }
    }
}



-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to