Bug fix. Resource Directory Entries must be sorted and names are case-insensitive.

This commit is contained in:
jfrijters 2012-03-19 12:35:33 +00:00
Родитель 95331aeebd
Коммит 34c85aa5b4
1 изменённых файлов: 36 добавлений и 3 удалений

Просмотреть файл

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2010 Jeroen Frijters Copyright (C) 2010-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -147,23 +147,42 @@ namespace IKVM.Reflection.Writer
{ {
foreach (ResourceDirectoryEntry entry in entries) foreach (ResourceDirectoryEntry entry in entries)
{ {
if (entry.OrdinalOrName.Ordinal == id.Ordinal && entry.OrdinalOrName.Name == id.Name) if (entry.OrdinalOrName.IsEqual(id))
{ {
return entry; return entry;
} }
} }
// the entries must be sorted
ResourceDirectoryEntry newEntry = new ResourceDirectoryEntry(id); ResourceDirectoryEntry newEntry = new ResourceDirectoryEntry(id);
if (id.Name == null) if (id.Name == null)
{ {
for (int i = namedEntries; i < entries.Count; i++)
{
if (entries[i].OrdinalOrName.IsGreaterThan(id))
{
entries.Insert(i, newEntry);
return newEntry;
}
}
entries.Add(newEntry); entries.Add(newEntry);
return newEntry;
} }
else else
{ {
entries.Insert(namedEntries++, newEntry); for (int i = 0; i < namedEntries; i++)
} {
if (entries[i].OrdinalOrName.IsGreaterThan(id))
{
entries.Insert(i, newEntry);
namedEntries++;
return newEntry; return newEntry;
} }
} }
entries.Insert(namedEntries++, newEntry);
return newEntry;
}
}
}
private int DirectoryLength private int DirectoryLength
{ {
@ -322,6 +341,20 @@ namespace IKVM.Reflection.Writer
Ordinal = 0xFFFF; Ordinal = 0xFFFF;
Name = value; Name = value;
} }
internal bool IsGreaterThan(OrdinalOrName other)
{
return this.Name == null
? this.Ordinal > other.Ordinal
: String.Compare(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) > 0;
}
internal bool IsEqual(OrdinalOrName other)
{
return this.Name == null
? this.Ordinal == other.Ordinal
: String.Compare(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) == 0;
}
} }
struct RESOURCEHEADER struct RESOURCEHEADER