public bool readKey(out RegFileRegistryKey myRegistryKey) { myRegistryKey = new RegFileRegistryKey(); while (!sr.EndOfStream) { String line = sr.ReadLine(); if (String.IsNullOrEmpty(line)) { continue; } if (line[0] == '[') { String keyName = line.Trim('[', ']'); if (NextMasterKey != null) { myRegistryKey.keyName = NextMasterKey; NextMasterKey = keyName; return(true); } else { NextMasterKey = keyName; } } else if (line[0] == '"' || line[0] == '@') { RegFileRegistryValue regValue = new RegFileRegistryValue(); bool more = line.EndsWith("\\"); if (more) { String extra; StringBuilder data = new StringBuilder(1024); data.Append(line.Remove(line.Length - 1)); while (more) { extra = sr.ReadLine(); more = extra.EndsWith("\\"); if (more) { extra = extra.Remove(extra.Length - 1); } data.Append(extra.Trim()); } line = data.ToString(); } int i = 0; bool isInString = false; StringBuilder valueNameB = new StringBuilder(line.Length); while (isInString || line[i] != '=') { if (line[i] == '\"') { isInString = !isInString; } else { if (line[i] == '\\') { i++; } valueNameB.Append(line[i]); }; i++; } String valueName = valueNameB.ToString(); if (valueName == "@" && i == 1) { valueName = ""; } String valueValue = line.Substring(i + 1); regValue.name = valueName; regValue.type = RegistryValueKind.Unknown; foreach (KeyValuePair <string, RegistryValueKind> value in regFileRegType) { if (valueValue.StartsWith(value.Key)) { regValue.type = value.Value; break; } } switch (regValue.type) { case RegistryValueKind.String: String strValue = valueValue.Substring(1, valueValue.Length - 2); strValue = strValue.Replace("\\\"", "\""); strValue = strValue.Replace("\\\\", "\\"); regValue.value = strValue; break; case RegistryValueKind.DWord: regValue.value = int.Parse(valueValue.Substring(6), System.Globalization.NumberStyles.HexNumber); break; case RegistryValueKind.Binary: { String strdata = valueValue.Substring(4); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); regValue.value = byteArray; break; } case RegistryValueKind.QWord: { String strdata = valueValue.Substring(7); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); long l = BitConverter.ToInt64(byteArray, 0); regValue.value = l; break; } case RegistryValueKind.ExpandString: { String strdata = valueValue.Substring(7); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); String regString = Encoding.Unicode.GetString(byteArray); regString = regString.Remove(regString.Length - 1); regValue.value = regString; break; } case RegistryValueKind.MultiString: { String strdata = valueValue.Substring(7); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); String regString = Encoding.Unicode.GetString(byteArray); String[] value; if (regString == "\0") { value = new String[0]; // not a single string is pressent.. } else { regString = regString.Remove(regString.Length - 2); value = regString.Split(new char[] { '\0' }); } regValue.value = value; break; } default: errors.Append(String.Format("\r\n------------------------------------------------\r\nRegistry value not supported Valuename:{0}\r\nType:{1}\r\nValue:{2}\r\nIn key:{3}", regValue.name, regValue.type, valueValue, NextMasterKey)); break; } myRegistryKey.values.Add(regValue); } } if (NextMasterKey != null) { myRegistryKey.keyName = NextMasterKey; NextMasterKey = null; return(true); } myRegistryKey = null; return(false); }
public bool readKey(out RegFileRegistryKey myRegistryKey) { myRegistryKey = new RegFileRegistryKey(); while (!sr.EndOfStream) { String line = sr.ReadLine(); if (String.IsNullOrEmpty(line)) continue; if (line[0] == '[') { String keyName = line.Trim('[', ']'); if (NextMasterKey != null) { myRegistryKey.keyName = NextMasterKey; NextMasterKey = keyName; return true; } else { NextMasterKey = keyName; } } else if (line[0] == '"' || line[0] == '@') { RegFileRegistryValue regValue = new RegFileRegistryValue(); bool more = line.EndsWith("\\"); if (more) { String extra; StringBuilder data = new StringBuilder(1024); data.Append(line.Remove(line.Length - 1)); while (more) { extra = sr.ReadLine(); more = extra.EndsWith("\\"); if (more) extra = extra.Remove(extra.Length - 1); data.Append(extra.Trim()); } line = data.ToString(); } int i = 0; bool isInString = false; StringBuilder valueNameB = new StringBuilder(line.Length); while (isInString || line[i] != '=') { if (line[i] == '\"') isInString = !isInString; else { if (line[i] == '\\') i++; valueNameB.Append(line[i]); }; i++; } String valueName = valueNameB.ToString(); if (valueName == "@" && i == 1) valueName = ""; String valueValue = line.Substring(i + 1); regValue.name = valueName; regValue.type = RegistryValueKind.Unknown; foreach(KeyValuePair<string, RegistryValueKind> value in regFileRegType) { if (valueValue.StartsWith(value.Key)) { regValue.type = value.Value; break; } } switch (regValue.type) { case RegistryValueKind.String: String strValue = valueValue.Substring(1, valueValue.Length - 2); strValue = strValue.Replace("\\\"", "\""); strValue = strValue.Replace("\\\\", "\\"); regValue.value = strValue; break; case RegistryValueKind.DWord: regValue.value = int.Parse(valueValue.Substring(6), System.Globalization.NumberStyles.HexNumber); break; case RegistryValueKind.Binary: { String strdata = valueValue.Substring(4); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); regValue.value = byteArray; break; } case RegistryValueKind.QWord: { String strdata = valueValue.Substring(7); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); long l = BitConverter.ToInt64(byteArray, 0); regValue.value = l; break; } case RegistryValueKind.ExpandString: { String strdata = valueValue.Substring(7); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); String regString = Encoding.Unicode.GetString(byteArray); regString = regString.Remove(regString.Length - 1); regValue.value = regString; break; } case RegistryValueKind.MultiString: { String strdata = valueValue.Substring(7); byte[] byteArray = RegFileHelper.HexCommaToByteArray(strdata); String regString = Encoding.Unicode.GetString(byteArray); String[] value; if (regString == "\0") { value = new String[0]; // not a single string is pressent.. } else { regString = regString.Remove(regString.Length - 2); value = regString.Split(new char[] { '\0' }); } regValue.value = value; break; } default: errors.Append(String.Format("\r\n------------------------------------------------\r\nRegistry value not supported Valuename:{0}\r\nType:{1}\r\nValue:{2}\r\nIn key:{3}", regValue.name, regValue.type, valueValue, NextMasterKey)); break; } myRegistryKey.values.Add(regValue); } } if (NextMasterKey != null) { myRegistryKey.keyName = NextMasterKey; NextMasterKey = null; return true; } myRegistryKey = null; return false; }