From eeeec21baceea10bd1f96cfc5395fae7ec837f0c Mon Sep 17 00:00:00 2001 From: Mohammadinamul Sheik Date: Fri, 20 Feb 2015 18:30:23 -0800 Subject: [PATCH] Make checksum and header checks decoder dependent. Change-Id: I0ec4aa69d9b5f013ae926cc368e25225d9d3073b --- .../define/DecoderSpecificConstants.java | 3 ++ .../dictionarypack/MetadataDbHelper.java | 2 +- .../latin/BinaryDictionaryFileDumper.java | 31 +++++++++++++------ .../latin/BinaryDictionaryGetter.java | 8 +++++ .../latin/makedict/FormatSpec.java | 4 ++- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/java-overridable/src/com/android/inputmethod/latin/define/DecoderSpecificConstants.java b/java-overridable/src/com/android/inputmethod/latin/define/DecoderSpecificConstants.java index 4789e208d..9e97b12a5 100644 --- a/java-overridable/src/com/android/inputmethod/latin/define/DecoderSpecificConstants.java +++ b/java-overridable/src/com/android/inputmethod/latin/define/DecoderSpecificConstants.java @@ -30,4 +30,7 @@ public class DecoderSpecificConstants { public static final String DECODER_DICT_SUFFIX = ""; + public static final boolean SHOULD_VERIFY_MAGIC_NUMBER = true; + public static final boolean SHOULD_VERIFY_CHECKSUM = true; + public static final boolean SHOULD_USE_DICT_VERSION = true; } diff --git a/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java b/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java index ce23eb752..73621f474 100644 --- a/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java +++ b/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java @@ -48,7 +48,7 @@ public class MetadataDbHelper extends SQLiteOpenHelper { private static final int METADATA_DATABASE_VERSION_WITH_CLIENTID = 6; // The current database version. // This MUST be increased every time the dictionary pack metadata URL changes. - private static final int CURRENT_METADATA_DATABASE_VERSION = 11; + private static final int CURRENT_METADATA_DATABASE_VERSION = 12; private final static long NOT_A_DOWNLOAD_ID = -1; diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index 10b1f1b77..bc62f3ae3 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -29,6 +29,7 @@ import android.util.Log; import com.android.inputmethod.dictionarypack.DictionaryPackConstants; import com.android.inputmethod.dictionarypack.MD5Calculator; +import com.android.inputmethod.latin.define.DecoderSpecificConstants; import com.android.inputmethod.latin.utils.DictionaryInfoUtils; import com.android.inputmethod.latin.utils.DictionaryInfoUtils.DictionaryInfo; import com.android.inputmethod.latin.utils.FileTransforms; @@ -67,6 +68,11 @@ public final class BinaryDictionaryFileDumper { private static final byte[] MAGIC_NUMBER_VERSION_2 = new byte[] { (byte)0x9B, (byte)0xC1, (byte)0x3A, (byte)0xFE }; + private static final boolean SHOULD_VERIFY_MAGIC_NUMBER = + DecoderSpecificConstants.SHOULD_VERIFY_MAGIC_NUMBER; + private static final boolean SHOULD_VERIFY_CHECKSUM = + DecoderSpecificConstants.SHOULD_VERIFY_CHECKSUM; + private static final String DICTIONARY_PROJECTION[] = { "id" }; private static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt"; @@ -302,13 +308,18 @@ public final class BinaryDictionaryFileDumper { checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream); bufferedOutputStream.flush(); bufferedOutputStream.close(); - final String actualRawChecksum = MD5Calculator.checksum( - new BufferedInputStream(new FileInputStream(outputFile))); - Log.i(TAG, "Computed checksum for downloaded dictionary. Expected = " + rawChecksum - + " ; actual = " + actualRawChecksum); - if (!TextUtils.isEmpty(rawChecksum) && !rawChecksum.equals(actualRawChecksum)) { - throw new IOException("Could not decode the file correctly : checksum differs"); + + if (SHOULD_VERIFY_CHECKSUM) { + final String actualRawChecksum = MD5Calculator.checksum( + new BufferedInputStream(new FileInputStream(outputFile))); + Log.i(TAG, "Computed checksum for downloaded dictionary. Expected = " + + rawChecksum + " ; actual = " + actualRawChecksum); + if (!TextUtils.isEmpty(rawChecksum) && !rawChecksum.equals(actualRawChecksum)) { + throw new IOException( + "Could not decode the file correctly : checksum differs"); + } } + final File finalFile = new File(finalFileName); finalFile.delete(); if (!outputFile.renameTo(finalFile)) { @@ -444,9 +455,11 @@ public final class BinaryDictionaryFileDumper { if (readMagicNumberSize < length) { throw new IOException("Less bytes to read than the magic number length"); } - if (!Arrays.equals(MAGIC_NUMBER_VERSION_2, magicNumberBuffer)) { - if (!Arrays.equals(MAGIC_NUMBER_VERSION_1, magicNumberBuffer)) { - throw new IOException("Wrong magic number for downloaded file"); + if (SHOULD_VERIFY_MAGIC_NUMBER) { + if (!Arrays.equals(MAGIC_NUMBER_VERSION_2, magicNumberBuffer)) { + if (!Arrays.equals(MAGIC_NUMBER_VERSION_1, magicNumberBuffer)) { + throw new IOException("Wrong magic number for downloaded file"); + } } } output.write(magicNumberBuffer); diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 9c70cad0a..e00532aa6 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -22,6 +22,7 @@ import android.content.res.AssetFileDescriptor; import android.util.Log; import com.android.inputmethod.latin.common.LocaleUtils; +import com.android.inputmethod.latin.define.DecoderSpecificConstants; import com.android.inputmethod.latin.makedict.DictionaryHeader; import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; @@ -54,6 +55,9 @@ final public class BinaryDictionaryGetter { */ private static final String COMMON_PREFERENCES_NAME = "LatinImeDictPrefs"; + private static final boolean SHOULD_USE_DICT_VERSION = + DecoderSpecificConstants.SHOULD_USE_DICT_VERSION; + // Name of the category for the main dictionary public static final String MAIN_DICTIONARY_CATEGORY = "main"; public static final String ID_CATEGORY_SEPARATOR = ":"; @@ -224,6 +228,10 @@ final public class BinaryDictionaryGetter { // those do not include whitelist entries, the new code with an old version of the dictionary // would lose whitelist functionality. private static boolean hackCanUseDictionaryFile(final File file) { + if (!SHOULD_USE_DICT_VERSION) { + return true; + } + try { // Read the version of the file final DictionaryHeader header = BinaryDictionaryUtils.getHeader(file); diff --git a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java index 3348a3767..1e9f8e47e 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java +++ b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java @@ -172,6 +172,8 @@ public final class FormatSpec { public static final int VERSION2 = 2; public static final int VERSION201 = 201; public static final int VERSION202 = 202; + // format version for Fava + public static final int VERSION300 = 300; public static final int MINIMUM_SUPPORTED_VERSION_OF_CODE_POINT_TABLE = VERSION201; // Dictionary version used for testing. public static final int VERSION4_ONLY_FOR_TESTING = 399; @@ -180,7 +182,7 @@ public final class FormatSpec { public static final int VERSION4 = VERSION403; public static final int VERSION4_DEV = VERSION403; public static final int MINIMUM_SUPPORTED_STATIC_VERSION = VERSION202; - public static final int MAXIMUM_SUPPORTED_STATIC_VERSION = VERSION202; + public static final int MAXIMUM_SUPPORTED_STATIC_VERSION = VERSION300; static final int MINIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4; static final int MAXIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4_DEV;