private string FixReference(string ps, string adName, byte[] token) { string searchName = String.Format(", {0},", adName); string searchToken = "PublicKeyToken="; string searchNull = "null"; string tokenString = TokenUtils.GetReferenceTokenString(token); string result = null; int index = ps.IndexOf(searchName); if (index >= 0) { index = ps.IndexOf(searchToken, index + searchName.Length); if (index > 0) { index = index + searchToken.Length; if (token == null) { if (ps.IndexOf(searchNull, index) == index) { } else { result = String.Format("{0}null{1}", ps.Substring(0, index), index + 16 >= ps.Length ? "" : ps.Substring(index + 16)); } } else { if (ps.IndexOf(searchNull, index) == index) { result = String.Format("{0}{1}{2}", ps.Substring(0, index), tokenString, index + searchNull.Length >= ps.Length ? "" : ps.Substring(index + searchNull.Length)); } else { result = String.Format("{0}{1}{2}", ps.Substring(0, index), tokenString, index + tokenString.Length >= ps.Length ? "" : ps.Substring(index + tokenString.Length)); } } } //searchToken found } //searchName found return(result); }
private string FixInternalsVisibleToAttributeReference(string ps, string adName, byte[] pubKey) { string result = null; if (ps == adName || ps.StartsWith(adName + ",")) { if (pubKey == null) { result = adName; } else { result = String.Format("{0}, PublicKey={1}", adName, TokenUtils.GetReferenceTokenString(pubKey)); } }//searchName found return(result); }
private void FixByteReference(byte[] bytes, string adName, byte[] token) { const int TOKEN_LEN = 16; string searchName = String.Format("{0}, Version=", adName); byte[] searchBytes = Encoding.UTF8.GetBytes(searchName); string searchToken = "PublicKeyToken="; byte[] searchTokenBytes = Encoding.UTF8.GetBytes(searchToken); string searchNull = "null"; byte[] searchNullBytes = Encoding.UTF8.GetBytes(searchNull); string searchNullLong = "null ";//16 bytes byte[] searchNullLongBytes = Encoding.UTF8.GetBytes(searchNullLong); string tokenString = TokenUtils.GetReferenceTokenString(token); byte[] tokenStringBytes = Encoding.UTF8.GetBytes(tokenString); int index = BytesUtils.ByteSearch(bytes, searchBytes, 0, bytes.Length); while (index > 0) { index = BytesUtils.ByteSearch(bytes, searchTokenBytes, index + searchBytes.Length, bytes.Length - index - searchBytes.Length); if (index > 0) { index = index + searchTokenBytes.Length; if (token == null) { if (BytesUtils.ByteSearch(bytes, searchNullBytes, index, searchNullBytes.Length) == index) { } else { for (int i = 0; i < searchNullBytes.Length; i++) { bytes[index + i] = searchNullBytes[i]; } index += searchNullBytes.Length; for (int i = 0; i < TOKEN_LEN - searchNullBytes.Length; i++) { bytes[index + i] = 0x20; } } } else { if (BytesUtils.ByteSearch(bytes, searchNullBytes, index, searchNullBytes.Length) == index) { if (BytesUtils.ByteSearch(bytes, searchNullLongBytes, index, searchNullLongBytes.Length) == index) { for (int i = 0; i < tokenStringBytes.Length; i++) { bytes[index + i] = tokenStringBytes[i]; } } else { #region complicated, found PublicKeyToken=null and need to extend space to sign, ignore now /* * //1. extend bytes array * //2. amend bytes length for custom attribute or resource * // * // below is old code for custom attribute only * { * int endIndex = index + 4; * int sizeIndex = 2; * bool ok = false; * if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex]) * { * ok = true; * } * else * { * sizeIndex++; * if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex]) * { * ok = true; * } * } * * if (ok)//need to extend * { * byte[] newBytes = new byte[bytes.Length + 12]; * Array.Copy(bytes, newBytes, index); * Array.Copy(tokenStringBytes, newBytes, tokenStringBytes.Length); * for (int i = index + 4; i < bytes.Length; i++) * { * newBytes[i + 12] = bytes[i]; * } * newBytes[sizeIndex] += 12; * * result = newBytes; * bytes = newBytes; * } * else * { * endIndex = index + 16; * sizeIndex = 2; * ok = false; * if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex]) * { * ok = true; * } * else * { * sizeIndex++; * if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex]) * { * ok = true; * } * } * * if (ok) //enough room * { * for (int i = 0; i < tokenStringBytes.Length; i++) * { * bytes[index + i] = tokenStringBytes[i]; * } * } * } * } */ #endregion complicated, found PublicKeyToken=null and need to extend space to sign } } else { for (int i = 0; i < tokenStringBytes.Length; i++) { bytes[index + i] = tokenStringBytes[i]; } } } index = BytesUtils.ByteSearch(bytes, searchBytes, index + searchTokenBytes.Length, bytes.Length - index - searchTokenBytes.Length); } //end if searchToken found } //end if searchName found }