コード例 #1
0
ファイル: TestExtensions.cs プロジェクト: kaosborn/Filebert
        public void UnitE_AsciizToString()
        {
            var buf    = new byte[] { 0x30, 0x31, 0x42, 0x43, 0x44, 0, 0x46, 0x47 };
            var result = ConvertTo.FromAsciizToString(buf, 2);

            Assert.AreEqual(result.GetType(), typeof(string));
            Assert.AreEqual("BCD", result);
        }
コード例 #2
0
            public Model(Stream stream, string path)
            {
                base._data = Data = new JpegFormat(this, stream, path);

                // Arbitrary choice of 50MB cutoff.
                if (Data.FileSize > 50000000)
                {
                    IssueModel.Add("File insanely huge", Severity.Fatal);
                    return;
                }

                byte[] buf = new byte[(int)Data.FileSize];

                stream.Position = 0;
                int got = stream.Read(buf, 0, (int)Data.FileSize);

                if (got != Data.FileSize)
                {
                    IssueModel.Add("Read error", Severity.Fatal);
                    return;
                }

                for (int len = 0, pos = 2; ; pos = pos + len + 2)
                {
                    if (pos > Data.FileSize - 4)
                    {
                        IssueModel.Add("File truncated", Severity.Fatal);
                        return;
                    }

                    if (buf[pos] != 0xFF)
                    {
                        IssueModel.Add("Missing marker.", Severity.Fatal);
                        return;
                    }

                    if (buf[pos + 1] == 0xD9)
                    {
                        IssueModel.Add("Unexpected EOI marker", Severity.Fatal);
                        return;
                    }

                    // Detect SOS (Start of Stream) marker.
                    if (buf[pos + 1] == 0xDA)
                    {
                        Data.sosPos = pos;
                        break;
                    }

                    len = (buf[pos + 2] << 8) + buf[pos + 3];
                    if (len < 2)
                    {
                        IssueModel.Add("Invalid segment length '" + len + "'.", Severity.Fatal);
                        return;
                    }

                    if (pos + len + 2 >= Data.FileSize)
                    {
                        IssueModel.Add("File truncated.", Severity.Fatal);
                        return;
                    }

                    if (len == 2)
                    {
                        IssueModel.Add("Contains empty segment.", Severity.Trivia);
                    }

                    ++Data.SegmentCount;

                    // Detect application marker:
                    if (buf[pos + 1] >= 0xE0 && buf[pos + 1] <= 0xEF)
                    {
                        string appName = ConvertTo.FromAsciizToString(buf, pos + 4);
                        Data.appNames.Add(appName);

                        if (buf[pos + 1] == 0xE0)
                        {
                            if (buf[pos + 4] != 'J' || buf[pos + 5] != 'F' || buf[pos + 6] != 'I' || buf[pos + 7] != 'F' || buf[pos + 8] != 0)
                            {
                                continue;
                            }

                            if ((Data.Apps & JpegApps.Jfif) != 0)
                            {
                                IssueModel.Add("Contains ghost JFIF segment.", Severity.Advisory);
                                continue;
                            }

                            if (len < 13)
                            {
                                IssueModel.Add("JFIF segment too small.", Severity.Fatal);
                                return;
                            }

                            Data.Apps        |= JpegApps.Jfif;
                            Data.VersionMajor = buf[pos + 0x09];
                            Data.VersionMinor = buf[pos + 0x0A];
                            Data.Units        = (JpegUnits)buf[pos + 0x0B];
                            Data.DensityX     = (buf[pos + 0x0C] << 8) + buf[pos + 0x0D];
                            Data.DensityY     = (buf[pos + 0x0E] << 8) + buf[pos + 0x0F];
                            Data.ThumbXLen    = buf[pos + 0x10];
                            Data.ThumbYLen    = buf[pos + 0x11];
                        }
                        else if (buf[pos + 1] == 0xE1)
                        {
                            if (buf[pos + 4] == 'E' && buf[pos + 5] == 'x' && buf[pos + 6] == 'i' && buf[pos + 7] == 'f' && buf[pos + 8] == 0)
                            {
                                if ((Data.Apps & JpegApps.Exif) != 0)
                                {
                                    IssueModel.Add("Contains ghost Exif segment", Severity.Trivia);
                                    continue;
                                }

                                Data.Apps |= JpegApps.Exif;
                                // Additional Exif parsing goes here...
                            }
                        }
                    }
                }

                // Detect EOI (end of image) marker: FFD9
                for (int pos = (int)Data.sosPos; pos < Data.FileSize - 1; ++pos)
                {
                    if (buf[pos] == 0xFF && buf[pos + 1] == 0xD9)
                    {
                        Data.ValidSize = pos + 2;
                        Data.eoiPos    = pos;
                        break;
                    }
                }

                CalcMark();
                GetDiagnostics();
            }