backend support for charset in ascii-armored streams

This commit is contained in:
Vincent Breitmoser
2015-01-27 09:17:23 +01:00
parent efe5c80b1c
commit 6c80025ead
8 changed files with 120 additions and 4 deletions

View File

@@ -234,6 +234,22 @@ public class PgpDecryptVerify extends BaseOperation {
boolean symmetricPacketFound = false;
boolean anyPacketFound = false;
// If the input stream is armored, and there is a charset specified, take a note for later
// https://tools.ietf.org/html/rfc4880#page56
String charset = null;
if (in instanceof ArmoredInputStream) {
for (String header : ((ArmoredInputStream) in).getArmorHeaders()) {
String[] pieces = header.split(":", 2);
if (pieces.length == 2 && "charset".equalsIgnoreCase(pieces[0])) {
charset = pieces[1].trim();
break;
}
}
if (charset != null) {
log.add(LogType.MSG_DC_CHARSET, indent, charset);
}
}
// go through all objects and find one we can decrypt
while (it.hasNext()) {
Object obj = it.next();
@@ -550,6 +566,7 @@ public class PgpDecryptVerify extends BaseOperation {
log.add(LogType.MSG_DC_OK_META_ONLY, indent);
DecryptVerifyResult result =
new DecryptVerifyResult(DecryptVerifyResult.RESULT_OK, log);
result.setCharset(charset);
result.setDecryptMetadata(metadata);
return result;
}
@@ -647,6 +664,7 @@ public class PgpDecryptVerify extends BaseOperation {
new DecryptVerifyResult(DecryptVerifyResult.RESULT_OK, log);
result.setDecryptMetadata(metadata);
result.setSignatureResult(signatureResultBuilder.build());
result.setCharset(charset);
return result;
}
@@ -807,7 +825,7 @@ public class PgpDecryptVerify extends BaseOperation {
while ((ch = fIn.read()) >= 0) {
bOut.write(ch);
if (ch == '\r' || ch == '\n') {
lookAhead = readPassedEOL(bOut, ch, fIn);
lookAhead = readPastEOL(bOut, ch, fIn);
break;
}
}
@@ -824,7 +842,7 @@ public class PgpDecryptVerify extends BaseOperation {
do {
bOut.write(ch);
if (ch == '\r' || ch == '\n') {
lookAhead = readPassedEOL(bOut, ch, fIn);
lookAhead = readPastEOL(bOut, ch, fIn);
break;
}
} while ((ch = fIn.read()) >= 0);
@@ -836,7 +854,7 @@ public class PgpDecryptVerify extends BaseOperation {
return lookAhead;
}
private static int readPassedEOL(ByteArrayOutputStream bOut, int lastCh, InputStream fIn)
private static int readPastEOL(ByteArrayOutputStream bOut, int lastCh, InputStream fIn)
throws IOException {
int lookAhead = fIn.read();

View File

@@ -81,6 +81,7 @@ public class PgpSignEncrypt extends BaseOperation {
private boolean mCleartextInput;
private String mOriginalFilename;
private boolean mFailOnMissingEncryptionKeyIds;
private String mCharset;
private byte[] mNfcSignedHash = null;
private Date mNfcCreationTimestamp = null;
@@ -118,6 +119,7 @@ public class PgpSignEncrypt extends BaseOperation {
this.mNfcCreationTimestamp = builder.mNfcCreationTimestamp;
this.mOriginalFilename = builder.mOriginalFilename;
this.mFailOnMissingEncryptionKeyIds = builder.mFailOnMissingEncryptionKeyIds;
this.mCharset = builder.mCharset;
}
public static class Builder {
@@ -145,6 +147,7 @@ public class PgpSignEncrypt extends BaseOperation {
private byte[] mNfcSignedHash = null;
private Date mNfcCreationTimestamp = null;
private boolean mFailOnMissingEncryptionKeyIds = false;
private String mCharset = null;
public Builder(Context context, ProviderHelper providerHelper, Progressable progressable,
InputData data, OutputStream outStream) {
@@ -211,6 +214,11 @@ public class PgpSignEncrypt extends BaseOperation {
return this;
}
public Builder setCharset(String charset) {
mCharset = charset;
return this;
}
/**
* Also encrypt with the signing keyring
*
@@ -283,6 +291,10 @@ public class PgpSignEncrypt extends BaseOperation {
if (mVersionHeader != null) {
armorOut.setHeader("Version", mVersionHeader);
}
// if we have a charset, put it in the header
if (mCharset != null) {
armorOut.setHeader("Charset", mCharset);
}
out = armorOut;
} else {
out = mOutStream;