227 changes: 227 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfReaderDecodeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
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;
using iText.Kernel;
using iText.Test;
using iText.Test.Attributes;

namespace iText.Kernel.Pdf {
public class PdfReaderDecodeTest : ExtendedITextTest {
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/PdfReaderDecodeTest/";

public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/kernel/pdf/PdfReaderDecodeTest/";

[NUnit.Framework.OneTimeSetUp]
public static void BeforeClass() {
CreateDestinationFolder(destinationFolder);
}

/// <exception cref="System.IO.IOException"/>
[NUnit.Framework.Test]
public virtual void NoMemoryHandlerTest() {
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new MemoryStream()));
FileStream @is = new FileStream(sourceFolder + "stream", FileMode.Open, FileAccess.Read);
byte[] b = new byte[51];
@is.Read(b);
PdfArray array = new PdfArray();
PdfStream stream = new PdfStream(b);
stream.Put(PdfName.Filter, array);
stream.MakeIndirect(pdfDocument);
NUnit.Framework.Assert.AreEqual(51, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(40, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(992, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(1000000, PdfReader.DecodeBytes(b, stream).Length);
}

/// <exception cref="System.IO.IOException"/>
[NUnit.Framework.Test]
[LogMessage(iText.IO.LogMessageConstant.INVALID_INDIRECT_REFERENCE)]
[LogMessage(iText.IO.LogMessageConstant.XREF_ERROR)]
public virtual void DefaultMemoryHandlerTest() {
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf"), new PdfWriter(new MemoryStream
()));
PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0);
byte[] b = stream.GetBytes(false);
PdfArray array = new PdfArray();
stream.Put(PdfName.Filter, array);
NUnit.Framework.Assert.AreEqual(51, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(40, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(992, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(1000000, PdfReader.DecodeBytes(b, stream).Length);
}

/// <exception cref="System.IO.IOException"/>
[NUnit.Framework.Test]
[LogMessage(iText.IO.LogMessageConstant.INVALID_INDIRECT_REFERENCE)]
[LogMessage(iText.IO.LogMessageConstant.XREF_ERROR)]
public virtual void CustomMemoryHandlerSingleTest() {
MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();
handler.SetMaxSizeOfSingleDecompressedPdfStream(1000);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties(
).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()));
PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0);
byte[] b = stream.GetBytes(false);
PdfArray array = new PdfArray();
stream.Put(PdfName.Filter, array);
NUnit.Framework.Assert.AreEqual(51, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(40, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(992, PdfReader.DecodeBytes(b, stream).Length);
array.Add(PdfName.Fl);
String expectedExceptionMessage = PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed;
String thrownExceptionMessage = null;
try {
PdfReader.DecodeBytes(b, stream);
}
catch (MemoryLimitsAwareException e) {
thrownExceptionMessage = e.Message;
}
NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage);
}

/// <exception cref="System.IO.IOException"/>
[NUnit.Framework.Test]
[LogMessage(iText.IO.LogMessageConstant.INVALID_INDIRECT_REFERENCE)]
[LogMessage(iText.IO.LogMessageConstant.XREF_ERROR)]
public virtual void OneFilterCustomMemoryHandlerSingleTest() {
MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();
handler.SetMaxSizeOfSingleDecompressedPdfStream(20);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties(
).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()));
PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0);
byte[] b = stream.GetBytes(false);
PdfArray array = new PdfArray();
stream.Put(PdfName.Filter, array);
// Limit is reached, but the stream has no filters. Therefore we don't consider ot to be suspicious
NUnit.Framework.Assert.AreEqual(51, PdfReader.DecodeBytes(b, stream).Length);
// Limit is reached, but the stream has only one filter. Therefore we don't consider ot to be suspicious
array.Add(PdfName.Fl);
NUnit.Framework.Assert.AreEqual(40, PdfReader.DecodeBytes(b, stream).Length);
}

