Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Code Quality 2 #799

Merged
merged 6 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/AcroForms/AcroFormFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private AcroFieldBase GetAcroField(DictionaryToken fieldDictionary, Catalog cata
var fieldFlags = (uint) (fieldFlagsToken?.Long ?? 0);

AcroFieldBase result;
if (fieldType == null)
if (fieldType is null)
{
result = new AcroNonTerminalField(fieldDictionary, "Non-Terminal Field", fieldFlags, information, AcroFieldType.Unknown, children);
}
Expand Down Expand Up @@ -562,7 +562,7 @@ private static bool IsChoiceSelected(IReadOnlyList<string> selectedOptionNames,
continue;
}

if (selectedOptionIndices == null)
if (selectedOptionIndices is null)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override string ToString()
{
string AppendIfNotNull(string val, string label, string result)
{
if (val == null)
if (val is null)
{
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Annotations/AppearanceStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal AppearanceStream(IDictionary<string, StreamToken> appearanceStreamsBySt
/// <exception cref="ArgumentOutOfRangeException"></exception>
public StreamToken Get(string state)
{
if (appearanceStreamsByState == null)
if (appearanceStreamsByState is null)
{
throw new Exception("Cannot get appearance by state when this is a stateless appearance stream");
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Annotations/QuadPointsQuadrilateral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class QuadPointsQuadrilateral
/// </summary>
public QuadPointsQuadrilateral(IReadOnlyList<PdfPoint> points)
{
if (points == null)
if (points is null)
{
throw new ArgumentNullException(nameof(points));
}
Expand Down
10 changes: 5 additions & 5 deletions src/UglyToad.PdfPig/Content/BasePageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected BasePageFactory(
public TPage Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers,
NamedDestinations namedDestinations)
{
if (dictionary == null)
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public TPage Create(int number, DictionaryToken dictionary, PageTreeMembers page

var contentStream = DirectObjectFinder.Get<StreamToken>(obj, PdfScanner);

if (contentStream == null)
if (contentStream is null)
{
throw new InvalidOperationException($"Could not find the contents for object {obj}.");
}
Expand All @@ -150,7 +150,7 @@ public TPage Create(int number, DictionaryToken dictionary, PageTreeMembers page
{
var contentStream = DirectObjectFinder.Get<StreamToken>(contents, PdfScanner);

if (contentStream == null)
if (contentStream is null)
{
throw new InvalidOperationException("Failed to parse the content for the page: " + number);
}
Expand Down Expand Up @@ -181,7 +181,7 @@ private TPage ProcessPageInternal(
{
IReadOnlyList<IGraphicsStateOperation> operations;

if (contentBytes == null || contentBytes.Count == 0)
if (contentBytes is null || contentBytes.Count == 0)
{
operations = Array.Empty<IGraphicsStateOperation>();
}
Expand Down Expand Up @@ -293,7 +293,7 @@ protected MediaBox GetMediaBox(int number, DictionaryToken dictionary, PageTreeM
{
mediaBox = pageTreeMembers.MediaBox;

if (mediaBox == null)
if (mediaBox is null)
{
ParsingOptions.Logger.Error(
$"The MediaBox was the wrong missing for page {number}. Using US Letter.");
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/DocumentInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public override string ToString()

private static void AppendPart(string name, string value, StringBuilder builder)
{
if (value == null)
if (value is null)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/InlineImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal InlineImage(PdfRectangle bounds, int widthInSamples, int heightInSample
public bool TryGetBytes(out IReadOnlyList<byte> bytes)
{
bytes = null;
if (bytesFactory == null)
if (bytesFactory is null)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal Page(int number, DictionaryToken dictionary, MediaBox mediaBox, CropBox

private static string GetText(PageContent content)
{
if (content?.Letters == null)
if (content?.Letters is null)
{
return string.Empty;
}
Expand Down
20 changes: 7 additions & 13 deletions src/UglyToad.PdfPig/Content/PageRotationDegrees.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,13 @@ public double Radians
{
get
{
switch (Value)
{
case 0:
return 0;
case 90:
return -0.5 * Math.PI;
case 180:
return -Math.PI;
case 270:
return -1.5 * Math.PI;
default:
throw new InvalidOperationException($"Invalid value for rotation: {Value}.");
}
return Value switch {
0 => 0,
90 => -0.5 * Math.PI,
180 => -Math.PI,
270 => -1.5 * Math.PI,
_ => throw new InvalidOperationException($"Invalid value for rotation: {Value}.")
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/PageTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class PageTreeNode
/// <summary>
/// Whether this node is the root node.
/// </summary>
public bool IsRoot => Parent == null;
public bool IsRoot => Parent is null;

/// <summary>
/// Create a new <see cref="PageTreeNode"/>.
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/Pages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ internal void AddPageFactory<TPage, TPageFactory>() where TPageFactory : IPageFa
typeof(ParsingOptions)
});

if (constructor == null)
if (constructor is null)
{
throw new InvalidOperationException($"Could not find valid constructor for page factory of type '{typeof(TPageFactory)}'. " +
"The page factory should have a constructor with the following parameters: " +
Expand Down
6 changes: 3 additions & 3 deletions src/UglyToad.PdfPig/Content/ResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void LoadFontDictionary(DictionaryToken fontDictionary)

var fontObject = DirectObjectFinder.Get<DictionaryToken>(objectKey, scanner);

if (fontObject == null)
if (fontObject is null)
{
//This is a valid use case
continue;
Expand Down Expand Up @@ -269,7 +269,7 @@ public bool TryGetNamedColorSpace(NameToken name, out ResourceColorSpace namedTo
{
namedToken = default(ResourceColorSpace);

if (name == null)
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
Expand All @@ -286,7 +286,7 @@ public bool TryGetNamedColorSpace(NameToken name, out ResourceColorSpace namedTo

public ColorSpaceDetails GetColorSpaceDetails(NameToken name, DictionaryToken dictionary)
{
if (dictionary == null)
if (dictionary is null)
{
dictionary = new DictionaryToken(new Dictionary<NameToken, IToken>());
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/Word.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Word
/// <param name="letters">The letters contained in the word, in the correct order.</param>
public Word(IReadOnlyList<Letter> letters)
{
if (letters == null)
if (letters is null)
{
throw new ArgumentNullException(nameof(letters));
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/CrossReference/CrossReferenceTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal CrossReferenceTable(CrossReferenceType type, IReadOnlyDictionary<Indire
TrailerDictionary trailer,
IReadOnlyList<CrossReferenceOffset> crossReferenceOffsets)
{
if (objectOffsets == null)
if (objectOffsets is null)
{
throw new ArgumentNullException(nameof(objectOffsets));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class CrossReferenceTableBuilder

public void Add(CrossReferenceTablePart part)
{
if (part == null)
if (part is null)
{
throw new ArgumentNullException(nameof(part));
}
Expand All @@ -38,7 +38,7 @@ public CrossReferenceTable Build(long firstCrossReferenceOffset, long offsetCorr

var currentPart = parts.FirstOrDefault(x => x.Offset == firstCrossReferenceOffset);

if (currentPart == null)
if (currentPart is null)
{
// no XRef at given position
log.Warn($"Did not find an XRef object at the specified startxref position {firstCrossReferenceOffset}");
Expand Down Expand Up @@ -73,7 +73,7 @@ public CrossReferenceTable Build(long firstCrossReferenceOffset, long offsetCorr
}

currentPart = parts.FirstOrDefault(x => x.Offset == prevBytePos || x.Offset == prevBytePos + offsetCorrection);
if (currentPart == null)
if (currentPart is null)
{
log.Warn("Did not found XRef object pointed to by 'Prev' key at position " + prevBytePos);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class TrailerDictionary
/// <param name="dictionary">The parsed dictionary from the document.</param>
internal TrailerDictionary(DictionaryToken dictionary)
{
if (dictionary == null)
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
Expand Down
6 changes: 3 additions & 3 deletions src/UglyToad.PdfPig/Encryption/CryptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ internal class CryptHandler
public CryptHandler(DictionaryToken cryptDictionary,
NameToken streamName, NameToken stringName)
{
if (streamName == null)
if (streamName is null)
{
throw new ArgumentNullException(nameof(streamName));
}

if (stringName == null)
if (stringName is null)
{
throw new ArgumentNullException(nameof(stringName));
}
Expand All @@ -32,7 +32,7 @@ public CryptHandler(DictionaryToken cryptDictionary,

public CryptDictionary GetNamedCryptDictionary(NameToken name)
{
if (name == null)
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static class EncryptionDictionaryFactory
{
public static EncryptionDictionary Read(DictionaryToken encryptionDictionary, IPdfTokenScanner tokenScanner)
{
if (encryptionDictionary == null)
if (encryptionDictionary is null)
{
throw new ArgumentNullException(nameof(encryptionDictionary));
}
Expand Down
49 changes: 15 additions & 34 deletions src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,18 @@ public EncryptionHandler(EncryptionDictionary encryptionDictionary, TrailerDicti
{
var token = trailerDictionary.Identifier[0];

switch (token)
{
case HexToken hex:
documentIdBytes = hex.Bytes.ToArray();
break;
case StringToken str:
documentIdBytes = str.GetBytes();
break;
default:
documentIdBytes = OtherEncodings.StringAsLatin1Bytes(token.Data);
break;
}

documentIdBytes = token switch {
HexToken hex => hex.Bytes.ToArray(),
StringToken str => str.GetBytes(),
_ => OtherEncodings.StringAsLatin1Bytes(token.Data)
};
}
else
{
documentIdBytes = [];
}

if (encryptionDictionary == null)
if (encryptionDictionary is null)
{
return;
}
Expand Down Expand Up @@ -337,7 +329,7 @@ private static bool IsOwnerPasswordRevision5And6(byte[] passwordBytes, Encryptio

public IToken Decrypt(IndirectReference reference, IToken token)
{
if (token == null)
if (token is null)
{
throw new ArgumentNullException(nameof(token));
}
Expand Down Expand Up @@ -736,7 +728,7 @@ private static void UpdateMd5(MD5 md5, byte[] data)

private static byte[] GetPaddedPassword(byte[] password)
{
if (password == null || password.Length == 0)
if (password is null || password.Length == 0)
{
return PaddingBytes;
}
Expand Down Expand Up @@ -779,7 +771,7 @@ private static byte[] TruncatePasswordTo127Bytes(byte[] password)
private static byte[] ComputeStupidIsoHash(byte[] password, byte[] salt, byte[] vector)
{
// There are some details here https://web.archive.org/web/20180311160224/esec-lab.sogeti.com/posts/2011/09/14/the-undocumented-password-validation-algorithm-of-adobe-reader-x.html
if (vector == null)
if (vector is null)
{
vector = [];
}
Expand Down Expand Up @@ -847,23 +839,12 @@ private static byte[] ComputeStupidIsoHash(byte[] password, byte[] salt, byte[]

var sumOfFirstSixteenBytesOfX = x.Take(16).Sum(v => (long)v);
var mod3 = sumOfFirstSixteenBytesOfX % 3;

HashAlgorithm nextHash;
switch (mod3)
{
case 0:
nextHash = SHA256.Create();
break;
case 1:
nextHash = SHA384.Create();
break;
case 2:
nextHash = SHA512.Create();
break;
default:
throw new PdfDocumentEncryptedException("Invalid remainder from summing first sixteen bytes of this round's hash.");
}

HashAlgorithm nextHash = mod3 switch {
0 => SHA256.Create(),
1 => SHA384.Create(),
2 => SHA512.Create(),
_ => throw new PdfDocumentEncryptedException("Invalid remainder from summing first sixteen bytes of this round's hash.")
};
input = nextHash.ComputeHash(x);
Array.Copy(input, key, 16);
Array.Copy(input, 16, iv, 0, 16);
Expand Down
Loading
Loading