You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by rh...@apache.org on 2003/01/29 00:58:08 UTC

cvs commit: xml-rpc/src/test/org/apache/xmlrpc Base64Test.java

rhoegg      2003/01/28 15:58:07

  Modified:    src/java/org/apache/xmlrpc Base64.java
               src/test/org/apache/xmlrpc Base64Test.java
  Log:
  PR: 9931
  Submitted by: Martin Redington / m.redington@acl.ac.uk
  Reviewed by: Ryan Hoegg / rhoegg@isisnetworks.net
  
  Updates Base64Test ro reflect whitespace problems mentioned
  in the bug report, and Base64 to fix the problem.
  
  Revision  Changes    Path
  1.5       +32 -8     xml-rpc/src/java/org/apache/xmlrpc/Base64.java
  
  Index: Base64.java
  ===================================================================
  RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/Base64.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Base64.java	1 Nov 2002 22:06:10 -0000	1.4
  +++ Base64.java	28 Jan 2003 23:58:07 -0000	1.5
  @@ -79,6 +79,9 @@
    */
   public final class  Base64
   {
  +    static final int CHUNK_SIZE = 76;
  +    static final byte[] CHUNK_SEPARATOR = "\n".getBytes();
  +
       static private final int  BASELENGTH         = 255;
       static private final int  LOOKUPLENGTH       = 64;
       static private final int  TWENTYFOURBITGROUP = 24;
  @@ -167,24 +170,34 @@
           int      fewerThan24bits   = lengthDataBits%TWENTYFOURBITGROUP;
           int      numberTriplets    = lengthDataBits/TWENTYFOURBITGROUP;
           byte     encodedData[]     = null;
  -
  +	int      encodedDataLength = 0;
   
           if (fewerThan24bits != 0)
           {
               //data not divisible by 24 bit
  -            encodedData = new byte[ (numberTriplets + 1 ) * 4 ];
  +            encodedDataLength = (numberTriplets + 1 ) * 4;
           }
           else
           {
               // 16 or 8 bit
  -            encodedData = new byte[ numberTriplets * 4 ];
  +            encodedDataLength = numberTriplets * 4;
           }
   
  +	// allow extra length for the separator
  +        int nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 :
  +                         (int) Math.ceil((float) encodedDataLength / CHUNK_SIZE));
  +
  +	encodedDataLength += (nbrChunks - 1) * CHUNK_SEPARATOR.length;
  +	encodedData = new byte[encodedDataLength];
  +
           byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
   
           int encodedIndex = 0;
           int dataIndex   = 0;
           int i           = 0;
  +	int nextSeparatorIndex = CHUNK_SIZE;
  +	int chunksSoFar = 0;
  +
           //log.debug("number of triplets = " + numberTriplets);
           for ( i = 0; i<numberTriplets; i++ )
           {
  @@ -198,7 +211,6 @@
               l  = (byte)(b2 & 0x0f);
               k  = (byte)(b1 & 0x03);
   
  -            encodedIndex = i * 4;
               byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
               byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
               byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
  @@ -212,11 +224,23 @@
               encodedData[encodedIndex+2] =
                   lookUpBase64Alphabet[ (l <<2 ) | val3 ];
               encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
  +
  +	    encodedIndex += 4;
  +
  +	    // this assumes that CHUNK_SIZE % 4 == 0
  +	    if(encodedIndex == nextSeparatorIndex){
  +		System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
  +                                 encodedIndex, CHUNK_SEPARATOR.length);
  +		chunksSoFar++;
  +		nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1)) + 
  +                                     (chunksSoFar * CHUNK_SEPARATOR.length);
  +		encodedIndex += CHUNK_SEPARATOR.length;
  +	    }
           }
   
           // form integral number of 6-bit groups
           dataIndex    = i*3;
  -        encodedIndex = i*4;
  +
           if (fewerThan24bits == EIGHTBIT )
           {
               b1 = binaryData[dataIndex];
  
  
  
  1.8       +2 -3      xml-rpc/src/test/org/apache/xmlrpc/Base64Test.java
  
  Index: Base64Test.java
  ===================================================================
  RCS file: /home/cvs/xml-rpc/src/test/org/apache/xmlrpc/Base64Test.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Base64Test.java	1 Nov 2002 22:06:10 -0000	1.7
  +++ Base64Test.java	28 Jan 2003 23:58:07 -0000	1.8
  @@ -131,9 +131,8 @@
                   assertEquals(TEST_DATA[i], new String(decoded));
               }
   
  -            // FIXME: The Base64.encode() function doesn't wrap at 76 chars.
  -            //assertEquals(Base64.encode(UNENCODED.getBytes()),
  -            //             ENCODED.getBytes());
  +            assertEquals(Base64.encode(UNENCODED.getBytes()),
  +                         ENCODED.getBytes());
               assertEquals(UNENCODED.getBytes(),
                            Base64.decode(ENCODED.getBytes()));
           }