diff --git a/briar-api/src/org/briarproject/api/Author.java b/briar-api/src/org/briarproject/api/Author.java index 0ff392f9f964cccb6e0a1c857a6ffeacde2fb79b..5c77dad6082d967409ee60a6b054f5440b8186ee 100644 --- a/briar-api/src/org/briarproject/api/Author.java +++ b/briar-api/src/org/briarproject/api/Author.java @@ -2,7 +2,7 @@ package org.briarproject.api; import static org.briarproject.api.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; /** A pseudonym for a user. */ public class Author { @@ -14,13 +14,9 @@ public class Author { private final byte[] publicKey; public Author(AuthorId id, String name, byte[] publicKey) { - if(name.length() == 0) throw new IllegalArgumentException(); - try { - if(name.getBytes("UTF-8").length > MAX_AUTHOR_NAME_LENGTH) - throw new IllegalArgumentException(); - } catch(UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + int length = name.getBytes(Charset.forName("UTF-8")).length; + if(length == 0 || length > MAX_AUTHOR_NAME_LENGTH) + throw new IllegalArgumentException(); this.id = id; this.name = name; this.publicKey = publicKey; diff --git a/briar-api/src/org/briarproject/api/messaging/Group.java b/briar-api/src/org/briarproject/api/messaging/Group.java index b7662e276ddd519d71c46116245fe8d02e83cd78..20488c021dac3bc782e5a50cc4ebeaf9e15d4c29 100644 --- a/briar-api/src/org/briarproject/api/messaging/Group.java +++ b/briar-api/src/org/briarproject/api/messaging/Group.java @@ -3,7 +3,7 @@ package org.briarproject.api.messaging; import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH; import static org.briarproject.api.messaging.MessagingConstants.MAX_GROUP_NAME_LENGTH; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; /** A group to which users may subscribe. */ public class Group { @@ -13,13 +13,9 @@ public class Group { private final byte[] salt; public Group(GroupId id, String name, byte[] salt) { - if(name.length() == 0) throw new IllegalArgumentException(); - try { - if(name.getBytes("UTF-8").length > MAX_GROUP_NAME_LENGTH) - throw new IllegalArgumentException(); - } catch(UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + int length = name.getBytes(Charset.forName("UTF-8")).length; + if(length == 0 || length > MAX_GROUP_NAME_LENGTH) + throw new IllegalArgumentException(); if(salt.length != GROUP_SALT_LENGTH) throw new IllegalArgumentException(); this.id = id; diff --git a/briar-core/src/org/briarproject/messaging/MessageFactoryImpl.java b/briar-core/src/org/briarproject/messaging/MessageFactoryImpl.java index f32905e5333db5506849af3128df24418f11b565..4b144ad5b3095ea6a00ced620020b6e91b9d5fa6 100644 --- a/briar-core/src/org/briarproject/messaging/MessageFactoryImpl.java +++ b/briar-core/src/org/briarproject/messaging/MessageFactoryImpl.java @@ -31,6 +31,7 @@ import org.briarproject.api.serial.DigestingConsumer; import org.briarproject.api.serial.SigningConsumer; import org.briarproject.api.serial.Writer; import org.briarproject.api.serial.WriterFactory; +import org.briarproject.util.StringUtils; class MessageFactoryImpl implements MessageFactory { @@ -68,7 +69,7 @@ class MessageFactoryImpl implements MessageFactory { // Validate the arguments if((author == null) != (privateKey == null)) throw new IllegalArgumentException(); - if(contentType.getBytes("UTF-8").length > MAX_CONTENT_TYPE_LENGTH) + if(StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH) throw new IllegalArgumentException(); if(body.length > MAX_BODY_LENGTH) throw new IllegalArgumentException(); diff --git a/briar-core/src/org/briarproject/serial/ReaderImpl.java b/briar-core/src/org/briarproject/serial/ReaderImpl.java index b8710776f6c712bf77a0211156670d620e9f8fae..4398485dc98ad04f3dd865752fbdbee0563e1b12 100644 --- a/briar-core/src/org/briarproject/serial/ReaderImpl.java +++ b/briar-core/src/org/briarproject/serial/ReaderImpl.java @@ -21,6 +21,7 @@ import static org.briarproject.serial.Tag.TRUE; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; @@ -234,7 +235,7 @@ class ReaderImpl implements Reader { if(length < 0 || length > maxLength) throw new FormatException(); if(length == 0) return ""; readIntoBuffer(length, true); - return new String(buf, 0, length, "UTF-8"); + return new String(buf, 0, length, Charset.forName("UTF-8")); } private int readStringLength(boolean consume) throws IOException { diff --git a/briar-core/src/org/briarproject/serial/WriterImpl.java b/briar-core/src/org/briarproject/serial/WriterImpl.java index 45995cdb0ef07697806671c69434b6debc5d084b..5555573a8d22d324a6ce96aff07afe6ade02b29e 100644 --- a/briar-core/src/org/briarproject/serial/WriterImpl.java +++ b/briar-core/src/org/briarproject/serial/WriterImpl.java @@ -16,6 +16,7 @@ import static org.briarproject.serial.Tag.TRUE; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -102,7 +103,7 @@ class WriterImpl implements Writer { } public void writeString(String s) throws IOException { - byte[] b = s.getBytes("UTF-8"); + byte[] b = s.getBytes(Charset.forName("UTF-8")); if(b.length <= Byte.MAX_VALUE) { write(STRING_8); write((byte) b.length); diff --git a/briar-desktop/src/org/briarproject/plugins/modem/ModemImpl.java b/briar-desktop/src/org/briarproject/plugins/modem/ModemImpl.java index 52817c7320d5d5f963814c4682c4123ff0307e10..45fec1e13a411cd3472f79e831abefda9323406b 100644 --- a/briar-desktop/src/org/briarproject/plugins/modem/ModemImpl.java +++ b/briar-desktop/src/org/briarproject/plugins/modem/ModemImpl.java @@ -307,6 +307,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener { for(int i = 0; i < b.length; i++) { line[lineLen] = b[i]; if(b[i] == '\n') { + // FIXME: Use CharsetDecoder to catch invalid ASCII String s = new String(line, 0, lineLen, "US-ASCII").trim(); lineLen = 0; if(LOG.isLoggable(INFO)) LOG.info("Modem status: " + s);