Add automatic decryption capabilities. (A3)

Bug: 7388852
Change-Id: Ie952b1ad6cfad2f83793bd968e159d3cfbf20a47
This commit is contained in:
Jean Chalard 2012-10-25 15:13:40 +09:00
parent b3c98901c5
commit 0044df6cf2
2 changed files with 59 additions and 2 deletions

View file

@ -24,8 +24,6 @@ import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
@ -42,6 +40,7 @@ public final class BinaryDictOffdeviceUtils {
private final static String SUFFIX = ".tmp";
public final static String COMPRESSION = "compression";
public final static String ENCRYPTION = "encryption";
public static class DecoderChainSpec {
ArrayList<String> mDecoderSpec = new ArrayList<String>();
@ -88,6 +87,14 @@ public final class BinaryDictOffdeviceUtils {
if (null == newSpec) return null;
return newSpec.addStep(COMPRESSION);
}
// It's not a compressed either - try to see if it's crypted.
final File decryptedFile = tryGetDecryptedFile(src);
if (null != decryptedFile) {
final DecoderChainSpec newSpec =
getRawBinaryDictionaryOrNullInternal(spec, decryptedFile);
if (null == newSpec) return null;
return newSpec.addStep(ENCRYPTION);
}
return null;
}
@ -108,4 +115,22 @@ public final class BinaryDictOffdeviceUtils {
return null;
}
}
/* Try to decrypt the file passed as an argument.
*
* If the file can be decrypted, the decrypted version is returned. Otherwise, null
* is returned.
*/
private static File tryGetDecryptedFile(final File src) {
try {
final File dst = File.createTempFile(PREFIX, SUFFIX);
final FileOutputStream dstStream = new FileOutputStream(dst);
copy(Crypt.getDecryptedStream(new BufferedInputStream(new FileInputStream(src))),
dstStream); // #copy() closes the streams
return dst;
} catch (IOException e) {
// Could not uncompress the file: presumably the file is simply not a compressed file
return null;
}
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.inputmethod.latin.dicttool;
import java.io.InputStream;
import java.io.OutputStream;
public class Crypt {
public static OutputStream getCryptedStream(final OutputStream out) {
// Encryption is not supported
return out;
}
public static InputStream getDecryptedStream(final InputStream in) {
// Decryption is not supported
return in;
}
}