You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2012/05/27 20:05:16 UTC

svn commit: r1343074 - in /thrift/trunk/lib: cpp/src/thrift/protocol/ cpp/test/ csharp/src/Protocol/ java/src/org/apache/thrift/protocol/ java/test/org/apache/thrift/protocol/ javame/src/org/apache/thrift/protocol/

Author: roger
Date: Sun May 27 18:05:16 2012
New Revision: 1343074

URL: http://svn.apache.org/viewvc?rev=1343074&view=rev
Log:
THRIFT-1612 Base64 encoding is broken
Patch: Andrew Cox

Added:
    thrift/trunk/lib/cpp/test/Base64Test.cpp
Modified:
    thrift/trunk/lib/cpp/src/thrift/protocol/TBase64Utils.cpp
    thrift/trunk/lib/cpp/test/Makefile.am
    thrift/trunk/lib/csharp/src/Protocol/TBase64Utils.cs
    thrift/trunk/lib/java/src/org/apache/thrift/protocol/TBase64Utils.java
    thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
    thrift/trunk/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java

Modified: thrift/trunk/lib/cpp/src/thrift/protocol/TBase64Utils.cpp
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/thrift/protocol/TBase64Utils.cpp?rev=1343074&r1=1343073&r2=1343074&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/thrift/protocol/TBase64Utils.cpp (original)
+++ thrift/trunk/lib/cpp/src/thrift/protocol/TBase64Utils.cpp Sun May 27 18:05:16 2012
@@ -30,16 +30,16 @@ static const uint8_t *kBase64EncodeTable
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
 void  base64_encode(const uint8_t *in, uint32_t len, uint8_t *buf) {
-  buf[0] = kBase64EncodeTable[(in[0] >> 2) & 0x3F];
+  buf[0] = kBase64EncodeTable[(in[0] >> 2) & 0x3f];
   if (len == 3) {
-    buf[1] = kBase64EncodeTable[((in[0] << 4) + (in[1] >> 4)) & 0x3f];
-    buf[2] = kBase64EncodeTable[((in[1] << 2) + (in[2] >> 6)) & 0x3f];
+    buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)];
+    buf[2] = kBase64EncodeTable[((in[1] << 2) & 0x3c) | ((in[2] >> 6) & 0x03)];
     buf[3] = kBase64EncodeTable[in[2] & 0x3f];
   } else if (len == 2) {
-    buf[1] = kBase64EncodeTable[((in[0] << 4) + (in[1] >> 4)) & 0x3f];
-    buf[2] = kBase64EncodeTable[(in[1] << 2) & 0x3f];
+    buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)];
+    buf[2] = kBase64EncodeTable[(in[1] << 2) & 0x3c];
   } else  { // len == 1
-    buf[1] = kBase64EncodeTable[(in[0] << 4) & 0x3f];
+    buf[1] = kBase64EncodeTable[(in[0] << 4) & 0x30];
   }
 }
 

