Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_10Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_13Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_14Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_15Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_16Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_1Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_2Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_3Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_4Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_9Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1250Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1252Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1254Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1257Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
51 changes: 51 additions & 0 deletions test/org/jcodings/specific/TestCaseMap.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jcodings.specific;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -82,6 +84,55 @@ public void testISOCaseMap() throws Exception {
}
}

// Map the whole input with a buffer of input length + 20, refilling it until the input is consumed (like CRuby and JRuby)
byte[] caseMapAll(Encoding enc, byte[] from, int flags) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IntHolder fromP = new IntHolder();
IntHolder flagP = new IntHolder();
flagP.value = flags;
int buffers = 0;
while (fromP.value < from.length) {
byte[] to = new byte[(from.length - fromP.value) * ++buffers + 20];
int len = enc.caseMap(flagP, from, fromP, from.length, to, 0, to.length);
out.write(to, 0, len);
}
return out.toByteArray();
}

static byte[] repeat(String prefix, char c, int n) {
byte[] bytes = new byte[prefix.length() + n];
for (int i = 0; i < prefix.length(); i++) bytes[i] = (byte) prefix.charAt(i);
Arrays.fill(bytes, prefix.length(), bytes.length, (byte) c);
return bytes;
}

static byte[] repeat(String s, int n) {
byte[] bytes = new byte[s.length() * n];
for (int i = 0; i < bytes.length; i++) bytes[i] = (byte) s.charAt(i % s.length());
return bytes;
}

@Test
public void testSharpSExpansionDoesNotOverflow() throws Exception {
List<Encoding> list = Arrays.<Encoding>asList(
ISO8859_1Encoding.INSTANCE, ISO8859_2Encoding.INSTANCE, ISO8859_3Encoding.INSTANCE,
ISO8859_4Encoding.INSTANCE, ISO8859_9Encoding.INSTANCE, ISO8859_10Encoding.INSTANCE,
ISO8859_13Encoding.INSTANCE, ISO8859_14Encoding.INSTANCE, ISO8859_15Encoding.INSTANCE,
ISO8859_16Encoding.INSTANCE, Windows_1250Encoding.INSTANCE, Windows_1252Encoding.INSTANCE,
Windows_1254Encoding.INSTANCE, Windows_1257Encoding.INSTANCE);
for (Encoding enc: list) {
for (int n = 1; n <= 64; n++) {
byte[] from = repeat("", (char) 0xdf, n);
String msg = enc + " " + n;
assertArrayEquals(msg, repeat("SS", n), caseMapAll(enc, from, Config.CASE_UPCASE));
assertArrayEquals(msg, repeat("ss", n), caseMapAll(enc, from, Config.CASE_DOWNCASE | Config.CASE_FOLD));
assertArrayEquals(msg, repeat("SS", n), caseMapAll(enc, from, Config.CASE_UPCASE | Config.CASE_DOWNCASE));
byte[] prefixed = caseMapAll(enc, repeat("a", (char) 0xdf, n), Config.CASE_UPCASE);
assertArrayEquals(msg, repeat("SS", n), Arrays.copyOfRange(prefixed, 1, prefixed.length));
}
}
}

@Test
public void testGeorgian() throws Exception {
Encoding enc = UTF8Encoding.INSTANCE;
Expand Down