Example #1
0
 public static string GetUtf32(string unicodeString)
 {
     var utf8 = new UTF32Encoding();
     byte[] encodeBytes = utf8.GetBytes(unicodeString);
     string decodedString = utf8.GetString(encodeBytes);
     return decodedString;
 }
Example #2
0
		static EncodingDetecingInputStream ()
		{
			StrictUTF8 = new UTF8Encoding (false, true);
			Strict1234UTF32 = new UTF32Encoding (true, false, true);
			StrictBigEndianUTF16 = new UnicodeEncoding (true, false, true);
			StrictUTF16 = new UnicodeEncoding (false, false, true);
		}
Example #3
0
        static StflApi()
        {
            // check if he has a graphical terminal. screen/tmux in not
            // detected in case someone is using it in pure text mode
            string termName = Environment.GetEnvironmentVariable("TERM");
            IsXterm = (termName != null && (termName.StartsWith("xterm") ||
                termName.StartsWith("rxvt")));
            // detect UTF-8 locale according to:
            // http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate
            var locale = Environment.GetEnvironmentVariable("LC_ALL") ??
                         Environment.GetEnvironmentVariable("LC_LCTYPE") ??
                         Environment.GetEnvironmentVariable("LANG") ??
                         String.Empty;
            locale = locale.ToUpperInvariant();
            IsUtf8Locale = locale.Contains("UTF-8") || locale.Contains("UTF8");

            EscapeLessThanCharacter = "<>";
            EscapeGreaterThanCharacter = ">";

            Utf32NativeEndian = new UTF32Encoding(
                bigEndian: !BitConverter.IsLittleEndian,
                byteOrderMark: false,
                throwOnInvalidCharacters: true
            );

            // UTF-32 handling is broken in mono < 4.2
            // fix in 4.4: https://github.com/mono/mono/commit/6bfb7e6d149f5e5c0fe04d680e3f7d36769ef541
            // fix in 4.2: https://github.com/mono/mono/commit/ea4ed4a47b98832e294d166bee5b8301fe87e216
            BrokenUtf32Handling = IsMonoVersionLessThan(4, 2);
        }
Example #4
0
        private static int GetUnicodeCode(char character)
        {
            UTF32Encoding encoding = new UTF32Encoding();
            byte[] bytes = encoding.GetBytes(character.ToString().ToCharArray());

            return BitConverter.ToInt32(bytes, 0);
        }
        private Encoding GetEncodingTypeByString(string str)
        {
            Encoding result = Encoding.Default;

            if (this.EncodingListComboBox.Text.IndexOf("UTF-8") != -1)
            {
                result = Encoding.UTF8;
            }
            else if (this.EncodingListComboBox.Text == "UTF-16 Big-Endian")
            {
                result = new System.Text.UnicodeEncoding(true, true);
            }
            else if (this.EncodingListComboBox.Text == "UTF-16 Little-Endian")
            {
                result = new System.Text.UnicodeEncoding(false, true);
            }
            else if (this.EncodingListComboBox.Text == "UTF-32 Big-Endian")
            {
                result = new System.Text.UTF32Encoding(true, true);
            }
            else if (this.EncodingListComboBox.Text == "UTF-32 Little-Endian")
            {
                result = new System.Text.UTF32Encoding(false, true);
            }

            return(result);
        }
Example #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            UTF32Encoding utf8 = new UTF32Encoding();
            string hashResult = BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(textBox1.Text)));

            label1.Text = hashResult;
            label2.Text = hashResult.Length.ToString();
        }
Example #7
0
        private void button2_Click(object sender, EventArgs e)
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            UTF32Encoding utf8 = new UTF32Encoding();
            string hashResult = BitConverter.ToString(sha1.ComputeHash(utf8.GetBytes(textBox2.Text)));

            label4.Text = hashResult;
            label3.Text = hashResult.Length.ToString();
        }
Example #8
0
        public static string Md5Encrypte(string inputString)
        {
            var u = new System.Text.UTF32Encoding();
            byte[] bytes = u.GetBytes(inputString);
            System.Security.Cryptography.MD5 md = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] result = md.ComputeHash(bytes);
            return Convert.ToBase64String(result);
        }
Example #9
0
		/// <summary>hashing from string. Result in format of 000-000</summary>
		public static string FromString(string input)
		{
			if (input.IsNullOrEmpty())
				return FromBuffer(null);


			var utf32 = new UTF32Encoding();
			return FromBuffer(utf32.GetBytes(input));
		}
Example #10
0
        /// <summary>
        /// Creates a password hash from the details provided
        /// </summary>
        /// <param name="created">The time when the person was created</param>
        /// <param name="password">The desired password</param>
        /// <param name="salt">The login's salt</param>
        /// <returns>A Byte[] with the password hash</returns>
        public Byte[] CreateHash(DateTime created, string password, string salt)
        {
            if (password == null) throw new ArgumentNullException("password");
            if (salt == null) throw new ArgumentNullException("salt");

            SHA1 sha = new SHA1CryptoServiceProvider();
            UTF32Encoding enc = new UTF32Encoding();
            var stringValue = string.Concat(_configurationManager.HashConstant, created, password, salt);
            return sha.ComputeHash(enc.GetBytes(stringValue));
        }
