Ejemplo n.º 1
0
        public async Task BomScan(Encoding enc)
        {
            var data         = Enumerable.Range(0, byte.MaxValue).Select(x => ANSI.GetChars(new byte[] { (byte)x })[0]).ToArray();
            var encodedBytes = (enc.GetPreamble().AsBucket() + enc.GetBytes(data).AsBucket()).ToArray();

            for (int i = 1; i < encodedBytes.Length - 1; i++)
            {
                var encSpan = encodedBytes.AsMemory();

                var b = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket();

                BucketBytes bb = await b.ConvertToUtf8(enc).ReadFullAsync(1024);

                Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Convert Iteration {i}");

                if (enc is UnicodeEncoding u && !u.GetPreamble().Any())
                {
                    continue; // Unicode without preamble not detectable without scan via .Peek()
                }
                b  = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket();
                bb = await b.NormalizeToUtf8().ReadFullAsync(1024);

                Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Normalize Iteration {i}");
            }
        }
Ejemplo n.º 2
0
        public async Task EncodeScan()
        {
            var enc          = new UTF8Encoding(false);
            var data         = Enumerable.Range(120, 20).Select(x => ANSI.GetChars(new byte[] { (byte)x })[0]).ToArray();
            var encodedBytes = (enc.GetPreamble().AsBucket() + enc.GetBytes(data).AsBucket()).ToArray();

            for (int i = 1; i < encodedBytes.Length - 1; i++)
            {
                var encSpan = encodedBytes.AsMemory();

                var b = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket();

                BucketBytes bb = await b.ConvertToUtf8(enc).ReadFullAsync(1024);

                Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Convert Iteration {i}");

                b  = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket();
                bb = await b.NormalizeToUtf8().ReadFullAsync(1024);

                Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Normalize Iteration {i}");
            }
        }
Ejemplo n.º 3
0
        internal static bool TryReadRefFile(string path, string?prefix, [NotNullWhen(true)] out string?result)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
#pragma warning disable CA1031 // Do not catch general exception types
            try
            {
                using var f = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 512);
                byte[] buf = new byte[512];

                int n = f.Read(buf, 0, buf.Length);

                if (buf.Length > 0 && n < buf.Length && n >= (prefix?.Length ?? 0))
                {
                    BucketEol eol = BucketEol.None;
                    if (buf.Length > 2 && buf[buf.Length - 1] == '\n')
                    {
                        if (buf[buf.Length - 2] == '\r')
                        {
                            eol = BucketEol.CRLF;
                        }
                        else
                        {
                            eol = BucketEol.LF;
                        }
                    }
                    else if (buf[buf.Length - 1] == '\r')
                    {
                        eol = BucketEol.CR;
                    }
                    else if (buf[buf.Length - 1] == '\n')
                    {
                        eol = BucketEol.LF;
                    }
                    else if (buf[buf.Length - 1] == '\0')
                    {
                        eol = BucketEol.Zero;
                    }

                    BucketBytes bb = new BucketBytes(buf, 0, n);

                    if (prefix != null)
                    {
                        var p = bb.Slice(0, prefix.Length).ToUTF8String();

                        if (prefix != p)
                        {
                            result = null;
                            return(false);
                        }
                        bb = bb.Slice(p.Length);
                    }

                    result = bb.ToUTF8String(eol);
                    return(true);
                }
            }
            catch (IOException)
            { }
            catch (NotSupportedException)
            { }
            catch (SystemException)
            { }
#pragma warning restore CA1031 // Do not catch general exception types

            result = null;
            return(false);
        }