Make checksum and header checks decoder dependent.

Change-Id: I0ec4aa69d9b5f013ae926cc368e25225d9d3073b
main
Mohammadinamul Sheik 2015-02-20 18:30:23 -08:00
parent fe3c4ef940
commit eeeec21bac
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 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;
// 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;

View File

@ -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);

View File

@ -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);

View File

@ -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;