Ejemplo n.º 1
0
 public StopEncoderFallbackBuffer(StopEncoderFallback fallback)
 {
     this.result = fallback.result ?? new EncoderResult();
 }
Ejemplo n.º 2
0
 public StopEncoderFallback(EncoderResult result)
 {
     this.result = result;
 }
Ejemplo n.º 3
0
        public static PhpBytes iconv(string in_charset, string out_charset, object str)
        {
            // check args
            if (str == null)
            {
                PhpException.ArgumentNull("str");
                return null;
            }
            if (out_charset == null)
            {
                PhpException.ArgumentNull("out_charset");
                return null;
            }

            // resolve out_charset
            bool transliterate, discard_ilseq;
            out_charset = ParseOutputEncoding(out_charset, out transliterate, out discard_ilseq);
            var out_encoding = ResolveEncoding(out_charset);
            if (out_encoding == null)
            {
                PhpException.Throw(PhpError.Notice, string.Format(Strings.wrong_charset, out_charset, in_charset, out_charset));
                return null;
            }

            // out_encoding.Clone() ensures it is NOT readOnly
            // then set EncoderFallback to catch handle unconvertable characters

            out_encoding = (Encoding)out_encoding.Clone();

            var out_result = new EncoderResult();

            if (transliterate)
                out_encoding.EncoderFallback = new TranslitEncoderFallback();   // transliterate unknown characters
            else if (discard_ilseq)
                out_encoding.EncoderFallback = new IgnoreEncoderFallback();    // ignore character and continue
            else
                out_encoding.EncoderFallback = new StopEncoderFallback(out_result);    // throw notice and discard all remaining characters

            try
            {
                //
                if (str.GetType() == typeof(PhpBytes))
                {
                    // resolve in_charset
                    if (in_charset == null)
                    {
                        PhpException.ArgumentNull("in_charset");
                        return null;
                    }
                    var in_encoding = ResolveEncoding(in_charset);
                    if (in_encoding == null)
                    {
                        PhpException.Throw(PhpError.Notice, string.Format(Strings.wrong_charset, in_charset, in_charset, out_charset));
                        return null;
                    }

                    // TODO: in_encoding.Clone() ensures it is NOT readOnly, then set DecoderFallback to catch invalid byte sequences

                    // convert <in_charset> to <out_charset>
                    return new PhpBytes(out_encoding.GetBytes(in_encoding.GetString(((PhpBytes)str).ReadonlyData)));
                }

                if (str.GetType() == typeof(string) || (str = Core.Convert.ObjectToString(str)) != null)
                {
                    // convert UTF16 to <out_charset>
                    return new PhpBytes(out_encoding.GetBytes((string)str));
                }
            }
            finally
            {
                if (out_result.firstFallbackCharIndex >= 0)
                {
                    // Notice: iconv(): Detected an illegal character in input string
                    PHP.Core.PhpException.Throw(Core.PhpError.Notice, Strings.illegal_character);
                }
            }
            
            return null;
        }
Ejemplo n.º 4
0
 public StopEncoderFallbackBuffer(StopEncoderFallback fallback)
 {
     this.result = fallback.result ?? new EncoderResult();
 }
Ejemplo n.º 5
0
 public StopEncoderFallback(EncoderResult result)
 {
     this.result = result;
 }
Ejemplo n.º 6
0
        public static PhpBytes iconv(string in_charset, string out_charset, object str)
        {
            // check args
            if (str == null)
            {
                PhpException.ArgumentNull("str");
                return(null);
            }
            if (out_charset == null)
            {
                PhpException.ArgumentNull("out_charset");
                return(null);
            }

            // resolve out_charset
            bool transliterate, discard_ilseq;

            out_charset = ParseOutputEncoding(out_charset, out transliterate, out discard_ilseq);
            var out_encoding = ResolveEncoding(out_charset);

            if (out_encoding == null)
            {
                PhpException.Throw(PhpError.Notice, string.Format(Strings.wrong_charset, out_charset, in_charset, out_charset));
                return(null);
            }

            // out_encoding.Clone() ensures it is NOT readOnly
            // then set EncoderFallback to catch handle unconvertable characters

            out_encoding = (Encoding)out_encoding.Clone();

            var out_result = new EncoderResult();

            if (transliterate)
            {
                out_encoding.EncoderFallback = new TranslitEncoderFallback();   // transliterate unknown characters
            }
            else if (discard_ilseq)
            {
                out_encoding.EncoderFallback = new IgnoreEncoderFallback();    // ignore character and continue
            }
            else
            {
                out_encoding.EncoderFallback = new StopEncoderFallback(out_result);    // throw notice and discard all remaining characters
            }
            try
            {
                //
                if (str.GetType() == typeof(PhpBytes))
                {
                    // resolve in_charset
                    if (in_charset == null)
                    {
                        PhpException.ArgumentNull("in_charset");
                        return(null);
                    }
                    var in_encoding = ResolveEncoding(in_charset);
                    if (in_encoding == null)
                    {
                        PhpException.Throw(PhpError.Notice, string.Format(Strings.wrong_charset, in_charset, in_charset, out_charset));
                        return(null);
                    }

                    // TODO: in_encoding.Clone() ensures it is NOT readOnly, then set DecoderFallback to catch invalid byte sequences

                    // convert <in_charset> to <out_charset>
                    return(new PhpBytes(out_encoding.GetBytes(in_encoding.GetString(((PhpBytes)str).ReadonlyData))));
                }

                if (str.GetType() == typeof(string) || (str = Core.Convert.ObjectToString(str)) != null)
                {
                    // convert UTF16 to <out_charset>
                    return(new PhpBytes(out_encoding.GetBytes((string)str)));
                }
            }
            finally
            {
                if (out_result.firstFallbackCharIndex >= 0)
                {
                    // Notice: iconv(): Detected an illegal character in input string
                    PHP.Core.PhpException.Throw(Core.PhpError.Notice, Strings.illegal_character);
                }
            }

            return(null);
        }