2003/02/12 06:21:05
[org.ibex.core.git] / src / org / bouncycastle / util / encoders / Base64.java
index 423fb30..448e0ac 100644 (file)
@@ -2,7 +2,7 @@ package org.bouncycastle.util.encoders;
 
 public class Base64
 {
-       private static byte[] encodingTable =
+       private static final byte[] encodingTable =
                {
                    (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
             (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
@@ -27,8 +27,9 @@ public class Base64
                byte[]  data)
        {
                byte[]  bytes;
-
-               if ((data.length % 3) == 0)
+               
+               int modulus = data.length % 3;
+               if (modulus == 0)
                {
                        bytes = new byte[4 * data.length / 3];
                }
@@ -37,25 +38,18 @@ public class Base64
                        bytes = new byte[4 * ((data.length / 3) + 1)];
                }
 
-               for (int i = 0, j = 0;
-                               i < ((data.length / 3) * 3); i += 3, j += 4)
+        int dataLength = (data.length - modulus);
+               int a1, a2, a3;
+               for (int i = 0, j = 0; i < dataLength; i += 3, j += 4)
                {
-                       int     b1, b2, b3, b4;
-                       int     d1, d2, d3;
-
-                       d1 = data[i] & 0xff;
-                       d2 = data[i + 1] & 0xff;
-                       d3 = data[i + 2] & 0xff;
-
-                       b1 = (d1 >>> 2) & 0x3f;
-                       b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
-                       b3 = ((d2 << 2) | (d3 >>> 6)) & 0x3f;
-                       b4 = d3 & 0x3f;
+                       a1 = data[i] & 0xff;
+                       a2 = data[i + 1] & 0xff;
+                       a3 = data[i + 2] & 0xff;
 
-                       bytes[j] = encodingTable[b1];
-                       bytes[j + 1] = encodingTable[b2];
-                       bytes[j + 2] = encodingTable[b3];
-                       bytes[j + 3] = encodingTable[b4];
+                       bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
+                       bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
+                       bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
+                       bytes[j + 3] = encodingTable[a3 & 0x3f];
                }
 
                /*
@@ -64,7 +58,7 @@ public class Base64
                int     b1, b2, b3;
                int     d1, d2;
 
-               switch (data.length % 3)
+               switch (modulus)
                {
                case 0:         /* nothing left to do */
                        break;
@@ -99,7 +93,7 @@ public class Base64
        /*
         * set up the decoding table.
         */
-       private static byte[] decodingTable;
+       private static final byte[] decodingTable;
 
        static
        {