Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Jan 14, 2025
2 parents 8265e47 + 66744c0 commit 0f2b5f2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 45 deletions.
19 changes: 19 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ You should have received a copy of the GNU Affero General Public License
using iText.IO.Font.Constants;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Exceptions;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf.Canvas;
Expand Down Expand Up @@ -262,5 +263,23 @@ public virtual void WriteUtf8ActualText() {
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "writeUtf8ActualText.pdf"
, sourceFolder + "cmp_writeUtf8ActualText.pdf", destinationFolder, "diffActualText_"));
}

[NUnit.Framework.Test]
public virtual void EmptyHexWriting() {
PdfString @string = new PdfString("");
NUnit.Framework.Assert.AreEqual("", @string.ToUnicodeString());
@string.SetHexWriting(true);
NUnit.Framework.Assert.AreEqual("", @string.ToUnicodeString());
}

[NUnit.Framework.Test]
public virtual void NullHexWriting() {
PdfString @string = new PdfString("hello");
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfException), () => {
@string.EncodeBytes(null);
}
);
NUnit.Framework.Assert.AreEqual("byte[] should not be null.", e.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
Expand Down Expand Up @@ -105,23 +106,13 @@ public virtual void PrepareDocForSignDeferredNotEnoughSizeTest() {
[NUnit.Framework.Test]
public virtual void PrepareDocForSignDeferredLittleSpaceTest() {
String input = sourceFolder + "helloWorldDoc.pdf";
String sigFieldName = "DeferredSignature1";
PdfName filter = PdfName.Adobe_PPKLite;
PdfName subFilter = PdfName.Adbe_pkcs7_detached;
PdfReader reader = new PdfReader(input);
PdfSigner signer = new PdfSigner(reader, new MemoryStream(), new StampingProperties());
SignerProperties signerProperties = new SignerProperties().SetFieldName(sigFieldName);
signer.SetSignerProperties(signerProperties);
SignatureFieldAppearance appearance = new SignatureFieldAppearance(SignerProperties.IGNORED_ID).SetContent
("Signature field which signing is deferred.");
signerProperties.SetPageRect(new Rectangle(36, 600, 200, 100)).SetPageNumber(1).SetSignatureAppearance(appearance
);
IExternalSignatureContainer external = new ExternalBlankSignatureContainer(filter, subFilter);
// This size is definitely not enough, however, the size check will pass.
// The test will fail lately on an invalid key
int estimatedSize = 0;
Exception e = NUnit.Framework.Assert.Catch(typeof(ArgumentException), () => signer.SignExternalContainer(external
, estimatedSize));
SignDeferredTest.DummySigner dummySigner = new SignDeferredTest.DummySigner(reader, new MemoryStream(), new
StampingProperties());
PdfDictionary content = new PdfDictionary();
content.Put(PdfName.Contents, new PdfString("test"));
Exception e = NUnit.Framework.Assert.Catch(typeof(ArgumentException), () => dummySigner.DummyClose(content
));
NUnit.Framework.Assert.AreEqual(SignExceptionMessageConstant.TOO_BIG_KEY, e.Message);
}

Expand Down Expand Up @@ -343,5 +334,20 @@ public virtual void ModifySigningDictionary(PdfDictionary signDic) {
}
}
//\endcond

//\cond DO_NOT_DOCUMENT
internal class DummySigner : PdfSigner {
public DummySigner(PdfReader reader, Stream outputStream, StampingProperties properties)
: base(reader, outputStream, properties) {
}

public virtual void DummyClose(PdfDictionary content) {
preClosed = true;
exclusionLocations = new Dictionary<PdfName, PdfLiteral>();
exclusionLocations.Put(PdfName.Contents, new PdfLiteral(1));
Close(content);
}
}
//\endcond
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class SignatureAppearanceRenderer : AbstractTextFieldRenderer {

private const float EPS = 1e-5f;

private readonly SignatureAppearanceRenderer.RenderingMode renderingMode;
private readonly SignatureAppearanceRenderer.DisplayOption displayOption;

private bool isFontSizeApproximated = false;

Expand All @@ -62,7 +62,7 @@ public class SignatureAppearanceRenderer : AbstractTextFieldRenderer {
/// <param name="modelElement">the model element</param>
public SignatureAppearanceRenderer(SignatureFieldAppearance modelElement)
: base(modelElement) {
renderingMode = RetrieveRenderingMode();
displayOption = RetrieveRenderingMode();
}

/// <summary><inheritDoc/></summary>
Expand Down Expand Up @@ -114,9 +114,9 @@ protected internal override void AdjustFieldLayout(LayoutContext layoutContext)
}
Rectangle descriptionRect = null;
Rectangle signatureRect = null;
switch (renderingMode) {
case SignatureAppearanceRenderer.RenderingMode.NAME_AND_DESCRIPTION:
case SignatureAppearanceRenderer.RenderingMode.GRAPHIC_AND_DESCRIPTION: {
switch (displayOption) {
case SignatureAppearanceRenderer.DisplayOption.NAME_AND_DESCRIPTION:
case SignatureAppearanceRenderer.DisplayOption.GRAPHIC_AND_DESCRIPTION: {
// Split the signature field into two and add the name of the signer or an image to the one side,
// the description to the other side.
UnitValue[] paddings = GetPaddings();
Expand All @@ -140,13 +140,13 @@ protected internal override void AdjustFieldLayout(LayoutContext layoutContext)
break;
}

case SignatureAppearanceRenderer.RenderingMode.GRAPHIC: {
case SignatureAppearanceRenderer.DisplayOption.GRAPHIC: {
// The signature field will consist of an image only; no description will be shown.
signatureRect = bBox;
break;
}

case SignatureAppearanceRenderer.RenderingMode.DESCRIPTION: {
case SignatureAppearanceRenderer.DisplayOption.DESCRIPTION: {
// Default one, it just shows whatever description was defined for the signature.
float additionalHeight = CalculateAdditionalHeight();
if (RetrieveHeight() == null) {
Expand All @@ -167,7 +167,7 @@ protected internal override void AdjustFieldLayout(LayoutContext layoutContext)
return;
}
}
AdjustChildrenLayout(renderingMode, signatureRect, descriptionRect, layoutContext.GetArea().GetPageNumber(
AdjustChildrenLayout(displayOption, signatureRect, descriptionRect, layoutContext.GetArea().GetPageNumber(
));
}

Expand Down Expand Up @@ -232,25 +232,25 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
FormFieldRendererUtil.ReapplyProperties(modelElement, properties);
}

private void AdjustChildrenLayout(SignatureAppearanceRenderer.RenderingMode renderingMode, Rectangle signatureRect
private void AdjustChildrenLayout(SignatureAppearanceRenderer.DisplayOption displayOption, Rectangle signatureRect
, Rectangle descriptionRect, int pageNum) {
switch (renderingMode) {
case SignatureAppearanceRenderer.RenderingMode.NAME_AND_DESCRIPTION: {
switch (displayOption) {
case SignatureAppearanceRenderer.DisplayOption.NAME_AND_DESCRIPTION: {
ParagraphRenderer name = (ParagraphRenderer)flatRenderer.GetChildRenderers()[0];
RelayoutParagraph(name, signatureRect, pageNum);
ParagraphRenderer description = (ParagraphRenderer)flatRenderer.GetChildRenderers()[1];
RelayoutParagraph(description, descriptionRect, pageNum);
break;
}

case SignatureAppearanceRenderer.RenderingMode.GRAPHIC_AND_DESCRIPTION: {
case SignatureAppearanceRenderer.DisplayOption.GRAPHIC_AND_DESCRIPTION: {
RelayoutImage(signatureRect, pageNum);
ParagraphRenderer description = (ParagraphRenderer)flatRenderer.GetChildRenderers()[1];
RelayoutParagraph(description, descriptionRect, pageNum);
break;
}

case SignatureAppearanceRenderer.RenderingMode.GRAPHIC: {
case SignatureAppearanceRenderer.DisplayOption.GRAPHIC: {
RelayoutImage(signatureRect, pageNum);
break;
}
Expand Down Expand Up @@ -338,8 +338,8 @@ private void ApproximateFontSizeToFitLayoutArea(LayoutContext layoutContext) {
if (this.HasOwnProperty(Property.FONT_SIZE) || modelElement.HasOwnProperty(Property.FONT_SIZE)) {
return;
}
if (SignatureAppearanceRenderer.RenderingMode.GRAPHIC == renderingMode || SignatureAppearanceRenderer.RenderingMode
.GRAPHIC_AND_DESCRIPTION == renderingMode || SignatureAppearanceRenderer.RenderingMode.CUSTOM == renderingMode
if (SignatureAppearanceRenderer.DisplayOption.GRAPHIC == displayOption || SignatureAppearanceRenderer.DisplayOption
.GRAPHIC_AND_DESCRIPTION == displayOption || SignatureAppearanceRenderer.DisplayOption.CUSTOM == displayOption
) {
// We can expect CLIP_ELEMENT log messages since the initial image size may be larger than the field height.
// But image size will be adjusted during its relayout in #adjustFieldLayout.
Expand All @@ -352,38 +352,38 @@ private void ApproximateFontSizeToFitLayoutArea(LayoutContext layoutContext) {
}
}

private SignatureAppearanceRenderer.RenderingMode RetrieveRenderingMode() {
private SignatureAppearanceRenderer.DisplayOption RetrieveRenderingMode() {
IList<IElement> contentElements = ((SignatureFieldAppearance)modelElement).GetContentElements();
if (contentElements.Count == 2 && contentElements[1] is Paragraph) {
if (contentElements[0] is Paragraph) {
return SignatureAppearanceRenderer.RenderingMode.NAME_AND_DESCRIPTION;
return SignatureAppearanceRenderer.DisplayOption.NAME_AND_DESCRIPTION;
}
if (contentElements[0] is Image) {
return SignatureAppearanceRenderer.RenderingMode.GRAPHIC_AND_DESCRIPTION;
return SignatureAppearanceRenderer.DisplayOption.GRAPHIC_AND_DESCRIPTION;
}
}
if (contentElements.Count == 1) {
if (contentElements[0] is Paragraph) {
return SignatureAppearanceRenderer.RenderingMode.DESCRIPTION;
return SignatureAppearanceRenderer.DisplayOption.DESCRIPTION;
}
if (contentElements[0] is Image) {
return SignatureAppearanceRenderer.RenderingMode.GRAPHIC;
return SignatureAppearanceRenderer.DisplayOption.GRAPHIC;
}
}
return SignatureAppearanceRenderer.RenderingMode.CUSTOM;
return SignatureAppearanceRenderer.DisplayOption.CUSTOM;
}

/// <summary>Signature rendering modes.</summary>
private enum RenderingMode {
/// <summary>The rendering mode is just the description.</summary>
/// <summary>Signature display options.</summary>
private enum DisplayOption {
/// <summary>The display option is just the description.</summary>
DESCRIPTION,
/// <summary>The rendering mode is the name of the signer and the description.</summary>
/// <summary>The display option is the name of the signer and the description.</summary>
NAME_AND_DESCRIPTION,
/// <summary>The rendering mode is an image and the description.</summary>
/// <summary>The display option is an image and the description.</summary>
GRAPHIC_AND_DESCRIPTION,
/// <summary>The rendering mode is just an image.</summary>
/// <summary>The display option is just an image.</summary>
GRAPHIC,
/// <summary>The rendering mode is div.</summary>
/// <summary>The display option is div.</summary>
CUSTOM
}
}
Expand Down
9 changes: 9 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/PdfString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ You should have received a copy of the GNU Affero General Public License
*/
using System;
using System.Text;
using iText.Commons.Utils;
using iText.IO.Font;
using iText.IO.Source;
using iText.IO.Util;
using iText.Kernel.Exceptions;
using iText.Kernel.Utils;

namespace iText.Kernel.Pdf {
Expand Down Expand Up @@ -326,6 +328,13 @@ protected internal virtual byte[] DecodeContent() {
/// <param name="bytes">byte array to manipulate with.</param>
/// <returns>Hexadecimal string or string with escaped symbols in byte array view.</returns>
protected internal virtual byte[] EncodeBytes(byte[] bytes) {
if (bytes == null) {
throw new PdfException(MessageFormatUtil.Format(KernelExceptionMessageConstant.ARG_SHOULD_NOT_BE_NULL, "byte[]"
));
}
if (bytes.Length == 0) {
return bytes;
}
if (hexWriting) {
ByteBuffer buf = new ByteBuffer(bytes.Length * 2);
foreach (byte b in bytes) {
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
86e2d518cf3bf4ca60842b4e1084a7f57db35fba
e78a4b7fe88d0ac47d4083cb01603adb40483bfc

0 comments on commit 0f2b5f2

Please sign in to comment.