am 16a6b2a9: Merge "Make dicttool read the compressed combined format."

* commit '16a6b2a941f153a440ce63d1aaec13e271c684d1':
  Make dicttool read the compressed combined format.
main
Jean Chalard 2013-09-29 23:17:11 -07:00 committed by Android Git Automerger
commit a8c7484237
3 changed files with 20 additions and 18 deletions

View File

@ -80,17 +80,17 @@ public final class BinaryDictOffdeviceUtils {
} }
/** /**
* Returns a decrypted/uncompressed binary dictionary. * Returns a decrypted/uncompressed dictionary.
* *
* This will decrypt/uncompress any number of times as necessary until it finds the binary * This will decrypt/uncompress any number of times as necessary until it finds the
* dictionary signature, and copy the decoded file to a temporary place. * dictionary signature, and copy the decoded file to a temporary place.
* If this is not a binary dictionary, the method returns null. * If this is not a dictionary, the method returns null.
*/ */
public static DecoderChainSpec getRawBinaryDictionaryOrNull(final File src) { public static DecoderChainSpec getRawDictionaryOrNull(final File src) {
return getRawBinaryDictionaryOrNullInternal(new DecoderChainSpec(), src, 0); return getRawDictionaryOrNullInternal(new DecoderChainSpec(), src, 0);
} }
private static DecoderChainSpec getRawBinaryDictionaryOrNullInternal( private static DecoderChainSpec getRawDictionaryOrNullInternal(
final DecoderChainSpec spec, final File src, final int depth) { final DecoderChainSpec spec, final File src, final int depth) {
// Unfortunately the decoding scheme we use can consider any data to be encrypted // Unfortunately the decoding scheme we use can consider any data to be encrypted
// and will product some output, meaning it's not possible to reliably detect encrypted // and will product some output, meaning it's not possible to reliably detect encrypted
@ -98,7 +98,8 @@ public final class BinaryDictOffdeviceUtils {
// over and over, ending in a stack overflow. Hence we limit the depth at which we try // over and over, ending in a stack overflow. Hence we limit the depth at which we try
// decoding the file. // decoding the file.
if (depth > MAX_DECODE_DEPTH) return null; if (depth > MAX_DECODE_DEPTH) return null;
if (BinaryDictDecoderUtils.isBinaryDictionary(src)) { if (BinaryDictDecoderUtils.isBinaryDictionary(src)
|| CombinedInputOutput.isCombinedDictionary(src.getAbsolutePath())) {
spec.mFile = src; spec.mFile = src;
return spec; return spec;
} }
@ -106,7 +107,7 @@ public final class BinaryDictOffdeviceUtils {
final File uncompressedFile = tryGetUncompressedFile(src); final File uncompressedFile = tryGetUncompressedFile(src);
if (null != uncompressedFile) { if (null != uncompressedFile) {
final DecoderChainSpec newSpec = final DecoderChainSpec newSpec =
getRawBinaryDictionaryOrNullInternal(spec, uncompressedFile, depth + 1); getRawDictionaryOrNullInternal(spec, uncompressedFile, depth + 1);
if (null == newSpec) return null; if (null == newSpec) return null;
return newSpec.addStep(COMPRESSION); return newSpec.addStep(COMPRESSION);
} }
@ -114,7 +115,7 @@ public final class BinaryDictOffdeviceUtils {
final File decryptedFile = tryGetDecryptedFile(src); final File decryptedFile = tryGetDecryptedFile(src);
if (null != decryptedFile) { if (null != decryptedFile) {
final DecoderChainSpec newSpec = final DecoderChainSpec newSpec =
getRawBinaryDictionaryOrNullInternal(spec, decryptedFile, depth + 1); getRawDictionaryOrNullInternal(spec, decryptedFile, depth + 1);
if (null == newSpec) return null; if (null == newSpec) return null;
return newSpec.addStep(ENCRYPTION); return newSpec.addStep(ENCRYPTION);
} }
@ -175,15 +176,16 @@ public final class BinaryDictOffdeviceUtils {
return XmlDictInputOutput.readDictionaryXml( return XmlDictInputOutput.readDictionaryXml(
new BufferedInputStream(new FileInputStream(file)), new BufferedInputStream(new FileInputStream(file)),
null /* shortcuts */, null /* bigrams */); null /* shortcuts */, null /* bigrams */);
} else if (CombinedInputOutput.isCombinedDictionary(filename)) {
if (report) System.out.println("Format : Combined format");
return CombinedInputOutput.readDictionaryCombined(
new BufferedInputStream(new FileInputStream(file)));
} else { } else {
final DecoderChainSpec decodedSpec = getRawBinaryDictionaryOrNull(file); final DecoderChainSpec decodedSpec = getRawDictionaryOrNull(file);
if (null == decodedSpec) { if (null == decodedSpec) {
crash(filename, new RuntimeException( crash(filename, new RuntimeException(
filename + " does not seem to be a dictionary file")); filename + " does not seem to be a dictionary file"));
} else if (CombinedInputOutput.isCombinedDictionary(
decodedSpec.mFile.getAbsolutePath())){
if (report) System.out.println("Format : Combined format");
return CombinedInputOutput.readDictionaryCombined(
new BufferedInputStream(new FileInputStream(decodedSpec.mFile)));
} else { } else {
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(decodedSpec.mFile, final DictDecoder dictDecoder = FormatSpec.getDictDecoder(decodedSpec.mFile,
DictDecoder.USE_BYTEARRAY); DictDecoder.USE_BYTEARRAY);

View File

@ -79,7 +79,7 @@ public class Package {
throw new RuntimeException("Too many/too few arguments for command " + COMMAND); throw new RuntimeException("Too many/too few arguments for command " + COMMAND);
} }
final BinaryDictOffdeviceUtils.DecoderChainSpec decodedSpec = final BinaryDictOffdeviceUtils.DecoderChainSpec decodedSpec =
BinaryDictOffdeviceUtils.getRawBinaryDictionaryOrNull(new File(mArgs[0])); BinaryDictOffdeviceUtils.getRawDictionaryOrNull(new File(mArgs[0]));
if (null == decodedSpec) { if (null == decodedSpec) {
System.out.println(mArgs[0] + " does not seem to be a dictionary"); System.out.println(mArgs[0] + " does not seem to be a dictionary");
return; return;

View File

@ -64,7 +64,7 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
// Test for an actually compressed dictionary and its contents // Test for an actually compressed dictionary and its contents
final BinaryDictOffdeviceUtils.DecoderChainSpec decodeSpec = final BinaryDictOffdeviceUtils.DecoderChainSpec decodeSpec =
BinaryDictOffdeviceUtils.getRawBinaryDictionaryOrNull(dst); BinaryDictOffdeviceUtils.getRawDictionaryOrNull(dst);
for (final String step : decodeSpec.mDecoderSpec) { for (final String step : decodeSpec.mDecoderSpec) {
assertEquals("Wrong decode spec", BinaryDictOffdeviceUtils.COMPRESSION, step); assertEquals("Wrong decode spec", BinaryDictOffdeviceUtils.COMPRESSION, step);
} }
@ -90,7 +90,7 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
// Test that a random data file actually fails // Test that a random data file actually fails
assertNull("Wrongly identified data file", assertNull("Wrongly identified data file",
BinaryDictOffdeviceUtils.getRawBinaryDictionaryOrNull(dst)); BinaryDictOffdeviceUtils.getRawDictionaryOrNull(dst));
final File gzDst = File.createTempFile("testGetRawDict", ".tmp"); final File gzDst = File.createTempFile("testGetRawDict", ".tmp");
gzDst.deleteOnExit(); gzDst.deleteOnExit();
@ -103,6 +103,6 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
// Test that a compressed random data file actually fails // Test that a compressed random data file actually fails
assertNull("Wrongly identified data file", assertNull("Wrongly identified data file",
BinaryDictOffdeviceUtils.getRawBinaryDictionaryOrNull(gzDst)); BinaryDictOffdeviceUtils.getRawDictionaryOrNull(gzDst));
} }
} }