Beispiel #1
0
        public static Encoding[] DetectOutgoingEncodings(string input, int[] preferredEncodings, bool preserveOrder)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            // empty strings can always be encoded as ASCII
            if (input.Length == 0)
            {
                return new Encoding[] { Encoding.ASCII }
            }
            ;

            List <Encoding> result = new List <Encoding>();

            // get the IMultiLanguage3 interface
            MultiLanguage.IMultiLanguage3 multilang3 = new MultiLanguage.CMultiLanguageClass();
            if (multilang3 == null)
            {
                throw new COMException("Failed to get IMultilang3");
            }
            try
            {
                int[]  resultCodePages   = new int[preferredEncodings.Length];
                uint   detectedCodepages = (uint)resultCodePages.Length;
                ushort specialChar       = (ushort)'?';

                // get unmanaged arrays
                IntPtr pPrefEncs     = Marshal.AllocCoTaskMem(sizeof(uint) * preferredEncodings.Length);
                IntPtr pDetectedEncs = Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

                try
                {
                    Marshal.Copy(preferredEncodings, 0, pPrefEncs, preferredEncodings.Length);

                    Marshal.Copy(resultCodePages, 0, pDetectedEncs, resultCodePages.Length);

                    MultiLanguage.MLCPF options = MultiLanguage.MLCPF.MLDETECTF_VALID_NLS | MultiLanguage.MLCPF.MLDETECTF_PREFERRED_ONLY;
                    if (preserveOrder)
                    {
                        options |= MultiLanguage.MLCPF.MLDETECTF_PRESERVE_ORDER;
                    }

                    options |= MultiLanguage.MLCPF.MLDETECTF_PREFERRED_ONLY;

                    // finally... call to DetectOutboundCodePage
                    multilang3.DetectOutboundCodePage(options,
                                                      input, (uint)input.Length,
                                                      pPrefEncs, (uint)preferredEncodings.Length,
                                                      pDetectedEncs, ref detectedCodepages,
                                                      ref specialChar);

                    // get result
                    if (detectedCodepages > 0)
                    {
                        int[] theResult = new int[detectedCodepages];
                        Marshal.Copy(pDetectedEncs, theResult, 0, theResult.Length);

                        // get the encodings for the codepages
                        for (int i = 0; i < detectedCodepages; i++)
                        {
                            result.Add(Encoding.GetEncoding(theResult[i]));
                        }
                    }
                }
                finally
                {
                    if (pPrefEncs != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(pPrefEncs);
                    }
                    Marshal.FreeCoTaskMem(pDetectedEncs);
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multilang3);
            }
            // nothing found
            return(result.ToArray());
        }
Beispiel #2
0
        /// <summary>
        /// Rerurns up to maxEncodings codpages that are assumed to be apropriate
        /// </summary>
        /// <param name="input">array containing the raw data</param>
        /// <param name="maxEncodings">maxiumum number of encodings to detect</param>
        /// <returns>an array of Encoding with assumed encodings</returns>
        public static Encoding[] DetectInputCodepages(byte[] input, int maxEncodings)
        {
            if (maxEncodings < 1)
            {
                throw new ArgumentOutOfRangeException("maxEncodings", "at least one encoding must be returned");
            }

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            // empty strings can always be encoded as ASCII
            if (input.Length == 0)
            {
                return new Encoding[] { Encoding.ASCII }
            }
            ;

            // expand the string to be at least 256 bytes
            if (input.Length < 256)
            {
                byte[] newInput = new byte[256];

                int steps = 256 / input.Length;
                for (int i = 0; i < steps; i++)
                {
                    Array.Copy(input, 0, newInput, input.Length * i, input.Length);
                }

                int rest = 256 % input.Length;
                if (rest > 0)
                {
                    Array.Copy(input, 0, newInput, steps * input.Length, rest);
                }
                input = newInput;
            }

            List <Encoding> result = new List <Encoding>();

            // get the IMultiLanguage" interface
            MultiLanguage.IMultiLanguage2 multilang2 = new MultiLanguage.CMultiLanguageClass();
            if (multilang2 == null)
            {
                throw new COMException("Failed to get IMultilang2");
            }
            try
            {
                MultiLanguage.DetectEncodingInfo[] detectedEncdings = new MultiLanguage.DetectEncodingInfo[maxEncodings];

                int scores = detectedEncdings.Length;
                int srcLen = input.Length;

                // setup options (none)
                MultiLanguage.MLDETECTCP options = MultiLanguage.MLDETECTCP.MLDETECTCP_NONE;

                // finally... call to DetectInputCodepage
                multilang2.DetectInputCodepage(options, 0,
                                               ref input[0], ref srcLen, ref detectedEncdings[0], ref scores);

                // get result
                if (scores > 0)
                {
                    for (int i = 0; i < scores; i++)
                    {
                        // add the result
                        result.Add(Encoding.GetEncoding((int)detectedEncdings[i].nCodePage));
                    }
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multilang2);
            }
            // nothing found
            return(result.ToArray());
        }
