THRIFT-2365 C# decodes too many binary bytes from JSON
Patch: Jens Geyer
diff --git a/lib/csharp/src/Protocol/TJSONProtocol.cs b/lib/csharp/src/Protocol/TJSONProtocol.cs
index 9c62bec..e1d0e78 100644
--- a/lib/csharp/src/Protocol/TJSONProtocol.cs
+++ b/lib/csharp/src/Protocol/TJSONProtocol.cs
@@ -737,7 +737,7 @@
// escaped?
if (ch != ESCSEQ[0])
- {
+ {
buffer.Write(new byte[] { (byte)ch }, 0, 1);
continue;
}
@@ -752,20 +752,20 @@
throw new TProtocolException(TProtocolException.INVALID_DATA,
"Expected control char");
}
- ch = ESCAPE_CHAR_VALS[off];
- buffer.Write(new byte[] { (byte)ch }, 0, 1);
+ ch = ESCAPE_CHAR_VALS[off];
+ buffer.Write(new byte[] { (byte)ch }, 0, 1);
continue;
- }
-
-
- // it's \uXXXX
- trans.ReadAll(tempBuffer, 0, 4);
- var wch = (short)((HexVal((byte)tempBuffer[0]) << 12) +
- (HexVal((byte)tempBuffer[1]) << 8) +
- (HexVal((byte)tempBuffer[2]) << 4) +
- HexVal(tempBuffer[3]));
- var tmp = utf8Encoding.GetBytes(new char[] { (char)wch });
- buffer.Write(tmp, 0, tmp.Length);
+ }
+
+
+ // it's \uXXXX
+ trans.ReadAll(tempBuffer, 0, 4);
+ var wch = (short)((HexVal((byte)tempBuffer[0]) << 12) +
+ (HexVal((byte)tempBuffer[1]) << 8) +
+ (HexVal((byte)tempBuffer[2]) << 4) +
+ HexVal(tempBuffer[3]));
+ var tmp = utf8Encoding.GetBytes(new char[] { (char)wch });
+ buffer.Write(tmp, 0, tmp.Length);
}
return buffer.ToArray();
}
@@ -891,7 +891,13 @@
int len = b.Length;
int off = 0;
int size = 0;
- while (len >= 4)
+ // reduce len to ignore fill bytes
+ while ((len > 0) && (b[len - 1] == '='))
+ {
+ --len;
+ }
+ // read & decode full byte triplets = 4 source bytes
+ while (len > 4)
{
// Decode 4 bytes at a time
TBase64Utils.decode(b, off, 4, b, size); // NB: decoded in place