From bb8defa23db2c6f196946b1ab9729b4c7168e715 Mon Sep 17 00:00:00 2001 From: VahidN Date: Mon, 2 Oct 2023 14:04:25 +0330 Subject: [PATCH] Fix missing Dispose call on local IDisposable --- .github/workflows/codeql/codeql-config.yml | 2 + .../System/util/Properties.cs | 2 +- .../iTextSharp/text/pdf/GlyphList.cs | 2 +- .../iTextSharp/text/pdf/PdfEncodings.cs | 11 ++-- .../iTextSharp/text/pdf/PdfEncryption.cs | 35 +++++++++--- .../iTextSharp/text/pdf/PdfPKCS7.cs | 56 +++++++++++-------- .../text/pdf/PdfPublicKeySecurityHandler.cs | 14 ++--- .../iTextSharp/text/pdf/PdfReader.cs | 22 ++++---- .../iTextSharp/text/pdf/PdfSigGenericPKCS.cs | 2 +- .../text/pdf/RandomAccessFileOrArray.cs | 2 +- .../iTextSharp/text/pdf/SimpleBookmark.cs | 33 +++++++---- .../text/pdf/SimpleNamedDestination.cs | 10 +++- .../text/pdf/TSAClientBouncyCastle.cs | 2 +- .../iTextSharp/text/pdf/Type1Font.cs | 6 +- .../iTextSharp/text/pdf/XfaForm.cs | 26 +++++---- .../iTextSharp/text/pdf/XfdfReader.cs | 3 +- .../text/pdf/codec/JBIG2SegmentReader.cs | 4 +- .../iTextSharp/text/pdf/codec/wmf/MetaDo.cs | 10 +++- .../text/xml/simpleparser/SimpleXMLParser.cs | 5 +- .../iTextSharp/text/xml/xmp/XmpReader.cs | 6 +- 20 files changed, 154 insertions(+), 99 deletions(-) diff --git a/.github/workflows/codeql/codeql-config.yml b/.github/workflows/codeql/codeql-config.yml index f528f04..e6c98fe 100644 --- a/.github/workflows/codeql/codeql-config.yml +++ b/.github/workflows/codeql/codeql-config.yml @@ -8,3 +8,5 @@ query-filters: id: cs/useless-if-statement - exclude: id: cs/empty-block + - exclude: + id: cs/useless-assignment-to-local diff --git a/src/iTextSharp.LGPLv2.Core/System/util/Properties.cs b/src/iTextSharp.LGPLv2.Core/System/util/Properties.cs index 275037d..156836a 100644 --- a/src/iTextSharp.LGPLv2.Core/System/util/Properties.cs +++ b/src/iTextSharp.LGPLv2.Core/System/util/Properties.cs @@ -55,7 +55,7 @@ public void Clear() public void Load(Stream inStream) { - var inp = new StreamReader(inStream, EncodingsRegistry.GetEncoding(1252)); + using var inp = new StreamReader(inStream, EncodingsRegistry.GetEncoding(1252)); while (true) { // Get next line diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/GlyphList.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/GlyphList.cs index f5349ff..f2c4480 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/GlyphList.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/GlyphList.cs @@ -23,7 +23,7 @@ static GlyphList() } var buf = new byte[1024]; - var outputStream = new MemoryStream(); + using var outputStream = new MemoryStream(); while (true) { var size = resourceStream.Read(buf, 0, buf.Length); diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncodings.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncodings.cs index f743be9..5cbaf9d 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncodings.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncodings.cs @@ -631,7 +631,7 @@ internal static void EncodeSequence(int size, byte[] seqs, char cid, IList planes) { - var rd = new StreamReader(inp, Encoding.ASCII); + using var rd = new StreamReader(inp, Encoding.ASCII); string line = null; var state = CIDNONE; var seqs = new byte[7]; @@ -674,10 +674,12 @@ internal static void EncodeStream(Stream inp, IList planes) var tk = new StringTokenizer(line); var t = tk.NextToken(); var size = t.Length / 2 - 1; - var start = long.Parse(t.Substring(1, t.Length - 2), NumberStyles.HexNumber, + var start = long.Parse(t.Substring(1, t.Length - 2), + NumberStyles.HexNumber, CultureInfo.InvariantCulture); t = tk.NextToken(); - var end = long.Parse(t.Substring(1, t.Length - 2), NumberStyles.HexNumber, + var end = long.Parse(t.Substring(1, t.Length - 2), + NumberStyles.HexNumber, CultureInfo.InvariantCulture); t = tk.NextToken(); var cid = int.Parse(t, CultureInfo.InvariantCulture); @@ -701,7 +703,8 @@ internal static void EncodeStream(Stream inp, IList planes) var tk = new StringTokenizer(line); var t = tk.NextToken(); var size = t.Length / 2 - 1; - var start = long.Parse(t.Substring(1, t.Length - 2), NumberStyles.HexNumber, + var start = long.Parse(t.Substring(1, t.Length - 2), + NumberStyles.HexNumber, CultureInfo.InvariantCulture); t = tk.NextToken(); var cid = int.Parse(t, CultureInfo.InvariantCulture); diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncryption.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncryption.cs index 648ec28..58d089e 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncryption.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncryption.cs @@ -236,7 +236,7 @@ public byte[] DecryptByteArray(byte[] b) throw new ArgumentNullException(nameof(b)); } - var ba = new MemoryStream(); + using var ba = new MemoryStream(); var dec = GetDecryptor(); var b2 = dec.Update(b, 0, b.Length); if (b2 != null) @@ -327,7 +327,7 @@ public PdfDictionary GetEncryptionDictionary() } #if NET40 - SHA1 sh = new SHA1CryptoServiceProvider(); + using var sh = new SHA1CryptoServiceProvider(); byte[] encodedRecipient = null; byte[] seed = PublicKeyHandler.GetSeed(); sh.TransformBlock(seed, 0, seed.Length, seed, 0); @@ -571,7 +571,10 @@ public void SetupByEncryptionKey(byte[] key, int keyLength) /// /// - public void SetupByOwnerPassword(byte[] documentId, byte[] ownerPassword, byte[] userKey, byte[] ownerKey, + public void SetupByOwnerPassword(byte[] documentId, + byte[] ownerPassword, + byte[] userKey, + byte[] ownerKey, int permissions) { SetupByOwnerPad(documentId, PadPassword(ownerPassword), userKey, ownerKey, permissions); @@ -758,8 +761,13 @@ private void SetupUserKey() /// implements step d of Algorithm 2.A: Retrieving the file encryption key from an encrypted document in order to /// decrypt it (revision 6 and later) - ISO 32000-2 section 7.6.4.3.3 /// - public void SetupByOwnerPassword(byte[] documentId, byte[] ownerPassword, - byte[] uValue, byte[] ueValue, byte[] oValue, byte[] oeValue, int permissions) + public void SetupByOwnerPassword(byte[] documentId, + byte[] ownerPassword, + byte[] uValue, + byte[] ueValue, + byte[] oValue, + byte[] oeValue, + int permissions) { if (oeValue == null) { @@ -778,8 +786,13 @@ public void SetupByOwnerPassword(byte[] documentId, byte[] ownerPassword, /// implements step e of Algorithm 2.A: Retrieving the file encryption key from an encrypted document in order to /// decrypt it (revision 6 and later) - ISO 32000-2 section 7.6.4.3.3 /// - public void SetupByUserPassword(byte[] documentId, byte[] userPassword, - byte[] uValue, byte[] ueValue, byte[] oValue, byte[] oeValue, int permissions) + public void SetupByUserPassword(byte[] documentId, + byte[] userPassword, + byte[] uValue, + byte[] ueValue, + byte[] oValue, + byte[] oeValue, + int permissions) { if (ueValue == null) { @@ -851,8 +864,12 @@ public static byte[] HashAlg2B(byte[] input, byte[] salt, byte[] userKey) Array.Copy(k1, 0, k1, singleSequenceSize * i, singleSequenceSize); } - var e = AesCbcNoPadding.ProcessBlock(true, k.CopyOf(16), - k1, 0, k1.Length, k.CopyOfRange(16, 32)); + var e = AesCbcNoPadding.ProcessBlock(true, + k.CopyOf(16), + k1, + 0, + k1.Length, + k.CopyOfRange(16, 32)); lastEByte = e[e.Length - 1] & 0xFF; diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPKCS7.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPKCS7.cs index 60f9d34..def0b50 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPKCS7.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPKCS7.cs @@ -148,7 +148,8 @@ public PdfPkcs7(byte[] contentsKey, byte[] certsKey) _signCerts = _certs; SigningCertificate = _certs[0]; CrLs = new List(); - var inp = new Asn1InputStream(new MemoryStream(contentsKey)); + using var memoryStream = new MemoryStream(contentsKey); + using var inp = new Asn1InputStream(memoryStream); _digest = ((DerOctetString)inp.ReadObject()).GetOctets(); _sig = SignerUtilities.GetSigner("SHA1withRSA"); _sig.Init(false, SigningCertificate.GetPublicKey()); @@ -167,7 +168,8 @@ public PdfPkcs7(byte[] contentsKey, byte[] certsKey) /// the /Contents key public PdfPkcs7(byte[] contentsKey) { - var din = new Asn1InputStream(new MemoryStream(contentsKey)); + using var memoryStream = new MemoryStream(contentsKey); + using var din = new Asn1InputStream(memoryStream); // // Basic checks to make sure it's a PKCS#7 SignedData Object @@ -353,8 +355,11 @@ public PdfPkcs7(byte[] contentsKey) /// the certificate revocation list /// the hash algorithm /// true if the sub-filter is adbe.pkcs7.sha1 - public PdfPkcs7(ICipherParameters privKey, X509Certificate[] certChain, object[] crlList, - string hashAlgorithm, bool hasRsAdata) + public PdfPkcs7(ICipherParameters privKey, + X509Certificate[] certChain, + object[] crlList, + string hashAlgorithm, + bool hasRsAdata) { if (certChain == null) { @@ -691,7 +696,9 @@ public static string VerifyCertificate(X509Certificate cert, object[] crls, Date /// the certificate revocation list or null /// the date or null for the current date /// null if the certificate chain could be validade or a - public static object[] VerifyCertificates(X509Certificate[] certs, IList keystore, object[] crls, + public static object[] VerifyCertificates(X509Certificate[] certs, + IList keystore, + object[] crls, DateTime calendar) { if (certs == null) @@ -917,12 +924,10 @@ public byte[] GetEncodedPkcs1() _digest = _sig.GenerateSignature(); } - var bOut = new MemoryStream(); - - var dout = Asn1OutputStream.Create(bOut); + using var bOut = new MemoryStream(); + using var dout = Asn1OutputStream.Create(bOut); dout.WriteObject(new DerOctetString(_digest)); dout.Dispose(); - return bOut.ToArray(); } @@ -1006,7 +1011,8 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli v = new Asn1EncodableVector(); foreach (var xcert in _certs) { - var tempstream = new Asn1InputStream(new MemoryStream(xcert.GetEncoded())); + using var memoryStream = new MemoryStream(xcert.GetEncoded()); + using var tempstream = new Asn1InputStream(memoryStream); v.Add(tempstream.ReadObject()); } @@ -1034,7 +1040,8 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli // add the authenticated attribute if present if (secondDigest != null /*&& signingTime != null*/) { - signerinfo.Add(new DerTaggedObject(false, 0, + signerinfo.Add(new DerTaggedObject(false, + 0, getAuthenticatedAttributeSet(secondDigest, signingTime, ocsp))); } @@ -1052,7 +1059,8 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli // Sam found Adobe expects time-stamped SHA1-1 of the encrypted digest if (tsaClient != null) { - var tsImprint = SHA1.Create().ComputeHash(_digest); + using var sha1 = SHA1.Create(); + var tsImprint = sha1.ComputeHash(_digest); var tsToken = tsaClient.GetTimeStampToken(this, tsImprint); if (tsToken != null) { @@ -1091,11 +1099,9 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli whole.Add(new DerObjectIdentifier(IdPkcs7SignedData)); whole.Add(new DerTaggedObject(0, new DerSequence(body))); - var bOut = new MemoryStream(); - - var dout = Asn1OutputStream.Create(bOut); + using var bOut = new MemoryStream(); + using var dout = Asn1OutputStream.Create(bOut); dout.WriteObject(new DerSequence(whole)); - return bOut.ToArray(); } @@ -1258,10 +1264,11 @@ private static Asn1Object getExtensionValue(X509Certificate cert, string oid) return null; } - var aIn = new Asn1InputStream(new MemoryStream(bytes)); + using var memoryStream = new MemoryStream(bytes); + using var aIn = new Asn1InputStream(memoryStream); var octs = (Asn1OctetString)aIn.ReadObject(); - aIn = new Asn1InputStream(new MemoryStream(octs.GetOctets())); - return aIn.ReadObject(); + using var ain = new Asn1InputStream(new MemoryStream(octs.GetOctets())); + return ain.ReadObject(); } /// @@ -1271,7 +1278,8 @@ private static Asn1Object getExtensionValue(X509Certificate cert, string oid) /// a DERObject private static Asn1Object getIssuer(byte[] enc) { - var inp = new Asn1InputStream(new MemoryStream(enc)); + using var memoryStream = new MemoryStream(enc); + using var inp = new Asn1InputStream(memoryStream); var seq = (Asn1Sequence)inp.ReadObject(); return (Asn1Object)seq[seq[0] is DerTaggedObject ? 3 : 2]; } @@ -1290,7 +1298,8 @@ private static string getStringFromGeneralName(Asn1Object names) /// a DERObject private static Asn1Object getSubject(byte[] enc) { - var inp = new Asn1InputStream(new MemoryStream(enc)); + using var memoryStream = new MemoryStream(enc); + using var inp = new Asn1InputStream(memoryStream); var seq = (Asn1Sequence)inp.ReadObject(); return (Asn1Object)seq[seq[0] is DerTaggedObject ? 5 : 4]; } @@ -1314,7 +1323,8 @@ private static Asn1EncodableVector buildUnauthenticatedAttributes(byte[] timeSta // @todo: move this together with the rest of the defintions var idTimeStampToken = "1.2.840.113549.1.9.16.2.14"; // RFC 3161 id-aa-timeStampToken - var tempstream = new Asn1InputStream(new MemoryStream(timeStampToken)); + using var memoryStream = new MemoryStream(timeStampToken); + using var tempstream = new Asn1InputStream(memoryStream); var unauthAttributes = new Asn1EncodableVector(); var v = new Asn1EncodableVector(); @@ -1408,7 +1418,7 @@ private void findOcsp(Asn1Sequence seq) } var os = (DerOctetString)seq[1]; - var inp = new Asn1InputStream(os.GetOctets()); + using var inp = new Asn1InputStream(os.GetOctets()); var resp = BasicOcspResponse.GetInstance(inp.ReadObject()); Ocsp = new BasicOcspResp(resp); } diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPublicKeySecurityHandler.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPublicKeySecurityHandler.cs index 5d5e774..3b8eb2f 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPublicKeySecurityHandler.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPublicKeySecurityHandler.cs @@ -64,17 +64,11 @@ public byte[] GetEncodedRecipient(int index) pkcs7Input[23] = one; var obj = createDerForRecipient(pkcs7Input, certificate); - - var baos = new MemoryStream(); - - var k = Asn1OutputStream.Create(baos, Asn1Encodable.Der); - + using var baos = new MemoryStream(); + using var k = Asn1OutputStream.Create(baos, Asn1Encodable.Der); k.WriteObject(obj); - cms = baos.ToArray(); - recipient.Cms = cms; - return cms; } @@ -104,8 +98,8 @@ public PdfArray GetEncodedRecipients() private static KeyTransRecipientInfo computeRecipientInfo(X509Certificate x509Certificate, byte[] abyte0) { - var asn1Inputstream = - new Asn1InputStream(new MemoryStream(x509Certificate.GetTbsCertificate())); + using var memoryStream = new MemoryStream(x509Certificate.GetTbsCertificate()); + using var asn1Inputstream = new Asn1InputStream(memoryStream); var tbscertificatestructure = TbsCertificateStructure.GetInstance(asn1Inputstream.ReadObject()); var algorithmidentifier = tbscertificatestructure.SubjectPublicKeyInfo.AlgorithmID; diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs index 414bd38..c0483df 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs @@ -633,7 +633,7 @@ public static byte[] Ascii85Decode(byte[] inp) throw new ArgumentNullException(nameof(inp)); } - var outp = new MemoryStream(); + using var outp = new MemoryStream(); var state = 0; var chn = new int[5]; for (var k = 0; k < inp.Length; ++k) @@ -719,7 +719,7 @@ public static byte[] AsciiHexDecode(byte[] inp) throw new ArgumentNullException(nameof(inp)); } - var outp = new MemoryStream(); + using var outp = new MemoryStream(); var first = true; var n1 = 0; for (var k = 0; k < inp.Length; ++k) @@ -812,8 +812,8 @@ public static byte[] DecodePredictor(byte[] inp, PdfObject dicPar) bpc = ((PdfNumber)obj).IntValue; } - var dataStream = new MemoryStream(inp); - var fout = new MemoryStream(inp.Length); + using var dataStream = new MemoryStream(inp); + using var fout = new MemoryStream(inp.Length); var bytesPerPixel = colors * bpc / 8; var bytesPerRow = (colors * width * bpc + 7) / 8; var curr = new byte[bytesPerRow]; @@ -956,7 +956,7 @@ public static byte[] FlateDecode(byte[] inp, bool strict) { var stream = new MemoryStream(inp); var zip = new ZInflaterInputStream(stream); - var outp = new MemoryStream(); + using var outp = new MemoryStream(); var b = new byte[strict ? 4092 : 1]; try { @@ -995,8 +995,10 @@ public static Rectangle GetNormalizedRectangle(PdfArray box) var lly = ((PdfNumber)GetPdfObjectRelease(box[1])).FloatValue; var urx = ((PdfNumber)GetPdfObjectRelease(box[2])).FloatValue; var ury = ((PdfNumber)GetPdfObjectRelease(box[3])).FloatValue; - return new Rectangle(Math.Min(llx, urx), Math.Min(lly, ury), - Math.Max(llx, urx), Math.Max(lly, ury)); + return new Rectangle(Math.Min(llx, urx), + Math.Min(lly, ury), + Math.Max(llx, urx), + Math.Max(lly, ury)); } /// @@ -2013,7 +2015,6 @@ public byte[] GetPageContent(int pageNum, RandomAccessFileOrArray file) return Array.Empty(); } - MemoryStream bout = null; if (contents.IsStream()) { return GetStreamBytes((PrStream)contents, file); @@ -2022,7 +2023,7 @@ public byte[] GetPageContent(int pageNum, RandomAccessFileOrArray file) if (contents.IsArray()) { var array = (PdfArray)contents; - bout = new MemoryStream(); + using var bout = new MemoryStream(); for (var k = 0; k < array.Size; ++k) { var item = GetPdfObjectRelease(array[k]); @@ -2666,7 +2667,8 @@ internal virtual void SetViewerPreferences(PdfViewerPreferencesImp vp) vp.AddToCatalog(catalog); } - protected internal static PdfDictionary DuplicatePdfDictionary(PdfDictionary original, PdfDictionary copy, + protected internal static PdfDictionary DuplicatePdfDictionary(PdfDictionary original, + PdfDictionary copy, PdfReader newReader) { if (original == null) diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfSigGenericPKCS.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfSigGenericPKCS.cs index d1304fe..284b4a5 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfSigGenericPKCS.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfSigGenericPKCS.cs @@ -94,7 +94,7 @@ public void SetSignInfo(ICipherParameters privKey, X509Certificate[] certChain, Pkcs.SetExternalDigest(_externalDigest, _externalRsAdata, _digestEncryptionAlgorithm); if (PdfName.AdbeX509RsaSha1.Equals(Get(PdfName.Subfilter))) { - var bout = new MemoryStream(); + using var bout = new MemoryStream(); for (var k = 0; k < certChain.Length; ++k) { var tmp = certChain[k].GetEncoded(); diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/RandomAccessFileOrArray.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/RandomAccessFileOrArray.cs index 271c7d0..3e01937 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/RandomAccessFileOrArray.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/RandomAccessFileOrArray.cs @@ -211,7 +211,7 @@ public static byte[] InputStreamToArray(Stream isp) } var b = new byte[8192]; - var outp = new MemoryStream(); + using var outp = new MemoryStream(); while (true) { var read = isp.Read(b, 0, b.Length); diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleBookmark.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleBookmark.cs index 820aa1b..abcdf0b 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleBookmark.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleBookmark.cs @@ -302,10 +302,12 @@ public static string EscapeBinaryString(string s) /// the export destination. The stream is not closed /// the encoding according to IANA conventions /// codes above 127 will always be escaped with &#nn; if true , - public static void ExportToXml(IList> list, Stream outp, string encoding, + public static void ExportToXml(IList> list, + Stream outp, + string encoding, bool onlyAscii) { - var wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); + using var wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); ExportToXml(list, wrt, encoding, onlyAscii); } @@ -318,7 +320,9 @@ public static void ExportToXml(IList> list, /// the export destination. The writer is not closed /// the encoding according to IANA conventions /// codes above 127 will always be escaped with &#nn; if true , - public static void ExportToXml(IList> list, TextWriter wrt, string encoding, + public static void ExportToXml(IList> list, + TextWriter wrt, + string encoding, bool onlyAscii) { if (wrt == null) @@ -344,7 +348,9 @@ public static void ExportToXml(IList> list, /// the export destination. The writer is not closed /// the indentation level. Pretty printing significant only /// codes above 127 will always be escaped with &#nn; if true , - public static void ExportToXmlNode(IList> list, TextWriter outp, int indent, + public static void ExportToXmlNode(IList> list, + TextWriter outp, + int indent, bool onlyAscii) { if (list == null) @@ -474,8 +480,10 @@ public static List> ImportFromXml(TextReade return book._topList; } - public static object[] IterateOutlines(PdfWriter writer, PdfIndirectReference parent, - IList> kids, bool namedAsNames) + public static object[] IterateOutlines(PdfWriter writer, + PdfIndirectReference parent, + IList> kids, + bool namedAsNames) { if (writer == null) { @@ -602,7 +610,8 @@ public static object[] IterateOutlines(PdfWriter writer, PdfIndirectReference pa /// the bookmarks /// the number to add to the pages in range /// the page ranges, always in pairs. It can be null - public static void ShiftPageNumbers(IList> list, int pageShift, + public static void ShiftPageNumbers(IList> list, + int pageShift, int[] pageRange) { if (list == null) @@ -730,7 +739,8 @@ public static string UnEscapeBinaryString(string s) internal static void CreateOutlineAction(PdfDictionary outline, INullValueDictionary map, - PdfWriter writer, bool namedAsNames) + PdfWriter writer, + bool namedAsNames) { try { @@ -885,7 +895,9 @@ internal static void CreateOutlineAction(PdfDictionary outline, } private static IList> bookmarkDepth( - PdfReader reader, PdfDictionary outline, NullValueDictionary pages) + PdfReader reader, + PdfDictionary outline, + NullValueDictionary pages) { var list = new List>(); while (outline != null) @@ -1096,7 +1108,8 @@ private static string makeBookmarkParam(PdfArray dest, NullValueDictionary map, PdfObject dest, + private static void mapGotoBookmark(INullValueDictionary map, + PdfObject dest, NullValueDictionary pages) { if (dest.IsString()) diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleNamedDestination.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleNamedDestination.cs index cb59e4f..94cc145 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleNamedDestination.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/SimpleNamedDestination.cs @@ -152,10 +152,12 @@ public static string EscapeBinaryString(string s) /// the export destination. The stream is not closed /// the encoding according to IANA conventions /// codes above 127 will always be escaped with &#nn; if true , - public static void ExportToXml(INullValueDictionary names, Stream outp, string encoding, + public static void ExportToXml(INullValueDictionary names, + Stream outp, + string encoding, bool onlyAscii) { - var wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); + using var wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); ExportToXml(names, wrt, encoding, onlyAscii); } @@ -168,7 +170,9 @@ public static void ExportToXml(INullValueDictionary names, Strea /// the export destination. The writer is not closed /// the encoding according to IANA conventions /// codes above 127 will always be escaped with &#nn; if true , - public static void ExportToXml(INullValueDictionary names, TextWriter wrt, string encoding, + public static void ExportToXml(INullValueDictionary names, + TextWriter wrt, + string encoding, bool onlyAscii) { if (names == null) diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/TSAClientBouncyCastle.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/TSAClientBouncyCastle.cs index 1de953c..f63f1d4 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/TSAClientBouncyCastle.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/TSAClientBouncyCastle.cs @@ -180,7 +180,7 @@ protected internal virtual byte[] GetTsaResponse(byte[] requestBytes) var inp = response.GetResponseStream(); var encoding = response.Headers["Content-Encoding"]; - var baos = new MemoryStream(); + using var baos = new MemoryStream(); var buffer = new byte[1024]; var bytesRead = 0; while ((bytesRead = inp.Read(buffer, 0, buffer.Length)) > 0) diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/Type1Font.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/Type1Font.cs index ccd45cf..36c66a6 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/Type1Font.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/Type1Font.cs @@ -196,7 +196,7 @@ internal Type1Font(string afmFile, string enc, bool emb, byte[] ttfAfm, byte[] p throw new DocumentException(afmFile + " not found as resource."); } - var ostr = new MemoryStream(); + using var ostr = new MemoryStream(); while (true) { var size = istr.Read(buf, 0, buf.Length); @@ -1012,7 +1012,9 @@ protected override int[] GetRawCharBBox(int c, string name) /// a 256 bytes long byte array where each unused byte is represented by 0 /// the indirect reference to a PdfDictionary containing the font descriptor or null /// the PdfDictionary containing the font dictionary - private PdfDictionary getFontBaseType(PdfIndirectReference fontDescriptor, int firstChar, int lastChar, + private PdfDictionary getFontBaseType(PdfIndirectReference fontDescriptor, + int firstChar, + int lastChar, byte[] shortTag) { var dic = new PdfDictionary(PdfName.Font); diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfaForm.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfaForm.cs index 1a96645..0249cdc 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfaForm.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfaForm.cs @@ -41,7 +41,7 @@ public XfaForm(PdfReader reader) } XfaPresent = true; - var bout = new MemoryStream(); + using var bout = new MemoryStream(); if (xfa.IsArray()) { var ar = (PdfArray)xfa; @@ -62,7 +62,7 @@ public XfaForm(PdfReader reader) } bout.Seek(0, SeekOrigin.Begin); - var xtr = XmlReader.Create(bout); + using var xtr = XmlReader.Create(bout); _domDocument = new XmlDocument(); _domDocument.PreserveWhitespace = true; _domDocument.Load(xtr); @@ -171,15 +171,16 @@ public static PdfObject GetXfaObject(PdfReader reader) public static byte[] SerializeDoc(XmlNode n) { var fout = new MemoryStream(); - using (var sw = XmlWriter.Create(fout, new XmlWriterSettings - { - // Specify the encoding manually, so we can ask for the UTF - // identifier to not be included in the output - Encoding = new UTF8Encoding(false), - // We have to omit the XML delcaration, otherwise we'll confuse - // the PDF readers when they try to process the XFA data - OmitXmlDeclaration = true, - })) + using (var sw = XmlWriter.Create(fout, + new XmlWriterSettings + { + // Specify the encoding manually, so we can ask for the UTF + // identifier to not be included in the output + Encoding = new UTF8Encoding(false), + // We have to omit the XML delcaration, otherwise we'll confuse + // the PDF readers when they try to process the XFA data + OmitXmlDeclaration = true, + })) { // We use an XmlSerializer here so that we include the node itself // in the output text ; if we would've used n.WriteContentTo, as @@ -718,7 +719,8 @@ public static string GetShortName(string s) /// the stack with the separeted SOM parts /// the full name public static void InverseSearchAdd(INullValueDictionary inverseSearch, - Stack2 stack, string unstack) + Stack2 stack, + string unstack) { if (inverseSearch == null) { diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfdfReader.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfdfReader.cs index 8e8795f..73fef61 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfdfReader.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfdfReader.cs @@ -66,7 +66,8 @@ public XfdfReader(string filename) /// the byte array with the form public XfdfReader(byte[] xfdfIn) { - SimpleXmlParser.Parse(this, new MemoryStream(xfdfIn)); + using var memoryStream = new MemoryStream(xfdfIn); + SimpleXmlParser.Parse(this, memoryStream); } /// diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/JBIG2SegmentReader.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/JBIG2SegmentReader.cs index ed54171..58fcd51 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/JBIG2SegmentReader.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/JBIG2SegmentReader.cs @@ -99,7 +99,7 @@ public static byte[] CopyByteArray(byte[] b) public byte[] GetGlobal(bool forEmbedding) { - var os = new MemoryStream(); + using var os = new MemoryStream(); try { foreach (Jbig2Segment s in _globals.Keys) @@ -415,7 +415,7 @@ public void AddSegment(Jbig2Segment s) /// a byte array public byte[] GetData(bool forEmbedding) { - var os = new MemoryStream(); + using var os = new MemoryStream(); foreach (int sn in _segs.Keys) { var s = (Jbig2Segment)_segs[sn]; diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/wmf/MetaDo.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/wmf/MetaDo.cs index 0d3bf2d..0f584af 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/wmf/MetaDo.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/codec/wmf/MetaDo.cs @@ -110,7 +110,7 @@ public static byte[] WrapBmp(Image image) if (image.OriginalData == null) { imgIn = image.Url.GetResponseStream(); - var outp = new MemoryStream(); + using var outp = new MemoryStream(); var b = 0; while ((b = imgIn.ReadByte()) != -1) { @@ -126,7 +126,7 @@ public static byte[] WrapBmp(Image image) } var sizeBmpWords = (data.Length - 14 + 1) >> 1; - var os = new MemoryStream(); + using var os = new MemoryStream(); // write metafile header WriteWord(os, 1); WriteWord(os, 9); @@ -496,7 +496,11 @@ public void ReadAll() var r = Meta.ReadShort(); var t = Meta.ReadShort(); var l = Meta.ReadShort(); - Cb.Arc(_state.TransformX(l), _state.TransformY(b), _state.TransformX(r), _state.TransformY(t), 0, + Cb.Arc(_state.TransformX(l), + _state.TransformY(b), + _state.TransformX(r), + _state.TransformY(t), + 0, 360); StrokeAndFill(); break; diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/simpleparser/SimpleXMLParser.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/simpleparser/SimpleXMLParser.cs index 514c92f..97c0d50 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/simpleparser/SimpleXMLParser.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/simpleparser/SimpleXMLParser.cs @@ -278,7 +278,7 @@ public static void Parse(ISimpleXmlDocHandler doc, Stream inp) } else if (encoding.Equals("CP037", StringComparison.Ordinal)) { - var bi = new MemoryStream(); + using var bi = new MemoryStream(); int c; while ((c = inp.ReadByte()) != -1) { @@ -302,7 +302,8 @@ public static void Parse(ISimpleXmlDocHandler doc, Stream inp) } } - Parse(doc, new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding))); + using var streamReader = new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding)); + Parse(doc, streamReader); } public static void Parse(ISimpleXmlDocHandler doc, TextReader r) diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/xmp/XmpReader.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/xmp/XmpReader.cs index 427cd76..3bff866 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/xmp/XmpReader.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/xmp/XmpReader.cs @@ -25,10 +25,10 @@ public XmpReader(byte[] bytes) throw new ArgumentNullException(nameof(bytes)); } - var bout = new MemoryStream(); + using var bout = new MemoryStream(); bout.Write(bytes, 0, bytes.Length); bout.Seek(0, SeekOrigin.Begin); - var xtr = XmlReader.Create(bout); + using var xtr = XmlReader.Create(bout); _domDocument = new XmlDocument(); _domDocument.PreserveWhitespace = true; _domDocument.Load(xtr); @@ -117,7 +117,7 @@ public byte[] SerializeDoc() NewLineChars = "\n", OmitXmlDeclaration = true, }; - var xw = XmlWriter.Create(fout, xwSettings); + using var xw = XmlWriter.Create(fout, xwSettings); var xmlNode = xmpmeta[0]; xmlNode.WriteTo(xw); xw.Flush();