Skip to content

Commit 0763ef4

Browse files
dzmitry.kachkouiText-CI
authored andcommitted
Add check for number values in pdfa module
DEVSIX-2979 Autoported commit. Original commit hash: [5122a85b0]
1 parent b1fb8c7 commit 0763ef4

File tree

8 files changed

+137
-104
lines changed

8 files changed

+137
-104
lines changed

itext.tests/itext.pdfa.tests/itext/pdfa/PdfABigNumberTest.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

itext.tests/itext.pdfa.tests/itext/pdfa/checker/PdfA1ImplementationLimitsCheckerTest.cs

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,34 @@ public virtual void ValidObjectsTest() {
6767
int maxStringLength = pdfA1Checker.GetMaxStringLength();
6868
int maxArrayCapacity = MAX_ARRAY_CAPACITY;
6969
int maxDictionaryCapacity = MAX_DICTIONARY_CAPACITY;
70-
NUnit.Framework.Assert.AreEqual(maxStringLength, 65535);
70+
long maxIntegerValue = pdfA1Checker.GetMaxIntegerValue();
71+
long minIntegerValue = pdfA1Checker.GetMinIntegerValue();
72+
double maxRealValue = pdfA1Checker.GetMaxRealValue();
73+
NUnit.Framework.Assert.AreEqual(65535, maxStringLength);
7174
PdfString longString = PdfACheckerTestUtils.GetLongString(maxStringLength);
7275
PdfArray longArray = PdfACheckerTestUtils.GetLongArray(maxArrayCapacity);
7376
PdfDictionary longDictionary = PdfACheckerTestUtils.GetLongDictionary(maxDictionaryCapacity);
74-
PdfObject[] longObjects = new PdfObject[] { longString, longArray, longDictionary };
77+
NUnit.Framework.Assert.AreEqual(2147483647, maxIntegerValue);
78+
NUnit.Framework.Assert.AreEqual(-2147483648, minIntegerValue);
79+
NUnit.Framework.Assert.AreEqual(32767, maxRealValue, 0.001);
80+
PdfNumber largeInteger = new PdfNumber(maxIntegerValue);
81+
PdfNumber negativeInteger = new PdfNumber(minIntegerValue);
82+
PdfNumber largeReal = new PdfNumber(maxRealValue - 0.001);
83+
PdfObject[] largeObjects = new PdfObject[] { longString, longArray, longDictionary, largeInteger, negativeInteger
84+
, largeReal };
7585
// No exceptions should not be thrown as all values match the
7686
// limitations provided in specification
77-
foreach (PdfObject longObject in longObjects) {
78-
pdfA1Checker.CheckPdfObject(longObject);
79-
CheckInArray(longObject);
80-
CheckInDictionary(longObject);
81-
CheckInComplexStructure(longObject);
82-
CheckInContentStream(longObject);
83-
CheckInArrayInContentStream(longObject);
84-
CheckInDictionaryInContentStream(longObject);
85-
CheckInFormXObject(longObject);
86-
CheckInTilingPattern(longObject);
87-
CheckInType3Font(longObject);
87+
foreach (PdfObject largeObject in largeObjects) {
88+
pdfA1Checker.CheckPdfObject(largeObject);
89+
CheckInArray(largeObject);
90+
CheckInDictionary(largeObject);
91+
CheckInComplexStructure(largeObject);
92+
CheckInContentStream(largeObject);
93+
CheckInArrayInContentStream(largeObject);
94+
CheckInDictionaryInContentStream(largeObject);
95+
CheckInFormXObject(largeObject);
96+
CheckInTilingPattern(largeObject);
97+
CheckInType3Font(largeObject);
8898
}
8999
}
90100

@@ -108,6 +118,38 @@ public virtual void IndependentLongStringTest() {
108118
;
109119
}
110120

121+
[NUnit.Framework.Test]
122+
public virtual void IndependentLargeIntegerTest() {
123+
NUnit.Framework.Assert.That(() => {
124+
PdfNumber largeNumber = new PdfNumber(pdfA1Checker.GetMaxIntegerValue() + 1L);
125+
// An exception should be thrown as provided integer is larger then
126+
// it is allowed per specification
127+
pdfA1Checker.CheckPdfObject(largeNumber);
128+
}
129+
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.INTEGER_NUMBER_IS_OUT_OF_RANGE))
130+
;
131+
}
132+
133+
[NUnit.Framework.Test]
134+
public virtual void IndependentLargeNegativeIntegerTest() {
135+
NUnit.Framework.Assert.That(() => {
136+
PdfNumber largeNumber = new PdfNumber(pdfA1Checker.GetMinIntegerValue() - 1L);
137+
// An exception should be thrown as provided integer is smaller then
138+
// it is allowed per specification
139+
pdfA1Checker.CheckPdfObject(largeNumber);
140+
}
141+
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.INTEGER_NUMBER_IS_OUT_OF_RANGE))
142+
;
143+
}
144+
145+
[NUnit.Framework.Test]
146+
public virtual void IndependentLargeRealTest() {
147+
PdfNumber largeNumber = new PdfNumber(pdfA1Checker.GetMaxRealValue() + 1.0);
148+
// TODO DEVSIX-4182
149+
// An exception is not thrown as any number greater then 32767 is considered as Integer
150+
pdfA1Checker.CheckPdfObject(largeNumber);
151+
}
152+
111153
[NUnit.Framework.Test]
112154
public virtual void IndependentLongArrayTest() {
113155
NUnit.Framework.Assert.That(() => {
@@ -180,6 +222,38 @@ public virtual void LongStringInContentStreamTest() {
180222
;
181223
}
182224

225+
[NUnit.Framework.Test]
226+
public virtual void LargeIntegerInContentStreamTest() {
227+
NUnit.Framework.Assert.That(() => {
228+
PdfNumber largeNumber = new PdfNumber(pdfA1Checker.GetMaxIntegerValue() + 1L);
229+
// An exception should be thrown as provided integer is larger then
230+
// it is allowed per specification
231+
CheckInContentStream(largeNumber);
232+
}
233+
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.INTEGER_NUMBER_IS_OUT_OF_RANGE))
234+
;
235+
}
236+
237+
[NUnit.Framework.Test]
238+
public virtual void LargeNegativeIntegerInContentStreamTest() {
239+
NUnit.Framework.Assert.That(() => {
240+
PdfNumber largeNumber = new PdfNumber(pdfA1Checker.GetMinIntegerValue() - 1L);
241+
// An exception should be thrown as provided integer is smaller then
242+
// it is allowed per specification
243+
CheckInContentStream(largeNumber);
244+
}
245+
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.INTEGER_NUMBER_IS_OUT_OF_RANGE))
246+
;
247+
}
248+
249+
[NUnit.Framework.Test]
250+
public virtual void LargeRealInContentStreamTest() {
251+
PdfNumber largeNumber = new PdfNumber(pdfA1Checker.GetMaxRealValue() + 1.0);
252+
// TODO DEVSIX-4182
253+
// An exception is not thrown as any number greater then 32767 is considered as Integer
254+
CheckInContentStream(largeNumber);
255+
}
256+
183257
[NUnit.Framework.Test]
184258
public virtual void LongArrayInContentStreamTest() {
185259
NUnit.Framework.Assert.That(() => {
@@ -334,7 +408,7 @@ public virtual void LongStringInType3FontTest() {
334408
private PdfString BuildLongString() {
335409
int maxAllowedLength = pdfA1Checker.GetMaxStringLength();
336410
int testLength = maxAllowedLength + 1;
337-
NUnit.Framework.Assert.AreEqual(testLength, 65536);
411+
NUnit.Framework.Assert.AreEqual(65536, testLength);
338412
return PdfACheckerTestUtils.GetLongString(testLength);
339413
}
340414

itext.tests/itext.pdfa.tests/itext/pdfa/checker/PdfA2ImplementationLimitsCheckerTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,17 @@ public virtual void DictionaryCapacityHasNoLimitsTest() {
107107
// and stream in PDFA 2
108108
pdfA2Checker.CheckPdfObject(longStream);
109109
}
110+
111+
[NUnit.Framework.Test]
112+
public virtual void IndependentLargeRealTest() {
113+
NUnit.Framework.Assert.That(() => {
114+
PdfNumber largeNumber = new PdfNumber(pdfA2Checker.GetMaxRealValue());
115+
// TODO DEVSIX-4182
116+
// An exception is thrown as any number greater then 32767 is considered as Integer
117+
pdfA2Checker.CheckPdfObject(largeNumber);
118+
}
119+
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.INTEGER_NUMBER_IS_OUT_OF_RANGE))
120+
;
121+
}
110122
}
111123
}

itext/itext.kernel/itext/kernel/pdf/PdfNumber.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public override bool Equals(Object o) {
143143
)o).value, value) == 0;
144144
}
145145

