Skip to content

Commit

Permalink
Improved header control in ArmoredOutputStream
Browse files Browse the repository at this point in the history
- in particular, supports removing the default version header
  • Loading branch information
peterdettman committed Jun 9, 2017
1 parent 08629c3 commit 53a508e
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions crypto/src/bcpg/ArmoredOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Org.BouncyCastle.Bcpg
public class ArmoredOutputStream
: BaseOutputStream
{
public static readonly string HeaderVersion = "Version";

private static readonly byte[] encodingTable =
{
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
Expand Down Expand Up @@ -101,44 +103,58 @@ private static void Encode(
private static readonly string footerStart = "-----END PGP ";
private static readonly string footerTail = "-----";

private static readonly string version = "BCPG C# v" + AssemblyInfo.Version;
private static readonly string Version = "BCPG C# v" + AssemblyInfo.Version;

private readonly IDictionary headers;

public ArmoredOutputStream(Stream outStream)
{
this.outStream = outStream;
this.headers = Platform.CreateHashtable();
this.headers["Version"] = version;
this.headers = Platform.CreateHashtable(1);
this.headers.Add(HeaderVersion, Version);
}

public ArmoredOutputStream(Stream outStream, IDictionary headers)
{
this.outStream = outStream;
this.headers = Platform.CreateHashtable(headers);
this.headers["Version"] = version;
if (!this.headers.Contains(HeaderVersion))
{
this.headers.Add(HeaderVersion, Version);
}
}

/**
* Set an additional header entry.
* Set an additional header entry. A null value will clear the entry for name.
*
* @param name the name of the header entry.
* @param v the value of the header entry.
*/
public void SetHeader(
string name,
string v)
public void SetHeader(string name, string v)
{
headers[name] = v;
if (v == null)
{
headers.Remove(name);
}
else
{
headers[name] = v;
}
}

/**
* Reset the headers to only contain a Version string.
*/
public void ResetHeaders()
{
string version = (string)headers[HeaderVersion];

headers.Clear();
headers["Version"] = version;

if (version != null)
{
headers[HeaderVersion] = Version;
}

This comment has been minimized.

Copy link
@jstedfast

jstedfast Sep 3, 2017

Contributor

Peter: should this really be using Version? Or should it be using version?

I think an argument could be made either way:

  1. It uses Version because it is meant to reset back to the original state
  2. It uses version because it wants to reset everything but the "Version" header

The comment is a bit ambiguous which is why I'm asking...

This comment has been minimized.

Copy link
@peterdettman

peterdettman Sep 4, 2017

Author Collaborator

@jstedfast Good eyes, it should have been 'version', i.e. preserve the version header. Fixed in git now.

This comment has been minimized.

Copy link
@jstedfast

jstedfast Sep 4, 2017

Contributor

Awesome :-)

}

/**
Expand Down Expand Up @@ -248,14 +264,17 @@ public override void WriteByte(
}

DoWrite(headerStart + type + headerTail + nl);
WriteHeaderEntry("Version", (string) headers["Version"]);
if (headers.Contains(HeaderVersion))
{
WriteHeaderEntry(HeaderVersion, (string)headers[HeaderVersion]);
}

foreach (DictionaryEntry de in headers)
{
string k = (string) de.Key;
if (k != "Version")
string k = (string)de.Key;
if (k != HeaderVersion)
{
string v = (string) de.Value;
string v = (string)de.Value;
WriteHeaderEntry(k, v);
}
}
Expand Down

0 comments on commit 53a508e

Please sign in to comment.