Skip to content

Commit

Permalink
Modernize code, fix typos and add ALT tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees committed Sep 9, 2023
1 parent c76d9ef commit ea8bc0f
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 268 deletions.
9 changes: 9 additions & 0 deletions MsgReader.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MR/@EntryIndexedValue">MR</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NL/@EntryIndexedValue">NL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RDN/@EntryIndexedValue">RDN</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=abcdef/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ADDRTYPE/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=APPTIME/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Besm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=BIDI/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=buptim/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=cdet/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=CFWS/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=CLSID/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=cmma/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=CNSM/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Consts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=creatim/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=dddd/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Decompressor/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=DFFF/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=DONT/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=encounterd/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=endian/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=esoterical/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Eucjp/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Eucjpsm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Euckr/@EntryIndexedValue">True</s:Boolean>
Expand Down Expand Up @@ -71,6 +75,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=OXCRPC/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=OXOCAL/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=OXPROPS/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=PARM/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pettazzi/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pkcs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Prebuf/@EntryIndexedValue">True</s:Boolean>
Expand All @@ -81,6 +86,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ptyp/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=rdquo/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=redirectedfrom/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Resnick/@EntryIndexedValue">True</s:Boolean>

<s:Boolean x:Key="/Default/UserDictionary/Words/=revtim/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=rsquo/@EntryIndexedValue">True</s:Boolean>
Expand All @@ -91,6 +97,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sjis/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sjissm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=SMIME/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=SPAC/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Spelde/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=splitted/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=srcdoc/@EntryIndexedValue">True</s:Boolean>
Expand All @@ -102,6 +109,8 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=TTYTDD/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=twips/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unsendable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=us_0027special/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=UUENCODE/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Winamp/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=winmail/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>
8 changes: 4 additions & 4 deletions MsgReaderCore/Mime/Decode/Base64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static byte[] Decode(string base64Encoded)
catch (Exception)
{
Logger.WriteToLog("Base64 decoding still failed returning empty byte array");
return new byte[0];
return Array.Empty<byte>();
}
}
}
Expand Down Expand Up @@ -82,9 +82,9 @@ private static string RemoveInvalidBase64Chars(string base64Encoded)
foreach (var chr in base64Encoded)
{
var val = (int)chr;
if ((val >= 65 && val <= 90) || // 'A'..'Z'
(val >= 97 && val <= 122) || // 'a'..'z'
(val >= 48 && val <= 57) || // '0'..'9'
if (val is >= 65 and <= 90 || // 'A'..'Z'
val is >= 97 and <= 122 || // 'a'..'z'
val is >= 48 and <= 57 || // '0'..'9'
val == 43 || val == 47) // '+' and '/'
result.Append(chr);
else
Expand Down
2 changes: 1 addition & 1 deletion MsgReaderCore/Mime/Decode/EncodedWord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static string Decode(string encodedWords)
break;

default:
throw new ArgumentException("The encoding " + encoding + " was not recognized");
throw new ArgumentException($"The encoding {encoding} was not recognized");
}

// Replace our encoded value with our decoded value
Expand Down
10 changes: 5 additions & 5 deletions MsgReaderCore/Mime/Decode/EncodingFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static class EncodingFinder
public static FallbackDecoderDelegate FallbackDecoder { private get; set; }

/// <summary>
/// Mapping from charactersets to encodings.
/// Mapping from character sets to encodings.
/// </summary>
private static Dictionary<string, Encoding> EncodingMap { get; set; }
#endregion
Expand All @@ -69,7 +69,7 @@ static EncodingFinder()

#region Reset
/// <summary>
/// Used to reset this static class to facilite isolated unit testing.
/// Used to reset this static class to facilitate isolated unit testing.
/// </summary>
internal static void Reset()
{
Expand Down Expand Up @@ -97,8 +97,8 @@ internal static Encoding FindEncoding(string characterSet)
var charSetUpper = characterSet.ToUpperInvariant();

// Check if the characterSet is explicitly mapped to an encoding
if (EncodingMap.ContainsKey(charSetUpper))
return EncodingMap[charSetUpper];
if (EncodingMap.TryGetValue(charSetUpper, out var encoding))
return encoding;

// Try to generally find the encoding
try
Expand Down Expand Up @@ -133,7 +133,7 @@ internal static Encoding FindEncoding(string characterSet)
if (fallbackDecoderResult != null)
return fallbackDecoderResult;

// If no solution was found, throw catched exception
// If no solution was found, throw exception
throw;
}
}
Expand Down
94 changes: 46 additions & 48 deletions MsgReaderCore/Mime/Decode/QuotedPrintable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,52 +79,50 @@ private static byte[] Rfc2047QuotedPrintableDecode(string toDecode, bool encoded
throw new ArgumentNullException(nameof(toDecode));

// Create a byte array builder which is roughly equivalent to a StringBuilder
using (var byteArrayBuilder = StreamHelpers.Manager.GetStream())
{
// Remove illegal control characters
toDecode = RemoveIllegalControlCharacters(toDecode);
using var byteArrayBuilder = StreamHelpers.Manager.GetStream();
// Remove illegal control characters
toDecode = RemoveIllegalControlCharacters(toDecode);

// Run through the whole string that needs to be decoded
for (var i = 0; i < toDecode.Length; i++)
// Run through the whole string that needs to be decoded
for (var i = 0; i < toDecode.Length; i++)
{
var currentChar = toDecode[i];
if (currentChar == '=')
{
var currentChar = toDecode[i];
if (currentChar == '=')
// Check that there is at least two characters behind the equal sign
if (toDecode.Length - i < 3)
{
// Check that there is at least two characters behind the equal sign
if (toDecode.Length - i < 3)
{
// We are at the end of the toDecode string, but something is missing. Handle it the way RFC 2045 states
WriteAllBytesToStream(byteArrayBuilder, DecodeEqualSignNotLongEnough(toDecode.Substring(i)));

// Since it was the last part, we should stop parsing anymore
break;
}

// Decode the Quoted-Printable part
var quotedPrintablePart = toDecode.Substring(i, 3);
WriteAllBytesToStream(byteArrayBuilder, DecodeEqualSign(quotedPrintablePart));

// We now consumed two extra characters. Go forward two extra characters
i += 2;
// We are at the end of the toDecode string, but something is missing. Handle it the way RFC 2045 states
WriteAllBytesToStream(byteArrayBuilder, DecodeEqualSignNotLongEnough(toDecode.Substring(i)));

// Since it was the last part, we should stop parsing anymore
break;
}

// Decode the Quoted-Printable part
var quotedPrintablePart = toDecode.Substring(i, 3);
WriteAllBytesToStream(byteArrayBuilder, DecodeEqualSign(quotedPrintablePart));

// We now consumed two extra characters. Go forward two extra characters
i += 2;
}
else
{
// This character is not quoted printable hex encoded.

// Could it be the _ character, which represents space
// and are we using the encoded word variant of QuotedPrintable
if (currentChar == '_' && encodedWordVariant)
// The RFC specifies that the "_" always represents hexadecimal 20 even if the
// SPACE character occupies a different code position in the character set in use.
byteArrayBuilder.WriteByte(0x20);
else
{
// This character is not quoted printable hex encoded.

// Could it be the _ character, which represents space
// and are we using the encoded word variant of QuotedPrintable
if (currentChar == '_' && encodedWordVariant)
// The RFC specifies that the "_" always represents hexadecimal 20 even if the
// SPACE character occupies a different code position in the character set in use.
byteArrayBuilder.WriteByte(0x20);
else
// This is not encoded at all. This is a literal which should just be included into the output.
byteArrayBuilder.WriteByte((byte)currentChar);
}
// This is not encoded at all. This is a literal which should just be included into the output.
byteArrayBuilder.WriteByte((byte)currentChar);
}

return byteArrayBuilder.ToArray();
}

return byteArrayBuilder.ToArray();
}
#endregion

Expand Down Expand Up @@ -162,7 +160,7 @@ private static string RemoveIllegalControlCharacters(string input)
throw new ArgumentNullException(nameof(input));

// First we remove any \r or \n which is not part of a \r\n pair
input = RemoveCarriageReturnAndNewLinewIfNotInPair(input);
input = RemoveCarriageReturnAndNewLineIfNotInPair(input);

// Here only legal \r\n is left over
// We now simply keep them, and the \t which is also allowed
Expand All @@ -173,14 +171,14 @@ private static string RemoveIllegalControlCharacters(string input)
}
#endregion