[NUnit.Framework.Test]
public virtual void DifferentFiltersEmptyTest() {
byte[] b = new byte[1000];
PdfArray array = new PdfArray();
array.Add(PdfName.Fl);
array.Add(PdfName.AHx);
array.Add(PdfName.A85);
array.Add(PdfName.RunLengthDecode);
PdfStream stream = new PdfStream(b);
stream.Put(PdfName.Filter, array);
NUnit.Framework.Assert.AreEqual(0, PdfReader.DecodeBytes(b, stream).Length);
}

/// <exception cref="System.IO.IOException"/>
[NUnit.Framework.Test]
[LogMessage(iText.IO.LogMessageConstant.INVALID_INDIRECT_REFERENCE)]
[LogMessage(iText.IO.LogMessageConstant.XREF_ERROR)]
public virtual void CustomMemoryHandlerSumTest() {
MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();
handler.SetMaxSizeOfDecompressedPdfStreamsSum(100000);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties(
).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()));
PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0);
byte[] b = stream.GetBytes(false);
String expectedExceptionMessage = PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed;
String thrownExceptionMessage = null;
try {
PdfReader.DecodeBytes(b, stream);
}
catch (MemoryLimitsAwareException e) {
thrownExceptionMessage = e.Message;
}
NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage);
}

/// <exception cref="System.IO.IOException"/>
[NUnit.Framework.Test]
[LogMessage(iText.IO.LogMessageConstant.INVALID_INDIRECT_REFERENCE)]
[LogMessage(iText.IO.LogMessageConstant.XREF_ERROR)]
public virtual void PageSumTest() {
MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();
handler.SetMaxSizeOfDecompressedPdfStreamsSum(1500000);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties(
).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()));
String expectedExceptionMessage = PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed;
String thrownExceptionMessage = null;
try {
pdfDocument.GetFirstPage().GetContentBytes();
}
catch (MemoryLimitsAwareException e) {
thrownExceptionMessage = e.Message;
}
NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage);
}

