private void FindToken() { try { if (String.IsNullOrEmpty(txtOldToken.Text)) { string file = PathUtils.GetFullFileName(_rows, 0, _sourceDir); if (!String.IsNullOrEmpty(file)) { string token = GetFirstPublicKeyToken(file); if (!String.IsNullOrEmpty(token)) { txtOldToken.Text = token; } } } string fileName = _replKeyFile; if (!String.IsNullOrEmpty(fileName) && File.Exists(fileName)) { txtNewToken.Text = TokenUtils.GetPublicKeyTokenString(TokenUtils.GetPublicKeyTokenFromKeyFile(fileName)); } else { txtNewToken.Text = String.Empty; } } catch (Exception ex) { SimpleMessage.ShowException(ex); } }
private string ReplaceReferencesToken(string file) { string newFile = Path.ChangeExtension(file, ".Repl.il"); if (File.Exists(newFile)) { File.Delete(newFile); } StreamReader sr = null; StreamWriter sw = null; try { string oldToken = Options.txtOldTokenText; string newToken = Options.txtNewTokenText; string oldStr1 = GetAssemblyReferenceString(oldToken); string newStr1 = GetAssemblyReferenceString(newToken); string oldStr2 = GetCAReferenceString(oldToken); string newStr2 = GetCAReferenceString(newToken); string newStr3 = GetCAInternalVisibleToReferenceString(Options.ReplKeyFile); Regex rgStr3 = new Regex(@"PublicKey=[0123456789abcdefABCDEF]*"); string oldBin2 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(oldStr2), true); string newBin2 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(newStr2), true); string newBin3 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(newStr3), true); //PublicKey=50 75 62 6C 69 63 4B 65 79 3D Regex rgBin3 = new Regex(@"50 75 62 6C 69 63 4B 65 79 3D [\d\s]+"); sr = new StreamReader(file); sw = new StreamWriter(newFile, false, System.Text.Encoding.Unicode); string line; while ((line = sr.ReadLine()) != null) { if (line.StartsWith(".assembly extern")) { sw.WriteLine(line); while ((line = sr.ReadLine()) != null) { if (line.IndexOf(oldStr1) >= 0) { line = line.Replace(oldStr1, newStr1); } sw.WriteLine(line); if (line == "}") { break; } } }//end of if .assembly extern else if (line.StartsWith(" .custom")) { StringBuilder sb = new StringBuilder(); //string lineStart = null; string lineEnd = null; bool isBinary = true; while (line != null) { //remove comment int p = line.LastIndexOf(" //"); if (p >= 0) { line = line.Substring(0, p); } line = line.Trim(); sb.Append(line); sb.Append(" "); if (lineEnd == null) { if (line.IndexOf("= {") >= 0) { //lineStart = "= {"; lineEnd = "}"; isBinary = false; } else if (line.IndexOf("= (") >= 0) { //lineStart = "= ("; lineEnd = ")"; isBinary = true; } } if (lineEnd != null && line.EndsWith(lineEnd)) { break; } line = sr.ReadLine(); } line = sb.ToString(); if (isBinary) { if (line.IndexOf(oldBin2) > 0) { line = line.Replace(oldBin2, newBin2); } else { Match m = rgBin3.Match(line); if (m.Success) { string pubKeyString = m.Value.Trim().Substring("50 75 62 6C 69 63 4B 65 79 3D ".Length); pubKeyString = pubKeyString.Substring(0, pubKeyString.Length - 6); //remove " 00 00" byte[] pubKey = BytesUtils.HexStringToBytes(Encoding.ASCII.GetString(BytesUtils.HexStringToBytes(pubKeyString))); string token = TokenUtils.GetPublicKeyTokenString(TokenUtils.GetPublicKeyToken(pubKey, Mono.Cecil.AssemblyHashAlgorithm.SHA1)); if (token.Equals(oldToken, StringComparison.CurrentCultureIgnoreCase)) { line = rgBin3.Replace(line, newBin3 + "00 00 "); } } } } else { if (line.IndexOf(oldStr2) > 0) { line = line.Replace(oldStr2, newStr2); } else { Match m = rgStr3.Match(line); if (m.Success) { string pubKeyString = m.Value.Substring("PublicKey=".Length); byte[] pubKey = BytesUtils.HexStringToBytes(pubKeyString); string token = TokenUtils.GetPublicKeyTokenString(TokenUtils.GetPublicKeyToken(pubKey, Mono.Cecil.AssemblyHashAlgorithm.SHA1)); if (token.Equals(oldToken, StringComparison.CurrentCultureIgnoreCase)) { line = rgStr3.Replace(line, newStr3); } } } } sw.WriteLine(line); }//end of if .custom else { sw.WriteLine(line); } } } catch { throw; } finally { if (sr != null) { sr.Close(); } if (sw != null) { sw.Close(); } } return(newFile); }
private void AddFiles(string path, string match) { try { escPressed = false; DirectoryInfo di = new DirectoryInfo(path + Path.DirectorySeparatorChar); FileInfo[] files = di.GetFiles(match); bool isAssembly = PathUtils.IsAssembly(match); foreach (FileInfo file in files) { if (escPressed) { break; } if (file.Length == 0) { continue; } SetStatusText(String.Format("Press ESC to cancel: {0}", file.FullName)); try { if (isAssembly) { //try //{ // AssemblyName an = AssemblyName.GetAssemblyName(file.FullName); // DataRow dr = dtFiles.NewRow(); // dr["file_name"] = file.Name; // dr["assembly_name"] = an.Name; // dr["version"] = an.Version.ToString(); // dr["public_token"] = TokenUtils.GetPublicKeyTokenString(an); // dr["processor_architecture"] = an.ProcessorArchitecture == ProcessorArchitecture.None ? String.Empty : an.ProcessorArchitecture.ToString(); // dr["full_name"] = an.FullName; // dtFiles.Rows.Add(dr); //} //catch (BadImageFormatException) if (PathUtils.IsNetModule(file.FullName)) { ModuleDefinition md = ModuleDefinition.ReadModule(file.FullName); DataRow dr = dtFiles.NewRow(); dr["file_name"] = file.Name; dr["assembly_name"] = String.Empty; dr["version"] = String.Empty; dr["public_token"] = null; dr["target_runtime"] = md.Runtime.ToString().Replace("Net_", "").Replace("_", "."); dr["processor_architecture"] = GetProcessorArchitecture(md); dr["full_name"] = md.FullyQualifiedName; dtFiles.Rows.Add(dr); } else { AssemblyDefinition ad = AssemblyDefinition.ReadAssembly(file.FullName); AssemblyNameDefinition an = ad.Name; DataRow dr = dtFiles.NewRow(); dr["file_name"] = file.Name; dr["assembly_name"] = an.Name; dr["version"] = an.Version.ToString(); dr["public_token"] = TokenUtils.GetPublicKeyTokenString(an.PublicKeyToken); dr["target_runtime"] = ad.MainModule.Runtime.ToString().Replace("Net_", "").Replace("_", "."); dr["processor_architecture"] = GetProcessorArchitecture(ad.MainModule); dr["full_name"] = an.FullName; dtFiles.Rows.Add(dr); } } else { DataRow dr = dtFiles.NewRow(); dr["file_name"] = file.Name; dr["full_name"] = file.FullName; dtFiles.Rows.Add(dr); } } catch { //not .net assembly } } } catch (Exception ex) { throw ex; } finally { SetStatusText(null); } }