Example #11
0
        public static string Md5Encrypte(string inputString)
        {
            var u = new System.Text.UTF32Encoding();

            byte[] bytes = u.GetBytes(inputString);
            System.Security.Cryptography.MD5 md = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] result = md.ComputeHash(bytes);
            return(Convert.ToBase64String(result));
        }
Example #12
0
    public void Read_Timeout()
    {
        using (SerialPort com1 = TCSupport.InitFirstSerialPort())
            using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
            {
                char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
                byte[] byteXmitBuffer = new System.Text.UTF32Encoding().GetBytes(charXmitBuffer);
                char[] charRcvBuffer  = new char[charXmitBuffer.Length];

                int result;

                Debug.WriteLine(
                    "Verifying that Read(char[], int, int) works appropriately after TimeoutException has been thrown");

                com1.Encoding    = new System.Text.UTF32Encoding();
                com2.Encoding    = new System.Text.UTF32Encoding();
                com1.ReadTimeout = 500; // 20 seconds

                com1.Open();

                if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback
                {
                    com2.Open();
                }

                //Write the first 3 bytes of a character
                com2.Write(byteXmitBuffer, 0, 3);

                try
                {
                    com1.Read(charXmitBuffer, 0, charXmitBuffer.Length);
                    Fail("Err_29299aize Expected ReadTo to throw TimeoutException");
                }
                catch (TimeoutException)
                {
                } //Expected

                Assert.Equal(3, com1.BytesToRead);

                com2.Write(byteXmitBuffer, 3, byteXmitBuffer.Length - 3);

                //		retValue &= TCSupport.WaitForPredicate(delegate() {return com1.BytesToRead == byteXmitBuffer.Length; },
                //			5000, "Err_91818aheid Expected BytesToRead={0} actual={1}", byteXmitBuffer.Length, com1.BytesToRead);

                TCSupport.WaitForExpected(() => com1.BytesToRead, byteXmitBuffer.Length,
                                          5000, "Err_91818aheid BytesToRead");
                result = com1.Read(charRcvBuffer, 0, charRcvBuffer.Length);

                Assert.Equal(charXmitBuffer.Length, result);
                Assert.Equal(charXmitBuffer, charRcvBuffer);

                VerifyBytesReadOnCom1FromCom2(com1, com2, byteXmitBuffer, charXmitBuffer, charRcvBuffer, 0,
                                              charRcvBuffer.Length);
            }
    }
		[Category ("NotDotNet")] // A1/B1 return 24 on MS
		public void GetByteCount2 ()
		{
			string s = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";

			UTF32Encoding le = new UTF32Encoding (false, true);
			Assert.AreEqual (28, le.GetByteCount (s), "#A1");
			Assert.AreEqual (0, le.GetByteCount (string.Empty), "#A2");

			UTF32Encoding be = new UTF32Encoding (true, true);
			Assert.AreEqual (28, be.GetByteCount (s), "#B1");
			Assert.AreEqual (0, be.GetByteCount (string.Empty), "#B2");
		}
		[Test] // GetByteCount (String)
		public void GetByteCount2_S_Null ()
		{
			UTF32Encoding enc = new UTF32Encoding ();
			try {
				enc.GetByteCount ((string) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("s", ex.ParamName, "#5");
			}
		}
        public void OpensReaderWithoutAutoDetectEncoding()
        {
            string expected = "test";
            Encoding utf32 = new UTF32Encoding(false, true);
            byte[] resourceData = GetBytes(expected, utf32);
            EncodedResource r = new EncodedResource(new InputStreamResource(new MemoryStream(resourceData), "description"), Encoding.UTF8, false);
            StreamReader reader = (StreamReader)r.OpenReader();
            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            string actual = reader.ReadToEnd();
//            Assert.AreEqual("\uFFFD\uFFFD\0\0t\0\0\0e\0\0\0s\0\0\0t\0\0\0", actual);
            Assert.AreEqual(Encoding.UTF8.GetString(resourceData), actual);
            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
        }
		[Category ("NotDotNet")] // A1/B1 return 24 on MS
		public void GetByteCount1 ()
		{
			char [] chars = new char[] { 'z', 'a', '\u0306',
				'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };

			UTF32Encoding le = new UTF32Encoding (false, true);
			Assert.AreEqual (28, le.GetByteCount (chars), "#A1");
			Assert.AreEqual (0, le.GetByteCount (new char [0]), "#A2");

			UTF32Encoding be = new UTF32Encoding (true, true);
			Assert.AreEqual (28, be.GetByteCount (chars), "#B1");
			Assert.AreEqual (0, be.GetByteCount (new char [0]), "#B2");
		}
Example #17
0
        public string Manipulate(string s)
        {
            string res = string.Empty;

            System.Text.UTF32Encoding encoding = new System.Text.UTF32Encoding();
            byte[] bytes = encoding.GetBytes(s);

            for (int i = 0; i < bytes.Length; i += sizeof(int))
            {
                res += $@"\x{BitConverter.ToInt32(bytes, i):X}";
            }

            return(res);
        }
Example #18
0
 private string DecodeBase64String(string StrValue)
 {
     try
     {
         System.Text.UTF32Encoding objUTF32 = new System.Text.UTF32Encoding();
         byte[] objbytes = Convert.FromBase64String(StrValue);
         return(objUTF32.GetString(objbytes));
     }
     catch (Exception ex)
     {
         VMuktiHelper.ExceptionHandler(ex, "DecodeBase64String()", "CtlSettings.xaml.cs");
         return(null);
     }
 }
        public void OpensReaderWithAutoDetectEncoding()
        {
            string expected = "test";
            Encoding utf32 = new UTF32Encoding(false, true);
            byte[] resourceData = GetBytes(expected, utf32);
            resourceData = (byte[])ArrayUtils.Concat(utf32.GetPreamble(), resourceData);
            EncodedResource r = new EncodedResource( new InputStreamResource( new MemoryStream( resourceData), "description" ), Encoding.UTF8, true);
            StreamReader reader = (StreamReader)r.OpenReader();
            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            string actual = reader.ReadToEnd();
            Assert.AreEqual( "\uFEFF" + expected , actual);
// interestingly the line below is *not* true!
//            Assert.AreEqual(utf32.GetString(resourceData), actual);
            Assert.AreEqual(utf32, reader.CurrentEncoding);
        }
Example #20
0
        /// <summary>
        /// Compute the salted hash of the given password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string ComputeSaltedHash(string password)
        {
            if (password == null)
                throw new ArgumentNullException("password");

            //Encoder for password (text->bytes)
            UTF32Encoding encoder = new UTF32Encoding();

            //Put password bytes and then saly bytes into byte array
            var toHash = encoder.GetBytes(password)
                .Concat(BitConverter.GetBytes(Salt))
                .ToArray();

            // Hash the salted password
            using (SHA512Managed sha1 = new SHA512Managed())
                return BitConverter.ToString(sha1.ComputeHash(toHash)).Replace("-", "");
        }
Example #21
0
    public static string Md5Sum(string strToEncrypt)
    {
        System.Text.UTF32Encoding ue = new System.Text.UTF32Encoding();
        byte[] bytes = ue.GetBytes(strToEncrypt);

        // encrypt bytes
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashBytes = md5.ComputeHash(bytes);

        // Convert the encrypted bytes back to a string (base 16)
        string hashString = "";

        for (int i = 0; i < hashBytes.Length; i++)
        {
            hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
        }

        return(hashString.PadLeft(32, '0'));
    }
		[Test] // GetByteCount (Char *)
		public unsafe void GetByteCount3 ()
		{
			char [] chars = new char[] { 'z', 'a', '\u0306',
				'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };

			fixed (char* cp = chars) {
				UTF32Encoding le = new UTF32Encoding (false, true);
				Assert.AreEqual (12, le.GetByteCount (cp, 3), "#A1");
				Assert.AreEqual (4, le.GetByteCount (cp, 1), "#A2");
				Assert.AreEqual (0, le.GetByteCount (cp, 0), "#A3");
				Assert.AreEqual (24, le.GetByteCount (cp, 6), "#A4");
				//Assert.AreEqual (24, le.GetByteCount (cp, 7), "#A5");

				UTF32Encoding be = new UTF32Encoding (true, true);
				Assert.AreEqual (12, be.GetByteCount (cp, 3), "#B1");
				Assert.AreEqual (4, be.GetByteCount (cp, 1), "#B2");
				Assert.AreEqual (0, be.GetByteCount (cp, 0), "#B3");
				Assert.AreEqual (24, be.GetByteCount (cp, 6), "#B4");
				//Assert.AreEqual (24, be.GetByteCount (cp, 7), "#B5");
			}
		}
        private Encoding GetEncodingTypeByFile(string file)
        {
            Encoding result = Encoding.Default;

            if (File.Exists(file))
            {
                byte[] bomByte = new byte[4];
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        bomByte = br.ReadBytes(4);
                    }
                }
                if (bomByte[0] == 0xEF && bomByte[1] == 0xBB && bomByte[2] == 0xBF)
                {
                    result = Encoding.UTF8;
                }
                else if (bomByte[0] == 0x00 && bomByte[1] == 0x00 && bomByte[2] == 0xFE && bomByte[3] == 0xFF)
                {
                    result = new System.Text.UTF32Encoding(true, true);
                }
                else if (bomByte[0] == 0xFF && bomByte[1] == 0xFE && bomByte[2] == 0x00 && bomByte[3] == 0x00)
                {
                    result = new System.Text.UTF32Encoding(false, true);
                }
                else if (bomByte[0] == 0xFE && bomByte[1] == 0xFF)
                {
                    result = new System.Text.UnicodeEncoding(true, true);
                }
                else if (bomByte[0] == 0xFF && bomByte[1] == 0xFE)
                {
                    result = new System.Text.UnicodeEncoding(false, true);
                }
            }
            return(result);
        }
Example #24
0
    public void Read_Timeout()
    {
        using (SerialPort com1 = TCSupport.InitFirstSerialPort())
            using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
            {
                char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
                byte[] byteXmitBuffer = new System.Text.UTF32Encoding().GetBytes(charXmitBuffer);
                char[] charRcvBuffer  = new char[charXmitBuffer.Length];

                int result;

                Debug.WriteLine(
                    "Verifying that Read(char[], int, int) works appropriately after TimeoutException has been thrown");

                com1.Encoding    = new System.Text.UTF32Encoding();
                com2.Encoding    = new System.Text.UTF32Encoding();
                com1.ReadTimeout = 500; // 20 seconds

                com1.Open();

                if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback
                {
                    com2.Open();
                }

                //Write the first 3 bytes of a character
                com2.Write(byteXmitBuffer, 0, 3);

                Assert.Throws <TimeoutException>(() => com1.ReadChar());

                Assert.Equal(3, com1.BytesToRead);

                com2.Write(byteXmitBuffer, 3, byteXmitBuffer.Length - 3);

                TCSupport.WaitForExpected(() => com1.BytesToRead, byteXmitBuffer.Length,
                                          5000, "Err_91818aheid BytesToRead");

                result = com1.ReadChar();

                if (result != charXmitBuffer[0])
                {
                    Fail("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", charXmitBuffer[0], result);
                }
                else
                {
                    charRcvBuffer[0] = (char)result;
                    int readResult = com1.Read(charRcvBuffer, 1, charRcvBuffer.Length - 1);

                    if (readResult + 1 != charXmitBuffer.Length)
                    {
                        Fail("Err_051884ajoedo Expected Read to read {0} characters actually read {1}", charXmitBuffer.Length - 1, readResult);
                    }
                    else
                    {
                        for (int i = 0; i < charXmitBuffer.Length; ++i)
                        {
                            if (charRcvBuffer[i] != charXmitBuffer[i])
                            {
                                Fail("Err_05188ahed Characters differ at {0} expected:{1}({1:X}) actual:{2}({2:X})", i, charXmitBuffer[i], charRcvBuffer[i]);
                            }
                        }
                    }
                }

                VerifyBytesReadOnCom1FromCom2(com1, com2, byteXmitBuffer, charXmitBuffer);
            }
    }
		public void IsMailNewsSave ()
		{
			UTF32Encoding le = new UTF32Encoding (false, true);
			Assert.IsFalse (le.IsMailNewsSave);

			UTF32Encoding be = new UTF32Encoding (true, true);
			Assert.IsFalse (be.IsMailNewsSave, "#2");
		}
		public void IsMailNewsDisplay ()
		{
			UTF32Encoding le = new UTF32Encoding (false, true);
			Assert.IsFalse (le.IsMailNewsDisplay);

			UTF32Encoding be = new UTF32Encoding (true, true);
			Assert.IsFalse (be.IsMailNewsDisplay, "#2");
		}
		public void IsBrowserDisplay ()
		{
			UTF32Encoding le = new UTF32Encoding (false, true);
			Assert.IsFalse (le.IsBrowserDisplay, "#1");

			UTF32Encoding be = new UTF32Encoding (true, true);
			Assert.IsFalse (be.IsBrowserDisplay, "#2");
		}
		public void GetPreamble ()
		{
			byte[] lePreamble = new UTF32Encoding(false, true).GetPreamble();
			Assert.AreEqual (new byte [] { 0xff, 0xfe, 0, 0 }, lePreamble, "#1");

			byte[] bePreamble = new UTF32Encoding(true, true).GetPreamble();
			Assert.AreEqual (new byte [] { 0, 0, 0xfe, 0xff }, bePreamble, "#2");
		}
		[Test] // GetByteCount (Char [], Int32, Int32)
		public void GetByteCount4_Index_Overflow ()
		{
			char [] chars = new char[] { 'z', 'a', '\u0306',
				'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };

			UTF32Encoding enc = new UTF32Encoding ();
			try {
				enc.GetByteCount (chars, 7, 1);
				Assert.Fail ("#1");
			} catch (ArgumentOutOfRangeException ex) {
				// Index and count must refer to a location
				// within the buffer
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				//Assert.AreEqual ("chars", ex.ParamName, "#5");
			}
		}
Example #30
0
        public string base64Decode(string data)
        {
            try
            {
                System.Text.Encoding encoder = null;
                switch (m_Format)
                {
                    case Formats.UNICODE:
                        encoder = new System.Text.UnicodeEncoding(); break;
                    case Formats.UTF7:
                        encoder = new System.Text.UTF7Encoding(); break;
                    case Formats.UTF8:
                        encoder = new System.Text.UTF8Encoding(); break;
                    case Formats.UTF32:
                        encoder = new System.Text.UTF32Encoding(); break;
                    case Formats.ASCII:
                    default:
                        encoder = new System.Text.ASCIIEncoding(); break;
                }
                System.Text.Decoder decoder = encoder.GetDecoder();
                // System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
                // System.Text.Decoder utf8Decode = encoder.GetDecoder();

                byte[] todecode_byte = Convert.FromBase64String(data);
                int charCount = decoder.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char = new char[charCount];
                decoder.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);
                return result;
            }
            catch (Exception e)
            {
                throw new Exception("Error in base64Decode: " + e.Message);
            }
        }
Example #31
0
        [System.Security.SecuritySafeCritical]  // auto-generated
#endif
        public static Encoding GetEncoding(int codepage)
        {
            Encoding result = EncodingProvider.GetEncodingFromProvider(codepage);
            if (result != null)
                return result;

            //
            // NOTE: If you add a new encoding that can be get by codepage, be sure to
            // add the corresponding item in EncodingTable.
            // Otherwise, the code below will throw exception when trying to call
            // EncodingTable.GetDataItem().
            //
            if (codepage < 0 || codepage > 65535) {
                throw new ArgumentOutOfRangeException(
                    "codepage", Environment.GetResourceString("ArgumentOutOfRange_Range",
                        0, 65535));
            }

            Contract.EndContractBlock();

            // Our Encoding

            // See if we have a hash table with our encoding in it already.
            if (encodings != null) {
                result = (Encoding)encodings[codepage];
            }

            if (result == null)
            {
                // Don't conflict with ourselves
                lock (InternalSyncObject)
                {
                    // Need a new hash table
                    // in case another thread beat us to creating the Dictionary
                    if (encodings == null) {
                        encodings = new Hashtable();
                    }

                    // Double check that we don't have one in the table (in case another thread beat us here)
                    if ((result = (Encoding)encodings[codepage]) != null)
                        return result;

                    // Special case the commonly used Encoding classes here, then call
                    // GetEncodingRare to avoid loading classes like MLangCodePageEncoding
                    // and ASCIIEncoding.  ASP.NET uses UTF-8 & ISO-8859-1.
                    switch (codepage)
                    {
                        case CodePageDefault:                   // 0, default code page
                            result = Encoding.Default;
                            break;
                        case CodePageUnicode:                   // 1200, Unicode
                            result = Unicode;
                            break;
                        case CodePageBigEndian:                 // 1201, big endian unicode
                            result = BigEndianUnicode;
                            break;
#if FEATURE_CODEPAGES_FILE                            
                        case CodePageWindows1252:               // 1252, Windows
                            result = new SBCSCodePageEncoding(codepage);
                            break;
#else

#if FEATURE_UTF7
                            // on desktop, UTF7 is handled by GetEncodingRare.
                            // On Coreclr, we handle this directly without bringing GetEncodingRare, so that we get real UTF-7 encoding.
                        case CodePageUTF7:                      // 65000, UTF7
                            result = UTF7;
                            break;
#endif 

#if FEATURE_UTF32        
                        case CodePageUTF32:             // 12000
                            result = UTF32;
                            break;
                        case CodePageUTF32BE:           // 12001
                            result = new UTF32Encoding(true, true);
                            break;
#endif

#endif
                        case CodePageUTF8:                      // 65001, UTF8
                            result = UTF8;
                            break;

                        // These are (hopefully) not very common, but also shouldn't slow us down much and make default
                        // case able to handle more code pages by calling GetEncodingCodePage
                        case CodePageNoOEM:             // 1
                        case CodePageNoMac:             // 2
                        case CodePageNoThread:          // 3
                        case CodePageNoSymbol:          // 42
                            // Win32 also allows the following special code page values.  We won't allow them except in the
                            // CP_ACP case.
                            // #define CP_ACP                    0           // default to ANSI code page
                            // #define CP_OEMCP                  1           // default to OEM  code page
                            // #define CP_MACCP                  2           // default to MAC  code page
                            // #define CP_THREAD_ACP             3           // current thread's ANSI code page
                            // #define CP_SYMBOL                 42          // SYMBOL translations
                            throw new ArgumentException(Environment.GetResourceString(
                                "Argument_CodepageNotSupported", codepage), "codepage");
#if FEATURE_ASCII
                        // Have to do ASCII and Latin 1 first so they don't get loaded as code pages
                        case CodePageASCII:             // 20127
                            result = ASCII;
                            break;
#endif
#if FEATURE_LATIN1
                        case ISO_8859_1:                // 28591
                            result = Latin1;
                            break;
#endif                      
                        default:
                        {
#if FEATURE_CODEPAGES_FILE
                            // 1st assume its a code page.
                            result = GetEncodingCodePage(codepage);
                            if (result == null)
                                result = GetEncodingRare(codepage);
                            break;
#else
                            // Is it a valid code page?
                            if (EncodingTable.GetCodePageDataItem(codepage) == null)
                            {
                                throw new NotSupportedException(
                                    Environment.GetResourceString("NotSupported_NoCodepageData", codepage));
                            }

                            result = UTF8;
                            break;
#endif // FEATURE_CODEPAGES_FILE
                        }
                    }
                    encodings.Add(codepage, result);
                }

            }
            return result;
        }
		[Test] // GetByteCount (Char [], Int32, Int32)
		public void GetByteCount4 ()
		{
			char [] chars = new char[] { 'z', 'a', '\u0306',
				'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };

			UTF32Encoding le = new UTF32Encoding (false, true);
			Assert.AreEqual (12, le.GetByteCount (chars, 0, 3), "#A1");
			Assert.AreEqual (16, le.GetByteCount (chars, 2, 4), "#A2");
			Assert.AreEqual (4, le.GetByteCount (chars, 4, 1), "#A3");
			Assert.AreEqual (4, le.GetByteCount (chars, 6, 1), "#A4");
			Assert.AreEqual (0, le.GetByteCount (chars, 6, 0), "#A5");
			Assert.AreEqual (24, le.GetByteCount (chars, 0, 6), "#A6");
			//Assert.AreEqual (24, le.GetByteCount (chars, 0, 7), "#A7");

			UTF32Encoding be = new UTF32Encoding (true, true);
			Assert.AreEqual (12, be.GetByteCount (chars, 0, 3), "#B1");
			Assert.AreEqual (16, be.GetByteCount (chars, 2, 4), "#B2");
			Assert.AreEqual (4, be.GetByteCount (chars, 4, 1), "#B3");
			Assert.AreEqual (4, be.GetByteCount (chars, 6, 1), "#B4");
			Assert.AreEqual (0, be.GetByteCount (chars, 6, 0), "#B5");
			Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B6");
			//Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B7");
		}
Example #33
0
        public void TestDetectEncodingTextFromFile(string text, string format, bool BOM, EncodingType resultType)
        {
            Encoding typeFormat;//определяем формат файла

            switch (format)
            {
            case "UTF8":
            {
                typeFormat = new UTF8Encoding(BOM);
                break;
            }

            case "UTF32":
            {
                typeFormat = new System.Text.UTF32Encoding(true, BOM);
                break;
            }

            case "Unicode":
            {
                typeFormat = new System.Text.UnicodeEncoding(true, BOM);
                break;
            }

            case "ASCII":
            {
                typeFormat = new System.Text.UnicodeEncoding(true, BOM);
                break;
            }

            default:
            {
                typeFormat = new System.Text.UnicodeEncoding(true, BOM);
                break;
            }
            }

            //Создание файла
            //Сохранение в определенном формате
            //чтение файла
            if (File.Exists("Foo.txt"))
            {
                File.Delete("Foo.txt");
            }

            using (var file = new StreamWriter("Foo.txt", false, typeFormat))
            {
                file.WriteLine(text);
            }

            //проверка идентификации кодировки текста в файле
            DetectEncodingType detect = new DetectEncodingType();

            //индетификация только по Utf8
            detect.AddDetectEncodingType(EncodingType.UTF8,
                                         (data) => { if (data.Length >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
                                                     {
                                                         return(true);
                                                     }
                                                     return(false); });

            detect.SetBOM("Foo.txt");

            EncodingType type = EncodingType.NotDefined;

            type = detect.Detect();

            Assert.AreEqual(type, resultType);
        }
Example #34
0
 private void encodingToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
 {
     Encoding encod = null;
     if (string.IsNullOrEmpty(encodingToolStripComboBox.Text))
         encod = Module.FilesEncoding;
     else if (encodingToolStripComboBox.Text.StartsWith("Default", StringComparison.CurrentCultureIgnoreCase))
         encod = Encoding.Default;
     else if (encodingToolStripComboBox.Text.Equals("ASCII", StringComparison.CurrentCultureIgnoreCase))
         encod = new ASCIIEncoding();
     else if (encodingToolStripComboBox.Text.Equals("Unicode", StringComparison.CurrentCultureIgnoreCase))
         encod = new UnicodeEncoding();
     else if (encodingToolStripComboBox.Text.Equals("UTF7", StringComparison.CurrentCultureIgnoreCase))
         encod = new UTF7Encoding();
     else if (encodingToolStripComboBox.Text.Equals("UTF8", StringComparison.CurrentCultureIgnoreCase))
         encod = new UTF8Encoding(false);
     else if (encodingToolStripComboBox.Text.Equals("UTF32", StringComparison.CurrentCultureIgnoreCase))
         encod = new UTF32Encoding(true, false);
     else
         encod = Module.FilesEncoding;
     if (encod != this.Encoding)
     {
         this.Encoding = encod;
         this.OnExtraDiffArgumentsChanged();
     }
 }
Example #35
0
 //Used to mimic the charCodeAt Javascript method
 static String CharCode(char chr)
 {
     UTF32Encoding encoding = new UTF32Encoding();
     byte[] bytes = encoding.GetBytes(chr.ToString().ToCharArray());
     return BitConverter.ToInt32(bytes, 0).ToString();
 }
Example #36
0
        private static Encoding DefineEncoding(Encoding enc, FileStream fs)
        {
            int bytesPerSignature = 0;
            byte[] signature = new byte[4];
            int c = fs.Read(signature, 0, 4);
            if (signature[0] == 0xFF && signature[1] == 0xFE && signature[2] == 0x00 && signature[3] == 0x00 && c >= 4)
            {
                enc = Encoding.UTF32;//UTF32 LE
                bytesPerSignature = 4;
            }
            else
            if (signature[0] == 0x00 && signature[1] == 0x00 && signature[2] == 0xFE && signature[3] == 0xFF)
            {
                enc = new UTF32Encoding(true, true);//UTF32 BE
                bytesPerSignature = 4;
            }
            else
            if (signature[0] == 0xEF && signature[1] == 0xBB && signature[2] == 0xBF)
            {
                enc = Encoding.UTF8;//UTF8
                bytesPerSignature = 3;
            }
            else
            if (signature[0] == 0xFE && signature[1] == 0xFF)
            {
                enc = Encoding.BigEndianUnicode;//UTF16 BE
                bytesPerSignature = 2;
            }
            else
            if (signature[0] == 0xFF && signature[1] == 0xFE)
            {
                enc = Encoding.Unicode;//UTF16 LE
                bytesPerSignature = 2;
            }

            fs.Seek(bytesPerSignature, SeekOrigin.Begin);

            return enc;
        }
Example #37
0
    public bool Read_Timeout()
    {
        SerialPort com1 = TCSupport.InitFirstSerialPort();
        SerialPort com2 = TCSupport.InitSecondSerialPort(com1);

        char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
        byte[] byteXmitBuffer = new System.Text.UTF32Encoding().GetBytes(charXmitBuffer);
        char[] charRcvBuffer  = new char[charXmitBuffer.Length];
        bool   retValue       = true;
        int    result;

        Console.WriteLine("Verifying that Read(char[], int, int) works appropriately after TimeoutException has been thrown");

        com1.Encoding    = new System.Text.UTF32Encoding();
        com2.Encoding    = new System.Text.UTF32Encoding();
        com1.ReadTimeout = 500; // 20 seconds

        com1.Open();

        if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback
        {
            com2.Open();
        }

        //Write the first 3 bytes of a character
        com2.Write(byteXmitBuffer, 0, 3);

        try
        {
            com1.ReadChar();
            Console.WriteLine("Err_29299aize Expected ReadChar to throw TimeoutException");
            retValue = false;
        }
        catch (TimeoutException) { }//Expected

        if (3 != com1.BytesToRead)
        {
            Console.WriteLine("Err_689855ozpbea Expected BytesToRead: {0} actual: {1}", 3, com1.BytesToRead);
            retValue = false;
        }

        com2.Write(byteXmitBuffer, 3, byteXmitBuffer.Length - 3);

        retValue &= TCSupport.WaitForExpected(delegate() { return(com1.BytesToRead); }, byteXmitBuffer.Length,
                                              5000, "Err_91818aheid BytesToRead");

        result = com1.ReadChar();

        if (result != charXmitBuffer[0])
        {
            retValue = false;
            Console.WriteLine("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", charXmitBuffer[0], result);
        }
        else
        {
            charRcvBuffer[0] = (char)result;
            int readResult = com1.Read(charRcvBuffer, 1, charRcvBuffer.Length - 1);

            if (readResult + 1 != charXmitBuffer.Length)
            {
                retValue = false;
                Console.WriteLine("Err_051884ajoedo Expected Read to read {0} cahracters actually read {1}",
                                  charXmitBuffer.Length - 1, readResult);
            }
            else
            {
                for (int i = 0; i < charXmitBuffer.Length; ++i)
                {
                    if (charRcvBuffer[i] != charXmitBuffer[i])
                    {
                        retValue = false;
                        Console.WriteLine("Err_05188ahed Characters differ at {0} expected:{1}({1:X}) actual:{2}({2:X})",
                                          i, charXmitBuffer[i], charRcvBuffer[i]);
                    }
                }
            }
        }

        if (!VerifyBytesReadOnCom1FromCom2(com1, com2, byteXmitBuffer, charXmitBuffer))
        {
            Console.WriteLine("Err_05188ajied Verify ReadLine after read failed");
            retValue = false;
        }

        if (!retValue)
        {
            Console.WriteLine("Err_9858wiapxzg Verifying that ReadChar() works appropriately after TimeoutException has been thrown failed");
        }

        com1.Close();
        com2.Close();

        return(retValue);
    }
		[Test] // GetByteCount (Char [], Int32, Int32)
		public void GetByteCount4_Index_Negative ()
		{
			char [] chars = new char[] { 'z', 'a', '\u0306',
				'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };

			UTF32Encoding enc = new UTF32Encoding ();
			try {
				enc.GetByteCount (chars, -1, 1);
				Assert.Fail ("#1");
			} catch (ArgumentOutOfRangeException ex) {
				// Non-negative number required
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("index", ex.ParamName, "#5");
			}
		}
Example #39
0
        private static void GetEncoding()
        {
            string encoding = null;
            SafeSetString("encoding", x => encoding = x);

            if (string.IsNullOrEmpty(encoding))
                Encoding = new UTF8Encoding(false);
            else if (encoding.Equals("Default", StringComparison.CurrentCultureIgnoreCase))
                Encoding = Encoding.Default;
            else if (encoding.Equals("Unicode", StringComparison.CurrentCultureIgnoreCase))
                Encoding = new UnicodeEncoding();
            else if (encoding.Equals("ASCII", StringComparison.CurrentCultureIgnoreCase))
                Encoding = new ASCIIEncoding();
            else if (encoding.Equals("UTF7", StringComparison.CurrentCultureIgnoreCase))
                Encoding = new UTF7Encoding();
            else if (encoding.Equals("UTF32", StringComparison.CurrentCultureIgnoreCase))
                Encoding = new UTF32Encoding(true, false);
            else
                Encoding = new UTF8Encoding(false);
        }
Example #40
0
        [System.Security.SecurityCritical]  // auto-generated
        private static Encoding GetEncodingRare(int codepage)
        {
            Contract.Assert(codepage != 0 && codepage != 1200 && codepage != 1201 && codepage != 65001,
                "[Encoding.GetEncodingRare]This code page (" + codepage + ") isn't supported by GetEncodingRare!");
            Encoding result;
            switch (codepage)
            {
                case CodePageUTF7:              // 65000
                    result = UTF7;
                    break;
                case CodePageUTF32:             // 12000
                    result = UTF32;
                    break;
                case CodePageUTF32BE:           // 12001
                    result = new UTF32Encoding(true, true);
                    break;
                case ISCIIAssemese:
                case ISCIIBengali:
                case ISCIIDevanagari:
                case ISCIIGujarathi:
                case ISCIIKannada:
                case ISCIIMalayalam:
                case ISCIIOriya:
                case ISCIIPanjabi:
                case ISCIITamil:
                case ISCIITelugu:
                    result = new ISCIIEncoding(codepage);
                    break;
                // GB2312-80 uses same code page for 20936 and mac 10008
                case CodePageMacGB2312:
          //     case CodePageGB2312:
          //        result = new DBCSCodePageEncoding(codepage, EUCCN);
                    result = new DBCSCodePageEncoding(CodePageMacGB2312, CodePageGB2312);
                    break;

                // Mac Korean 10003 and 20949 are the same
                case CodePageMacKorean:
                    result = new DBCSCodePageEncoding(CodePageMacKorean, CodePageDLLKorean);
                    break;
                // GB18030 Code Pages
                case GB18030:
                    result = new GB18030Encoding();
                    break;
                // ISO2022 Code Pages
                case ISOKorean:
            //    case ISOSimplifiedCN
                case ChineseHZ:
                case ISO2022JP:         // JIS JP, full-width Katakana mode (no half-width Katakana)
                case ISO2022JPESC:      // JIS JP, esc sequence to do Katakana.
                case ISO2022JPSISO:     // JIS JP with Shift In/ Shift Out Katakana support
                    result = new ISO2022Encoding(codepage);
                    break;
                // Duplicate EUC-CN (51936) just calls a base code page 936,
                // so does ISOSimplifiedCN (50227), which's gotta be broken
                case DuplicateEUCCN:
                case ISOSimplifiedCN:
                    result = new DBCSCodePageEncoding(codepage, EUCCN);    // Just maps to 936
                    break;
                case EUCJP:
                    result = new EUCJPEncoding();
                    break;
                case EUCKR:
                    result = new DBCSCodePageEncoding(codepage, CodePageDLLKorean);    // Maps to 20949
                    break;
                case ENC50229:
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));
                case ISO_8859_8I:
                    result = new SBCSCodePageEncoding(codepage, ISO_8859_8_Visual);        // Hebrew maps to a different code page
                    break;
                default:
                    // Not found, already tried codepage table code pages in GetEncoding()
                    throw new NotSupportedException(
                        Environment.GetResourceString("NotSupported_NoCodepageData", codepage));
            }
            return result;
        }