Skip to content

Commit 13ae506

Browse files
committed
update Java version
1 parent 2cda61d commit 13ae506

File tree

3 files changed

+217
-8
lines changed

3 files changed

+217
-8
lines changed

src/main/java/com/upokecenter/cbor/CBORDataUtilities.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ static String ToStringHelper(CBORObject obj, int depth) {
124124
sb.append(HexAlphabet.charAt((cp >> 8) & 15));
125125
sb.append(HexAlphabet.charAt((cp >> 4) & 15));
126126
sb.append(HexAlphabet.charAt(cp & 15));
127+
++i;
127128
} else if (cp >= 0x7F || cp < 0x20 || cp == (int)'\\' || cp ==
128129
(int)'\"') {
129130
sb.append("\\u");

src/main/java/com/upokecenter/cbor/CBORUtilities.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,10 @@ public static int ByteArrayCompare(byte[] a, byte[] b) {
471471
}
472472
int c = Math.min(a.length, b.length);
473473
for (int i = 0; i < c; ++i) {
474-
if (a[i] != b[i]) {
475-
return (a[i] < b[i]) ? -1 : 1;
474+
byte ai = a[i];
475+
byte bi = b[i];
476+
if (ai != bi) {
477+
return ((((int)ai) & 0xff) < (((int)bi) & 0xff)) ? -1 : 1;
476478
}
477479
}
478480
return (a.length != b.length) ? ((a.length < b.length) ? -1 : 1) : 0;
@@ -489,8 +491,10 @@ public static int ByteArrayCompareLengthFirst(byte[] a, byte[] b) {
489491
return a.length < b.length ? -1 : 1;
490492
}
491493
for (int i = 0; i < a.length; ++i) {
492-
if (a[i] != b[i]) {
493-
return (a[i] < b[i]) ? -1 : 1;
494+
byte ai = a[i];
495+
byte bi = b[i];
496+
if (ai != bi) {
497+
return ((((int)ai) & 0xff) < (((int)bi) & 0xff)) ? -1 : 1;
494498
}
495499
}
496500
return 0;

src/test/java/com/upokecenter/test/CBORObjectTest.java

Lines changed: 208 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11229,8 +11229,8 @@ public void TestRoundTripRegressions() {
1122911229
(byte)0xe1,
1123011230
(byte)0x99,
1123111231
(byte)0x92,
11232-
(byte)0xf0, (
11233-
byte)0xb5, (byte)0xa4, (byte)0xa2, 0x3a, 0x77, 0x11, 0x4c, 0x6f,
11232+
(byte)0xf0,
11233+
(byte)0xb5, (byte)0xa4, (byte)0xa2, 0x3a, 0x77, 0x11, 0x4c, 0x6f,
1123411234
(byte)0xff,
1123511235
};
1123611236
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
@@ -11292,8 +11292,8 @@ public void TestRoundTripRegressions() {
1129211292
(byte)0xd2,
1129311293
(byte)0xe7,
1129411294
(byte)0x99, 0x00, 0x01, 0x1b, 0x2a, 0x6e, 0x6f, 0x67, 0x4b, 0x18, 0x60,
11295-
0x51, 0x1b, 0x46, (
11296-
byte)0x9f, (byte)0xd3, (byte)0xb7, (byte)0xf4, 0x74, (byte)0xad, 0x6c,
11295+
0x51, 0x1b, 0x46,
11296+
(byte)0x9f, (byte)0xd3, (byte)0xb7, (byte)0xf4, 0x74, (byte)0xad, 0x6c,
1129711297
(byte)0xff,
1129811298
};
1129911299
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
@@ -11375,4 +11375,208 @@ public void TestToObject_TypeMapper() {
1137511375
stringTemp);
1137611376
}
1137711377
}
11378+
11379+
@Test
11380+
public void TestRegressionFour() {
11381+
CBORObject o1 = CBORObject.FromObject(new byte[] { (byte)5, (byte)2 });
11382+
CBORObject o2 = CBORObject.FromObject(new byte[] { (byte)0x85, (byte)2 });
11383+
TestCommon.CompareTestLess(o1, o2);
11384+
}
11385+
11386+
@Test
11387+
public void TestRegressionOne() {
11388+
{
11389+
CBOREncodeOptions options = new CBOREncodeOptions();
11390+
byte[] bytes = new byte[] {
11391+
(byte)0xbf, 0x0d,
11392+
(byte)0xdb, 0x7f, 0x53,
11393+
(byte)0xd5, 0x1e,
11394+
(byte)0xab, 0x1f,
11395+
(byte)0xb2,
11396+
(byte)0xc2,
11397+
(byte)0xb8, 0x02, 0x7f, 0x7a, 0x00, 0x00, 0x00, 0x09,
11398+
(byte)0xf0,
11399+
(byte)0xb8,
11400+
(byte)0xbf,
11401+
(byte)0xbf, 0x00,
11402+
(byte)0xf0,
11403+
(byte)0x9d,
11404+
(byte)0x84,
11405+
(byte)0xa1,
11406+
(byte)0xff, 0x1a, 0x00, 0x46, 0x31,
11407+
(byte)0xdf, 0x7f, 0x69, 0x05, 0x47, 0x76, 0x4f, 0x01,
11408+
(byte)0xf4,
11409+
(byte)0x80,
11410+
(byte)0x80,
11411+
(byte)0x80, (byte)0xff, 0x3a, 0x0a, (byte)0xaa, (byte)0xf2, 0x00,
11412+
(byte)0xff,
11413+
};
11414+
byte[] encodedBytes = new byte[] {
11415+
(byte)0xa1, 0x0d,
11416+
(byte)0xdb, 0x7f, 0x53,
11417+
(byte)0xd5, 0x1e,
11418+
(byte)0xab, 0x1f,
11419+
(byte)0xb2,
11420+
(byte)0xc2,
11421+
(byte)0xa2, 0x69, 0x05, 0x47, 0x76, 0x4f, 0x01,
11422+
(byte)0xf4,
11423+
(byte)0x80,
11424+
(byte)0x80,
11425+
(byte)0x80, 0x3a, 0x0a,
11426+
(byte)0xaa,
11427+
(byte)0xf2, 0x00, 0x69,
11428+
(byte)0xf0,
11429+
(byte)0xb8,
11430+
(byte)0xbf,
11431+
(byte)0xbf, 0x00, (
11432+
byte)0xf0, (byte)0x9d, (byte)0x84, (byte)0xa1, 0x1a, 0x00, 0x46, 0x31,
11433+
(byte)0xdf,
11434+
};
11435+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes));
11436+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(encodedBytes));
11437+
}
11438+
}
11439+
11440+
@Test
11441+
public void TestRegressionTwo() {
11442+
{
11443+
CBOREncodeOptions options = new CBOREncodeOptions();
11444+
byte[] bytes = new byte[] {
11445+
(byte)0xb8, 0x02, 0x7f, 0x7a, 0x00, 0x00, 0x00,
11446+
0x09,
11447+
(byte)0xf0,
11448+
(byte)0xb8,
11449+
(byte)0xbf,
11450+
(byte)0xbf, 0x00,
11451+
(byte)0xf0,
11452+
(byte)0x9d,
11453+
(byte)0x84,
11454+
(byte)0xa1,
11455+
(byte)0xff, 0x1a, 0x00, 0x46, 0x31,
11456+
(byte)0xdf, 0x7f, 0x69, 0x05, 0x47, 0x76, 0x4f, 0x01,
11457+
(byte)0xf4,
11458+
(byte)0x80,
11459+
(byte)0x80,
11460+
(byte)0x80, (byte)0xff, 0x3a, 0x0a, (byte)0xaa, (byte)0xf2, 0x00,
11461+
};
11462+
byte[] encodedBytes = new byte[] {
11463+
(byte)0xa2, 0x69, 0x05, 0x47, 0x76, 0x4f,
11464+
0x01,
11465+
(byte)0xf4,
11466+
(byte)0x80,
11467+
(byte)0x80,
11468+
(byte)0x80, 0x3a, 0x0a,
11469+
(byte)0xaa,
11470+
(byte)0xf2, 0x00, 0x69,
11471+
(byte)0xf0,
11472+
(byte)0xb8,
11473+
(byte)0xbf,
11474+
(byte)0xbf, 0x00, (
11475+
byte)0xf0, (byte)0x9d, (byte)0x84, (byte)0xa1, 0x1a, 0x00, 0x46, 0x31,
11476+
(byte)0xdf,
11477+
};
11478+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes));
11479+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(encodedBytes));
11480+
}
11481+
}
11482+
11483+
@Test
11484+
public void TestRegressionThree() {
11485+
{
11486+
CBOREncodeOptions options = new CBOREncodeOptions();
11487+
byte[] bytes = new byte[] {
11488+
(byte)0xb8, 0x02, 0x7f, 0x7a, 0x00, 0x00, 0x00,
11489+
0x09,
11490+
(byte)0xf0,
11491+
(byte)0xb8,
11492+
(byte)0xbf,
11493+
(byte)0xbf, 0x00,
11494+
(byte)0xf0,
11495+
(byte)0x9d,
11496+
(byte)0x84,
11497+
(byte)0xa1,
11498+
(byte)0xff, 0, 0x7f, 0x69, 0x05, 0x47, 0x76, 0x4f, 0x01,
11499+
(byte)0xf4,
11500+
(byte)0x80,
11501+
(byte)0x80,
11502+
(byte)0x80, (byte)0xff, 0,
11503+
};
11504+
byte[] encodedBytes = new byte[] {
11505+
(byte)0xa2, 0x69, 0x05, 0x47, 0x76, 0x4f,
11506+
0x01,
11507+
(byte)0xf4,
11508+
(byte)0x80,
11509+
(byte)0x80,
11510+
(byte)0x80, 0, 0x69,
11511+
(byte)0xf0,
11512+
(byte)0xb8, (
11513+
byte)0xbf, (byte)0xbf, 0x00, (byte)0xf0, (byte)0x9d, (byte)0x84,
11514+
(byte)0xa1, 0,
11515+
};
11516+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes));
11517+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(encodedBytes));
11518+
}
11519+
}
11520+
11521+
@Test
11522+
public void TestStringCompareBug() {
11523+
CBORObject a, b, c, d;
11524+
byte[] bytes = new byte[] {
11525+
0x69, 0x05, 0x47, 0x76, 0x4f, 0x01,
11526+
(byte)0xf4,
11527+
(byte)0x80,
11528+
(byte)0x80,
11529+
(byte)0x80,
11530+
};
11531+
a = CBORObject.DecodeFromBytes(bytes);
11532+
c = a;
11533+
bytes = new byte[] {
11534+
0x7f, 0x69, 0x05, 0x47, 0x76, 0x4f, 0x01,
11535+
(byte)0xf4,
11536+
(byte)0x80,
11537+
(byte)0x80,
11538+
(byte)0x80, (byte)0xff,
11539+
};
11540+
b = CBORObject.DecodeFromBytes(bytes);
11541+
d = b;
11542+
TestCommon.CompareTestEqual(a, b);
11543+
bytes = new byte[] {
11544+
0x7f, 0x7a, 0x00, 0x00, 0x00, 0x09,
11545+
(byte)0xf0,
11546+
(byte)0xb8,
11547+
(byte)0xbf,
11548+
(byte)0xbf, 0x00,
11549+
(byte)0xf0,
11550+
(byte)0x9d,
11551+
(byte)0x84,
11552+
(byte)0xa1,
11553+
(byte)0xff,
11554+
};
11555+
a = CBORObject.DecodeFromBytes(bytes);
11556+
bytes = new byte[] {
11557+
0x7f, 0x69,
11558+
(byte)0xf0,
11559+
(byte)0xb8,
11560+
(byte)0xbf,
11561+
(byte)0xbf, 0x00,
11562+
(byte)0xf0,
11563+
(byte)0x9d,
11564+
(byte)0x84,
11565+
(byte)0xa1,
11566+
(byte)0xff,
11567+
};
11568+
b = CBORObject.DecodeFromBytes(bytes);
11569+
TestCommon.CompareTestEqual(a, b);
11570+
TestCommon.CompareTestLess(c, a);
11571+
TestCommon.CompareTestLess(c, b);
11572+
TestCommon.CompareTestLess(d, a);
11573+
TestCommon.CompareTestLess(d, b);
11574+
CBORObject o1 = CBORObject.NewMap();
11575+
o1.Add(b, CBORObject.FromObject(0));
11576+
o1.Add(c, CBORObject.FromObject(0));
11577+
CBORObject o2 = CBORObject.NewMap();
11578+
o2.Add(c, CBORObject.FromObject(0));
11579+
o2.Add(b, CBORObject.FromObject(0));
11580+
TestCommon.CompareTestEqual(a, b);
11581+
}
1137811582
}

0 commit comments

Comments
 (0)