#region RemoveCarriageReturnAndNewLinewIfNotInPair
#region RemoveCarriageReturnAndNewLineIfNotInPair
/// <summary>
/// This method will remove any \r and \n which is not paired as \r\n
/// </summary>
/// <param name="input">String to remove lonely \r and \n's from</param>
/// <returns>A string without lonely \r and \n's</returns>
/// <exception cref="ArgumentNullException">If <paramref name="input" /> is <see langword="null" /></exception>
private static string RemoveCarriageReturnAndNewLinewIfNotInPair(string input)
private static string RemoveCarriageReturnAndNewLineIfNotInPair(string input)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
Expand Down Expand Up @@ -263,7 +261,7 @@ private static byte[] DecodeEqualSignNotLongEnough(string decode)
throw new ArgumentException(@"First part of decode must be an equal sign", nameof(decode));

// We will now believe that the string sent to us, was actually not encoded
// Therefore it must be in US-ASCII and we will return the bytes it corrosponds to
// Therefore it must be in US-ASCII and we will return the bytes it corresponds to
return Encoding.ASCII.GetBytes(decode);
}
#endregion
Expand Down Expand Up @@ -297,14 +295,14 @@ private static byte[] DecodeEqualSign(string decode)
// It might be a
// - hex-string like =3D, denoting the character with hex value 3D
// - it might be the last character on the line before a CRLF
// pair, denoting a soft linebreak, which simply
// pair, denoting a soft line break, which simply
// splits the text up, because of the 76 chars per line restriction
if (decode.Contains("\r\n"))
// Soft break detected
// We want to return string.Empty which is equivalent to a zero-length byte array
return new byte[0];
return Array.Empty<byte>();

