Skip to content

Commit 5ac1031

Browse files
IdamkinIitext-teamcity
authored andcommitted
Various fixes for unencrypted wrapper API
-Refactor exceptions of added wrappers. -Revert breaking changes -Add wrapper for encrypted payload document -Add new tests -Improve testing checks -Add some shorthand methods -Remove unneeded TODOs DEVSIX-1213 Autoported commit. Original commit hash: [dd3ec4a32]
1 parent d2580ee commit 5ac1031

File tree

10 files changed

+144
-37
lines changed

10 files changed

+144
-37
lines changed

itext.tests/itext.kernel.tests/itext/kernel/crypto/UnencryptedWrapperTest.cs

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using iText.IO.Source;
34
using iText.Kernel.Font;
45
using iText.Kernel.Pdf;
56
using iText.Kernel.Pdf.Canvas;
@@ -24,13 +25,41 @@ public static void BeforeClass() {
2425
/// <exception cref="System.Exception"/>
2526
[NUnit.Framework.Test]
2627
public virtual void CreateSimpleWrapperDocumentTest() {
27-
String inPath = sourceFolder + "cmp_customEncryptedDocument.pdf";
28-
String cmpPath = sourceFolder + "cmp_simpleUnencryptedWrapper.pdf";
29-
String outPath = destinationFolder + "simpleUnencryptedWrapper.pdf";
30-
String diff = "diff_simpleUnencryptedWrapper.pdf_";
28+
CreateWrapper("customEncryptedDocument.pdf", "simpleUnencryptedWrapper.pdf", "iText");
29+
}
30+
31+
/// <exception cref="System.IO.IOException"/>
32+
/// <exception cref="System.Exception"/>
33+
[NUnit.Framework.Test]
34+
public virtual void ExtractCustomEncryptedDocumentTest() {
35+
ExtractEncrypted("customEncryptedDocument.pdf", "simpleUnencryptedWrapper.pdf", null);
36+
}
37+
38+
/// <exception cref="System.IO.IOException"/>
39+
/// <exception cref="System.Exception"/>
40+
[NUnit.Framework.Test]
41+
public virtual void CreateWrapperForStandardEncryptedTest() {
42+
CreateWrapper("standardEncryptedDocument.pdf", "standardUnencryptedWrapper.pdf", "Standard");
43+
}
44+
45+
/// <exception cref="System.IO.IOException"/>
46+
/// <exception cref="System.Exception"/>
47+
[NUnit.Framework.Test]
48+
public virtual void ExtractStandardEncryptedDocumentTest() {
49+
ExtractEncrypted("standardEncryptedDocument.pdf", "standardUnencryptedWrapper.pdf", "World".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1
50+
));
51+
}
52+
53+
/// <exception cref="System.IO.IOException"/>
54+
/// <exception cref="System.Exception"/>
55+
private void CreateWrapper(String encryptedName, String wrapperName, String cryptoFilter) {
56+
String inPath = sourceFolder + "cmp_" + encryptedName;
57+
String cmpPath = sourceFolder + "cmp_" + wrapperName;
58+
String outPath = destinationFolder + wrapperName;
59+
String diff = "diff_" + wrapperName + "_";
3160
PdfDocument document = new PdfDocument(new PdfWriter(outPath, new WriterProperties().SetPdfVersion(PdfVersion
3261
.PDF_2_0)));
33-
PdfFileSpec fs = PdfEncryptedPayloadFileSpecFactory.Create(document, inPath, new PdfEncryptedPayload("iText"
62+
PdfFileSpec fs = PdfEncryptedPayloadFileSpecFactory.Create(document, inPath, new PdfEncryptedPayload(cryptoFilter
3463
));
3564
document.SetEncryptedPayload(fs);
3665
PdfFont font = PdfFontFactory.CreateFont();
@@ -45,20 +74,33 @@ public virtual void CreateSimpleWrapperDocumentTest() {
4574

4675
/// <exception cref="System.IO.IOException"/>
4776
/// <exception cref="System.Exception"/>
48-
[NUnit.Framework.Test]
49-
public virtual void ExtractCustomEncryptedDocumentTest() {
50-
String inPath = sourceFolder + "cmp_simpleUnencryptedWrapper.pdf";
51-
String cmpPath = sourceFolder + "cmp_customEncryptedDocument.pdf";
52-
String outPath = destinationFolder + "customEncryptedDocument.pdf";
77+
private void ExtractEncrypted(String encryptedName, String wrapperName, byte[] password) {
78+
String inPath = sourceFolder + "cmp_" + wrapperName;
79+
String cmpPath = sourceFolder + "cmp_" + encryptedName;
80+
String outPath = destinationFolder + encryptedName;
81+
String diff = "diff_" + encryptedName + "_";
5382
PdfDocument document = new PdfDocument(new PdfReader(inPath));
54-
PdfStream stream = document.GetEncryptedPayloadAsStream();
55-
byte[] encryptedDocumentBytes = stream.GetBytes();
83+
PdfEncryptedPayloadDocument encryptedDocument = document.GetEncryptedPayloadDocument();
84+
byte[] encryptedDocumentBytes = encryptedDocument.GetDocumentBytes();
5685
FileStream fos = new FileStream(outPath, FileMode.Create);
5786
fos.Write(encryptedDocumentBytes);
5887
fos.Dispose();
5988
document.Close();
60-
//TODO: check files by bytes
61-
NUnit.Framework.Assert.IsNotNull(encryptedDocumentBytes);
89+
PdfEncryptedPayload ep = encryptedDocument.GetEncryptedPayload();
90+
NUnit.Framework.Assert.AreEqual(PdfEncryptedPayloadFileSpecFactory.GenerateFileDisplay(ep), encryptedDocument
91+
.GetName());
92+
if (password != null) {
93+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff
94+
, password, password));
95+
}
96+
else {
97+
RandomAccessFileOrArray raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateBestSource
98+
(cmpPath));
99+
byte[] cmpBytes = new byte[(int)raf.Length()];
100+
raf.ReadFully(cmpBytes);
101+
raf.Close();
102+
NUnit.Framework.Assert.AreEqual(cmpBytes, encryptedDocumentBytes);
103+
}
62104
}
63105
}
64106
}

