diff --git a/src/FileSizeFromBase64.NET/FileSizeFromBase64.NET.csproj b/src/FileSizeFromBase64.NET/FileSizeFromBase64.NET.csproj
index 903bddd..87ff13e 100644
--- a/src/FileSizeFromBase64.NET/FileSizeFromBase64.NET.csproj
+++ b/src/FileSizeFromBase64.NET/FileSizeFromBase64.NET.csproj
@@ -1,40 +1,40 @@
-
- netstandard2.1
- FileSizeFromBase64.NET
- FileSizeFromBase64.NET
- true
- Kodjo Laurent Egbakou
- Get file size from base64 string
- nuget.md
- https://github.com/lioncoding-oss/FileSizeFromBase64.NET
- https://github.com/lioncoding-oss/FileSizeFromBase64.NET
- logo.png
- 2.0.1
- See: https://github.com/lioncoding-oss/FileSizeFromBase64.NET
- FileSize, Base64, Paddings, MIME-type
- Copyright 2026
- MIT
- en
-
+
+ netstandard2.1
+ FileSizeFromBase64.NET
+ FileSizeFromBase64.NET
+ true
+ Kodjo Laurent Egbakou
+ Get file size from base64 string
+ nuget.md
+ https://github.com/lioncoding-oss/FileSizeFromBase64.NET
+ https://github.com/lioncoding-oss/FileSizeFromBase64.NET
+ logo.png
+ 2.0.1
+ See: https://github.com/lioncoding-oss/FileSizeFromBase64.NET
+ FileSize, Base64, Paddings, MIME-type
+ Copyright 2026
+ MIT
+ en
+
-
- true
- bin\Debug
- DEBUG
-
-
- true
- portable
- true
- bin\Release\FileSizeFromBase64.NET.xml
-
+
+ true
+ bin\Debug
+ DEBUG
+
+
+ true
+ portable
+ true
+ bin\Release\FileSizeFromBase64.NET.xml
+
-
-
-
-
-
+
+
+
+
+
-
+
\ No newline at end of file
diff --git a/src/FileSizeFromBase64.NET/FileSizeHelpers.cs b/src/FileSizeFromBase64.NET/FileSizeHelpers.cs
index 0569cb1..b8c3de2 100644
--- a/src/FileSizeFromBase64.NET/FileSizeHelpers.cs
+++ b/src/FileSizeFromBase64.NET/FileSizeHelpers.cs
@@ -14,25 +14,35 @@ public static class FileSizeHelpers
/// Indicate if the padding management is required or not. Default is false
/// The unit of measure of the file size returned by the method. The default unit of measure is Byte.
/// The size of the file represented by the base64 string.
- public static double GetFileSizeFromBase64String(string base64String, bool applyPaddingsRules = false, UnitsOfMeasurement unitsOfMeasurement = UnitsOfMeasurement.Byte)
+ public static double GetFileSizeFromBase64String(
+ string base64String,
+ bool applyPaddingsRules = false,
+ UnitsOfMeasurement unitsOfMeasurement = UnitsOfMeasurement.Byte)
{
- if (string.IsNullOrEmpty(base64String)) return 0;
+ if (string.IsNullOrEmpty(base64String)) return 0d;
- var base64Length = base64String
- .AsSpan()
- [(base64String.IndexOf(',', StringComparison.Ordinal) + 1)..]
- .Length;
+ ReadOnlySpan base64ReadOnlySpan = base64String.AsSpan();
+
+ // RFC 2397 https://www.rfc-editor.org/rfc/rfc2397.html data:[][;base64],
+ if (base64ReadOnlySpan.StartsWith("data:".AsSpan()))
+ {
+ var commaIndex = base64String.IndexOf(',');
+ if (commaIndex >= 0)
+ base64ReadOnlySpan = base64ReadOnlySpan[(commaIndex + 1)..];
+ }
+
+ var base64Length = base64ReadOnlySpan.Length;
var fileSizeInByte = Math.Ceiling((double)base64Length / 4) * 3;
if (applyPaddingsRules && base64Length >= 2)
{
- ReadOnlySpan paddings = base64String.AsSpan()[^2..];
+ ReadOnlySpan paddings = base64ReadOnlySpan[^2..];
fileSizeInByte = paddings switch
{
- _ when paddings.Equals("==", StringComparison.Ordinal) => fileSizeInByte - 2,
- _ when paddings[1].Equals('=') => fileSizeInByte - 1,
+ _ when paddings[0] == '=' && paddings[1] == '=' => fileSizeInByte - 2,
+ _ when paddings[1] == '=' => fileSizeInByte - 1,
_ => fileSizeInByte
};
}
@@ -40,25 +50,4 @@ _ when paddings[1].Equals('=') => fileSizeInByte - 1,
return fileSizeInByte > 0 ? fileSizeInByte / (int)unitsOfMeasurement : 0;
}
}
-
- ///
- /// Unit of measurement.
- ///
- public enum UnitsOfMeasurement
- {
- ///
- /// B.
- ///
- Byte = 1,
-
- ///
- /// KB.
- ///
- KiloByte = 1_024,
-
- ///
- /// MB.
- ///
- MegaByte = 1_048_576
- }
}
\ No newline at end of file
diff --git a/src/FileSizeFromBase64.NET/UnitsOfMeasurement.cs b/src/FileSizeFromBase64.NET/UnitsOfMeasurement.cs
new file mode 100644
index 0000000..7934cef
--- /dev/null
+++ b/src/FileSizeFromBase64.NET/UnitsOfMeasurement.cs
@@ -0,0 +1,22 @@
+namespace FileSizeFromBase64.NET;
+
+///
+/// Unit of measurement.
+///
+public enum UnitsOfMeasurement
+{
+ ///
+ /// B.
+ ///
+ Byte = 1,
+
+ ///
+ /// KB.
+ ///
+ KiloByte = 1_024,
+
+ ///
+ /// MB.
+ ///
+ MegaByte = 1_048_576
+}
\ No newline at end of file
diff --git a/tests/FileSizeFromBase64.Benchmarks/FileSizeFromBase64.Benchmarks.csproj b/tests/FileSizeFromBase64.Benchmarks/FileSizeFromBase64.Benchmarks.csproj
index a32b62f..cb66cf3 100644
--- a/tests/FileSizeFromBase64.Benchmarks/FileSizeFromBase64.Benchmarks.csproj
+++ b/tests/FileSizeFromBase64.Benchmarks/FileSizeFromBase64.Benchmarks.csproj
@@ -1,20 +1,24 @@
-
- Exe
- net10.0
- false
- enable
-
+
+ Exe
+ net10.0
+ false
+ enable
+
-
-
-
-
+
+
+
-
-
- Always
-
-
-
+
+
+ Always
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tests/FileSizeFromBase64.Benchmarks/FileSizeHelpersBenchmark.cs b/tests/FileSizeFromBase64.Benchmarks/FileSizeHelpersBenchmark.cs
index 771a3f5..34b8117 100644
--- a/tests/FileSizeFromBase64.Benchmarks/FileSizeHelpersBenchmark.cs
+++ b/tests/FileSizeFromBase64.Benchmarks/FileSizeHelpersBenchmark.cs
@@ -10,7 +10,8 @@
namespace FileSizeFromBase64.Benchmarks;
-[Config(typeof(Config))]
+[MediumRunJob]
+[MemoryDiagnoser]
public class FileSizeHelpersBenchmark
{
private sealed class Config : ManualConfig
@@ -21,6 +22,7 @@ public Config()
AddJob(baseJob.WithNuGet("FileSizeFromBase64.NET", "1.0.0").WithId("v1.0.0").WithBaseline(true));
AddJob(baseJob.WithNuGet("FileSizeFromBase64.NET", "2.0.0").WithId("v2.0.0"));
+ AddJob(baseJob.WithNuGet("FileSizeFromBase64.NET", "2.0.1").WithId("v2.0.1"));
AddDiagnoser(MemoryDiagnoser.Default);
SummaryStyle = SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage);
diff --git a/tests/FileSizeFromBase64.NET.ConsoleApp/FileSizeFromBase64.NET.ConsoleApp.csproj b/tests/FileSizeFromBase64.NET.ConsoleApp/FileSizeFromBase64.NET.ConsoleApp.csproj
index 238b43a..d614938 100644
--- a/tests/FileSizeFromBase64.NET.ConsoleApp/FileSizeFromBase64.NET.ConsoleApp.csproj
+++ b/tests/FileSizeFromBase64.NET.ConsoleApp/FileSizeFromBase64.NET.ConsoleApp.csproj
@@ -1,14 +1,14 @@
-
- Exe
- net10.0
- enable
- enable
-
+
+ Exe
+ net10.0
+ enable
+ enable
+
-
-
-
+
+
+
-
+
\ No newline at end of file
diff --git a/tests/FileSizeFromBase64.NET.Tests/FileSizeFromBase64.NET.Tests.csproj b/tests/FileSizeFromBase64.NET.Tests/FileSizeFromBase64.NET.Tests.csproj
index b171a80..2df0ef6 100644
--- a/tests/FileSizeFromBase64.NET.Tests/FileSizeFromBase64.NET.Tests.csproj
+++ b/tests/FileSizeFromBase64.NET.Tests/FileSizeFromBase64.NET.Tests.csproj
@@ -1,35 +1,35 @@
-
- net10.0
- enable
+
+ net10.0
+ enable
- false
- $(NoWarn);CA1707
-
+ false
+ $(NoWarn);CA1707
+
-
-
-
-
- runtime; build; native; contentfiles; analyzers; buildtransitive
- all
-
-
- runtime; build; native; contentfiles; analyzers; buildtransitive
- all
-
-
-
+
+
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+
-
-
-
+
+
+
-
-
- Always
-
-
+
+
+ Always
+
+
-
+
\ No newline at end of file