Ejemplo n.º 1
0
        /// <summary>
        /// Decodes a URL, like System.Web.HttpUtility.UrlDecode
        /// </summary>
        /// <returns>The decoded URL</returns>
        /// <param name="value">The URL fragment to decode</param>
        /// <param name="encoding">The encoding to use</param>
        public static string UrlDecode(string value, System.Text.Encoding encoding = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            encoding = encoding ?? System.Text.Encoding.UTF8;

            var decoder = encoding.GetDecoder();
            var inbuf   = new byte[8];
            var outbuf  = new char[8];

            return(RE_NUMBER.Replace(value, (m) => {
                if (m.Value == "+")
                {
                    return " ";
                }

                try
                {
                    var hex = m.Groups["number"].Value;
                    var bytelen = hex.Length / 2;
                    Utility.HexStringAsByteArray(hex, inbuf);
                    var c = decoder.GetChars(inbuf, 0, bytelen, outbuf, 0);
                    return new string(outbuf, 0, c);
                }
                catch
                {
                }

                //Fallback
                return m.Value;
            }));
        }
        /// <summary>
        /// Decodes any byte array into a string
        /// </summary>
        /// <param name="byteArray">raw byte array to be decoded</param>
        /// <param name="encoding">byte array encoding type</param>
        /// <returns>decoded string</returns>
        public string DecodeByteArrayToString(byte[] byteArray, System.Text.Encoding encoding)
        {
            char[] chars          = new char[byteArray.Length + 1];
            System.Text.Decoder d = encoding.GetDecoder();
            int charLen           = d.GetChars(byteArray, 0, byteArray.Length, chars, 0);

            return(new System.String(chars));
        }
