mime: respect charset header (default to utf-8)

This commit is contained in:
Vincent Breitmoser
2015-09-16 19:54:57 +02:00
parent ece06b1933
commit 6624d1f830
7 changed files with 21 additions and 25 deletions

View File

@@ -172,7 +172,13 @@ public class InputDataOperation extends BaseOperation<InputDataParcel> {
log.add(LogType.MSG_DATA_MIME_LENGTH, 3, totalLength); log.add(LogType.MSG_DATA_MIME_LENGTH, 3, totalLength);
OpenPgpMetadata metadata = new OpenPgpMetadata(mFilename, bd.getMimeType(), 0L, totalLength); String charset = bd.getCharset();
// the charset defaults to us-ascii, but we want to default to utf-8
if ("us-ascii".equals(charset)) {
charset = "utf-8";
}
OpenPgpMetadata metadata = new OpenPgpMetadata(mFilename, bd.getMimeType(), 0L, totalLength, charset);
out.close(); out.close();
outputUris.add(uri); outputUris.add(uri);

View File

@@ -34,9 +34,6 @@ public class DecryptVerifyResult extends InputPendingResult {
OpenPgpSignatureResult mSignatureResult; OpenPgpSignatureResult mSignatureResult;
OpenPgpDecryptionResult mDecryptionResult; OpenPgpDecryptionResult mDecryptionResult;
OpenPgpMetadata mDecryptionMetadata; OpenPgpMetadata mDecryptionMetadata;
// This holds the charset which was specified in the ascii armor, if specified
// https://tools.ietf.org/html/rfc4880#page56
String mCharset;
CryptoInputParcel mCachedCryptoInputParcel; CryptoInputParcel mCachedCryptoInputParcel;
@@ -96,14 +93,6 @@ public class DecryptVerifyResult extends InputPendingResult {
mDecryptionMetadata = decryptMetadata; mDecryptionMetadata = decryptMetadata;
} }
public String getCharset () {
return mCharset;
}
public void setCharset(String charset) {
mCharset = charset;
}
public void setOutputBytes(byte[] outputBytes) { public void setOutputBytes(byte[] outputBytes) {
mOutputBytes = outputBytes; mOutputBytes = outputBytes;
} }

View File

@@ -556,12 +556,12 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
originalFilename, originalFilename,
mimeType, mimeType,
literalData.getModificationTime().getTime(), literalData.getModificationTime().getTime(),
originalSize == null ? 0 : originalSize); originalSize == null ? 0 : originalSize,
charset);
log.add(LogType.MSG_DC_OK_META_ONLY, indent); log.add(LogType.MSG_DC_OK_META_ONLY, indent);
DecryptVerifyResult result = DecryptVerifyResult result =
new DecryptVerifyResult(DecryptVerifyResult.RESULT_OK, log); new DecryptVerifyResult(DecryptVerifyResult.RESULT_OK, log);
result.setCharset(charset);
result.setDecryptionMetadata(metadata); result.setDecryptionMetadata(metadata);
return result; return result;
} }
@@ -607,7 +607,7 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
} }
metadata = new OpenPgpMetadata( metadata = new OpenPgpMetadata(
originalFilename, mimeType, literalData.getModificationTime().getTime(), alreadyWritten); originalFilename, mimeType, literalData.getModificationTime().getTime(), alreadyWritten, charset);
if (signature != null) { if (signature != null) {
updateProgress(R.string.progress_verifying_signature, 90, 100); updateProgress(R.string.progress_verifying_signature, 90, 100);
@@ -663,7 +663,6 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
result.setCachedCryptoInputParcel(cryptoInput); result.setCachedCryptoInputParcel(cryptoInput);
result.setSignatureResult(signatureResultBuilder.build()); result.setSignatureResult(signatureResultBuilder.build());
result.setCharset(charset);
result.setDecryptionResult(decryptionResultBuilder.build()); result.setDecryptionResult(decryptionResultBuilder.build());
result.setDecryptionMetadata(metadata); result.setDecryptionMetadata(metadata);

View File

@@ -628,15 +628,14 @@ public class OpenPgpService extends RemoteService {
} }
} }
OpenPgpMetadata metadata = pgpResult.getDecryptionMetadata();
if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION, -1) >= 4) { if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION, -1) >= 4) {
OpenPgpMetadata metadata = pgpResult.getDecryptionMetadata();
if (metadata != null) { if (metadata != null) {
result.putExtra(OpenPgpApi.RESULT_METADATA, metadata); result.putExtra(OpenPgpApi.RESULT_METADATA, metadata);
} }
} }
String charset = pgpResult.getCharset(); String charset = metadata != null ? metadata.getCharset() : null;
if (charset != null) { if (charset != null) {
result.putExtra(OpenPgpApi.RESULT_CHARSET, charset); result.putExtra(OpenPgpApi.RESULT_CHARSET, charset);
} }