/// <exception cref="System.IO.IOException"/>
[NUnit.Framework.Test]
[LogMessage(iText.IO.LogMessageConstant.INVALID_INDIRECT_REFERENCE)]
[LogMessage(iText.IO.LogMessageConstant.XREF_ERROR)]
public virtual void PageAsSingleStreamTest() {
MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();
handler.SetMaxSizeOfSingleDecompressedPdfStream(1500000);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties(
).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()));
String expectedExceptionMessage = PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed;
String thrownExceptionMessage = null;
try {
pdfDocument.GetFirstPage().GetContentBytes();
}
catch (MemoryLimitsAwareException e) {
thrownExceptionMessage = e.Message;
}
NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage);
}
}
}
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions itext/itext.kernel/itext/kernel/PdfException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ public class PdfException : Exception {

public const String DocumentForCopyToCannotBeNull = "Document for copyTo cannot be null.";

public const String DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed = "During decompression multiple streams in sum occupied more memory than allowed. Please either check your pdf or increase the allowed single decompressed pdf stream maximum size value by setting the appropriate parameter of ReaderProperties's MemoryLimitsAwareHandler.";

public const String DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed = "During decompression a single stream occupied more memory than allowed. Please either check your pdf or increase the allowed multiple decompressed pdf streams maximum size value by setting the appropriate parameter of ReaderProperties's MemoryLimitsAwareHandler.";

public const String DuringDecompressionSingleStreamOccupiedMoreThanMaxIntegerValue = "During decompression a single stream occupied more than a maximum integer value. Please check your pdf.";

public const String EndOfContentStreamReachedBeforeEndOfImageData = "End of content stream reached before end of image data.";

public const String ErrorWhileReadingObjectStream = "Error while reading Object Stream.";
Expand Down
76 changes: 76 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/MemoryLimitsAwareException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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 iText.Kernel;

namespace iText.Kernel.Pdf {
/// <summary>Exception class for exceptions occurred during decompressed pdf streams processing.</summary>
public class MemoryLimitsAwareException : PdfException {
/// <summary>Creates a new instance of MemoryLimitsAwareException.</summary>
/// <param name="message">the detail message.</param>
public MemoryLimitsAwareException(String message)
: base(message) {
}

/// <summary>Creates a new instance of MemoryLimitsAwareException.</summary>
/// <param name="cause">
/// the cause (which is saved for later retrieval by
/// <see cref="System.Exception.InnerException()"/>
/// method).
/// </param>
public MemoryLimitsAwareException(Exception cause)
: this(UnknownPdfException, cause) {
}

/// <summary>Creates a new instance of MemoryLimitsAwareException.</summary>
/// <param name="message">the detail message.</param>
/// <param name="cause">
/// the cause (which is saved for later retrieval by
/// <see cref="System.Exception.InnerException()"/>
/// method).
/// </param>
public MemoryLimitsAwareException(String message, Exception cause)
: base(message, cause) {
}
}
}
82 changes: 82 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/MemoryLimitsAwareFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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.IO;
using iText.Kernel.Pdf.Filters;

namespace iText.Kernel.Pdf {
/// <summary>Handles memory limits aware processing.</summary>
/// <seealso>
///
/// <see cref="MemoryLimitsAwareHandler"/>
/// </seealso>
public abstract class MemoryLimitsAwareFilter : IFilterHandler {
/// <summary>
/// Creates a
/// <see cref="MemoryLimitsAwareOutputStream"/>
/// which will be used for decompression of the passed pdf stream.
/// </summary>
/// <param name="streamDictionary">the pdf stream which is going to be decompressed.</param>
/// <returns>
/// the
/// <see cref="System.IO.MemoryStream"/>
/// which will be used for decompression of the passed pdf stream
/// </returns>
public virtual MemoryStream EnableMemoryLimitsAwareHandler(PdfDictionary streamDictionary) {
MemoryLimitsAwareOutputStream outputStream = new MemoryLimitsAwareOutputStream();
MemoryLimitsAwareHandler memoryLimitsAwareHandler = null;
if (null != streamDictionary.GetIndirectReference()) {
memoryLimitsAwareHandler = streamDictionary.GetIndirectReference().GetDocument().memoryLimitsAwareHandler;
}
else {
// We do not reuse some static instance because one can process pdfs in different threads.
memoryLimitsAwareHandler = new MemoryLimitsAwareHandler();
}
if (null != memoryLimitsAwareHandler && memoryLimitsAwareHandler.considerCurrentPdfStream) {
outputStream.SetMaxStreamSize(memoryLimitsAwareHandler.GetMaxSizeOfSingleDecompressedPdfStream());
}
return outputStream;
}

public abstract byte[] Decode(byte[] arg1, PdfName arg2, PdfObject arg3, PdfDictionary arg4);
}
}
231 changes: 231 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/MemoryLimitsAwareHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/*
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 iText.Kernel;

namespace iText.Kernel.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 const int SINGLE_SCALE_COEFFICIENT = 100;

private const int SUM_SCALE_COEFFICIENT = 500;

private const int SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE = int.MaxValue / 100;

private const 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 virtual 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 virtual iText.Kernel.Pdf.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 virtual 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 virtual iText.Kernel.Pdf.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 virtual iText.Kernel.Pdf.MemoryLimitsAwareHandler ConsiderBytesOccupiedByDecompressedPdfStream(long
numOfOccupiedBytes) {
if (considerCurrentPdfStream && memoryUsedForCurrentPdfStreamDecompression < numOfOccupiedBytes) {
memoryUsedForCurrentPdfStreamDecompression = numOfOccupiedBytes;
if (memoryUsedForCurrentPdfStreamDecompression > maxSizeOfSingleDecompressedPdfStream) {
throw new MemoryLimitsAwareException(PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed
);
}
}
return this;
}

/// <summary>Begins handling of current pdf stream decompression.</summary>
/// <returns>
/// this
/// <see cref="MemoryLimitsAwareHandler"/>
/// instance.
/// </returns>
internal virtual iText.Kernel.Pdf.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 virtual iText.Kernel.Pdf.MemoryLimitsAwareHandler EndDecompressedPdfStreamProcessing() {
allMemoryUsedForDecompression += memoryUsedForCurrentPdfStreamDecompression;
if (allMemoryUsedForDecompression > maxSizeOfDecompressedPdfStreamsSum) {
throw new MemoryLimitsAwareException(PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed
);
}
EnsureCurrentStreamIsReset();
considerCurrentPdfStream = false;
return this;
}

internal virtual 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;
}
}
}
131 changes: 131 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/MemoryLimitsAwareOutputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
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;
using iText.IO.Util;
using iText.Kernel;

namespace iText.Kernel.Pdf {
/// <summary>This class implements an output stream which can be used for memory limits aware decompression of pdf streams.
/// </summary>
internal class MemoryLimitsAwareOutputStream : MemoryStream {
/// <summary>The maximum size of array to allocate.</summary>
/// <remarks>
/// The maximum size of array to allocate.
/// Attempts to allocate larger arrays will result in an exception.
/// </remarks>
private const int DEFAULT_MAX_STREAM_SIZE = int.MaxValue - 8;

/// <summary>The maximum size of array to allocate.</summary>
/// <remarks>
/// The maximum size of array to allocate.
/// Attempts to allocate larger arrays will result in an exception.
/// </remarks>
private int maxStreamSize = DEFAULT_MAX_STREAM_SIZE;

/// <summary>Creates a new byte array output stream.</summary>
/// <remarks>
/// Creates a new byte array output stream. The buffer capacity is
/// initially 32 bytes, though its size increases if necessary.
/// </remarks>
public MemoryLimitsAwareOutputStream()
: base() {
}

/// <summary>
/// Creates a new byte array output stream, with a buffer capacity of
/// the specified size, in bytes.
/// </summary>
/// <param name="size">the initial size.</param>
/// <exception cref="System.ArgumentException">if size is negative.</exception>
public MemoryLimitsAwareOutputStream(int size)
: base(size) {
}

/// <summary>Gets the maximum size which can be occupied by this output stream.</summary>
/// <returns>the maximum size which can be occupied by this output stream.</returns>
public virtual long GetMaxStreamSize() {
return maxStreamSize;
}

/// <summary>Sets the maximum size which can be occupied by this output stream.</summary>
/// <param name="maxStreamSize">the maximum size which can be occupied by this output stream.</param>
/// <returns>
/// this
/// <see cref="MemoryLimitsAwareOutputStream"/>
/// </returns>
public virtual iText.Kernel.Pdf.MemoryLimitsAwareOutputStream SetMaxStreamSize(int maxStreamSize) {
this.maxStreamSize = maxStreamSize;
return this;
}

/// <summary><inheritDoc/></summary>
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(PdfException.DuringDecompressionSingleStreamOccupiedMoreThanMaxIntegerValue
);
}
if (minCapacity > maxStreamSize) {
throw new MemoryLimitsAwareException(PdfException.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);
}
}
}
7 changes: 7 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public class PdfDocument : IEventDispatcher, IDisposable {
internal IDictionary<PdfIndirectReference, byte[]> serializedObjectsCache = new Dictionary<PdfIndirectReference
, byte[]>();

/// <summary>Handler which will be used for decompression of pdf streams.</summary>
internal MemoryLimitsAwareHandler memoryLimitsAwareHandler = null;

/// <summary>Open PDF document in reading mode.</summary>
/// <param name="reader">PDF reader.</param>
public PdfDocument(PdfReader reader)
Expand Down Expand Up @@ -1847,6 +1850,10 @@ public PdfDocument(PdfReader reader, PdfWriter writer)
throw new PdfException(PdfException.PdfReaderHasBeenAlreadyUtilized);
}
reader.pdfDocument = this;
memoryLimitsAwareHandler = reader.properties.memoryLimitsAwareHandler;
if (null == memoryLimitsAwareHandler) {
memoryLimitsAwareHandler = new MemoryLimitsAwareHandler(reader.tokens.GetSafeFile().Length());
}
reader.ReadPdf();
foreach (ICounter counter in GetCounters()) {
counter.OnDocumentRead(reader.GetFileLength());
Expand Down
9 changes: 7 additions & 2 deletions itext/itext.kernel/itext/kernel/pdf/PdfPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ source product.
*/
using System;
using System.Collections.Generic;
using System.IO;
using Common.Logging;
using iText.IO.Util;
using iText.Kernel;
Expand Down Expand Up @@ -916,11 +915,17 @@ protected internal PdfPage(PdfDocument pdfDocument)
/// </exception>
public virtual byte[] GetContentBytes() {
try {
MemoryStream baos = new MemoryStream();
MemoryLimitsAwareHandler handler = GetDocument().memoryLimitsAwareHandler;
long usedMemory = null == handler ? -1 : handler.GetAllMemoryUsedForDecompression();
MemoryLimitsAwareOutputStream baos = new MemoryLimitsAwareOutputStream();
int streamCount = GetContentStreamCount();
byte[] streamBytes;
for (int i = 0; i < streamCount; i++) {
streamBytes = GetStreamBytes(i);
// usedMemory has changed, that means that some of currently processed pdf streams are suspicious
if (null != handler && usedMemory < handler.GetAllMemoryUsedForDecompression()) {
baos.SetMaxStreamSize(handler.GetMaxSizeOfSingleDecompressedPdfStream());
}
baos.Write(streamBytes);
if (0 != streamBytes.Length && !iText.IO.Util.TextUtil.IsWhiteSpace((char)streamBytes[streamBytes.Length -
1])) {
Expand Down
25 changes: 25 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/PdfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,25 @@ public PdfReader(String filename)
}
}
}
MemoryLimitsAwareHandler memoryLimitsAwareHandler = null;
if (null != streamDictionary.GetIndirectReference()) {
memoryLimitsAwareHandler = streamDictionary.GetIndirectReference().GetDocument().memoryLimitsAwareHandler;
}
if (null != memoryLimitsAwareHandler) {
HashSet<PdfName> filterSet = new HashSet<PdfName>();
int index;
for (index = 0; index < filters.Size(); index++) {
PdfName filterName = filters.GetAsName(index);
if (!filterSet.Add(filterName)) {
memoryLimitsAwareHandler.BeginDecompressedPdfStreamProcessing();
break;
}
}
if (index == filters.Size()) {
// The stream isn't suspicious. We shouldn't process it.
memoryLimitsAwareHandler = null;
}
}
PdfArray dp = new PdfArray();
PdfObject dpo = streamDictionary.Get(PdfName.DecodeParms);
if (dpo == null || (dpo.GetObjectType() != PdfObject.DICTIONARY && dpo.GetObjectType() != PdfObject.ARRAY)
Expand Down Expand Up @@ -432,6 +451,12 @@ public PdfReader(String filename)
decodeParams = null;
}
b = filterHandler.Decode(b, filterName, decodeParams, streamDictionary);
if (null != memoryLimitsAwareHandler) {
memoryLimitsAwareHandler.ConsiderBytesOccupiedByDecompressedPdfStream(b.Length);
}
}
if (null != memoryLimitsAwareHandler) {
memoryLimitsAwareHandler.EndDecompressedPdfStreamProcessing();
}
return b;
}
Expand Down
16 changes: 16 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/ReaderProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class ReaderProperties {

protected internal X509Certificate certificate;

protected internal MemoryLimitsAwareHandler memoryLimitsAwareHandler;

//added by ujihara for decryption
//added by Aiken Sam for certificate decryption
//added by Aiken Sam for certificate decryption
Expand Down Expand Up @@ -91,5 +93,19 @@ public class ReaderProperties {
this.certificate = null;
this.certificateKey = null;
}

/// <summary>Sets the memory handler which will be used to handle decompressed pdf streams.</summary>
/// <param name="memoryLimitsAwareHandler">the memory handler which will be used to handle decompressed pdf streams
/// </param>
/// <returns>
/// this
/// <see cref="ReaderProperties"/>
/// instance.
/// </returns>
public virtual ReaderProperties SetMemoryLimitsAwareHandler(MemoryLimitsAwareHandler memoryLimitsAwareHandler
) {
this.memoryLimitsAwareHandler = memoryLimitsAwareHandler;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public InlineImageParseException(String message)
filters.Put(PdfName.DCTDecode, stubfilter);
filters.Put(PdfName.JBIG2Decode, stubfilter);
filters.Put(PdfName.JPXDecode, stubfilter);
((FlateDecodeFilter)filters.Get(PdfName.FlateDecode)).SetStrictDecoding(true);
filters.Put(PdfName.FlateDecode, new FlateDecodeStrictFilter());
PdfReader.DecodeBytes(samples, imageDictionary, filters);
}
catch (Exception) {
Expand Down
17 changes: 13 additions & 4 deletions itext/itext.kernel/itext/kernel/pdf/filters/ASCII85DecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,27 @@ source product.

namespace iText.Kernel.Pdf.Filters {
/// <summary>Handles ASCII85Decode filter</summary>
public class ASCII85DecodeFilter : IFilterHandler {
public virtual byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
public class ASCII85DecodeFilter : MemoryLimitsAwareFilter {
/// <summary><inheritDoc/></summary>
public override byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
) {
b = ASCII85Decode(b);
MemoryStream outputStream = EnableMemoryLimitsAwareHandler(streamDictionary);
b = ASCII85Decode(b, outputStream);
return b;
}

/// <summary>Decodes the input bytes according to ASCII85.</summary>
/// <param name="in">the byte[] to be decoded</param>
/// <returns>the decoded byte[]</returns>
public static byte[] ASCII85Decode(byte[] @in) {
MemoryStream @out = new MemoryStream();
return ASCII85Decode(@in, new MemoryStream());
}

/// <summary>Decodes the input bytes according to ASCII85.</summary>
/// <param name="in">the byte[] to be decoded</param>
/// <param name="out">the out stream which will be used to write the bytes.</param>
/// <returns>the decoded byte[]</returns>
private static byte[] ASCII85Decode(byte[] @in, MemoryStream @out) {
int state = 0;
int[] chn = new int[5];
for (int k = 0; k < @in.Length; ++k) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,27 @@ source product.

namespace iText.Kernel.Pdf.Filters {
/// <summary>Handles ASCIIHexDecode filter</summary>
public class ASCIIHexDecodeFilter : IFilterHandler {
public virtual byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
public class ASCIIHexDecodeFilter : MemoryLimitsAwareFilter {
/// <summary><inheritDoc/></summary>
public override byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
) {
b = ASCIIHexDecode(b);
MemoryStream outputStream = EnableMemoryLimitsAwareHandler(streamDictionary);
b = ASCIIHexDecode(b, outputStream);
return b;
}

/// <summary>Decodes a byte[] according to ASCII Hex encoding.</summary>
/// <param name="in">byte[] to be decoded</param>
/// <returns>decoded byte[]</returns>
public static byte[] ASCIIHexDecode(byte[] @in) {
MemoryStream @out = new MemoryStream();
return ASCIIHexDecode(@in, new MemoryStream());
}

/// <summary>Decodes a byte[] according to ASCII Hex encoding.</summary>
/// <param name="in">byte[] to be decoded</param>
/// <param name="out">the out stream which will be used to write the bytes.</param>
/// <returns>decoded byte[]</returns>
private static byte[] ASCIIHexDecode(byte[] @in, MemoryStream @out) {
bool first = true;
int n1 = 0;
for (int k = 0; k < @in.Length; ++k) {
Expand Down
40 changes: 32 additions & 8 deletions itext/itext.kernel/itext/kernel/pdf/filters/FlateDecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,52 @@ source product.

namespace iText.Kernel.Pdf.Filters {
/// <summary>Handles FlateDecode filter.</summary>
public class FlateDecodeFilter : IFilterHandler {
/// <summary>Defines how the corrupted streams should be treated.</summary>
private bool strictDecoding = false;

public class FlateDecodeFilter : MemoryLimitsAwareFilter {
/// <summary>Creates a FlateDecodeFilter.</summary>
public FlateDecodeFilter()
: this(false) {
}

/// <summary>Creates a FlateDecodeFilter.</summary>
/// <param name="strictDecoding">defines whether the decoder will try to read a corrupted stream</param>
[System.ObsoleteAttribute(@"will be removed in 7.2, use FlateDecodeStrictFilter instead.")]
public FlateDecodeFilter(bool strictDecoding) {
this.strictDecoding = strictDecoding;
}

/// <summary>Checks whether the decoder will try to read a corrupted stream (not strict) or not (strict)</summary>
/// <returns>true if the decoder will try to read a corrupted stream otherwise false</returns>
[System.ObsoleteAttribute(@"will be removed in 7.2, use FlateDecodeStrictFilter instead.")]
public virtual bool IsStrictDecoding() {
return strictDecoding;
}

/// <summary>Defines how the corrupted streams should be treated.</summary>
/// <param name="strict">true if the decoder should try to read a corrupted stream otherwise false</param>
/// <returns>the decoder</returns>
[System.ObsoleteAttribute(@"will be removed in 7.2, use FlateDecodeStrictFilter instead.")]
public virtual iText.Kernel.Pdf.Filters.FlateDecodeFilter SetStrictDecoding(bool strict) {
this.strictDecoding = strict;
return this;
}

public virtual byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
/// <summary><inheritDoc/></summary>
public override byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
) {
byte[] res = FlateDecode(b, true);
MemoryStream outputStream = EnableMemoryLimitsAwareHandler(streamDictionary);
byte[] res = FlateDecode(b, true, outputStream);
if (res == null && !strictDecoding) {
res = FlateDecode(b, false);
outputStream.JReset();
res = FlateDecode(b, false, outputStream);
}
b = DecodePredictor(res, decodeParams);
return b;
}

/// <summary>Defines how the corrupted streams should be treated.</summary>
[System.ObsoleteAttribute(@"will be removed in 7.2, use FlateDecodeStrictFilter instead.")]
private bool strictDecoding = false;

/// <summary>A helper to flateDecode.</summary>
/// <param name="in">the input data</param>
/// <param name="strict">
Expand All @@ -99,9 +106,23 @@ public FlateDecodeFilter()
/// </param>
/// <returns>the decoded data</returns>
public static byte[] FlateDecode(byte[] @in, bool strict) {
return FlateDecode(@in, strict, new MemoryStream());
}

/// <summary>A helper to flateDecode.</summary>
/// <param name="in">the input data</param>
/// <param name="strict">
///
/// <see langword="true"/>
/// to read a correct stream.
/// <see langword="false"/>
/// to try to read a corrupted stream.
/// </param>
/// <param name="out">the out stream which will be used to write the bytes.</param>
/// <returns>the decoded data</returns>
private static byte[] FlateDecode(byte[] @in, bool strict, MemoryStream @out) {
MemoryStream stream = new MemoryStream(@in);
ZInflaterInputStream zip = new ZInflaterInputStream(stream);
MemoryStream @out = new MemoryStream();
byte[] b = new byte[strict ? 4092 : 1];
try {
int n;
Expand All @@ -112,6 +133,9 @@ public FlateDecodeFilter()
@out.Dispose();
return @out.ToArray();
}
catch (MemoryLimitsAwareException e) {
throw;
}
catch (Exception) {
if (strict) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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;
using System.util.zlib;
using iText.Kernel.Pdf;

namespace iText.Kernel.Pdf.Filters {
/// <summary>Handles strict FlateDecode filter.</summary>
public class FlateDecodeStrictFilter : FlateDecodeFilter {
/// <summary><inheritDoc/></summary>
public override byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
) {
MemoryStream outputStream = EnableMemoryLimitsAwareHandler(streamDictionary);
byte[] res = FlateDecode(b, outputStream);
b = DecodePredictor(res, decodeParams);
return b;
}

/// <summary>A helper to flateDecode.</summary>
/// <param name="in">the input data</param>
/// <param name="out">the out stream which will be used to write the bytes.</param>
/// <returns>the decoded data</returns>
private static byte[] FlateDecode(byte[] @in, MemoryStream @out) {
MemoryStream stream = new MemoryStream(@in);
ZInflaterInputStream zip = new ZInflaterInputStream(stream);
byte[] b = new byte[4092];
try {
int n;
while ((n = zip.Read(b)) >= 0) {
@out.Write(b, 0, n);
}
zip.Dispose();
@out.Dispose();
return @out.ToArray();
}
catch (MemoryLimitsAwareException e) {
throw;
}
catch (Exception) {
return null;
}
}
}
}
17 changes: 13 additions & 4 deletions itext/itext.kernel/itext/kernel/pdf/filters/LZWDecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ source product.

namespace iText.Kernel.Pdf.Filters {
/// <summary>Handles LZWDECODE filter</summary>
public class LZWDecodeFilter : IFilterHandler {
public virtual byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
public class LZWDecodeFilter : MemoryLimitsAwareFilter {
/// <summary><inheritDoc/></summary>
public override byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
) {
b = LZWDecode(b);
MemoryStream outputStream = EnableMemoryLimitsAwareHandler(streamDictionary);
b = LZWDecode(b, outputStream);
b = FlateDecodeFilter.DecodePredictor(b, decodeParams);
return b;
}
Expand All @@ -58,7 +60,14 @@ public class LZWDecodeFilter : IFilterHandler {
/// <param name="in">byte[] to be decoded</param>
/// <returns>decoded byte[]</returns>
public static byte[] LZWDecode(byte[] @in) {
MemoryStream @out = new MemoryStream();
return LZWDecode(@in, new MemoryStream());
}

/// <summary>Decodes a byte[] according to the LZW encoding.</summary>
/// <param name="in">byte[] to be decoded</param>
/// <param name="out">the out stream which will be used to write the bytes.</param>
/// <returns>decoded byte[]</returns>
private static byte[] LZWDecode(byte[] @in, MemoryStream @out) {
LZWDecoder lzw = new LZWDecoder();
lzw.Decode(@in, @out);
return @out.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ source product.

namespace iText.Kernel.Pdf.Filters {
/// <summary>Handles RunLengthDecode filter.</summary>
public class RunLengthDecodeFilter : IFilterHandler {
public virtual byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
public class RunLengthDecodeFilter : MemoryLimitsAwareFilter {
/// <summary><inheritDoc/></summary>
public override byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary
) {
MemoryStream baos = new MemoryStream();
MemoryStream outputStream = EnableMemoryLimitsAwareHandler(streamDictionary);
byte dupCount;
for (int i = 0; i < b.Length; i++) {
dupCount = b[i];
Expand All @@ -59,18 +60,18 @@ public class RunLengthDecodeFilter : IFilterHandler {
}
if ((dupCount & 0x80) == 0) {
int bytesToCopy = dupCount + 1;
baos.Write(b, i + 1, bytesToCopy);
outputStream.Write(b, i + 1, bytesToCopy);
i += bytesToCopy;
}
else {
// make dupcount copies of the next byte
i++;
for (int j = 0; j < 257 - (dupCount & 0xff); j++) {
baos.Write(b[i]);
outputStream.Write(b[i]);
}
}
}
return baos.ToArray();
return outputStream.ToArray();
}
}
}
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7d8ade0ec9efc4be07be282e0204c209952423d3
696df736f8c4c6385dae5c4eff22163d5eca29b6