public void LoadFromXML(XmlNode patch) { XmlNode tmpNode = null; //DiffPatch p = new DiffPatch(); this.ID = int.Parse(patch.Attributes["id"].InnerText); this.Name = patch.Attributes["name"].InnerText; this.Type = patch.Attributes["type"].InnerText; tmpNode = patch.ParentNode; if (tmpNode != null && tmpNode.Name == "patchgroup") { this.GroupID = int.Parse(tmpNode.Attributes["id"].InnerText); } if (patch.Attributes["recommended"] != null) { this.Recommended = true; } tmpNode = patch.SelectSingleNode("desc"); if (tmpNode != null) { this.Desc = tmpNode.InnerText; } foreach (XmlNode i in patch.SelectNodes("input")) { var input = new DiffInput(); input.LoadFromXML(i); this.Inputs.Add(input); } /* var input = new DiffInput(); * input.LoadFromXML(change); * this.Inputs.Add(input);*/ tmpNode = patch.SelectSingleNode("changes"); if (tmpNode != null) { foreach (XmlNode change in tmpNode.ChildNodes) { DiffChange c = new DiffChange(); if (change.Name == "byte") { c.Type = ChangeType.Byte; } else if (change.Name == "word") { c.Type = ChangeType.Word; } else if (change.Name == "dword") { c.Type = ChangeType.Dword; } else if (change.Name == "string") { c.Type = ChangeType.String; } else { c.Type = ChangeType.None; } if (change.Attributes["new"].InnerText.StartsWith("$")) { c.New_ = change.Attributes["new"].InnerText; } c.Offset = uint.Parse(change.Attributes["offset"].InnerText, System.Globalization.NumberStyles.HexNumber); if (c.Type == ChangeType.String) { if (c.New_ == null) { c.New_ = change.Attributes["new"].InnerText; } c.Old = change.Attributes["old"].InnerText; } else if (c.Type == ChangeType.Byte) { if (c.New_ == null) { c.New_ = byte.Parse(change.Attributes["new"].InnerText, System.Globalization.NumberStyles.HexNumber); } c.Old = byte.Parse(change.Attributes["old"].InnerText, System.Globalization.NumberStyles.HexNumber); } else if (c.Type == ChangeType.Word) { if (c.New_ == null) { c.New_ = ushort.Parse(change.Attributes["new"].InnerText, System.Globalization.NumberStyles.HexNumber); } c.Old = ushort.Parse(change.Attributes["old"].InnerText, System.Globalization.NumberStyles.HexNumber); } else if (c.Type == ChangeType.Dword) { if (c.New_ == null) { c.New_ = uint.Parse(change.Attributes["new"].InnerText, System.Globalization.NumberStyles.HexNumber); } c.Old = uint.Parse(change.Attributes["old"].InnerText, System.Globalization.NumberStyles.HexNumber); } this.Changes.Add(c); } } }
public int Load(string fileName, DiffType type) { if (!File.Exists(fileName)) { return(1); } m_fileInfo = new FileInfo(fileName); if (m_patches != null) { m_patches.Clear(); } if (m_xpatches != null) { m_xpatches.Clear(); } m_type = type; if (type == DiffType.xDiff) { XmlDocument XDoc = null; /*try * {*/ XDoc = new XmlDocument(); XDoc.Load(fileName); this.ExeBuildDate = XDoc.SelectSingleNode("//diff/exe/builddate").InnerText; this.ExeName = XDoc.SelectSingleNode("//diff/exe/filename").InnerText; this.ExeCRC = int.Parse(XDoc.SelectSingleNode("//diff/exe/crc").InnerText); string xtype = XDoc.SelectSingleNode("//diff/exe/type").InnerText; this.ExeType = 0; this.Name = XDoc.SelectSingleNode("//diff/info/name").InnerText; this.Author = XDoc.SelectSingleNode("//diff/info/author").InnerText; this.Version = XDoc.SelectSingleNode("//diff/info/version").InnerText; this.ReleaseDate = XDoc.SelectSingleNode("//diff/info/releasedate").InnerText; //extra addition for adding .xdiff section m_xDiffSection.peHeader = Convert.ToUInt32(XDoc.SelectSingleNode("//diff/override/peheader").InnerText, 10); m_xDiffSection.imgSize = Convert.ToUInt32(XDoc.SelectSingleNode("//diff/override/imagesize").InnerText, 10); m_xDiffSection.sectCount = Convert.ToUInt16(XDoc.SelectSingleNode("//diff/override/sectioncount").InnerText, 10); m_xDiffSection.xDiffStart = Convert.ToUInt32(XDoc.SelectSingleNode("//diff/override/xdiffstart").InnerText, 10); UInt32 vSize = Convert.ToUInt32(XDoc.SelectSingleNode("//diff/override/vsize").InnerText, 10); UInt32 vOffset = Convert.ToUInt32(XDoc.SelectSingleNode("//diff/override/voffset").InnerText, 10); UInt32 rSize = Convert.ToUInt32(XDoc.SelectSingleNode("//diff/override/rsize").InnerText, 10); UInt32 rOffset = Convert.ToUInt32(XDoc.SelectSingleNode("//diff/override/roffset").InnerText, 10); m_xDiffSection.realSize = rSize + rOffset; //now create section data m_xDiffSection.sectionData = new byte[40]; UInt32 i = 0; // first section name foreach (byte b in Encoding.ASCII.GetBytes(".xdiff\x00\x00")) { m_xDiffSection.sectionData[i] = b; i++; } //next the offset and sizes WriteDword(ref m_xDiffSection.sectionData, vSize, 8); WriteDword(ref m_xDiffSection.sectionData, vOffset, 12); WriteDword(ref m_xDiffSection.sectionData, rSize, 16); WriteDword(ref m_xDiffSection.sectionData, rOffset, 20); //next the relocation and line numbers info - fill with 0 WriteDword(ref m_xDiffSection.sectionData, 0, 24); WriteDword(ref m_xDiffSection.sectionData, 0, 28); WriteDword(ref m_xDiffSection.sectionData, 0, 32); //Lastly Characteristics WriteDword(ref m_xDiffSection.sectionData, 0xE0000060, 36); XmlNode patches = XDoc.SelectSingleNode("//diff/patches"); foreach (XmlNode patch in patches.ChildNodes) { if (patch.Name == "patchgroup") { //XmlNode tmpNode = null; DiffPatchGroup g = new DiffPatchGroup(); g.ID = int.Parse(patch.Attributes["id"].InnerText); g.Name = patch.Attributes["name"].InnerText; foreach (XmlNode node in patch.ChildNodes) { if (node.Name == "patch") { DiffPatch p = new DiffPatch(); p.LoadFromXML(node); this.xPatches.Add(p.ID, p); g.Patches.Add(p); } } this.xPatches.Add(g.ID, g); } else if (patch.Name == "patch") { DiffPatch p = new DiffPatch(); p.LoadFromXML(patch); this.xPatches.Add(p.ID, p); } } /*} * catch (Exception ex) * { * MessageBox.Show("Failed to parse xDiff file: \n"+ex.ToString()); * return 2; * }*/ } else if (type == DiffType.Diff) { bool hex = false; using (StreamReader r = new StreamReader(fileName)) { string line; while (!r.EndOfStream && (line = r.ReadLine()) != null) { line = line.Trim(); if (line.Length < 5) { continue; } if (line.StartsWith("OCRC:")) { this.ExeCRC = int.Parse(line.Substring(5)); } else if (line.StartsWith("BLURB:")) { this.Name = line.Substring(6); } else if (line.StartsWith("READHEX")) { hex = true; } else if (line.StartsWith("byte_")) { string pType, pName; string pGroup; DiffChange change = new DiffChange(); DiffPatch patch = new DiffPatch(); string[] split = line.Split(':'); Regex regex = new Regex("(.+)_\\[(.+)\\]_(.+)"); Match match = regex.Match(split[0]); pName = ""; pType = ""; if (match.Success) { change.Type = ChangeType.Byte; pType = match.Groups[1].Captures[0].Value; pName = split[0].Substring(5); //match.Captures[2].Value.Replace('_', ' '); } else { regex = new Regex("(.+)_\\[(.+)\\]\\((.+)\\)_(.+)"); match = regex.Match(split[0]); if (match.Success) { change.Type = ChangeType.Byte; pType = match.Groups[1].Captures[0].Value; pGroup = match.Groups[3].Captures[0].Value; pName = split[0].Substring(5); //match.Groups[3].Captures[0].Value.Replace('_', ' '); } else { continue; } } change.Offset = uint.Parse(split[1], System.Globalization.NumberStyles.HexNumber); change.Old = (byte)((!hex) ? byte.Parse(split[2]) : byte.Parse(split[2], System.Globalization.NumberStyles.HexNumber)); change.New_ = (byte)((!hex) ? byte.Parse(split[3]) : byte.Parse(split[3], System.Globalization.NumberStyles.HexNumber)); if (m_patches.ContainsKey(pName)) { m_patches[pName].Changes.Add(change); } else { patch.Changes.Add(change); patch.Name = pName; patch.Type = pType; m_patches.Add(pName, patch); } } } } } else { return(2); } return(0); }