// Parses an integer from a String in the given style. Returns false rather // than throwing exceptin if input is invalid // public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out TName result) { if (s == null) { result = 0; return(false); } var r = TType.TryParse(s, style, NumberFormatInfo.GetInstance(provider), out var res); result = res; return(r); }
public PdbScopeMemoryGraph(string pdbScopeFile) : base(10000) { var children = new GrowableArray <NodeIndex>(1000); Dictionary <string, NodeTypeIndex> knownTypes = new Dictionary <string, NodeTypeIndex>(1000); XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true, IgnoreComments = true }; using (XmlReader reader = XmlReader.Create(pdbScopeFile, settings)) { int foundBestRoot = int.MaxValue; // it is zero when when we find the best root. Address imageBase = 0; uint sizeOfImageHeader = 0; Address lastAddress = 0; int badValues = 0; Queue <Section> sections = new Queue <Section>(); Address prevAddr = 0; Address expectedAddr = 0; RootIndex = NodeIndex.Invalid; NodeIndex firstNodeIndex = NodeIndex.Invalid; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "Section": { Section section = new Section(); section.Start = Address.Parse(reader.GetAttribute("Start")); section.Size = uint.Parse(reader.GetAttribute("Size")); section.Name = reader.GetAttribute("Name"); sections.Enqueue(section); lastAddress = Math.Max(lastAddress, section.EndRoundedUpToPage); } break; case "Module": if (imageBase == 0) { imageBase = Address.Parse(reader.GetAttribute("Base")); sizeOfImageHeader = 1024; // We are using the file size number NodeIndex nodeIndex = GetNodeIndex(imageBase); NodeTypeIndex typeIndex = CreateType("Image Header"); children.Clear(); SetNode(nodeIndex, typeIndex, (int)sizeOfImageHeader, children); expectedAddr = imageBase + 0x1000; DebugWriteLine("Loading Module Map table used to decode $N symbol prefixes."); string dllFilePath = reader.GetAttribute("FilePath"); if (dllFilePath != null) { LoadModuleMap(dllFilePath, pdbScopeFile); } else { DebugWriteLine("Could not find path to original DLL being analyzed."); } if (m_moduleMap != null) { DebugWriteLine("Loaded Module Map of " + m_moduleMap.Count + " Project N style IL modules to unmangled $N_ prefixes."); } else { DebugWriteLine("Warning: No Module Map Found: $N_ prefixes will not be unmangled."); } } break; case "ObjectTypes": case "Type": case "Dicectory": case "Sections": case "PdbscopeReport": case "Symbols": case "SourceFiles": case "File": break; case "Symbol": string addrStr = reader.GetAttribute("addr"); Address addr; if (addrStr != null && Address.TryParse(addrStr, NumberStyles.AllowHexSpecifier, null, out addr)) { if (addr < lastAddress) { // Get Size string sizeStr = reader.GetAttribute("size"); uint size = 0; if (sizeStr != null) { uint.TryParse(sizeStr, out size); } // Get Children children.Clear(); string to = reader.GetAttribute("to"); if (to != null) { GetChildrenForAddresses(ref children, to); } // Get Name, make a type out of it string name; NodeTypeIndex typeIndex = GetTypeForXmlElement(knownTypes, reader, size, out name); // Currently PdbScope files have extra information lines where it shows the different generic instantiations associated // with a given symbol. These ways have the same address as the previous entry and have no size (size will be 0) so // we filter these lines out with the following condition. if (prevAddr != addr || size != 0) { prevAddr = addr; if (addr < expectedAddr) { DebugWriteLine(string.Format("Got Address {0:x} which is less than the expected address {1:x}. Discarding {2}", addr, expectedAddr, name)); badValues++; if (50 < badValues) { throw new ApplicationException("Too many cases where the addresses were not ascending in the file"); } continue; // discard } /*** We want to make sure we account for all bytes, so log when we see gaps ***/ // If we don't match see if it is because of section boundary. if (addr != expectedAddr) { EmitNodesForGaps(sections, expectedAddr, addr); } expectedAddr = addr + size; NodeIndex nodeIndex = GetNodeIndex((Address)addr); SetNode(nodeIndex, typeIndex, (int)size, children); // See if this is a good root if (foundBestRoot != 0 && name != null) { if (name == "RHBinder__ShimExeMain") { RootIndex = nodeIndex; foundBestRoot = 0; } else if (0 < foundBestRoot && name.Contains("ILT$Main")) { RootIndex = nodeIndex; foundBestRoot = 1; } else if (1 < foundBestRoot & name.Contains("DllMainCRTStartup")) { RootIndex = nodeIndex; foundBestRoot = 1; } else if (2 < foundBestRoot & name.Contains("Main")) { RootIndex = nodeIndex; foundBestRoot = 2; } } // Remember first node. if (firstNodeIndex == NodeIndex.Invalid) { firstNodeIndex = nodeIndex; } } } else { DebugWriteLine(string.Format("Warning Discarding Symbol node {0:x} outside the last address in the image {1:x}", addr, lastAddress)); } } else { DebugWriteLine("Error: symbol without addr"); } break; default: DebugWriteLine(string.Format("Skipping unknown element {0}", reader.Name)); break; } } } EmitNodesForGaps(sections, expectedAddr, lastAddress); if (RootIndex == NodeIndex.Invalid) { RootIndex = firstNodeIndex; } DebugWriteLine(string.Format("Image Base {0:x} LastAddress {1:x}", imageBase, lastAddress)); DebugWriteLine(string.Format("Total Virtual Size {0} ({0:x})", lastAddress - imageBase)); DebugWriteLine(string.Format("Total File Size {0} ({0:x})", TotalSize)); } AllowReading(); }
static bool Parse <TEnum> (Type enumType, string value, bool ignoreCase, out TEnum result) { result = default(TEnum); MonoEnumInfo info; MonoEnumInfo.GetInfo(enumType, out info); // is 'value' a named constant? int loc = FindName(info.name_hash, info.names, value, ignoreCase); if (loc >= 0) { result = (TEnum)info.values.GetValue(loc); return(true); } TypeCode typeCode = ((Enum)info.values.GetValue(0)).GetTypeCode(); // is 'value' a list of named constants? if (value.IndexOf(',') != -1) { string [] names = value.Split(split_char); ulong retVal = 0; for (int i = 0; i < names.Length; ++i) { loc = FindName(info.name_hash, info.names, names [i].Trim(), ignoreCase); if (loc < 0) { return(false); } retVal |= GetValue(info.values.GetValue(loc), typeCode); } result = (TEnum)ToObject(enumType, retVal); return(true); } // is 'value' a number? switch (typeCode) { case TypeCode.SByte: sbyte sb; if (!SByte.TryParse(value, out sb)) { return(false); } result = (TEnum)ToObject(enumType, sb); break; case TypeCode.Byte: byte b; if (!Byte.TryParse(value, out b)) { return(false); } result = (TEnum)ToObject(enumType, b); break; case TypeCode.Int16: short i16; if (!Int16.TryParse(value, out i16)) { return(false); } result = (TEnum)ToObject(enumType, i16); break; case TypeCode.UInt16: ushort u16; if (!UInt16.TryParse(value, out u16)) { return(false); } result = (TEnum)ToObject(enumType, u16); break; case TypeCode.Int32: int i32; if (!Int32.TryParse(value, out i32)) { return(false); } result = (TEnum)ToObject(enumType, i32); break; case TypeCode.UInt32: uint u32; if (!UInt32.TryParse(value, out u32)) { return(false); } result = (TEnum)ToObject(enumType, u32); break; case TypeCode.Int64: long i64; if (!Int64.TryParse(value, out i64)) { return(false); } result = (TEnum)ToObject(enumType, i64); break; case TypeCode.UInt64: ulong u64; if (!UInt64.TryParse(value, out u64)) { return(false); } result = (TEnum)ToObject(enumType, u64); break; default: break; } return(true); }
private static bool TryParseAsInteger(EETypePtr enumEEType, String value, out Object result) { result = null; if (value.Length == 0) { return(false); } if (!(Char.IsDigit(value[0]) || value[0] == '+' || value[0] == '-')) { return(false); } RuntimeImports.RhCorElementType corElementType = enumEEType.CorElementType; unsafe { switch (corElementType) { case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN: { Boolean v; if (!Boolean.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR: { Char v; if (!Char.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1: { SByte v; if (!SByte.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1: { Byte v; if (!Byte.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2: { Int16 v; if (!Int16.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2: { UInt16 v; if (!UInt16.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4: { Int32 v; if (!Int32.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4: { UInt32 v; if (!UInt32.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8: { Int64 v; if (!Int64.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8: { UInt64 v; if (!UInt64.TryParse(value, out v)) { return(false); } result = RuntimeImports.RhBox(enumEEType, &v); return(true); } default: throw new NotSupportedException(); } } }
public static bool TryParseEnum <TEnum>(this string value, out TEnum result) where TEnum : struct { if (string.IsNullOrEmpty(value)) { result = default(TEnum); return(false); } var enumType = typeof(TEnum); if (!enumType.IsEnum) { throw new ArgumentException(string.Format("Type '{0}' is not an enum", enumType.FullName)); } result = default(TEnum); // Try to parse the value directly if (Enum.IsDefined(enumType, value)) { result = (TEnum)Enum.Parse(enumType, value); return(true); } // Get some info on enum var enumValues = Enum.GetValues(enumType); if (enumValues.Length == 0) { return(false); } // probably can't happen as you cant define empty enum? var enumTypeCode = Type.GetTypeCode(enumValues.GetValue(0).GetType()); // Try to parse it as a flag if (value.IndexOf(',') != -1) { if (!Attribute.IsDefined(enumType, typeof(FlagsAttribute))) { return(false); } // value has flags but enum is not flags // todo: cache this for efficiency var enumInfo = new Dictionary <string, object>(); var enumNames = Enum.GetNames(enumType); for (var i = 0; i < enumNames.Length; i++) { enumInfo.Add(enumNames[i], enumValues.GetValue(i)); } ulong retVal = 0; foreach (var name in value.Split(FlagDelimiter)) { var trimmedName = name.Trim(); if (!enumInfo.ContainsKey(trimmedName)) { return(false); } // Enum has no such flag var enumValueObject = enumInfo[trimmedName]; ulong enumValueLong; switch (enumTypeCode) { case TypeCode.Byte: enumValueLong = (byte)enumValueObject; break; case TypeCode.SByte: enumValueLong = (byte)((sbyte)enumValueObject); break; case TypeCode.Int16: enumValueLong = (ushort)((short)enumValueObject); break; case TypeCode.Int32: enumValueLong = (uint)((int)enumValueObject); break; case TypeCode.Int64: enumValueLong = (ulong)((long)enumValueObject); break; case TypeCode.UInt16: enumValueLong = (ushort)enumValueObject; break; case TypeCode.UInt32: enumValueLong = (uint)enumValueObject; break; case TypeCode.UInt64: enumValueLong = (ulong)enumValueObject; break; default: return(false); // should never happen } retVal |= enumValueLong; } result = (TEnum)Enum.ToObject(enumType, retVal); return(true); } // the value may be a number, so parse it directly switch (enumTypeCode) { case TypeCode.SByte: sbyte sb; if (!SByte.TryParse(value, out sb)) { return(false); } result = (TEnum)Enum.ToObject(enumType, sb); break; case TypeCode.Byte: byte b; if (!Byte.TryParse(value, out b)) { return(false); } result = (TEnum)Enum.ToObject(enumType, b); break; case TypeCode.Int16: short i16; if (!Int16.TryParse(value, out i16)) { return(false); } result = (TEnum)Enum.ToObject(enumType, i16); break; case TypeCode.UInt16: ushort u16; if (!UInt16.TryParse(value, out u16)) { return(false); } result = (TEnum)Enum.ToObject(enumType, u16); break; case TypeCode.Int32: int i32; if (!Int32.TryParse(value, out i32)) { return(false); } result = (TEnum)Enum.ToObject(enumType, i32); break; case TypeCode.UInt32: uint u32; if (!UInt32.TryParse(value, out u32)) { return(false); } result = (TEnum)Enum.ToObject(enumType, u32); break; case TypeCode.Int64: long i64; if (!Int64.TryParse(value, out i64)) { return(false); } result = (TEnum)Enum.ToObject(enumType, i64); break; case TypeCode.UInt64: ulong u64; if (!UInt64.TryParse(value, out u64)) { return(false); } result = (TEnum)Enum.ToObject(enumType, u64); break; default: return(false); // should never happen } return(true); }
public static object Parse(Type enumType, string value, bool ignoreCase) { if (enumType == null) { throw new ArgumentNullException("enumType"); } if (value == null) { throw new ArgumentNullException("value"); } if (!enumType.IsEnum) { throw new ArgumentException("enumType is not an Enum type.", "enumType"); } value = value.Trim(); if (value.Length == 0) { throw new ArgumentException("An empty string is not considered a valid value."); } MonoEnumInfo info; MonoEnumInfo.GetInfo(enumType, out info); // is 'value' a named constant? int loc = FindName(info.name_hash, info.names, value, ignoreCase); if (loc >= 0) { return(info.values.GetValue(loc)); } TypeCode typeCode = ((Enum)info.values.GetValue(0)).GetTypeCode(); // is 'value' a list of named constants? if (value.IndexOf(',') != -1) { string [] names = value.Split(split_char); ulong retVal = 0; for (int i = 0; i < names.Length; ++i) { loc = FindName(info.name_hash, info.names, names [i].Trim(), ignoreCase); if (loc < 0) { throw new ArgumentException("The requested value was not found."); } retVal |= GetValue(info.values.GetValue(loc), typeCode); } return(ToObject(enumType, retVal)); } // is 'value' a number? #if !NET_2_0 try { return(ToObject(enumType, Convert.ChangeType(value, typeCode))); } catch (FormatException) { throw new ArgumentException(String.Format("The requested value '{0}' was not found.", value)); } #else switch (typeCode) { case TypeCode.SByte: sbyte sb; if (SByte.TryParse(value, out sb)) { return(ToObject(enumType, sb)); } break; case TypeCode.Byte: byte b; if (Byte.TryParse(value, out b)) { return(ToObject(enumType, b)); } break; case TypeCode.Int16: short i16; if (Int16.TryParse(value, out i16)) { return(ToObject(enumType, i16)); } break; case TypeCode.UInt16: ushort u16; if (UInt16.TryParse(value, out u16)) { return(ToObject(enumType, u16)); } break; case TypeCode.Int32: int i32; if (Int32.TryParse(value, out i32)) { return(ToObject(enumType, i32)); } break; case TypeCode.UInt32: uint u32; if (UInt32.TryParse(value, out u32)) { return(ToObject(enumType, u32)); } break; case TypeCode.Int64: long i64; if (Int64.TryParse(value, out i64)) { return(ToObject(enumType, i64)); } break; case TypeCode.UInt64: ulong u64; if (UInt64.TryParse(value, out u64)) { return(ToObject(enumType, u64)); } break; default: break; } throw new ArgumentException(String.Format("The requested value '{0}' was not found.", value)); #endif }
/// <summary> /// String to UInt64(字符串 转换成 无符号、数值、整数) /// </summary> /// <remarks> /// 2014-06-23 16:31 Created By iceStone /// </remarks> /// <param name="s">String</param> /// <param name="def">Default</param> /// <returns>Byte</returns> public static ulong ToUInt64(this string s, ulong def = default(ulong)) { ulong result; return(UInt64.TryParse(s, out result) ? result : def); }