diff --git a/MetadataProcessor.Shared/Tables/nanoSignaturesTable.cs b/MetadataProcessor.Shared/Tables/nanoSignaturesTable.cs index b72f4f10..7bb12937 100644 --- a/MetadataProcessor.Shared/Tables/nanoSignaturesTable.cs +++ b/MetadataProcessor.Shared/Tables/nanoSignaturesTable.cs @@ -108,6 +108,10 @@ static nanoSignaturesTable() /// private readonly IDictionary _idsBySignatures = new Dictionary(new ByteArrayComparer()); + /// + /// The signatures as they are written to the output stream. + /// + private readonly List _signatureTable = new List(); /// /// Assembly tables context - contains all tables used for building target assembly. @@ -116,11 +120,6 @@ static nanoSignaturesTable() private readonly bool _verbose = false; - /// - /// Last available signature id (offset in resulting table). - /// - private ushort _lastAvailableId; - /// /// Creates new instance of object. /// @@ -468,12 +467,7 @@ public void WriteDataTypeForTypeDef(TypeDefinition typeDefinition, nanoBinaryWri public void Write( nanoBinaryWriter writer) { - foreach (var signature in _idsBySignatures - .OrderBy(item => item.Value) - .Select(item => item.Key)) - { - writer.WriteBytes(signature); - } + writer.WriteBytes(_signatureTable.ToArray()); } private byte[] GetSignature( @@ -736,18 +730,29 @@ private ushort GetOrCreateSignatureIdImpl( return id; } - var fullSignatures = GetFullSignaturesArray(); - for (var i = 0; i <= fullSignatures.Length - signature.Length; ++i) + for (int i = 0; i < _signatureTable.Count - signature.Length; i++) { - if (signature.SequenceEqual(fullSignatures.Skip(i).Take(signature.Length))) + bool found = true; + for (int j = 0; j < signature.Length; ++j) { - return (ushort)i; + if (_signatureTable[i + j] != signature[j]) + { + found = false; + break; + } + } + + if (found) + { + id = (ushort)i; + _idsBySignatures.Add(signature, id); + return id; } } - id = _lastAvailableId; + id = (ushort)_signatureTable.Count; _idsBySignatures.Add(signature, id); - _lastAvailableId += (ushort)signature.Length; + _signatureTable.AddRange(signature); return id; } @@ -775,20 +780,6 @@ private void WriteTypeInfo( } } - private byte[] GetFullSignaturesArray() - { - return _idsBySignatures - .OrderBy(item => item.Value) - .Select(item => item.Key) - .Aggregate(new List(), - (current, item) => - { - current.AddRange(item); - return current; - }) - .ToArray(); - } - private void WriteSubTypeInfo(TypeReference typeDefinition, nanoBinaryWriter writer) { ushort referenceId;