Beispiel #3
0
        private static Encoding DetectOutgoingEncoding(string input, int[] preferredEncodings, bool preserveOrder)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            // empty strings can always be encoded as ASCII
            if (input.Length == 0)
            {
                return(Encoding.ASCII);
            }

            Encoding result = Encoding.ASCII;

            // get the IMultiLanguage3 interface
            MultiLanguage.IMultiLanguage3 multilang3 = new MultiLanguage.CMultiLanguageClass();
            if (multilang3 == null)
            {
                throw new COMException("Failed to get IMultilang3");
            }
            try
            {
                int[]  resultCodePages   = new int[preferredEncodings != null ? preferredEncodings.Length : Encoding.GetEncodings().Length];
                uint   detectedCodepages = (uint)resultCodePages.Length;
                ushort specialChar       = (ushort)'?';

                // get unmanaged arrays
                IntPtr pPrefEncs     = preferredEncodings == null ? IntPtr.Zero : Marshal.AllocCoTaskMem(sizeof(uint) * preferredEncodings.Length);
                IntPtr pDetectedEncs = Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

                try
                {
                    if (preferredEncodings != null)
                    {
                        Marshal.Copy(preferredEncodings, 0, pPrefEncs, preferredEncodings.Length);
                    }

                    Marshal.Copy(resultCodePages, 0, pDetectedEncs, resultCodePages.Length);

                    MultiLanguage.MLCPF options = MultiLanguage.MLCPF.MLDETECTF_VALID_NLS;
                    if (preserveOrder)
                    {
                        options |= MultiLanguage.MLCPF.MLDETECTF_PRESERVE_ORDER;
                    }

                    if (preferredEncodings != null)
                    {
                        options |= MultiLanguage.MLCPF.MLDETECTF_PREFERRED_ONLY;
                    }

                    multilang3.DetectOutboundCodePage(options,
                                                      input, (uint)input.Length,
                                                      pPrefEncs, (uint)(preferredEncodings == null ? 0 : preferredEncodings.Length),

                                                      pDetectedEncs, ref detectedCodepages,
                                                      ref specialChar);

                    // get result
                    if (detectedCodepages > 0)
                    {
                        int[] theResult = new int[detectedCodepages];
                        Marshal.Copy(pDetectedEncs, theResult, 0, theResult.Length);
                        result = Encoding.GetEncoding(theResult[0]);
                    }
                }
                finally
                {
                    if (pPrefEncs != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(pPrefEncs);
                    }
                    Marshal.FreeCoTaskMem(pDetectedEncs);
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multilang3);
            }
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="preferedEncodings"></param>
        /// <param name="preserveOrder"></param>
        /// <returns></returns>
        private static Encoding DetectOutgoingEncoding(string input, int[] preferedEncodings, bool preserveOrder)
        {
            if (input == null)
            throw new ArgumentNullException("input");

             // empty strings can always be encoded as ASCII
             if (input.Length == 0)
            return Encoding.ASCII;

             Encoding result = Encoding.ASCII;

             // get the IMultiLanguage3 interface
             MultiLanguage.IMultiLanguage3 multilang3 = new MultiLanguage.CMultiLanguageClass();
             if (multilang3 == null)
            throw new System.Runtime.InteropServices.COMException("Failed to get IMultilang3");
             try
             {
            int[] resultCodePages = new int[preferedEncodings != null ? preferedEncodings.Length : Encoding.GetEncodings().Length];
            uint detectedCodepages = (uint)resultCodePages.Length;
            ushort specialChar = (ushort)'?';

            // get unmanaged arrays
            IntPtr pPrefEncs = preferedEncodings == null ? IntPtr.Zero : Marshal.AllocCoTaskMem(sizeof(uint) * preferedEncodings.Length);
            IntPtr pDetectedEncs = Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

            try
            {
               if (preferedEncodings != null)
                  Marshal.Copy(preferedEncodings, 0, pPrefEncs, preferedEncodings.Length);

               Marshal.Copy(resultCodePages, 0, pDetectedEncs, resultCodePages.Length);

               MultiLanguage.MLCPF options = MultiLanguage.MLCPF.MLDETECTF_VALID_NLS;
               if (preserveOrder)
                  options |= MultiLanguage.MLCPF.MLDETECTF_PRESERVE_ORDER;

               if (preferedEncodings != null)
                  options |= MultiLanguage.MLCPF.MLDETECTF_PREFERRED_ONLY;

               multilang3.DetectOutboundCodePage(options,
                   input, (uint)input.Length,
                   pPrefEncs, (uint)(preferedEncodings == null ? 0 : preferedEncodings.Length),

                   pDetectedEncs, ref detectedCodepages,
                   ref specialChar);

               // get result
               if (detectedCodepages > 0)
               {
                  int[] theResult = new int[detectedCodepages];
                  Marshal.Copy(pDetectedEncs, theResult, 0, theResult.Length);
                  result = Encoding.GetEncoding(theResult[0]);
               }

            }
            finally
            {
               if (pPrefEncs != IntPtr.Zero)
                  Marshal.FreeCoTaskMem(pPrefEncs);
               Marshal.FreeCoTaskMem(pDetectedEncs);
            }
             }
             finally
             {
            Marshal.FinalReleaseComObject(multilang3);
             }
             return result;
        }
