Esempio n. 1
0
        public static string GetStreamExtension(Stream stream)
        {
            // https://github.com/awslabs/aws-sdk-xamarin/blob/master/AWS.XamarinSDK/AWSSDK_Core/Amazon.Runtime/Internal/Util/HashStream.cs
            // Amazon.Runtime.Internal.Util.HashStream.Position {set; get;} throw NotSupportedException. First check CanSeek and default to PDF file extension
            if (!stream.CanSeek)
            {
                return(".pdf");
            }

            if (stream.Length < 4)
            {
                throw new InvalidDataException("Less than 4 bytes found in stream.");
            }

            stream.Position = 0;

            byte[] test = new byte[4];

            stream.Read(test, 0, 4);

            stream.Position = 0;

            string extension = string.Empty;

            if (test[0] == 0x25 && test[1] == 0x21) // standard ps or eps signature
            {
                extension = ".ps";

                if (stream.Length > 23)
                {
                    test = new byte[23];
                    stream.Read(test, 0, 23);

                    stream.Position = 0;

                    string tmp = System.Text.Encoding.ASCII.GetString(test);

                    if (tmp.ToUpper().Contains("EPS"))
                    {
                        extension = ".eps";
                    }
                }
            }
            else if (test[0] == 0xc5 && test[1] == 0xd0 && test[2] == 0xd3 && test[3] == 0xc6) // eps with preview header signature / magic number (always C5D0D3C6)
            {
                extension = ".eps";
            }
            else if (test[0] == 0x25 && test[1] == 0x50 && test[2] == 0x44 && test[3] == 0x46) // pdf signature
            {
                extension = ".pdf";
            }
            else
            {
                // try to search for pdf signature once again
                // this time look into all first 32 bytes as I run into pdf's that has extra bytes
                // at the beginning of the pdf file before the actual signature

                stream.Position = 0;

                if (stream.Length > 32)
                {
                    test = new byte[32];
                    stream.Read(test, 0, 32);

                    stream.Position = 0;

                    if (BufferHelper.IndexOf(test, new byte[] { 0x25, 0x50, 0x44, 0x46 }) > -1)
                    {
                        extension = ".pdf";
                    }
                }

                if (string.IsNullOrWhiteSpace(extension))
                {
                    // we didn't find pdf marker within first 32 bytes, read whole stream and search for pdf marker anywhere
                    BinaryReader reader = new BinaryReader(stream);
                    test = reader.ReadBytes((int)stream.Length);

                    stream.Position = 0;

                    if (BufferHelper.IndexOf(test, new byte[] { 0x25, 0x50, 0x44, 0x46 }) > -1)
                    {
                        extension = ".pdf";
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(extension))
            {
                throw new FormatException("Stream format is not valid! Please make sure it's PDF, PS or EPS.");
            }

            return(extension);
        }
        public static string GetStreamExtension(Stream stream)
        {
            if (stream.Length < 4)
            {
                throw new InvalidDataException("Less than 4 bytes found in stream.");
            }

            stream.Position = 0;

            byte[] test = new byte[4];

            stream.Read(test, 0, 4);

            stream.Position = 0;

            string extension = string.Empty;

            if (test[0] == 0x25 && test[1] == 0x21) // standard ps or eps signature
            {
                extension = ".ps";

                if (stream.Length > 23)
                {
                    test = new byte[23];
                    stream.Read(test, 0, 23);

                    stream.Position = 0;

                    string tmp = System.Text.Encoding.ASCII.GetString(test);

                    if (tmp.ToUpper().Contains("EPS"))
                    {
                        extension = ".eps";
                    }
                }
            }
            else if (test[0] == 0xc5 && test[1] == 0xd0 && test[2] == 0xd3 && test[3] == 0xc6) // eps with preview header signature / magic number (always C5D0D3C6)
            {
                extension = ".eps";
            }
            else if (test[0] == 0x25 && test[1] == 0x50 && test[2] == 0x44 && test[3] == 0x46) // pdf signature
            {
                extension = ".pdf";
            }
            else
            {
                // try to search for pdf signature once again
                // this time look into all first 32 bytes as I run into pdf's that has extra bytes
                // at the beginning of the pdf file before the actual signature

                stream.Position = 0;

                if (stream.Length > 32)
                {
                    test = new byte[32];
                    stream.Read(test, 0, 32);

                    stream.Position = 0;

                    if (BufferHelper.IndexOf(test, new byte[] { 0x25, 0x50, 0x44, 0x46 }) > -1)
                    {
                        extension = ".pdf";
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(extension))
            {
                throw new FormatException("Stream format is not valid! Please make sure it's PDF, PS or EPS.");
            }

            return(extension);
        }