Esempio n. 1
0
        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);
        }
        public virtual void TestJPXDecodeFilter()
        {
            PdfStream pdfStream = new PdfStream(FLATE_DECODED_BYTES);

            pdfStream.Put(PdfName.Filter, new PdfArray(JavaUtil.ArraysAsList((PdfObject)PdfName.FlateDecode, (PdfObject
                                                                                                              )PdfName.JPXDecode)));
            NUnit.Framework.Assert.AreEqual(BYTES, pdfStream.GetBytes());
        }
        public virtual void TestJBIG2DecodeFilter()
        {
            PdfStream pdfStream = new PdfStream(FLATE_DECODED_BYTES);

            pdfStream.Put(PdfName.Filter, new PdfArray(JavaUtil.ArraysAsList((PdfObject)PdfName.FlateDecode, (PdfObject
                                                                                                              )PdfName.JBIG2Decode)));
            NUnit.Framework.Assert.That(() => {
                pdfStream.GetBytes(true);
            }
                                        , NUnit.Framework.Throws.InstanceOf <PdfException>().With.Message.EqualTo(MessageFormatUtil.Format(PdfException.Filter1IsNotSupported, PdfName.JBIG2Decode)))
            ;
        }
        public virtual void CustomMemoryHandlerSumTest()
        {
            MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();

            handler.SetMaxSizeOfDecompressedPdfStreamsSum(100000);
            NUnit.Framework.Assert.That(() => {
                using (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);
                    PdfReader.DecodeBytes(b, stream);
                }
            }
                                        , NUnit.Framework.Throws.InstanceOf <MemoryLimitsAwareException>().With.Message.EqualTo(PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed))
            ;
        }
 public virtual void DefaultMemoryHandlerTest()
 {
     using (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);
     }
 }
        public virtual void OverriddenMemoryHandlerNoStreamsAreSuspiciousTest()
        {
            MemoryLimitsAwareHandler handler = new _MemoryLimitsAwareHandler_235();

            handler.SetMaxSizeOfSingleDecompressedPdfStream(20);
            using (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);
                array.Add(PdfName.Fl);
                array.Add(PdfName.Fl);
                // Limit is reached but the stream with several copies of the filter is not considered
                // to be suspicious
                PdfReader.DecodeBytes(b, stream);
            }
        }
        public virtual void OneFilterCustomMemoryHandlerSingleTest()
        {
            MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();

            handler.SetMaxSizeOfSingleDecompressedPdfStream(20);
            using (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);
            }
        }
