Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class AmazonS3Cache : ImageCacheBase
/// <summary>
/// The regular expression for parsing a remote uri.
/// </summary>
private static readonly Regex RemoteRegex = new Regex("^http(s)?://", RegexOptions.Compiled);
private static readonly Regex RemoteRegex = new Regex("^http(s)?://", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);

/// <summary>
/// The assembly version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AzureBlobCache : ImageCacheBase
/// <summary>
/// The regular expression for parsing a remote uri.
/// </summary>
private static readonly Regex RemoteRegex = new Regex("^http(s)?://", RegexOptions.Compiled);
private static readonly Regex RemoteRegex = new Regex("^http(s)?://", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);

/// <summary>
/// The assembly version.
Expand Down
26 changes: 16 additions & 10 deletions src/ImageProcessor.Web/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ namespace ImageProcessor.Web.Extensions
/// </summary>
public static class StringExtensions
{
/// <summary>
/// The regular expression to search strings for an array of positive floats.
/// </summary>
private static readonly Regex PositiveFloatArrayRegex = new Regex(@"[\d+\.]+(?=[,-])|[\d+\.]+(?![,-])", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);

/// <summary>
/// The regular expression to search strings for an array of positive integers.
/// </summary>
private static readonly Regex PositiveIntegerArrayRegex = new Regex(@"[\d+]+(?=[,-])|[\d+]+(?![,-])", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);

/// <summary>
/// Creates an MD5 fingerprint of the String.
/// </summary>
Expand All @@ -38,8 +48,8 @@ public static string ToMD5Fingerprint(this string expression)
// Concatenate the hash bytes into one long String.
return hash.Aggregate(
new StringBuilder(32),
(sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture)))
.ToString().ToLowerInvariant();
(sb, b) => sb.Append(b.ToString("x2", CultureInfo.InvariantCulture)))
.ToString();
}
}

Expand All @@ -59,8 +69,8 @@ public static string ToSHA1Fingerprint(this string expression)
// Concatenate the hash bytes into one long String.
return hash.Aggregate(
new StringBuilder(40),
(sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture)))
.ToString().ToLowerInvariant();
(sb, b) => sb.Append(b.ToString("x2", CultureInfo.InvariantCulture)))
.ToString();
}
}

Expand All @@ -76,9 +86,7 @@ public static int[] ToPositiveIntegerArray(this string expression)
throw new ArgumentNullException(nameof(expression));
}

var regex = new Regex(@"[\d+]+(?=[,-])|[\d+]+(?![,-])", RegexOptions.Compiled);

MatchCollection matchCollection = regex.Matches(expression);
var matchCollection = PositiveIntegerArrayRegex.Matches(expression);

// Get the collections.
int count = matchCollection.Count;
Expand All @@ -105,9 +113,7 @@ public static float[] ToPositiveFloatArray(this string expression)
throw new ArgumentNullException(nameof(expression));
}

var regex = new Regex(@"[\d+\.]+(?=[,-])|[\d+\.]+(?![,-])", RegexOptions.Compiled);

MatchCollection matchCollection = regex.Matches(expression);
var matchCollection = PositiveFloatArrayRegex.Matches(expression);

