From 0044df6cf2f4ef00d78e530220565b8272187446 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 25 Oct 2012 15:13:40 +0900 Subject: [PATCH] Add automatic decryption capabilities. (A3) Bug: 7388852 Change-Id: Ie952b1ad6cfad2f83793bd968e159d3cfbf20a47 --- .../dicttool/BinaryDictOffdeviceUtils.java | 29 +++++++++++++++-- .../inputmethod/latin/dicttool/Crypt.java | 32 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java index 9dcd7eb42..09717eb56 100644 --- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java @@ -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 mDecoderSpec = new ArrayList(); @@ -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; + } + } } diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java new file mode 100644 index 000000000..10a7301d7 --- /dev/null +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java @@ -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; + } +}