Skip to content

Commit

Permalink
Bug and regression fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Jan 18, 2022
1 parent f749305 commit 28f308a
Show file tree
Hide file tree
Showing 12 changed files with 1,082 additions and 31 deletions.
10 changes: 7 additions & 3 deletions CBOR.nuspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<package
><metadata><version>4.5.1</version><id>PeterO.Cbor</id><requireLicenseAcceptance>false</requireLicenseAcceptance><releaseNotes>Version 4.5.1
><metadata><version>4.5.2</version><id>PeterO.Cbor</id><requireLicenseAcceptance>false</requireLicenseAcceptance><releaseNotes>Version 4.5.2

- Bug and regression fixes

Version 4.5.1

- Fix reported security issue

Version 4.5:

- Add support for JSON Pointers and JSON Patches
Expand All @@ -12,4 +16,4 @@ Version 4.5:
- Add support for deserializing CBOR objects to IReadOnlyList, IReadOnlyCollection, and ReadOnlyDictionary

Note that after version 4.5x, the CBOR library&apos;s repository will stop including special projects for .NET 2.0 and .NET 4.0, leaving the .NET-Standard project for building the library.</releaseNotes><summary></summary><license type='expression'>CC0-1.0</license><projectUrl>https://github.com/peteroupc/CBOR</projectUrl><authors>Peter Occil</authors><description>A C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 8949.</description><owners>Peter Occil</owners><title>CBOR (Concise Binary Object Representation)</title><tags>cbor data serialization binary json</tags><dependencies><group targetFramework='.NETStandard1.0'><dependency id='PeterO.URIUtility' version='1.0.0' /><dependency id='PeterO.Numbers' version='1.8.2' /></group><group targetFramework='.NETFramework2.0'><dependency id='PeterO.URIUtility' version='1.0.0' /><dependency id='PeterO.Numbers' version='1.8.2' /></group><group targetFramework='.NETFramework4.0'><dependency id='PeterO.URIUtility' version='1.0.0' /><dependency id='PeterO.Numbers' version='1.8.2' /></group></dependencies></metadata><files><file src='CBOR/bin/Release/netstandard1.0/CBOR.dll' target='/lib/netstandard1.0' /><file src='CBOR/bin/Release/netstandard1.0/CBOR.xml' target='/lib/netstandard1.0' /><file src='CBOR20/bin/Release/CBOR.dll' target='/lib/net20' /><file src='CBOR20/bin/Release/CBOR.xml' target='/lib/net20' /><file src='CBOR40/bin/Release/CBOR.dll' target='/lib/net40' /><file src='CBOR40/bin/Release/CBOR.xml' target='/lib/net40' /></files></package
>
>
8 changes: 6 additions & 2 deletions CBOR/CBOR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>4.5.1</Version>
<Version>4.5.2</Version>
<Owners>Peter Occil</Owners>
<Description>A C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 8949.</Description>
<Summary>A C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 8949. </Summary>
Expand All @@ -13,10 +13,14 @@
<PackageLicenseExpression>CC0-1.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/peteroupc/CBOR</PackageProjectUrl>
<PackageReleaseNotes>
Version 4.5.2

- Bug and regression fixes

Version 4.5.1

- Fix reported security issue

Version 4.5:

