| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| /* | ||
| This file is part of the iText (R) project. | ||
| Copyright (c) 1998-2019 iText Group NV | ||
| Authors: iText Software. | ||
| This program is free software; you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License version 3 | ||
| as published by the Free Software Foundation with the addition of the | ||
| following permission added to Section 15 as permitted in Section 7(a): | ||
| FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY | ||
| ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT | ||
| OF THIRD PARTY RIGHTS | ||
| This program is distributed in the hope that it will be useful, but | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| or FITNESS FOR A PARTICULAR PURPOSE. | ||
| See the GNU Affero General Public License for more details. | ||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program; if not, see http://www.gnu.org/licenses or write to | ||
| the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| Boston, MA, 02110-1301 USA, or download the license from the following URL: | ||
| http://itextpdf.com/terms-of-use/ | ||
| The interactive user interfaces in modified source and object code versions | ||
| of this program must display Appropriate Legal Notices, as required under | ||
| Section 5 of the GNU Affero General Public License. | ||
| In accordance with Section 7(b) of the GNU Affero General Public License, | ||
| a covered work must retain the producer line in every PDF that is created | ||
| or manipulated using iText. | ||
| You can be released from the requirements of the license by purchasing | ||
| a commercial license. Buying such a license is mandatory as soon as you | ||
| develop commercial activities involving the iText software without | ||
| disclosing the source code of your own applications. | ||
| These activities include: offering paid services to customers as an ASP, | ||
| serving PDFs on the fly in a web application, shipping iText with a closed | ||
| source product. | ||
| For more information, please contact iText Software Corp. at this | ||
| address: sales@itextpdf.com | ||
| */ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace iTextSharp.text.pdf | ||
| { | ||
| /// <summary> | ||
| /// A | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// handles memory allocation and prevents decompressed pdf streams from occupation of more space than allowed. | ||
| /// </summary> | ||
| public class MemoryLimitsAwareHandler | ||
| { | ||
| private static readonly int SINGLE_SCALE_COEFFICIENT = 100; | ||
| private static readonly int SUM_SCALE_COEFFICIENT = 500; | ||
|
|
||
| private static readonly int SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE = int.MaxValue / 100; | ||
| private static readonly long SUM_OF_DECOMPRESSED_PDF_STREAMW_MIN_SIZE = int.MaxValue / 20; | ||
|
|
||
| private int MaxSizeOfSingleDecompressedPdfStream; | ||
| private long MaxSizeOfDecompressedPdfStreamsSum; | ||
|
|
||
| private long AllMemoryUsedForDecompression = 0; | ||
| private long MemoryUsedForCurrentPdfStreamDecompression = 0; | ||
|
|
||
| internal bool ConsiderCurrentPdfStream = false; | ||
|
|
||
| /// <summary> | ||
| /// Creates a | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// which will be used to handle decompression of pdf streams. | ||
| /// The max allowed memory limits will be generated by default. | ||
| /// </summary> | ||
| public MemoryLimitsAwareHandler() | ||
| { | ||
| MaxSizeOfSingleDecompressedPdfStream = SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE; | ||
| MaxSizeOfDecompressedPdfStreamsSum = SUM_OF_DECOMPRESSED_PDF_STREAMW_MIN_SIZE; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// which will be used to handle decompression of pdf streams. | ||
| /// The max allowed memory limits will be generated by default, based on the size of the document. | ||
| /// </summary> | ||
| /// <param name="documentSize">the size of the document, which is going to be handled by iText.</param> | ||
| public MemoryLimitsAwareHandler(long documentSize) | ||
| { | ||
| MaxSizeOfSingleDecompressedPdfStream = (int)CalculateDefaultParameter(documentSize, SINGLE_SCALE_COEFFICIENT, SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE); | ||
| MaxSizeOfDecompressedPdfStreamsSum = CalculateDefaultParameter(documentSize, SUM_SCALE_COEFFICIENT, SUM_OF_DECOMPRESSED_PDF_STREAMW_MIN_SIZE); | ||
| } | ||
|
|
||
| /// <summary>Gets the maximum allowed size which can be occupied by a single decompressed pdf stream.</summary> | ||
| /// <returns>the maximum allowed size which can be occupied by a single decompressed pdf stream.</returns> | ||
| public int GetMaxSizeOfSingleDecompressedPdfStream() | ||
| { | ||
| return MaxSizeOfSingleDecompressedPdfStream; | ||
| } | ||
|
|
||
| /// <summary>Sets the maximum allowed size which can be occupied by a single decompressed pdf stream.</summary> | ||
| /// <remarks> | ||
| /// Sets the maximum allowed size which can be occupied by a single decompressed pdf stream. | ||
| /// This value correlates with maximum heap size. This value should not exceed limit of the heap size. | ||
| /// iText will throw an exception if during decompression a pdf stream with two or more filters of identical type | ||
| /// occupies more memory than allowed. | ||
| /// </remarks> | ||
| /// <param name="maxSizeOfSingleDecompressedPdfStream">the maximum allowed size which can be occupied by a single decompressed pdf stream. | ||
| /// </param> | ||
| /// <returns> | ||
| /// this | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// instance. | ||
| /// </returns> | ||
| public MemoryLimitsAwareHandler SetMaxSizeOfSingleDecompressedPdfStream(int maxSizeOfSingleDecompressedPdfStream) | ||
| { | ||
| this.MaxSizeOfSingleDecompressedPdfStream = maxSizeOfSingleDecompressedPdfStream; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary>Gets the maximum allowed size which can be occupied by all decompressed pdf streams.</summary> | ||
| /// <returns>the maximum allowed size value which streams may occupy</returns> | ||
| public long GetMaxSizeOfDecompressedPdfStreamsSum() | ||
| { | ||
| return MaxSizeOfDecompressedPdfStreamsSum; | ||
| } | ||
|
|
||
| /// <summary>Sets the maximum allowed size which can be occupied by all decompressed pdf streams.</summary> | ||
| /// <remarks> | ||
| /// Sets the maximum allowed size which can be occupied by all decompressed pdf streams. | ||
| /// This value can be limited by the maximum expected PDF file size when it's completely decompressed. | ||
| /// Setting this value correlates with the maximum processing time spent on document reading | ||
| /// iText will throw an exception if during decompression pdf streams with two or more filters of identical type | ||
| /// occupy more memory than allowed. | ||
| /// </remarks> | ||
| /// <param name="maxSizeOfDecompressedPdfStreamsSum">he maximum allowed size which can be occupied by all decompressed pdf streams. | ||
| /// </param> | ||
| /// <returns> | ||
| /// this | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// instance. | ||
| /// </returns> | ||
| public MemoryLimitsAwareHandler SetMaxSizeOfDecompressedPdfStreamsSum(long maxSizeOfDecompressedPdfStreamsSum) | ||
| { | ||
| this.MaxSizeOfDecompressedPdfStreamsSum = maxSizeOfDecompressedPdfStreamsSum; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary>Considers the number of bytes which are occupied by the decompressed pdf stream.</summary> | ||
| /// <remarks> | ||
| /// Considers the number of bytes which are occupied by the decompressed pdf stream. | ||
| /// If memory limits have not been faced, throws an exception. | ||
| /// </remarks> | ||
| /// <param name="numOfOccupiedBytes">the number of bytes which are occupied by the decompressed pdf stream.</param> | ||
| /// <returns> | ||
| /// this | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// instance. | ||
| /// </returns> | ||
| /// <seealso> | ||
| /// | ||
| /// <see cref="MemoryLimitsAwareException"/> | ||
| /// </seealso> | ||
| internal MemoryLimitsAwareHandler ConsiderBytesOccupiedByDecompressedPdfStream(long numOfOccupiedBytes) | ||
| { | ||
| if (ConsiderCurrentPdfStream) | ||
| { | ||
| if (MemoryUsedForCurrentPdfStreamDecompression < numOfOccupiedBytes) | ||
| { | ||
| MemoryUsedForCurrentPdfStreamDecompression = numOfOccupiedBytes; | ||
| if (MemoryUsedForCurrentPdfStreamDecompression > MaxSizeOfSingleDecompressedPdfStream) | ||
| { | ||
| throw new MemoryLimitsAwareException(MemoryLimitsAwareException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed); | ||
| } | ||
| } | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary>Begins handling of current pdf stream decompression.</summary> | ||
| /// <returns> | ||
| /// this | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// instance. | ||
| /// </returns> | ||
| internal MemoryLimitsAwareHandler BeginDecompressedPdfStreamProcessing() | ||
| { | ||
| EnsureCurrentStreamIsReset(); | ||
| ConsiderCurrentPdfStream = true; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary>Ends handling of current pdf stream decompression.</summary> | ||
| /// <remarks> | ||
| /// Ends handling of current pdf stream decompression. | ||
| /// If memory limits have not been faced, throws an exception. | ||
| /// </remarks> | ||
| /// <returns> | ||
| /// this | ||
| /// <see cref="MemoryLimitsAwareHandler"/> | ||
| /// instance. | ||
| /// </returns> | ||
| /// <seealso> | ||
| /// | ||
| /// <see cref="MemoryLimitsAwareException"/> | ||
| /// </seealso> | ||
| internal MemoryLimitsAwareHandler EndDecompressedPdfStreamProcessing() | ||
| { | ||
| AllMemoryUsedForDecompression += MemoryUsedForCurrentPdfStreamDecompression; | ||
| if (AllMemoryUsedForDecompression > MaxSizeOfDecompressedPdfStreamsSum) | ||
| { | ||
| throw new MemoryLimitsAwareException(MemoryLimitsAwareException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed); | ||
| } | ||
| EnsureCurrentStreamIsReset(); | ||
| ConsiderCurrentPdfStream = false; | ||
| return this; | ||
| } | ||
|
|
||
| internal long GetAllMemoryUsedForDecompression() | ||
| { | ||
| return AllMemoryUsedForDecompression; | ||
| } | ||
|
|
||
| private static long CalculateDefaultParameter(long documentSize, int scale, long min) | ||
| { | ||
| long result = documentSize * scale; | ||
| if (result < min) | ||
| { | ||
| result = min; | ||
| } | ||
| if (result > min * scale) | ||
| { | ||
| result = min * scale; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private void EnsureCurrentStreamIsReset() | ||
| { | ||
| MemoryUsedForCurrentPdfStreamDecompression = 0; | ||
| } | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| This file is part of the iText (R) project. | ||
| Copyright (c) 1998-2019 iText Group NV | ||
| Authors: iText Software. | ||
| This program is free software; you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License version 3 | ||
| as published by the Free Software Foundation with the addition of the | ||
| following permission added to Section 15 as permitted in Section 7(a): | ||
| FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY | ||
| ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT | ||
| OF THIRD PARTY RIGHTS | ||
| This program is distributed in the hope that it will be useful, but | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| or FITNESS FOR A PARTICULAR PURPOSE. | ||
| See the GNU Affero General Public License for more details. | ||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program; if not, see http://www.gnu.org/licenses or write to | ||
| the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| Boston, MA, 02110-1301 USA, or download the license from the following URL: | ||
| http://itextpdf.com/terms-of-use/ | ||
| The interactive user interfaces in modified source and object code versions | ||
| of this program must display Appropriate Legal Notices, as required under | ||
| Section 5 of the GNU Affero General Public License. | ||
| In accordance with Section 7(b) of the GNU Affero General Public License, | ||
| a covered work must retain the producer line in every PDF that is created | ||
| or manipulated using iText. | ||
| You can be released from the requirements of the license by purchasing | ||
| a commercial license. Buying such a license is mandatory as soon as you | ||
| develop commercial activities involving the iText software without | ||
| disclosing the source code of your own applications. | ||
| These activities include: offering paid services to customers as an ASP, | ||
| serving PDFs on the fly in a web application, shipping iText with a closed | ||
| source product. | ||
| For more information, please contact iText Software Corp. at this | ||
| address: sales@itextpdf.com | ||
| */ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
|
|
||
| namespace iTextSharp.text.pdf | ||
| { | ||
|
|
||
| /** | ||
| * This class implements an output stream which can be used for memory limits aware decompression of pdf streams. | ||
| */ | ||
| internal class MemoryLimitsAwareOutputStream : MemoryStream { | ||
|
|
||
| /** | ||
| * The maximum size of array to allocate. | ||
| * Attempts to allocate larger arrays will result in an exception. | ||
| */ | ||
| private static readonly int DEFAULT_MAX_STREAM_SIZE = int.MaxValue - 8; | ||
|
|
||
| /** | ||
| * The maximum size of array to allocate. | ||
| * Attempts to allocate larger arrays will result in an exception. | ||
| */ | ||
| private int maxStreamSize = DEFAULT_MAX_STREAM_SIZE; | ||
|
|
||
| /** | ||
| * Creates a new byte array output stream. The buffer capacity is | ||
| * initially 32 bytes, though its size increases if necessary. | ||
| */ | ||
| public MemoryLimitsAwareOutputStream() : base() | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new byte array output stream, with a buffer capacity of | ||
| * the specified size, in bytes. | ||
| * | ||
| * @param size the initial size. | ||
| * @throws IllegalArgumentException if size is negative. | ||
| */ | ||
| public MemoryLimitsAwareOutputStream(int size) : base(size) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Gets the maximum size which can be occupied by this output stream. | ||
| * | ||
| * @return the maximum size which can be occupied by this output stream. | ||
| */ | ||
| public long GetMaxStreamSize() | ||
| { | ||
| return maxStreamSize; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the maximum size which can be occupied by this output stream. | ||
| * | ||
| * @param maxStreamSize the maximum size which can be occupied by this output stream. | ||
| * @return this {@link MemoryLimitsAwareOutputStream} | ||
| */ | ||
| public MemoryLimitsAwareOutputStream SetMaxStreamSize(int maxStreamSize) | ||
| { | ||
| this.maxStreamSize = maxStreamSize; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public override void Write(byte[] b, int off, int len) | ||
| { | ||
| if ((off < 0) || (off > b.Length) || (len < 0) || ((off + len) - b.Length > 0)) | ||
| { | ||
| throw new IndexOutOfRangeException(); | ||
| } | ||
| int minCapacity = (int)this.Position + len; | ||
| if (minCapacity < 0) | ||
| { | ||
| // overflow | ||
| throw new MemoryLimitsAwareException(MemoryLimitsAwareException.DuringDecompressionSingleStreamOccupiedMoreThanMaxIntegerValue | ||
| ); | ||
| } | ||
| if (minCapacity > maxStreamSize) | ||
| { | ||
| throw new MemoryLimitsAwareException(MemoryLimitsAwareException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed | ||
| ); | ||
| } | ||
| // calculate new capacity | ||
| int oldCapacity = this.GetBuffer().Length; | ||
| int newCapacity = oldCapacity << 1; | ||
| if (newCapacity < 0 || newCapacity - minCapacity < 0) | ||
| { | ||
| // overflow | ||
| newCapacity = minCapacity; | ||
| } | ||
| if (newCapacity - maxStreamSize > 0) | ||
| { | ||
| newCapacity = maxStreamSize; | ||
| this.Capacity = newCapacity; | ||
| } | ||
| base.Write(b, off, len); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| This file is part of the iText (R) project. | ||
| Copyright (c) 1998-2019 iText Group NV | ||
| Authors: iText Software. | ||
| This program is free software; you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License version 3 | ||
| as published by the Free Software Foundation with the addition of the | ||
| following permission added to Section 15 as permitted in Section 7(a): | ||
| FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY | ||
| ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT | ||
| OF THIRD PARTY RIGHTS | ||
| This program is distributed in the hope that it will be useful, but | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| or FITNESS FOR A PARTICULAR PURPOSE. | ||
| See the GNU Affero General Public License for more details. | ||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program; if not, see http://www.gnu.org/licenses or write to | ||
| the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| Boston, MA, 02110-1301 USA, or download the license from the following URL: | ||
| http://itextpdf.com/terms-of-use/ | ||
| The interactive user interfaces in modified source and object code versions | ||
| of this program must display Appropriate Legal Notices, as required under | ||
| Section 5 of the GNU Affero General Public License. | ||
| In accordance with Section 7(b) of the GNU Affero General Public License, | ||
| a covered work must retain the producer line in every PDF that is created | ||
| or manipulated using iText. | ||
| You can be released from the requirements of the license by purchasing | ||
| a commercial license. Buying such a license is mandatory as soon as you | ||
| develop commercial activities involving the iText software without | ||
| disclosing the source code of your own applications. | ||
| These activities include: offering paid services to customers as an ASP, | ||
| serving PDFs on the fly in a web application, shipping iText with a closed | ||
| source product. | ||
| For more information, please contact iText Software Corp. at this | ||
| address: sales@itextpdf.com | ||
| */ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using Org.BouncyCastle.X509; | ||
| using Org.BouncyCastle.Crypto; | ||
|
|
||
| namespace iTextSharp.text.pdf | ||
| { | ||
| public class ReaderProperties | ||
| { | ||
| internal X509Certificate certificate = null; | ||
| internal ICipherParameters certificateKey = null; | ||
| internal byte[] ownerPassword = null; | ||
| internal bool partialRead = false; | ||
| internal bool closeSourceOnconstructorError = true; | ||
| internal MemoryLimitsAwareHandler memoryLimitsAwareHandler = null; | ||
|
|
||
| public ReaderProperties SetCertificate(X509Certificate certificate) | ||
| { | ||
| this.certificate = certificate; | ||
| return this; | ||
| } | ||
|
|
||
| public ReaderProperties SetCertificateKey(ICipherParameters certificateKey) | ||
| { | ||
| this.certificateKey = certificateKey; | ||
| return this; | ||
| } | ||
|
|
||
| public ReaderProperties SetOwnerPassword(byte[] ownerPassword) | ||
| { | ||
| this.ownerPassword = ownerPassword; | ||
| return this; | ||
| } | ||
|
|
||
| public ReaderProperties SetPartialRead(bool partialRead) | ||
| { | ||
| this.partialRead = partialRead; | ||
| return this; | ||
| } | ||
|
|
||
| public ReaderProperties SetCloseSourceOnconstructorError(bool closeSourceOnconstructorError) | ||
| { | ||
| this.closeSourceOnconstructorError = closeSourceOnconstructorError; | ||
| return this; | ||
| } | ||
|
|
||
| public ReaderProperties SetMemoryLimitsAwareHandler(MemoryLimitsAwareHandler memoryLimitsAwareHandler) | ||
| { | ||
| this.memoryLimitsAwareHandler = memoryLimitsAwareHandler; | ||
| return this; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| This file is part of the iText (R) project. | ||
| Copyright (c) 1998-2019 iText Group NV | ||
| Authors: iText Software. | ||
| This program is free software; you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License version 3 | ||
| as published by the Free Software Foundation with the addition of the | ||
| following permission added to Section 15 as permitted in Section 7(a): | ||
| FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY | ||
| ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT | ||
| OF THIRD PARTY RIGHTS | ||
| This program is distributed in the hope that it will be useful, but | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| or FITNESS FOR A PARTICULAR PURPOSE. | ||
| See the GNU Affero General Public License for more details. | ||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program; if not, see http://www.gnu.org/licenses or write to | ||
| the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| Boston, MA, 02110-1301 USA, or download the license from the following URL: | ||
| http://itextpdf.com/terms-of-use/ | ||
| The interactive user interfaces in modified source and object code versions | ||
| of this program must display Appropriate Legal Notices, as required under | ||
| Section 5 of the GNU Affero General Public License. | ||
| In accordance with Section 7(b) of the GNU Affero General Public License, | ||
| a covered work must retain the producer line in every PDF that is created | ||
| or manipulated using iText. | ||
| You can be released from the requirements of the license by purchasing | ||
| a commercial license. Buying such a license is mandatory as soon as you | ||
| develop commercial activities involving the iText software without | ||
| disclosing the source code of your own applications. | ||
| These activities include: offering paid services to customers as an ASP, | ||
| serving PDFs on the fly in a web application, shipping iText with a closed | ||
| source product. | ||
| For more information, please contact iText Software Corp. at this | ||
| address: sales@itextpdf.com | ||
| */ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using NUnit.Framework; | ||
| using iTextSharp.text; | ||
| using iTextSharp.text.pdf; | ||
|
|
||
| namespace itextsharp.tests.iTextSharp.text.pdf | ||
| { | ||
| internal class CompressionTest | ||
| { | ||
| private const string TEST_RESOURCES_PATH = @"..\..\resources\text\pdf\CompressionTest\"; | ||
| private const string TARGET_PATH = @"CompressionTest\"; | ||
|
|
||
| [Test] | ||
| public void decompressionBombInsideSingleStreamTest01() | ||
| { | ||
| MemoryLimitsAwareHandler memoryHandler = new MemoryLimitsAwareHandler(); | ||
| memoryHandler.SetMaxSizeOfSingleDecompressedPdfStream(5000000); | ||
| memoryHandler.SetMaxSizeOfDecompressedPdfStreamsSum(1000000000000000000l); // just to ensure that the single stream related exception is thrown | ||
|
|
||
| ReaderProperties properties = new ReaderProperties(); | ||
| properties.SetMemoryLimitsAwareHandler(memoryHandler); | ||
|
|
||
| PdfReader reader = new PdfReader(properties, TEST_RESOURCES_PATH + "acsploit_output.pdf"); | ||
|
|
||
| testDecompressionBomb(reader, MemoryLimitsAwareException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed); | ||
| } | ||
|
|
||
| [Test] | ||
| public void decompressionBombInsideMultipleStreamsTimingTest01() { | ||
| MemoryLimitsAwareHandler memoryHandler = new MemoryLimitsAwareHandler(); | ||
| memoryHandler.SetMaxSizeOfSingleDecompressedPdfStream(int.MaxValue / 10 * 9); // just to ensure that the multiple streams related exception is thrown | ||
| memoryHandler.SetMaxSizeOfDecompressedPdfStreamsSum(1000000); | ||
|
|
||
| ReaderProperties properties = new ReaderProperties(); | ||
| properties.SetMemoryLimitsAwareHandler(memoryHandler); | ||
|
|
||
| PdfReader reader = new PdfReader(properties, TEST_RESOURCES_PATH + "acsploit_timing.pdf"); | ||
|
|
||
| testDecompressionBomb(reader, MemoryLimitsAwareException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed); | ||
| } | ||
|
|
||
| [Test] | ||
| public void decompressionBombInsideMultipleStreamsTimingTest02() | ||
| { | ||
| MemoryLimitsAwareHandler memoryHandler = new MemoryLimitsAwareHandler(); | ||
| memoryHandler.SetMaxSizeOfSingleDecompressedPdfStream(int.MaxValue / 10 * 9); // just to ensure that the multiple streams related exception is thrown | ||
| memoryHandler.SetMaxSizeOfDecompressedPdfStreamsSum(1000000); | ||
|
|
||
| ReaderProperties properties = new ReaderProperties(); | ||
| properties.SetMemoryLimitsAwareHandler(memoryHandler); | ||
|
|
||
| PdfReader reader = new PdfReader(properties, TEST_RESOURCES_PATH + "acsploit_timing2.pdf"); | ||
|
|
||
| testDecompressionBomb(reader, MemoryLimitsAwareException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed); | ||
| } | ||
| private static void testDecompressionBomb(PdfReader reader, String expectedExceptionMessage) | ||
| { | ||
|
|
||
| String thrownExceptionMessage = null; | ||
| try { | ||
| byte[] bytes = reader.GetPageContent(1); | ||
| } catch (MemoryLimitsAwareException e) { | ||
| thrownExceptionMessage = e.Message; | ||
| } catch (OutOfMemoryException e) { | ||
| Assert.IsTrue(false); | ||
| } | ||
|
|
||
| reader.Close(); | ||
| Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage); | ||
| } | ||
|
|
||
| } | ||
| } |