You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Arne Georg Gleditsch (Commented) (JIRA)" <ji...@apache.org> on 2012/03/01 15:29:56 UTC

[jira] [Commented] (THRIFT-1156) Bug with Base 64 coding in TBase64Utils

    [ https://issues.apache.org/jira/browse/THRIFT-1156?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13220063#comment-13220063 ] 

Arne Georg Gleditsch commented on THRIFT-1156:
----------------------------------------------

The following patch appears to fix the issue:

{noformat}
--- org/apache/thrift/protocol/TBase64Utils.java.orig	2011-11-26 20:00:38.000000000 +0100
+++ org/apache/thrift/protocol/TBase64Utils.java	2012-03-01 15:27:48.510651281 +0100
@@ -56,17 +56,17 @@
     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) & 0xF)));
       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) & 0xF)));
       dst[dstOff + 2] =
         (byte)ENCODE_TABLE.charAt((src[srcOff+1] << 2) & 0x3F);

{noformat}

                
> Bug with Base 64 coding in TBase64Utils
> ---------------------------------------
>
>                 Key: THRIFT-1156
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1156
>             Project: Thrift
>          Issue Type: Bug
>          Components: Java - Library
>    Affects Versions: 0.6
>         Environment: Java SE, Android
>            Reporter: Głowka Rafał
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> There are some problems with coding byte[] when values are less that 0.
> The example code :
> 	public static void main(String[] args) 
> 	{
> 		byte[] data = new byte[256];
> 		byte[] data2 = new byte[512];
> 		for (int i = 0; i < 512; i++)
> 			data2[i] = 0;
> 		for (byte b = -128; b < 127; b++)
> 			data[b + 128] = b;
> 		int len = 256;
> 		int off = 0;
> 		int off2 = 0;
> 		while (len >= 3) {
> 			// Encode 3 bytes at a time
> 			TBase64Utils.encode(data, off, 3, data2, off2);
> 			off += 3;
> 			off2 += 4;
> 			len -= 3;
> 		}
> 		if (len > 0) {
> 			// Encode remainder
> 			TBase64Utils.encode(data, off, len, data2, off2);
> 		}
> 		len = 256;
> 		off = 0;
> 		off2 = 0;
> 		while (len >= 3) {
> 			// Encode 3 bytes at a time
> 			TBase64Utils.decode(data2, off2, 4, data, off);
> 			off += 3;
> 			len -= 3;
> 			off2 += 4;
> 		}
> 		if (len > 0) {
> 			// Encode remainder
> 			TBase64Utils.decode(data2, off2, len, data, off);
> 		}
> 		for (byte b = -128; b < 127; b++)
> 			System.out.println("should be : " + (b) + " is : " + data[b + 128]);
> 	}
> The results : 
> ...
> should be : -18 is : -18
> should be : -17 is : -18
> should be : -16 is : -1
> should be : -15 is : -15
> should be : -14 is : -15
> should be : -13 is : -14
> should be : -12 is : -12
> should be : -11 is : -12
> should be : -10 is : -11
> should be : -9 is : -9
> should be : -8 is : -5
> should be : -7 is : -8
> should be : -6 is : -6
> should be : -5 is : -6
> should be : -4 is : -5
> should be : -3 is : -3
> should be : -2 is : -3
> should be : -1 is : -1
> should be : 0 is : 0
> should be : 1 is : 1
> ...

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira