mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved header control in ArmoredOutputStream
- in particular, supports removing the default version header
- Loading branch information
1 parent
08629c3
commit 53a508e
Showing
1 changed file
with
33 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
peterdettman
Author
Collaborator
|
||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
} | ||
|
Peter: should this really be using
Version
? Or should it be usingversion
?I think an argument could be made either way:
Version
because it is meant to reset back to the original stateversion
because it wants to reset everything but the "Version" headerThe comment is a bit ambiguous which is why I'm asking...