void DkimWriteHeaders (FormatOptions options, IList<string> fields, DkimCanonicalizationAlgorithm headerCanonicalizationAlgorithm, Stream stream)
{
var counts = new Dictionary<string, int> ();
for (int i = 0; i < fields.Count; i++) {
var headers = fields[i].StartsWith ("Content-", StringComparison.OrdinalIgnoreCase) ? Body.Headers : Headers;
var name = fields[i].ToLowerInvariant ();
int index, count, n = 0;
if (!counts.TryGetValue (name, out count))
count = 0;
// Note: signers choosing to sign an existing header field that occurs more
// than once in the message (such as Received) MUST sign the physically last
// instance of that header field in the header block. Signers wishing to sign
// multiple instances of such a header field MUST include the header field
// name multiple times in the list of header fields and MUST sign such header
// fields in order from the bottom of the header field block to the top.
index = headers.LastIndexOf (name);
// find the n'th header with this name
while (n < count && --index >= 0) {
if (headers[index].Field.Equals (name, StringComparison.OrdinalIgnoreCase))
n++;
}
if (index < 0)
continue;
var header = headers[index];
switch (headerCanonicalizationAlgorithm) {
case DkimCanonicalizationAlgorithm.Relaxed:
DkimWriteHeaderRelaxed (options, stream, header, false);
break;
default:
DkimWriteHeaderSimple (options, stream, header, false);
break;
}
counts[name] = ++count;
}
}