You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/06/29 22:20:01 UTC

svn commit: r789436 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/util/Base64.java test/org/apache/commons/runtime/TestStrings.java

Author: mturk
Date: Mon Jun 29 20:20:00 2009
New Revision: 789436

URL: http://svn.apache.org/viewvc?rev=789436&view=rev
Log:
Allow wrapped lines when decoding

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Base64.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Base64.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Base64.java?rev=789436&r1=789435&r2=789436&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Base64.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Base64.java Mon Jun 29 20:20:00 2009
@@ -37,6 +37,7 @@
      */
     public  static final int    COLS    = 72;
     private static final char   padding = '=';
+    private static final char   LF      = '\n';
     private static final char[] basis64 = {
         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
@@ -49,22 +50,22 @@
 
     // ASCII table
     private static final byte[] pr2six = {
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
-        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
-        64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
-        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
-        64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
-        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 63, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 62, 65, 65, 65, 64,
+        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 65, 65, 65, 65, 65, 65,
+        65,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
+        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 65, 65, 65, 65, 65,
+        65, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+        65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65
     };
 
     /**
@@ -126,7 +127,7 @@
             dst[dp++] = (byte)basis64[out[3]];
             if (wrap > 0) {
                 if (++oc % (wrap / 4) == 0)
-                    dst[dp++] = '\n';
+                    dst[dp++] = LF;
             }
         }
         // Pad with remaining 1 or 2 bytes
@@ -185,7 +186,7 @@
             dst[dp++] = basis64[out[3]];
             if (wrap > 0) {
                 if (++oc % (wrap / 4) == 0)
-                    dst[dp++] = '\n';
+                    dst[dp++] = LF;
             }
         }
         // Pad with remaining 1 or 2 bytes
@@ -263,30 +264,40 @@
     public static int decodeLength(byte[] src, int srcPos, int len)
     {
         int sc = 0;
+        int lf = 0;
         int nprbytes;
 
-        while (sc < len && pr2six[(int)src[srcPos + sc++] & 0xFF] <= 63);
-
-        nprbytes = sc - 1;
-        return ((nprbytes + 3) / 4) * 3;
+        while (sc < len) {
+            int ch = (int)src[srcPos + sc] & 0xFF;
+             if (pr2six[ch] == 64)
+                 lf++;
+             if (pr2six[ch] == 65)
+                 break;
+            sc ++;
+        }
+        nprbytes = sc;
+        return ((nprbytes + 3) / 4) * 3 - lf;
     }
 
     public static int decode(byte[] src, int srcPos, byte[] dst,
                              int dstPos, int len)
         throws IndexOutOfBoundsException, NullPointerException
     {
-        int nd;
         int sc = 0;
         int sp = srcPos;
         int dp = dstPos;
         int nprbytes;
 
-        while (sc < len && pr2six[(int)src[srcPos + sc++] & 0xFF] <= 63);
+        while (sc < len && pr2six[(int)src[srcPos + sc++] & 0xFF] <= 64);
 
         nprbytes = sc - 1;
-        nd = ((nprbytes + 3) / 4) * 3;
 
         while (nprbytes > 4) {
+            if (src[sp + 0] == LF) {
+                sp += 1;
+                nprbytes -= 1;
+                continue;
+            }
             dst[dp++] = (byte)(pr2six[src[sp + 0]] << 2 | pr2six[src[sp + 1]] >> 4);
             dst[dp++] = (byte)(pr2six[src[sp + 1]] << 4 | pr2six[src[sp + 2]] >> 2);
             dst[dp++] = (byte)(pr2six[src[sp + 2]] << 6 | pr2six[src[sp + 3]]);
@@ -302,8 +313,7 @@
         if (nprbytes > 3)
             dst[dp++] = (byte)(pr2six[src[sp + 2]] << 6 | pr2six[src[sp + 3]]);
 
-        nd -= (4 - nprbytes) & 3;
-        return nd;
+        return dp - dstPos;
     }
 
     public static String decode(String string)

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java?rev=789436&r1=789435&r2=789436&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java Mon Jun 29 20:20:00 2009
@@ -276,6 +276,8 @@
                                   Base64.decode("QQ=="));
         assertEquals("Base64 S", "Base 64 encoding",
                                   Base64.decode("QmFzZSA2NCBlbmNvZGluZw=="));
+        assertEquals("Base64 S", "Base 64 encoding",
+                                  Base64.decode("QmFzZSA2\n\nNCBl\nbmNv\nZGluZw=="));
     }
 
 }