Make utf-8 conversion more robust to bad input.

This commit is contained in:
James Roseborough
2008-01-14 23:11:09 +00:00
parent 2461b46908
commit 8570761928
2 changed files with 9 additions and 5 deletions

View File

@@ -102,21 +102,24 @@ public class LString extends LValue {
*/
public String toJavaString() {
char[] c = new char[m_length];
int n = 0;
int n=0, p=0;
int b;
for ( int i=0; i<m_length; i++ ) {
switch ( (b = m_bytes[m_offset+i]) & 0xe0 ) {
default:
if ( b == 0 )
return new String( c, 0, n );
c[n++] = (char) (0xff & b);
c[p=n++] = (char) (0xff & b);
break;
case 0x80:
case 0xa0:
c[p] = (char) ((c[p] << 6) | ( b & 0x3f ));
break;
case 0xc0:
c[n++] = (char) (((b & 0x1f) << 6) | ( m_bytes[m_offset+(++i)] & 0x3f));
c[p=n++] = (char) (b & 0x1f);
break;
case 0xe0:
c[n++] = (char) (((b & 0xf) << 12) | ((m_bytes[m_offset+i+1] & 0x3f) << 6) | (m_bytes[m_offset+i+2] & 0x3f));
i += 2;
c[p=n++] = (char) (b & 0xf);
break;
}
}

View File

@@ -76,6 +76,7 @@ public class LStringTest extends TestCase {
assertEquals( userFriendly( before ), userFriendly( after ) );
}
public void testNullTerminated() {
char[] c = { 'a', 'b', 'c', '\0', 'd', 'e', 'f' };
String before = new String(c);