Skip to content

Commit

Permalink
Coorect the byte order in Block1/Block2 as it was backwards
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Mar 22, 2018
1 parent 4ca0683 commit a543c9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 38 deletions.
37 changes: 8 additions & 29 deletions src/CoAPNet/Options/BlockWise.cs
Expand Up @@ -113,50 +113,29 @@ public override void FromBytes(byte[] data)
BlockNumber = 0;
last = 0;
}
else if (data.Length == 1)
else if (data.Length <= 3)
{
BlockNumber = (int)ValueUInt & 0x0F;
last = (ValueUInt & 0xF0) >> 4;
}
else if (data.Length == 2)
{
BlockNumber = (int)ValueUInt & 0x0FFF;
last = (ValueUInt & 0xF000) >> 12;
}
else if (data.Length == 3)
{
BlockNumber = (int)ValueUInt & 0x0FFFFF;
last = (ValueUInt & 0xF00000) >> 20;
BlockNumber = (int)(ValueUInt & 0xFFFFF0) >> 4;
last = ValueUInt & 0x0F;
}
else
{
throw new CoapOptionException($"Invalid length ({data.Length}) of Block1/Block2 option");
}

IsMoreFollowing = (last & 0x01) > 0;
var szx = (int)((last >> 1));
IsMoreFollowing = (last & 0x08) > 0;
var szx = (int)((last & 0x07));
BlockSize = InternalSupportedBlockSizes.First(b => b.Item1 == szx).Item2;
}

/// <inheritdoc/>
public override byte[] GetBytes()
{
var szx = InternalSupportedBlockSizes.First(b => b.Item2 == BlockSize).Item1;
var last = (byte)((szx & 0x07) << 5) | (IsMoreFollowing ? 0x10 : 0x00);

if (BlockNumber <= 0x0F)
{
ValueUInt = (uint)(last | (BlockNumber & 0x0F));
}
else if (BlockNumber <= 0x0FFF)
{
ValueUInt = (uint)((last << 8) | (BlockNumber & 0x0FFF));
}
else if (BlockNumber <= 0x0FFFFF)
{
ValueUInt = (uint)((last << 16) | (BlockNumber & 0x0FFFFF));
}
var last = (byte)((szx & 0x07) | (IsMoreFollowing ? 0x08 : 0x00));

ValueUInt = (uint)(last | ((BlockNumber << 4) & 0xFFFFF0));

return base.GetBytes();
}

Expand Down
19 changes: 10 additions & 9 deletions tests/CoAPNet.Tests/CoapOptionsTests.cs
Expand Up @@ -469,15 +469,16 @@ public void TestOptionInvalidCastException()

[Category("[RFC7959] Section 2.2"), Category("Blocks")]
[TestCase(0, 16, false, new byte[] { })]
[TestCase(1, 16, false, new byte[] { 0x01 })]
[TestCase(2, 32, false, new byte[] { 0x22 })]
[TestCase(3, 64, true, new byte[] { 0x53 })]
[TestCase(4095, 128, true, new byte[] { 0x7F, 0xFF })]
[TestCase(1048575, 256, true, new byte[] { 0x9F, 0xFF, 0xFF})]
[TestCase(15, 16, true, new byte[] { 0x1F })]
[TestCase(16, 16, true, new byte[] { 0x10, 0x10 })]
[TestCase(39, 16, false, new byte[] { 0, 39 })]
[TestCase(79, 16, false, new byte[] { 0, 79 })]
[TestCase(1, 16, false, new byte[] { 0x10 })]
[TestCase(2, 32, false, new byte[] { 0x21 })]
[TestCase(3, 64, true, new byte[] { 0x3A })]
[TestCase(4095, 128, true, new byte[] { 0xFF, 0xFB })]
[TestCase(1048575, 256, true, new byte[] { 0xFF, 0xFF, 0xFC})]
[TestCase(15, 16, true, new byte[] { 0xF8 })]
[TestCase(16, 16, true, new byte[] { 0x01, 0x08 })]
[TestCase(39, 16, false, new byte[] { 0x02, 0x70 })]
[TestCase(79, 16, false, new byte[] { 0x04, 0xF0 })]
[TestCase(0, 128, true, new byte[] { 0x0b })]

This comment has been minimized.

Copy link
@NZSmartie

NZSmartie Mar 22, 2018

Author Owner

Yes, these tests were hand crafted...

public void TestBlockOption(int blockNumber, int blockSize, bool more, byte[] expected)
{
var optionToBytes = new Options.Block1(blockNumber, blockSize, more);
Expand Down

0 comments on commit a543c9f

Please sign in to comment.