Ejemplo n.º 1
0
        static void SetupPipeWait(App app)
        {
            var pipe = new NamedPipeServerStream(IPCName, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

            pipe.BeginWaitForConnection(result =>
            {
                pipe.EndWaitForConnection(result);

                var buf = new byte[sizeof(int)];
                pipe.Read(buf, 0, buf.Length);
                var len = BitConverter.ToInt32(buf, 0);
                buf     = new byte[len];
                pipe.Read(buf, 0, buf.Length);
                var commandLine = Coder.BytesToString(buf, Coder.CodePage.UTF8);

                app.Dispatcher.Invoke(() =>
                {
                    var window = app.CreateWindowsFromArgs(commandLine);
                    if (window != null)
                    {
                        window.Activate();
                        window.Show();
                        SetForegroundWindow(new WindowInteropHelper(window).Handle);
                    }
                });

                SetupPipeWait(app);
            }, null);
        }
Ejemplo n.º 2
0
        void VerifyCoder(Coder.CodePage codePage, string value, byte[] expected, bool bom, string reverse = null)
        {
            if (reverse == null)
            {
                reverse = value;
            }

            var result = Coder.TryStringToBytes(value, codePage, bom);

            Assert.AreEqual(result != null, expected != null);
            if (expected == null)
            {
                return;
            }

            var str = Coder.BytesToString(result, codePage, bom);

            Assert.AreEqual(str, reverse);

            Assert.AreEqual(expected.Length, result.Length);
            for (var ctr = 0; ctr < result.Length; ctr++)
            {
                Assert.AreEqual(expected[ctr], result[ctr]);
            }
        }
Ejemplo n.º 3
0
        public void TestUnicode()
        {
            var dir = Path.Combine(Directory.GetCurrentDirectory(), "TestData");

            Directory.CreateDirectory(dir);

            using (var combined = File.Create(Path.Combine(dir, "Combined.txt")))
            {
                foreach (var codePage in Coder.GetStringCodePages())
                {
                    if (Coder.PreambleSize(codePage) == 0)
                    {
                        continue;
                    }
                    for (var useBom = 0; useBom < 2; ++useBom)
                    {
                        var bom = useBom == 0;
                        foreach (var ending in GetValues <Endings>())
                        {
                            Encoding encoder = null;
                            switch (codePage)
                            {
                            case Coder.CodePage.UTF8:
                                encoder = new UTF8Encoding(false);
                                break;

                            case Coder.CodePage.UTF16LE:
                            case Coder.CodePage.UTF16BE:
                                encoder = new UnicodeEncoding(codePage == Coder.CodePage.UTF16BE, false);
                                break;

                            case Coder.CodePage.UTF32BE:
                            case Coder.CodePage.UTF32LE:
                                encoder = new UTF32Encoding(codePage == Coder.CodePage.UTF32BE, false);
                                break;

                            default:
                                throw new Exception("No encoder found");
                            }
                            var filename = Path.Combine(dir, $"{codePage}-{bom}-{ending}.txt");
                            var str      = GetText(codePage, bom, ending);
                            File.WriteAllText(filename, str, encoder);

                            var bytes = File.ReadAllBytes(filename);
                            combined.Write(bytes, 0, bytes.Length);

                            var encoding2 = Coder.GuessUnicodeEncoding(bytes);
                            var str2      = Coder.BytesToString(bytes, encoding2);
                            var bom2      = (str2.Length > 0) && (str2[0] == '\ufeff');

                            Assert.AreEqual(codePage, encoding2);
                            Assert.AreEqual(bom, bom2);
                            Assert.AreEqual(str, str2);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
 string GetDBValue(object value)
 {
     if ((value == DBNull.Value) || (value == null))
     {
         return(NULL);
     }
     if (value is byte[])
     {
         return(Coder.BytesToString(value as byte[], Coder.CodePage.Hex));
     }
     return(value.ToString());
 }
Ejemplo n.º 5
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var bytes = value as byte[];

            if (bytes == null)
            {
                return(null);
            }

            if (lastValue == null)
            {
                lastValue = Coder.BytesToString(bytes, Coder.CodePage.Hex);
            }
            return(lastValue);
        }
Ejemplo n.º 6
0
        void Command_Edit_Convert(EditConvertDialog.Result result)
        {
            var strs = GetSelectionStrings();

            if (!CheckCanEncode(strs, result.InputType))
            {
                return;
            }
            var bytes = strs.AsParallel().AsOrdered().Select(str => Coder.StringToBytes(str, result.InputType, result.InputBOM)).ToList();

            if (!CheckCanEncode(bytes, result.OutputType))
            {
                return;
            }
            ReplaceSelections(bytes.AsParallel().AsOrdered().Select(data => Coder.BytesToString(data, result.OutputType, result.OutputBOM)).ToList());
        }
Ejemplo n.º 7
0
        void Command_Edit_Data_Decrypt(EditDataEncryptDialog.Result result)
        {
            var strs = GetSelectionStrings();

            if (!CheckCanEncode(strs, result.InputCodePage))
            {
                return;
            }
            var decrypted = strs.AsParallel().AsOrdered().Select(str => Cryptor.Decrypt(Coder.StringToBytes(str, result.InputCodePage), result.CryptorType, result.Key)).ToList();

            if (!CheckCanEncode(decrypted, result.OutputCodePage))
            {
                return;
            }
            ReplaceSelections(decrypted.AsParallel().AsOrdered().Select(data => Coder.BytesToString(data, result.OutputCodePage)).ToList());
        }
Ejemplo n.º 8
0
        void Command_Edit_Data_Compress(EditDataCompressDialog.Result result)
        {
            var strs = GetSelectionStrings();

            if (!CheckCanEncode(strs, result.InputCodePage))
            {
                return;
            }
            var compressed = strs.AsParallel().AsOrdered().Select(str => Compressor.Compress(Coder.StringToBytes(str, result.InputCodePage), result.CompressorType)).ToList();

            if (!CheckCanEncode(compressed, result.OutputCodePage))
            {
                return;
            }
            ReplaceSelections(compressed.AsParallel().AsOrdered().Select(data => Coder.BytesToString(data, result.OutputCodePage)).ToList());
        }
Ejemplo n.º 9
0
        static async Task <string> GetURL(string url, Coder.CodePage codePage = Coder.CodePage.None)
        {
            using (var client = new WebClient())
            {
                client.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)";
                client.Encoding = Encoding.UTF8;
                var uri = new Uri(url);
                if (codePage == Coder.CodePage.None)
                {
                    return(await client.DownloadStringTaskAsync(uri));
                }

                var data = await client.DownloadDataTaskAsync(uri);

                return(Coder.BytesToString(data, codePage));
            }
        }
Ejemplo n.º 10
0
        public static Searcher GetSearcher(List <string> findStrs, List <Coder.CodePage> codePages, bool matchCase)
        {
            var data = new List <Tuple <byte[], bool> >();

            foreach (var findStr in findStrs)
            {
                foreach (var codePage in codePages)
                {
                    var bytes = Coder.TryStringToBytes(findStr, codePage);
                    if (bytes != null)
                    {
                        data.Add(Tuple.Create(bytes, (!Coder.IsStr(codePage)) || (matchCase) || (Coder.AlwaysCaseSensitive(codePage))));
                    }
                }
            }
            data = data.Distinct(tuple => $"{Coder.BytesToString(tuple.Item1, Coder.CodePage.Hex)}-{tuple.Item2}").ToList();
            return(new Searcher(data));
        }
Ejemplo n.º 11
0
        void InsertFiles(IEnumerable <string> fileNames)
        {
            if ((Selections.Count != 1) && (Selections.Count != fileNames.Count()))
            {
                throw new Exception("Must have either one or equal number of selections.");
            }

            var strs = new List <string>();

            foreach (var fileName in fileNames)
            {
                var bytes = File.ReadAllBytes(fileName);
                strs.Add(Coder.BytesToString(bytes, Coder.CodePage.AutoByBOM, true));
            }

            if (Selections.Count == 1)
            {
                ReplaceOneWithMany(strs, false);
            }
            if (Selections.Count == fileNames.Count())
            {
                ReplaceSelections(strs);
            }
        }