You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Jason Robertson <ja...@veriprise.com> on 2001/01/29 22:59:21 UTC

Bug: Base64.decode

The utils class Base64 has a bug in the decode method - the byte array returned has the wrong length. This simple example shows the problem:

public class test
{
    public static void main( String args[] ) 
    {
        byte[] original = new byte[1];
        original[0] = 'A';
        
        byte[] encoded = org.apache.xerces.utils.Base64.encode( original );
        byte[] decoded = org.apache.xerces.utils.Base64.decode( encoded );
        
        if ( original.length != decoded.length )
        {
            System.out.println( "Sizes aren't the same!" );
            System.out.println( "Original: '" + new String( original ) + "'" );
            System.out.println( "Decoded : '" + new String( decoded ) + "'" );
        }
    }
}

The output is this:

     Sizes aren't the same!
     Original: 'A'
     Decoded : 'A   '

Note the extra spaces after the decoded value (actually zero-value bytes, I think). The problem in the code is that no adjustment is made for padding characters when the size of the output buffer is determined:

        decodedData      = new byte[ numberQuadruple*3 + 1 ];

Should be:

        decodedData      = new byte[ numberQuadruple*3 - numberPadCharacters ];

(where numberPadCharacters must be determined). 

Not really sure why the +1 is there on the current code, seems very C-ish in that the code is assuming it's a string and is making room for the null-terminating character.

Jason