- Add support for JSON Pointers and JSON Patches
Expand Down
1 change: 1 addition & 0 deletions CBOR/PeterO/Cbor/CBORDataUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ internal static string ToStringHelper(CBORObject obj, int depth) {
sb.Append(HexAlphabet[(cp >> 8) & 15]);
sb.Append(HexAlphabet[(cp >> 4) & 15]);
sb.Append(HexAlphabet[cp & 15]);
++i;
} else if (cp >= 0x7F || cp < 0x20 || cp == (int)'\\' || cp ==
(int)'\"') {
sb.Append("\\u");
Expand Down
8 changes: 2 additions & 6 deletions CBOR/PeterO/Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7942,12 +7942,8 @@ private static int MapCompare(
if (listACount != listBCount) {
return listACount < listBCount ? -1 : 1;
}
var sortedASet = new List<CBORObject>(mapA.Keys);
var sortedBSet = new List<CBORObject>(mapB.Keys);
// DebugUtility.Log("---sorting mapA's keys");
// sortedASet.Sort();
// DebugUtility.Log("---sorting mapB's keys");
// sortedBSet.Sort();
var sortedASet = new List<CBORObject>(PropertyMap.GetSortedKeys(mapA));
var sortedBSet = new List<CBORObject>(PropertyMap.GetSortedKeys(mapB));
// DebugUtility.Log("---done sorting");
listACount = sortedASet.Count;
listBCount = sortedBSet.Count;
Expand Down
12 changes: 8 additions & 4 deletions CBOR/PeterO/Cbor/CBORUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,10 @@ public static int ByteArrayCompare(byte[] a, byte[] b) {
}
int c = Math.Min(a.Length, b.Length);
for (var i = 0; i < c; ++i) {
if (a[i] != b[i]) {
return (a[i] < b[i]) ? -1 : 1;
byte ai = a[i];
byte bi = b[i];
if (ai != bi) {
return ((((int)ai) & 0xff) < (((int)bi) & 0xff)) ? -1 : 1;
}
}
return (a.Length != b.Length) ? ((a.Length < b.Length) ? -1 : 1) : 0;
Expand All @@ -487,8 +489,10 @@ public static int ByteArrayCompareLengthFirst(byte[] a, byte[] b) {
return a.Length < b.Length ? -1 : 1;
}
for (var i = 0; i < a.Length; ++i) {
if (a[i] != b[i]) {
return (a[i] < b[i]) ? -1 : 1;
byte ai = a[i];
byte bi = b[i];
if (ai != bi) {
return ((((int)ai) & 0xff) < (((int)bi) & 0xff)) ? -1 : 1;
}
}
return 0;
Expand Down
23 changes: 22 additions & 1 deletion CBOR/PeterO/Cbor/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private sealed class OrderedDictionary<TKey, TValue> :
private readonly IDictionary<TKey, TValue> dict;
private readonly LinkedList<TKey> list;
public OrderedDictionary() {
this.dict = new Dictionary<TKey, TValue>();
this.dict = new SortedDictionary<TKey, TValue>();
this.list = new LinkedList<TKey>();
}
public void Add(KeyValuePair<TKey, TValue> kvp) {
Expand Down Expand Up @@ -179,6 +179,12 @@ public ICollection<TKey> Keys {
}
}

public ICollection<TKey> SortedKeys {
get {
return this.dict.Keys;
}
}

public ICollection<TValue> Values {
get {
return new ValueWrapper<TKey, TValue>(this.dict, this.list);
Expand Down Expand Up @@ -895,6 +901,21 @@ public static object EnumToObjectAsInteger(Enum value) {
Convert.ToInt32(value, CultureInfo.InvariantCulture));
}

public static ICollection<TKey>
GetSortedKeys<TKey, TValue>(
IDictionary<TKey, TValue> dict) {
var odict = dict as OrderedDictionary<TKey, TValue>;
if (odict != null) {
return odict.SortedKeys;
}
var sdict = dict as SortedDictionary<TKey, TValue>;
if (sdict != null) {
return sdict.Keys;
}
throw new InvalidOperationException("Internal error: Map doesn't" +
"\u0020support sorted keys");
}

public static ICollection<KeyValuePair<TKey, TValue>>
GetEntries<TKey, TValue>(
IDictionary<TKey, TValue> dict) {
Expand Down
6 changes: 3 additions & 3 deletions CBOR20/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;
[assembly: System.CLSCompliant(true)]
[assembly: AssemblyInformationalVersion("4.5.1")]
[assembly: AssemblyVersion("4.5.1.0")]
[assembly: AssemblyFileVersion("4.5.1.0")]
[assembly: AssemblyInformationalVersion("4.5.2")]
[assembly: AssemblyVersion("4.5.2.0")]
[assembly: AssemblyFileVersion("4.5.2.0")]
[assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" +
"on)")]
[assembly: AssemblyTitle("CBOR (Concise Binary Object Representati" +
Expand Down
6 changes: 3 additions & 3 deletions CBOR40/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;
[assembly: System.CLSCompliant(true)]
[assembly: AssemblyInformationalVersion("4.5.1")]
[assembly: AssemblyVersion("4.5.1.0")]
[assembly: AssemblyFileVersion("4.5.1.0")]
[assembly: AssemblyInformationalVersion("4.5.2")]
[assembly: AssemblyVersion("4.5.2.0")]
[assembly: AssemblyFileVersion("4.5.2.0")]
[assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" +
"on)")]
[assembly: AssemblyTitle("CBOR (Concise Binary Object Representati" +
Expand Down
61 changes: 54 additions & 7 deletions CBORTest/CBORGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ private static void GenerateArgument(
private static int[]
valueMajorTypes = {
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4,
4, 5, 6, 6, 7, 7, 7, 7, 7, 7,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7,
};

private static int[]
valueMajorTypesHighDepth = {
0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 6, 7,
};

private static int[] valueMajorTypesHighLength = {
Expand Down Expand Up @@ -130,20 +136,50 @@ private static void GenerateUtf8(IRandomGenExtended ra, ByteWriter bs, int
}
}

private void GenerateSmall(IRandomGenExtended r, int depth, ByteWriter bs) {
int v = r.GetInt32(100);
if (v < 25) {
GenerateArgument(r, 0, r.GetInt32(100), bs);
} else if (v < 35) {
bs.Write(0x41);
bs.Write(0x20);
} else if (v < 45) {
bs.Write(0x41);
bs.Write(0x20);
} else if (v < 50) {
bs.Write(0x81);
this.GenerateSmall(r, depth + 1, bs);
} else if (v < 53) {
bs.Write(0xa2);
bs.Write(0xf7);
bs.Write(0xf6);
this.GenerateSmall(r, depth + 1, bs);
bs.Write(0xf5);
} else if (v < 80) {
bs.Write(r.GetInt32(0x40));
} else if (v < 100) {
bs.Write(r.GetInt32(0x60));
}
}
private void Generate(IRandomGenExtended r, int depth, ByteWriter bs) {
int majorType = valueMajorTypes[r.GetInt32(valueMajorTypes.Length)];
if (depth > 6) {
majorType = valueMajorTypesHighDepth[r.GetInt32(
valueMajorTypesHighDepth.Length)];
}
if (bs.ByteLength > 2000000) {
majorType = valueMajorTypesHighLength[r.GetInt32(
valueMajorTypesHighLength.Length)];
}
if (majorType == 3 || majorType == 2) {
if (majorType == 3 || majorType == 2) { // Byte and text strings
int len = r.GetInt32(1000);
if (r.GetInt32(50) == 0 && depth < 2) {
var v = (long)r.GetInt32(100000) * r.GetInt32(100000);
len = (int)(v / 100000);
}
if (depth > 6) {
} else if (depth > 6) {
len = r.GetInt32(100) == 0 ? 1 : 0;
} else if (depth > 2) {
len = r.GetInt32(16) + 1;
}
// TODO: Ensure key uniqueness
if (r.GetInt32(2) == 0) {
Expand Down Expand Up @@ -174,11 +210,18 @@ private void Generate(IRandomGenExtended r, int depth, ByteWriter bs) {
}
}
return;
} else if (majorType == 4 || majorType == 5) {
} else if (majorType == 4 || majorType == 5) { // Arrays and maps
int len = r.GetInt32(8);
if (r.GetInt32(50) == 0 && depth < 2) {
var v = (long)r.GetInt32(1000) * r.GetInt32(1000);
len = (int)(v / 1000);
} else if (depth > 6) {
len = r.GetInt32(100) == 0 ? 1 : 0;
} else if (depth > 2) {
len = r.GetInt32(3) + 1;
}
if (depth > 6) {
len = r.GetInt32(100) < 50 ? 1 : (r.GetInt32(100) < 10 ? 2 : 0);
}
bool indefiniteLength = r.GetInt32(2) == 0;
if (indefiniteLength) {
Expand All @@ -187,7 +230,11 @@ private void Generate(IRandomGenExtended r, int depth, ByteWriter bs) {
GenerateArgument(r, majorType, len, bs);
}
for (int i = 0; i < len; ++i) {
this.Generate(r, depth + 1, bs);
if (depth > 6) {
this.GenerateSmall(r, depth + 1, bs);
} else {
this.Generate(r, depth + 1, bs);
}
if (majorType == 5) {
this.Generate(r, depth + 1, bs);
}
Expand Down Expand Up @@ -229,7 +276,7 @@ private void Generate(IRandomGenExtended r, int depth, ByteWriter bs) {
}
break;
}
if (majorType == 6) {
if (majorType == 6) { // Tags
this.Generate(r, depth + 1, bs);
}
}
Expand Down
Loading

0 comments on commit 28f308a

Please sign in to comment.