// Get the collections.
int count = matchCollection.Count;
Expand Down
55 changes: 30 additions & 25 deletions src/ImageProcessor.Web/Helpers/CommonParameterParserUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class CommonParameterParserUtility
/// <summary>
/// The collection of known colors.
/// </summary>
private static readonly Dictionary<string, KnownColor> KnownColors = new Dictionary<string, KnownColor>();
private static readonly Dictionary<string, KnownColor> KnownColors = new Dictionary<string, KnownColor>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// The regular expression to search strings for colors.
Expand All @@ -34,12 +34,12 @@ public static class CommonParameterParserUtility
/// <summary>
/// The regular expression to search strings for angles.
/// </summary>
private static readonly Regex AngleRegex = new Regex("(^(rotate(bounded)?|angle)|[^.](&,)?rotate(bounded)?|angle)(=|-)[^&|,]+", RegexOptions.Compiled);
private static readonly Regex AngleRegex = new Regex("(^(rotate(bounded)?|angle)|[^.](&,)?rotate(bounded)?|angle)(=|-)[^&|,]+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);

/// <summary>
/// The regular expression to search strings for values between 1 and 100.
/// </summary>
private static readonly Regex In100RangeRegex = new Regex("(-?0*(?:100|[1-9][0-9]?))", RegexOptions.Compiled);
private static readonly Regex In100RangeRegex = new Regex("(-?0*(?:100|[1-9][0-9]?))", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);

/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the angle for the given string.
Expand All @@ -52,15 +52,16 @@ public static class CommonParameterParserUtility
/// </returns>
public static float ParseAngle(string input)
{
foreach (Match match in AngleRegex.Matches(input))
if (AngleRegex.Match(input) is var match && match.Success)
{
// Split on angle
string value = match.Value;
value = match.Value.IndexOf("ANGLE", StringComparison.InvariantCultureIgnoreCase) >= 0
? value.Substring(value.IndexOf("-", StringComparison.Ordinal) + 1)
var value = match.Value;
value = match.Value.IndexOf("angle", StringComparison.OrdinalIgnoreCase) >= 0
? value.Substring(value.IndexOf('-') + 1)
: match.Value.Split('=')[1];

float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out float angle);
float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var angle);

return angle;
}

