Skip to content

Commit b7cd29b

Browse files
committed
Start next version
1 parent e017e93 commit b7cd29b

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.upokecenter</groupId>
55
<artifactId>cbor</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.0.0</version>
7+
<version>2.1.0-SNAPSHOT</version>
88
<name>CBOR</name>
99
<description>
1010
A Java implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 7049. According to that RFC, CBOR's data model "is an extended version of the JSON data model", supporting many more types of data than JSON. This implementation was written by Peter O. and is released to the Public Domain under the CC0 Declaration.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public void WriteCodePoint(int codePoint) throws java.io.IOException {
106106
}
107107
} else if (codePoint <= 0x10ffff) {
108108
this.builder.append((char)((((codePoint - 0x10000) >> 10) &
109-
0x3ff) +0xd800));
110-
this.builder.append((char)(((codePoint - 0x10000) & 0x3ff) +0xdc00));
109+
0x3ff) + 0xd800));
110+
this.builder.append((char)(((codePoint - 0x10000) & 0x3ff) + 0xdc00));
111111
}
112112
}
113113
}

src/test/java/com/upokecenter/test/DataUtilitiesTest.java

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,67 @@
11
package com.upokecenter.test;
22

3+
import java.util.*;
34
import java.io.*;
45

56
import org.junit.Assert;
67
import org.junit.Test;
78
import com.upokecenter.util.*;
89

