aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/InputDataOperation.java4
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java18
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java2
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/CharsetVerifier.java22
-rw-r--r--OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java6
-rw-r--r--OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/CharsetVerifierTest.java5
6 files changed, 32 insertions, 25 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/InputDataOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/InputDataOperation.java
index 0de878232..43fc11b84 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/InputDataOperation.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/InputDataOperation.java
@@ -355,8 +355,8 @@ public class InputDataOperation extends BaseOperation<InputDataParcel> {
log.add(LogType.MSG_DATA_MIME_CHARSET, 3, charsetVerifier.getCharset());
}
- metadata = new OpenPgpMetadata(mFilename, mimeType, 0L, totalLength,
- charsetVerifier.getCharset(), charsetVerifier.isProbablyText());
+ metadata = new OpenPgpMetadata(mFilename, charsetVerifier.getGuessedMimeType(), 0L, totalLength,
+ charsetVerifier.getCharset());
}
out.close();
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java
index b0d39d88f..e15139a7f 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java
@@ -417,7 +417,7 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
metadata = new OpenPgpMetadata(
originalFilename, mimeType,
literalData.getModificationTime().getTime(),
- originalSize == null ? 0 : originalSize, charset, false);
+ originalSize == null ? 0 : originalSize, charset);
log.add(LogType.MSG_DC_OK_META_ONLY, indent);
DecryptVerifyResult result =
@@ -483,24 +483,20 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
Log.d(Constants.TAG, "decrypt time taken: " + String.format("%.2f", opTime / 1000.0) + "s");
// special treatment to detect pgp mime types
+ // TODO move into CharsetVerifier? seems like that would be a plausible place for this logic
if (matchesPrefix(firstBytes, "-----BEGIN PGP PUBLIC KEY BLOCK-----")
|| matchesPrefix(firstBytes, "-----BEGIN PGP PRIVATE KEY BLOCK-----")) {
mimeType = Constants.MIME_TYPE_KEYS;
} else if (matchesPrefix(firstBytes, "-----BEGIN PGP MESSAGE-----")) {
// this is NOT application/pgp-encrypted, see RFC 3156!
mimeType = Constants.MIME_TYPE_ENCRYPTED_ALTERNATE;
- }
-
- log.add(LogType.MSG_DC_CLEAR_META_MIME, indent + 1, mimeType);
-
- if (charsetVerifier.isDefinitelyBinary()) {
- metadata = new OpenPgpMetadata(originalFilename, mimeType, literalData.getModificationTime().getTime(),
- alreadyWritten);
} else {
- metadata = new OpenPgpMetadata(originalFilename, mimeType, literalData.getModificationTime().getTime(),
- alreadyWritten, charsetVerifier.getCharset(), charsetVerifier.isProbablyText());
+ mimeType = charsetVerifier.getGuessedMimeType();
}
+ metadata = new OpenPgpMetadata(originalFilename, mimeType, literalData.getModificationTime().getTime(),
+ alreadyWritten, charsetVerifier.getCharset());
+ log.add(LogType.MSG_DC_CLEAR_META_MIME, indent + 1, mimeType);
Log.d(Constants.TAG, metadata.toString());
indent -= 1;
@@ -883,7 +879,7 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
log.add(LogType.MSG_DC_OK, indent);
- OpenPgpMetadata metadata = new OpenPgpMetadata("", "text/plain", -1, clearText.length, "utf-8", true);
+ OpenPgpMetadata metadata = new OpenPgpMetadata("", "text/plain", -1, clearText.length, "utf-8");
DecryptVerifyResult result = new DecryptVerifyResult(DecryptVerifyResult.RESULT_OK, log);
result.setSignatureResult(signatureChecker.getSignatureResult());
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java
index 9ffd95ae9..6f85342d6 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java
@@ -592,7 +592,7 @@ public class DecryptListFragment
Intent chooserIntent = Intent.createChooser(intent, getString(R.string.intent_show));
chooserIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
- if (!share && metadata.isLooksLikeText()) {
+ if (!share && ClipDescription.compareMimeTypes(metadata.getMimeType(), "text/*")) {
LabeledIntent internalIntent = new LabeledIntent(
new Intent(intent)
.setClass(activity, DisplayTextActivity.class)
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/CharsetVerifier.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/CharsetVerifier.java
index c1d11cc26..c03decc89 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/CharsetVerifier.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/CharsetVerifier.java
@@ -33,19 +33,21 @@ public class CharsetVerifier {
private boolean isPossibleTextMimeType;
private boolean isTextMimeType;
private String charset;
+ private String mimeType;
- public CharsetVerifier(@NonNull byte[] buf, String mimeType, @Nullable String charset) {
+ public CharsetVerifier(@NonNull byte[] buf, @NonNull String mimeType, @Nullable String charset) {
- isPossibleTextMimeType = ClipDescription.compareMimeTypes(mimeType, "application/octet-stream")
- || ClipDescription.compareMimeTypes(mimeType, "application/x-download")
- || ClipDescription.compareMimeTypes(mimeType, "text/*");
+ this.mimeType = mimeType;
+ isTextMimeType = ClipDescription.compareMimeTypes(mimeType, "text/*");
+ isPossibleTextMimeType = isTextMimeType
+ || ClipDescription.compareMimeTypes(mimeType, "application/octet-stream")
+ || ClipDescription.compareMimeTypes(mimeType, "application/x-download");
if (!isPossibleTextMimeType) {
charsetDecoder = null;
bufWrap = null;
dummyOutput = null;
return;
}
- isTextMimeType = ClipDescription.compareMimeTypes(mimeType, "text/*");
bufWrap = ByteBuffer.wrap(buf);
dummyOutput = CharBuffer.allocate(buf.length);
@@ -96,6 +98,16 @@ public class CharsetVerifier {
}
}
+ public String getGuessedMimeType() {
+ if (isTextMimeType) {
+ return mimeType;
+ }
+ if (isProbablyText()) {
+ return "text/plain";
+ }
+ return mimeType;
+ }
+
public boolean isCharsetFaulty() {
finishIfNecessary();
return isFaulty;
diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java
index 013465c44..fba8be061 100644
--- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java
+++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/InputDataOperationTest.java
@@ -162,7 +162,6 @@ public class InputDataOperationTest {
OpenPgpMetadata metadata = result.mMetadata.get(0);
Assert.assertEquals("text/plain", metadata.getMimeType());
Assert.assertEquals("utf-8", metadata.getCharset());
- Assert.assertTrue("data should be looksLikeText", metadata.isLooksLikeText());
metadata = result.mMetadata.get(1);
Assert.assertEquals("text/testvalue", metadata.getMimeType());
@@ -214,7 +213,6 @@ public class InputDataOperationTest {
OpenPgpMetadata metadata = result.mMetadata.get(0);
Assert.assertEquals("text/plain", metadata.getMimeType());
- Assert.assertTrue("data should be looksLikeText", metadata.isLooksLikeText());
Assert.assertNull("charset was bad so it should not be set", metadata.getCharset());
Assert.assertTrue("faulty charset should have been detected",
@@ -236,7 +234,6 @@ public class InputDataOperationTest {
OpenPgpMetadata metadata = result.mMetadata.get(0);
Assert.assertEquals("text/plain", metadata.getMimeType());
- Assert.assertTrue("data should be looksLikeText", metadata.isLooksLikeText());
Assert.assertEquals("charset should be set since it was guessed and not faulty",
"utf-8", metadata.getCharset());
@@ -258,8 +255,7 @@ public class InputDataOperationTest {
Assert.assertNull(result.mDecryptVerifyResult);
OpenPgpMetadata metadata = result.mMetadata.get(0);
- Assert.assertEquals("application/octet-stream", metadata.getMimeType());
- Assert.assertTrue("data should be looksLikeText", metadata.isLooksLikeText());
+ Assert.assertEquals("text/plain", metadata.getMimeType());
Assert.assertEquals("charset should be set since it was guessed and not faulty",
"utf-8", metadata.getCharset());
diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/CharsetVerifierTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/CharsetVerifierTest.java
index 8ad6a021f..a87c1595e 100644
--- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/CharsetVerifierTest.java
+++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/CharsetVerifierTest.java
@@ -34,13 +34,14 @@ public class CharsetVerifierTest {
bytes[4] = (byte) 0xc3;
bytes[5] = (byte) 0x28;
- CharsetVerifier charsetVerifier = new CharsetVerifier(bytes, "text/plain", "utf-8");
+ CharsetVerifier charsetVerifier = new CharsetVerifier(bytes, "text/something", "utf-8");
charsetVerifier.readBytesFromBuffer(0, bytes.length);
assertFalse("text/plain should not be marked as binary, even if it is", charsetVerifier.isDefinitelyBinary());
assertTrue("text/plain should be marked as text, even if it isn't valid", charsetVerifier.isProbablyText());
assertTrue("encoding contained illegal chars, so it should be marked as faulty", charsetVerifier.isCharsetFaulty());
assertFalse("charset was specified and should not be marked as guessed", charsetVerifier.isCharsetGuessed());
+ assertEquals("mimetype should be preserved", "text/something", charsetVerifier.getGuessedMimeType());
assertEquals("charset should be utf-8 since it was given explicitly", "utf-8", charsetVerifier.getCharset());
assertEquals("charset should be utf-8 since it was given explicitly", "utf-8", charsetVerifier.getMaybeFaultyCharset());
}
@@ -59,6 +60,7 @@ public class CharsetVerifierTest {
assertTrue("encoding contained illegal chars, so it should be marked as faulty", charsetVerifier.isCharsetFaulty());
assertTrue("charset was guessed and should be marked as such", charsetVerifier.isCharsetGuessed());
assertNull("charset should be null since the guess was faulty", charsetVerifier.getCharset());
+ assertEquals("mimetype should be set to text", "text/plain", charsetVerifier.getGuessedMimeType());
assertEquals("maybe-faulty charset should be utf-8", "utf-8", charsetVerifier.getMaybeFaultyCharset());
}
@@ -73,6 +75,7 @@ public class CharsetVerifierTest {
assertTrue("application/octet-stream with text content should be probably text", charsetVerifier.isProbablyText());
assertFalse("detected charset should not be faulty", charsetVerifier.isCharsetFaulty());
assertTrue("charset was guessed and should be marked as such", charsetVerifier.isCharsetGuessed());
+ assertEquals("mimetype should be set to text", "text/plain", charsetVerifier.getGuessedMimeType());
assertEquals("guessed charset is utf-8", "utf-8", charsetVerifier.getCharset());
}