Skip to content

Commit

Permalink
Handle binary plist integers when the length is not sent as a power
Browse files Browse the repository at this point in the history
  • Loading branch information
artehe committed Jan 3, 2024
1 parent 1c2cf64 commit 675095b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Netimobiledevice/Plist/IntegerNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Netimobiledevice.Plist
{
Expand Down Expand Up @@ -148,7 +150,18 @@ internal override void ReadBinary(Stream stream, int nodeLength)
break;
}
default: {
throw new PlistFormatException("UInt > 64Bit");
byte[] valueBuf = buf.Skip(buf.Length - nodeLength).Take(nodeLength).ToArray();
if (valueBuf.Length > 15) {
throw new PlistFormatException("UInt > 64Bit");
}
else {
uint nearestPowOf2 = BitOperations.RoundUpToPowerOf2((uint) valueBuf.Length);
int nthRoot = (int) Math.Log(nearestPowOf2, 2);
using (Stream valueBufStream = new MemoryStream(valueBuf)) {
ReadBinary(valueBufStream, nthRoot);
}
}
break;
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions NetimobiledeviceTest/Plist/IntegerNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,32 @@ public void ParseHandlesNegativeValue()
int actualValue = (int) node.Value;
Assert.AreEqual(-536854523, actualValue);
}

[TestMethod]
public void ReadBinaryHandlesNodeLengthNotAsAPower()
{
IntegerNode node = new IntegerNode();
byte[] longNodeLength = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xB, 0xB8 };

using (MemoryStream stream = new MemoryStream(longNodeLength)) {
node.ReadBinary(stream, 4);
}

int nodeValue = (int) node.Value;
Assert.AreEqual(3000, nodeValue);
}

[TestMethod]
public void ReadBinaryHandlesNodeLengthAsAPower()
{
IntegerNode node = new IntegerNode();
byte[] longNodeLength = new byte[] { 0, 0, 0x7, 0xD0 };

using (MemoryStream stream = new MemoryStream(longNodeLength)) {
node.ReadBinary(stream, 2);
}

int nodeValue = (int) node.Value;
Assert.AreEqual(2000, nodeValue);
}
}

0 comments on commit 675095b

Please sign in to comment.