-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for scanning the document for objects when no valid cro…
…ss-reference table is found.
- Loading branch information
Showing
5 changed files
with
250 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// Copyright (c) PdfToSvg.NET contributors. | ||
// https://github.com/dmester/pdftosvg.net | ||
// Licensed under the MIT License. | ||
|
||
using PdfToSvg.DocumentModel; | ||
using PdfToSvg.IO; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
|
||
namespace PdfToSvg.Parsing | ||
{ | ||
internal static class ObjectScanner | ||
{ | ||
private enum ScanToken | ||
{ | ||
Obj, | ||
EndObj, | ||
Stream, | ||
EndStream, | ||
Trailer, | ||
} | ||
|
||
private enum ParserState | ||
{ | ||
None, | ||
InObject, | ||
InStream, | ||
AfterStream, | ||
} | ||
|
||
private class ScanLexeme | ||
{ | ||
public ScanToken Token; | ||
public long Position; | ||
|
||
public int ObjectNumber; | ||
public int Generation; | ||
|
||
public override string ToString() | ||
{ | ||
if (Token == ScanToken.Obj) | ||
{ | ||
return $"obj {ObjectNumber} {Generation}"; | ||
} | ||
|
||
return Token.ToString().ToLowerInvariant(); | ||
} | ||
} | ||
|
||
private class PositionComparer : IEqualityComparer<ScanLexeme> | ||
{ | ||
public bool Equals(ScanLexeme x, ScanLexeme y) | ||
{ | ||
return x.Position == y.Position; | ||
} | ||
|
||
public int GetHashCode(ScanLexeme obj) | ||
{ | ||
return unchecked((int)obj.Position); | ||
} | ||
} | ||
|
||
private static List<ScanLexeme> ScanStream(Stream stream, CancellationToken cancellationToken) | ||
{ | ||
var bufferStartPosition = 0L; | ||
var buffer = new byte[8096]; | ||
var bufferLength = 0; | ||
|
||
var lexemes = new List<ScanLexeme>(); | ||
|
||
int read; | ||
do | ||
{ | ||
if (bufferLength > 256) | ||
{ | ||
Buffer.BlockCopy(buffer, bufferLength - 128, buffer, 0, 128); | ||
bufferStartPosition += bufferLength - 128; | ||
bufferLength = 128; | ||
} | ||
|
||
read = stream.ReadAll(buffer, bufferLength, buffer.Length - bufferLength); | ||
bufferLength += read; | ||
|
||
if (read > 0) | ||
{ | ||
var bufferText = Encoding.ASCII.GetString(buffer, 0, bufferLength); | ||
|
||
foreach (Match match in Regex.Matches(bufferText, "[\r\n] {0,6}(stream\\b|endstream\\b|endobj\\b|trailer\\s{0,10}<<|(\\d{1,8}) (\\d{1,8}) obj\\b)")) | ||
{ | ||
var value = match.Groups[1].Value; | ||
var lexeme = new ScanLexeme(); | ||
|
||
lexeme.Position = bufferStartPosition + match.Groups[1].Index; | ||
|
||
if (value.StartsWith("stream")) | ||
{ | ||
lexeme.Token = ScanToken.Stream; | ||
} | ||
else if (value.StartsWith("endstream")) | ||
{ | ||
lexeme.Token = ScanToken.EndStream; | ||
} | ||
else if (value.StartsWith("endobj")) | ||
{ | ||
lexeme.Token = ScanToken.EndObj; | ||
} | ||
else if (value.StartsWith("trailer")) | ||
{ | ||
lexeme.Token = ScanToken.Trailer; | ||
} | ||
else | ||
{ | ||
lexeme.Token = ScanToken.Obj; | ||
lexeme.ObjectNumber = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture); | ||
lexeme.Generation = int.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); | ||
} | ||
|
||
lexemes.Add(lexeme); | ||
} | ||
} | ||
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
} | ||
while (read > 0); | ||
|
||
return lexemes; | ||
} | ||
|
||
public static bool TryScanObjects(Stream stream, out XRefTable xrefs, out List<long> trailerPositions, CancellationToken cancellationToken) | ||
{ | ||
stream.Position = 0; | ||
|
||
var lexemes = ScanStream(stream, cancellationToken); | ||
var state = ParserState.None; | ||
|
||
xrefs = new XRefTable(); | ||
|
||
trailerPositions = new List<long>(); | ||
|
||
foreach (var lexeme in lexemes.Distinct(new PositionComparer())) | ||
{ | ||
switch (lexeme.Token) | ||
{ | ||
case ScanToken.Obj: | ||
if (state != ParserState.InStream) | ||
{ | ||
xrefs.Add(new XRef | ||
{ | ||
ByteOffset = lexeme.Position, | ||
ObjectNumber = lexeme.ObjectNumber, | ||
Generation = lexeme.Generation, | ||
Type = XRefEntryType.NotFree, | ||
}); | ||
state = ParserState.InObject; | ||
} | ||
break; | ||
case ScanToken.Stream: | ||
if (state == ParserState.InObject) | ||
{ | ||
state = ParserState.InStream; | ||
} | ||
break; | ||
case ScanToken.EndStream: | ||
if (state == ParserState.InStream) | ||
{ | ||
state = ParserState.AfterStream; | ||
} | ||
break; | ||
case ScanToken.EndObj: | ||
if (state == ParserState.InStream) | ||
{ | ||
state = ParserState.None; | ||
} | ||
break; | ||
case ScanToken.Trailer: | ||
if (state != ParserState.InStream) | ||
{ | ||
trailerPositions.Add(lexeme.Position); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return trailerPositions.Count > 0 && xrefs.Count > 0; | ||
} | ||
|
||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.