diff --git a/src/UglyToad.PdfPig/Parser/CatalogFactory.cs b/src/UglyToad.PdfPig/Parser/CatalogFactory.cs index 9f272f88..2fb7c602 100644 --- a/src/UglyToad.PdfPig/Parser/CatalogFactory.cs +++ b/src/UglyToad.PdfPig/Parser/CatalogFactory.cs @@ -20,7 +20,7 @@ } } - public static Catalog Create(IndirectReference rootReference, DictionaryToken dictionary, + public static Catalog Create(IndirectReference rootReference, DictionaryToken dictionary, IPdfTokenScanner scanner, bool isLenientParsing) { @@ -64,8 +64,6 @@ return new Catalog(dictionary, pages, pageTree); } -#if NETSTANDARD2_0_OR_GREATER - private static PageTreeNode ProcessPagesNode(IndirectReference referenceInput, DictionaryToken nodeDictionaryInput, IndirectReference parentReferenceInput, bool isRoot, IPdfTokenScanner pdfTokenScanner, bool isLenientParsing, PageCounter pageNumber) { bool isPage = CheckIfIsPage(nodeDictionaryInput, parentReferenceInput, isRoot, pdfTokenScanner, isLenientParsing); @@ -79,11 +77,11 @@ //If we got here, we have to iterate till we manage to exit - var toProcess = new Queue<(PageTreeNode thisPage, IndirectReference reference, DictionaryToken nodeDictionary, IndirectReference parentReference, List nodeChildren)>(); - var firstPage = new PageTreeNode(nodeDictionaryInput, referenceInput, false, null); - var setChildren = new List(); + var toProcess = new Queue<(PageTreeNode thisPage, IndirectReference reference, DictionaryToken nodeDictionary, IndirectReference parentReference, List nodeChildren)>(); + var firstPage = new PageTreeNode(nodeDictionaryInput, referenceInput, false, null); + var setChildren = new List(); var firstPageChildren = new List(); - + setChildren.Add(() => firstPage.WithChildren(firstPageChildren)); toProcess.Enqueue((thisPage: firstPage, reference: referenceInput, nodeDictionary: nodeDictionaryInput, parentReference: parentReferenceInput, nodeChildren: firstPageChildren)); @@ -162,69 +160,5 @@ return isPage; } } - -#endif - - // Keep the algorithm below from throwing a StackOverflow exception. - // It probably should be refactored to not be recursive - private const ushort MAX_TREE_DEPTH = 1024; - - private static PageTreeNode ProcessPagesNode(IndirectReference reference, DictionaryToken nodeDictionary, IndirectReference parentReference, bool isRoot, IPdfTokenScanner pdfTokenScanner, bool isLenientParsing, PageCounter pageNumber, int depth = 0) - { - depth++; - - if (depth > MAX_TREE_DEPTH) { throw new PdfDocumentFormatException($"Tree exceeded maximum depth of {MAX_TREE_DEPTH}, aborting."); } - - var isPage = false; - - if (!nodeDictionary.TryGet(NameToken.Type, pdfTokenScanner, out NameToken type)) - { - if (!isLenientParsing) { throw new PdfDocumentFormatException($"Node in the document pages tree did not define a type: {nodeDictionary}."); } - - if (!nodeDictionary.TryGet(NameToken.Kids, pdfTokenScanner, out ArrayToken _)) { isPage = true; } - } - else - { - isPage = type.Equals(NameToken.Page); - - if (!isPage && !type.Equals(NameToken.Pages) && !isLenientParsing) { throw new PdfDocumentFormatException($"Node in the document pages tree defined invalid type: {nodeDictionary}."); } - } - - if (!isLenientParsing && !isRoot) - { - if (!nodeDictionary.TryGet(NameToken.Parent, pdfTokenScanner, out IndirectReferenceToken parentReferenceToken)) { throw new PdfDocumentFormatException($"Could not find parent indirect reference token on pages tree node: {nodeDictionary}."); } - - if (!parentReferenceToken.Data.Equals(parentReference)) { throw new PdfDocumentFormatException($"Pages tree node parent reference {parentReferenceToken.Data} did not match actual parent {parentReference}."); } - } - - if (isPage) - { - pageNumber.Increment(); - var newPage = new PageTreeNode(nodeDictionary, reference, true, pageNumber.PageCount).WithChildren(EmptyArray.Instance); - return newPage; - } - - if (!nodeDictionary.TryGet(NameToken.Kids, pdfTokenScanner, out ArrayToken kids)) - { - if (!isLenientParsing) { throw new PdfDocumentFormatException($"Pages node in the document pages tree did not define a kids array: {nodeDictionary}."); } - - kids = new ArrayToken(EmptyArray.Instance); - } - - var nodeChildren = new List(); - - foreach (var kid in kids.Data) - { - if (!(kid is IndirectReferenceToken kidRef)) { throw new PdfDocumentFormatException($"Kids array contained invalid entry (must be indirect reference): {kid}."); } - - if (!DirectObjectFinder.TryGet(kidRef, pdfTokenScanner, out DictionaryToken kidDictionaryToken)) { throw new PdfDocumentFormatException($"Could not find dictionary associated with reference in pages kids array: {kidRef}."); } - - var kidNode = ProcessPagesNode(kidRef.Data, kidDictionaryToken, reference, false, pdfTokenScanner, isLenientParsing, pageNumber, depth); - - nodeChildren.Add(kidNode); - } - - return new PageTreeNode(nodeDictionary, reference, false, null).WithChildren(nodeChildren); - } } }