Beispiel #5
0
        /// <summary>
        /// Rerurns up to maxEncodings codpages that are assumed to be apropriate
        /// </summary>
        /// <param name="input">array containing the raw data</param>
        /// <param name="maxEncodings">maxiumum number of encodings to detect</param>
        /// <returns>an array of Encoding with assumed encodings</returns>
        public static Encoding[] DetectInputCodepages(byte[] input, int maxEncodings)
        {
            if (maxEncodings < 1)
            throw new ArgumentOutOfRangeException("at least one encoding must be returend", "maxEncodings");

             if (input == null)
            throw new ArgumentNullException("input");

             // empty strings can always be encoded as ASCII
             if (input.Length == 0)
            return new Encoding[] { Encoding.ASCII };

             // expand the string to be at least 256 bytes
             if (input.Length < 256)
             {
            byte[] newInput = new byte[256];
            int steps = 256 / input.Length;
            for (int i = 0; i < steps; i++)
               Array.Copy(input, 0, newInput, input.Length * i, input.Length);

            int rest = 256 % input.Length;
            if (rest > 0)
               Array.Copy(input, 0, newInput, steps * input.Length, rest);
            input = newInput;
             }

             List<Encoding> result = new List<Encoding>();

             // get the IMultiLanguage" interface
             MultiLanguage.IMultiLanguage2 multilang2 = new MultiLanguage.CMultiLanguageClass();

             if (multilang2 == null)
            throw new System.Runtime.InteropServices.COMException("Failed to get IMultilang2");
             try
             {
            MultiLanguage.DetectEncodingInfo[] detectedEncdings = new MultiLanguage.DetectEncodingInfo[maxEncodings];

            int scores = detectedEncdings.Length;
            int srcLen = input.Length;

            // setup options (none)
            MultiLanguage.MLDETECTCP options = MultiLanguage.MLDETECTCP.MLDETECTCP_NONE;

            // finally... call to DetectInputCodepage
            multilang2.DetectInputCodepage(options, 0,
                ref input[0], ref srcLen, ref detectedEncdings[0], ref scores);

            // get result
            if (scores > 0)
            {
               for (int i = 0; i < scores; i++)
               {
                  // add the result
                  result.Add(Encoding.GetEncoding((int)detectedEncdings[i].nCodePage));
               }
            }
             }
             finally
             {
            Marshal.FinalReleaseComObject(multilang2);
             }

             // nothing found
             return result.ToArray();
        }