Added: thrift/trunk/lib/cpp/test/Base64Test.cpp
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/test/Base64Test.cpp?rev=1343074&view=auto
==============================================================================
--- thrift/trunk/lib/cpp/test/Base64Test.cpp (added)
+++ thrift/trunk/lib/cpp/test/Base64Test.cpp Sun May 27 18:05:16 2012
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <boost/test/auto_unit_test.hpp>
+#include <thrift/protocol/TBase64Utils.h>
+
+using apache::thrift::protocol::base64_encode;
+using apache::thrift::protocol::base64_decode;
+
+BOOST_AUTO_TEST_SUITE( Base64Test )
+
+void setupTestData(int i, uint8_t* data, int& len) {
+  len = 0;
+  do {
+    data[len] = (uint8_t)(i & 0xFF);
+    i >>= 8;
+    len++;
+  } while ((len < 3) && (i != 0));
+
+  BOOST_ASSERT(i == 0);
+}
+
+void checkEncoding(uint8_t* data, int len) {
+  for (int i = 0; i < len; i++) {
+    BOOST_ASSERT(isalnum(data[i]) || data[i] == '/' || data[i] == '+');
+  }
+}
+
+BOOST_AUTO_TEST_CASE( test_Base64_Encode_Decode ) {
+  int len;
+  uint8_t testInput[3];
+  uint8_t testOutput[4];
+
+  // Test all possible encoding / decoding cases given the
+  // three byte limit for base64_encode.
+
+  for (int i = 0xFFFFFF; i >= 0; i--) {
+
+    // fill testInput based on i
+    setupTestData(i, testInput, len);
+
+    // encode the test data, then decode it again
+    base64_encode(testInput, len, testOutput);
+
+    // verify each byte has a valid Base64 value (alphanumeric or either + or /)
+    checkEncoding(testOutput, len);
+
+    // decode output and check that it matches input
+    base64_decode(testOutput, len + 1);
+    BOOST_ASSERT(0 == memcmp(testInput, testOutput, len));
+
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END()

Modified: thrift/trunk/lib/cpp/test/Makefile.am
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/test/Makefile.am?rev=1343074&r1=1343073&r2=1343074&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/test/Makefile.am (original)
+++ thrift/trunk/lib/cpp/test/Makefile.am Sun May 27 18:05:16 2012
@@ -76,7 +76,8 @@ TESTS = \
 UnitTests_SOURCES = \
 	UnitTestMain.cpp \
 	TMemoryBufferTest.cpp \
-	TBufferBaseTest.cpp
+	TBufferBaseTest.cpp \
+	Base64Test.cpp
 
 if !WITH_BOOSTTHREADS
 UnitTests_SOURCES += \

Modified: thrift/trunk/lib/csharp/src/Protocol/TBase64Utils.cs
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/csharp/src/Protocol/TBase64Utils.cs?rev=1343074&r1=1343073&r2=1343074&view=diff
==============================================================================
--- thrift/trunk/lib/csharp/src/Protocol/TBase64Utils.cs (original)
+++ thrift/trunk/lib/csharp/src/Protocol/TBase64Utils.cs Sun May 27 18:05:16 2012
@@ -34,10 +34,10 @@ namespace Thrift.Protocol
 			{
 				dst[dstOff + 1] =
 					(byte)ENCODE_TABLE[
-						((src[srcOff] << 4) + (src[srcOff + 1] >> 4)) & 0x3F];
+						((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
 				dst[dstOff + 2] =
 					(byte)ENCODE_TABLE[
-						((src[srcOff + 1] << 2) + (src[srcOff + 2] >> 6)) & 0x3F];
+						((src[srcOff + 1] << 2) & 0x3C) | ((src[srcOff + 2] >> 6) & 0x03)];
 				dst[dstOff + 3] =
 					(byte)ENCODE_TABLE[src[srcOff + 2] & 0x3F];
 			}
@@ -45,15 +45,15 @@ namespace Thrift.Protocol
 			{
 				dst[dstOff + 1] =
 					(byte)ENCODE_TABLE[
-						((src[srcOff] << 4) + (src[srcOff + 1] >> 4)) & 0x3F];
+						((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
 				dst[dstOff + 2] =
-					(byte)ENCODE_TABLE[(src[srcOff + 1] << 2) & 0x3F];
+					(byte)ENCODE_TABLE[(src[srcOff + 1] << 2) & 0x3C];
 
 			}
 			else
 			{ // len == 1) {
 				dst[dstOff + 1] =
-					(byte)ENCODE_TABLE[(src[srcOff] << 4) & 0x3F];
+					(byte)ENCODE_TABLE[(src[srcOff] << 4) & 0x30];
 			}
 		}
 

Modified: thrift/trunk/lib/java/src/org/apache/thrift/protocol/TBase64Utils.java
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/java/src/org/apache/thrift/protocol/TBase64Utils.java?rev=1343074&r1=1343073&r2=1343074&view=diff
==============================================================================
--- thrift/trunk/lib/java/src/org/apache/thrift/protocol/TBase64Utils.java (original)
+++ thrift/trunk/lib/java/src/org/apache/thrift/protocol/TBase64Utils.java Sun May 27 18:05:16 2012
@@ -56,24 +56,23 @@ class TBase64Utils {
     if (len == 3) {
       dst[dstOff + 1] =
         (byte)ENCODE_TABLE.charAt(
-                         ((src[srcOff] << 4) + (src[srcOff+1] >> 4)) & 0x3F);
+                         ((src[srcOff] << 4) & 0x30) | ((src[srcOff+1] >> 4) & 0x0F));
       dst[dstOff + 2] =
         (byte)ENCODE_TABLE.charAt(
-                         ((src[srcOff+1] << 2) + (src[srcOff+2] >> 6)) & 0x3F);
+                         ((src[srcOff+1] << 2) & 0x3C) | ((src[srcOff+2] >> 6) & 0x03));
       dst[dstOff + 3] =
         (byte)ENCODE_TABLE.charAt(src[srcOff+2] & 0x3F);
     }
     else if (len == 2) {
       dst[dstOff+1] =
         (byte)ENCODE_TABLE.charAt(
-                          ((src[srcOff] << 4) + (src[srcOff+1] >> 4)) & 0x3F);
+                          ((src[srcOff] << 4) & 0x30) | ((src[srcOff+1] >> 4) & 0x0F));
       dst[dstOff + 2] =
-        (byte)ENCODE_TABLE.charAt((src[srcOff+1] << 2) & 0x3F);
-
+        (byte)ENCODE_TABLE.charAt((src[srcOff+1] << 2) & 0x3C);
     }
     else { // len == 1) {
       dst[dstOff + 1] =
-        (byte)ENCODE_TABLE.charAt((src[srcOff] << 4) & 0x3F);
+        (byte)ENCODE_TABLE.charAt((src[srcOff] << 4) & 0x30);
     }
   }
 

Modified: thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java?rev=1343074&r1=1343073&r2=1343074&view=diff
==============================================================================
--- thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java (original)
+++ thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java Sun May 27 18:05:16 2012
@@ -74,7 +74,13 @@ public abstract class ProtocolTestBase e
   }
 
   public void testBinary() throws Exception {
-    for (byte[] b : Arrays.asList(new byte[0], new byte[]{0,1,2,3,4,5,6,7,8,9,10}, new byte[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}, new byte[128])) {
+    for (byte[] b : Arrays.asList(new byte[0],
+                                  new byte[]{0,1,2,3,4,5,6,7,8,9,10},
+                                  new byte[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14},
+                                  new byte[]{0x5D},
+                                  new byte[]{(byte)0xD5,(byte)0x5D},
+                                  new byte[]{(byte)0xFF,(byte)0xD5,(byte)0x5D},
+                                  new byte[128])) {
       if (canBeUsedNaked()) {
         internalTestNakedBinary(b);
       }

Modified: thrift/trunk/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java?rev=1343074&r1=1343073&r2=1343074&view=diff
==============================================================================
--- thrift/trunk/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java (original)
+++ thrift/trunk/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java Sun May 27 18:05:16 2012
@@ -56,24 +56,23 @@ class TBase64Utils {
     if (len == 3) {
       dst[dstOff + 1] =
         (byte)ENCODE_TABLE.charAt(
-                         ((src[srcOff] << 4) + (src[srcOff+1] >> 4)) & 0x3F);
+                         ((src[srcOff] << 4) & 0x30) | ((src[srcOff+1] >> 4) & 0x0F));
       dst[dstOff + 2] =
         (byte)ENCODE_TABLE.charAt(
-                         ((src[srcOff+1] << 2) + (src[srcOff+2] >> 6)) & 0x3F);
+                         ((src[srcOff+1] << 2) & 0x3C) | ((src[srcOff+2] >> 6) & 0x03));
       dst[dstOff + 3] =
         (byte)ENCODE_TABLE.charAt(src[srcOff+2] & 0x3F);
     }
     else if (len == 2) {
       dst[dstOff+1] =
         (byte)ENCODE_TABLE.charAt(
-                          ((src[srcOff] << 4) + (src[srcOff+1] >> 4)) & 0x3F);
+                          ((src[srcOff] << 4) & 0x30) | ((src[srcOff+1] >> 4) & 0x0F));
       dst[dstOff + 2] =
-        (byte)ENCODE_TABLE.charAt((src[srcOff+1] << 2) & 0x3F);
-
+        (byte)ENCODE_TABLE.charAt((src[srcOff+1] << 2) & 0x3C);
     }
     else { // len == 1) {
       dst[dstOff + 1] =
-        (byte)ENCODE_TABLE.charAt((src[srcOff] << 4) & 0x3F);
+        (byte)ENCODE_TABLE.charAt((src[srcOff] << 4) & 0x30);
     }
   }