Пример #1
0
        public void SecondPartInvalidFormatFalse()
        {
            var input = StringBytesTestConverter.Convert("85 9t", false);

            var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var _);

            Assert.False(result);
        }
Пример #2
0
        public void TryReadIncorrectFormatMultiplePartsFalse()
        {
            var input = StringBytesTestConverter.Convert("76362 100 1000", false);

            var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var _);

            Assert.False(result);
        }
Пример #3
0
        public void SecondPartInvalidFormatFalse()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes("85 9t");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var _);

            Assert.False(result);
        }
Пример #4
0
        public void TryReadIncorrectFormatMultiplePartsFalse()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes(@"76362 100 1000");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var _);

            Assert.False(result);
        }
Пример #5
0
        public void ValidWithLongTrue()
        {
            var input = StringBytesTestConverter.Convert("214748364700 6", false);

            var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var definition);

            Assert.True(result);

            Assert.Equal(214748364700L, definition.FirstNumber);
            Assert.Equal(6, definition.Count);
        }
Пример #6
0
        public void ValidTrue()
        {
            var input = StringBytesTestConverter.Convert("12 32", false);

            var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var definition);

            Assert.True(result);

            Assert.Equal(12, definition.FirstNumber);
            Assert.Equal(32, definition.Count);
        }
Пример #7
0
        public void ValidTrue()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes("12 32");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var definition);

            Assert.True(result);

            Assert.Equal(12, definition.FirstNumber);
            Assert.Equal(32, definition.Count);
        }
Пример #8
0
        public void ValidWithLongTrue()
        {
            var bytes = OtherEncodings.StringAsLatin1Bytes("214748364700 6");

            var input = new RandomAccessBuffer(bytes);

            var result = TableSubsectionDefinition.TryRead(log, input, out var definition);

            Assert.True(result);

            Assert.Equal(214748364700L, definition.FirstNumber);
            Assert.Equal(6, definition.Count);
        }
Пример #9
0
            public bool TryParse(IRandomAccessRead source, long offset, bool isLenientParsing, CosObjectPool pool, out CrossReferenceTablePartBuilder builder)
            {
                builder = null;

                var tableStartOffset = source.GetPosition();

                if (source.Peek() != 'x')
                {
                    return(false);
                }

                var xref = ReadHelper.ReadString(source);

                if (!xref.Trim().Equals("xref"))
                {
                    return(false);
                }

                // check for trailer after xref
                var str = ReadHelper.ReadString(source);

                byte[] b = OtherEncodings.StringAsLatin1Bytes(str);

                source.Rewind(b.Length);

                if (str.StartsWith("trailer"))
                {
                    log.Warn("skipping empty xref table");
                    return(false);
                }

                builder = new CrossReferenceTablePartBuilder
                {
                    Offset   = offset,
                    XRefType = CrossReferenceType.Table
                };

                // Tables can have multiple sections. Each starts with a starting object id and a count.
                while (true)
                {
                    if (!TableSubsectionDefinition.TryRead(log, source, out var subsectionDefinition))
                    {
                        log.Warn($"Unexpected subsection definition in the cross-reference table at offset {offset}");

                        if (isLenientParsing)
                        {
                            break;
                        }

                        return(false);
                    }

                    var currentObjectId = subsectionDefinition.FirstNumber;

                    ReadHelper.SkipSpaces(source);
                    for (var i = 0; i < subsectionDefinition.Count; i++)
                    {
                        if (source.IsEof() || ReadHelper.IsEndOfName((char)source.Peek()))
                        {
                            break;
                        }

                        if (source.Peek() == 't')
                        {
                            break;
                        }

                        //Ignore table contents
                        var currentLine = ReadHelper.ReadLine(source);
                        var splitString = currentLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (splitString.Length < 3)
                        {
                            log.Warn("invalid xref line: " + currentLine);
                            break;
                        }

                        // This supports the corrupt table as reported in PDFBOX-474 (XXXX XXX XX n)
                        if (splitString[splitString.Length - 1].Equals(InUseEntry))
                        {
                            try
                            {
                                var objectOffset = long.Parse(splitString[0]);

                                if (objectOffset >= tableStartOffset && objectOffset <= source.GetPosition())
                                {
                                    // PDFBOX-3923: offset points inside this table - that can't be good
                                    throw new InvalidOperationException(
                                              $"Object offset {objectOffset} is within its own cross-reference table for object {currentObjectId}");
                                }

                                var generation = int.Parse(splitString[1]);
                                builder.Add(currentObjectId, generation, objectOffset);
                            }
                            catch (FormatException e)
                            {
                                throw new InvalidOperationException("Bad", e);
                            }
                        }
                        else if (!splitString[2].Equals(FreeEntry))
                        {
                            throw new InvalidOperationException(
                                      $"Corrupt cross-reference table entry for object {currentObjectId}. The indicator was not 'n' or 'f' but {splitString[2]}.");
                        }

                        currentObjectId++;

                        ReadHelper.SkipSpaces(source);
                    }

                    ReadHelper.SkipSpaces(source);
                    if (!ReadHelper.IsDigit(source))
                    {
                        break;
                    }
                }

                if (!TryParseTrailer(source, isLenientParsing, pool, out var trailer))
                {
                    throw new InvalidOperationException($"Something went wrong trying to read the XREF table at {offset}.");
                }

                builder.Dictionary = trailer;
                builder.Previous   = trailer.GetLongOrDefault(CosName.PREV);

                return(true);
            }