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);
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();
outputUris.add(uri);

View file

@ -34,9 +34,6 @@ public class DecryptVerifyResult extends InputPendingResult {
OpenPgpSignatureResult mSignatureResult;
OpenPgpDecryptionResult mDecryptionResult;
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;
@ -96,14 +93,6 @@ public class DecryptVerifyResult extends InputPendingResult {
mDecryptionMetadata = decryptMetadata;
}
public String getCharset () {
return mCharset;
}
public void setCharset(String charset) {
mCharset = charset;
}
public void setOutputBytes(byte[] outputBytes) {
mOutputBytes = outputBytes;
}

View file

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

View file

@ -472,7 +472,8 @@ public class DecryptListFragment
Intent intent = new Intent(activity, DisplayTextActivity.class);
intent.setAction(Intent.ACTION_VIEW);
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);
} else {

View file

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

View file

@ -792,9 +792,9 @@ public class PgpEncryptDecryptTest {
Assert.assertArrayEquals("decrypted ciphertext should equal plaintext bytes",
out.toByteArray(), plaindata);
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",
new String(out.toByteArray(), result.getCharset()), plaintext);
new String(out.toByteArray(), result.getDecryptionMetadata().getCharset()), plaintext);
Assert.assertEquals("decryptionResult should be RESULT_ENCRYPTED",
OpenPgpDecryptionResult.RESULT_ENCRYPTED, result.getDecryptionResult().getResult());
Assert.assertEquals("signatureResult should be RESULT_NO_SIGNATURE",