146+
/// <summary>Checks if string representation of the value contains decimal point.</summary>
147+
/// <returns>true if contains so the number must be real not integer</returns>
148+
public virtual bool HasDecimalPoint() {
149+
return this.ToString().Contains(".");
150+
}
151+
146152
public override int GetHashCode() {
147153
if (changed) {
148154
//if the instance was modified, hashCode also will be changed, it may cause inconsistency.

itext/itext.pdfa/itext/pdfa/PdfAConformanceException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public const String IF_OUTPUTINTENTS_ARRAY_HAS_MORE_THAN_ONE_ENTRY_WITH_DESTOUTP
176176
public const String IF_SPECIFIED_RENDERING_SHALL_BE_ONE_OF_THE_FOLLOWING_RELATIVECOLORIMETRIC_ABSOLUTECOLORIMETRIC_PERCEPTUAL_OR_SATURATION
177177
= "If specified rendering shall be one of the following relativecolorimetric absolutecolorimetric perceptual or saturation";
178178

179+
public const String INTEGER_NUMBER_IS_OUT_OF_RANGE = "Integer number is out of range";
180+
179181
public const String THE_DOCUMENT_DOES_NOT_CONTAIN_A_PDFA_OUTPUTINTENT_BUT_PAGE_CONTAINS_TRANSPARENCY_AND_DOES_NOT_CONTAIN_BLENDING_COLOR_SPACE
180182
= "If the document does not contain a OutputIntent, then page with transparency shall include the dictionary with Group key that include a CS with blending colour space";
181183

itext/itext.pdfa/itext/pdfa/checker/PdfA1Checker.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ protected internal override void CheckContentStreamObject(PdfObject @object) {
304304
break;
305305
}
306306

307+
case PdfObject.NUMBER: {
308+
CheckPdfNumber((PdfNumber)@object);
309+
break;
310+
}
311+
307312
case PdfObject.ARRAY: {
308313
PdfArray array = (PdfArray)@object;
309314
CheckPdfArray(array);
@@ -441,15 +446,36 @@ protected internal override void CheckOutputIntents(PdfDictionary catalog) {
441446
}
442447

443448
protected internal override void CheckPdfNumber(PdfNumber number) {
444-
if (Math.Abs(number.LongValue()) > GetMaxRealValue() && number.ToString().Contains(".")) {
445-
throw new PdfAConformanceException(PdfAConformanceException.REAL_NUMBER_IS_OUT_OF_RANGE);
449+
if (number.HasDecimalPoint()) {
450+
if (Math.Abs(number.LongValue()) > GetMaxRealValue()) {
451+
throw new PdfAConformanceException(PdfAConformanceException.REAL_NUMBER_IS_OUT_OF_RANGE);
452+
}
453+
}
454+
else {
455+
if (number.LongValue() > GetMaxIntegerValue() || number.LongValue() < GetMinIntegerValue()) {
456+
throw new PdfAConformanceException(PdfAConformanceException.INTEGER_NUMBER_IS_OUT_OF_RANGE);
457+
}
446458
}
447459
}
448460

461+
/// <summary>Retrieve maximum allowed real value.</summary>
462+
/// <returns>maximum allowed real number</returns>
449463
protected internal virtual double GetMaxRealValue() {
450464
return 32767;
451465
}
452466

467+
/// <summary>Retrieve maximal allowed integer value.</summary>
468+
/// <returns>maximal allowed integer number</returns>
469+
protected internal virtual long GetMaxIntegerValue() {
470+
return int.MaxValue;
471+
}
472+
473+
/// <summary>Retrieve minimal allowed integer value.</summary>
474+
/// <returns>minimal allowed integer number</returns>
475+
protected internal virtual long GetMinIntegerValue() {
476+
return int.MinValue;
477+
}
478+
453479
protected internal override void CheckPdfArray(PdfArray array) {
454480
if (array.Size() > GetMaxArrayCapacity()) {
455481
throw new PdfAConformanceException(PdfAConformanceException.MAXIMUM_ARRAY_CAPACITY_IS_EXCEEDED);

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e627059f81304c1772aeb99e55acbb4f21bb25c5
1+
5122a85b0f187436d65f354d97b1d080e8975e20

0 commit comments

Comments
 (0)