Merge "Make checksum and header checks decoder dependent."

main
Mohammadinamul Sheik 2015-02-23 21:47:27 +00:00 committed by Android (Google) Code Review
commit bae3c4bac5
5 changed files with 37 additions and 11 deletions

View File

@ -30,4 +30,7 @@ public class DecoderSpecificConstants {
public static final String DECODER_DICT_SUFFIX = ""; 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;
} }

View File

@ -48,7 +48,7 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
private static final int METADATA_DATABASE_VERSION_WITH_CLIENTID = 6; private static final int METADATA_DATABASE_VERSION_WITH_CLIENTID = 6;
// The current database version. // The current database version.
// This MUST be increased every time the dictionary pack metadata URL changes. // 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; private final static long NOT_A_DOWNLOAD_ID = -1;

View File

@ -29,6 +29,7 @@ import android.util.Log;
import com.android.inputmethod.dictionarypack.DictionaryPackConstants; import com.android.inputmethod.dictionarypack.DictionaryPackConstants;
import com.android.inputmethod.dictionarypack.MD5Calculator; 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;
import com.android.inputmethod.latin.utils.DictionaryInfoUtils.DictionaryInfo; import com.android.inputmethod.latin.utils.DictionaryInfoUtils.DictionaryInfo;
import com.android.inputmethod.latin.utils.FileTransforms; import com.android.inputmethod.latin.utils.FileTransforms;
@ -67,6 +68,11 @@ public final class BinaryDictionaryFileDumper {
private static final byte[] MAGIC_NUMBER_VERSION_2 = private static final byte[] MAGIC_NUMBER_VERSION_2 =
new byte[] { (byte)0x9B, (byte)0xC1, (byte)0x3A, (byte)0xFE }; 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 DICTIONARY_PROJECTION[] = { "id" };
private static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt"; private static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt";
@ -302,13 +308,18 @@ public final class BinaryDictionaryFileDumper {
checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream); checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream);
bufferedOutputStream.flush(); bufferedOutputStream.flush();
bufferedOutputStream.close(); bufferedOutputStream.close();
if (SHOULD_VERIFY_CHECKSUM) {
final String actualRawChecksum = MD5Calculator.checksum( final String actualRawChecksum = MD5Calculator.checksum(
new BufferedInputStream(new FileInputStream(outputFile))); new BufferedInputStream(new FileInputStream(outputFile)));
Log.i(TAG, "Computed checksum for downloaded dictionary. Expected = " + rawChecksum Log.i(TAG, "Computed checksum for downloaded dictionary. Expected = "
+ " ; actual = " + actualRawChecksum); + rawChecksum + " ; actual = " + actualRawChecksum);
if (!TextUtils.isEmpty(rawChecksum) && !rawChecksum.equals(actualRawChecksum)) { if (!TextUtils.isEmpty(rawChecksum) && !rawChecksum.equals(actualRawChecksum)) {
throw new IOException("Could not decode the file correctly : checksum differs"); throw new IOException(
"Could not decode the file correctly : checksum differs");
} }
}
final File finalFile = new File(finalFileName); final File finalFile = new File(finalFileName);
finalFile.delete(); finalFile.delete();
if (!outputFile.renameTo(finalFile)) { if (!outputFile.renameTo(finalFile)) {
@ -444,11 +455,13 @@ public final class BinaryDictionaryFileDumper {
if (readMagicNumberSize < length) { if (readMagicNumberSize < length) {
throw new IOException("Less bytes to read than the magic number length"); throw new IOException("Less bytes to read than the magic number length");
} }
if (SHOULD_VERIFY_MAGIC_NUMBER) {
if (!Arrays.equals(MAGIC_NUMBER_VERSION_2, magicNumberBuffer)) { if (!Arrays.equals(MAGIC_NUMBER_VERSION_2, magicNumberBuffer)) {
if (!Arrays.equals(MAGIC_NUMBER_VERSION_1, magicNumberBuffer)) { if (!Arrays.equals(MAGIC_NUMBER_VERSION_1, magicNumberBuffer)) {
throw new IOException("Wrong magic number for downloaded file"); throw new IOException("Wrong magic number for downloaded file");
} }
} }
}
output.write(magicNumberBuffer); output.write(magicNumberBuffer);
// Actually copy the file // Actually copy the file

View File

@ -22,6 +22,7 @@ import android.content.res.AssetFileDescriptor;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.common.LocaleUtils; 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.DictionaryHeader;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; 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 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 // Name of the category for the main dictionary
public static final String MAIN_DICTIONARY_CATEGORY = "main"; public static final String MAIN_DICTIONARY_CATEGORY = "main";
public static final String ID_CATEGORY_SEPARATOR = ":"; 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 // those do not include whitelist entries, the new code with an old version of the dictionary
// would lose whitelist functionality. // would lose whitelist functionality.
private static boolean hackCanUseDictionaryFile(final File file) { private static boolean hackCanUseDictionaryFile(final File file) {
if (!SHOULD_USE_DICT_VERSION) {
return true;
}
try { try {
// Read the version of the file // Read the version of the file
final DictionaryHeader header = BinaryDictionaryUtils.getHeader(file); final DictionaryHeader header = BinaryDictionaryUtils.getHeader(file);

View File

@ -172,6 +172,8 @@ public final class FormatSpec {
public static final int VERSION2 = 2; public static final int VERSION2 = 2;
public static final int VERSION201 = 201; public static final int VERSION201 = 201;
public static final int VERSION202 = 202; 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; public static final int MINIMUM_SUPPORTED_VERSION_OF_CODE_POINT_TABLE = VERSION201;
// Dictionary version used for testing. // Dictionary version used for testing.
public static final int VERSION4_ONLY_FOR_TESTING = 399; 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 = VERSION403;
public static final int VERSION4_DEV = VERSION403; public static final int VERSION4_DEV = VERSION403;
public static final int MINIMUM_SUPPORTED_STATIC_VERSION = VERSION202; 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 MINIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4;
static final int MAXIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4_DEV; static final int MAXIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4_DEV;