예제 #1
0
        private void InitForm(object o)
        {
            if (o is MemberReference)
            {
                MemberReference mr = (MemberReference)o;
                txtOriginalName.Text = InsUtils.GetOldMemberName(mr);
                txtCurrentName.Text  = mr.Name;
            }
            else if (o is Resource)
            {
                Resource r = (Resource)o;
                txtOriginalName.Text = String.Empty;
                txtCurrentName.Text  = r.Name;
            }
            else
            {
                txtOriginalName.Text = String.Empty;
                txtCurrentName.Text  = o.ToString();
            }

            if (!String.IsNullOrEmpty(txtOriginalName.Text))
            {
                byte[] bytes = Encoding.Unicode.GetBytes(txtOriginalName.Text);
                txtOriginalNameHex.Text = BytesUtils.BytesToHexString(bytes, true);
            }

            if (!String.IsNullOrEmpty(txtCurrentName.Text))
            {
                byte[] bytes = Encoding.Unicode.GetBytes(txtCurrentName.Text);
                txtCurrentNameHex.Text = BytesUtils.BytesToHexString(bytes, true);
            }
        }
        private string GetCAInternalVisibleToReferenceString(string keyFile)
        {
            //PublicKey=00240000048000009400000006020000002400005253413100040000010001007b92cbd52d63863dde18c3fcbfcb11380ad0030031dec9f977ed149dcc4c802b4e6e9701c64bb6fd9a4031a2c8c02877b0a698c994a6c26fea913b2e01f52a0db7e6d00a4048ecab3f7de211a6c23cbf5d74eec6318c001395599ceb92a715ae9d4c1431fc31324c1163adfa623842aed9a70218ba9f0e9442c7a98e2c930f9e
            byte[] pubKey = TokenUtils.GetPublicKeyFromKeyFile(keyFile);
            string s      = BytesUtils.BytesToHexString(pubKey);

            return(String.Format("PublicKey={0}", s));
        }
 public static string GetReferenceTokenString(byte[] token)
 {
     if (token != null && token.Length > 0)
     {
         return(BytesUtils.BytesToHexString(token));
     }
     return("null");
 }
        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);
        }
 public static string GetPublicKeyTokenString(byte[] token)
 {
     return(BytesUtils.BytesToHexString(token));
 }