Это лучшее, что я мог придумать в короткие сроки:
private static uint GetSectionProtection(DataSectionFlags characteristics)
{
uint result = 0;
if (characteristics.HasFlag(DataSectionFlags.MemoryNotCached))
{
// PageNoCache
result |= 0x200;
}
var ladder = new KeyValuePair<DataSectionFlags[], uint>[]
{
new KeyValuePair<DataSectionFlags[], uint>(new [] { DataSectionFlags.MemoryExecute, DataSectionFlags.MemoryRead, DataSectionFlags.MemoryWrite, }, 0x40),
new KeyValuePair<DataSectionFlags[], uint>(new [] { DataSectionFlags.MemoryExecute, DataSectionFlags.MemoryRead, }, 0x20),
new KeyValuePair<DataSectionFlags[], uint>(new [] { DataSectionFlags.MemoryExecute, DataSectionFlags.MemoryWrite, }, 0x80),
new KeyValuePair<DataSectionFlags[], uint>(new [] { DataSectionFlags.MemoryExecute, }, 0x10),
new KeyValuePair<DataSectionFlags[], uint>(new [] { DataSectionFlags.MemoryRead, DataSectionFlags.MemoryWrite, }, 0x04),
new KeyValuePair<DataSectionFlags[], uint>(new [] { DataSectionFlags.MemoryRead, }, 0x02),
new KeyValuePair<DataSectionFlags[], uint>(new [] { DataSectionFlags.MemoryWrite, }, 0x08),
new KeyValuePair<DataSectionFlags[], uint>(new DataSectionFlags[] { }, 0x01),
};
result |= ladder.Where(x => x.Key.All(y => characteristics.HasFlag(y))).First().Value;
return result;
}
Возможно, более читаемая версия:
private static uint GetSectionProtection(DataSectionFlags characteristics)
{
uint result = 0;
if (characteristics.HasFlag(DataSectionFlags.MemoryNotCached))
{
// PageNoCache
result |= 0x200;
}
var ladder = new []
{
new { Flags = new [] { DataSectionFlags.MemoryExecute, DataSectionFlags.MemoryRead, DataSectionFlags.MemoryWrite, }, Value = (uint)0x40 },
new { Flags = new [] { DataSectionFlags.MemoryExecute, DataSectionFlags.MemoryRead, }, Value = (uint)0x20 },
new { Flags = new [] { DataSectionFlags.MemoryExecute, DataSectionFlags.MemoryWrite, }, Value = (uint)0x80 },
new { Flags = new [] { DataSectionFlags.MemoryExecute, }, Value = (uint)0x10 },
new { Flags = new [] { DataSectionFlags.MemoryRead, DataSectionFlags.MemoryWrite, }, Value = (uint)0x04 },
new { Flags = new [] { DataSectionFlags.MemoryRead, }, Value = (uint)0x02 },
new { Flags = new [] { DataSectionFlags.MemoryWrite, }, Value = (uint)0x08 },
new { Flags = new DataSectionFlags[] { }, Value = (uint)0x01 },
};
result |= ladder.Where(x => x.Flags.All(y => characteristics.HasFlag(y))).First().Value;
return result;
}