Ejemplo n.º 3
0
        static int _m_GetDecoder(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.Text.Encoding gen_to_be_invoked = (System.Text.Encoding)translator.FastGetCSObj(L, 1);



                {
                    System.Text.Decoder gen_ret = gen_to_be_invoked.GetDecoder(  );
                    translator.Push(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Ejemplo n.º 4
0
 public override int GetCharCount(byte[] bytes, int index, int count)
 {
     return(utf8Encoding.GetDecoder().GetCharCount(bytes, index, count, true));
 }
Ejemplo n.º 5
0
        public static bool ReadDelimitedDataFrom(this System.Text.Encoding encoding, byte[] buffer, char[] delimits, int offset, int count, out string result, out int read, out System.Exception any, bool includeDelimits = true)
        {
            read = Common.Binary.Zero;

            any = null;

            result = string.Empty;

            //Todo, check for large delemits and use a hash or always use a hash.
            //System.Collections.Generic.HashSet<char> delimitsC = new System.Collections.Generic.HashSet<char>(delimits);

            if (delimits == null)
            {
                delimits = EmptyChar;
            }

            int max;

            if (count.Equals(Common.Binary.Zero) || ArrayExtensions.IsNullOrEmpty(buffer, out max))
            {
                result = null;

                return(false);
            }

            //Account for the position
            max -= offset;

            //The smaller of the two, max and count
            if ((count = Common.Binary.Min(ref max, ref count)).Equals(0))
            {
                return(false);
            }

            bool sawDelimit = false;

            //Make the builder
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            //Use default..
            if (encoding == null)
            {
                encoding = System.Text.Encoding.Default;
            }

            System.Text.Decoder decoder = encoding.GetDecoder();

            //int charCount = decoder.GetCharCount(buffer, offset, count);

            //if(charCount == 0) return true;

            int toRead, delemitsLength = delimits.Length;

            toRead = delemitsLength <= Common.Binary.Zero ? count : Common.Binary.Max(1, delemitsLength);

            bool complete;

            int charsUsed;

            int justRead;

            //Could use Pool to save allocatios until StringBuilder can handle char*
            char[] results = new char[toRead];

            do
            {
                //Convert to utf16 from the encoding
#if UNSAFE
                unsafe { decoder.Convert((byte *)System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <byte>(buffer, offset), count, (char *)System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <char>(results, 0), toRead, count <= 0, out justRead, out charsUsed, out complete); }
#else
                decoder.Convert(buffer, offset, count, results, 0, toRead, count <= 0, out justRead, out charsUsed, out complete);
#endif

                //If there are not enough bytes to decode the char
                if (justRead.Equals(Common.Binary.Zero))
                {
                    break;
                }

                //Move the offsets and count for what was converted from the decoder
                offset += justRead;

                count -= justRead;

                //Iterate the decoded characters looking for a delemit
                if (delemitsLength > Common.Binary.Zero)
                {
                    for (int c = 0, e = charsUsed; c < e; ++c)
                    {
                        //Compare the char decoded to the delimits, if encountered set sawDelimit and either include the delemit or not.
#if NATIVE || UNSAFE
                        if (System.Array.IndexOf <char>(delimits, (char)System.Runtime.InteropServices.Marshal.ReadInt16(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <char>(results, c))) >= 0)
#else
                        if (System.Array.IndexOf <char>(delimits, results[c]) >= 0)
#endif
                        {
                            sawDelimit = true;

                            if (false.Equals(includeDelimits))
                            {
                                charsUsed = c;
                            }
                            else
                            {
                                charsUsed = ++c;
                            }

                            break;
                        }
                    }
                }

                if (charsUsed > 0)
                {
                    builder.Append(results, 0, charsUsed);
                }
            } while (count > 0 && false.Equals(sawDelimit));

            if (builder == null)
            {
                result = null;

                return(sawDelimit);
            }

            result = builder.Length.Equals(Common.Binary.Zero) ? string.Empty : builder.ToString();

            //Take the amount of bytes in the string as what was read.
            read = encoding.GetByteCount(result);

            builder = null;

            return(sawDelimit);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// This will look at the start of the file for the BOM and process it
        /// if it's found.
        /// </summary>
        private void CheckforAndHandleBOM()
        {
            byte[] utf32be = new byte[] { 0x00, 0x00, 0xfe, 0xff };
            byte[] utf32le = new byte[] { 0xff, 0xfe, 0x00, 0x00 };
            byte[] utf16be = new byte[] { 0xfe, 0xff };
            byte[] utf16le = new byte[] { 0xff, 0xfe };
            byte[] utf8    = new byte[] { 0xef, 0xbb, 0xbf };

            if (IsCurrentData(utf32le) || IsCurrentData(utf32be))
            {
                m_foundBOM  = true;
                m_BOMLength = utf32be.Length;
                //m_BOMEncoding = System.Text.Encoding.???;	// not currently defined for 32
                m_position = m_BOMLength;
            }
            else if (IsCurrentData(utf8))
            {
                m_foundBOM    = true;
                m_BOMLength   = utf8.Length;
                m_BOMEncoding = System.Text.Encoding.UTF8;
                m_position    = m_BOMLength;
            }
            else if (IsCurrentData(utf16le))
            {
                m_foundBOM    = true;
                m_BOMLength   = utf16le.Length;
                m_BOMEncoding = System.Text.Encoding.Unicode;
                m_position    = m_BOMLength;
            }
            else if (IsCurrentData(utf16be))
            {
                m_foundBOM    = true;
                m_BOMLength   = utf16be.Length;
                m_BOMEncoding = System.Text.Encoding.BigEndianUnicode;
                m_position    = m_BOMLength;
            }

            if (m_foundBOM)             // has one
            {
                if (m_BOMEncoding == System.Text.Encoding.UTF8)
                {
                    // no extra processing needed for UTF8 - this is the default format
                }
                else if (m_BOMEncoding == System.Text.Encoding.Unicode ||
                         m_BOMEncoding == System.Text.Encoding.BigEndianUnicode)
                {
                    System.Text.UTF8Encoding utf8Encoder = new System.Text.UTF8Encoding(false, true);
                    try
                    {
                        // decode the wide Unicode byte data to wide chars
                        System.Text.Decoder uniDecoder = m_BOMEncoding.GetDecoder();
                        int    charCount = uniDecoder.GetCharCount(m_FileData, 2, m_FileData.Length - 2);
                        char[] chars     = new Char[charCount];
                        uniDecoder.GetChars(m_FileData, 2, m_FileData.Length - 2, chars, 0);

                        // decode the wide chars to utf8 bytes
                        int newLength = utf8Encoder.GetByteCount(chars);
                        m_FileData = new byte[newLength];
                        utf8Encoder.GetBytes(chars, 0, chars.Length, m_FileData, 0);

                        // log msg for user to see
                        if (m_Log != null)
                        {
                            m_Log.AddWarning(String.Format(Sfm2XmlStrings.FileConvertedFrom0To1,
                                                           m_BOMEncoding.WebName, utf8Encoder.WebName));
                        }
                    }
                    catch (System.Exception e)
                    {
                        if (m_Log != null)
                        {
                            m_Log.AddFatalError(String.Format(Sfm2XmlStrings.CannotConvertFileFrom0To1,
                                                              m_BOMEncoding.WebName, utf8Encoder.WebName));
                            m_Log.AddFatalError(String.Format(Sfm2XmlStrings.Exception0, e.Message));
                        }
                        m_position = 0;
                        m_FileData = new byte[0];                               // don't process anything
                    }
                }
                else
                {
                    m_position = 0;
                    m_FileData = new byte[0];                           // don't process anything
                    if (m_Log != null)
                    {
                        m_Log.AddFatalError(Sfm2XmlStrings.CannotProcessUtf32Files);
                    }
                }
            }
        }
Ejemplo n.º 7
0
 // Instance constructor
 public StringDecoderInstance(Context context, ObjectInstance thisPrototype, string encoding)
     : base(context, thisPrototype)
 {
     Encoding = Context.GetEncoding(encoding);
     if (Encoding == null)
         throw new JavaScriptException(Engine, "TypeError", "Unknown encoding: " + encoding);
     Decoder = Encoding.GetDecoder();
 }