Skip to content

Commit

Permalink
[clrphlib] Make Apiset lookups case insensitive (fi19)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasg committed Mar 15, 2020
1 parent 73b2f2e commit 89dbce6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
34 changes: 25 additions & 9 deletions ClrPhlib/src/managed/Phlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,12 @@ private ref class V2V4ApiSetSchema sealed : ApiSetSchema
ApiSetTarget^ Lookup(String^ name) override
{
// TODO : check if ext- is not present on win7 and 8.1
if (!name->StartsWith("api-"))
if (!name->StartsWith("api-", System::StringComparison::CurrentCultureIgnoreCase))
return nullptr;

// Force lowercase name
name = name->ToLower();

// remove "api-" or "ext-" prefix
name = name->Substring(4);

Expand All @@ -178,7 +181,7 @@ private ref class V2V4ApiSetSchema sealed : ApiSetSchema
auto const cur = (min + max) / 2;
auto pair = All[cur];

if (name->StartsWith(pair.Key))
if (name->StartsWith(pair.Key, System::StringComparison::CurrentCultureIgnoreCase))
return pair.Value;

if (String::CompareOrdinal(name, pair.Key) < 0)
Expand Down Expand Up @@ -209,7 +212,11 @@ ApiSetSchema^ ApiSetSchemaImpl::GetApiSetSchemaV2(API_SET_NAMESPACE_V2 const * c
// Retrieve api min-win contract name
auto const name_buffer = reinterpret_cast<PWCHAR>(base + it->NameOffset);
auto const name = gcnew String(name_buffer, 0, it->NameLength / sizeof(WCHAR));
schema->All->Add(KeyValuePair<String^, ApiSetTarget^>(name, targets));

// force storing lowercase variant for comparison
auto const lower_name = name->ToLower();

schema->All->Add(KeyValuePair<String^, ApiSetTarget^>(lower_name, targets));
}
return schema;
}
Expand All @@ -233,7 +240,11 @@ ApiSetSchema^ ApiSetSchemaImpl::GetApiSetSchemaV4(API_SET_NAMESPACE_V4 const * c
// Retrieve api min-win contract name
auto const name_buffer = reinterpret_cast<PWCHAR>(base + it->NameOffset);
auto const name = gcnew String(name_buffer, 0, it->NameLength / sizeof(WCHAR));
schema->All->Add(KeyValuePair<String^, ApiSetTarget^>(name, targets));

// force storing lowercase variant for comparison
auto const lower_name = name->ToLower();

schema->All->Add(KeyValuePair<String^, ApiSetTarget^>(lower_name, targets));
}
return schema;
}
Expand All @@ -247,8 +258,8 @@ private ref class V6ApiSetSchema sealed : ApiSetSchema
List<KeyValuePair<String^, ApiSetTarget^>>^ GetAll() override { return All; }
ApiSetTarget^ Lookup(String^ name) override
{
// remove "api-" or "ext-" prefix
//name = name->Substring(4);
// Force lowercase name
name = name->ToLower();

// Note: The list is initially alphabetically sorted!!!
auto min = 0;
Expand All @@ -258,7 +269,7 @@ private ref class V6ApiSetSchema sealed : ApiSetSchema
auto const cur = (min + max) / 2;
auto pair = HashedAll[cur];

if (name->StartsWith(pair.Key))
if (name->StartsWith(pair.Key, System::StringComparison::CurrentCultureIgnoreCase))
return pair.Value;

if (String::CompareOrdinal(name, pair.Key) < 0)
Expand Down Expand Up @@ -290,8 +301,13 @@ ApiSetSchema^ ApiSetSchemaImpl::GetApiSetSchemaV6(API_SET_NAMESPACE_V6 const * c
auto const name_buffer = reinterpret_cast<PWCHAR>(base + it->NameOffset);
auto const name = gcnew String(name_buffer, 0, it->NameLength / sizeof(WCHAR));
auto const hash_name = gcnew String(name_buffer, 0, it->HashedLength / sizeof(WCHAR));
schema->All->Add(KeyValuePair<String^, ApiSetTarget^>(name, targets));
schema->HashedAll->Add(KeyValuePair<String^, ApiSetTarget^>(hash_name, targets));

// force storing lowercase variant for comparison
auto const lower_name = name->ToLower();
auto const lower_hash_name = hash_name->ToLower();

schema->All->Add(KeyValuePair<String^, ApiSetTarget^>(lower_name, targets));
schema->HashedAll->Add(KeyValuePair<String^, ApiSetTarget^>(lower_hash_name, targets));
}
return schema;
}
Expand Down
3 changes: 2 additions & 1 deletion DependenciesLib/BinaryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ public static Tuple<ModuleSearchStrategy, PE> ResolveModule(PE RootPe, string Mo
public static string LookupApiSetLibrary(string ImportDllName)
{
// Look for api set target
if (!ImportDllName.StartsWith("api-") && !ImportDllName.StartsWith("ext-"))
if (!ImportDllName.StartsWith("api-", StringComparison.CurrentCultureIgnoreCase) && !ImportDllName.StartsWith("ext-", StringComparison.CurrentCultureIgnoreCase))
return "";


// Strip the .dll extension and search for matching targets
var ImportDllWIthoutExtension = Path.GetFileNameWithoutExtension(ImportDllName);
Expand Down

0 comments on commit 89dbce6

Please sign in to comment.