Beispiel #6
0
        public static Encoding[] DetectOutgoingEncodings(string input, int[] preferredEncodings, bool preserveOrder)
        {

            if (input == null)
                throw new ArgumentNullException("input");

            // empty strings can always be encoded as ASCII
            if (input.Length == 0)
                return new Encoding[] { Encoding.ASCII };

            List<Encoding> result = new List<Encoding>();

            // get the IMultiLanguage3 interface
            MultiLanguage.IMultiLanguage3 multilang3 = new MultiLanguage.CMultiLanguageClass();
            if (multilang3 == null)
                throw new COMException("Failed to get IMultilang3");
            try
            {
                int[] resultCodePages = new int[preferredEncodings.Length];
                uint detectedCodepages = (uint)resultCodePages.Length;
                ushort specialChar = (ushort)'?';

                // get unmanaged arrays
                IntPtr pPrefEncs = Marshal.AllocCoTaskMem(sizeof(uint) * preferredEncodings.Length);
                IntPtr pDetectedEncs = Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

                try
                {
                    Marshal.Copy(preferredEncodings, 0, pPrefEncs, preferredEncodings.Length);

                    Marshal.Copy(resultCodePages, 0, pDetectedEncs, resultCodePages.Length);

                    MultiLanguage.MLCPF options = MultiLanguage.MLCPF.MLDETECTF_VALID_NLS | MultiLanguage.MLCPF.MLDETECTF_PREFERRED_ONLY;
                    if (preserveOrder)
                        options |= MultiLanguage.MLCPF.MLDETECTF_PRESERVE_ORDER;

                    options |= MultiLanguage.MLCPF.MLDETECTF_PREFERRED_ONLY;

                    // finally... call to DetectOutboundCodePage
                    multilang3.DetectOutboundCodePage(options,
                        input, (uint)input.Length,
                        pPrefEncs, (uint)preferredEncodings.Length,
                        pDetectedEncs, ref detectedCodepages,
                        ref specialChar);

                    // get result
                    if (detectedCodepages > 0)
                    {
                        int[] theResult = new int[detectedCodepages];
                        Marshal.Copy(pDetectedEncs, theResult, 0, theResult.Length);

                        // get the encodings for the codepages
                        for (int i = 0; i < detectedCodepages; i++)
                            result.Add(Encoding.GetEncoding(theResult[i]));

                    }

                }
                finally
                {
                    if (pPrefEncs != IntPtr.Zero)
                        Marshal.FreeCoTaskMem(pPrefEncs);
                    Marshal.FreeCoTaskMem(pDetectedEncs);
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multilang3);
            }
            // nothing found
            return result.ToArray();
        }
        /// <summary>
        /// Rerurns up to maxEncodings codpages that are assumed to be apropriate
        /// </summary>
        /// <param name="input">array containing the raw data</param>
        /// <param name="maxEncodings">maxiumum number of encodings to detect</param>
        /// <returns>an array of Encoding with assumed encodings</returns>
        public static Encoding[] DetectInputCodepages(byte[] input, int maxEncodings, uint preferedCodePage)
        {
            if (maxEncodings < 1)
            {
                throw new ArgumentOutOfRangeException("at least one encoding must be returend", "maxEncodings");
            }

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            // empty strings can always be encoded as ASCII
            if (input.Length == 0)
            {
                return new Encoding[] { Encoding.ASCII }
            }
            ;

            // expand the string to be at least 256 bytes
            if (input.Length < 256)
            {
                byte[] newInput = new byte[256];

                int steps = 256 / input.Length;
                for (int i = 0; i < steps; i++)
                {
                    Array.Copy(input, 0, newInput, input.Length * i, input.Length);
                }

                int rest = 256 % input.Length;
                if (rest > 0)
                {
                    Array.Copy(input, 0, newInput, steps * input.Length, rest);
                }
                input = newInput;
            }

            List <Encoding> result = new List <Encoding>();

            // get the IMultiLanguage" interface
            MultiLanguage.IMultiLanguage3 multilang3 = new MultiLanguage.CMultiLanguageClass();
            if (multilang3 == null)
            {
                throw new System.Runtime.InteropServices.COMException("Failed to get IMultilang3");
            }
            try
            {
                MultiLanguage.DetectEncodingInfo[] detectedEncdings;

                detectedEncdings = new MultiLanguage.DetectEncodingInfo[maxEncodings];

                int scores = detectedEncdings.Length;
                int srcLen = input.Length;

                // setup options (none)
                MultiLanguage.MLDETECTCP options = MultiLanguage.MLDETECTCP.MLDETECTCP_NONE;

                // finally... call to DetectInputCodepage
                // get unmanaged arrays
                IntPtr pPrefEncs = Marshal.AllocCoTaskMem(sizeof(uint) * PreferedEncodings.Length);
                if (PreferedEncodings != null)
                {
                    Marshal.Copy(PreferedEncodings, 0, pPrefEncs, PreferedEncodings.Length);
                }

                MultiLanguage.HRESULT hres = MultiLanguage.HRESULT.E_FAIL;


                //we need an array of signed bytes...
                sbyte[] sbInput = null;
                sbInput = new SByte[srcLen];
                for (int i = 0; i < srcLen; ++i)
                {
                    sbInput[i] = (SByte)input[i];
                }

                try
                {
                    hres = multilang3.DetectInputCodepage(
                        options,
                        preferedCodePage,
                        ref sbInput[0],
                        ref srcLen,
                        ref detectedEncdings[0],
                        ref scores
                        );
                }
                catch (COMException exc)
                {
                    MessageBox.Show(exc.Message);
                }


                //hres = multilang3.DetectInputCodepage(
                //    options,
                //    preferedCodePage,
                //    ref input[0],
                //    ref srcLen,
                //    ref detectedEncdings[0],
                //    ref scores
                //    );

                if (hres != MultiLanguage.HRESULT.S_OK)
                {
                    MessageBox.Show("Error 3 ! HRESULT=" + hres.ToString());
                }

                // get result
                if (scores > 0)
                {
                    for (int i = 0; i < scores; i++)
                    {
                        // add the result
                        result.Add(Encoding.GetEncoding((int)detectedEncdings[i].nCodePage));
                    }
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multilang3);
            }
            // nothing found
            return(result.ToArray());
        }