// Hex string detected. Convertion needed.
// Hex string detected. Conversion needed.
// It might be that the string located after the equal sign is not hex characters
// An example: =JU
// In that case we would like to catch the FormatException and do something else
Expand Down Expand Up @@ -338,12 +336,12 @@ private static byte[] DecodeEqualSign(string decode)
// the data.

// So we choose to believe this is actually an un-encoded string
// Therefore it must be in US-ASCII and we will return the bytes it corrosponds to
// Therefore it must be in US-ASCII and we will return the bytes it corresponds to
return Encoding.ASCII.GetBytes(decode);
}
catch (Exception)
{
return new byte[0];
return Array.Empty<byte>();
}
}
#endregion
Expand Down
6 changes: 3 additions & 3 deletions MsgReaderCore/Mime/Decode/Rfc2231Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
// ReSharper disable CommentTypo

namespace MsgReader.Mime.Decode;

Expand Down Expand Up @@ -213,7 +214,7 @@ private static List<KeyValuePair<string, string>> DecodePairs(IList<KeyValuePair
if (jKey.Equals(key + "*" + continuationCount))
{
// This value part of the continuation is not encoded
// Therefore remove qoutes if any and add to our stringbuilder
// Therefore remove quotes if any and add to our string builder
builder.Append(valueJKey);

// Remember to increment i, as we have now treated one more KeyValuePair
Expand Down Expand Up @@ -258,8 +259,7 @@ private static List<KeyValuePair<string, string>> DecodePairs(IList<KeyValuePair
key = key.Replace("*", "");

// Decode the value
string throwAway;
value = DecodeSingleValue(value, out throwAway);
value = DecodeSingleValue(value, out _);

// Now input the new value with the new key
resultPairs.Add(new KeyValuePair<string, string>(key, value));
Expand Down
4 changes: 2 additions & 2 deletions MsgReaderCore/Mime/Decode/Rfc2822DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,12 @@ private static DateTime ExtractDateTime(string dateInput)
// Needs to find: 21 Nov 1997 09:55:06

// Seconds does not need to be specified
// Even though it is illigal, sometimes hours, minutes or seconds are only specified with one digit
// Even though it is illegal, sometimes hours, minutes or seconds are only specified with one digit

// Year with 2 or 4 digits (1922 or 22)
const string year = @"(\d\d\d\d|\d\d)";

// Time with one or two digits for hour and minute and optinal seconds (06:04:06 or 6:4:6 or 06:04 or 6:4)
// Time with one or two digits for hour and minute and optional seconds (06:04:06 or 6:4:6 or 06:04 or 6:4)
const string time = @"\d?\d:\d?\d(:\d?\d)?";

// Correct format is 21 Nov 1997 09:55:06
Expand Down
2 changes: 1 addition & 1 deletion MsgReaderCore/Mime/Decode/SizeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static string ExtractUnit(string sizeWithUnit)
private static bool IsDigit(char value)
{
// we don't want to use char.IsDigit since it would accept esoterical unicode digits
return value >= '0' && value <= '9';
return value is >= '0' and <= '9';
}
#endregion

Expand Down
Loading

0 comments on commit ea8bc0f

Please sign in to comment.