itext/itext.kernel/itext/kernel/PdfException.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,18 @@ public class PdfException : Exception {
220220

221221
public const String ErrorWhileReadingObjectStream = "Error while reading Object Stream.";
222222

223-
public const String EncryptedPayloadFileSpecDoesntHaveCorrectEncryptedPayloadDictionary = "Encrypted payload file spec shall have encrypted payload dictionary with 'Subtype' specifying crypto filter and with 'Type' equal to 'EncryptedPayload' if present";
223+
public const String EncryptedPayloadFileSpecDoesntHaveEncryptedPayloadDictionary = "Encrypted payload file spec shall have encrypted payload dictionary.";
224224

225225
public const String EncryptedPayloadFileSpecShallBeIndirect = "Encrypted payload file spec shall be indirect.";
226226

227227
public const String EncryptedPayloadFileSpecShallHaveEFDictionary = "Encrypted payload file spec shall have 'EF' key. The value of such key shall be a dictionary that contains embedded file stream.";
228228

229229
public const String EncryptedPayloadFileSpecShallHaveTypeEqualToFilespec = "Encrypted payload file spec shall have 'Type' key. The value of such key shall be 'Filespec'.";
230230

231+
public const String EncryptedPayloadShallHaveTypeEqualsToEncryptedPayloadIfPresent = "Encrypted payload dictionary shall have field 'Type' equal to 'EncryptedPayload' if present";
232+
233+
public const String EncryptedPayloadShallHaveSubtype = "Encrypted payload shall have 'Subtype' field specifying crypto filter";
234+
231235
public const String FailedToGetTsaResponseFrom1 = "Failed to get TSA response from {0}.";
232236

233237
public const String FieldFlatteningIsNotSupportedInAppendMode = "Field flattening is not supported in append mode.";

itext/itext.kernel/itext/kernel/pdf/PdfDocument.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,21 +1439,32 @@ public virtual PdfArray GetAssociatedFiles() {
14391439
/// if this document isn't an unencrypted wrapper document.
14401440
/// </summary>
14411441
/// <returns>encrypted payload of this document.</returns>
1442-
public virtual PdfStream GetEncryptedPayloadAsStream() {
1442+
public virtual PdfEncryptedPayloadDocument GetEncryptedPayloadDocument() {
14431443
if (GetReader() != null && GetReader().IsEncrypted()) {
14441444
return null;
14451445
}
14461446
PdfCollection collection = GetCatalog().GetCollection();
1447-
if (collection != null && PdfName.H.Equals(collection.GetView())) {
1447+
if (collection != null && collection.IsViewHidden()) {
14481448
PdfString documentName = collection.GetInitialDocument();
14491449
PdfNameTree embeddedFiles = GetCatalog().GetNameTree(PdfName.EmbeddedFiles);
1450-
PdfObject fileSpecObject = embeddedFiles.GetNames().Get(documentName.ToUnicodeString());
1450+
String documentNameUnicode = documentName.ToUnicodeString();
1451+
PdfObject fileSpecObject = embeddedFiles.GetNames().Get(documentNameUnicode);
14511452
if (fileSpecObject != null && fileSpecObject.IsDictionary()) {
1452-
PdfFileSpec fileSpec = PdfEncryptedPayloadFileSpecFactory.Wrap((PdfDictionary)fileSpecObject);
1453-
if (fileSpec != null) {
1454-
PdfDictionary embeddedDictionary = ((PdfDictionary)fileSpec.GetPdfObject()).GetAsDictionary(PdfName.EF);
1455-
PdfStream uf = embeddedDictionary.GetAsStream(PdfName.UF);
1456-
return uf != null ? uf : embeddedDictionary.GetAsStream(PdfName.F);
1453+
try {
1454+
PdfFileSpec fileSpec = PdfEncryptedPayloadFileSpecFactory.Wrap((PdfDictionary)fileSpecObject);
1455+
if (fileSpec != null) {
1456+
PdfDictionary embeddedDictionary = ((PdfDictionary)fileSpec.GetPdfObject()).GetAsDictionary(PdfName.EF);
1457+
PdfStream stream = embeddedDictionary.GetAsStream(PdfName.UF);
1458+
if (stream == null) {
1459+
stream = embeddedDictionary.GetAsStream(PdfName.F);
1460+
}
1461+
if (stream != null) {
1462+
return new PdfEncryptedPayloadDocument(stream, fileSpec, documentNameUnicode);
1463+
}
1464+
}
1465+
}
1466+
catch (PdfException e) {
1467+
LogManager.GetLogger(GetType()).Error(e.Message);
14571468
}
14581469
}
14591470
}
@@ -1484,7 +1495,7 @@ public virtual void SetEncryptedPayload(PdfFileSpec fs) {
14841495
}
14851496
PdfEncryptedPayload encryptedPayload = PdfEncryptedPayload.ExtractFrom(fs);
14861497
if (encryptedPayload == null) {
1487-
throw new PdfException(PdfException.EncryptedPayloadFileSpecDoesntHaveCorrectEncryptedPayloadDictionary);
1498+
throw new PdfException(PdfException.EncryptedPayloadFileSpecDoesntHaveEncryptedPayloadDictionary);
14881499
}
14891500
PdfCollection collection = GetCatalog().GetCollection();
14901501
if (collection != null) {

itext/itext.kernel/itext/kernel/pdf/PdfEncryptedPayload.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using iText.Kernel;
23
using iText.Kernel.Pdf.Filespec;
34

45
namespace iText.Kernel.Pdf {
@@ -14,7 +15,7 @@ private PdfEncryptedPayload(PdfDictionary pdfObject)
1415
}
1516

1617
public static iText.Kernel.Pdf.PdfEncryptedPayload ExtractFrom(PdfFileSpec fileSpec) {
17-
if (fileSpec.GetPdfObject().IsDictionary()) {
18+
if (fileSpec != null && fileSpec.GetPdfObject().IsDictionary()) {
1819
return iText.Kernel.Pdf.PdfEncryptedPayload.Wrap(((PdfDictionary)fileSpec.GetPdfObject()).GetAsDictionary(
1920
PdfName.EP));
2021
}
@@ -23,12 +24,13 @@ public static iText.Kernel.Pdf.PdfEncryptedPayload ExtractFrom(PdfFileSpec fileS
2324

2425
public static iText.Kernel.Pdf.PdfEncryptedPayload Wrap(PdfDictionary dictionary) {
2526
PdfName type = dictionary.GetAsName(PdfName.Type);
26-
if (type == null || type.Equals(PdfName.EncryptedPayload)) {
27-
if (dictionary.GetAsName(PdfName.Subtype) != null) {
28-
return new iText.Kernel.Pdf.PdfEncryptedPayload(dictionary);
29-
}
27+
if (type != null && !type.Equals(PdfName.EncryptedPayload)) {
28+
throw new PdfException(PdfException.EncryptedPayloadShallHaveTypeEqualsToEncryptedPayloadIfPresent);
3029
}
31-
return null;
30+
if (dictionary.GetAsName(PdfName.Subtype) == null) {
31+
throw new PdfException(PdfException.EncryptedPayloadShallHaveSubtype);
32+
}
33+
return new iText.Kernel.Pdf.PdfEncryptedPayload(dictionary);
3234
}
3335

3436
public virtual PdfName GetSubtype() {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using iText.Kernel.Pdf.Filespec;
3+
4+
namespace iText.Kernel.Pdf {
5+
public class PdfEncryptedPayloadDocument : PdfObjectWrapper<PdfStream> {
6+
private PdfFileSpec fileSpec;
7+
8+
private String name;
9+
10+
public PdfEncryptedPayloadDocument(PdfStream pdfObject, PdfFileSpec fileSpec, String name)
11+
: base(pdfObject) {
12+
this.fileSpec = fileSpec;
13+
this.name = name;
14+
}
15+
16+
public virtual byte[] GetDocumentBytes() {
17+
return GetPdfObject().GetBytes();
18+
}
19+
20+
public virtual PdfFileSpec GetFileSpec() {
21+
return fileSpec;
22+
}
23+
24+
public virtual String GetName() {
25+
return name;
26+
}
27+
28+
public virtual PdfEncryptedPayload GetEncryptedPayload() {
29+
return PdfEncryptedPayload.ExtractFrom(fileSpec);
30+
}
31+
32+
protected internal override bool IsWrappedObjectMustBeIndirect() {
33+
return true;
34+
}
35+
}
36+
}

itext/itext.kernel/itext/kernel/pdf/collection/PdfCollection.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,23 @@ public virtual iText.Kernel.Pdf.Collection.PdfCollection SetView(int viewType) {
111111
return this;
112112
}
113113

114-
//TODO is it ok to break backwards compatibility?
115-
public virtual PdfName GetView() {
116-
return GetPdfObject().GetAsName(PdfName.View);
114+
[System.ObsoleteAttribute(@"Will always return null. The return will be changed to PdfName in 7.2. Use getPdfObject().getAsName(PdfName.View) , or one of IsViewDetails() , IsViewTile() , IsViewHidden() ."
115+
)]
116+
public virtual PdfNumber GetView() {
117+
return GetPdfObject().GetAsNumber(PdfName.View);
118+
}
119+
120+
public virtual bool IsViewDetails() {
121+
PdfName view = GetPdfObject().GetAsName(PdfName.View);
122+
return view == null || view.Equals(PdfName.D);
123+
}
124+
125+
public virtual bool IsViewTile() {
126+
return PdfName.T.Equals(GetPdfObject().GetAsName(PdfName.View));
127+
}
128+
129+
public virtual bool IsViewHidden() {
130+
return PdfName.H.Equals(GetPdfObject().GetAsName(PdfName.View));
117131
}
118132

119133
/// <summary>Sets the Collection sort dictionary.</summary>

itext/itext.kernel/itext/kernel/pdf/filespec/PdfEncryptedPayloadFileSpecFactory.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,15 @@ public static PdfFileSpec Wrap(PdfDictionary dictionary) {
120120
if (ef == null || (ef.GetAsStream(PdfName.F) == null) && (ef.GetAsStream(PdfName.UF) == null)) {
121121
throw new PdfException(PdfException.EncryptedPayloadFileSpecShallHaveEFDictionary);
122122
}
123-
//TODO: it is possible to retrieve file spec without this check, should we do it?
124123
if (!PdfName.Filespec.Equals(dictionary.GetAsName(PdfName.Type))) {
125124
throw new PdfException(PdfException.EncryptedPayloadFileSpecShallHaveTypeEqualToFilespec);
126125
}
127-
//TODO: it is possible to retrieve file spec without this check, should we do it?
128126
if (!dictionary.IsIndirect()) {
129127
throw new PdfException(PdfException.EncryptedPayloadFileSpecShallBeIndirect);
130128
}
131129
PdfFileSpec fileSpec = PdfFileSpec.WrapFileSpecObject(dictionary);
132130
if (PdfEncryptedPayload.ExtractFrom(fileSpec) == null) {
133-
throw new PdfException(PdfException.EncryptedPayloadFileSpecDoesntHaveCorrectEncryptedPayloadDictionary);
131+
throw new PdfException(PdfException.EncryptedPayloadFileSpecDoesntHaveEncryptedPayloadDictionary);
134132
}
135133
return fileSpec;
136134
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10795d2195d5e4eab142a5dc71d49186efea5d74
1+
dd3ec4a32d3b4661bc40869b4d6a97f9c735b0e1

0 commit comments

Comments
 (0)