Expand All @@ -79,22 +80,22 @@ public static float ParseAngle(string input)
/// </returns>
public static Color ParseColor(string input)
{
foreach (Match match in ColorRegex.Matches(input))
if (ColorRegex.Match(input) is var match && match.Success)
{
string value = match.Value;
var value = match.Value;

if (KnownColors.ContainsKey(value))
if (KnownColors.TryGetValue(value, out var knownColor))
{
return Color.FromKnownColor(KnownColors[value]);
return Color.FromKnownColor(knownColor);
}

if (value.Contains(","))
{
int[] split = value.ToPositiveIntegerArray();
byte red = split[0].ToByte();
byte green = split[1].ToByte();
byte blue = split[2].ToByte();
byte alpha = split[3].ToByte();
var split = value.ToPositiveIntegerArray();
var red = split[0].ToByte();
var green = split[1].ToByte();
var blue = split[2].ToByte();
var alpha = split[3].ToByte();

return Color.FromArgb(alpha, red, green, blue);
}
Expand All @@ -117,7 +118,7 @@ public static Color ParseColor(string input)
/// </returns>
public static int ParseIn100Range(string input)
{
int value = 0;
var value = 0;
foreach (Match match in In100RangeRegex.Matches(input))
{
value = int.Parse(match.Value, CultureInfo.InvariantCulture);
Expand All @@ -135,23 +136,27 @@ public static int ParseIn100Range(string input)
private static Regex BuildColorRegex()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(@"(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2}|(");
stringBuilder.Append(@"(\d+,\d+,\d+,\d+|([0-9a-f]{3}){1,2}|(");

var knownColors = (KnownColor[])Enum.GetValues(typeof(KnownColor));

for (int i = 0; i < knownColors.Length; i++)
for (var i = 0; i < knownColors.Length; i++)
{
KnownColor knownColor = knownColors[i];
string name = knownColor.ToString().ToLowerInvariant();
var knownColor = knownColors[i];
var name = knownColor.ToString();

KnownColors.Add(name, knownColor);

stringBuilder.Append(i > 0 ? "|" + name : name);
if (i > 0)
{
stringBuilder.Append('|');
}

stringBuilder.Append(Regex.Escape(name));
}

stringBuilder.Append("))");

return new Regex(stringBuilder.ToString(), RegexOptions.IgnoreCase);
return new Regex(stringBuilder.ToString(), RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
}
}
}
28 changes: 6 additions & 22 deletions src/ImageProcessor.Web/Helpers/ImageHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public ImageHelpers()
/// <summary>
/// The image format regex.
/// </summary>
private static readonly Regex FormatRegex = new Regex(@"(\.?)(png8|" + ExtensionRegexPattern + ")", RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
private static readonly Regex FormatRegex = new Regex(@"(\.?)(png8|" + ExtensionRegexPattern + ")", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.RightToLeft);

/// <summary>
/// The image format regex for matching the file format at the end of a string.
/// </summary>
private static readonly Regex EndFormatRegex = new Regex(@"(\.)" + ExtensionRegexPattern + "$", RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
private static readonly Regex EndFormatRegex = new Regex(@"(\.)" + ExtensionRegexPattern + "$", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.RightToLeft);

/// <summary>
/// Checks a given string to check whether the value contains a valid image extension.
Expand Down Expand Up @@ -132,7 +132,7 @@ public string GetExtension(string fullPath, string queryString, string defaultEx
}

// Ah the enigma that is the png file.
if (value.EndsWith("png8", StringComparison.InvariantCultureIgnoreCase))
if (value.EndsWith("png8", StringComparison.OrdinalIgnoreCase))
{
return "png";
}
Expand Down Expand Up @@ -180,26 +180,10 @@ internal string GetContentTypeForExtension(string extension)
private static string BuildExtensionRegexPattern()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("(");
int counter = 0;
foreach (ISupportedImageFormat imageFormat in ImageProcessorBootstrapper.Instance.SupportedImageFormats)
{
foreach (string fileExtension in imageFormat.FileExtensions)
{
if (counter == 0)
{
stringBuilder.Append(fileExtension.ToLowerInvariant());
}
else
{
stringBuilder.AppendFormat("|{0}", fileExtension.ToLowerInvariant());
}
}

counter++;
}
stringBuilder.Append('(');
stringBuilder.Append(string.Join("|", ImageProcessorBootstrapper.Instance.SupportedImageFormats.SelectMany(f => f.FileExtensions).Select(Regex.Escape)));
stringBuilder.Append(')');

stringBuilder.Append(")");
return stringBuilder.ToString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public class ColorTypeConverter : QueryParamConverter
/// <summary>
/// The web color regex.
/// </summary>
private static readonly Regex HexColorRegex = new Regex("([0-9a-fA-F]{3}){1,2}", RegexOptions.Compiled);
private static readonly Regex HexColorRegex = new Regex("([0-9a-fA-F]{3}){1,2}", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);

/// <summary>
/// The number color regex.
/// </summary>
private static readonly Regex NumberRegex = new Regex(@"\d+", RegexOptions.Compiled);
private static readonly Regex NumberRegex = new Regex(@"\d+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);

/// <summary>
/// The system color table map.
Expand Down Expand Up @@ -230,34 +230,31 @@ public override object ConvertTo(CultureInfo culture, object value, Type destina
throw new ArgumentNullException(nameof(destinationType));
}

if (destinationType == typeof(string))
if (destinationType == typeof(string) && value != null)
{
if (value != null)
{
var color = (Color)value;

if (color == Color.Empty)
{
return string.Empty;
}
var color = (Color)value;

if (color.IsKnownColor)
{
return color.Name;
}
if (color == Color.Empty)
{
return string.Empty;
}

if (color.IsNamedColor)
{
return "'" + color.Name + "'";
}
if (color.IsKnownColor)
{
return color.Name;
}

// In the Web scenario, colors should be formatted in #RRGGBB notation
var sb = new StringBuilder("#", 7);
sb.Append(color.R.ToString("X2", CultureInfo.InvariantCulture));
sb.Append(color.G.ToString("X2", CultureInfo.InvariantCulture));
sb.Append(color.B.ToString("X2", CultureInfo.InvariantCulture));
return sb.ToString();
if (color.IsNamedColor)
{
return "'" + color.Name + "'";
}

// In the Web scenario, colors should be formatted in #RRGGBB notation
var sb = new StringBuilder("#", 7);
sb.Append(color.R.ToString("X2", CultureInfo.InvariantCulture));
sb.Append(color.G.ToString("X2", CultureInfo.InvariantCulture));
sb.Append(color.B.ToString("X2", CultureInfo.InvariantCulture));
return sb.ToString();
}

return base.ConvertTo(culture, value, destinationType);
Expand Down
2 changes: 1 addition & 1 deletion src/ImageProcessor.Web/Helpers/UrlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void ParseUrl(string url, string servicePrefix, out string request
if (!string.IsNullOrWhiteSpace(servicePrefix))
{
// Handle when prefix is contained in filename.
string[] split = Regex.Split(url, Regex.Escape(servicePrefix), RegexOptions.IgnoreCase);
string[] split = Regex.Split(url, Regex.Escape(servicePrefix), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
if (split.Length > 1)
{
url = string.Join(servicePrefix, split.Skip(1).ToArray()).TrimStart("?");
Expand Down
Loading