910
public class DataUtilitiesTest {
11+
public static List<byte[]> GenerateIllegalUtf8Sequences() {
12+
ArrayList<byte[]> list = new ArrayList<byte[]>();
13+
// Generate illegal single bytes
14+
for (int i = 0x80; i <= 0xff; ++i) {
15+
if (i < 0xc2 || i > 0xf4) {
16+
list.add(new byte[] { (byte)i, (byte)0x80 });
17+
}
18+
list.add(new byte[] { (byte)i });
19+
}
20+
list.add(new byte[] { (byte)0xe0, (byte)0xa0 });
21+
list.add(new byte[] { (byte)0xe1, (byte)0x80 });
22+
list.add(new byte[] { (byte)0xef, (byte)0x80 });
23+
list.add(new byte[] { (byte)0xf0, (byte)0x90 });
24+
list.add(new byte[] { (byte)0xf1, (byte)0x80 });
25+
list.add(new byte[] { (byte)0xf3, (byte)0x80 });
26+
list.add(new byte[] { (byte)0xf4, (byte)0x80 });
27+
list.add(new byte[] { (byte)0xf0, (byte)0x90, (byte)0x80 });
28+
list.add(new byte[] { (byte)0xf1, (byte)0x80, (byte)0x80 });
29+
list.add(new byte[] { (byte)0xf3, (byte)0x80, (byte)0x80 });
30+
list.add(new byte[] { (byte)0xf4, (byte)0x80, (byte)0x80 });
31+
// Generate illegal multibyte sequences
32+
for (int i = 0x00; i <= 0xff; ++i) {
33+
if (i < 0x80 || i > 0xbf) {
34+
list.add(new byte[] { (byte)0xc2, (byte)i });
35+
list.add(new byte[] { (byte)0xdf, (byte)i });
36+
list.add(new byte[] { (byte)0xe1, (byte)i, (byte)0x80 });
37+
list.add(new byte[] { (byte)0xef, (byte)i, (byte)0x80 });
38+
list.add(new byte[] { (byte)0xf1, (byte)i, (byte)0x80, (byte)0x80 });
39+
list.add(new byte[] { (byte)0xf3, (byte)i, (byte)0x80, (byte)0x80 });
40+
list.add(new byte[] { (byte)0xe0, (byte)0xa0, (byte)i });
41+
list.add(new byte[] { (byte)0xe1, (byte)0x80, (byte)i });
42+
list.add(new byte[] { (byte)0xef, (byte)0x80, (byte)i });
43+
list.add(new byte[] { (byte)0xf0, (byte)0x90, (byte)i, (byte)0x80 });
44+
list.add(new byte[] { (byte)0xf1, (byte)0x80, (byte)i, (byte)0x80 });
45+
list.add(new byte[] { (byte)0xf3, (byte)0x80, (byte)i, (byte)0x80 });
46+
list.add(new byte[] { (byte)0xf4, (byte)0x80, (byte)i, (byte)0x80 });
47+
list.add(new byte[] { (byte)0xf0, (byte)0x90, (byte)0x80, (byte)i });
48+
list.add(new byte[] { (byte)0xf1, (byte)0x80, (byte)0x80, (byte)i });
49+
list.add(new byte[] { (byte)0xf3, (byte)0x80, (byte)0x80, (byte)i });
50+
list.add(new byte[] { (byte)0xf4, (byte)0x80, (byte)0x80, (byte)i });
51+
}
52+
if (i < 0xa0 || i > 0xbf) {
53+
list.add(new byte[] { (byte)0xe0, (byte)i, (byte)0x80 });
54+
}
55+
if (i < 0x90 || i > 0xbf) {
56+
list.add(new byte[] { (byte)0xf0, (byte)i, (byte)0x80, (byte)0x80 });
57+
}
58+
if (i < 0x80 || i > 0x8f) {
59+
list.add(new byte[] { (byte)0xf4, (byte)i, (byte)0x80, (byte)0x80 });
60+
}
61+
}
62+
return list;
63+
}
64+
1065
@Test
1166
public void TestCodePointAt() {
1267
try {
@@ -40,7 +95,7 @@ public void TestCodePointCompare() {
4095
((DataUtilities.CodePointCompare("abc", "abc")==0) ? 0 : ((DataUtilities.CodePointCompare("abc", "abc")< 0) ? -1 : 1)));
4196
Assert.assertEquals(
4297
0,
43-
((DataUtilities.CodePointCompare("\ud800\udc00" , "\ud800\udc00"
98+
((DataUtilities.CodePointCompare("\ud800\udc00" , "\ud800\udc00"
4499
)==0) ? 0 : ((DataUtilities.CodePointCompare("\ud800\udc00" , "\ud800\udc00"
45100
)< 0) ? -1 : 1)));
46101
Assert.assertEquals(
@@ -180,6 +235,31 @@ public void TestGetUtf8String() {
180235
Assert.fail(ex.toString());
181236
throw new IllegalStateException("", ex);
182237
}
238+
List<byte[]> illegalSeqs = GenerateIllegalUtf8Sequences();
239+
for (byte[] seq : illegalSeqs) {
240+
try {
241+
DataUtilities.GetUtf8String(seq, false);
242+
Assert.fail("Should have failed");
243+
} catch (IllegalArgumentException ex) {
244+
} catch (Exception ex) {
245+
Assert.fail(ex.toString());
246+
throw new IllegalStateException("", ex);
247+
}
248+
String strret = DataUtilities.GetUtf8String(seq, true);
249+
if (!(strret.length() > 0))Assert.fail();
250+
Assert.assertEquals('\ufffd', strret.charAt(0));
251+
try {
252+
DataUtilities.GetUtf8String(seq, 0, seq.length, false);
253+
Assert.fail("Should have failed");
254+
} catch (IllegalArgumentException ex) {
255+
} catch (Exception ex) {
256+
Assert.fail(ex.toString());
257+
throw new IllegalStateException("", ex);
258+
}
259+
strret = DataUtilities.GetUtf8String(seq, 0, seq.length, true);
260+
if (!(strret.length() > 0))Assert.fail();
261+
Assert.assertEquals('\ufffd', strret.charAt(0));
262+
}
183263
}
184264
@Test
185265
public void TestReadUtf8() {
@@ -294,6 +374,42 @@ public void TestReadUtf8ToString() {
294374
Assert.fail(ex.toString());
295375
throw new IllegalStateException("", ex);
296376
}
377+
List<byte[]> illegalSeqs = GenerateIllegalUtf8Sequences();
378+
for (byte[] seq : illegalSeqs) {
379+
java.io.ByteArrayInputStream ms = null;
380+
try {
381+
ms = new java.io.ByteArrayInputStream(seq);
382+
383+
try {
384+
DataUtilities.ReadUtf8ToString(ms, -1, false);
385+
Assert.fail("Should have failed");
386+
} catch (IOException ex) {
387+
} catch (Exception ex) {
388+
Assert.fail(ex.toString());
389+
throw new IllegalStateException("", ex);
390+
}
391+
}
392+
finally {
393+
try { if (ms != null)ms.close(); } catch (java.io.IOException ex) {}
394+
}
395+
java.io.ByteArrayInputStream ms = null;
396+
try {
397+
ms = new java.io.ByteArrayInputStream(seq);
398+
399+
String strret = null;
400+
try {
401+
strret = DataUtilities.ReadUtf8ToString(ms, -1, true);
402+
} catch (Exception ex) {
403+
Assert.fail(ex.toString());
404+
throw new IllegalStateException("", ex);
405+
}
406+
if (!(strret.length() > 0))Assert.fail();
407+
Assert.assertEquals('\ufffd', strret.charAt(0));
408+
}
409+
finally {
410+
try { if (ms != null)ms.close(); } catch (java.io.IOException ex) {}
411+
}
412+
}
297413
}
298414
@Test
299415
public void TestToLowerCaseAscii() {

0 commit comments

Comments
 (0)