From f10820769836b265266c75763ebbd798bd52db09 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Fri, 25 Jun 2010 01:24:11 +0000 Subject: [PATCH] * Fixed a bug in BitPack where existing 1 bits were not being overwritten by new 0 bits * Added BitPack.PackBit() * Made Binary LLSD deserialization more lenient when parsing the header. Tests pass again git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@3364 52acb1d6-8a22-11de-b505-999d5b087335 --- .../LLSD/BinaryLLSD.cs | 8 +++++--- OpenMetaverse.Tests/BinaryLLSDTests.cs | 2 +- OpenMetaverse.Tests/TypeTests.cs | 16 +++++++++++++++ OpenMetaverse/BitPack.cs | 20 ++++++++++++++++++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/OpenMetaverse.StructuredData/LLSD/BinaryLLSD.cs b/OpenMetaverse.StructuredData/LLSD/BinaryLLSD.cs index a3a17fff..9d1096ef 100644 --- a/OpenMetaverse.StructuredData/LLSD/BinaryLLSD.cs +++ b/OpenMetaverse.StructuredData/LLSD/BinaryLLSD.cs @@ -53,7 +53,8 @@ namespace OpenMetaverse.StructuredData private const int int32Length = 4; private const int doubleLength = 8; - private const string llsdBinaryHead = "\n"; + private const string llsdBinaryHead = ""; + private const string llsdBinaryHead2 = ""; private const byte undefBinaryValue = (byte)'!'; private const byte trueBinaryValue = (byte)'1'; private const byte falseBinaryValue = (byte)'0'; @@ -98,10 +99,11 @@ namespace OpenMetaverse.StructuredData SkipWhiteSpace(stream); - bool result = FindString(stream, llsdBinaryHead); - if (!result) + if (!FindString(stream, llsdBinaryHead) && !FindString(stream, llsdBinaryHead2)) throw new OSDException("Failed to decode binary LLSD"); + SkipWhiteSpace(stream); + return ParseLLSDBinaryElement(stream); } diff --git a/OpenMetaverse.Tests/BinaryLLSDTests.cs b/OpenMetaverse.Tests/BinaryLLSDTests.cs index 59db9091..185d6686 100644 --- a/OpenMetaverse.Tests/BinaryLLSDTests.cs +++ b/OpenMetaverse.Tests/BinaryLLSDTests.cs @@ -49,7 +49,7 @@ namespace OpenMetaverse.Tests [TestFixture()] public class BinarySDTests { - private static byte[] binaryHead = { 0x3c, 0x3f, 0x6c, 0x6c, 0x73, 0x64, 0x2f, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x3f, 0x3e, 0xa }; + private static readonly byte[] binaryHead = Encoding.ASCII.GetBytes(""); [Test()] public void HelperFunctions() diff --git a/OpenMetaverse.Tests/TypeTests.cs b/OpenMetaverse.Tests/TypeTests.cs index 0622e678..1819eea8 100644 --- a/OpenMetaverse.Tests/TypeTests.cs +++ b/OpenMetaverse.Tests/TypeTests.cs @@ -194,6 +194,22 @@ namespace OpenMetaverse.Tests b = bitpacker.UnpackBits(16); Assert.IsTrue(b == 1000, "Unpacked " + b + " instead of 1000"); + + packedBytes = new byte[1]; + bitpacker = new BitPack(packedBytes, 0); + bitpacker.PackBit(true); + + bitpacker = new BitPack(packedBytes, 0); + b = bitpacker.UnpackBits(1); + Assert.IsTrue(b == 1, "Unpacked " + b + " instead of 1"); + + packedBytes = new byte[1] { Byte.MaxValue }; + bitpacker = new BitPack(packedBytes, 0); + bitpacker.PackBit(false); + + bitpacker = new BitPack(packedBytes, 0); + b = bitpacker.UnpackBits(1); + Assert.IsTrue(b == 0, "Unpacked " + b + " instead of 0"); } [Test] diff --git a/OpenMetaverse/BitPack.cs b/OpenMetaverse/BitPack.cs index fc1079b3..c1f5ac67 100644 --- a/OpenMetaverse/BitPack.cs +++ b/OpenMetaverse/BitPack.cs @@ -55,6 +55,8 @@ namespace OpenMetaverse private const int MAX_BITS = 8; + private static readonly byte[] ON = new byte[] { 1 }; + private static readonly byte[] OFF = new byte[] { 0 }; private int bytePos; private int bitPos; @@ -108,6 +110,18 @@ namespace OpenMetaverse PackBitArray(input, totalCount); } + /// + /// Pack a single bit in to the data + /// + /// Bit to pack + public void PackBit(bool bit) + { + if (bit) + PackBitArray(ON, 1); + else + PackBitArray(OFF, 1); + } + /// /// /// @@ -327,8 +341,12 @@ namespace OpenMetaverse while (count > 0) { + byte curBit = (byte)(0x80 >> bitPos); + if ((data[curBytePos] & (0x01 << (count - 1))) != 0) - Data[bytePos] |= (byte)(0x80 >> bitPos); + Data[bytePos] |= curBit; + else + Data[bytePos] &= (byte)~curBit; --count; ++bitPos;