From 4c23ceca82b43c907c437370a9ee1eeafd83895e Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Tue, 12 Jan 2016 17:55:38 +0000 Subject: [PATCH] Fixed stupid bugs in metadata encoder and parser. --- .../data/MetadataEncoderImpl.java | 60 +++++++++++-------- .../briarproject/data/MetadataParserImpl.java | 10 ++-- .../data/MetadataEncoderParserImplTest.java | 4 +- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java b/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java index fc7dc3f9dc..f476df1134 100644 --- a/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java +++ b/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java @@ -68,7 +68,7 @@ class MetadataEncoderImpl implements MetadataEncoder { private void encodeInteger(ByteArrayOutputStream out, byte i) { out.write(INT_8); - out.write(i); + encodeInt8(out, i); } private void encodeInteger(ByteArrayOutputStream out, short i) { @@ -76,8 +76,7 @@ class MetadataEncoderImpl implements MetadataEncoder { encodeInteger(out, (byte) i); } else { out.write(INT_16); - out.write((byte) (i >> 8)); - out.write((byte) ((i << 8) >> 8)); + encodeInt16(out, i); } } @@ -86,10 +85,7 @@ class MetadataEncoderImpl implements MetadataEncoder { encodeInteger(out, (short) i); } else { out.write(INT_32); - out.write((byte) (i >> 24)); - out.write((byte) ((i << 8) >> 24)); - out.write((byte) ((i << 16) >> 24)); - out.write((byte) ((i << 24) >> 24)); + encodeInt32(out, i); } } @@ -98,24 +94,27 @@ class MetadataEncoderImpl implements MetadataEncoder { encodeInteger(out, (int) i); } else { out.write(INT_64); - out.write((byte) (i >> 56)); - out.write((byte) ((i << 8) >> 56)); - out.write((byte) ((i << 16) >> 56)); - out.write((byte) ((i << 24) >> 56)); - out.write((byte) ((i << 32) >> 56)); - out.write((byte) ((i << 40) >> 56)); - out.write((byte) ((i << 48) >> 56)); - out.write((byte) ((i << 56) >> 56)); + encodeInt64(out, i); } } - private void encodeFloat(ByteArrayOutputStream out, float f) { - encodeFloat(out, (double) f); + private void encodeInt8(ByteArrayOutputStream out, byte i) { + out.write(i); } - private void encodeFloat(ByteArrayOutputStream out, double d) { - long i = Double.doubleToLongBits(d); - out.write(FLOAT_64); + private void encodeInt16(ByteArrayOutputStream out, short i) { + out.write((byte) (i >> 8)); + out.write((byte) ((i << 8) >> 8)); + } + + private void encodeInt32(ByteArrayOutputStream out, int i) { + out.write((byte) (i >> 24)); + out.write((byte) ((i << 8) >> 24)); + out.write((byte) ((i << 16) >> 24)); + out.write((byte) ((i << 24) >> 24)); + } + + private void encodeInt64(ByteArrayOutputStream out, long i) { out.write((byte) (i >> 56)); out.write((byte) ((i << 8) >> 56)); out.write((byte) ((i << 16) >> 56)); @@ -126,17 +125,26 @@ class MetadataEncoderImpl implements MetadataEncoder { out.write((byte) ((i << 56) >> 56)); } + private void encodeFloat(ByteArrayOutputStream out, float f) { + encodeFloat(out, (double) f); + } + + private void encodeFloat(ByteArrayOutputStream out, double d) { + out.write(FLOAT_64); + encodeInt64(out, Double.doubleToLongBits(d)); + } + private void encodeString(ByteArrayOutputStream out, String s) { byte[] b = StringUtils.toUtf8(s); if (b.length <= Byte.MAX_VALUE) { out.write(STRING_8); - encodeInteger(out, (byte) b.length); + encodeInt8(out, (byte) b.length); } else if (b.length <= Short.MAX_VALUE) { out.write(STRING_16); - encodeInteger(out, (short) b.length); + encodeInt16(out, (short) b.length); } else { out.write(STRING_32); - encodeInteger(out, b.length); + encodeInt32(out, b.length); } out.write(b, 0, b.length); } @@ -144,13 +152,13 @@ class MetadataEncoderImpl implements MetadataEncoder { private void encodeRaw(ByteArrayOutputStream out, byte[] b) { if (b.length <= Byte.MAX_VALUE) { out.write(RAW_8); - encodeInteger(out, (byte) b.length); + encodeInt8(out, (byte) b.length); } else if (b.length <= Short.MAX_VALUE) { out.write(RAW_16); - encodeInteger(out, (short) b.length); + encodeInt16(out, (short) b.length); } else { out.write(RAW_32); - encodeInteger(out, b.length); + encodeInt32(out, b.length); } out.write(b, 0, b.length); } diff --git a/briar-core/src/org/briarproject/data/MetadataParserImpl.java b/briar-core/src/org/briarproject/data/MetadataParserImpl.java index e4624dc603..ce4c71a3a1 100644 --- a/briar-core/src/org/briarproject/data/MetadataParserImpl.java +++ b/briar-core/src/org/briarproject/data/MetadataParserImpl.java @@ -143,16 +143,18 @@ class MetadataParserImpl implements MetadataParser { private BdfList parseList(ByteArrayInputStream in) throws FormatException { BdfList list = new BdfList(); - while (peek(in) != END) list.add(parseObject(in)); - if (in.read() != END) throw new FormatException(); + for (int b = peek(in); b != -1 && b != (END & 0xFF); b = peek(in)) + list.add(parseObject(in)); + if (in.read() != (END & 0xFF)) throw new FormatException(); return list; } private BdfDictionary parseDictionary(ByteArrayInputStream in) throws FormatException { BdfDictionary dict = new BdfDictionary(); - while (peek(in) != END) dict.put(parseString(in), parseObject(in)); - if (in.read() != END) throw new FormatException(); + for (int b = peek(in); b != -1 && b != (END & 0xFF); b = peek(in)) + dict.put(parseString(in), parseObject(in)); + if (in.read() != (END & 0xFF)) throw new FormatException(); return dict; } diff --git a/briar-tests/src/org/briarproject/data/MetadataEncoderParserImplTest.java b/briar-tests/src/org/briarproject/data/MetadataEncoderParserImplTest.java index 038ac23c12..ca44fa0255 100644 --- a/briar-tests/src/org/briarproject/data/MetadataEncoderParserImplTest.java +++ b/briar-tests/src/org/briarproject/data/MetadataEncoderParserImplTest.java @@ -86,7 +86,7 @@ public class MetadataEncoderParserImplTest extends BriarTestCase { d.put("test", b); Metadata metadata = e.encode(d); - assertEquals(p.parse(metadata).getRaw("test", null), b); + assertArrayEquals(p.parse(metadata).getRaw("test", null), b); } @Test @@ -149,7 +149,7 @@ public class MetadataEncoderParserImplTest extends BriarTestCase { assertEquals(p.parse(metadata).getDictionary("test", null) .getList("Two", null).get(0), "\u0080"); assertEquals(p.parse(metadata).getDictionary("test", null) - .getList("Two", null).get(0), "\uD800\uDC00"); + .getList("Two", null).get(1), "\uD800\uDC00"); assertEquals(p.parse(metadata).getDictionary("another test", null) .getBoolean("should be true", false), true); -- GitLab