Esempio n. 8
0
        public virtual void CopyObject4()
        {
            FileStream  fos1    = new FileStream(destinationFolder + "copyObject4_1.pdf", FileMode.Create);
            PdfWriter   writer1 = new PdfWriter(fos1);
            PdfDocument pdfDoc1 = new PdfDocument(writer1);
            PdfPage     page1   = pdfDoc1.AddNewPage();

            page1.Flush();
            PdfDictionary    catalog1 = pdfDoc1.GetCatalog().GetPdfObject();
            PdfStream        stream1  = ((PdfStream) new PdfStream().MakeIndirect(pdfDoc1));
            List <PdfObject> tmpArray = new List <PdfObject>(3);

            tmpArray.Add(new PdfNumber(1));
            tmpArray.Add(new PdfNumber(2));
            tmpArray.Add(new PdfNumber(3));
            stream1.GetOutputStream().Write(new PdfArray(tmpArray));
            catalog1.Put(new PdfName("stream"), stream1);
            pdfDoc1.Close();
            PdfDocument pdfDoc1R = new PdfDocument(new PdfReader(destinationFolder + "copyObject4_1.pdf"));

            stream1 = (PdfStream)pdfDoc1R.GetCatalog().GetPdfObject().Get(new PdfName("stream"));
            FileStream  fos2    = new FileStream(destinationFolder + "copyObject4_2.pdf", FileMode.Create);
            PdfWriter   writer2 = new PdfWriter(fos2);
            PdfDocument pdfDoc2 = new PdfDocument(writer2);
            PdfPage     page2   = pdfDoc2.AddNewPage();

            page2.Flush();
            PdfDictionary catalog2 = pdfDoc2.GetCatalog().GetPdfObject();

            catalog2.Put(new PdfName("stream"), ((PdfStream)stream1.CopyTo(pdfDoc2)));
            pdfDoc1R.Close();
            pdfDoc2.Close();
            PdfReader   reader      = new PdfReader(destinationFolder + "copyObject4_2.pdf");
            PdfDocument pdfDocument = new PdfDocument(reader);

            NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt");
            PdfDictionary catalog = pdfDocument.GetCatalog().GetPdfObject();
            PdfStream     stream  = (PdfStream)catalog.GetAsStream(new PdfName("stream"));

            byte[] bytes = stream.GetBytes();
            NUnit.Framework.Assert.AreEqual(ByteUtils.GetIsoBytes("[1 2 3]"), bytes);
            reader.Close();
        }
 public virtual void OverriddenMemoryHandlerAllStreamsAreSuspiciousTest()
 {
     NUnit.Framework.Assert.That(() => {
         MemoryLimitsAwareHandler handler = new _MemoryLimitsAwareHandler_203();
         handler.SetMaxSizeOfSingleDecompressedPdfStream(20);
         using (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);
             array.Add(PdfName.Fl);
             // Limit is reached, and the stream with one filter is considered to be suspicious
             PdfReader.DecodeBytes(b, stream);
         }
     }
                                 , NUnit.Framework.Throws.InstanceOf <MemoryLimitsAwareException>().With.Message.EqualTo(PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed))
     ;
 }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        public virtual void CreatePdfStreamByInputStream()
        {
            String      filename = destinationFolder + "createPdfStreamByInputStream.pdf";
            FileStream  fos      = new FileStream(filename, FileMode.Create);
            PdfWriter   writer   = new PdfWriter(fos);
            PdfDocument document = new PdfDocument(writer);

            document.GetDocumentInfo().SetAuthor("Alexander Chingarev").SetCreator("iText 6").SetTitle("Empty iText 6 Document"
                                                                                                       );
            PdfPage page = document.AddNewPage();

            page.Flush();
            String    streamContent = "Some text content with strange symbols ∞²";
            PdfStream stream        = new PdfStream(document, new MemoryStream(streamContent.GetBytes()));

            stream.Flush();
            int streamIndirectNumber = stream.GetIndirectReference().GetObjNumber();

            document.Close();
            //        com.itextpdf.text.pdf.PdfReader reader = new PdfReader(filename);
            //        Assert.assertEquals("Rebuilt", false, reader.isRebuilt());
            //        Assert.assertNotNull(reader.getPageN(1));
            //        String date = reader.getDocumentInfo().get("CreationDate");
            //        Calendar cl = com.itextpdf.text.pdf.PdfDate.decode(date);
            //        long diff = new GregorianCalendar().getTimeInMillis() - cl.getTimeInMillis();
            //        String message = "Unexpected creation date. Different from now is " + (float)diff/1000 + "s";
            //        Assert.assertTrue(message, diff < 5000);
            //        reader.close();
            PdfReader reader6 = new PdfReader(filename);

            document = new PdfDocument(reader6);
            NUnit.Framework.Assert.AreEqual(false, reader6.HasRebuiltXref(), "Rebuilt");
            NUnit.Framework.Assert.AreEqual(false, reader6.HasFixedXref(), "Fixed");
            PdfStream pdfStream = (PdfStream)document.GetXref().Get(streamIndirectNumber).GetRefersTo();

            NUnit.Framework.Assert.AreEqual(streamContent.GetBytes(), pdfStream.GetBytes(), "Stream by InputStream");
            document.Close();
        }
        public virtual void CustomMemoryHandlerSingleTest()
        {
            MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();

            handler.SetMaxSizeOfSingleDecompressedPdfStream(1000);
            NUnit.Framework.Assert.That(() => {
                using (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);
                    PdfReader.DecodeBytes(b, stream);
                }
            }
                                        , NUnit.Framework.Throws.InstanceOf <MemoryLimitsAwareException>().With.Message.EqualTo(PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed))
            ;
        }