View File

@@ -472,7 +472,8 @@ public class DecryptListFragment
Intent intent = new Intent(activity, DisplayTextActivity.class); Intent intent = new Intent(activity, DisplayTextActivity.class);
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(outputUri, "text/plain"); intent.setDataAndType(outputUri, "text/plain");
intent.putExtra(DisplayTextActivity.EXTRA_METADATA, result.mDecryptVerifyResult); intent.putExtra(DisplayTextActivity.EXTRA_RESULT, result.mDecryptVerifyResult);
intent.putExtra(DisplayTextActivity.EXTRA_METADATA, metadata);
activity.startActivity(intent); activity.startActivity(intent);
} else { } else {

View File

@@ -25,9 +25,9 @@ import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Toast; import android.widget.Toast;
import org.openintents.openpgp.OpenPgpMetadata;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.ui.base.BaseActivity; import org.sufficientlysecure.keychain.ui.base.BaseActivity;
@@ -35,6 +35,7 @@ import org.sufficientlysecure.keychain.util.FileHelper;
public class DisplayTextActivity extends BaseActivity { public class DisplayTextActivity extends BaseActivity {
public static final String EXTRA_RESULT = "result";
public static final String EXTRA_METADATA = "metadata"; public static final String EXTRA_METADATA = "metadata";
@Override @Override
@@ -60,11 +61,12 @@ public class DisplayTextActivity extends BaseActivity {
return; return;
} }
DecryptVerifyResult result = intent.getParcelableExtra(EXTRA_METADATA); DecryptVerifyResult result = intent.getParcelableExtra(EXTRA_RESULT);
OpenPgpMetadata metadata = intent.getParcelableExtra(EXTRA_METADATA);
String plaintext; String plaintext;
try { try {
plaintext = FileHelper.readTextFromUri(this, intent.getData(), result.getCharset()); plaintext = FileHelper.readTextFromUri(this, intent.getData(), metadata.getCharset());
} catch (IOException e) { } catch (IOException e) {
Toast.makeText(this, R.string.error_preparing_data, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.error_preparing_data, Toast.LENGTH_LONG).show();
return; return;

View File

@@ -792,9 +792,9 @@ public class PgpEncryptDecryptTest {
Assert.assertArrayEquals("decrypted ciphertext should equal plaintext bytes", Assert.assertArrayEquals("decrypted ciphertext should equal plaintext bytes",
out.toByteArray(), plaindata); out.toByteArray(), plaindata);
Assert.assertEquals("charset should be read correctly", Assert.assertEquals("charset should be read correctly",
"iso-2022-jp", result.getCharset()); "iso-2022-jp", result.getDecryptionMetadata().getCharset());
Assert.assertEquals("decrypted ciphertext should equal plaintext", Assert.assertEquals("decrypted ciphertext should equal plaintext",
new String(out.toByteArray(), result.getCharset()), plaintext); new String(out.toByteArray(), result.getDecryptionMetadata().getCharset()), plaintext);
Assert.assertEquals("decryptionResult should be RESULT_ENCRYPTED", Assert.assertEquals("decryptionResult should be RESULT_ENCRYPTED",
OpenPgpDecryptionResult.RESULT_ENCRYPTED, result.getDecryptionResult().getResult()); OpenPgpDecryptionResult.RESULT_ENCRYPTED, result.getDecryptionResult().getResult());
Assert.assertEquals("signatureResult should be RESULT_NO_SIGNATURE", Assert.assertEquals("signatureResult should be RESULT_NO_SIGNATURE",